Aqcuiring full path to running process from outside the kernel
mwm-keyword-freebsdhackers2.e313df at mired.org
Mon Aug 21 21:07:07 UTC 2006
In <86sljqnzbz.fsf at xps.des.no>, Dag-Erling Smørgrav <des at des.no> typed:
> Mike Meyer <mwm-keyword-freebsdhackers2.e313df at mired.org> writes:
> > However, you can also make a best effort. Check argv to see if you
> > have an absolute path. If not, do what the shell would do to find the
> > executable - look for it on $PATH. Once you've found it, call
> > realpath(3) to canonicalize the path.
> You got it the wrong way around. First pass argv to realpath(3),
> fall back to using $PATH only if realpath(3) fails (which it shouldn't
> unless you've called chdir(2), chroot(2) or jail(2) earlier in the
> process, or the executable was moved or removed)
No, I got it the right way 'round. If the shell walks the PATH, then
calling realpath(3) on argv is the wrong thing to do, as it'll
resolve the path relative to the pwd. In particular, since realpath(3)
doesn't care if the last component of the path actually exists or not,
any argv that doesn't have a '/' in it will be mapped to that name
in the current directory. That's assuming that the shell doesn't pass
an argv that's the full path to the executable; if the shell does
that, then my version still works. Since there are shells that don't
do that, you gotta walk the PATH before calling realpath(3) if the
shell walked the PATH.
Testing for absolute paths is the wrong way test for "did the shell
walk PATH", though. You should test for any occurrence of '/' instead,
but being absolutely right about it may involve changing the test to
match the shell being used.
Mike Meyer <mwm at mired.org> http://www.mired.org/consulting.html
Independent Network/Unix/Perforce consultant, email for more information.
More information about the freebsd-hackers