Return to BSD News archive
Newsgroups: comp.os.386bsd.development Path: sserve!newshost.anu.edu.au!harbinger.cc.monash.edu.au!msunews!uwm.edu!vixen.cso.uiuc.edu!howland.reston.ans.net!news.sprintlink.net!news.onramp.net!lerami!rwsys!hammy!gordon From: gordon@sneaky.lonestar.org (Gordon Burditt) Subject: Re: How to find the filename of the binary executable... Organization: /usr/lib/news/organi[sz]ation Message-ID: <D0z930.2rw@sneaky.lonestar.org> References: <3c35e2$6sv@shore.shore.net> Date: Sat, 17 Dec 1994 22:57:00 GMT Lines: 30 >Can someone tell me how to find the filename of the *binary* >file being executed (on, say, FreeBSD 1.1.5.1, or sunos.recent)? > >P.S.: if your answer is argv[0], you don't understand the >question... You don't. Especially if there is a security issue involved (for example, the program is setuid or setgid), and someone has something to gain by spoofing it. *THE* filename? Who said there was only one? Who said the name at the time of the exec() still points at the same file now? argv[0] might be missing, a convincing lie, or garbage. The $PATH you inherited might have nothing to do with the $PATH used to find the executable. In practice, you can get about 95% of the cases with: 1. If argv[0] begins with a /, use it. 2. If argv[0] contains a /, prepend the current working directory at the time of the exec() and a /, and use it. 3. Otherwise, try prepending each component of the PATH and a / to argv[0] in turn, and checking for the existence of a file with execute permissions. The first one you find, use it. 4. At this point you're down to the other 5%, and you don't have an answer. Gordon L. Burditt sneaky.lonestar.org!gordon