#!/usr/bin/env bash

PID="$1"

if [[ -z "$PID" ]]; then
    echo "usage: $0 <pid>"
    exit 1
fi

FD_DIR="/proc/$PID/fd"

if [[ ! -d "$FD_DIR" ]]; then
    echo "process not found or no permission"
    exit 1
fi

for fd in "$FD_DIR"/*; do
    [[ -e "$fd" ]] || continue

    FD_NUM=$(basename "$fd")

    TARGET=$(readlink "$fd" 2>/dev/null)

    SIZE=$(stat -c%s "$fd" 2>/dev/null)

    echo "fd=$FD_NUM size=$SIZE target=$TARGET"
done
