bin/68841: [PATCH] Add pthread_atfork to libc_r
Dan Nelson
dnelson at allantgroup.com
Thu Jul 8 20:10:01 PDT 2004
>Number: 68841
>Category: bin
>Synopsis: [PATCH] Add pthread_atfork to libc_r
>Confidential: no
>Severity: non-critical
>Priority: medium
>Responsible: freebsd-bugs
>State: open
>Quarter:
>Keywords:
>Date-Required:
>Class: sw-bug
>Submitter-Id: current-users
>Arrival-Date: Fri Jul 09 03:10:01 GMT 2004
>Closed-Date:
>Last-Modified:
>Originator: Dan Nelson
>Release: FreeBSD 5.2-CURRENT i386
>Organization:
The Allant Group
>Environment:
System: FreeBSD dan.emsphone.com 5.2-CURRENT FreeBSD 5.2-CURRENT #341: Wed Jun 23 23:03:45 CDT 2004 zsh at dan.emsphone.com:/usr/src/sys/i386/compile/DANSMP i386
>Description:
pthread_atfork was initially added to libkse but not libthr or libc_r,
breaking ABI compatibility. It was later added to libthr. This patch
provides it for libc_r (tested).
>How-To-Repeat:
>Fix:
Index: libc_r/uthread/Makefile.inc
===================================================================
RCS file: /home/ncvs/src/lib/libc_r/uthread/Makefile.inc,v
retrieving revision 1.37
diff -u -p -r1.37 Makefile.inc
--- libc_r/uthread/Makefile.inc 25 Jul 2003 17:02:33 -0000 1.37
+++ libc_r/uthread/Makefile.inc 19 May 2004 20:27:55 -0000
@@ -10,6 +10,7 @@ SRCS+= \
uthread_acl_set_fd.c \
uthread_acl_aclcheck_fd.c \
uthread_aio_suspend.c \
+ uthread_atfork.c \
uthread_attr_destroy.c \
uthread_attr_init.c \
uthread_attr_getdetachstate.c \
Index: libc_r/uthread/pthread_private.h
===================================================================
RCS file: /home/ncvs/src/lib/libc_r/uthread/pthread_private.h,v
retrieving revision 1.82
diff -u -p -r1.82 pthread_private.h
--- libc_r/uthread/pthread_private.h 22 Jan 2004 19:15:08 -0000 1.82
+++ libc_r/uthread/pthread_private.h 19 May 2004 21:22:01 -0000
@@ -446,6 +446,13 @@ struct pthread_cleanup {
void *routine_arg;
};
+struct pthread_atfork {
+ TAILQ_ENTRY(pthread_atfork) qe;
+ void (*prepare)(void);
+ void (*parent)(void);
+ void (*child)(void);
+};
+
struct pthread_attr {
int sched_policy;
int sched_inherit;
@@ -1020,6 +1027,9 @@ SCLASS struct pthread *_thread_initial
;
#endif
+SCLASS TAILQ_HEAD(atfork_head, pthread_atfork) _atfork_list;
+SCLASS pthread_mutex_t _atfork_mutex;
+
/* Default thread attributes: */
SCLASS struct pthread_attr _pthread_attr_default
#ifdef GLOBAL_PTHREAD_PRIVATE
Index: libc_r/uthread/uthread_atfork.c
===================================================================
RCS file: libc_r/uthread/uthread_atfork.c
diff -N libc_r/uthread/uthread_atfork.c
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ libc_r/uthread/uthread_atfork.c 19 May 2004 21:30:33 -0000
@@ -0,0 +1,56 @@
+/*
+ * Copyright (c) 2003 Daniel Eischen <deischen at freebsd.org>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Neither the name of the author nor the names of any co-contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD: src/lib/libpthread/thread/thr_atfork.c,v 1.1 2003/11/05 03:42:10 davidxu Exp $
+ */
+#include <errno.h>
+#include <stdlib.h>
+#include <pthread.h>
+#include <sys/queue.h>
+#include "pthread_private.h"
+
+__weak_reference(_pthread_atfork, pthread_atfork);
+
+int
+_pthread_atfork(void (*prepare)(void), void (*parent)(void),
+ void (*child)(void))
+{
+ struct pthread_atfork *af;
+
+ if (_thread_initial == NULL)
+ _thread_init();
+
+ if ((af = malloc(sizeof(struct pthread_atfork))) == NULL)
+ return (ENOMEM);
+
+ af->prepare = prepare;
+ af->parent = parent;
+ af->child = child;
+ _pthread_mutex_lock(&_atfork_mutex);
+ TAILQ_INSERT_TAIL(&_atfork_list, af, qe);
+ _pthread_mutex_unlock(&_atfork_mutex);
+ return (0);
+}
+
Index: libc_r/uthread/uthread_fork.c
===================================================================
RCS file: /home/ncvs/src/lib/libc_r/uthread/uthread_fork.c,v
retrieving revision 1.27
diff -u -p -r1.27 uthread_fork.c
--- libc_r/uthread/uthread_fork.c 19 Mar 2002 22:58:56 -0000 1.27
+++ libc_r/uthread/uthread_fork.c 19 May 2004 21:26:32 -0000
@@ -48,6 +48,7 @@ pid_t
_fork(void)
{
struct pthread *curthread = _get_curthread();
+ struct pthread_atfork *af;
int i, flags, use_deadlist = 0;
pid_t ret;
pthread_t pthread;
@@ -59,9 +60,23 @@ _fork(void)
*/
_thread_kern_sig_defer();
+ _pthread_mutex_lock(&_atfork_mutex);
+
+ /* Run down atfork prepare handlers. */
+ TAILQ_FOREACH_REVERSE(af, &_atfork_list, atfork_head, qe) {
+ if (af->prepare != NULL)
+ af->prepare();
+ }
+
/* Fork a new process: */
if ((ret = __sys_fork()) != 0) {
- /* Parent process or error. Nothing to do here. */
+ /* Run down atfork parent handlers. */
+ TAILQ_FOREACH(af, &_atfork_list, qe) {
+ if (af->parent != NULL)
+ af->parent();
+ }
+ _pthread_mutex_unlock(&_atfork_mutex);
+
} else {
/* Close the pthread kernel pipe: */
__sys_close(_thread_kern_pipe[0]);
@@ -207,8 +222,15 @@ _fork(void)
}
}
}
+ /* Run down atfork child handlers. */
+ TAILQ_FOREACH(af, &_atfork_list, qe) {
+ if (af->child != NULL)
+ af->child();
+ }
+ _mutex_reinit(&_atfork_mutex);
}
+
/*
* Undefer and handle pending signals, yielding if necessary:
*/
Index: libc_r/uthread/uthread_init.c
===================================================================
RCS file: /home/ncvs/src/lib/libc_r/uthread/uthread_init.c,v
retrieving revision 1.46
diff -u -p -r1.46 uthread_init.c
--- libc_r/uthread/uthread_init.c 3 Dec 2003 06:54:40 -0000 1.46
+++ libc_r/uthread/uthread_init.c 19 May 2004 21:25:29 -0000
@@ -438,6 +438,8 @@ _thread_init(void)
TAILQ_INIT(&_thread_list);
TAILQ_INSERT_HEAD(&_thread_list, _thread_initial, tle);
_set_curthread(_thread_initial);
+ TAILQ_INIT(&_atfork_list);
+ _pthread_mutex_init(&_atfork_mutex, NULL);
/* Initialise the global signal action structure: */
sigfillset(&act.sa_mask);
>Release-Note:
>Audit-Trail:
>Unformatted:
More information about the freebsd-bugs
mailing list