Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/lib/libc/gen pthread_atfork(3): Block signals during the cal...



details:   https://anonhg.NetBSD.org/src/rev/2164c0ebaf2a
branches:  trunk
changeset: 370050:2164c0ebaf2a
user:      riastradh <riastradh%NetBSD.org@localhost>
date:      Tue Sep 13 10:18:47 2022 +0000

description:
pthread_atfork(3): Block signals during the call to pthread_atfork.

This doesn't affect the calls to the atfork handlers -- it only
protects access to the lists of handlers from interruption by a
signal, in case the signal handler calls fork(2).

diffstat:

 lib/libc/gen/pthread_atfork.c |  27 +++++++++++++++++----------
 1 files changed, 17 insertions(+), 10 deletions(-)

diffs (77 lines):

diff -r bf6ded12e015 -r 2164c0ebaf2a lib/libc/gen/pthread_atfork.c
--- a/lib/libc/gen/pthread_atfork.c     Tue Sep 13 10:15:28 2022 +0000
+++ b/lib/libc/gen/pthread_atfork.c     Tue Sep 13 10:18:47 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: pthread_atfork.c,v 1.16 2022/05/31 08:43:13 andvar Exp $       */
+/*     $NetBSD: pthread_atfork.c,v 1.17 2022/09/13 10:18:47 riastradh Exp $    */
 
 /*-
  * Copyright (c) 2002 The NetBSD Foundation, Inc.
@@ -31,7 +31,7 @@
 
 #include <sys/cdefs.h>
 #if defined(LIBC_SCCS) && !defined(lint)
-__RCSID("$NetBSD: pthread_atfork.c,v 1.16 2022/05/31 08:43:13 andvar Exp $");
+__RCSID("$NetBSD: pthread_atfork.c,v 1.17 2022/09/13 10:18:47 riastradh Exp $");
 #endif /* LIBC_SCCS and not lint */
 
 #include "namespace.h"
@@ -101,15 +101,20 @@
     void (*child)(void))
 {
        struct atfork_callback *newprepare, *newparent, *newchild;
+       sigset_t mask, omask;
+       int error;
 
        newprepare = newparent = newchild = NULL;
 
+       sigfillset(&mask);
+       thr_sigsetmask(SIG_SETMASK, &mask, &omask);
+
        mutex_lock(&atfork_lock);
        if (prepare != NULL) {
                newprepare = af_alloc();
                if (newprepare == NULL) {
-                       mutex_unlock(&atfork_lock);
-                       return ENOMEM;
+                       error = ENOMEM;
+                       goto out;
                }
                newprepare->fn = prepare;
        }
@@ -119,8 +124,8 @@
                if (newparent == NULL) {
                        if (newprepare != NULL)
                                af_free(newprepare);
-                       mutex_unlock(&atfork_lock);
-                       return ENOMEM;
+                       error = ENOMEM;
+                       goto out;
                }
                newparent->fn = parent;
        }
@@ -132,8 +137,8 @@
                                af_free(newprepare);
                        if (newparent != NULL)
                                af_free(newparent);
-                       mutex_unlock(&atfork_lock);
-                       return ENOMEM;
+                       error = ENOMEM;
+                       goto out;
                }
                newchild->fn = child;
        }
@@ -150,9 +155,11 @@
                SIMPLEQ_INSERT_TAIL(&parentq, newparent, next);
        if (child)
                SIMPLEQ_INSERT_TAIL(&childq, newchild, next);
-       mutex_unlock(&atfork_lock);
+       error = 0;
 
-       return 0;
+out:   mutex_unlock(&atfork_lock);
+       thr_sigsetmask(SIG_SETMASK, &omask, NULL);
+       return error;
 }
 
 pid_t



Home | Main Index | Thread Index | Old Index