9891dea96107dbff815ce2aa59647131efed7c20
[openafs.git] / src / util / softsig.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  *
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  *
9  * Portions Copyright (c) 2003 Apple Computer, Inc.
10  */
11
12 #include <afsconfig.h>
13 #include <afs/param.h>
14
15 #define _POSIX_PTHREAD_SEMANTICS
16 #include <afs/param.h>
17 #include <assert.h>
18 #include <stdio.h>
19 #ifndef  AFS_NT40_ENV
20 #include <signal.h>
21 #include <unistd.h>
22 #else
23 #include <afs/procmgmt.h>
24 #endif
25 #include <pthread.h>
26
27 #include "pthread_nosigs.h"
28
29 /*------------------------------------------------------------------------
30  * Under Darwin 6.x (including 7.0), sigwait() is broken, so we use
31  * sigsuspend() instead.  We also don't block signals we don't know
32  * about, so they should kill us, rather than us returning zero status.
33  *------------------------------------------------------------------------*/
34
35 static pthread_t softsig_tid;
36 static struct {
37     void (*handler) (int);
38     int pending;
39 #if !defined(AFS_DARWIN60_ENV)
40     int fatal;
41 #endif /* !defined(AFS_DARWIN60_ENV) */
42     int inited;
43 } softsig_sigs[NSIG];
44
45 static void *
46 softsig_thread(void *arg)
47 {
48     sigset_t ss, os;
49     int i;
50
51     sigemptyset(&ss);
52     /* get the list of signals _not_ blocked by AFS_SIGSET_CLEAR() */
53     pthread_sigmask(SIG_BLOCK, &ss, &os);
54     pthread_sigmask(SIG_SETMASK, &os, NULL);
55     sigaddset(&ss, SIGUSR1);
56 #if defined(AFS_DARWIN60_ENV)
57     pthread_sigmask (SIG_BLOCK, &ss, NULL);
58     sigdelset (&os, SIGUSR1);
59 #else /* !defined(AFS_DARWIN60_ENV) */
60     for (i = 0; i < NSIG; i++) {
61         if (!sigismember(&os, i) && i != SIGSTOP && i != SIGKILL) {
62             sigaddset(&ss, i);
63             softsig_sigs[i].fatal = 1;
64         }
65     }
66 #endif /* defined(AFS_DARWIN60_ENV) */
67
68     while (1) {
69         void (*h) (int);
70         int sigw;
71
72         h = NULL;
73
74         for (i = 0; i < NSIG; i++) {
75             if (softsig_sigs[i].handler && !softsig_sigs[i].inited) {
76                 sigaddset(&ss, i);
77 #if defined(AFS_DARWIN60_ENV)
78                 pthread_sigmask (SIG_BLOCK, &ss, NULL);
79                 sigdelset (&os, i);
80 #endif /* defined(AFS_DARWIN60_ENV) */
81                 softsig_sigs[i].inited = 1;
82             }
83             if (softsig_sigs[i].pending) {
84                 softsig_sigs[i].pending = 0;
85                 h = softsig_sigs[i].handler;
86                 break;
87             }
88         }
89         if (i == NSIG) {
90 #if defined(AFS_DARWIN60_ENV)
91             sigsuspend (&os);
92 #else /* !defined(AFS_DARWIN60_ENV) */
93             sigwait(&ss, &sigw);
94             if (sigw != SIGUSR1) {
95                 if (softsig_sigs[sigw].fatal)
96                     exit(0);
97                 softsig_sigs[sigw].pending = 1;
98             }
99 #endif /* defined(AFS_DARWIN60_ENV) */
100         } else if (h)
101             h(i);
102     }
103 }
104
105 #if defined(AFS_DARWIN60_ENV)
106 static void
107 softsig_usr1(int signo)
108 {
109     signal (SIGUSR1, softsig_usr1);
110 }
111 #endif /* defined(AFS_DARWIN60_ENV) */
112
113 void
114 softsig_init()
115 {
116     int rc;
117     AFS_SIGSET_DECL;
118     AFS_SIGSET_CLEAR();
119     rc = pthread_create(&softsig_tid, NULL, &softsig_thread, NULL);
120     assert(0 == rc);
121     AFS_SIGSET_RESTORE();
122 #if defined(AFS_DARWIN60_ENV)
123     signal (SIGUSR1, softsig_usr1);
124 #endif /* defined(AFS_DARWIN60_ENV) */
125 }
126
127 static void
128 softsig_handler(int signo)
129 {
130     signal(signo, softsig_handler);
131     softsig_sigs[signo].pending = 1;
132     pthread_kill(softsig_tid, SIGUSR1);
133 }
134
135 void
136 softsig_signal(int signo, void (*handler) (int))
137 {
138     softsig_sigs[signo].handler = handler;
139     softsig_sigs[signo].inited = 0;
140     signal(signo, softsig_handler);
141     pthread_kill(softsig_tid, SIGUSR1);
142 }
143
144 #if defined(TEST)
145 static void
146 print_foo(int signo)
147 {
148     printf("foo, signo = %d, tid = %d\n", signo, pthread_self());
149 }
150
151 int
152 main()
153 {
154     softsig_init();
155     softsig_signal(SIGINT, print_foo);
156     printf("main is tid %d\n", pthread_self());
157     while (1)
158         sleep(60);
159 }
160 #endif