-
Notifications
You must be signed in to change notification settings - Fork 252
Description
For an project I've been working on, I've found it would be handy if the read-only functions public functions in libsubid were thread-safe to call. I can protect my own calls to the library with a mutex, but that doesn't protect against issues if my code gets included as part of another application that calls into libsubid without using that mutex. I suppose that case is a bit contrived, so my mutex workaround seems workable for now, but I'd prefer for this to eventually be bulletproof.
Factors I've found so far that could affect thread safety:
-
subordinate_uid_dbandsubordinate_gid_db(subordinateio.cpp) keep a fair amount of mutable state with no thread synchronization.Just making them thread-local might be enough to solve that issue, although that would technically be a breaking change. I'm not seeing any thread-locals elsewhere in the source, so I'm not sure if there would be compatibility issues with them on some platform that the project actively supports. It looks like the locking logic might also need some revamping if these were thread-local since it seems to be written with the assumption that there will be one lock per process.
Putting a mutex around them might also be an option as long as all supported platforms have support for C11 mutexes. Alternatively, if the read-only functions used their own instances of
commonio_db, we could skip some of the locking concerns. -
The static
lock_countandnscd_need_reloadvariables incommonio.caren't thread-safe. -
The various calls to
getpwnam, etc. would have to be switched to usegetpwnam_r.
If there's general interest in making the library thread-safe where feasible, I can dedicate some time to reading through the code more thoroughly and seeing if I can get some reasonable patches together.