xref: /illumos-gate/usr/src/lib/libc/port/sys/signal.c (revision 1da57d55)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5a574db85Sraf  * Common Development and Distribution License (the "License").
6a574db85Sraf  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21a574db85Sraf 
227c478bd9Sstevel@tonic-gate /*
23a574db85Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
27*7257d1b4Sraf #pragma weak _signal = signal
28*7257d1b4Sraf #pragma weak _sighold = sighold
29*7257d1b4Sraf #pragma weak _sigrelse = sigrelse
30*7257d1b4Sraf #pragma weak _sigignore = sigignore
31*7257d1b4Sraf #pragma weak _sigset = sigset
327c478bd9Sstevel@tonic-gate 
33*7257d1b4Sraf #include "lint.h"
347c478bd9Sstevel@tonic-gate #include <sys/types.h>
357c478bd9Sstevel@tonic-gate #include <unistd.h>
367c478bd9Sstevel@tonic-gate #include <errno.h>
377c478bd9Sstevel@tonic-gate #include <signal.h>
387c478bd9Sstevel@tonic-gate #include <wait.h>
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate /*
417c478bd9Sstevel@tonic-gate  * Check for valid signal number as per SVID.
427c478bd9Sstevel@tonic-gate  */
437c478bd9Sstevel@tonic-gate #define	CHECK_SIG(s, code) \
447c478bd9Sstevel@tonic-gate 	if ((s) <= 0 || (s) >= NSIG || (s) == SIGKILL || (s) == SIGSTOP) { \
457c478bd9Sstevel@tonic-gate 		errno = EINVAL; \
467c478bd9Sstevel@tonic-gate 		return (code); \
477c478bd9Sstevel@tonic-gate 	}
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate /*
507c478bd9Sstevel@tonic-gate  * Equivalent to stopdefault set in the kernel implementation (sig.c).
517c478bd9Sstevel@tonic-gate  */
527c478bd9Sstevel@tonic-gate #define	STOPDEFAULT(s) \
537c478bd9Sstevel@tonic-gate 	((s) == SIGSTOP || (s) == SIGTSTP || (s) == SIGTTOU || (s) == SIGTTIN)
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate /*
577c478bd9Sstevel@tonic-gate  * SVr3.x signal compatibility routines. They are now
587c478bd9Sstevel@tonic-gate  * implemented as library routines instead of system
597c478bd9Sstevel@tonic-gate  * calls.
607c478bd9Sstevel@tonic-gate  */
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate void(*
signal(int sig,void (* func)(int))637c478bd9Sstevel@tonic-gate signal(int sig, void(*func)(int)))(int)
647c478bd9Sstevel@tonic-gate {
657c478bd9Sstevel@tonic-gate 	struct sigaction nact;
667c478bd9Sstevel@tonic-gate 	struct sigaction oact;
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate 	CHECK_SIG(sig, SIG_ERR);
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate 	nact.sa_handler = func;
717c478bd9Sstevel@tonic-gate 	nact.sa_flags = SA_RESETHAND|SA_NODEFER;
727c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&nact.sa_mask);
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate 	/*
757c478bd9Sstevel@tonic-gate 	 * Pay special attention if sig is SIGCHLD and
767c478bd9Sstevel@tonic-gate 	 * the disposition is SIG_IGN, per sysV signal man page.
777c478bd9Sstevel@tonic-gate 	 */
787c478bd9Sstevel@tonic-gate 	if (sig == SIGCHLD) {
797c478bd9Sstevel@tonic-gate 		nact.sa_flags |= SA_NOCLDSTOP;
807c478bd9Sstevel@tonic-gate 		if (func == SIG_IGN)
817c478bd9Sstevel@tonic-gate 			nact.sa_flags |= SA_NOCLDWAIT;
827c478bd9Sstevel@tonic-gate 	}
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate 	if (STOPDEFAULT(sig))
857c478bd9Sstevel@tonic-gate 		nact.sa_flags |= SA_RESTART;
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate 	if (sigaction(sig, &nact, &oact) < 0)
887c478bd9Sstevel@tonic-gate 		return (SIG_ERR);
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate 	return (oact.sa_handler);
917c478bd9Sstevel@tonic-gate }
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate int
sighold(int sig)947c478bd9Sstevel@tonic-gate sighold(int sig)
957c478bd9Sstevel@tonic-gate {
967c478bd9Sstevel@tonic-gate 	sigset_t set;
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate 	CHECK_SIG(sig, -1);
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate 	/*
1017c478bd9Sstevel@tonic-gate 	 * errno set on failure by either sigaddset or sigprocmask.
1027c478bd9Sstevel@tonic-gate 	 */
1037c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&set);
1047c478bd9Sstevel@tonic-gate 	if (sigaddset(&set, sig) < 0)
1057c478bd9Sstevel@tonic-gate 		return (-1);
1067c478bd9Sstevel@tonic-gate 	return (sigprocmask(SIG_BLOCK, &set, (sigset_t *)0));
1077c478bd9Sstevel@tonic-gate }
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate int
sigrelse(int sig)1107c478bd9Sstevel@tonic-gate sigrelse(int sig)
1117c478bd9Sstevel@tonic-gate {
1127c478bd9Sstevel@tonic-gate 	sigset_t set;
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate 	CHECK_SIG(sig, -1);
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate 	/*
1177c478bd9Sstevel@tonic-gate 	 * errno set on failure by either sigaddset or sigprocmask.
1187c478bd9Sstevel@tonic-gate 	 */
1197c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&set);
1207c478bd9Sstevel@tonic-gate 	if (sigaddset(&set, sig) < 0)
1217c478bd9Sstevel@tonic-gate 		return (-1);
1227c478bd9Sstevel@tonic-gate 	return (sigprocmask(SIG_UNBLOCK, &set, (sigset_t *)0));
1237c478bd9Sstevel@tonic-gate }
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate int
sigignore(int sig)1267c478bd9Sstevel@tonic-gate sigignore(int sig)
1277c478bd9Sstevel@tonic-gate {
1287c478bd9Sstevel@tonic-gate 	struct sigaction act;
1297c478bd9Sstevel@tonic-gate 	sigset_t set;
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 	CHECK_SIG(sig, -1);
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate 	act.sa_handler = SIG_IGN;
1347c478bd9Sstevel@tonic-gate 	act.sa_flags = 0;
1357c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&act.sa_mask);
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 	/*
1387c478bd9Sstevel@tonic-gate 	 * Pay special attention if sig is SIGCHLD and
1397c478bd9Sstevel@tonic-gate 	 * the disposition is SIG_IGN, per sysV signal man page.
1407c478bd9Sstevel@tonic-gate 	 */
1417c478bd9Sstevel@tonic-gate 	if (sig == SIGCHLD) {
1427c478bd9Sstevel@tonic-gate 		act.sa_flags |= SA_NOCLDSTOP;
1437c478bd9Sstevel@tonic-gate 		act.sa_flags |= SA_NOCLDWAIT;
1447c478bd9Sstevel@tonic-gate 	}
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 	if (STOPDEFAULT(sig))
1477c478bd9Sstevel@tonic-gate 		act.sa_flags |= SA_RESTART;
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 	if (sigaction(sig, &act, (struct sigaction *)0) < 0)
1507c478bd9Sstevel@tonic-gate 		return (-1);
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&set);
1537c478bd9Sstevel@tonic-gate 	if (sigaddset(&set, sig) < 0)
1547c478bd9Sstevel@tonic-gate 		return (-1);
1557c478bd9Sstevel@tonic-gate 	return (sigprocmask(SIG_UNBLOCK, &set, (sigset_t *)0));
1567c478bd9Sstevel@tonic-gate }
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate int
__sigpause(int sig)159a574db85Sraf __sigpause(int sig)
1607c478bd9Sstevel@tonic-gate {
1617c478bd9Sstevel@tonic-gate 	sigset_t set;
1627c478bd9Sstevel@tonic-gate 	int rval;
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 	CHECK_SIG(sig, -1);
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate 	/*
1677c478bd9Sstevel@tonic-gate 	 * sigpause() is defined to unblock the signal
1687c478bd9Sstevel@tonic-gate 	 * and not block it again on return.
1697c478bd9Sstevel@tonic-gate 	 * sigsuspend() restores the original signal set,
1707c478bd9Sstevel@tonic-gate 	 * so we have to unblock sig overtly.
1717c478bd9Sstevel@tonic-gate 	 */
1727c478bd9Sstevel@tonic-gate 	(void) sigprocmask(0, (sigset_t *)0, &set);
1737c478bd9Sstevel@tonic-gate 	if (sigdelset(&set, sig) < 0)
1747c478bd9Sstevel@tonic-gate 		return (-1);
1757c478bd9Sstevel@tonic-gate 	rval = sigsuspend(&set);
1767c478bd9Sstevel@tonic-gate 	(void) sigrelse(sig);
1777c478bd9Sstevel@tonic-gate 	return (rval);
1787c478bd9Sstevel@tonic-gate }
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate void(*
sigset(int sig,void (* func)(int))1817c478bd9Sstevel@tonic-gate sigset(int sig, void(*func)(int)))(int)
1827c478bd9Sstevel@tonic-gate {
1837c478bd9Sstevel@tonic-gate 	struct sigaction nact;
1847c478bd9Sstevel@tonic-gate 	struct sigaction oact;
1857c478bd9Sstevel@tonic-gate 	sigset_t nset;
1867c478bd9Sstevel@tonic-gate 	sigset_t oset;
1877c478bd9Sstevel@tonic-gate 	int code;
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 	CHECK_SIG(sig, SIG_ERR);
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&nset);
1927c478bd9Sstevel@tonic-gate 	if (sigaddset(&nset, sig) < 0)
1937c478bd9Sstevel@tonic-gate 		return (SIG_ERR);
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate 	if (func == SIG_HOLD) {
1967c478bd9Sstevel@tonic-gate 		if (sigprocmask(SIG_BLOCK, &nset, &oset) < 0)
1977c478bd9Sstevel@tonic-gate 			return (SIG_ERR);
1987c478bd9Sstevel@tonic-gate 		if (sigaction(sig, (struct sigaction *)0, &oact) < 0)
1997c478bd9Sstevel@tonic-gate 			return (SIG_ERR);
2007c478bd9Sstevel@tonic-gate 	} else {
2017c478bd9Sstevel@tonic-gate 		nact.sa_handler = func;
2027c478bd9Sstevel@tonic-gate 		nact.sa_flags = 0;
2037c478bd9Sstevel@tonic-gate 		(void) sigemptyset(&nact.sa_mask);
2047c478bd9Sstevel@tonic-gate 		/*
2057c478bd9Sstevel@tonic-gate 		 * Pay special attention if sig is SIGCHLD and
2067c478bd9Sstevel@tonic-gate 		 * the disposition is SIG_IGN, per sysV signal man page.
2077c478bd9Sstevel@tonic-gate 		 */
2087c478bd9Sstevel@tonic-gate 		if (sig == SIGCHLD) {
2097c478bd9Sstevel@tonic-gate 			nact.sa_flags |= SA_NOCLDSTOP;
2107c478bd9Sstevel@tonic-gate 			if (func == SIG_IGN)
2117c478bd9Sstevel@tonic-gate 				nact.sa_flags |= SA_NOCLDWAIT;
2127c478bd9Sstevel@tonic-gate 		}
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 		if (STOPDEFAULT(sig))
2157c478bd9Sstevel@tonic-gate 			nact.sa_flags |= SA_RESTART;
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 		if (sigaction(sig, &nact, &oact) < 0)
2187c478bd9Sstevel@tonic-gate 			return (SIG_ERR);
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 		if (sigprocmask(SIG_UNBLOCK, &nset, &oset) < 0)
2217c478bd9Sstevel@tonic-gate 			return (SIG_ERR);
2227c478bd9Sstevel@tonic-gate 	}
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 	if ((code = sigismember(&oset, sig)) < 0)
2257c478bd9Sstevel@tonic-gate 		return (SIG_ERR);
2267c478bd9Sstevel@tonic-gate 	else if (code == 1)
2277c478bd9Sstevel@tonic-gate 		return (SIG_HOLD);
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate 	return (oact.sa_handler);
2307c478bd9Sstevel@tonic-gate }
231