10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * Copyright (c) 2000-2001 Sendmail, Inc. and its suppliers.
30Sstevel@tonic-gate * All rights reserved.
40Sstevel@tonic-gate *
50Sstevel@tonic-gate * By using this file, you agree to the terms and conditions set
60Sstevel@tonic-gate * forth in the LICENSE file which can be found at the top level of
70Sstevel@tonic-gate * the sendmail distribution.
80Sstevel@tonic-gate */
90Sstevel@tonic-gate
100Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
110Sstevel@tonic-gate
120Sstevel@tonic-gate #include <sm/gen.h>
13*616Sjbeck SM_RCSID("@(#)$Id: signal.c,v 1.17 2005/06/14 23:07:20 ca Exp $")
140Sstevel@tonic-gate
150Sstevel@tonic-gate #if SM_CONF_SETITIMER
16*616Sjbeck # include <sm/time.h>
170Sstevel@tonic-gate #endif /* SM_CONF_SETITIMER */
180Sstevel@tonic-gate #include <errno.h>
190Sstevel@tonic-gate #include <stdlib.h>
200Sstevel@tonic-gate #include <time.h>
210Sstevel@tonic-gate #include <unistd.h>
220Sstevel@tonic-gate #include <sm/clock.h>
230Sstevel@tonic-gate #include <sm/signal.h>
240Sstevel@tonic-gate #include <signal.h>
250Sstevel@tonic-gate #include <sm/string.h>
260Sstevel@tonic-gate
270Sstevel@tonic-gate unsigned int volatile InCriticalSection; /* >0 if inside critical section */
280Sstevel@tonic-gate int volatile PendingSignal; /* pending signal to resend */
290Sstevel@tonic-gate
30*616Sjbeck /*
310Sstevel@tonic-gate ** SM_SIGNAL -- set a signal handler
320Sstevel@tonic-gate **
330Sstevel@tonic-gate ** This is essentially old BSD "signal(3)".
340Sstevel@tonic-gate **
350Sstevel@tonic-gate ** NOTE: THIS CAN BE CALLED FROM A SIGNAL HANDLER. DO NOT ADD
360Sstevel@tonic-gate ** ANYTHING TO THIS ROUTINE UNLESS YOU KNOW WHAT YOU ARE
370Sstevel@tonic-gate ** DOING.
380Sstevel@tonic-gate */
390Sstevel@tonic-gate
400Sstevel@tonic-gate sigfunc_t
sm_signal(sig,handler)410Sstevel@tonic-gate sm_signal(sig, handler)
420Sstevel@tonic-gate int sig;
430Sstevel@tonic-gate sigfunc_t handler;
440Sstevel@tonic-gate {
450Sstevel@tonic-gate # if defined(SA_RESTART) || (!defined(SYS5SIGNALS) && !defined(BSD4_3))
460Sstevel@tonic-gate struct sigaction n, o;
470Sstevel@tonic-gate # endif /* defined(SA_RESTART) || (!defined(SYS5SIGNALS) && !defined(BSD4_3)) */
480Sstevel@tonic-gate
490Sstevel@tonic-gate /*
500Sstevel@tonic-gate ** First, try for modern signal calls
510Sstevel@tonic-gate ** and restartable syscalls
520Sstevel@tonic-gate */
530Sstevel@tonic-gate
540Sstevel@tonic-gate # ifdef SA_RESTART
550Sstevel@tonic-gate (void) memset(&n, '\0', sizeof n);
560Sstevel@tonic-gate # if USE_SA_SIGACTION
570Sstevel@tonic-gate n.sa_sigaction = (void(*)(int, siginfo_t *, void *)) handler;
580Sstevel@tonic-gate n.sa_flags = SA_RESTART|SA_SIGINFO;
590Sstevel@tonic-gate # else /* USE_SA_SIGACTION */
600Sstevel@tonic-gate n.sa_handler = handler;
610Sstevel@tonic-gate n.sa_flags = SA_RESTART;
620Sstevel@tonic-gate # endif /* USE_SA_SIGACTION */
630Sstevel@tonic-gate if (sigaction(sig, &n, &o) < 0)
640Sstevel@tonic-gate return SIG_ERR;
650Sstevel@tonic-gate return o.sa_handler;
660Sstevel@tonic-gate # else /* SA_RESTART */
670Sstevel@tonic-gate
680Sstevel@tonic-gate /*
690Sstevel@tonic-gate ** Else check for SYS5SIGNALS or
700Sstevel@tonic-gate ** BSD4_3 signals
710Sstevel@tonic-gate */
720Sstevel@tonic-gate
730Sstevel@tonic-gate # if defined(SYS5SIGNALS) || defined(BSD4_3)
740Sstevel@tonic-gate # ifdef BSD4_3
750Sstevel@tonic-gate return signal(sig, handler);
760Sstevel@tonic-gate # else /* BSD4_3 */
770Sstevel@tonic-gate return sigset(sig, handler);
780Sstevel@tonic-gate # endif /* BSD4_3 */
790Sstevel@tonic-gate # else /* defined(SYS5SIGNALS) || defined(BSD4_3) */
800Sstevel@tonic-gate
810Sstevel@tonic-gate /*
820Sstevel@tonic-gate ** Finally, if nothing else is available,
830Sstevel@tonic-gate ** go for a default
840Sstevel@tonic-gate */
850Sstevel@tonic-gate
860Sstevel@tonic-gate (void) memset(&n, '\0', sizeof n);
870Sstevel@tonic-gate n.sa_handler = handler;
880Sstevel@tonic-gate if (sigaction(sig, &n, &o) < 0)
890Sstevel@tonic-gate return SIG_ERR;
900Sstevel@tonic-gate return o.sa_handler;
910Sstevel@tonic-gate # endif /* defined(SYS5SIGNALS) || defined(BSD4_3) */
920Sstevel@tonic-gate # endif /* SA_RESTART */
930Sstevel@tonic-gate }
94*616Sjbeck /*
950Sstevel@tonic-gate ** SM_BLOCKSIGNAL -- hold a signal to prevent delivery
960Sstevel@tonic-gate **
970Sstevel@tonic-gate ** Parameters:
980Sstevel@tonic-gate ** sig -- the signal to block.
990Sstevel@tonic-gate **
1000Sstevel@tonic-gate ** Returns:
1010Sstevel@tonic-gate ** 1 signal was previously blocked
1020Sstevel@tonic-gate ** 0 signal was not previously blocked
1030Sstevel@tonic-gate ** -1 on failure.
1040Sstevel@tonic-gate */
1050Sstevel@tonic-gate
1060Sstevel@tonic-gate int
sm_blocksignal(sig)1070Sstevel@tonic-gate sm_blocksignal(sig)
1080Sstevel@tonic-gate int sig;
1090Sstevel@tonic-gate {
1100Sstevel@tonic-gate # ifdef BSD4_3
1110Sstevel@tonic-gate # ifndef sigmask
1120Sstevel@tonic-gate # define sigmask(s) (1 << ((s) - 1))
1130Sstevel@tonic-gate # endif /* ! sigmask */
1140Sstevel@tonic-gate return (sigblock(sigmask(sig)) & sigmask(sig)) != 0;
1150Sstevel@tonic-gate # else /* BSD4_3 */
1160Sstevel@tonic-gate # ifdef ALTOS_SYSTEM_V
1170Sstevel@tonic-gate sigfunc_t handler;
1180Sstevel@tonic-gate
1190Sstevel@tonic-gate handler = sigset(sig, SIG_HOLD);
1200Sstevel@tonic-gate if (handler == SIG_ERR)
1210Sstevel@tonic-gate return -1;
1220Sstevel@tonic-gate else
1230Sstevel@tonic-gate return handler == SIG_HOLD;
1240Sstevel@tonic-gate # else /* ALTOS_SYSTEM_V */
1250Sstevel@tonic-gate sigset_t sset, oset;
1260Sstevel@tonic-gate
1270Sstevel@tonic-gate (void) sigemptyset(&sset);
1280Sstevel@tonic-gate (void) sigaddset(&sset, sig);
1290Sstevel@tonic-gate if (sigprocmask(SIG_BLOCK, &sset, &oset) < 0)
1300Sstevel@tonic-gate return -1;
1310Sstevel@tonic-gate else
1320Sstevel@tonic-gate return sigismember(&oset, sig);
1330Sstevel@tonic-gate # endif /* ALTOS_SYSTEM_V */
1340Sstevel@tonic-gate # endif /* BSD4_3 */
1350Sstevel@tonic-gate }
136*616Sjbeck /*
1370Sstevel@tonic-gate ** SM_RELEASESIGNAL -- release a held signal
1380Sstevel@tonic-gate **
1390Sstevel@tonic-gate ** Parameters:
1400Sstevel@tonic-gate ** sig -- the signal to release.
1410Sstevel@tonic-gate **
1420Sstevel@tonic-gate ** Returns:
1430Sstevel@tonic-gate ** 1 signal was previously blocked
1440Sstevel@tonic-gate ** 0 signal was not previously blocked
1450Sstevel@tonic-gate ** -1 on failure.
1460Sstevel@tonic-gate */
1470Sstevel@tonic-gate
1480Sstevel@tonic-gate int
sm_releasesignal(sig)1490Sstevel@tonic-gate sm_releasesignal(sig)
1500Sstevel@tonic-gate int sig;
1510Sstevel@tonic-gate {
1520Sstevel@tonic-gate # ifdef BSD4_3
1530Sstevel@tonic-gate return (sigsetmask(sigblock(0) & ~sigmask(sig)) & sigmask(sig)) != 0;
1540Sstevel@tonic-gate # else /* BSD4_3 */
1550Sstevel@tonic-gate # ifdef ALTOS_SYSTEM_V
1560Sstevel@tonic-gate sigfunc_t handler;
1570Sstevel@tonic-gate
1580Sstevel@tonic-gate handler = sigset(sig, SIG_HOLD);
1590Sstevel@tonic-gate if (sigrelse(sig) < 0)
1600Sstevel@tonic-gate return -1;
1610Sstevel@tonic-gate else
1620Sstevel@tonic-gate return handler == SIG_HOLD;
1630Sstevel@tonic-gate # else /* ALTOS_SYSTEM_V */
1640Sstevel@tonic-gate sigset_t sset, oset;
1650Sstevel@tonic-gate
1660Sstevel@tonic-gate (void) sigemptyset(&sset);
1670Sstevel@tonic-gate (void) sigaddset(&sset, sig);
1680Sstevel@tonic-gate if (sigprocmask(SIG_UNBLOCK, &sset, &oset) < 0)
1690Sstevel@tonic-gate return -1;
1700Sstevel@tonic-gate else
1710Sstevel@tonic-gate return sigismember(&oset, sig);
1720Sstevel@tonic-gate # endif /* ALTOS_SYSTEM_V */
1730Sstevel@tonic-gate # endif /* BSD4_3 */
1740Sstevel@tonic-gate }
175*616Sjbeck /*
1760Sstevel@tonic-gate ** PEND_SIGNAL -- Add a signal to the pending signal list
1770Sstevel@tonic-gate **
1780Sstevel@tonic-gate ** Parameters:
1790Sstevel@tonic-gate ** sig -- signal to add
1800Sstevel@tonic-gate **
1810Sstevel@tonic-gate ** Returns:
1820Sstevel@tonic-gate ** none.
1830Sstevel@tonic-gate **
1840Sstevel@tonic-gate ** NOTE: THIS CAN BE CALLED FROM A SIGNAL HANDLER. DO NOT ADD
1850Sstevel@tonic-gate ** ANYTHING TO THIS ROUTINE UNLESS YOU KNOW WHAT YOU ARE
1860Sstevel@tonic-gate ** DOING.
1870Sstevel@tonic-gate */
1880Sstevel@tonic-gate
1890Sstevel@tonic-gate void
pend_signal(sig)1900Sstevel@tonic-gate pend_signal(sig)
1910Sstevel@tonic-gate int sig;
1920Sstevel@tonic-gate {
1930Sstevel@tonic-gate int sigbit;
1940Sstevel@tonic-gate int save_errno = errno;
1950Sstevel@tonic-gate #if SM_CONF_SETITIMER
1960Sstevel@tonic-gate struct itimerval clr;
1970Sstevel@tonic-gate #endif /* SM_CONF_SETITIMER */
1980Sstevel@tonic-gate
1990Sstevel@tonic-gate /*
2000Sstevel@tonic-gate ** Don't want to interrupt something critical, hence delay
2010Sstevel@tonic-gate ** the alarm for one second. Hopefully, by then we
2020Sstevel@tonic-gate ** will be out of the critical section. If not, then
2030Sstevel@tonic-gate ** we will just delay again. The events to be run will
2040Sstevel@tonic-gate ** still all be run, maybe just a little bit late.
2050Sstevel@tonic-gate */
2060Sstevel@tonic-gate
2070Sstevel@tonic-gate switch (sig)
2080Sstevel@tonic-gate {
2090Sstevel@tonic-gate case SIGHUP:
2100Sstevel@tonic-gate sigbit = PEND_SIGHUP;
2110Sstevel@tonic-gate break;
2120Sstevel@tonic-gate
2130Sstevel@tonic-gate case SIGINT:
2140Sstevel@tonic-gate sigbit = PEND_SIGINT;
2150Sstevel@tonic-gate break;
2160Sstevel@tonic-gate
2170Sstevel@tonic-gate case SIGTERM:
2180Sstevel@tonic-gate sigbit = PEND_SIGTERM;
2190Sstevel@tonic-gate break;
2200Sstevel@tonic-gate
2210Sstevel@tonic-gate case SIGUSR1:
2220Sstevel@tonic-gate sigbit = PEND_SIGUSR1;
2230Sstevel@tonic-gate break;
2240Sstevel@tonic-gate
2250Sstevel@tonic-gate case SIGALRM:
2260Sstevel@tonic-gate /* don't have to pend these */
2270Sstevel@tonic-gate sigbit = 0;
2280Sstevel@tonic-gate break;
2290Sstevel@tonic-gate
2300Sstevel@tonic-gate default:
2310Sstevel@tonic-gate /* If we get here, we are in trouble */
2320Sstevel@tonic-gate abort();
2330Sstevel@tonic-gate
2340Sstevel@tonic-gate /* NOTREACHED */
2350Sstevel@tonic-gate /* shut up stupid compiler warning on HP-UX 11 */
2360Sstevel@tonic-gate sigbit = 0;
2370Sstevel@tonic-gate break;
2380Sstevel@tonic-gate }
2390Sstevel@tonic-gate
2400Sstevel@tonic-gate if (sigbit != 0)
2410Sstevel@tonic-gate PendingSignal |= sigbit;
2420Sstevel@tonic-gate (void) sm_signal(SIGALRM, sm_tick);
2430Sstevel@tonic-gate #if SM_CONF_SETITIMER
2440Sstevel@tonic-gate clr.it_interval.tv_sec = 0;
2450Sstevel@tonic-gate clr.it_interval.tv_usec = 0;
2460Sstevel@tonic-gate clr.it_value.tv_sec = 1;
2470Sstevel@tonic-gate clr.it_value.tv_usec = 0;
2480Sstevel@tonic-gate (void) setitimer(ITIMER_REAL, &clr, NULL);
2490Sstevel@tonic-gate #else /* SM_CONF_SETITIMER */
2500Sstevel@tonic-gate (void) alarm(1);
2510Sstevel@tonic-gate #endif /* SM_CONF_SETITIMER */
2520Sstevel@tonic-gate errno = save_errno;
2530Sstevel@tonic-gate }
254*616Sjbeck /*
2550Sstevel@tonic-gate ** SM_ALLSIGNALS -- act on all signals
2560Sstevel@tonic-gate **
2570Sstevel@tonic-gate ** Parameters:
2580Sstevel@tonic-gate ** block -- whether to block or release all signals.
2590Sstevel@tonic-gate **
2600Sstevel@tonic-gate ** Returns:
2610Sstevel@tonic-gate ** none.
2620Sstevel@tonic-gate */
2630Sstevel@tonic-gate
2640Sstevel@tonic-gate void
sm_allsignals(block)2650Sstevel@tonic-gate sm_allsignals(block)
2660Sstevel@tonic-gate bool block;
2670Sstevel@tonic-gate {
2680Sstevel@tonic-gate # ifdef BSD4_3
2690Sstevel@tonic-gate # ifndef sigmask
2700Sstevel@tonic-gate # define sigmask(s) (1 << ((s) - 1))
2710Sstevel@tonic-gate # endif /* ! sigmask */
2720Sstevel@tonic-gate if (block)
2730Sstevel@tonic-gate {
2740Sstevel@tonic-gate int mask = 0;
2750Sstevel@tonic-gate
2760Sstevel@tonic-gate mask |= sigmask(SIGALRM);
2770Sstevel@tonic-gate mask |= sigmask(SIGCHLD);
2780Sstevel@tonic-gate mask |= sigmask(SIGHUP);
2790Sstevel@tonic-gate mask |= sigmask(SIGINT);
2800Sstevel@tonic-gate mask |= sigmask(SIGTERM);
2810Sstevel@tonic-gate mask |= sigmask(SIGUSR1);
2820Sstevel@tonic-gate
2830Sstevel@tonic-gate (void) sigblock(mask);
2840Sstevel@tonic-gate }
2850Sstevel@tonic-gate else
2860Sstevel@tonic-gate sigsetmask(0);
2870Sstevel@tonic-gate # else /* BSD4_3 */
2880Sstevel@tonic-gate # ifdef ALTOS_SYSTEM_V
2890Sstevel@tonic-gate if (block)
2900Sstevel@tonic-gate {
2910Sstevel@tonic-gate (void) sigset(SIGALRM, SIG_HOLD);
2920Sstevel@tonic-gate (void) sigset(SIGCHLD, SIG_HOLD);
2930Sstevel@tonic-gate (void) sigset(SIGHUP, SIG_HOLD);
2940Sstevel@tonic-gate (void) sigset(SIGINT, SIG_HOLD);
2950Sstevel@tonic-gate (void) sigset(SIGTERM, SIG_HOLD);
2960Sstevel@tonic-gate (void) sigset(SIGUSR1, SIG_HOLD);
2970Sstevel@tonic-gate }
2980Sstevel@tonic-gate else
2990Sstevel@tonic-gate {
3000Sstevel@tonic-gate (void) sigset(SIGALRM, SIG_DFL);
3010Sstevel@tonic-gate (void) sigset(SIGCHLD, SIG_DFL);
3020Sstevel@tonic-gate (void) sigset(SIGHUP, SIG_DFL);
3030Sstevel@tonic-gate (void) sigset(SIGINT, SIG_DFL);
3040Sstevel@tonic-gate (void) sigset(SIGTERM, SIG_DFL);
3050Sstevel@tonic-gate (void) sigset(SIGUSR1, SIG_DFL);
3060Sstevel@tonic-gate }
3070Sstevel@tonic-gate # else /* ALTOS_SYSTEM_V */
3080Sstevel@tonic-gate sigset_t sset;
3090Sstevel@tonic-gate
3100Sstevel@tonic-gate (void) sigemptyset(&sset);
3110Sstevel@tonic-gate (void) sigaddset(&sset, SIGALRM);
3120Sstevel@tonic-gate (void) sigaddset(&sset, SIGCHLD);
3130Sstevel@tonic-gate (void) sigaddset(&sset, SIGHUP);
3140Sstevel@tonic-gate (void) sigaddset(&sset, SIGINT);
3150Sstevel@tonic-gate (void) sigaddset(&sset, SIGTERM);
3160Sstevel@tonic-gate (void) sigaddset(&sset, SIGUSR1);
3170Sstevel@tonic-gate (void) sigprocmask(block ? SIG_BLOCK : SIG_UNBLOCK, &sset, NULL);
3180Sstevel@tonic-gate # endif /* ALTOS_SYSTEM_V */
3190Sstevel@tonic-gate # endif /* BSD4_3 */
3200Sstevel@tonic-gate }
321*616Sjbeck /*
3220Sstevel@tonic-gate ** SM_SIGNAL_NOOP -- A signal no-op function
3230Sstevel@tonic-gate **
3240Sstevel@tonic-gate ** Parameters:
3250Sstevel@tonic-gate ** sig -- signal received
3260Sstevel@tonic-gate **
3270Sstevel@tonic-gate ** Returns:
3280Sstevel@tonic-gate ** SIGFUNC_RETURN
3290Sstevel@tonic-gate */
3300Sstevel@tonic-gate
3310Sstevel@tonic-gate /* ARGSUSED */
3320Sstevel@tonic-gate SIGFUNC_DECL
sm_signal_noop(sig)3330Sstevel@tonic-gate sm_signal_noop(sig)
3340Sstevel@tonic-gate int sig;
3350Sstevel@tonic-gate {
3360Sstevel@tonic-gate int save_errno = errno;
3370Sstevel@tonic-gate
3380Sstevel@tonic-gate FIX_SYSV_SIGNAL(sig, sm_signal_noop);
3390Sstevel@tonic-gate errno = save_errno;
3400Sstevel@tonic-gate return SIGFUNC_RETURN;
3410Sstevel@tonic-gate }
3420Sstevel@tonic-gate
343