4 ms·
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?
by sureglymop 4y ago
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?
- wbl 4y agoMan 3 exec
- saagarjha 4y agoargv[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.
- hhvn 4y agoSee 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.
- sureglymop 4y agoInteresting, 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].
- tyingq 4y agoThe 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