|
Home > Archive > Unix Programming > November 2005 > argv[0] in execvp()
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
argv[0] in execvp()
|
|
| Alex Vinokur 2005-11-26, 2:50 am |
|
QUOTES from http://www.mkssoftware.com/docs/man3/execl.3.asp
----------------------------------------------------
int execvp(const char *file, char *const argv[])
argv
Is the argument list for the new process image. This should contain an
array of pointers to character strings, and the array should be
terminated by a NULL pointer. The value in argv[0] should point to a
file name that is associated with the process being started by the
exec() function.
-----------------------------------------------------
Why should 'argv[0]' point to a file name?
Is not it enough the 'file' parameter to execute the program?
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn
| |
| Paul Pluzhnikov 2005-11-26, 2:50 am |
| "Alex Vinokur" <alexvn@users.sourceforge.net> writes:
> Why should 'argv[0]' point to a file name?
Convention.
> Is not it enough the 'file' parameter to execute the program?
It is.
Cheers,
--
In order to understand recursion you must first understand recursion.
Remove /-nsp/ for email.
| |
| Lew Pitcher 2005-11-26, 2:50 am |
| -----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Alex Vinokur wrote:
> QUOTES from http://www.mkssoftware.com/docs/man3/execl.3.asp
>
> ----------------------------------------------------
> int execvp(const char *file, char *const argv[])
>
> argv
> Is the argument list for the new process image. This should contain an
> array of pointers to character strings, and the array should be
> terminated by a NULL pointer. The value in argv[0] should point to a
> file name that is associated with the process being started by the
> exec() function.
> -----------------------------------------------------
>
>
> Why should 'argv[0]' point to a file name?
> Is not it enough the 'file' parameter to execute the program?
Nope. The file parameter is (in effect) used by the loader to load the binary,
but the argv[] parameter is (in effect) used by the linker to pass information
to the running binary.
While the manpage says to give the filename as argv[0], consider that some
times it is useful to pass other information as argv[0]. For instance,
argv[0] = "-";
argv[1] = NULL;
execvp("/bin/bash",argv);
Take a look at your login shell through ps. See the similarity?
- --
Lew Pitcher
Master Codewright & JOAT-in-training | GPG public key available on request
Registered Linux User #112576 (http://counter.li.org/)
Slackware - Because I know what I'm doing.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.7 (GNU/Linux)
iD8DBQFDh/ ilagVFX4UWr64RAgSbAKCE4obSv0vt2bRJo4dsUE
sv7c7a/QCfTvTS
c7rNoxzKbY38b978uWGQQXw=
=NG3B
-----END PGP SIGNATURE-----
| |
| Paul Pluzhnikov 2005-11-26, 2:50 am |
| Lew Pitcher <lpitcher@sympatico.ca> writes:
>
> Nope. The file parameter is (in effect) used by the loader to load the binary,
> but the argv[] parameter is (in effect) used by the linker to pass information
> to the running binary.
Huh? Which linker are you talking about?
After the program is running, linker is not involved *at all*.
The runtime loader is involved, but it doesn't care about agrv[0]
on any UNIX I know.
> While the manpage says to give the filename as argv[0], consider that some
> times it is useful to pass other information as argv[0]. For instance,
>
> argv[0] = "-";
> argv[1] = NULL;
> execvp("/bin/bash",argv);
>
> Take a look at your login shell through ps. See the similarity?
So you've just confirmed that argv[0] does *not* need to be the
file name, contradicting your own earlier "Nope" statement.
Yes, exec()ed programs themselves could well care about values
of argv[0], argv[1], etc. They could also care about time of day,
or values of environment variables. But none of this is "required
to execute the program" (i.e. exec() it and get it to its entry
point).
Cheers,
--
In order to understand recursion you must first understand recursion.
Remove /-nsp/ for email.
| |
| Gordon Burditt 2005-11-26, 5:50 pm |
| >argv
>Is the argument list for the new process image. This should contain an
>array of pointers to character strings, and the array should be
>terminated by a NULL pointer. The value in argv[0] should point to a
>file name that is associated with the process being started by the
>exec() function.
>-----------------------------------------------------
>
>
>Why should 'argv[0]' point to a file name?
Because some programs look at it and change their behavior based
on what they see there. For example, "cp" and "mv" may be
the same program and behave differently based on how they are called.
>Is not it enough the 'file' parameter to execute the program?
If you don't care whether it misbehaves when you run it, you
can pass anything. The program will still run.
Some (idiotic) programs attempt to find their other pieces (config
files, data, etc.) based on the directory of argv[0]. This approach
often involves requiring the installation directory for the executables
be writable by the user (so the app can put data files there, too),
which encourages viruses to tamper with or replace them.
Some programs attempt to find their own executable to read their
own symbol table, often to avoid coding a command lookup table,
so the user can type something like "qsort" and crash the program.
Reasons for passing strange stuff in argv[0]:
- To hide (poorly) what you are doing from people running "ps"
- To exploit security holes in setuid programs.
- To force programs using a stupid method of finding their config
files to use an alternate config file (often to break security).
Gordon L. Burditt
| |
| Alex Vinokur 2005-11-27, 5:53 pm |
|
"Gordon Burditt" <gordonb.4a1p6@burditt.org> wrote in message news:11ohbueegkqnua8@corp.supernews.com...
[snip]
> Reasons for passing strange stuff in argv[0]:
>
> - To hide (poorly) what you are doing from people running "ps"
What is the relationship between "ps" and argv[0]?
> - To exploit security holes in setuid programs.
Similar question: what is the relationship between setuid programs and argv[0]?
[snip]
--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn
| |
| Måns Rullgård 2005-11-27, 5:53 pm |
| "Alex Vinokur" <alexvn@x-privat.org> writes:
> "Gordon Burditt" <gordonb.4a1p6@burditt.org> wrote in message news:11ohbueegkqnua8@corp.supernews.com...
> [snip]
>
> What is the relationship between "ps" and argv[0]?
ps usually displays whatever is in argv[0] as the process name. If
you're doing something nasty, setting argv[0] to something
innocent-looking might delay detection.
>
> Similar question: what is the relationship between setuid programs
> and argv[0]?
Some programs do different things depending on the value of argv[0].
Passing a bizarre value might trigger a bug such as a buffer overflow.
This is obviously worse if it happens to a setuid program.
--
Måns Rullgård
mru@inprovide.com
|
|
|
|
|