From nobody Mon Jul 29 23:59:01 2024 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4WXwLZ1fVjz5SFNN; Mon, 29 Jul 2024 23:59:02 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R11" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4WXwLY69Fwz4DYn; Mon, 29 Jul 2024 23:59:01 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1722297541; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=zLW8PhStRqGKC3U9x6jPPtCcV8pRThxhCJ0Fww73+XE=; b=F4oMCV7QWPPOOFJoXhg4ebX2vyPHezV3jMPorOnDkbk/tWQoCL+xLWLJHuz6/l2oxd2CwJ VX/tRnl2XjNs/EhQEhg117e1qa0h5kZXWVzwRCO3wzG9FwKFvDMwJovQJMIGQoeKfNU4st bceqbwFTv8Cye3LaTtlCi2HXX16dODpdptFLfvcPU+lKSQFEM58vZiSrSJzUjPcBIlWjzt HqM3MyJ40mjA4rz/cT//4P9R9U5sSRRZluOmPgScYHg3R+X/UbkP1wtI74MxwZ+C8T2UdY XqmLZIVyMCx14MDXx/Eb5qZo7JwBivPwJXcImr+4wtVnwWRyLhl1jZewxU3lRA== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1722297541; a=rsa-sha256; cv=none; b=J8u20wy1tTqsqMT9G8vsqPrXgjMDS7ZuPPAwAMDU4IfGyPlBmZLDc5b1tJUlU0x5QYqpwr UNW3uviQwNQouq24w8oF+pGKeGcAQ4IsVzbd9ZVunl4uzFNMRpc+O1TkX/ohlT65uFdfx8 Rb+HfOl0d4T0I91T02RGYyBwrjUbtTiZdieKnxmPCTTWYVaEfHSIcBPhzhZ15A3hlddVe0 L/Ls5teomPFZHbKLDknc9TP9bTcBePUL/RsbosTlvJD+TckQ+xGBdlnLdPa891xNIUCrx9 MAKUgznlqKU9XQYj5Y5KoNDNWXZKUZM3Y8uSyNc1dWaQWb4N2Vo6/1WuSauUcA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1722297541; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=zLW8PhStRqGKC3U9x6jPPtCcV8pRThxhCJ0Fww73+XE=; b=Petw0TFFIN5brpRQEz8U64YX5tHS3dzsGFFulM/F1m3LI4/Hka5ZQ2Un3SNeWZt+Oj5V1a ID7YXHZ+saws/XPl4uVQ9I8GlxSTzLPErs6UGKNKiOHdMEexpmEWCjZbakgayKEQYau1kw BWpBJ0ACgwozGnpNsSABob/Eq4Qa1hCpgtbnxg1bpa6tCg4Fvguvmfkw4AXInhXYAT9zwB 6vp1Fhj39/BP67dX3pLbk0LBx1TOPYGKWEu/NoJIUM/OSgtaUvXDFSeljK0iCh+mTKELlC JTX0E34F61BI5AAxybhw29AbfEtBPXhNyUCFSSxmPXjXmfiCMazPyXhyPCQ7rg== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4WXwLY5VHLztmm; Mon, 29 Jul 2024 23:59:01 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.18.1/8.18.1) with ESMTP id 46TNx183051805; Mon, 29 Jul 2024 23:59:01 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 46TNx1X0051802; Mon, 29 Jul 2024 23:59:01 GMT (envelope-from git) Date: Mon, 29 Jul 2024 23:59:01 GMT Message-Id: <202407292359.46TNx1X0051802@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Konstantin Belousov Subject: git: 3f3ec4b99f79 - main - exit(3): make it thread-safe List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-all@freebsd.org Sender: owner-dev-commits-src-all@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: kib X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 3f3ec4b99f79d32a0bf15495559ca9883bd751f2 Auto-Submitted: auto-generated The branch main has been updated by kib: URL: https://cgit.FreeBSD.org/src/commit/?id=3f3ec4b99f79d32a0bf15495559ca9883bd751f2 commit 3f3ec4b99f79d32a0bf15495559ca9883bd751f2 Author: Konstantin Belousov AuthorDate: 2024-07-24 20:41:32 +0000 Commit: Konstantin Belousov CommitDate: 2024-07-29 23:57:34 +0000 exit(3): make it thread-safe It was explained by Rich Felker on libc-coord. See https://austingroupbugs.net/view.php?id=1845. Reviewed by: imp, markj Tested by: antoine (exp-run) Sponsored by: The FreeBSD Foundation MFC after: 1 month Differential revision: https://reviews.freebsd.org/D46108 --- lib/libc/stdlib/exit.3 | 19 ++++++++++++++++++- lib/libc/stdlib/exit.c | 21 +++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/lib/libc/stdlib/exit.3 b/lib/libc/stdlib/exit.3 index 1ff590bb3ae8..b117e77c9e3b 100644 --- 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,23 @@ values described in .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. +The exit status of the process is the +.Fa status +argument of the first +.Nm exit +call which thread proceeds the atexit handlers. +.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 index bca978428ce3..16631fad5b90 100644 --- a/lib/libc/stdlib/exit.c +++ b/lib/libc/stdlib/exit.c @@ -31,6 +31,7 @@ #include "namespace.h" #include +#include #include #include "un-namespace.h" @@ -48,6 +49,20 @@ void (*__cleanup)(void); */ int __isthreaded = 0; +static pthread_mutex_t exit_mutex; +static pthread_once_t exit_mutex_once = PTHREAD_ONCE_INIT; + +static void +exit_mutex_init_once(void) +{ + pthread_mutexattr_t ma; + + _pthread_mutexattr_init(&ma); + _pthread_mutexattr_settype(&ma, PTHREAD_MUTEX_RECURSIVE); + _pthread_mutex_init(&exit_mutex, &ma); + _pthread_mutexattr_destroy(&ma); +} + /* * Exit, flushing stdio buffers if necessary. */ @@ -59,6 +74,12 @@ exit(int status) _thread_autoinit_dummy_decl = 1; + /* Make exit(3) thread-safe */ + if (__isthreaded) { + _once(&exit_mutex_once, exit_mutex_init_once); + _pthread_mutex_lock(&exit_mutex); + } + /* * We're dealing with cleaning up thread_local destructors in the case of * the process termination through main() exit.