1 /*	$OpenBSD: uthread_sigaction.c,v 1.8 2003/09/22 21:39:35 miod Exp $	*/
2 /*
3  * Copyright (c) 1995-1998 John Birrell <jb@cimlogic.com.au>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *	This product includes software developed by John Birrell.
17  * 4. Neither the name of the author nor the names of any co-contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * $FreeBSD: uthread_sigaction.c,v 1.8 1999/08/28 00:03:48 peter Exp $
34  */
35 #include <signal.h>
36 #include <errno.h>
37 #ifdef _THREAD_SAFE
38 #include <pthread.h>
39 #include "pthread_private.h"
40 
41 int
sigaction(int sig,const struct sigaction * act,struct sigaction * oact)42 sigaction(int sig, const struct sigaction * act, struct sigaction * oact)
43 {
44 	int ret = 0;
45 	struct sigaction gact;
46 
47 	/* Check if the signal number is out of range: */
48 	if (sig < 1 || sig >= NSIG) {
49 		/* Return an invalid argument: */
50 		errno = EINVAL;
51 		ret = -1;
52 	} else {
53 		/*
54 		 * Check if the existing signal action structure contents are
55 		 * to be returned:
56 		 */
57 		if (oact != NULL) {
58 			/* Return the existing signal action contents: */
59 			oact->sa_handler = _thread_sigact[sig - 1].sa_handler;
60 			oact->sa_mask = _thread_sigact[sig - 1].sa_mask;
61 			oact->sa_flags = _thread_sigact[sig - 1].sa_flags;
62 		}
63 
64 		/* Check if a signal action was supplied: */
65 		if (act != NULL) {
66 			/* Set the new signal handler: */
67 			_thread_sigact[sig - 1].sa_mask = act->sa_mask;
68 			_thread_sigact[sig - 1].sa_flags = act->sa_flags;
69 			_thread_sigact[sig - 1].sa_handler = act->sa_handler;
70 		}
71 
72 		/*
73 		 * Check if the kernel needs to be advised of a change
74 		 * in signal action:
75 		 */
76 		if (act != NULL && sig != _SCHED_SIGNAL && sig != SIGCHLD &&
77 		    sig != SIGINFO) {
78 			gact.sa_mask = act->sa_mask;
79 			sigaddset(&gact.sa_mask, _SCHED_SIGNAL);
80 			gact.sa_flags = act->sa_flags | SA_SIGINFO;
81 
82 			/*
83 			 * Check if the signal handler is being set to
84 			 * the default or ignore handlers:
85 			 */
86 			if (act->sa_handler == SIG_DFL ||
87 			    act->sa_handler == SIG_IGN)
88 				/* Specify the built in handler: */
89 				gact.sa_handler = act->sa_handler;
90 			else
91 				/* Specify the thread kernel signal handler */
92 				gact.sa_handler =
93 					(void (*) (int)) _thread_sig_handler;
94 
95 			/* Change the signal action in the kernel: */
96 		    	if (_thread_sys_sigaction(sig, &gact, NULL) != 0)
97 				ret = -1;
98 		}
99 	}
100 
101 	/* Return the completion status: */
102 	return (ret);
103 }
104 #endif
105