Synchronized FFI access to POSIX environment variable functions

I feel like I've said this several times already but: it is not possible to define a locking scheme internal to the existing set of POSIX environment variable access APIs that will make everything fully safe. This is primarily because environ and the third argument to C main allow applications to bypass whatever locking may exist internally, and secondarily because getenv doesn't copy the string it returns (I presume that's what getenv_r is meant to address).

To fix the problem "properly", the C library would have to expose the pthread_rwlock_t object that it uses internally, and document that all operations on environ (except for passing it to exec* functions after fork, because then you're single threaded anyway) must take the lock appropriately. Exposure of internal C library lock objects is unprecedented. I don't even know how to speculate how the committees would react.

5 Likes