1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
|
/*
* Copyright (c) 2000-2001 Sendmail, Inc. and its suppliers.
* All rights reserved.
*
* By using this file, you agree to the terms and conditions set
* forth in the LICENSE file which can be found at the top level of
* the sendmail distribution.
*/
#include <sm/gen.h>
SM_RCSID("@(#)$Sendmail: signal.c,v 1.13 2001/08/14 16:05:47 ca Exp $")
#if SM_CONF_SETITIMER
# include <sys/time.h>
#endif /* SM_CONF_SETITIMER */
#include <errno.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <sm/clock.h>
#include <sm/signal.h>
#include <signal.h>
#include <sm/string.h>
unsigned int volatile InCriticalSection; /* >0 if inside critical section */
int volatile PendingSignal; /* pending signal to resend */
/*
** SM_SIGNAL -- set a signal handler
**
** This is essentially old BSD "signal(3)".
**
** NOTE: THIS CAN BE CALLED FROM A SIGNAL HANDLER. DO NOT ADD
** ANYTHING TO THIS ROUTINE UNLESS YOU KNOW WHAT YOU ARE
** DOING.
*/
sigfunc_t
sm_signal(sig, handler)
int sig;
sigfunc_t handler;
{
# if defined(SA_RESTART) || (!defined(SYS5SIGNALS) && !defined(BSD4_3))
struct sigaction n, o;
# endif /* defined(SA_RESTART) || (!defined(SYS5SIGNALS) && !defined(BSD4_3)) */
/*
** First, try for modern signal calls
** and restartable syscalls
*/
# ifdef SA_RESTART
(void) memset(&n, '\0', sizeof n);
# if USE_SA_SIGACTION
n.sa_sigaction = (void(*)(int, siginfo_t *, void *)) handler;
n.sa_flags = SA_RESTART|SA_SIGINFO;
# else /* USE_SA_SIGACTION */
n.sa_handler = handler;
n.sa_flags = SA_RESTART;
# endif /* USE_SA_SIGACTION */
if (sigaction(sig, &n, &o) < 0)
return SIG_ERR;
return o.sa_handler;
# else /* SA_RESTART */
/*
** Else check for SYS5SIGNALS or
** BSD4_3 signals
*/
# if defined(SYS5SIGNALS) || defined(BSD4_3)
# ifdef BSD4_3
return signal(sig, handler);
# else /* BSD4_3 */
return sigset(sig, handler);
# endif /* BSD4_3 */
# else /* defined(SYS5SIGNALS) || defined(BSD4_3) */
/*
** Finally, if nothing else is available,
** go for a default
*/
(void) memset(&n, '\0', sizeof n);
n.sa_handler = handler;
if (sigaction(sig, &n, &o) < 0)
return SIG_ERR;
return o.sa_handler;
# endif /* defined(SYS5SIGNALS) || defined(BSD4_3) */
# endif /* SA_RESTART */
}
/*
** SM_BLOCKSIGNAL -- hold a signal to prevent delivery
**
** Parameters:
** sig -- the signal to block.
**
** Returns:
** 1 signal was previously blocked
** 0 signal was not previously blocked
** -1 on failure.
*/
int
sm_blocksignal(sig)
int sig;
{
# ifdef BSD4_3
# ifndef sigmask
# define sigmask(s) (1 << ((s) - 1))
# endif /* ! sigmask */
return (sigblock(sigmask(sig)) & sigmask(sig)) != 0;
# else /* BSD4_3 */
# ifdef ALTOS_SYSTEM_V
sigfunc_t handler;
handler = sigset(sig, SIG_HOLD);
if (handler == SIG_ERR)
return -1;
else
return handler == SIG_HOLD;
# else /* ALTOS_SYSTEM_V */
sigset_t sset, oset;
(void) sigemptyset(&sset);
(void) sigaddset(&sset, sig);
if (sigprocmask(SIG_BLOCK, &sset, &oset) < 0)
return -1;
else
return sigismember(&oset, sig);
# endif /* ALTOS_SYSTEM_V */
# endif /* BSD4_3 */
}
/*
** SM_RELEASESIGNAL -- release a held signal
**
** Parameters:
** sig -- the signal to release.
**
** Returns:
** 1 signal was previously blocked
** 0 signal was not previously blocked
** -1 on failure.
*/
int
sm_releasesignal(sig)
int sig;
{
# ifdef BSD4_3
return (sigsetmask(sigblock(0) & ~sigmask(sig)) & sigmask(sig)) != 0;
# else /* BSD4_3 */
# ifdef ALTOS_SYSTEM_V
sigfunc_t handler;
handler = sigset(sig, SIG_HOLD);
if (sigrelse(sig) < 0)
return -1;
else
return handler == SIG_HOLD;
# else /* ALTOS_SYSTEM_V */
sigset_t sset, oset;
(void) sigemptyset(&sset);
(void) sigaddset(&sset, sig);
if (sigprocmask(SIG_UNBLOCK, &sset, &oset) < 0)
return -1;
else
return sigismember(&oset, sig);
# endif /* ALTOS_SYSTEM_V */
# endif /* BSD4_3 */
}
/*
** PEND_SIGNAL -- Add a signal to the pending signal list
**
** Parameters:
** sig -- signal to add
**
** Returns:
** none.
**
** NOTE: THIS CAN BE CALLED FROM A SIGNAL HANDLER. DO NOT ADD
** ANYTHING TO THIS ROUTINE UNLESS YOU KNOW WHAT YOU ARE
** DOING.
*/
void
pend_signal(sig)
int sig;
{
int sigbit;
int save_errno = errno;
#if SM_CONF_SETITIMER
struct itimerval clr;
#endif /* SM_CONF_SETITIMER */
/*
** Don't want to interrupt something critical, hence delay
** the alarm for one second. Hopefully, by then we
** will be out of the critical section. If not, then
** we will just delay again. The events to be run will
** still all be run, maybe just a little bit late.
*/
switch (sig)
{
case SIGHUP:
sigbit = PEND_SIGHUP;
break;
case SIGINT:
sigbit = PEND_SIGINT;
break;
case SIGTERM:
sigbit = PEND_SIGTERM;
break;
case SIGUSR1:
sigbit = PEND_SIGUSR1;
break;
case SIGALRM:
/* don't have to pend these */
sigbit = 0;
break;
default:
/* If we get here, we are in trouble */
abort();
/* NOTREACHED */
/* shut up stupid compiler warning on HP-UX 11 */
sigbit = 0;
break;
}
if (sigbit != 0)
PendingSignal |= sigbit;
(void) sm_signal(SIGALRM, sm_tick);
#if SM_CONF_SETITIMER
clr.it_interval.tv_sec = 0;
clr.it_interval.tv_usec = 0;
clr.it_value.tv_sec = 1;
clr.it_value.tv_usec = 0;
(void) setitimer(ITIMER_REAL, &clr, NULL);
#else /* SM_CONF_SETITIMER */
(void) alarm(1);
#endif /* SM_CONF_SETITIMER */
errno = save_errno;
}
/*
** SM_ALLSIGNALS -- act on all signals
**
** Parameters:
** block -- whether to block or release all signals.
**
** Returns:
** none.
*/
void
sm_allsignals(block)
bool block;
{
# ifdef BSD4_3
# ifndef sigmask
# define sigmask(s) (1 << ((s) - 1))
# endif /* ! sigmask */
if (block)
{
int mask = 0;
mask |= sigmask(SIGALRM);
mask |= sigmask(SIGCHLD);
mask |= sigmask(SIGHUP);
mask |= sigmask(SIGINT);
mask |= sigmask(SIGTERM);
mask |= sigmask(SIGUSR1);
(void) sigblock(mask);
}
else
sigsetmask(0);
# else /* BSD4_3 */
# ifdef ALTOS_SYSTEM_V
if (block)
{
(void) sigset(SIGALRM, SIG_HOLD);
(void) sigset(SIGCHLD, SIG_HOLD);
(void) sigset(SIGHUP, SIG_HOLD);
(void) sigset(SIGINT, SIG_HOLD);
(void) sigset(SIGTERM, SIG_HOLD);
(void) sigset(SIGUSR1, SIG_HOLD);
}
else
{
(void) sigset(SIGALRM, SIG_DFL);
(void) sigset(SIGCHLD, SIG_DFL);
(void) sigset(SIGHUP, SIG_DFL);
(void) sigset(SIGINT, SIG_DFL);
(void) sigset(SIGTERM, SIG_DFL);
(void) sigset(SIGUSR1, SIG_DFL);
}
# else /* ALTOS_SYSTEM_V */
sigset_t sset;
(void) sigemptyset(&sset);
(void) sigaddset(&sset, SIGALRM);
(void) sigaddset(&sset, SIGCHLD);
(void) sigaddset(&sset, SIGHUP);
(void) sigaddset(&sset, SIGINT);
(void) sigaddset(&sset, SIGTERM);
(void) sigaddset(&sset, SIGUSR1);
(void) sigprocmask(block ? SIG_BLOCK : SIG_UNBLOCK, &sset, NULL);
# endif /* ALTOS_SYSTEM_V */
# endif /* BSD4_3 */
}
/*
** SM_SIGNAL_NOOP -- A signal no-op function
**
** Parameters:
** sig -- signal received
**
** Returns:
** SIGFUNC_RETURN
*/
/* ARGSUSED */
SIGFUNC_DECL
sm_signal_noop(sig)
int sig;
{
int save_errno = errno;
FIX_SYSV_SIGNAL(sig, sm_signal_noop);
errno = save_errno;
return SIGFUNC_RETURN;
}
|