Re: Aqcuiring full path to running process from outside the kernel



In <86sljqnzbz.fsf@xxxxxxxxxx>, Dag-Erling Smørgrav <des@xxxxxx> typed:
Mike Meyer <mwm-keyword-freebsdhackers2.e313df@xxxxxxxxx> writes:
However, you can also make a best effort. Check argv[0] 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[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. In particular, since realpath(3)
doesn't care if the last component of the path actually exists or not,
any argv[0] 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[0] 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
--
Mike Meyer <mwm@xxxxxxxxx> http://www.mired.org/consulting.html
Independent Network/Unix/Perforce consultant, email for more information.
_______________________________________________
freebsd-hackers@xxxxxxxxxxx mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
To unsubscribe, send any mail to "freebsd-hackers-unsubscribe@xxxxxxxxxxx"



Relevant Pages

  • Re: Aqcuiring full path to running process from outside the kernel
    ... have an absolute path. ... do what the shell would do to find the ... fall back to using $PATH only if realpathfails (which it shouldn't ... Dag-Erling Smørgrav - des@xxxxxx ...
    (freebsd-hackers)
  • Re: [opensuse] What is the name ?
    ... No absolute path found for shell: ... xterm -geomtry <geometry> ...
    (SuSE)
  • Re: os.path.join
    ... Elliot Peele wrote: ... absolute path, all previous components (on Windows, including the ... It's behavior is exactly the same as if you did a series of "cd" commands ... at the shell with the same parameters. ...
    (comp.lang.python)
  • Re: [opensuse] What is the name ?
    ... No absolute path found for shell: ... xterm -geomtry <geometry> ...
    (SuSE)
  • Re: Executable programs
    ... My example had no shebang. ... until one recognizes the header of the file, loads it, and executes it. ... fs/binfmt_aout.c takes care of a.out format binaries ... then the shell may decide to try to interpret the file ...
    (comp.os.linux.setup)