Re:Unix's special way of marking login shells goes back to V...
by sureglymop on Sunday December 11, 2022 @08:33AM
(#33943006)
But how does it work? How is the program started if there has to be a - infront of the name? How do you set argv[0] when executing something?
[ Parent
| on HN ]
Re:Unix's special way of marking login shells goes back to V...
by hhvn on Sunday December 11, 2022 @08:40AM
(#33943072)
See the exec family of functions, e.g. execl:
int execl(const char *pathname, const char *arg, ...);
Normally you would call it like execl("program", "program", "argument", NULL), duplicating the program's name/path, but it's possible to set the 2nd argument, which corresponds to argv[0] (in the case of execl), to something different. This behaviour just isn't always exposed in the shell and other languages.
Re:Unix's special way of marking login shells goes back to V...
by sureglymop on Sunday December 11, 2022 @08:50AM
(#33943147)
Interesting, thank you! I don't know why i had it stuck in my head that you would provide exec with the path and then the args starting from argv[1].
Re:Unix's special way of marking login shells goes back to V...
by tyingq on Sunday December 11, 2022 @08:43AM
(#33943091)
The command line version of exec offers "-a name", and the various library/syscall forms allow you to specify argv[0] separately from the path of the binary to exec.
You can set it yourself within a running program as well, like:
perl -e '$0="testme";while(1){sleep 1}' &
ps -ef | grep testme
Re:Unix's special way of marking login shells goes back to V...
by saagarjha on Sunday December 11, 2022 @08:37AM
(#33943035)
argv[0] is independent of the executable path. In fact there’s a common “trick” on Unix systems where you offer one binary and it figures out what it is supposed to be by peeking at argv[0]. Also, you’re allowed to freely modify argv, so if anyone reads it by scanning your stack it’ll get the new value.
Re:Unix's special way of marking login shells goes back to V...
by bonzini on Sunday December 11, 2022 @09:12AM
(#33943337)
But that trick is usually done with symbolic links, so in that case argv[0] _is_ related to the executable path.
Re:Unix's special way of marking login shells goes back to V...
by jmmv on Sunday December 11, 2022 @10:55AM
(#33944227)
Hard links, not symbolic links.
Re:Unix's special way of marking login shells goes back to V...
by rollcat on Sunday December 11, 2022 @11:13AM
(#33944414)
Either works.