0

In

#include <unistd.h>
int execve(const char * pathname , char *const  argv [], char *const  envp []);

argv [0] of execve corresponds to argv[0] in the main function of the new program, and is thus typically the same as the basename component of the pathname argument. Is it a requirement that we should always follow?

When a program file can be invoked by different values of argv[0], does that rule/convention mean that when I invoke a program file and want argv[0] of its main() to have a specific value, I have to create a symlink to the program file named after the value of argv[0], and invoke the program by the symlink? Or not necessarily? For example, this answer to “Why does argv include the program name?” says

Bash runs in POSIX mode when argv[0] is sh. It runs as a login shell when argv[0] begins with -.

Does that mean that

  • to invoke bash in POSIX mode, I have to create a symlink /path/to/sh to /bin/bash and invoke bash by execve("/path/to/sh", argv, ...) with argv[0] pointing to string "sh"? Is this way preferrable over execve("/bin/bash", argv, ...) where argv[1] points to string "--posix" and argv[0] doesn't matter?

  • to invoke bash as a login shell, I have to create a symlink /path/to/- to /bin/bash and invoke bash by execve("/path/to/-", argv, ...) with argv[0] pointing to string "-"? Is this way preferable over execve("/bin/bash", argv, ...) where argv[1] points to string "--login" and argv[0] doesn't matter?

When is it useful that argv [0] of execve is not the same as the basename component of the pathname argument?

7
  • What question are you getting at here? Besides the title, you have eight questions in the Question. You don't invoke bash from the command-line via execve(), so I don't know which way you're going with this question.
    – Jeff Schaller
    Nov 28, 2018 at 18:59
  • Number of question marks doesn't matter. To entertain you, I can end every sentence with a question mark. If I know my answer clearly, I wouldn't post my confusion. If my confusion confuses you, you don't understand that I am asking questions.
    – Tim
    Nov 28, 2018 at 19:13
  • Just my opinion, but your Question diverges between using a system call execve() and using symlinks to invoke bash. Maybe I'm not smart enough to understand where you're going.
    – Jeff Schaller
    Nov 28, 2018 at 19:22
  • It is more about acceptance than smartness.
    – Tim
    Nov 28, 2018 at 19:23
  • 1
    Jeff’s not being rude. After all the time you’ve been using the site, it seems reasonable to expect that you would have understood by now that it works best when a question is a single question, and not a number of more-or-less related questions. Especially in this case since at least some of your questions can be answered from the linked question. Nov 28, 2018 at 21:21

1 Answer 1

3

The accepted answer to the question you linked to says

To begin with, note that argv[0] is not necessarily the program name.

which addresses your main question.

Your various questions about starting a program seem to stem from a misunderstanding. You don’t need to use links (symbolic or hard) to start a program with argv[0] set to a value other than its name; you need to be able to control the array given to execve. This is what login does when it starts the shell: it prefixes it with - in argv[0].

Bash starts in POSIX mode when its argv[0] is sh, so that it can be used as /bin/sh. If that required using --posix, you’d need to change a lot of shell scripts.

The same reasoning applies to using - for a login shell: this convention works with any shell, whether it supports a --login option or not.

The login use-case is an example of when it’s useful for argv[0] to not be equal to the program’s name.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .