xref: /titanic_52/usr/src/lib/libc/port/sys/signal.c (revision 7257d1b4d25bfac0c802847390e98a464fd787ac)
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 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
29*7257d1b4Sraf #pragma weak _signal = signal
30*7257d1b4Sraf #pragma weak _sighold = sighold
31*7257d1b4Sraf #pragma weak _sigrelse = sigrelse
32*7257d1b4Sraf #pragma weak _sigignore = sigignore
33*7257d1b4Sraf #pragma weak _sigset = sigset
347c478bd9Sstevel@tonic-gate 
35*7257d1b4Sraf #include "lint.h"
367c478bd9Sstevel@tonic-gate #include <sys/types.h>
377c478bd9Sstevel@tonic-gate #include <unistd.h>
387c478bd9Sstevel@tonic-gate #include <errno.h>
397c478bd9Sstevel@tonic-gate #include <signal.h>
407c478bd9Sstevel@tonic-gate #include <wait.h>
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate /*
437c478bd9Sstevel@tonic-gate  * Check for valid signal number as per SVID.
447c478bd9Sstevel@tonic-gate  */
457c478bd9Sstevel@tonic-gate #define	CHECK_SIG(s, code) \
467c478bd9Sstevel@tonic-gate 	if ((s) <= 0 || (s) >= NSIG || (s) == SIGKILL || (s) == SIGSTOP) { \
477c478bd9Sstevel@tonic-gate 		errno = EINVAL; \
487c478bd9Sstevel@tonic-gate 		return (code); \
497c478bd9Sstevel@tonic-gate 	}
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate /*
527c478bd9Sstevel@tonic-gate  * Equivalent to stopdefault set in the kernel implementation (sig.c).
537c478bd9Sstevel@tonic-gate  */
547c478bd9Sstevel@tonic-gate #define	STOPDEFAULT(s) \
557c478bd9Sstevel@tonic-gate 	((s) == SIGSTOP || (s) == SIGTSTP || (s) == SIGTTOU || (s) == SIGTTIN)
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate /*
597c478bd9Sstevel@tonic-gate  * SVr3.x signal compatibility routines. They are now
607c478bd9Sstevel@tonic-gate  * implemented as library routines instead of system
617c478bd9Sstevel@tonic-gate  * calls.
627c478bd9Sstevel@tonic-gate  */
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate void(*
657c478bd9Sstevel@tonic-gate signal(int sig, void(*func)(int)))(int)
667c478bd9Sstevel@tonic-gate {
677c478bd9Sstevel@tonic-gate 	struct sigaction nact;
687c478bd9Sstevel@tonic-gate 	struct sigaction oact;
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate 	CHECK_SIG(sig, SIG_ERR);
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate 	nact.sa_handler = func;
737c478bd9Sstevel@tonic-gate 	nact.sa_flags = SA_RESETHAND|SA_NODEFER;
747c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&nact.sa_mask);
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate 	/*
777c478bd9Sstevel@tonic-gate 	 * Pay special attention if sig is SIGCHLD and
787c478bd9Sstevel@tonic-gate 	 * the disposition is SIG_IGN, per sysV signal man page.
797c478bd9Sstevel@tonic-gate 	 */
807c478bd9Sstevel@tonic-gate 	if (sig == SIGCHLD) {
817c478bd9Sstevel@tonic-gate 		nact.sa_flags |= SA_NOCLDSTOP;
827c478bd9Sstevel@tonic-gate 		if (func == SIG_IGN)
837c478bd9Sstevel@tonic-gate 			nact.sa_flags |= SA_NOCLDWAIT;
847c478bd9Sstevel@tonic-gate 	}
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate 	if (STOPDEFAULT(sig))
877c478bd9Sstevel@tonic-gate 		nact.sa_flags |= SA_RESTART;
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate 	if (sigaction(sig, &nact, &oact) < 0)
907c478bd9Sstevel@tonic-gate 		return (SIG_ERR);
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate 	return (oact.sa_handler);
937c478bd9Sstevel@tonic-gate }
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate int
967c478bd9Sstevel@tonic-gate sighold(int sig)
977c478bd9Sstevel@tonic-gate {
987c478bd9Sstevel@tonic-gate 	sigset_t set;
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate 	CHECK_SIG(sig, -1);
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate 	/*
1037c478bd9Sstevel@tonic-gate 	 * errno set on failure by either sigaddset or sigprocmask.
1047c478bd9Sstevel@tonic-gate 	 */
1057c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&set);
1067c478bd9Sstevel@tonic-gate 	if (sigaddset(&set, sig) < 0)
1077c478bd9Sstevel@tonic-gate 		return (-1);
1087c478bd9Sstevel@tonic-gate 	return (sigprocmask(SIG_BLOCK, &set, (sigset_t *)0));
1097c478bd9Sstevel@tonic-gate }
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate int
1127c478bd9Sstevel@tonic-gate sigrelse(int sig)
1137c478bd9Sstevel@tonic-gate {
1147c478bd9Sstevel@tonic-gate 	sigset_t set;
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate 	CHECK_SIG(sig, -1);
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate 	/*
1197c478bd9Sstevel@tonic-gate 	 * errno set on failure by either sigaddset or sigprocmask.
1207c478bd9Sstevel@tonic-gate 	 */
1217c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&set);
1227c478bd9Sstevel@tonic-gate 	if (sigaddset(&set, sig) < 0)
1237c478bd9Sstevel@tonic-gate 		return (-1);
1247c478bd9Sstevel@tonic-gate 	return (sigprocmask(SIG_UNBLOCK, &set, (sigset_t *)0));
1257c478bd9Sstevel@tonic-gate }
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate int
1287c478bd9Sstevel@tonic-gate sigignore(int sig)
1297c478bd9Sstevel@tonic-gate {
1307c478bd9Sstevel@tonic-gate 	struct sigaction act;
1317c478bd9Sstevel@tonic-gate 	sigset_t set;
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate 	CHECK_SIG(sig, -1);
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate 	act.sa_handler = SIG_IGN;
1367c478bd9Sstevel@tonic-gate 	act.sa_flags = 0;
1377c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&act.sa_mask);
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 	/*
1407c478bd9Sstevel@tonic-gate 	 * Pay special attention if sig is SIGCHLD and
1417c478bd9Sstevel@tonic-gate 	 * the disposition is SIG_IGN, per sysV signal man page.
1427c478bd9Sstevel@tonic-gate 	 */
1437c478bd9Sstevel@tonic-gate 	if (sig == SIGCHLD) {
1447c478bd9Sstevel@tonic-gate 		act.sa_flags |= SA_NOCLDSTOP;
1457c478bd9Sstevel@tonic-gate 		act.sa_flags |= SA_NOCLDWAIT;
1467c478bd9Sstevel@tonic-gate 	}
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate 	if (STOPDEFAULT(sig))
1497c478bd9Sstevel@tonic-gate 		act.sa_flags |= SA_RESTART;
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate 	if (sigaction(sig, &act, (struct sigaction *)0) < 0)
1527c478bd9Sstevel@tonic-gate 		return (-1);
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&set);
1557c478bd9Sstevel@tonic-gate 	if (sigaddset(&set, sig) < 0)
1567c478bd9Sstevel@tonic-gate 		return (-1);
1577c478bd9Sstevel@tonic-gate 	return (sigprocmask(SIG_UNBLOCK, &set, (sigset_t *)0));
1587c478bd9Sstevel@tonic-gate }
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate int
161a574db85Sraf __sigpause(int sig)
1627c478bd9Sstevel@tonic-gate {
1637c478bd9Sstevel@tonic-gate 	sigset_t set;
1647c478bd9Sstevel@tonic-gate 	int rval;
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate 	CHECK_SIG(sig, -1);
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 	/*
1697c478bd9Sstevel@tonic-gate 	 * sigpause() is defined to unblock the signal
1707c478bd9Sstevel@tonic-gate 	 * and not block it again on return.
1717c478bd9Sstevel@tonic-gate 	 * sigsuspend() restores the original signal set,
1727c478bd9Sstevel@tonic-gate 	 * so we have to unblock sig overtly.
1737c478bd9Sstevel@tonic-gate 	 */
1747c478bd9Sstevel@tonic-gate 	(void) sigprocmask(0, (sigset_t *)0, &set);
1757c478bd9Sstevel@tonic-gate 	if (sigdelset(&set, sig) < 0)
1767c478bd9Sstevel@tonic-gate 		return (-1);
1777c478bd9Sstevel@tonic-gate 	rval = sigsuspend(&set);
1787c478bd9Sstevel@tonic-gate 	(void) sigrelse(sig);
1797c478bd9Sstevel@tonic-gate 	return (rval);
1807c478bd9Sstevel@tonic-gate }
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate void(*
1837c478bd9Sstevel@tonic-gate sigset(int sig, void(*func)(int)))(int)
1847c478bd9Sstevel@tonic-gate {
1857c478bd9Sstevel@tonic-gate 	struct sigaction nact;
1867c478bd9Sstevel@tonic-gate 	struct sigaction oact;
1877c478bd9Sstevel@tonic-gate 	sigset_t nset;
1887c478bd9Sstevel@tonic-gate 	sigset_t oset;
1897c478bd9Sstevel@tonic-gate 	int code;
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 	CHECK_SIG(sig, SIG_ERR);
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&nset);
1947c478bd9Sstevel@tonic-gate 	if (sigaddset(&nset, sig) < 0)
1957c478bd9Sstevel@tonic-gate 		return (SIG_ERR);
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 	if (func == SIG_HOLD) {
1987c478bd9Sstevel@tonic-gate 		if (sigprocmask(SIG_BLOCK, &nset, &oset) < 0)
1997c478bd9Sstevel@tonic-gate 			return (SIG_ERR);
2007c478bd9Sstevel@tonic-gate 		if (sigaction(sig, (struct sigaction *)0, &oact) < 0)
2017c478bd9Sstevel@tonic-gate 			return (SIG_ERR);
2027c478bd9Sstevel@tonic-gate 	} else {
2037c478bd9Sstevel@tonic-gate 		nact.sa_handler = func;
2047c478bd9Sstevel@tonic-gate 		nact.sa_flags = 0;
2057c478bd9Sstevel@tonic-gate 		(void) sigemptyset(&nact.sa_mask);
2067c478bd9Sstevel@tonic-gate 		/*
2077c478bd9Sstevel@tonic-gate 		 * Pay special attention if sig is SIGCHLD and
2087c478bd9Sstevel@tonic-gate 		 * the disposition is SIG_IGN, per sysV signal man page.
2097c478bd9Sstevel@tonic-gate 		 */
2107c478bd9Sstevel@tonic-gate 		if (sig == SIGCHLD) {
2117c478bd9Sstevel@tonic-gate 			nact.sa_flags |= SA_NOCLDSTOP;
2127c478bd9Sstevel@tonic-gate 			if (func == SIG_IGN)
2137c478bd9Sstevel@tonic-gate 				nact.sa_flags |= SA_NOCLDWAIT;
2147c478bd9Sstevel@tonic-gate 		}
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate 		if (STOPDEFAULT(sig))
2177c478bd9Sstevel@tonic-gate 			nact.sa_flags |= SA_RESTART;
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate 		if (sigaction(sig, &nact, &oact) < 0)
2207c478bd9Sstevel@tonic-gate 			return (SIG_ERR);
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 		if (sigprocmask(SIG_UNBLOCK, &nset, &oset) < 0)
2237c478bd9Sstevel@tonic-gate 			return (SIG_ERR);
2247c478bd9Sstevel@tonic-gate 	}
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 	if ((code = sigismember(&oset, sig)) < 0)
2277c478bd9Sstevel@tonic-gate 		return (SIG_ERR);
2287c478bd9Sstevel@tonic-gate 	else if (code == 1)
2297c478bd9Sstevel@tonic-gate 		return (SIG_HOLD);
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 	return (oact.sa_handler);
2327c478bd9Sstevel@tonic-gate }
233