Allow passing in human-readable units for specifying amounts of space
[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 <stdlib.h>
19 #include <stdio.h>
20 #ifndef  AFS_NT40_ENV
21 #include <signal.h>
22 #include <unistd.h>
23 #else
24 #include <afs/procmgmt.h>
25 #endif
26 #include <pthread.h>
27
28 #include "pthread_nosigs.h"
29
30 /*------------------------------------------------------------------------
31  * Under Darwin 6.x (including 7.0), sigwait() is broken, so we use
32  * sigsuspend() instead.  We also don't block signals we don't know
33  * about, so they should kill us, rather than us returning zero status.
34  *------------------------------------------------------------------------*/
35
36 static pthread_t softsig_tid;
37 static struct {
38     void (*handler) (int);
39     int pending;
40 #if !(defined(AFS_DARWIN60_ENV) || (defined(AFS_NBSD_ENV) && !defined(AFS_NBSD50_ENV)))
41     int fatal;
42 #endif /* !defined(AFS_DARWIN60_ENV) || !defined(AFS_NBSD_ENV) */
43     int inited;
44 } softsig_sigs[NSIG];
45
46 static void *
47 softsig_thread(void *arg)
48 {
49     sigset_t ss, os;
50     int i;
51
52     sigemptyset(&ss);
53     /* get the list of signals _not_ blocked by AFS_SIGSET_CLEAR() */
54     pthread_sigmask(SIG_BLOCK, &ss, &os);
55     pthread_sigmask(SIG_SETMASK, &os, NULL);
56     sigaddset(&ss, SIGUSR1);
57 #if defined(AFS_DARWIN60_ENV) || (defined(AFS_NBSD_ENV) && !defined(AFS_NBSD50_ENV))
58     pthread_sigmask (SIG_BLOCK, &ss, NULL);
59     sigdelset (&os, SIGUSR1);
60 #else /* !defined(AFS_DARWIN60_ENV) && !defined(AFS_NBSD_ENV) */
61     for (i = 0; i < NSIG; i++) {
62         if (!sigismember(&os, i) && i != SIGSTOP && i != SIGKILL) {
63             sigaddset(&ss, i);
64             softsig_sigs[i].fatal = 1;
65         }
66     }
67 #endif /* defined(AFS_DARWIN60_ENV) || defined(AFS_NBSD_ENV) */
68
69     while (1) {
70         void (*h) (int);
71 #if !defined(AFS_DARwiN60_ENV) && !defined(AFS_NBSD_ENV)
72         int sigw;
73 #endif
74
75         h = NULL;
76
77         for (i = 0; i < NSIG; i++) {
78             if (softsig_sigs[i].handler && !softsig_sigs[i].inited) {
79                 sigaddset(&ss, i);
80 #if defined(AFS_DARWIN60_ENV) || (defined(AFS_NBSD_ENV) && !defined(AFS_NBSD50_ENV))
81                 pthread_sigmask (SIG_BLOCK, &ss, NULL);
82                 sigdelset (&os, i);
83 #endif /* defined(AFS_DARWIN60_ENV) || defined(AFS_NBSD_ENV) */
84                 softsig_sigs[i].inited = 1;
85             }
86             if (softsig_sigs[i].pending) {
87                 softsig_sigs[i].pending = 0;
88                 h = softsig_sigs[i].handler;
89                 break;
90             }
91         }
92         if (i == NSIG) {
93 #if defined(AFS_DARWIN60_ENV) || (defined(AFS_NBSD_ENV) && !defined(AFS_NBSD50_ENV))
94             sigsuspend (&os);
95 #else /* !defined(AFS_DARWIN60_ENV) && !defined(AFS_NBSD_ENV) */
96             sigwait(&ss, &sigw);
97             if (sigw != SIGUSR1) {
98                 if (softsig_sigs[sigw].fatal)
99                     exit(0);
100                 softsig_sigs[sigw].pending = 1;
101             }
102 #endif /* defined(AFS_DARWIN60_ENV) || defined(AFS_NBSD_ENV) */
103         } else if (h)
104             h(i);
105     }
106     return NULL;
107 }
108
109 static void
110 softsig_usr1(int signo)
111 {
112     signal (SIGUSR1, softsig_usr1);
113 }
114
115 void
116 softsig_init(void)
117 {
118     int rc;
119     AFS_SIGSET_DECL;
120     AFS_SIGSET_CLEAR();
121     rc = pthread_create(&softsig_tid, NULL, &softsig_thread, NULL);
122     assert(0 == rc);
123     AFS_SIGSET_RESTORE();
124     signal (SIGUSR1, softsig_usr1);
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(int argc, char **argv)
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