From nobody Wed Aug 21 10:33:22 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 4WpjNM3GhZz5Tdmj; Wed, 21 Aug 2024 10:33:23 +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 4WpjNM1PpKz4pq9; Wed, 21 Aug 2024 10:33:23 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1724236403; 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=UgRCtEkMc4RvoA5JH8D2abkvbiO8sp6OGo4rRU5ZeSE=; b=msbuplWk0Uqjk2ffhatiuscyaArCoQlWyGSoC2qhxP7vo5F6qCjSUyQm+lLrQ95ZrVsAm9 osa1jbYpPLlou96BZlBfhmgg3jPdOHoUCC34xWCEo7lLzcW114baGISrBHybtiebWVmO1E 119q9ji4nB4cWnda07IeWD/Pj/ZDQnsMiZUW5ktYgNWHWW9NbQLoh4vewH9J7FNLJRO7XH VNWFNWi4O4LLKDctnir9XTLoJDgRDijeR7kNt3y79Wz3lV+z3Jv7RPJQJ/+eD4MerXb0DY DuS7D0lDV9gv3ZuWPTX/Cw3dTQD84k+K4WCEFqDNHVJ8LzjbdYMsRYt3P928LA== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1724236403; a=rsa-sha256; cv=none; b=d3jO56JCB2NES9Svlfd9XtYsFOTxSpLHX2CGwdRwbg+EGBK34/yTKFUIxRvoKn1JRoRHxj VigQNk0Y9SzKGFLiPiALHqAv1odwh8rQzGEYlx2TbYhAbyP4vqGJAIMM2hxzCfrQ6CQed3 gOYCP9fixy19NIHHZuhPTMeD+0I0M1K1/mGjp4krzJ4WbTi/JG91osj8m2j4jtKs2Cc4vC EVwAi7q02ICPIMuREfr4gl8IR1xdp/5Nl7JUwA6eRi492JMCh0GNo9SNOEgYKm/dhQ3ypg CgjS8FyX334IaR3J0o5johtrcAfiefjWyn72od5owcNSQdGWR0Rgcx6hJ0HJKA== 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=1724236403; 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=UgRCtEkMc4RvoA5JH8D2abkvbiO8sp6OGo4rRU5ZeSE=; b=ROzLEC+G5Xa94jZfWmxLerInW44BZcDZfJzQ4xtIn5C+vHlL6QkbaM1Ax/6rFdw/4//RZ4 cTxT7FHZVSm9ZD02tFtxDyUwhV30UYu5DHZitUsrmagfrvaIQu+f0UyvTfmiZSVvwP+Q8t 2XXdw39n+HsBwx/tjPtIyFeRvfFtpwdHw9HJ0EaAQFoxccLsImkcP3W/+SIUcGwNBw8lG7 PawOUI1vm4en/HAztn/sBLDLERhDarzb83No9pDT7bX71o93VzEnkHgueOF7FPBGuutTc8 ZX6kzSDkHeuApVAMQV0ItUazMWza9NTUeLzFAylAe1yolEISRBlORj8dQjvvPg== 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 4WpjNL6kHRzg3S; Wed, 21 Aug 2024 10:33:22 +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 47LAXMlZ078396; Wed, 21 Aug 2024 10:33:22 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 47LAXM9b078393; Wed, 21 Aug 2024 10:33:22 GMT (envelope-from git) Date: Wed, 21 Aug 2024 10:33:22 GMT Message-Id: <202408211033.47LAXM9b078393@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Konstantin Belousov Subject: git: 8d801641a398 - stable/14 - 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/stable/14 X-Git-Reftype: branch X-Git-Commit: 8d801641a39834cf8ce72dc88cf8bc5712cc1cad Auto-Submitted: auto-generated The branch stable/14 has been updated by kib: URL: https://cgit.FreeBSD.org/src/commit/?id=8d801641a39834cf8ce72dc88cf8bc5712cc1cad commit 8d801641a39834cf8ce72dc88cf8bc5712cc1cad Author: Konstantin Belousov AuthorDate: 2024-07-24 20:41:32 +0000 Commit: Konstantin Belousov CommitDate: 2024-08-21 10:32:49 +0000 exit(3): make it thread-safe (cherry picked from commit 3f3ec4b99f79d32a0bf15495559ca9883bd751f2) --- lib/libc/stdlib/exit.3 | 20 ++++++++++++++++++-- lib/libc/stdlib/exit.c | 21 +++++++++++++++++++++ 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/lib/libc/stdlib/exit.3 b/lib/libc/stdlib/exit.3 index afdc58ec4831..c190f5fac27f 100644 --- a/lib/libc/stdlib/exit.3 +++ b/lib/libc/stdlib/exit.3 @@ -29,9 +29,8 @@ .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" -.\" @(#)exit.3 8.1 (Berkeley) 6/4/93 .\" -.Dd August 5, 2021 +.Dd July 24, 2024 .Dt EXIT 3 .Os .Sh NAME @@ -104,6 +103,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 a0c9622944c5..b2c2fa7db4bb 100644 --- a/lib/libc/stdlib/exit.c +++ b/lib/libc/stdlib/exit.c @@ -34,6 +34,7 @@ static char sccsid[] = "@(#)exit.c 8.1 (Berkeley) 6/4/93"; #endif /* LIBC_SCCS and not lint */ #include "namespace.h" #include +#include #include #include "un-namespace.h" @@ -51,6 +52,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. */ @@ -62,6 +77,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.