10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
55891Sraf * Common Development and Distribution License (the "License").
65891Sraf * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
215891Sraf
220Sstevel@tonic-gate /*
235891Sraf * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
240Sstevel@tonic-gate * Use is subject to license terms.
250Sstevel@tonic-gate */
260Sstevel@tonic-gate
270Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
280Sstevel@tonic-gate
29*6812Sraf #pragma weak _signal = signal
30*6812Sraf #pragma weak _sighold = sighold
31*6812Sraf #pragma weak _sigrelse = sigrelse
32*6812Sraf #pragma weak _sigignore = sigignore
33*6812Sraf #pragma weak _sigset = sigset
340Sstevel@tonic-gate
35*6812Sraf #include "lint.h"
360Sstevel@tonic-gate #include <sys/types.h>
370Sstevel@tonic-gate #include <unistd.h>
380Sstevel@tonic-gate #include <errno.h>
390Sstevel@tonic-gate #include <signal.h>
400Sstevel@tonic-gate #include <wait.h>
410Sstevel@tonic-gate
420Sstevel@tonic-gate /*
430Sstevel@tonic-gate * Check for valid signal number as per SVID.
440Sstevel@tonic-gate */
450Sstevel@tonic-gate #define CHECK_SIG(s, code) \
460Sstevel@tonic-gate if ((s) <= 0 || (s) >= NSIG || (s) == SIGKILL || (s) == SIGSTOP) { \
470Sstevel@tonic-gate errno = EINVAL; \
480Sstevel@tonic-gate return (code); \
490Sstevel@tonic-gate }
500Sstevel@tonic-gate
510Sstevel@tonic-gate /*
520Sstevel@tonic-gate * Equivalent to stopdefault set in the kernel implementation (sig.c).
530Sstevel@tonic-gate */
540Sstevel@tonic-gate #define STOPDEFAULT(s) \
550Sstevel@tonic-gate ((s) == SIGSTOP || (s) == SIGTSTP || (s) == SIGTTOU || (s) == SIGTTIN)
560Sstevel@tonic-gate
570Sstevel@tonic-gate
580Sstevel@tonic-gate /*
590Sstevel@tonic-gate * SVr3.x signal compatibility routines. They are now
600Sstevel@tonic-gate * implemented as library routines instead of system
610Sstevel@tonic-gate * calls.
620Sstevel@tonic-gate */
630Sstevel@tonic-gate
640Sstevel@tonic-gate void(*
signal(int sig,void (* func)(int))650Sstevel@tonic-gate signal(int sig, void(*func)(int)))(int)
660Sstevel@tonic-gate {
670Sstevel@tonic-gate struct sigaction nact;
680Sstevel@tonic-gate struct sigaction oact;
690Sstevel@tonic-gate
700Sstevel@tonic-gate CHECK_SIG(sig, SIG_ERR);
710Sstevel@tonic-gate
720Sstevel@tonic-gate nact.sa_handler = func;
730Sstevel@tonic-gate nact.sa_flags = SA_RESETHAND|SA_NODEFER;
740Sstevel@tonic-gate (void) sigemptyset(&nact.sa_mask);
750Sstevel@tonic-gate
760Sstevel@tonic-gate /*
770Sstevel@tonic-gate * Pay special attention if sig is SIGCHLD and
780Sstevel@tonic-gate * the disposition is SIG_IGN, per sysV signal man page.
790Sstevel@tonic-gate */
800Sstevel@tonic-gate if (sig == SIGCHLD) {
810Sstevel@tonic-gate nact.sa_flags |= SA_NOCLDSTOP;
820Sstevel@tonic-gate if (func == SIG_IGN)
830Sstevel@tonic-gate nact.sa_flags |= SA_NOCLDWAIT;
840Sstevel@tonic-gate }
850Sstevel@tonic-gate
860Sstevel@tonic-gate if (STOPDEFAULT(sig))
870Sstevel@tonic-gate nact.sa_flags |= SA_RESTART;
880Sstevel@tonic-gate
890Sstevel@tonic-gate if (sigaction(sig, &nact, &oact) < 0)
900Sstevel@tonic-gate return (SIG_ERR);
910Sstevel@tonic-gate
920Sstevel@tonic-gate return (oact.sa_handler);
930Sstevel@tonic-gate }
940Sstevel@tonic-gate
950Sstevel@tonic-gate int
sighold(int sig)960Sstevel@tonic-gate sighold(int sig)
970Sstevel@tonic-gate {
980Sstevel@tonic-gate sigset_t set;
990Sstevel@tonic-gate
1000Sstevel@tonic-gate CHECK_SIG(sig, -1);
1010Sstevel@tonic-gate
1020Sstevel@tonic-gate /*
1030Sstevel@tonic-gate * errno set on failure by either sigaddset or sigprocmask.
1040Sstevel@tonic-gate */
1050Sstevel@tonic-gate (void) sigemptyset(&set);
1060Sstevel@tonic-gate if (sigaddset(&set, sig) < 0)
1070Sstevel@tonic-gate return (-1);
1080Sstevel@tonic-gate return (sigprocmask(SIG_BLOCK, &set, (sigset_t *)0));
1090Sstevel@tonic-gate }
1100Sstevel@tonic-gate
1110Sstevel@tonic-gate int
sigrelse(int sig)1120Sstevel@tonic-gate sigrelse(int sig)
1130Sstevel@tonic-gate {
1140Sstevel@tonic-gate sigset_t set;
1150Sstevel@tonic-gate
1160Sstevel@tonic-gate CHECK_SIG(sig, -1);
1170Sstevel@tonic-gate
1180Sstevel@tonic-gate /*
1190Sstevel@tonic-gate * errno set on failure by either sigaddset or sigprocmask.
1200Sstevel@tonic-gate */
1210Sstevel@tonic-gate (void) sigemptyset(&set);
1220Sstevel@tonic-gate if (sigaddset(&set, sig) < 0)
1230Sstevel@tonic-gate return (-1);
1240Sstevel@tonic-gate return (sigprocmask(SIG_UNBLOCK, &set, (sigset_t *)0));
1250Sstevel@tonic-gate }
1260Sstevel@tonic-gate
1270Sstevel@tonic-gate int
sigignore(int sig)1280Sstevel@tonic-gate sigignore(int sig)
1290Sstevel@tonic-gate {
1300Sstevel@tonic-gate struct sigaction act;
1310Sstevel@tonic-gate sigset_t set;
1320Sstevel@tonic-gate
1330Sstevel@tonic-gate CHECK_SIG(sig, -1);
1340Sstevel@tonic-gate
1350Sstevel@tonic-gate act.sa_handler = SIG_IGN;
1360Sstevel@tonic-gate act.sa_flags = 0;
1370Sstevel@tonic-gate (void) sigemptyset(&act.sa_mask);
1380Sstevel@tonic-gate
1390Sstevel@tonic-gate /*
1400Sstevel@tonic-gate * Pay special attention if sig is SIGCHLD and
1410Sstevel@tonic-gate * the disposition is SIG_IGN, per sysV signal man page.
1420Sstevel@tonic-gate */
1430Sstevel@tonic-gate if (sig == SIGCHLD) {
1440Sstevel@tonic-gate act.sa_flags |= SA_NOCLDSTOP;
1450Sstevel@tonic-gate act.sa_flags |= SA_NOCLDWAIT;
1460Sstevel@tonic-gate }
1470Sstevel@tonic-gate
1480Sstevel@tonic-gate if (STOPDEFAULT(sig))
1490Sstevel@tonic-gate act.sa_flags |= SA_RESTART;
1500Sstevel@tonic-gate
1510Sstevel@tonic-gate if (sigaction(sig, &act, (struct sigaction *)0) < 0)
1520Sstevel@tonic-gate return (-1);
1530Sstevel@tonic-gate
1540Sstevel@tonic-gate (void) sigemptyset(&set);
1550Sstevel@tonic-gate if (sigaddset(&set, sig) < 0)
1560Sstevel@tonic-gate return (-1);
1570Sstevel@tonic-gate return (sigprocmask(SIG_UNBLOCK, &set, (sigset_t *)0));
1580Sstevel@tonic-gate }
1590Sstevel@tonic-gate
1600Sstevel@tonic-gate int
__sigpause(int sig)1615891Sraf __sigpause(int sig)
1620Sstevel@tonic-gate {
1630Sstevel@tonic-gate sigset_t set;
1640Sstevel@tonic-gate int rval;
1650Sstevel@tonic-gate
1660Sstevel@tonic-gate CHECK_SIG(sig, -1);
1670Sstevel@tonic-gate
1680Sstevel@tonic-gate /*
1690Sstevel@tonic-gate * sigpause() is defined to unblock the signal
1700Sstevel@tonic-gate * and not block it again on return.
1710Sstevel@tonic-gate * sigsuspend() restores the original signal set,
1720Sstevel@tonic-gate * so we have to unblock sig overtly.
1730Sstevel@tonic-gate */
1740Sstevel@tonic-gate (void) sigprocmask(0, (sigset_t *)0, &set);
1750Sstevel@tonic-gate if (sigdelset(&set, sig) < 0)
1760Sstevel@tonic-gate return (-1);
1770Sstevel@tonic-gate rval = sigsuspend(&set);
1780Sstevel@tonic-gate (void) sigrelse(sig);
1790Sstevel@tonic-gate return (rval);
1800Sstevel@tonic-gate }
1810Sstevel@tonic-gate
1820Sstevel@tonic-gate void(*
sigset(int sig,void (* func)(int))1830Sstevel@tonic-gate sigset(int sig, void(*func)(int)))(int)
1840Sstevel@tonic-gate {
1850Sstevel@tonic-gate struct sigaction nact;
1860Sstevel@tonic-gate struct sigaction oact;
1870Sstevel@tonic-gate sigset_t nset;
1880Sstevel@tonic-gate sigset_t oset;
1890Sstevel@tonic-gate int code;
1900Sstevel@tonic-gate
1910Sstevel@tonic-gate CHECK_SIG(sig, SIG_ERR);
1920Sstevel@tonic-gate
1930Sstevel@tonic-gate (void) sigemptyset(&nset);
1940Sstevel@tonic-gate if (sigaddset(&nset, sig) < 0)
1950Sstevel@tonic-gate return (SIG_ERR);
1960Sstevel@tonic-gate
1970Sstevel@tonic-gate if (func == SIG_HOLD) {
1980Sstevel@tonic-gate if (sigprocmask(SIG_BLOCK, &nset, &oset) < 0)
1990Sstevel@tonic-gate return (SIG_ERR);
2000Sstevel@tonic-gate if (sigaction(sig, (struct sigaction *)0, &oact) < 0)
2010Sstevel@tonic-gate return (SIG_ERR);
2020Sstevel@tonic-gate } else {
2030Sstevel@tonic-gate nact.sa_handler = func;
2040Sstevel@tonic-gate nact.sa_flags = 0;
2050Sstevel@tonic-gate (void) sigemptyset(&nact.sa_mask);
2060Sstevel@tonic-gate /*
2070Sstevel@tonic-gate * Pay special attention if sig is SIGCHLD and
2080Sstevel@tonic-gate * the disposition is SIG_IGN, per sysV signal man page.
2090Sstevel@tonic-gate */
2100Sstevel@tonic-gate if (sig == SIGCHLD) {
2110Sstevel@tonic-gate nact.sa_flags |= SA_NOCLDSTOP;
2120Sstevel@tonic-gate if (func == SIG_IGN)
2130Sstevel@tonic-gate nact.sa_flags |= SA_NOCLDWAIT;
2140Sstevel@tonic-gate }
2150Sstevel@tonic-gate
2160Sstevel@tonic-gate if (STOPDEFAULT(sig))
2170Sstevel@tonic-gate nact.sa_flags |= SA_RESTART;
2180Sstevel@tonic-gate
2190Sstevel@tonic-gate if (sigaction(sig, &nact, &oact) < 0)
2200Sstevel@tonic-gate return (SIG_ERR);
2210Sstevel@tonic-gate
2220Sstevel@tonic-gate if (sigprocmask(SIG_UNBLOCK, &nset, &oset) < 0)
2230Sstevel@tonic-gate return (SIG_ERR);
2240Sstevel@tonic-gate }
2250Sstevel@tonic-gate
2260Sstevel@tonic-gate if ((code = sigismember(&oset, sig)) < 0)
2270Sstevel@tonic-gate return (SIG_ERR);
2280Sstevel@tonic-gate else if (code == 1)
2290Sstevel@tonic-gate return (SIG_HOLD);
2300Sstevel@tonic-gate
2310Sstevel@tonic-gate return (oact.sa_handler);
2320Sstevel@tonic-gate }
233