summaryrefslogtreecommitdiff
path: root/lib/libpthread
diff options
context:
space:
mode:
authorBrad Smith <brad@cvs.openbsd.org>2005-10-30 03:37:35 +0000
committerBrad Smith <brad@cvs.openbsd.org>2005-10-30 03:37:35 +0000
commit62b1ec6e3667b903b262c213211453eadd3f1f93 (patch)
treeab5296a9073f4c6c817bd4ab531b69b2d76522c2 /lib/libpthread
parentdec824965a7de11d9973b93c5288156da4541f74 (diff)
Add pthread_atfork(3)
From FreeBSD 'looks ok' fgsch@ miod@ man page reviewed by jmc@
Diffstat (limited to 'lib/libpthread')
-rw-r--r--lib/libpthread/man/Makefile.inc3
-rw-r--r--lib/libpthread/man/pthread_atfork.373
-rw-r--r--lib/libpthread/man/pthreads.312
-rw-r--r--lib/libpthread/shlib_version2
-rw-r--r--lib/libpthread/uthread/Makefile.inc3
-rw-r--r--lib/libpthread/uthread/pthread_private.h12
-rw-r--r--lib/libpthread/uthread/uthread_atfork.c58
-rw-r--r--lib/libpthread/uthread/uthread_fork.c24
-rw-r--r--lib/libpthread/uthread/uthread_init.c4
9 files changed, 183 insertions, 8 deletions
diff --git a/lib/libpthread/man/Makefile.inc b/lib/libpthread/man/Makefile.inc
index 604c27760d2..680ee64ca41 100644
--- a/lib/libpthread/man/Makefile.inc
+++ b/lib/libpthread/man/Makefile.inc
@@ -1,4 +1,4 @@
-# $OpenBSD: Makefile.inc,v 1.20 2005/10/12 16:00:35 jmc Exp $
+# $OpenBSD: Makefile.inc,v 1.21 2005/10/30 03:37:34 brad Exp $
# $FreeBSD: Makefile.inc,v 1.6 1999/08/28 00:03:02 peter Exp $
# POSIX thread man files
@@ -6,6 +6,7 @@
.PATH: ${SRCDIR}/man
MAN+= \
+ pthread_atfork.3 \
pthread_attr_init.3 \
pthread_attr_setstackaddr.3 \
pthread_attr_setstacksize.3 \
diff --git a/lib/libpthread/man/pthread_atfork.3 b/lib/libpthread/man/pthread_atfork.3
new file mode 100644
index 00000000000..452d3de62a3
--- /dev/null
+++ b/lib/libpthread/man/pthread_atfork.3
@@ -0,0 +1,73 @@
+.\"
+.\" $OpenBSD: pthread_atfork.3,v 1.4 2005/10/30 03:37:34 brad Exp $
+.\" $FreeBSD: src/share/man/man3/pthread_atfork.3,v 1.0 2004/06/21 15:59:00 xx Exp $
+.\"
+.Dd June 21, 2004
+.Dt PTHREAD_ATFORK 3
+.Os
+.Sh NAME
+.Nm pthread_atfork
+.Nd register fork handlers
+.Sh SYNOPSIS
+.Fd #include <pthread.h>
+.Pp
+.Ft int
+.Fn pthread_atfork "void (*prepare)(void)" "void (*parent)(void)" "void (*child)(void)"
+.Sh DESCRIPTION
+The
+.Fn pthread_atfork
+function declares fork handlers to be called before and after
+.Xr fork 2 ,
+in the context of the thread that called
+.Xr fork 2 .
+The
+.Fa prepare
+fork handler will be called before
+.Xr fork 2
+processing commences.
+The
+.Fa parent
+fork handle will be called after
+.Xr fork 2
+processing completes in the parent process.
+The
+.Fa child
+fork handler will be called after
+.Xr fork 2
+processing completes in the child process.
+If no handling is desired at one or more of these three points, the
+corresponding fork handler address(es) may be set to NULL.
+.Pp
+The order of calls to
+.Fn pthread_atfork
+is significant.
+The
+.Fa parent
+and
+.Fa child
+fork handlers will be called in the order in which they were
+established by calls to
+.Fn pthread_atfork .
+The
+.Fa prepare
+fork handlers will be called in the opposite order.
+.Sh RETURN VALUES
+Upon successful completion,
+.Fn pthread_atfork
+returns a value of zero; otherwise, an error number is returned
+to indicate the error.
+.Sh ERRORS
+The
+.Fn pthread_atfork
+function will fail if:
+.Bl -tag -width Er
+.It Bq Er ENOMEM
+Insufficient table space exists to record the fork handler addresses.
+.El
+.Sh SEE ALSO
+.Xr fork 2
+.Sh STANDARDS
+The
+.Fn pthread_atfork
+function is expected to conform to
+.St -p1003.1 .
diff --git a/lib/libpthread/man/pthreads.3 b/lib/libpthread/man/pthreads.3
index f13f6f1f3a9..31f00f217fd 100644
--- a/lib/libpthread/man/pthreads.3
+++ b/lib/libpthread/man/pthreads.3
@@ -1,4 +1,4 @@
-.\" $OpenBSD: pthreads.3,v 1.24 2004/05/01 22:15:10 marc Exp $
+.\" $OpenBSD: pthreads.3,v 1.25 2005/10/30 03:37:34 brad Exp $
.\" David Leonard <d@openbsd.org>, 1998. Public domain.
.Dd August 17, 1998
.Dt PTHREADS 3
@@ -48,6 +48,8 @@ Attribute Object Routines
.It
Cancellation Routines
.It
+Cleanup Routines
+.It
Condition Variable Routines
.It
Data Management Routines
@@ -81,6 +83,13 @@ Get stacksize attribute.
.It Fn pthread_attr_setstacksize
Set stacksize attribute.
.El
+.Ss Cleanup Routines
+The functions available are as follows:
+.Pp
+.Bl -tag -width "pthread_atfork()" -compact
+.It Fn pthread_atfork
+Register fork handlers.
+.El
.Ss Cancellation Routines
The functions available are as follows:
.Pp
@@ -482,6 +491,7 @@ The status display verbosity is controlled by the
environment variable.
.El
.Sh SEE ALSO
+.Xr pthread_atfork 3 ,
.Xr pthread_attr_init 3 ,
.Xr pthread_attr_setdetachstate 3 ,
.Xr pthread_attr_setstackaddr 3 ,
diff --git a/lib/libpthread/shlib_version b/lib/libpthread/shlib_version
index 1394f4e4035..689bf7b8582 100644
--- a/lib/libpthread/shlib_version
+++ b/lib/libpthread/shlib_version
@@ -1,2 +1,2 @@
major=6
-minor=1
+minor=2
diff --git a/lib/libpthread/uthread/Makefile.inc b/lib/libpthread/uthread/Makefile.inc
index 02c07c5481b..6ef4e88c344 100644
--- a/lib/libpthread/uthread/Makefile.inc
+++ b/lib/libpthread/uthread/Makefile.inc
@@ -1,4 +1,4 @@
-# $OpenBSD: Makefile.inc,v 1.18 2004/02/22 23:59:26 brad Exp $
+# $OpenBSD: Makefile.inc,v 1.19 2005/10/30 03:37:34 brad Exp $
# $FreeBSD: Makefile.inc,v 1.19 1999/08/28 00:03:19 peter Exp $
# uthread sources
@@ -8,6 +8,7 @@ CFLAGS += -I${SRCDIR}/arch/${MACHINE_ARCH}
SRCS+= \
uthread_accept.c \
+ uthread_atfork.c \
uthread_attr_destroy.c \
uthread_attr_init.c \
uthread_attr_getdetachstate.c \
diff --git a/lib/libpthread/uthread/pthread_private.h b/lib/libpthread/uthread/pthread_private.h
index aef55382c95..bfb038aa671 100644
--- a/lib/libpthread/uthread/pthread_private.h
+++ b/lib/libpthread/uthread/pthread_private.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: pthread_private.h,v 1.53 2004/06/07 21:11:23 marc Exp $ */
+/* $OpenBSD: pthread_private.h,v 1.54 2005/10/30 03:37:34 brad Exp $ */
/*
* Copyright (c) 1995-1998 John Birrell <jb@cimlogic.com.au>.
* All rights reserved.
@@ -345,6 +345,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;
@@ -901,6 +908,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
diff --git a/lib/libpthread/uthread/uthread_atfork.c b/lib/libpthread/uthread/uthread_atfork.c
new file mode 100644
index 00000000000..10e6f6be25b
--- /dev/null
+++ b/lib/libpthread/uthread/uthread_atfork.c
@@ -0,0 +1,58 @@
+/* $OpenBSD: uthread_atfork.c,v 1.3 2005/10/30 03:37:34 brad Exp $ */
+
+/*
+ * Copyright (c) 2003 Daniel Eischen <deischen@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: /repoman/r/ncvs/src/lib/libc_r/uthread/uthread_atfork.c,v 1.1 2004/12/10 03:36:45 grog Exp $
+ */
+
+#include <errno.h>
+#include <stdlib.h>
+#ifdef _THREAD_SAFE
+#include <pthread.h>
+#include <sys/queue.h>
+#include "pthread_private.h"
+
+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);
+}
+#endif
diff --git a/lib/libpthread/uthread/uthread_fork.c b/lib/libpthread/uthread/uthread_fork.c
index 02f7656859b..09a3279392a 100644
--- a/lib/libpthread/uthread/uthread_fork.c
+++ b/lib/libpthread/uthread/uthread_fork.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: uthread_fork.c,v 1.11 2003/08/06 21:08:05 millert Exp $ */
+/* $OpenBSD: uthread_fork.c,v 1.12 2005/10/30 03:37:34 brad Exp $ */
/*
* Copyright (c) 1995-1998 John Birrell <jb@cimlogic.com.au>
* All rights reserved.
@@ -45,6 +45,7 @@ pid_t
fork(void)
{
struct pthread *curthread = _get_curthread();
+ struct pthread_atfork *af;
int i, flags;
pid_t ret;
pthread_t pthread;
@@ -55,9 +56,22 @@ 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 = _thread_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: */
_thread_sys_close(_thread_kern_pipe[0]);
@@ -178,6 +192,12 @@ fork(void)
}
}
}
+ /* Run down atfork child handlers. */
+ TAILQ_FOREACH(af, &_atfork_list, qe) {
+ if (af->child != NULL)
+ af->child();
+ }
+ _mutex_reinit(&_atfork_mutex);
}
/*
diff --git a/lib/libpthread/uthread/uthread_init.c b/lib/libpthread/uthread/uthread_init.c
index 531753880e6..69a70e5a22a 100644
--- a/lib/libpthread/uthread/uthread_init.c
+++ b/lib/libpthread/uthread/uthread_init.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: uthread_init.c,v 1.32 2004/06/07 21:11:23 marc Exp $ */
+/* $OpenBSD: uthread_init.c,v 1.33 2005/10/30 03:37:34 brad Exp $ */
/*
* Copyright (c) 1995-1998 John Birrell <jb@cimlogic.com.au>
* All rights reserved.
@@ -289,6 +289,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);