2 * Copyright (c) 2000-2001 Proofpoint, Inc. and its suppliers.
5 * By using this file, you agree to the terms and conditions set
6 * forth in the LICENSE file which can be found at the top level of
7 * the sendmail distribution.
11 SM_RCSID("@(#)$Id: signal.c,v 1.18 2013-11-22 20:51:43 ca Exp $")
21 #include <sm/signal.h>
23 #include <sm/string.h>
25 unsigned int volatile InCriticalSection; /* >0 if inside critical section */
26 int volatile PendingSignal; /* pending signal to resend */
29 ** SM_SIGNAL -- set a signal handler
31 ** This is essentially old BSD "signal(3)".
33 ** NOTE: THIS CAN BE CALLED FROM A SIGNAL HANDLER. DO NOT ADD
34 ** ANYTHING TO THIS ROUTINE UNLESS YOU KNOW WHAT YOU ARE
39 sm_signal(sig, handler)
43 #if defined(SA_RESTART) || (!defined(SYS5SIGNALS) && !defined(BSD4_3))
44 struct sigaction n, o;
48 ** First, try for modern signal calls
49 ** and restartable syscalls
53 (void) memset(&n, '\0', sizeof n);
55 n.sa_sigaction = (void(*)(int, siginfo_t *, void *)) handler;
56 n.sa_flags = SA_RESTART|SA_SIGINFO;
58 n.sa_handler = handler;
59 n.sa_flags = SA_RESTART;
61 if (sigaction(sig, &n, &o) < 0)
64 #else /* SA_RESTART */
67 ** Else check for SYS5SIGNALS or
71 # if defined(SYS5SIGNALS) || defined(BSD4_3)
73 return signal(sig, handler);
75 return sigset(sig, handler);
77 # else /* defined(SYS5SIGNALS) || defined(BSD4_3) */
80 ** Finally, if nothing else is available,
84 (void) memset(&n, '\0', sizeof n);
85 n.sa_handler = handler;
86 if (sigaction(sig, &n, &o) < 0)
89 # endif /* defined(SYS5SIGNALS) || defined(BSD4_3) */
90 #endif /* SA_RESTART */
93 ** SM_BLOCKSIGNAL -- hold a signal to prevent delivery
96 ** sig -- the signal to block.
99 ** 1 signal was previously blocked
100 ** 0 signal was not previously blocked
110 # define sigmask(s) (1 << ((s) - 1))
112 return (sigblock(sigmask(sig)) & sigmask(sig)) != 0;
114 # ifdef ALTOS_SYSTEM_V
117 handler = sigset(sig, SIG_HOLD);
118 if (handler == SIG_ERR)
121 return handler == SIG_HOLD;
122 # else /* ALTOS_SYSTEM_V */
125 (void) sigemptyset(&sset);
126 (void) sigaddset(&sset, sig);
127 if (sigprocmask(SIG_BLOCK, &sset, &oset) < 0)
130 return sigismember(&oset, sig);
131 # endif /* ALTOS_SYSTEM_V */
135 ** SM_RELEASESIGNAL -- release a held signal
138 ** sig -- the signal to release.
141 ** 1 signal was previously blocked
142 ** 0 signal was not previously blocked
147 sm_releasesignal(sig)
151 return (sigsetmask(sigblock(0) & ~sigmask(sig)) & sigmask(sig)) != 0;
153 # ifdef ALTOS_SYSTEM_V
156 handler = sigset(sig, SIG_HOLD);
157 if (sigrelse(sig) < 0)
160 return handler == SIG_HOLD;
161 # else /* ALTOS_SYSTEM_V */
164 (void) sigemptyset(&sset);
165 (void) sigaddset(&sset, sig);
166 if (sigprocmask(SIG_UNBLOCK, &sset, &oset) < 0)
169 return sigismember(&oset, sig);
170 # endif /* ALTOS_SYSTEM_V */
174 ** PEND_SIGNAL -- Add a signal to the pending signal list
177 ** sig -- signal to add
182 ** NOTE: THIS CAN BE CALLED FROM A SIGNAL HANDLER. DO NOT ADD
183 ** ANYTHING TO THIS ROUTINE UNLESS YOU KNOW WHAT YOU ARE
192 int save_errno = errno;
193 #if SM_CONF_SETITIMER
194 struct itimerval clr;
198 ** Don't want to interrupt something critical, hence delay
199 ** the alarm for one second. Hopefully, by then we
200 ** will be out of the critical section. If not, then
201 ** we will just delay again. The events to be run will
202 ** still all be run, maybe just a little bit late.
208 sigbit = PEND_SIGHUP;
212 sigbit = PEND_SIGINT;
216 sigbit = PEND_SIGTERM;
220 sigbit = PEND_SIGUSR1;
224 /* don't have to pend these */
229 /* If we get here, we are in trouble */
233 /* shut up stupid compiler warning on HP-UX 11 */
239 PendingSignal |= sigbit;
240 (void) sm_signal(SIGALRM, sm_tick);
241 #if SM_CONF_SETITIMER
242 clr.it_interval.tv_sec = 0;
243 clr.it_interval.tv_usec = 0;
244 clr.it_value.tv_sec = 1;
245 clr.it_value.tv_usec = 0;
246 (void) setitimer(ITIMER_REAL, &clr, NULL);
247 #else /* SM_CONF_SETITIMER */
249 #endif /* SM_CONF_SETITIMER */
253 ** SM_ALLSIGNALS -- act on all signals
256 ** block -- whether to block or release all signals.
268 # define sigmask(s) (1 << ((s) - 1))
274 mask |= sigmask(SIGALRM);
275 mask |= sigmask(SIGCHLD);
276 mask |= sigmask(SIGHUP);
277 mask |= sigmask(SIGINT);
278 mask |= sigmask(SIGTERM);
279 mask |= sigmask(SIGUSR1);
281 (void) sigblock(mask);
286 # ifdef ALTOS_SYSTEM_V
289 (void) sigset(SIGALRM, SIG_HOLD);
290 (void) sigset(SIGCHLD, SIG_HOLD);
291 (void) sigset(SIGHUP, SIG_HOLD);
292 (void) sigset(SIGINT, SIG_HOLD);
293 (void) sigset(SIGTERM, SIG_HOLD);
294 (void) sigset(SIGUSR1, SIG_HOLD);
298 (void) sigset(SIGALRM, SIG_DFL);
299 (void) sigset(SIGCHLD, SIG_DFL);
300 (void) sigset(SIGHUP, SIG_DFL);
301 (void) sigset(SIGINT, SIG_DFL);
302 (void) sigset(SIGTERM, SIG_DFL);
303 (void) sigset(SIGUSR1, SIG_DFL);
305 # else /* ALTOS_SYSTEM_V */
308 (void) sigemptyset(&sset);
309 (void) sigaddset(&sset, SIGALRM);
310 (void) sigaddset(&sset, SIGCHLD);
311 (void) sigaddset(&sset, SIGHUP);
312 (void) sigaddset(&sset, SIGINT);
313 (void) sigaddset(&sset, SIGTERM);
314 (void) sigaddset(&sset, SIGUSR1);
315 (void) sigprocmask(block ? SIG_BLOCK : SIG_UNBLOCK, &sset, NULL);
316 # endif /* ALTOS_SYSTEM_V */
320 ** SM_SIGNAL_NOOP -- A signal no-op function
323 ** sig -- signal received
334 int save_errno = errno;
336 FIX_SYSV_SIGNAL(sig, sm_signal_noop);
338 return SIGFUNC_RETURN;