076f82
commit ff450cdbdee0b8cb6b9d653d6d2fa892de29be31
076f82
Author: Arjun Shankar <arjun@redhat.com>
076f82
Date:   Tue May 24 17:57:36 2022 +0200
076f82
076f82
    Fix deadlock when pthread_atfork handler calls pthread_atfork or dlclose
076f82
    
076f82
    In multi-threaded programs, registering via pthread_atfork,
076f82
    de-registering implicitly via dlclose, or running pthread_atfork
076f82
    handlers during fork was protected by an internal lock.  This meant
076f82
    that a pthread_atfork handler attempting to register another handler or
076f82
    dlclose a dynamically loaded library would lead to a deadlock.
076f82
    
076f82
    This commit fixes the deadlock in the following way:
076f82
    
076f82
    During the execution of handlers at fork time, the atfork lock is
076f82
    released prior to the execution of each handler and taken again upon its
076f82
    return.  Any handler registrations or de-registrations that occurred
076f82
    during the execution of the handler are accounted for before proceeding
076f82
    with further handler execution.
076f82
    
076f82
    If a handler that hasn't been executed yet gets de-registered by another
076f82
    handler during fork, it will not be executed.   If a handler gets
076f82
    registered by another handler during fork, it will not be executed
076f82
    during that particular fork.
076f82
    
076f82
    The possibility that handlers may now be registered or deregistered
076f82
    during handler execution means that identifying the next handler to be
076f82
    run after a given handler may register/de-register others requires some
076f82
    bookkeeping.  The fork_handler struct has an additional field, 'id',
076f82
    which is assigned sequentially during registration.  Thus, handlers are
076f82
    executed in ascending order of 'id' during 'prepare', and descending
076f82
    order of 'id' during parent/child handler execution after the fork.
076f82
    
076f82
    Two tests are included:
076f82
    
076f82
    * tst-atfork3: Adhemerval Zanella <adhemerval.zanella@linaro.org>
076f82
      This test exercises calling dlclose from prepare, parent, and child
076f82
      handlers.
076f82
    
076f82
    * tst-atfork4: This test exercises calling pthread_atfork and dlclose
076f82
      from the prepare handler.
076f82
    
076f82
    [BZ #24595, BZ #27054]
076f82
    
076f82
    Co-authored-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
076f82
    Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>
076f82
    (cherry picked from commit 52a103e237329b9f88a28513fe7506ffc3bd8ced)
076f82
076f82
diff --git a/include/register-atfork.h b/include/register-atfork.h
076f82
index fadde14700947ac6..6d7bfd87688d6530 100644
076f82
--- a/include/register-atfork.h
076f82
+++ b/include/register-atfork.h
076f82
@@ -26,6 +26,7 @@ struct fork_handler
076f82
   void (*parent_handler) (void);
076f82
   void (*child_handler) (void);
076f82
   void *dso_handle;
076f82
+  uint64_t id;
076f82
 };
076f82
 
076f82
 /* Function to call to unregister fork handlers.  */
076f82
@@ -39,19 +40,18 @@ enum __run_fork_handler_type
076f82
   atfork_run_parent
076f82
 };
076f82
 
076f82
-/* Run the atfork handlers and lock/unlock the internal lock depending
076f82
-   of the WHO argument:
076f82
-
076f82
-   - atfork_run_prepare: run all the PREPARE_HANDLER in reverse order of
076f82
-			 insertion and locks the internal lock.
076f82
-   - atfork_run_child: run all the CHILD_HANDLER and unlocks the internal
076f82
-		       lock.
076f82
-   - atfork_run_parent: run all the PARENT_HANDLER and unlocks the internal
076f82
-			lock.
076f82
-
076f82
-   Perform locking only if DO_LOCKING.  */
076f82
-extern void __run_fork_handlers (enum __run_fork_handler_type who,
076f82
-				 _Bool do_locking) attribute_hidden;
076f82
+/* Run the atfork prepare handlers in the reverse order of registration and
076f82
+   return the ID of the last registered handler.  If DO_LOCKING is true, the
076f82
+   internal lock is held locked upon return.  */
076f82
+extern uint64_t __run_prefork_handlers (_Bool do_locking) attribute_hidden;
076f82
+
076f82
+/* Given a handler type (parent or child), run all the atfork handlers in
076f82
+   the order of registration up to and including the handler with id equal
076f82
+   to LASTRUN.  If DO_LOCKING is true, the internal lock is unlocked prior
076f82
+   to return.  */
076f82
+extern void __run_postfork_handlers (enum __run_fork_handler_type who,
076f82
+                                     _Bool do_locking,
076f82
+                                     uint64_t lastrun) attribute_hidden;
076f82
 
076f82
 /* C library side function to register new fork handlers.  */
076f82
 extern int __register_atfork (void (*__prepare) (void),
076f82
diff --git a/posix/fork.c b/posix/fork.c
076f82
index 021691b9b7441f15..890b806eb48cb75a 100644
076f82
--- a/posix/fork.c
076f82
+++ b/posix/fork.c
076f82
@@ -46,8 +46,9 @@ __libc_fork (void)
076f82
      best effort to make is async-signal-safe at least for single-thread
076f82
      case.  */
076f82
   bool multiple_threads = __libc_single_threaded == 0;
076f82
+  uint64_t lastrun;
076f82
 
076f82
-  __run_fork_handlers (atfork_run_prepare, multiple_threads);
076f82
+  lastrun = __run_prefork_handlers (multiple_threads);
076f82
 
076f82
   struct nss_database_data nss_database_data;
076f82
 
076f82
@@ -105,7 +106,7 @@ __libc_fork (void)
076f82
       reclaim_stacks ();
076f82
 
076f82
       /* Run the handlers registered for the child.  */
076f82
-      __run_fork_handlers (atfork_run_child, multiple_threads);
076f82
+      __run_postfork_handlers (atfork_run_child, multiple_threads, lastrun);
076f82
     }
076f82
   else
076f82
     {
076f82
@@ -123,7 +124,7 @@ __libc_fork (void)
076f82
 	}
076f82
 
076f82
       /* Run the handlers registered for the parent.  */
076f82
-      __run_fork_handlers (atfork_run_parent, multiple_threads);
076f82
+      __run_postfork_handlers (atfork_run_parent, multiple_threads, lastrun);
076f82
 
076f82
       if (pid < 0)
076f82
 	__set_errno (save_errno);
076f82
diff --git a/posix/register-atfork.c b/posix/register-atfork.c
076f82
index 6fd9e4c56aafd7cc..6370437aa68e039e 100644
076f82
--- a/posix/register-atfork.c
076f82
+++ b/posix/register-atfork.c
076f82
@@ -19,6 +19,8 @@
076f82
 #include <libc-lock.h>
076f82
 #include <stdbool.h>
076f82
 #include <register-atfork.h>
076f82
+#include <intprops.h>
076f82
+#include <stdio.h>
076f82
 
076f82
 #define DYNARRAY_ELEMENT           struct fork_handler
076f82
 #define DYNARRAY_STRUCT            fork_handler_list
076f82
@@ -27,7 +29,7 @@
076f82
 #include <malloc/dynarray-skeleton.c>
076f82
 
076f82
 static struct fork_handler_list fork_handlers;
076f82
-static bool fork_handler_init = false;
076f82
+static uint64_t fork_handler_counter;
076f82
 
076f82
 static int atfork_lock = LLL_LOCK_INITIALIZER;
076f82
 
076f82
@@ -37,11 +39,8 @@ __register_atfork (void (*prepare) (void), void (*parent) (void),
076f82
 {
076f82
   lll_lock (atfork_lock, LLL_PRIVATE);
076f82
 
076f82
-  if (!fork_handler_init)
076f82
-    {
076f82
-      fork_handler_list_init (&fork_handlers);
076f82
-      fork_handler_init = true;
076f82
-    }
076f82
+  if (fork_handler_counter == 0)
076f82
+    fork_handler_list_init (&fork_handlers);
076f82
 
076f82
   struct fork_handler *newp = fork_handler_list_emplace (&fork_handlers);
076f82
   if (newp != NULL)
076f82
@@ -50,6 +49,13 @@ __register_atfork (void (*prepare) (void), void (*parent) (void),
076f82
       newp->parent_handler = parent;
076f82
       newp->child_handler = child;
076f82
       newp->dso_handle = dso_handle;
076f82
+
076f82
+      /* IDs assigned to handlers start at 1 and increment with handler
076f82
+         registration.  Un-registering a handlers discards the corresponding
076f82
+         ID.  It is not reused in future registrations.  */
076f82
+      if (INT_ADD_OVERFLOW (fork_handler_counter, 1))
076f82
+        __libc_fatal ("fork handler counter overflow");
076f82
+      newp->id = ++fork_handler_counter;
076f82
     }
076f82
 
076f82
   /* Release the lock.  */
076f82
@@ -104,37 +110,111 @@ __unregister_atfork (void *dso_handle)
076f82
   lll_unlock (atfork_lock, LLL_PRIVATE);
076f82
 }
076f82
 
076f82
-void
076f82
-__run_fork_handlers (enum __run_fork_handler_type who, _Bool do_locking)
076f82
+uint64_t
076f82
+__run_prefork_handlers (_Bool do_locking)
076f82
 {
076f82
-  struct fork_handler *runp;
076f82
+  uint64_t lastrun;
076f82
 
076f82
-  if (who == atfork_run_prepare)
076f82
+  if (do_locking)
076f82
+    lll_lock (atfork_lock, LLL_PRIVATE);
076f82
+
076f82
+  /* We run prepare handlers from last to first.  After fork, only
076f82
+     handlers up to the last handler found here (pre-fork) will be run.
076f82
+     Handlers registered during __run_prefork_handlers or
076f82
+     __run_postfork_handlers will be positioned after this last handler, and
076f82
+     since their prepare handlers won't be run now, their parent/child
076f82
+     handlers should also be ignored.  */
076f82
+  lastrun = fork_handler_counter;
076f82
+
076f82
+  size_t sl = fork_handler_list_size (&fork_handlers);
076f82
+  for (size_t i = sl; i > 0;)
076f82
     {
076f82
-      if (do_locking)
076f82
-	lll_lock (atfork_lock, LLL_PRIVATE);
076f82
-      size_t sl = fork_handler_list_size (&fork_handlers);
076f82
-      for (size_t i = sl; i > 0; i--)
076f82
-	{
076f82
-	  runp = fork_handler_list_at (&fork_handlers, i - 1);
076f82
-	  if (runp->prepare_handler != NULL)
076f82
-	    runp->prepare_handler ();
076f82
-	}
076f82
+      struct fork_handler *runp
076f82
+        = fork_handler_list_at (&fork_handlers, i - 1);
076f82
+
076f82
+      uint64_t id = runp->id;
076f82
+
076f82
+      if (runp->prepare_handler != NULL)
076f82
+        {
076f82
+          if (do_locking)
076f82
+            lll_unlock (atfork_lock, LLL_PRIVATE);
076f82
+
076f82
+          runp->prepare_handler ();
076f82
+
076f82
+          if (do_locking)
076f82
+            lll_lock (atfork_lock, LLL_PRIVATE);
076f82
+        }
076f82
+
076f82
+      /* We unlocked, ran the handler, and locked again.  In the
076f82
+         meanwhile, one or more deregistrations could have occurred leading
076f82
+         to the current (just run) handler being moved up the list or even
076f82
+         removed from the list itself.  Since handler IDs are guaranteed to
076f82
+         to be in increasing order, the next handler has to have:  */
076f82
+
076f82
+      /* A. An earlier position than the current one has.  */
076f82
+      i--;
076f82
+
076f82
+      /* B. A lower ID than the current one does.  The code below skips
076f82
+         any newly added handlers with higher IDs.  */
076f82
+      while (i > 0
076f82
+             && fork_handler_list_at (&fork_handlers, i - 1)->id >= id)
076f82
+        i--;
076f82
     }
076f82
-  else
076f82
+
076f82
+  return lastrun;
076f82
+}
076f82
+
076f82
+void
076f82
+__run_postfork_handlers (enum __run_fork_handler_type who, _Bool do_locking,
076f82
+                         uint64_t lastrun)
076f82
+{
076f82
+  size_t sl = fork_handler_list_size (&fork_handlers);
076f82
+  for (size_t i = 0; i < sl;)
076f82
     {
076f82
-      size_t sl = fork_handler_list_size (&fork_handlers);
076f82
-      for (size_t i = 0; i < sl; i++)
076f82
-	{
076f82
-	  runp = fork_handler_list_at (&fork_handlers, i);
076f82
-	  if (who == atfork_run_child && runp->child_handler)
076f82
-	    runp->child_handler ();
076f82
-	  else if (who == atfork_run_parent && runp->parent_handler)
076f82
-	    runp->parent_handler ();
076f82
-	}
076f82
+      struct fork_handler *runp = fork_handler_list_at (&fork_handlers, i);
076f82
+      uint64_t id = runp->id;
076f82
+
076f82
+      /* prepare handlers were not run for handlers with ID > LASTRUN.
076f82
+         Thus, parent/child handlers will also not be run.  */
076f82
+      if (id > lastrun)
076f82
+        break;
076f82
+
076f82
       if (do_locking)
076f82
-	lll_unlock (atfork_lock, LLL_PRIVATE);
076f82
+        lll_unlock (atfork_lock, LLL_PRIVATE);
076f82
+
076f82
+      if (who == atfork_run_child && runp->child_handler)
076f82
+        runp->child_handler ();
076f82
+      else if (who == atfork_run_parent && runp->parent_handler)
076f82
+        runp->parent_handler ();
076f82
+
076f82
+      if (do_locking)
076f82
+        lll_lock (atfork_lock, LLL_PRIVATE);
076f82
+
076f82
+      /* We unlocked, ran the handler, and locked again.  In the meanwhile,
076f82
+         one or more [de]registrations could have occurred.  Due to this,
076f82
+         the list size must be updated.  */
076f82
+      sl = fork_handler_list_size (&fork_handlers);
076f82
+
076f82
+      /* The just-run handler could also have moved up the list. */
076f82
+
076f82
+      if (sl > i && fork_handler_list_at (&fork_handlers, i)->id == id)
076f82
+        /* The position of the recently run handler hasn't changed.  The
076f82
+           next handler to be run is an easy increment away.  */
076f82
+        i++;
076f82
+      else
076f82
+        {
076f82
+          /* The next handler to be run is the first handler in the list
076f82
+             to have an ID higher than the current one.  */
076f82
+          for (i = 0; i < sl; i++)
076f82
+            {
076f82
+              if (fork_handler_list_at (&fork_handlers, i)->id > id)
076f82
+                break;
076f82
+            }
076f82
+        }
076f82
     }
076f82
+
076f82
+  if (do_locking)
076f82
+    lll_unlock (atfork_lock, LLL_PRIVATE);
076f82
 }
076f82
 
076f82
 
076f82
diff --git a/sysdeps/pthread/Makefile b/sysdeps/pthread/Makefile
076f82
index 00419c4d199df912..5147588c130c9415 100644
076f82
--- a/sysdeps/pthread/Makefile
076f82
+++ b/sysdeps/pthread/Makefile
076f82
@@ -154,16 +154,36 @@ tests += tst-cancelx2 tst-cancelx3 tst-cancelx6 tst-cancelx8 tst-cancelx9 \
076f82
 	 tst-cleanupx0 tst-cleanupx1 tst-cleanupx2 tst-cleanupx3
076f82
 
076f82
 ifeq ($(build-shared),yes)
076f82
-tests += tst-atfork2 tst-pt-tls4 tst-_res1 tst-fini1 tst-create1
076f82
+tests += \
076f82
+  tst-atfork2 \
076f82
+  tst-pt-tls4 \
076f82
+  tst-_res1 \
076f82
+  tst-fini1 \
076f82
+  tst-create1 \
076f82
+  tst-atfork3 \
076f82
+  tst-atfork4 \
076f82
+# tests
076f82
+
076f82
 tests-nolibpthread += tst-fini1
076f82
 endif
076f82
 
076f82
-modules-names += tst-atfork2mod tst-tls4moda tst-tls4modb \
076f82
-		 tst-_res1mod1 tst-_res1mod2 tst-fini1mod \
076f82
-		 tst-create1mod
076f82
+modules-names += \
076f82
+  tst-atfork2mod \
076f82
+  tst-tls4moda \
076f82
+  tst-tls4modb \
076f82
+  tst-_res1mod1 \
076f82
+  tst-_res1mod2 \
076f82
+  tst-fini1mod \
076f82
+  tst-create1mod \
076f82
+  tst-atfork3mod \
076f82
+  tst-atfork4mod \
076f82
+# module-names
076f82
+
076f82
 test-modules = $(addprefix $(objpfx),$(addsuffix .so,$(modules-names)))
076f82
 
076f82
 tst-atfork2mod.so-no-z-defs = yes
076f82
+tst-atfork3mod.so-no-z-defs = yes
076f82
+tst-atfork4mod.so-no-z-defs = yes
076f82
 tst-create1mod.so-no-z-defs = yes
076f82
 
076f82
 ifeq ($(build-shared),yes)
076f82
@@ -226,8 +246,18 @@ tst-atfork2-ENV = MALLOC_TRACE=$(objpfx)tst-atfork2.mtrace \
076f82
 		  LD_PRELOAD=$(common-objpfx)/malloc/libc_malloc_debug.so
076f82
 $(objpfx)tst-atfork2mod.so: $(shared-thread-library)
076f82
 
076f82
+$(objpfx)tst-atfork3: $(shared-thread-library)
076f82
+LDFLAGS-tst-atfork3 = -rdynamic
076f82
+$(objpfx)tst-atfork3mod.so: $(shared-thread-library)
076f82
+
076f82
+$(objpfx)tst-atfork4: $(shared-thread-library)
076f82
+LDFLAGS-tst-atfork4 = -rdynamic
076f82
+$(objpfx)tst-atfork4mod.so: $(shared-thread-library)
076f82
+
076f82
 ifeq ($(build-shared),yes)
076f82
 $(objpfx)tst-atfork2.out: $(objpfx)tst-atfork2mod.so
076f82
+$(objpfx)tst-atfork3.out: $(objpfx)tst-atfork3mod.so
076f82
+$(objpfx)tst-atfork4.out: $(objpfx)tst-atfork4mod.so
076f82
 endif
076f82
 
076f82
 ifeq ($(build-shared),yes)
076f82
diff --git a/sysdeps/pthread/tst-atfork3.c b/sysdeps/pthread/tst-atfork3.c
076f82
new file mode 100644
076f82
index 0000000000000000..bb2250e432ab79ad
076f82
--- /dev/null
076f82
+++ b/sysdeps/pthread/tst-atfork3.c
076f82
@@ -0,0 +1,118 @@
076f82
+/* Check if pthread_atfork handler can call dlclose (BZ#24595).
076f82
+   Copyright (C) 2022 Free Software Foundation, Inc.
076f82
+   This file is part of the GNU C Library.
076f82
+
076f82
+   The GNU C Library is free software; you can redistribute it and/or
076f82
+   modify it under the terms of the GNU Lesser General Public
076f82
+   License as published by the Free Software Foundation; either
076f82
+   version 2.1 of the License, or (at your option) any later version.
076f82
+
076f82
+   The GNU C Library is distributed in the hope that it will be useful,
076f82
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
076f82
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
076f82
+   Lesser General Public License for more details.
076f82
+
076f82
+   You should have received a copy of the GNU Lesser General Public
076f82
+   License along with the GNU C Library; if not, see
076f82
+   <http://www.gnu.org/licenses/>.  */
076f82
+
076f82
+#include <stdio.h>
076f82
+#include <pthread.h>
076f82
+#include <unistd.h>
076f82
+#include <stdlib.h>
076f82
+#include <stdbool.h>
076f82
+
076f82
+#include <support/check.h>
076f82
+#include <support/xthread.h>
076f82
+#include <support/capture_subprocess.h>
076f82
+#include <support/xdlfcn.h>
076f82
+
076f82
+/* Check if pthread_atfork handlers do not deadlock when calling a function
076f82
+   that might alter the internal fork handle list, such as dlclose.
076f82
+
076f82
+   The test registers a callback set with pthread_atfork(), dlopen() a shared
076f82
+   library (nptl/tst-atfork3mod.c), calls an exported symbol from the library
076f82
+   (which in turn also registers atfork handlers), and calls fork to trigger
076f82
+   the callbacks.  */
076f82
+
076f82
+static void *handler;
076f82
+static bool run_dlclose_prepare;
076f82
+static bool run_dlclose_parent;
076f82
+static bool run_dlclose_child;
076f82
+
076f82
+static void
076f82
+prepare (void)
076f82
+{
076f82
+  if (run_dlclose_prepare)
076f82
+    xdlclose (handler);
076f82
+}
076f82
+
076f82
+static void
076f82
+parent (void)
076f82
+{
076f82
+  if (run_dlclose_parent)
076f82
+    xdlclose (handler);
076f82
+}
076f82
+
076f82
+static void
076f82
+child (void)
076f82
+{
076f82
+  if (run_dlclose_child)
076f82
+    xdlclose (handler);
076f82
+}
076f82
+
076f82
+static void
076f82
+proc_func (void *closure)
076f82
+{
076f82
+}
076f82
+
076f82
+static void
076f82
+do_test_generic (bool dlclose_prepare, bool dlclose_parent, bool dlclose_child)
076f82
+{
076f82
+  run_dlclose_prepare = dlclose_prepare;
076f82
+  run_dlclose_parent = dlclose_parent;
076f82
+  run_dlclose_child = dlclose_child;
076f82
+
076f82
+  handler = xdlopen ("tst-atfork3mod.so", RTLD_NOW);
076f82
+
076f82
+  int (*atfork3mod_func)(void);
076f82
+  atfork3mod_func = xdlsym (handler, "atfork3mod_func");
076f82
+
076f82
+  atfork3mod_func ();
076f82
+
076f82
+  struct support_capture_subprocess proc
076f82
+    = support_capture_subprocess (proc_func, NULL);
076f82
+  support_capture_subprocess_check (&proc, "tst-atfork3", 0, sc_allow_none);
076f82
+
076f82
+  handler = atfork3mod_func = NULL;
076f82
+
076f82
+  support_capture_subprocess_free (&proc;;
076f82
+}
076f82
+
076f82
+static void *
076f82
+thread_func (void *closure)
076f82
+{
076f82
+  return NULL;
076f82
+}
076f82
+
076f82
+static int
076f82
+do_test (void)
076f82
+{
076f82
+  {
076f82
+    /* Make the process acts as multithread.  */
076f82
+    pthread_attr_t attr;
076f82
+    xpthread_attr_init (&attr);
076f82
+    xpthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
076f82
+    xpthread_create (&attr, thread_func, NULL);
076f82
+  }
076f82
+
076f82
+  TEST_COMPARE (pthread_atfork (prepare, parent, child), 0);
076f82
+
076f82
+  do_test_generic (true  /* prepare */, false /* parent */, false /* child */);
076f82
+  do_test_generic (false /* prepare */, true  /* parent */, false /* child */);
076f82
+  do_test_generic (false /* prepare */, false /* parent */, true  /* child */);
076f82
+
076f82
+  return 0;
076f82
+}
076f82
+
076f82
+#include <support/test-driver.c>
076f82
diff --git a/sysdeps/pthread/tst-atfork3mod.c b/sysdeps/pthread/tst-atfork3mod.c
076f82
new file mode 100644
076f82
index 0000000000000000..6d0658cb9efdecbc
076f82
--- /dev/null
076f82
+++ b/sysdeps/pthread/tst-atfork3mod.c
076f82
@@ -0,0 +1,44 @@
076f82
+/* Copyright (C) 2022 Free Software Foundation, Inc.
076f82
+   This file is part of the GNU C Library.
076f82
+
076f82
+   The GNU C Library is free software; you can redistribute it and/or
076f82
+   modify it under the terms of the GNU Lesser General Public
076f82
+   License as published by the Free Software Foundation; either
076f82
+   version 2.1 of the License, or (at your option) any later version.
076f82
+
076f82
+   The GNU C Library is distributed in the hope that it will be useful,
076f82
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
076f82
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
076f82
+   Lesser General Public License for more details.
076f82
+
076f82
+   You should have received a copy of the GNU Lesser General Public
076f82
+   License along with the GNU C Library; if not, see
076f82
+   <http://www.gnu.org/licenses/>.  */
076f82
+
076f82
+#include <unistd.h>
076f82
+#include <stdlib.h>
076f82
+#include <pthread.h>
076f82
+
076f82
+#include <support/check.h>
076f82
+
076f82
+static void
076f82
+mod_prepare (void)
076f82
+{
076f82
+}
076f82
+
076f82
+static void
076f82
+mod_parent (void)
076f82
+{
076f82
+}
076f82
+
076f82
+static void
076f82
+mod_child (void)
076f82
+{
076f82
+}
076f82
+
076f82
+int atfork3mod_func (void)
076f82
+{
076f82
+  TEST_COMPARE (pthread_atfork (mod_prepare, mod_parent, mod_child), 0);
076f82
+
076f82
+  return 0;
076f82
+}
076f82
diff --git a/sysdeps/pthread/tst-atfork4.c b/sysdeps/pthread/tst-atfork4.c
076f82
new file mode 100644
076f82
index 0000000000000000..52dc87e73b846ab9
076f82
--- /dev/null
076f82
+++ b/sysdeps/pthread/tst-atfork4.c
076f82
@@ -0,0 +1,128 @@
076f82
+/* pthread_atfork supports handlers that call pthread_atfork or dlclose.
076f82
+   Copyright (C) 2022 Free Software Foundation, Inc.
076f82
+   This file is part of the GNU C Library.
076f82
+
076f82
+   The GNU C Library is free software; you can redistribute it and/or
076f82
+   modify it under the terms of the GNU Lesser General Public
076f82
+   License as published by the Free Software Foundation; either
076f82
+   version 2.1 of the License, or (at your option) any later version.
076f82
+
076f82
+   The GNU C Library is distributed in the hope that it will be useful,
076f82
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
076f82
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
076f82
+   Lesser General Public License for more details.
076f82
+
076f82
+   You should have received a copy of the GNU Lesser General Public
076f82
+   License along with the GNU C Library; if not, see
076f82
+   <https://www.gnu.org/licenses/>.  */
076f82
+
076f82
+#include <support/xdlfcn.h>
076f82
+#include <stdio.h>
076f82
+#include <support/xthread.h>
076f82
+#include <sys/types.h>
076f82
+#include <sys/wait.h>
076f82
+#include <support/xunistd.h>
076f82
+#include <support/check.h>
076f82
+#include <stdlib.h>
076f82
+
076f82
+static void *
076f82
+thread_func (void *x)
076f82
+{
076f82
+  return NULL;
076f82
+}
076f82
+
076f82
+static unsigned int second_atfork_handler_runcount = 0;
076f82
+
076f82
+static void
076f82
+second_atfork_handler (void)
076f82
+{
076f82
+  second_atfork_handler_runcount++;
076f82
+}
076f82
+
076f82
+static void *h = NULL;
076f82
+
076f82
+static unsigned int atfork_handler_runcount = 0;
076f82
+
076f82
+static void
076f82
+prepare (void)
076f82
+{
076f82
+  /* These atfork handlers are registered while atfork handlers are being
076f82
+     executed and thus will not be executed during the corresponding
076f82
+     fork.  */
076f82
+  TEST_VERIFY_EXIT (pthread_atfork (second_atfork_handler,
076f82
+                                    second_atfork_handler,
076f82
+                                    second_atfork_handler) == 0);
076f82
+
076f82
+  /* This will de-register the atfork handlers registered by the dlopen'd
076f82
+     library and so they will not be executed.  */
076f82
+  if (h != NULL)
076f82
+    {
076f82
+      xdlclose (h);
076f82
+      h = NULL;
076f82
+    }
076f82
+
076f82
+  atfork_handler_runcount++;
076f82
+}
076f82
+
076f82
+static void
076f82
+after (void)
076f82
+{
076f82
+  atfork_handler_runcount++;
076f82
+}
076f82
+
076f82
+static int
076f82
+do_test (void)
076f82
+{
076f82
+  /* Make sure __libc_single_threaded is 0.  */
076f82
+  pthread_attr_t attr;
076f82
+  xpthread_attr_init (&attr);
076f82
+  xpthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
076f82
+  xpthread_create (&attr, thread_func, NULL);
076f82
+
076f82
+  void (*reg_atfork_handlers) (void);
076f82
+
076f82
+  h = xdlopen ("tst-atfork4mod.so", RTLD_LAZY);
076f82
+
076f82
+  reg_atfork_handlers = xdlsym (h, "reg_atfork_handlers");
076f82
+
076f82
+  reg_atfork_handlers ();
076f82
+
076f82
+  /* We register our atfork handlers *after* loading the module so that our
076f82
+     prepare handler is called first at fork, where we then dlclose the
076f82
+     module before its prepare handler has a chance to be called.  */
076f82
+  TEST_VERIFY_EXIT (pthread_atfork (prepare, after, after) == 0);
076f82
+
076f82
+  pid_t pid = xfork ();
076f82
+
076f82
+  /* Both the parent and the child processes should observe this.  */
076f82
+  TEST_VERIFY_EXIT (atfork_handler_runcount == 2);
076f82
+  TEST_VERIFY_EXIT (second_atfork_handler_runcount == 0);
076f82
+
076f82
+  if (pid > 0)
076f82
+    {
076f82
+      int childstat;
076f82
+
076f82
+      xwaitpid (-1, &childstat, 0);
076f82
+      TEST_VERIFY_EXIT (WIFEXITED (childstat)
076f82
+                        && WEXITSTATUS (childstat) == 0);
076f82
+
076f82
+      /* This time, the second set of atfork handlers should also be called
076f82
+         since the handlers are already in place before fork is called.  */
076f82
+
076f82
+      pid = xfork ();
076f82
+
076f82
+      TEST_VERIFY_EXIT (atfork_handler_runcount == 4);
076f82
+      TEST_VERIFY_EXIT (second_atfork_handler_runcount == 2);
076f82
+
076f82
+      if (pid > 0)
076f82
+        {
076f82
+          xwaitpid (-1, &childstat, 0);
076f82
+          TEST_VERIFY_EXIT (WIFEXITED (childstat)
076f82
+                            && WEXITSTATUS (childstat) == 0);
076f82
+        }
076f82
+    }
076f82
+
076f82
+  return 0;
076f82
+}
076f82
+
076f82
+#include <support/test-driver.c>
076f82
diff --git a/sysdeps/pthread/tst-atfork4mod.c b/sysdeps/pthread/tst-atfork4mod.c
076f82
new file mode 100644
076f82
index 0000000000000000..e111efeb185916e0
076f82
--- /dev/null
076f82
+++ b/sysdeps/pthread/tst-atfork4mod.c
076f82
@@ -0,0 +1,48 @@
076f82
+/* pthread_atfork supports handlers that call pthread_atfork or dlclose.
076f82
+   Copyright (C) 2022 Free Software Foundation, Inc.
076f82
+   This file is part of the GNU C Library.
076f82
+
076f82
+   The GNU C Library is free software; you can redistribute it and/or
076f82
+   modify it under the terms of the GNU Lesser General Public
076f82
+   License as published by the Free Software Foundation; either
076f82
+   version 2.1 of the License, or (at your option) any later version.
076f82
+
076f82
+   The GNU C Library is distributed in the hope that it will be useful,
076f82
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
076f82
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
076f82
+   Lesser General Public License for more details.
076f82
+
076f82
+   You should have received a copy of the GNU Lesser General Public
076f82
+   License along with the GNU C Library; if not, see
076f82
+   <https://www.gnu.org/licenses/>.  */
076f82
+
076f82
+#include <pthread.h>
076f82
+#include <stdlib.h>
076f82
+
076f82
+/* This dynamically loaded library simply registers its atfork handlers when
076f82
+   asked to.  The atfork handlers should never be executed because the
076f82
+   library is unloaded before fork is called by the test program.  */
076f82
+
076f82
+static void
076f82
+prepare (void)
076f82
+{
076f82
+  abort ();
076f82
+}
076f82
+
076f82
+static void
076f82
+parent (void)
076f82
+{
076f82
+  abort ();
076f82
+}
076f82
+
076f82
+static void
076f82
+child (void)
076f82
+{
076f82
+  abort ();
076f82
+}
076f82
+
076f82
+void
076f82
+reg_atfork_handlers (void)
076f82
+{
076f82
+  pthread_atfork (prepare, parent, child);
076f82
+}