1 /*	$NetBSD: posix_signals.c,v 1.1.1.1 2009/06/23 10:09:00 tron Exp $	*/
2 
3 /*++
4 /* NAME
5 /*	posix_signals 3
6 /* SUMMARY
7 /*	POSIX signal handling compatibility
8 /* SYNOPSIS
9 /*	#include <posix_signals.h>
10 /*
11 /*	int	sigemptyset(m)
12 /*	sigset_t *m;
13 /*
14 /*	int	sigaddset(set, signum)
15 /*	sigset_t *set;
16 /*	int	signum;
17 /*
18 /*	int	sigprocmask(how, set, old)
19 /*	int	how;
20 /*	sigset_t *set;
21 /*	sigset_t *old;
22 /*
23 /*	int	sigaction(sig, act, oact)
24 /*	int	sig;
25 /*	struct sigaction *act;
26 /*	struct sigaction *oact;
27 /* DESCRIPTION
28 /*	These routines emulate the POSIX signal handling interface.
29 /* AUTHOR(S)
30 /*	Pieter Schoenmakers
31 /*	Eindhoven University of Technology
32 /*	P.O. Box 513
33 /*	5600 MB Eindhoven
34 /*	The Netherlands
35 /*--*/
36 
37 /* System library. */
38 
39 #include "sys_defs.h"
40 #include <signal.h>
41 #include <errno.h>
42 
43 /* Utility library.*/
44 
45 #include "posix_signals.h"
46 
47 #ifdef MISSING_SIGSET_T
48 
sigemptyset(sigset_t * m)49 int     sigemptyset(sigset_t *m)
50 {
51     return *m = 0;
52 }
53 
sigaddset(sigset_t * set,int signum)54 int     sigaddset(sigset_t *set, int signum)
55 {
56     *set |= sigmask(signum);
57     return 0;
58 }
59 
sigprocmask(int how,sigset_t * set,sigset_t * old)60 int     sigprocmask(int how, sigset_t *set, sigset_t *old)
61 {
62     int previous;
63 
64     if (how == SIG_BLOCK)
65 	previous = sigblock(*set);
66     else if (how == SIG_SETMASK)
67 	previous = sigsetmask(*set);
68     else if (how == SIG_UNBLOCK) {
69 	int     m = sigblock(0);
70 
71 	previous = sigsetmask(m & ~*set);
72     } else {
73 	errno = EINVAL;
74 	return -1;
75     }
76 
77     if (old)
78 	*old = previous;
79     return 0;
80 }
81 
82 #endif
83 
84 #ifdef MISSING_SIGACTION
85 
86 static struct sigaction actions[NSIG] = {};
87 
sighandle(int signum)88 static int sighandle(int signum)
89 {
90     if (signum == SIGCHLD) {
91 	/* XXX If the child is just stopped, don't invoke the handler.	 */
92     }
93     actions[signum].sa_handler(signum);
94 }
95 
sigaction(int sig,struct sigaction * act,struct sigaction * oact)96 int     sigaction(int sig, struct sigaction *act, struct sigaction *oact)
97 {
98     static int initialized = 0;
99 
100     if (!initialized) {
101 	int     i;
102 
103 	for (i = 0; i < NSIG; i++)
104 	    actions[i].sa_handler = SIG_DFL;
105 	initialized = 1;
106     }
107     if (sig <= 0 || sig >= NSIG) {
108 	errno = EINVAL;
109 	return -1;
110     }
111     if (oact)
112 	*oact = actions[sig];
113 
114     {
115 	struct sigvec mine = {
116 	    sighandle, act->sa_mask,
117 	    act->sa_flags & SA_RESTART ? SV_INTERRUPT : 0
118 	};
119 
120 	if (sigvec(sig, &mine, NULL))
121 	    return -1;
122     }
123 
124     actions[sig] = *act;
125     return 0;
126 }
127 
128 #endif
129