Aqcuiring full path to running process from outside the kernel
Mike Meyer
mwm-keyword-freebsdhackers2.e313df at mired.org
Tue Aug 22 14:09:50 UTC 2006
In <8664glnfhw.fsf at xps.des.no>, Dag-Erling Smørgrav <des at des.no> typed:
> Mike Meyer <mwm at mired.org> writes:
> > In <86sljqnzbz.fsf at xps.des.no>, Dag-Erling Smørgrav <des at des.no> typed:
> > > You got it the wrong way around. First pass argv[0] 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[0] is the wrong thing to do, as it'll
> > resolve the path relative to the pwd.
>
> but argv[0] is either an absolute path or a path relative to pwd,
> unless your shell is broken.
>
> des at xps ~% cat >bin/foo
> #!/bin/sh
> echo $0 $@
> des at xps ~% chmod a+rx bin/foo
> des at xps ~% ./bin/foo
> ./bin/foo
> des at xps ~% foo
> /home/des/bin/foo
That's happening because sh does the path search on argv[0] (if it
would have done it when you executed the command) and uses the result
instead of argv[0] for $0. If you try it in C, it doesn't work:
bhuda% cat >foo.c
#include <stdio.h>
main(int argc, char **argv) {
printf("%s\n", argv[0]) ;
}
bhuda% cc foo.c -o bin/foo
bhuda% ./bin/foo
./bin/foo
bhuda% foo
foo
bhuda%
Changing shells to /bin/sh or bin/csh doesn't change things.
<mike
--
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
mailing list