*BSD News Article 39099


Return to BSD News archive

Path: sserve!newshost.anu.edu.au!harbinger.cc.monash.edu.au!msunews!uwm.edu!news.alpha.net!news.mathworks.com!noc.near.net!shore.shore.net!rwwa.com!witr
From: witr@rwwa.com (Robert Withrow)
Newsgroups: comp.os.386bsd.development
Subject: Answer: How to find the filename of the binary executable...
Date: 8 Dec 1994 04:09:52 GMT
Organization: North Shore Access/Eco Software, Inc; (info@shore.net)
Lines: 21
Distribution: world
Message-ID: <3c60ug$cup@shore.shore.net>
References: <3c35e2$6sv@shore.shore.net> <3c3d5b$kor@dagny.galt.com> <3c5p7q$693@shore.shore.net>
Reply-To: witr@rwwa.com
NNTP-Posting-Host: rwwa.com

As several people have noted (correctly) that it is relatively straight
forward to find the *inode* of the file being executed, but not the
pathname.  This I knew.  I also don't know of any easy or portable way
determine a pathname from an inode number.

But Sean McDermott deserves credit for proving that I was way too
hasty in rejecting the contents argv[0] as usefull.  He pointed
out to me (in private email) that one can, by starting with
argv[0] and by iteratively stat()ing and examining the first few
bytes (the magic number) of the files you get doing this (and
interpreting the contents of the beginning of interpreted files),
one can essentially do what exec() and the kernel does in activating
a program, or what file(1) does.

Of course, a short perusal of /etc/magic would convice anyone that
this method is not entirely without risk... ;-)  And one is left
with the task of parsing the begining of interpreted files.

-- 
 Robert Withrow, Tel: +1 617 598 4480, Fax: +1 617 598 4430
 R.W. Withrow Associates, 319 Lynnway, Lynn MA 01901 USA, Net: witr@rwwa.COM