diff options
-rw-r--r-- | lib/libc_r/TEST/test_fork.c | 89 | ||||
-rw-r--r-- | lib/libc_r/uthread/uthread_atfork.c | 93 | ||||
-rw-r--r-- | lib/libc_r/uthread/uthread_fork.c | 8 | ||||
-rw-r--r-- | lib/libpthread/uthread/uthread_atfork.c | 93 | ||||
-rw-r--r-- | lib/libpthread/uthread/uthread_fork.c | 8 |
5 files changed, 280 insertions, 11 deletions
diff --git a/lib/libc_r/TEST/test_fork.c b/lib/libc_r/TEST/test_fork.c index efaafb0a7ba..bf0cc583c80 100644 --- a/lib/libc_r/TEST/test_fork.c +++ b/lib/libc_r/TEST/test_fork.c @@ -20,25 +20,78 @@ void * sleeper(void *arg) { + pthread_set_name_np(pthread_self(), "slpr"); - printf("sleeper\n"); sleep(10); PANIC("sleeper timed out"); } +static pid_t parent_pid; + static void sigchld(sig) int sig; { int status; + /* we should have got a SIGCHLD */ ASSERT(sig == SIGCHLD); + /* We should be the parent */ + ASSERT(getpid() == parent_pid); + /* wait for any child */ CHECKe(wait(&status)); + /* the child should have called exit(0) */ ASSERT(WIFEXITED(status)); ASSERT(WEXITSTATUS(status) == 0); + printf("parent ok\n"); SUCCEED; } +static int atfork_state = 0; + +void +atfork_child2() +{ + ASSERT(atfork_state++ == 3); + ASSERT(getpid() != parent_pid); +} + +void +atfork_parent2() +{ + ASSERT(atfork_state++ == 3); + ASSERT(getpid() == parent_pid); +} + +void +atfork_prepare2() +{ + ASSERT(atfork_state++ == 0); + ASSERT(getpid() == parent_pid); +} + + +void +atfork_child1() +{ + ASSERT(atfork_state++ == 2); + ASSERT(getpid() != parent_pid); +} + +void +atfork_parent1() +{ + ASSERT(atfork_state++ == 2); + ASSERT(getpid() == parent_pid); +} + +void +atfork_prepare1() +{ + ASSERT(atfork_state++ == 1); + ASSERT(getpid() == parent_pid); +} + int main() { @@ -46,6 +99,8 @@ main() pid_t pid; pthread_t sleeper_thread; + parent_pid = getpid(); + CHECKe(flags = fcntl(STDOUT_FILENO, F_GETFL)); if ((flags & (O_NONBLOCK | O_NDELAY))) { CHECKe(fcntl(STDOUT_FILENO, F_SETFL, @@ -57,21 +112,37 @@ main() CHECKe(signal(SIGCHLD, sigchld)); + /* Install some atfork handlers */ + + CHECKr(pthread_atfork(&atfork_prepare1, &atfork_parent1, + &atfork_child1)); + CHECKr(pthread_atfork(&atfork_prepare2, &atfork_parent2, + &atfork_child2)); + printf("forking\n"); CHECKe(pid = fork()); switch(pid) { case 0: - sleep(1); - printf("child process %d\n", getpid()); - _thread_dump_info(); - printf("\n"); + /* child: */ + /* Our pid should change */ + ASSERT(getpid() != parent_pid); + /* Our sleeper thread should have disappeared */ + ASSERT(ESRCH == pthread_cancel(sleeper_thread)); + /* The atfork handler should have run */ + ASSERT(atfork_state++ == 4); + printf("child ok\n"); _exit(0); - PANIC("_exit"); + PANIC("child _exit"); default: - printf("parent process %d [child %d]\n", getpid(), pid); - _thread_dump_info(); - printf("\n"); + /* parent: */ + /* Our pid should stay the same */ + ASSERT(getpid() == parent_pid); + /* Our sleeper thread should still be around */ + CHECKr(pthread_cancel(sleeper_thread)); + /* The atfork handler should have run */ + ASSERT(atfork_state++ == 4); + /* wait for the SIGCHLD from the child */ CHECKe(pause()); PANIC("pause"); } diff --git a/lib/libc_r/uthread/uthread_atfork.c b/lib/libc_r/uthread/uthread_atfork.c new file mode 100644 index 00000000000..a3ea65e8288 --- /dev/null +++ b/lib/libc_r/uthread/uthread_atfork.c @@ -0,0 +1,93 @@ +/* + * David Leonard <d@openbsd.org>, 1999. Public domain. + * $OpenBSD: uthread_atfork.c,v 1.1 1999/01/17 23:46:26 d Exp $ + */ + +#include <stdlib.h> +#include <sys/queue.h> +#ifdef _THREAD_SAFE +#include <pthread.h> +#include "pthread_private.h" + +struct atfork_entry { + void (*handler)(void); + TAILQ_ENTRY(atfork_entry) entries; +}; + +static TAILQ_HEAD(atfork_list, atfork_entry) atfork_head[3] = + { TAILQ_HEAD_INITIALIZER(atfork_head[PTHREAD_ATFORK_PREPARE]), + TAILQ_HEAD_INITIALIZER(atfork_head[PTHREAD_ATFORK_PARENT]), + TAILQ_HEAD_INITIALIZER(atfork_head[PTHREAD_ATFORK_CHILD]) }; + +void +_thread_atfork(which) +{ + struct atfork_list *head; + struct atfork_entry *ae; + + head = &atfork_head[which]; + + /* Call the fork handlers in order: */ + for (ae = head->tqh_first; ae != NULL; ae = ae->entries.tqe_next) + (*ae->handler)(); +} + +int +pthread_atfork(prepare, parent, child) + void (*prepare)(void); + void (*parent)(void); + void (*child)(void); +{ + int ret = 0; + struct atfork_entry *prepare_entry = NULL; + struct atfork_entry *parent_entry = NULL; + struct atfork_entry *child_entry = NULL; + + if (ret == 0 && prepare != NULL) { + /* Allocate space for the prepare handler: */ + if ((prepare_entry = malloc(sizeof *prepare_entry)) != NULL) + prepare_entry->handler = prepare; + else + ret = -1; + } + + if (ret == 0 && parent != NULL) { + /* Allocate space for the parent handler: */ + if ((parent_entry = malloc(sizeof *parent_entry)) != NULL) + parent_entry->handler = parent; + else + ret = -1; + } + + if (ret == 0 && child != NULL) { + /* Allocate space for the child handler: */ + if ((child_entry = malloc(sizeof *child_entry)) != NULL) + child_entry->handler = child; + else + ret = -1; + } + + if (ret == 0) { + /* Insert the handlers into the handler lists: */ + if (prepare_entry != NULL) + TAILQ_INSERT_HEAD(&atfork_head[PTHREAD_ATFORK_PREPARE], + prepare_entry, entries); + if (parent_entry != NULL) + TAILQ_INSERT_TAIL(&atfork_head[PTHREAD_ATFORK_PARENT], + parent_entry, entries); + if (child_entry != NULL) + TAILQ_INSERT_TAIL(&atfork_head[PTHREAD_ATFORK_CHILD], + child_entry, entries); + } else { + /* Release unused resources: */ + if (prepare_entry) + free(prepare_entry); + if (child_entry) + free(child_entry); + if (parent_entry) + free(parent_entry); + } + + return (ret); +} +#endif diff --git a/lib/libc_r/uthread/uthread_fork.c b/lib/libc_r/uthread/uthread_fork.c index 7f532f38adb..913e79e0bdd 100644 --- a/lib/libc_r/uthread/uthread_fork.c +++ b/lib/libc_r/uthread/uthread_fork.c @@ -29,7 +29,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $OpenBSD: uthread_fork.c,v 1.3 1999/01/06 05:29:23 d Exp $ + * $OpenBSD: uthread_fork.c,v 1.4 1999/01/17 23:46:26 d Exp $ */ #include <errno.h> #include <string.h> @@ -48,12 +48,17 @@ fork(void) pthread_t pthread; pthread_t pthread_next; + /* Call atfork handlers: */ + _thread_atfork(PTHREAD_ATFORK_PREPARE); + /* Lock the thread list: */ _lock_thread_list(); /* Fork a new process: */ if ((ret = _thread_sys_fork()) != 0) { /* Parent process or error. Nothing to do here. */ + if (ret > 0) + _thread_atfork(PTHREAD_ATFORK_PARENT); } else { /* Close the pthread kernel pipe: */ _thread_sys_close(_thread_kern_pipe[0]); @@ -121,6 +126,7 @@ fork(void) /* Point to the next thread: */ pthread = pthread_next; } + _thread_atfork(PTHREAD_ATFORK_CHILD); } } diff --git a/lib/libpthread/uthread/uthread_atfork.c b/lib/libpthread/uthread/uthread_atfork.c new file mode 100644 index 00000000000..a3ea65e8288 --- /dev/null +++ b/lib/libpthread/uthread/uthread_atfork.c @@ -0,0 +1,93 @@ +/* + * David Leonard <d@openbsd.org>, 1999. Public domain. + * $OpenBSD: uthread_atfork.c,v 1.1 1999/01/17 23:46:26 d Exp $ + */ + +#include <stdlib.h> +#include <sys/queue.h> +#ifdef _THREAD_SAFE +#include <pthread.h> +#include "pthread_private.h" + +struct atfork_entry { + void (*handler)(void); + TAILQ_ENTRY(atfork_entry) entries; +}; + +static TAILQ_HEAD(atfork_list, atfork_entry) atfork_head[3] = + { TAILQ_HEAD_INITIALIZER(atfork_head[PTHREAD_ATFORK_PREPARE]), + TAILQ_HEAD_INITIALIZER(atfork_head[PTHREAD_ATFORK_PARENT]), + TAILQ_HEAD_INITIALIZER(atfork_head[PTHREAD_ATFORK_CHILD]) }; + +void +_thread_atfork(which) +{ + struct atfork_list *head; + struct atfork_entry *ae; + + head = &atfork_head[which]; + + /* Call the fork handlers in order: */ + for (ae = head->tqh_first; ae != NULL; ae = ae->entries.tqe_next) + (*ae->handler)(); +} + +int +pthread_atfork(prepare, parent, child) + void (*prepare)(void); + void (*parent)(void); + void (*child)(void); +{ + int ret = 0; + struct atfork_entry *prepare_entry = NULL; + struct atfork_entry *parent_entry = NULL; + struct atfork_entry *child_entry = NULL; + + if (ret == 0 && prepare != NULL) { + /* Allocate space for the prepare handler: */ + if ((prepare_entry = malloc(sizeof *prepare_entry)) != NULL) + prepare_entry->handler = prepare; + else + ret = -1; + } + + if (ret == 0 && parent != NULL) { + /* Allocate space for the parent handler: */ + if ((parent_entry = malloc(sizeof *parent_entry)) != NULL) + parent_entry->handler = parent; + else + ret = -1; + } + + if (ret == 0 && child != NULL) { + /* Allocate space for the child handler: */ + if ((child_entry = malloc(sizeof *child_entry)) != NULL) + child_entry->handler = child; + else + ret = -1; + } + + if (ret == 0) { + /* Insert the handlers into the handler lists: */ + if (prepare_entry != NULL) + TAILQ_INSERT_HEAD(&atfork_head[PTHREAD_ATFORK_PREPARE], + prepare_entry, entries); + if (parent_entry != NULL) + TAILQ_INSERT_TAIL(&atfork_head[PTHREAD_ATFORK_PARENT], + parent_entry, entries); + if (child_entry != NULL) + TAILQ_INSERT_TAIL(&atfork_head[PTHREAD_ATFORK_CHILD], + child_entry, entries); + } else { + /* Release unused resources: */ + if (prepare_entry) + free(prepare_entry); + if (child_entry) + free(child_entry); + if (parent_entry) + free(parent_entry); + } + + return (ret); +} +#endif diff --git a/lib/libpthread/uthread/uthread_fork.c b/lib/libpthread/uthread/uthread_fork.c index 7f532f38adb..913e79e0bdd 100644 --- a/lib/libpthread/uthread/uthread_fork.c +++ b/lib/libpthread/uthread/uthread_fork.c @@ -29,7 +29,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $OpenBSD: uthread_fork.c,v 1.3 1999/01/06 05:29:23 d Exp $ + * $OpenBSD: uthread_fork.c,v 1.4 1999/01/17 23:46:26 d Exp $ */ #include <errno.h> #include <string.h> @@ -48,12 +48,17 @@ fork(void) pthread_t pthread; pthread_t pthread_next; + /* Call atfork handlers: */ + _thread_atfork(PTHREAD_ATFORK_PREPARE); + /* Lock the thread list: */ _lock_thread_list(); /* Fork a new process: */ if ((ret = _thread_sys_fork()) != 0) { /* Parent process or error. Nothing to do here. */ + if (ret > 0) + _thread_atfork(PTHREAD_ATFORK_PARENT); } else { /* Close the pthread kernel pipe: */ _thread_sys_close(_thread_kern_pipe[0]); @@ -121,6 +126,7 @@ fork(void) /* Point to the next thread: */ pthread = pthread_next; } + _thread_atfork(PTHREAD_ATFORK_CHILD); } } |