3 ms·
Mutating argv is fine for how it is usually done. That is, to permute the arguments in a getopt() call so that all nonoptions are at the end. It is fine becaus
by GuB-42 2y ago
Mutating argv is fine for how it is usually done. That is, to permute the arguments in a getopt() call so that all nonoptions are at the end.
It is fine because it is usually done during the initialization phase, before starting any other thread. setenv() can be used here too, though I prefer to avoid doing that in any case. I also prefer not to touch argv, but since that's how GNU getopt() works, I just go with it.
Once the program is running and has started its threads, I consider setenv() is a big no no. The Rust documentation agrees with me: "In multi-threaded programs on other operating systems, the only safe option is to not use set_var or remove_var at all.". Note: here, "other operating systems" means "not Windows".
- saagarjha 2y agoA big reason to mutate argv is to change the process's name for tools like top.
- GoblinSlayer 2y agoFor that you write to /proc/self/comm, that's where top gets it from.
- saagarjha 2y agoHmm, htop then? Whatever is reading /proc/self/cmdline
- gobblegobble2 2y agoIt may work for top, but not ps among others. The only reliable way is clobbering argv. That's just the way it is. In my opinion, glibc should finally provide setproctitle(), so programs like postgresql or chrome (https://source.chromium.org/chromium/chromium/src/+/main:base/process/set_process_title_linux.cc;l=77 https://source.chromium.org/chromium/chromium/src/+/main:bas...) don't have to resort to argv hacks.
- GoblinSlayer 2y agoThere's prctl https://man7.org/linux/man-pages/man2/PR_SET_NAME.2const.html https://man7.org/linux/man-pages/man2/PR_SET_NAME.2const.htm... that writes to /proc/self/comm
- eadler 2y agoThis isn't true for the majority of operating systems.