Combining the above /proc based with the answers to this question about searching a directory tree for symlinks gives the following:
# find /proc -ignore_readdir_race -name task -prune -o -path '*/fd/*' -lname '/file/of/interest' -ls -quit
37023 0 l-wx------ 1 username username 64 Dec 16 09:06 /proc/4140/fd/1 -> /file/of/interest
The -ignore_readdir_race
avoids errors due to short lived processes, the -prune
gets rid of task level results (not strictly necessary when stopping at the first process, since those paths would still give the same answer, but useful to avoid redundancy in other use cases, like listing all the results)
Combine -print
with cut
to extract the pid directly:
# find /proc -ignore_readdir_race -name task -prune -o -path '*/fd/*' -lname '/file/of/interest' -print -quit | cut -d / -f 3
4140
Once you have the pid, you can use ps to get the process details (as per the answers to this SuperUser question):
# ps -p 4140 -o command=
/some/command --with=some args
(Note, I tested this on Linux, and it should work the same on BSD, but I'm not 100% sure about Android)