c2021d0
#
c2021d0
# Posted upstream:
c2021d0
# https://sourceware.org/ml/libc-alpha/2013-10/msg00065.html
c2021d0
#
c2021d0
# This is related to bug 1013801 in that it fixes the problem
c2021d0
# by modifying the runtime. In bug 1013801 we have libselinux
c2021d0
# using pthread_atfork which pulls in libpthread, but we don't
c2021d0
# want that, we want libpthread to be pulled in only when
c2021d0
# actually needed by the application. This patch makes it
c2021d0
# possible to avoid requiring libpthread and still use
c2021d0
# pthread_atfork.
c2021d0
#
c2021d0
# The general idea for the design is in the leading comment
c2021d0
# in the source code.
c2021d0
#
c2021d0
diff --git a/nptl/sysdeps/unix/sysv/linux/Makefile b/nptl/sysdeps/unix/sysv/linux/Makefile
c2021d0
index 6078e2d..36fd50b 100644
Siddhesh Poyarekar 1ea305b
--- a/nptl/Makefile
Siddhesh Poyarekar 1ea305b
+++ b/nptl/Makefile
c2021d0
@@ -18,7 +18,9 @@
c2021d0
 
Siddhesh Poyarekar 1ea305b
 routines = alloca_cutoff forward libc-lowlevellock libc-cancellation \
Siddhesh Poyarekar 1ea305b
 	   libc-cleanup libc_pthread_init libc_multiple_threads \
Siddhesh Poyarekar 1ea305b
-	   register-atfork unregister-atfork
Siddhesh Poyarekar 1ea305b
+	   register-atfork unregister-atfork libc_pthread_atfork
c2021d0
+
c2021d0
+static-only-routines += libc_pthread_atfork
Siddhesh Poyarekar 1ea305b
 shared-only-routines = forward
c2021d0
 
Siddhesh Poyarekar 6223dbf
 libpthread-routines = nptl-init vars events version pt-interp \
Siddhesh Poyarekar 1ea305b
diff --git a/nptl/libc_pthread_atfork.c b/nptl/libc_pthread_atfork.c
c2021d0
new file mode 100644
c2021d0
index 0000000..667049a
c2021d0
--- /dev/null
Siddhesh Poyarekar 1ea305b
+++ b/nptl/libc_pthread_atfork.c
c2021d0
@@ -0,0 +1,54 @@
c2021d0
+/* Copyright (C) 2013 Free Software Foundation, Inc.
c2021d0
+   This file is part of the GNU C Library.
c2021d0
+   Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
c2021d0
+
c2021d0
+   The GNU C Library is free software; you can redistribute it and/or
c2021d0
+   modify it under the terms of the GNU Lesser General Public
c2021d0
+   License as published by the Free Software Foundation; either
c2021d0
+   version 2.1 of the License, or (at your option) any later version.
c2021d0
+
c2021d0
+   The GNU C Library is distributed in the hope that it will be useful,
c2021d0
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
c2021d0
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
c2021d0
+   Lesser General Public License for more details.
c2021d0
+
c2021d0
+   You should have received a copy of the GNU Lesser General Public
c2021d0
+   License along with the GNU C Library; if not, see
c2021d0
+   <http://www.gnu.org/licenses/>.  */
c2021d0
+
c2021d0
+/* The standard design pattern for making it optional to link against
c2021d0
+   libpthread is to mark the function weak, test if the function
c2021d0
+   address is non-zero and call the function, otherwise use a fallback.
c2021d0
+   The problem with pthread_atfork is that there is no viable
c2021d0
+   fallback.  If you need to do something during fork it has to be done
c2021d0
+   via pthread_atfork.  This makes having libpthread optional and using
c2021d0
+   pthread_atfork impossible.  We make it possible by providing
c2021d0
+   pthread_atfork in libc_nonshared.a.  The real work of pthread_atfork
c2021d0
+   is done by __register_atfork which is already provided in
c2021d0
+   libc_nonshared.a.  It's included in libc_nonshared.a because
c2021d0
+   __dso_handle has to be unique to each DSO such that unloading the DSO
c2021d0
+   can unregister the atfork handlers.  We build pthread_atfork again
c2021d0
+   under a different file name and include it into libc_nonshared.a and
c2021d0
+   libc.a. We keep pthread_atfork in libpthread_nonshared.a and
c2021d0
+   libpthread.a for compatibility and completeness.
c2021d0
+
c2021d0
+   Applications that can't rely on a new glibc should use the following
c2021d0
+   code to optionally include libpthread and still register a function
c2021d0
+   via pthread_atfork i.e. use __register_atfork directly:
c2021d0
+
c2021d0
+   extern void *__dso_handle __attribute__ ((__weak__, __visibility__ ("hidden")));
c2021d0
+   extern int __register_atfork (void (*) (void), void (*) (void), void (*) (void), void *);
c2021d0
+
c2021d0
+   static int __app_atfork (void (*prepare) (void), void (*parent) (void), void (*child) (void))
c2021d0
+     {
c2021d0
+       return __register_atfork (prepare, parent, child,
c2021d0
+				 &__dso_handle == NULL ? NULL : __dso_handle);
c2021d0
+     }
c2021d0
+
c2021d0
+   This code requires glibc 2.3.2 or newer. Previous to 2.3.2 no such
c2021d0
+   interfaces exist and at that point is is impossible to have an
c2021d0
+   optional libpthread and call pthread_atfork.
c2021d0
+
c2021d0
+   This code adds no more ABI requirements than already exist since
c2021d0
+   __dso_handle and __register_atfork are already part of the ABI.  */
c2021d0
+#include <pthread_atfork.c>