Re:C stdlib isn't threadsafe and even safe Rust didn't save us
by saagarjha on Thursday January 23, 2025 @04:14AM
(#42802225)
A big reason to mutate argv is to change the process's name for tools like top.
Re:C stdlib isn't threadsafe and even safe Rust didn't save us
by GoblinSlayer on Thursday January 23, 2025 @04:45AM
(#42802432)
For that you write to /proc/self/comm, that's where top gets it from.
Re:C stdlib isn't threadsafe and even safe Rust didn't save us
by gobblegobble2 on Thursday January 23, 2025 @07:15AM
(#42803242)
It 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:bas...) don't have to resort to argv hacks.
Re:C stdlib isn't threadsafe and even safe Rust didn't save us
by GoblinSlayer on Friday January 24, 2025 @10:29AM
(#42814018)
There's prctl https://man7.org/linux/man-pages/man2/PR_SET_NAME.2const.htm... that writes to /proc/self/comm
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".