l i n u x - u s e r s - g r o u p - o f - d a v i s
Next Meeting:
July 7: Social gathering
Next Installfest:
Latest News:
Jun. 14: June LUGOD meeting cancelled
Page last updated:
2004 Aug 27 23:28

The following is an archive of a post made to our 'vox-tech mailing list' by one of its subscribers.

Report this post as spam:

(Enter your email address)
Re: [vox-tech] Identifying the directory that containsthe currently running executable?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [vox-tech] Identifying the directory that containsthe currently running executable?

--On Friday, August 27, 2004 11:36:50 AM -0700 Bill Kendrick <nbs@sonic.net> wrote:

The program can examine argv[0], but
that may contain only the program name without the directory path, in
which case you would generally read the PATH environment variable and
look for the program in each directory listed. It's also possible for
argv[0] to contain an irrelevant or misleading string, so you
shouldn't  depend on it if security is an issue.
Such as an alias, for example?  (I suppose I could try testing...)
The system call to execute a new program is called execve(). The shell, the perl exec command, the other libc exec* functions, etc. all end up calling execve() at some point.

Execve() takes three arguments:

1) The name of the program to execute.

2) An array of argument strings. These become the argv array for the new process.

3) An array of environment strings. These become the new process's environment.

When you call execve() yourself, e.g. from inside a C program, you can put anything you want in the argument and environment arrays. The kernel doesn't care if the name of the program to execute matches the first entry in the argument array. It doesn't care where the strings in the environment array came from.

All that stuff about argv[0] being the name of the process, and processes inheriting their environment from their parent process? It's all just convention. Shells follow these rules when they start subprocesses, so users and programs come to expect these rules, so other programs that start subprocesses follow the same rules unless there's a good reason not to.

Ken Herron
vox-tech mailing list

LUGOD Group on LinkedIn
Sign up for LUGOD event announcements
Your email address:
LUGOD Group on Facebook
'Like' LUGOD on Facebook:

Hosting provided by:
Sunset Systems
Sunset Systems offers preconfigured Linux systems, remote system administration and custom software development.

LUGOD: Linux Users' Group of Davis
PO Box 2082, Davis, CA 95617
Contact Us

LUGOD is a 501(c)7 non-profit organization
based in Davis, California
and serving the Sacramento area.
"Linux" is a trademark of Linus Torvalds.

Sponsored in part by:
Appahost Applications
For a significant contribution towards our projector, and a generous donation to allow us to continue meeting at the Davis Library.