1 /*        $NetBSD: signalvar.h,v 1.104 2021/11/01 05:07:17 thorpej Exp $        */
2 
3 /*
4  * Copyright (c) 1991, 1993
5  *        The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  *        @(#)signalvar.h     8.6 (Berkeley) 2/19/95
32  */
33 
34 #ifndef   _SYS_SIGNALVAR_H_
35 #define   _SYS_SIGNALVAR_H_
36 
37 #include <sys/siginfo.h>
38 #include <sys/queue.h>
39 #include <sys/mutex.h>
40 #include <sys/stdbool.h>
41 
42 #ifndef _KERNEL
43 #include <string.h>     /* Required for memset(3) and memcpy(3) prototypes */
44 #endif /* _KERNEL */
45 
46 /*
47  * Kernel signal definitions and data structures,
48  * not exported to user programs.
49  */
50 
51 /*
52  * Queue of signals.
53  */
54 typedef TAILQ_HEAD(ksiginfoq, ksiginfo) ksiginfoq_t;
55 
56 /*
57  * Process signal actions, possibly shared between processes.
58  */
59 struct sigacts {
60           struct sigact_sigdesc {
61                     struct sigaction sd_sigact;
62                     const void          *sd_tramp;
63                     int                 sd_vers;
64           } sa_sigdesc[NSIG];           /* disposition of signals */
65 
66           int                 sa_refcnt;          /* reference count */
67           kmutex_t  sa_mutex; /* lock on sa_refcnt */
68 };
69 
70 /*
71  * Pending signals, per LWP and per process.
72  */
73 typedef struct sigpend {
74           ksiginfoq_t         sp_info;
75           sigset_t  sp_set;
76 } sigpend_t;
77 
78 /*
79  * Process signal state.
80  */
81 struct sigctx {
82           struct _ksiginfo ps_info;     /* for core dump/debugger XXX */
83           int                  ps_lwp;  /* for core dump/debugger XXX */
84           bool                 ps_faked;          /* for core dump/debugger XXX */
85           void                *ps_sigcode;        /* address of signal trampoline */
86           sigset_t   ps_sigignore;      /* Signals being ignored. */
87           sigset_t   ps_sigcatch;       /* Signals being caught by user. */
88           sigset_t   ps_sigpass;        /* Signals evading the debugger. */
89 };
90 
91 /* additional signal action values, used only temporarily/internally */
92 #define   SIG_CATCH (void (*)(int))2
93 
94 /*
95  * get signal action for process and signal; currently only for current process
96  */
97 #define SIGACTION(p, sig)     (p->p_sigacts->sa_sigdesc[(sig)].sd_sigact)
98 #define   SIGACTION_PS(ps, sig)         (ps->sa_sigdesc[(sig)].sd_sigact)
99 
100 /*
101  * Copy a sigaction structure without padding.
102  */
103 static __inline void
sigaction_copy(struct sigaction * dst,const struct sigaction * src)104 sigaction_copy(struct sigaction *dst, const struct sigaction *src)
105 {
106           memset(dst, 0, sizeof(*dst));
107           dst->_sa_u._sa_handler = src->_sa_u._sa_handler;
108           memcpy(&dst->sa_mask, &src->sa_mask, sizeof(dst->sa_mask));
109           dst->sa_flags = src->sa_flags;
110 }
111 
112 /*
113  * Signal properties and actions.
114  * The array below categorizes the signals and their default actions
115  * according to the following properties:
116  */
117 #define   SA_KILL             0x0001              /* terminates process by default */
118 #define   SA_CORE             0x0002              /* ditto and coredumps */
119 #define   SA_STOP             0x0004              /* suspend process */
120 #define   SA_TTYSTOP          0x0008              /* ditto, from tty */
121 #define   SA_IGNORE 0x0010              /* ignore by default */
122 #define   SA_CONT             0x0020              /* continue if suspended */
123 #define   SA_CANTMASK         0x0040              /* non-maskable, catchable */
124 #define   SA_NORESET          0x0080              /* not reset when caught */
125 #define   SA_TOLWP  0x0100              /* to LWP that generated, if local */
126 #define   SA_TOALL  0x0200              /* always to all LWPs */
127 
128 #ifdef _KERNEL
129 
130 #include <sys/systm.h>                            /* for copyin_t/copyout_t */
131 
132 extern sigset_t contsigmask, stopsigmask, sigcantmask;
133 
134 struct vnode;
135 struct coredump_iostate;
136 
137 /*
138  * Machine-independent functions:
139  */
140 int       coredump_netbsd(struct lwp *, struct coredump_iostate *);
141 int       coredump_netbsd32(struct lwp *, struct coredump_iostate *);
142 int       real_coredump_netbsd(struct lwp *, struct coredump_iostate *);
143 void      execsigs(struct proc *);
144 int       issignal(struct lwp *);
145 void      pgsignal(struct pgrp *, int, int);
146 void      kpgsignal(struct pgrp *, struct ksiginfo *, void *, int);
147 void      postsig(int);
148 void      psignal(struct proc *, int);
149 void      kpsignal(struct proc *, struct ksiginfo *, void *);
150 void      child_psignal(struct proc *, int);
151 void      siginit(struct proc *);
152 void      trapsignal(struct lwp *, struct ksiginfo *);
153 void      sigexit(struct lwp *, int) __dead;
154 void      killproc(struct proc *, const char *);
155 void      setsigvec(struct proc *, int, struct sigaction *);
156 int       killpg1(struct lwp *, struct ksiginfo *, int, int);
157 void      proc_unstop(struct proc *p);
158 void      eventswitch(int, int, int);
159 void      eventswitchchild(struct proc *, int, int);
160 
161 int       sigaction1(struct lwp *, int, const struct sigaction *,
162               struct sigaction *, const void *, int);
163 int       sigprocmask1(struct lwp *, int, const sigset_t *, sigset_t *);
164 void      sigpending1(struct lwp *, sigset_t *);
165 void      sigsuspendsetup(struct lwp *, const sigset_t *);
166 void      sigsuspendteardown(struct lwp *);
167 int       sigsuspend1(struct lwp *, const sigset_t *);
168 int       sigaltstack1(struct lwp *, const stack_t *, stack_t *);
169 int       sigismasked(struct lwp *, int);
170 
171 int       sigget(sigpend_t *, ksiginfo_t *, int, const sigset_t *);
172 void      sigclear(sigpend_t *, const sigset_t *, ksiginfoq_t *);
173 void      sigclearall(struct proc *, const sigset_t *, ksiginfoq_t *);
174 
175 int       kpsignal2(struct proc *, ksiginfo_t *);
176 
177 void      signal_init(void);
178 
179 struct sigacts      *sigactsinit(struct proc *, int);
180 void      sigactsunshare(struct proc *);
181 void      sigactsfree(struct sigacts *);
182 
183 void      kpsendsig(struct lwp *, const struct ksiginfo *, const sigset_t *);
184 void      sendsig_reset(struct lwp *, int);
185 void      sendsig(const struct ksiginfo *, const sigset_t *);
186 
187 ksiginfo_t          *ksiginfo_alloc(struct proc *, ksiginfo_t *, int);
188 void      ksiginfo_free(ksiginfo_t *);
189 void      ksiginfo_queue_drain0(ksiginfoq_t *);
190 
191 struct sys_____sigtimedwait50_args;
192 int       sigtimedwait1(struct lwp *, const struct sys_____sigtimedwait50_args *,
193     register_t *, copyin_t, copyout_t, copyin_t, copyout_t);
194 
195 void      signotify(struct lwp *);
196 int       sigispending(struct lwp *, int);
197 
198 /*
199  * Machine-dependent functions:
200  */
201 void      sendsig_sigcontext(const struct ksiginfo *, const sigset_t *);
202 void      sendsig_siginfo(const struct ksiginfo *, const sigset_t *);
203 
204 extern    struct pool ksiginfo_pool;
205 
206 /*
207  * firstsig:
208  *
209  *        Return the first signal in a signal set.
210  */
211 static __inline int
firstsig(const sigset_t * ss)212 firstsig(const sigset_t *ss)
213 {
214           int sig;
215 
216           sig = ffs(ss->__bits[0]);
217           if (sig != 0)
218                     return (sig);
219 #if NSIG > 33
220           sig = ffs(ss->__bits[1]);
221           if (sig != 0)
222                     return (sig + 32);
223 #endif
224 #if NSIG > 65
225           sig = ffs(ss->__bits[2]);
226           if (sig != 0)
227                     return (sig + 64);
228 #endif
229 #if NSIG > 97
230           sig = ffs(ss->__bits[3]);
231           if (sig != 0)
232                     return (sig + 96);
233 #endif
234           return (0);
235 }
236 
237 static __inline void
ksiginfo_queue_init(ksiginfoq_t * kq)238 ksiginfo_queue_init(ksiginfoq_t *kq)
239 {
240           TAILQ_INIT(kq);
241 }
242 
243 static __inline void
ksiginfo_queue_drain(ksiginfoq_t * kq)244 ksiginfo_queue_drain(ksiginfoq_t *kq)
245 {
246           if (!TAILQ_EMPTY(kq))
247                     ksiginfo_queue_drain0(kq);
248 }
249 
250 #endif    /* _KERNEL */
251 
252 #ifdef    _KERNEL
253 #ifdef    SIGPROP
254 const int sigprop[NSIG] = {
255           0,                                                /* 0 unused */
256           SA_KILL,                                /* 1 SIGHUP */
257           SA_KILL,                                /* 2 SIGINT */
258           SA_KILL|SA_CORE,                        /* 3 SIGQUIT */
259           SA_KILL|SA_CORE|SA_NORESET|SA_TOLWP,    /* 4 SIGILL */
260           SA_KILL|SA_CORE|SA_NORESET|SA_TOLWP,    /* 5 SIGTRAP */
261           SA_KILL|SA_CORE,                        /* 6 SIGABRT */
262           SA_KILL|SA_CORE|SA_TOLWP,               /* 7 SIGEMT */
263           SA_KILL|SA_CORE|SA_TOLWP,               /* 8 SIGFPE */
264           SA_KILL|SA_CANTMASK|SA_TOALL,           /* 9 SIGKILL */
265           SA_KILL|SA_CORE|SA_TOLWP,               /* 10 SIGBUS */
266           SA_KILL|SA_CORE|SA_TOLWP,               /* 11 SIGSEGV */
267           SA_KILL|SA_CORE|SA_TOLWP,               /* 12 SIGSYS */
268           SA_KILL,                                /* 13 SIGPIPE */
269           SA_KILL,                                /* 14 SIGALRM */
270           SA_KILL,                                /* 15 SIGTERM */
271           SA_IGNORE,                                        /* 16 SIGURG */
272           SA_STOP|SA_CANTMASK|SA_TOALL,           /* 17 SIGSTOP */
273           SA_STOP|SA_TTYSTOP|SA_TOALL,            /* 18 SIGTSTP */
274           SA_IGNORE|SA_CONT|SA_TOALL,             /* 19 SIGCONT */
275           SA_IGNORE,                                        /* 20 SIGCHLD */
276           SA_STOP|SA_TTYSTOP|SA_TOALL,            /* 21 SIGTTIN */
277           SA_STOP|SA_TTYSTOP|SA_TOALL,            /* 22 SIGTTOU */
278           SA_IGNORE,                                        /* 23 SIGIO */
279           SA_KILL,                                /* 24 SIGXCPU */
280           SA_KILL,                                /* 25 SIGXFSZ */
281           SA_KILL,                                /* 26 SIGVTALRM */
282           SA_KILL,                                /* 27 SIGPROF */
283           SA_IGNORE,                                        /* 28 SIGWINCH  */
284           SA_IGNORE,                                        /* 29 SIGINFO */
285           SA_KILL,                                /* 30 SIGUSR1 */
286           SA_KILL,                                /* 31 SIGUSR2 */
287           SA_IGNORE|SA_NORESET,                             /* 32 SIGPWR */
288           SA_KILL,                                /* 33 SIGRTMIN + 0 */
289           SA_KILL,                                /* 34 SIGRTMIN + 1 */
290           SA_KILL,                                /* 35 SIGRTMIN + 2 */
291           SA_KILL,                                /* 36 SIGRTMIN + 3 */
292           SA_KILL,                                /* 37 SIGRTMIN + 4 */
293           SA_KILL,                                /* 38 SIGRTMIN + 5 */
294           SA_KILL,                                /* 39 SIGRTMIN + 6 */
295           SA_KILL,                                /* 40 SIGRTMIN + 7 */
296           SA_KILL,                                /* 41 SIGRTMIN + 8 */
297           SA_KILL,                                /* 42 SIGRTMIN + 9 */
298           SA_KILL,                                /* 43 SIGRTMIN + 10 */
299           SA_KILL,                                /* 44 SIGRTMIN + 11 */
300           SA_KILL,                                /* 45 SIGRTMIN + 12 */
301           SA_KILL,                                /* 46 SIGRTMIN + 13 */
302           SA_KILL,                                /* 47 SIGRTMIN + 14 */
303           SA_KILL,                                /* 48 SIGRTMIN + 15 */
304           SA_KILL,                                /* 49 SIGRTMIN + 16 */
305           SA_KILL,                                /* 50 SIGRTMIN + 17 */
306           SA_KILL,                                /* 51 SIGRTMIN + 18 */
307           SA_KILL,                                /* 52 SIGRTMIN + 19 */
308           SA_KILL,                                /* 53 SIGRTMIN + 20 */
309           SA_KILL,                                /* 54 SIGRTMIN + 21 */
310           SA_KILL,                                /* 55 SIGRTMIN + 22 */
311           SA_KILL,                                /* 56 SIGRTMIN + 23 */
312           SA_KILL,                                /* 57 SIGRTMIN + 24 */
313           SA_KILL,                                /* 58 SIGRTMIN + 25 */
314           SA_KILL,                                /* 59 SIGRTMIN + 26 */
315           SA_KILL,                                /* 60 SIGRTMIN + 27 */
316           SA_KILL,                                /* 61 SIGRTMIN + 28 */
317           SA_KILL,                                /* 62 SIGRTMIN + 29 */
318           SA_KILL,                                /* 63 SIGRTMIN + 30 */
319 };
320 #undef    SIGPROP
321 #else
322 extern const int sigprop[NSIG];
323 #endif    /* SIGPROP */
324 #endif    /* _KERNEL */
325 #endif    /* !_SYS_SIGNALVAR_H_ */
326