News for hackers. Stuff that matters.
Re:C stdlib isn't threadsafe and even safe Rust didn't save us by stouset on Thursday January 23, 2025 @12:11AM (#42800909)
> As far as I'm concerned, the environment is a read-only input parameter set on process creation like argv.

This holds for a lot of programs, but what if you're writing a shell?

Re:C stdlib isn't threadsafe and even safe Rust didn't save us by tsimionescu on Thursday January 23, 2025 @12:44AM (#42801078)
Even then, you could maintain a separate copy of the environment that you control and freely mutate. Basically, during startup, you create a copy of the env you received. Any setenv primitive you expose to users will modify this copy (that you can sync properly yourself). When you want to launch a process, you explicitly provide the internal copy of the env to that process, you don't rely on libc providing its own copy.

Of course, this means you won't see any changes to env vars from libraries you may use that call setenv(), but you also shouldn't need, or want, that in a shell.

I still think having a proper synchronous thread safe setenv()/getenv() in libc is the better choice.

Re:C stdlib isn't threadsafe and even safe Rust didn't save us by GoblinSlayer on Thursday January 23, 2025 @04:19AM (#42802258)
It doesn't look like there's any incentive to change it, e.g. getenv_r is an unpopular function.
Re:C stdlib isn't threadsafe and even safe Rust didn't save us by dzaima on Thursday January 23, 2025 @12:54AM (#42801117)
If you're writing a shell, you can spend the 15 minutes to write a custom mutable data structure for your envvars; no need to significantly worsen the entire ecosystem to reduce the size of shells by a couple dozen lines (or, rather, move those lines into libc..)
Re:C stdlib isn't threadsafe and even safe Rust didn't save us by harrall on Thursday January 23, 2025 @02:13AM (#42801518)
I’ve written a lot of subprocess runners and environmental variables passed to a sub-process is just data at that point and you store it in your own variable like you would store someone’s name or someone’s age.