This page describes the guaranteed behavior of the functions readdir and readdir_r and contains the following sentence:
After a call to fork(), either the parent or child (but not both) may continue processing the directory stream using readdir(), rewinddir() or seekdir(). If both the parent and child processes use these functions, the result is undefined.
I'm writing a fork-safe wrapper around the DIR*-related family of functions as an exercise and I'm wondering whether it's safe to closedir the directory stream in both the parent and the child simultaneously or whether the process that doesn't continue reading from the directory stream isn't supposed to interact with the directory stream at all.
forkhandler and force the user to pick whenopendiring the directory whether the parent or child inherits it on fork. My wrapper library that doesn't exist yet will promise nothing if you use the unwrapped library calls in addition to the wrapper. – Greg Nisbet Mar 22 '19 at 02:00ptracehere, the fds are shared becauseopendirwas called beforefork. The relevant scenario isforkwithoutexec, so the execution context is identical in the parent and child. – Stephen Kitt Mar 22 '19 at 06:10