Page MenuHomeFreeBSD

D46108.id141330.diff
No OneTemporary

D46108.id141330.diff

diff --git a/lib/libc/stdlib/exit.3 b/lib/libc/stdlib/exit.3
--- a/lib/libc/stdlib/exit.3
+++ b/lib/libc/stdlib/exit.3
@@ -29,7 +29,7 @@
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
-.Dd August 5, 2021
+.Dd July 24, 2024
.Dt EXIT 3
.Os
.Sh NAME
@@ -102,6 +102,18 @@
.Xr sysexits 3
may be used to provide more information to the parent process.
.Pp
+Calls to the
+.Fn exit
+function are serialized.
+All functions registered by
+.Xr atexit 3
+are executed in the first thread that called
+.Nm exit .
+If any other thread of the process calls
+.Nm exit
+before all registered functions have completed or before the process
+terminates, the thread is blocked until the process terminates.
+.Pp
Note that
.Fn exit
does nothing to prevent bottomless recursion should a function registered
diff --git a/lib/libc/stdlib/exit.c b/lib/libc/stdlib/exit.c
--- a/lib/libc/stdlib/exit.c
+++ b/lib/libc/stdlib/exit.c
@@ -31,6 +31,7 @@
#include "namespace.h"
#include <stdlib.h>
+#include <pthread.h>
#include <unistd.h>
#include "un-namespace.h"
@@ -48,6 +49,8 @@
*/
int __isthreaded = 0;
+static pthread_mutex_t exit_mutex = PTHREAD_MUTEX_INITIALIZER;
+
/*
* Exit, flushing stdio buffers if necessary.
*/
@@ -59,6 +62,10 @@
_thread_autoinit_dummy_decl = 1;
+ /* Make exit(3) thread-safe */
+ if (__isthreaded)
+ _pthread_mutex_lock(&exit_mutex);
+
/*
* We're dealing with cleaning up thread_local destructors in the case of
* the process termination through main() exit.

File Metadata

Mime Type
text/plain
Expires
Sat, Dec 20, 7:36 AM (18 h, 1 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
27093515
Default Alt Text
D46108.id141330.diff (1 KB)

Event Timeline