From owner-freebsd-hackers@FreeBSD.ORG Mon Aug 21 21:07:07 2006 Return-Path: X-Original-To: freebsd-hackers@freebsd.org Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 6C2E416A4DD for ; Mon, 21 Aug 2006 21:07:07 +0000 (UTC) (envelope-from mwm-keyword-freebsdhackers2.e313df@mired.org) Received: from mired.org (vpn.mired.org [66.92.153.74]) by mx1.FreeBSD.org (Postfix) with SMTP id 5C65943D49 for ; Mon, 21 Aug 2006 21:07:06 +0000 (GMT) (envelope-from mwm-keyword-freebsdhackers2.e313df@mired.org) Received: (qmail 11298 invoked by uid 1001); 21 Aug 2006 21:06:56 -0000 Received: by bhuda.mired.org (tmda-sendmail, from uid 1001); Mon, 21 Aug 2006 17:06:55 -0400 (EDT) MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: quoted-printable Message-ID: <17642.8303.746281.383448@bhuda.mired.org> Date: Mon, 21 Aug 2006 17:06:55 -0400 To: des@des.no (Dag-Erling =?iso-8859-1?Q?Sm=F8rgrav?=) 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> X-Mailer: VM 7.17 under 21.4 (patch 19) "Constant Variable" XEmacs Lucid X-Primary-Address: mwm@mired.org X-face: "5Mnwy%?j>IIV\)A=):rjWL~NB2aH[}Yq8Z=u~vJ`"(,&SiLvbbz2W`; h9L,Yg`+vb1>RG% *h+%X^n0EZd>TM8_IB;a8F?(Fb"lw'IgCoyM.[Lg#r\ X-Delivery-Agent: TMDA/1.0.3 (Seattle Slew) From: Mike Meyer Cc: Reko Turja , freebsd-hackers@freebsd.org Subject: Re: Aqcuiring full path to running process from outside the kernel X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 21 Aug 2006 21:07:07 -0000 In <86sljqnzbz.fsf@xps.des.no>, Dag-Erling Sm=F8rgrav type= d: > Mike Meyer 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=09=09http://www.mired.org/consulting.html Independent Network/Unix/Perforce consultant, email for more informatio= n.