home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!dtix!darwin.sura.net!jvnc.net!princeton!stroke.Princeton.EDU!spencer
- From: spencer@stroke.Princeton.EDU (S. Spencer Sun)
- Newsgroups: comp.unix.programmer
- Subject: Re: How can a program know where it was invoked?
- Message-ID: <1992Aug15.022032.13233@Princeton.EDU>
- Date: 15 Aug 92 02:20:32 GMT
- References: <bmckeon.713727455@unix1.tcd.ie> <1992Aug13.182509.10985@nic.umass.edu> <1992Aug14.183521.1704@sco.com>
- Sender: news@Princeton.EDU (USENET News System)
- Reply-To: spencer@phoenix.princeton.edu (S. Spencer Sun)
- Organization: Live Organ Transplants
- Lines: 47
- Nntp-Posting-Host: stroke.princeton.edu
-
- In article <1992Aug14.183521.1704@sco.com>, brianm@sco.com (Brian Moffet) writes:
- >Not necessarily. Under the Unix OS, you can do an attempt at figuring
- >out where the binary is:
- >
- >1: check to see if the argv[0] is an absolute/relative path name.
- >2: check to see if your psargs have any good information (may not be doable)
- >3: get the environment variable PATH and go through it in order
- > checking to see whether the program in question exists in that directory.
- >4: Check to see if there is your program name in the current directory.
- >
- >If you find that the file you found was actually a symbolic link,
- >you have to trace that link to see if it ends with a valid executable.
- >
- >If all of these fail, you have a program which someone changed the name on
- >via execl() or something similar. In this case, you're not going to have
- >much luck.
-
- Just to clarify, I don't think this is really what Mr. Moffet meant, but
- IMHO the way his article is written implies that the very last paragraph
- quoted above is true only if checks 1-4 fail and this is not true.
- Suppose I execl something with argv[0] = "/bin/zsh" even though what I'm
- really running is "~spencer/zsh-beta/zsh"? Condition 1 is satisifed,
- condition 3 could be satisfied, condition 4 could be satisfied if I'm in
- any directory with a copy of zsh in it, and maybe condition 3 could be
- satisfied if I knew what psargs was. They could all be symbolic links,
- too, but they'd all still be wrong.
-
- OK, any situation where this fails is likely to be extremely artificial,
- but then again, (1) I could say I'm looking at it from a theoretical
- standpoint (read: I'm just being argumentative); and (2) making a
- symbolic link named '-i' to a setuid shell script is pretty artificial
- too.
-
- >My question would be, "why would you want to know where the program is in the
- >first place?" Most cases of having some startup script can be solved by
- >placing the startup script (or whatever) someplace sane, not where the
- >directory is...
-
- I agree with this though. I would imagine that in general there is some
- other way of achieving the desired effect. (Which isn't to say
- necessarily that there SHOULDN'T be a reliable way for an executable to
- find out exactly where it resides, but.)
-
- ----------- The opinions expressed in this article are solely mine. -----------
- <Insert lame attempt at disclaimer humor>
- sss/PU'94 Dept of CS (spencer@phoenix.princeton.edu)/JvNCnet (spencer@jvnc.net)
- "'Rue the day'? Who talks like that?"
-