Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 21 Aug 2006 17:06:55 -0400
From:      Mike Meyer <mwm-keyword-freebsdhackers2.e313df@mired.org>
To:        des@des.no (Dag-Erling =?iso-8859-1?Q?Sm=F8rgrav?=)
Cc:        Reko Turja <reko.turja@liukuma.net>, freebsd-hackers@freebsd.org
Subject:   Re: Aqcuiring full path to running process from outside the kernel
Message-ID:  <17642.8303.746281.383448@bhuda.mired.org>
In-Reply-To: <86sljqnzbz.fsf@xps.des.no>
References:  <017601c6c486$6477c370$0a0aa8c0@rivendell> <17640.52899.432083.511555@bhuda.mired.org> <86sljqnzbz.fsf@xps.des.no>

next in thread | previous in thread | raw e-mail | index | archive | help
In <86sljqnzbz.fsf@xps.des.no>, Dag-Erling Sm=F8rgrav <des@des.no> type=
d:
> Mike Meyer <mwm-keyword-freebsdhackers2.e313df@mired.org> writes:
> > However, you can also make a best effort. Check argv[0] to see if y=
ou
> > have an absolute path. If not, do what the shell would do to find t=
he
> > 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.

=09<mike
--=20
Mike Meyer <mwm@mired.org>=09=09http://www.mired.org/consulting.html
Independent Network/Unix/Perforce consultant, email for more informatio=
n.



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?17642.8303.746281.383448>