From owner-freebsd-hackers@FreeBSD.ORG Tue Aug 22 14:09:49 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 DF52216A4DA for ; Tue, 22 Aug 2006 14:09:49 +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 7BF4D43D53 for ; Tue, 22 Aug 2006 14:09:48 +0000 (GMT) (envelope-from mwm-keyword-freebsdhackers2.e313df@mired.org) Received: (qmail 8010 invoked by uid 1001); 22 Aug 2006 14:09:35 -0000 Received: by bhuda.mired.org (tmda-sendmail, from uid 1001); Tue, 22 Aug 2006 10:09:35 -0400 (EDT) MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: quoted-printable Message-ID: <17643.4127.339206.625625@bhuda.mired.org> Date: Tue, 22 Aug 2006 10:09:35 -0400 To: des@des.no (Dag-Erling =?iso-8859-1?Q?Sm=F8rgrav?=) In-Reply-To: <8664glnfhw.fsf@xps.des.no> References: <017601c6c486$6477c370$0a0aa8c0@rivendell> <17640.52899.432083.511555@bhuda.mired.org> <86sljqnzbz.fsf@xps.des.no> <17642.8303.746281.383448@bhuda.mired.org> <8664glnfhw.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: Tue, 22 Aug 2006 14:09:50 -0000 In <8664glnfhw.fsf@xps.des.no>, Dag-Erling Sm=F8rgrav type= d: > Mike Meyer writes: > > In <86sljqnzbz.fsf@xps.des.no>, Dag-Erling Sm=F8rgrav = 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 shou= ldn't > > > unless you've called chdir(2), chroot(2) or jail(2) earlier in th= e > > > process, or the executable was moved or removed) > > No, I got it the right way 'round. If the shell walks the PATH, the= n > > calling realpath(3) on argv[0] is the wrong thing to do, as it'll > > resolve the path relative to the pwd. >=20 > but argv[0] is either an absolute path or a path relative to pwd, > unless your shell is broken. >=20 > des@xps ~% cat >bin/foo > #!/bin/sh > echo $0 $@ > des@xps ~% chmod a+rx bin/foo > des@xps ~% ./bin/foo > ./bin/foo > des@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 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%=20 Changing shells to /bin/sh or bin/csh doesn't change things. =09=09=09http://www.mired.org/consulting.html Independent Network/Unix/Perforce consultant, email for more informatio= n.