kern_sig.c revision 1.31 1 1.31 mycroft /* $NetBSD: kern_sig.c,v 1.31 1994/08/30 03:05:42 mycroft Exp $ */
2 1.29 cgd
3 1.29 cgd /*
4 1.29 cgd * Copyright (c) 1982, 1986, 1989, 1991, 1993
5 1.29 cgd * The Regents of the University of California. All rights reserved.
6 1.29 cgd * (c) UNIX System Laboratories, Inc.
7 1.29 cgd * All or some portions of this file are derived from material licensed
8 1.29 cgd * to the University of California by American Telephone and Telegraph
9 1.29 cgd * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10 1.29 cgd * the permission of UNIX System Laboratories, Inc.
11 1.29 cgd *
12 1.29 cgd * Redistribution and use in source and binary forms, with or without
13 1.29 cgd * modification, are permitted provided that the following conditions
14 1.29 cgd * are met:
15 1.29 cgd * 1. Redistributions of source code must retain the above copyright
16 1.29 cgd * notice, this list of conditions and the following disclaimer.
17 1.29 cgd * 2. Redistributions in binary form must reproduce the above copyright
18 1.29 cgd * notice, this list of conditions and the following disclaimer in the
19 1.29 cgd * documentation and/or other materials provided with the distribution.
20 1.29 cgd * 3. All advertising materials mentioning features or use of this software
21 1.29 cgd * must display the following acknowledgement:
22 1.29 cgd * This product includes software developed by the University of
23 1.29 cgd * California, Berkeley and its contributors.
24 1.29 cgd * 4. Neither the name of the University nor the names of its contributors
25 1.29 cgd * may be used to endorse or promote products derived from this software
26 1.29 cgd * without specific prior written permission.
27 1.29 cgd *
28 1.29 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 1.29 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 1.29 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 1.29 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 1.29 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 1.29 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 1.29 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 1.29 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 1.29 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 1.29 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 1.29 cgd * SUCH DAMAGE.
39 1.29 cgd *
40 1.29 cgd * @(#)kern_sig.c 8.7 (Berkeley) 4/18/94
41 1.29 cgd */
42 1.29 cgd
43 1.29 cgd #define SIGPROP /* include signal properties table */
44 1.29 cgd #include <sys/param.h>
45 1.29 cgd #include <sys/signalvar.h>
46 1.29 cgd #include <sys/resourcevar.h>
47 1.29 cgd #include <sys/namei.h>
48 1.29 cgd #include <sys/vnode.h>
49 1.29 cgd #include <sys/proc.h>
50 1.29 cgd #include <sys/systm.h>
51 1.29 cgd #include <sys/timeb.h>
52 1.29 cgd #include <sys/times.h>
53 1.29 cgd #include <sys/buf.h>
54 1.29 cgd #include <sys/acct.h>
55 1.29 cgd #include <sys/file.h>
56 1.29 cgd #include <sys/kernel.h>
57 1.29 cgd #include <sys/wait.h>
58 1.29 cgd #include <sys/ktrace.h>
59 1.29 cgd #include <sys/syslog.h>
60 1.29 cgd #include <sys/stat.h>
61 1.29 cgd #include <sys/core.h>
62 1.29 cgd
63 1.29 cgd #include <machine/cpu.h>
64 1.29 cgd
65 1.29 cgd #include <vm/vm.h>
66 1.29 cgd #include <sys/user.h> /* for coredump */
67 1.29 cgd
68 1.29 cgd void stop __P((struct proc *p));
69 1.29 cgd
70 1.29 cgd /*
71 1.29 cgd * Can process p, with pcred pc, send the signal signum to process q?
72 1.29 cgd */
73 1.29 cgd #define CANSIGNAL(p, pc, q, signum) \
74 1.29 cgd ((pc)->pc_ucred->cr_uid == 0 || \
75 1.29 cgd (pc)->p_ruid == (q)->p_cred->p_ruid || \
76 1.29 cgd (pc)->pc_ucred->cr_uid == (q)->p_cred->p_ruid || \
77 1.29 cgd (pc)->p_ruid == (q)->p_ucred->cr_uid || \
78 1.29 cgd (pc)->pc_ucred->cr_uid == (q)->p_ucred->cr_uid || \
79 1.29 cgd ((signum) == SIGCONT && (q)->p_session == (p)->p_session))
80 1.29 cgd
81 1.29 cgd struct sigaction_args {
82 1.29 cgd int signum;
83 1.29 cgd struct sigaction *nsa;
84 1.29 cgd struct sigaction *osa;
85 1.29 cgd };
86 1.29 cgd /* ARGSUSED */
87 1.29 cgd sigaction(p, uap, retval)
88 1.29 cgd struct proc *p;
89 1.29 cgd register struct sigaction_args *uap;
90 1.29 cgd int *retval;
91 1.29 cgd {
92 1.29 cgd struct sigaction vec;
93 1.29 cgd register struct sigaction *sa;
94 1.29 cgd register struct sigacts *ps = p->p_sigacts;
95 1.29 cgd register int signum;
96 1.29 cgd int bit, error;
97 1.29 cgd
98 1.29 cgd signum = uap->signum;
99 1.29 cgd if (signum <= 0 || signum >= NSIG ||
100 1.29 cgd signum == SIGKILL || signum == SIGSTOP)
101 1.29 cgd return (EINVAL);
102 1.29 cgd sa = &vec;
103 1.29 cgd if (uap->osa) {
104 1.29 cgd sa->sa_handler = ps->ps_sigact[signum];
105 1.29 cgd sa->sa_mask = ps->ps_catchmask[signum];
106 1.29 cgd bit = sigmask(signum);
107 1.29 cgd sa->sa_flags = 0;
108 1.29 cgd if ((ps->ps_sigonstack & bit) != 0)
109 1.29 cgd sa->sa_flags |= SA_ONSTACK;
110 1.29 cgd if ((ps->ps_sigintr & bit) == 0)
111 1.29 cgd sa->sa_flags |= SA_RESTART;
112 1.29 cgd if (p->p_flag & P_NOCLDSTOP)
113 1.29 cgd sa->sa_flags |= SA_NOCLDSTOP;
114 1.29 cgd if (error = copyout((caddr_t)sa, (caddr_t)uap->osa,
115 1.29 cgd sizeof (vec)))
116 1.29 cgd return (error);
117 1.29 cgd }
118 1.29 cgd if (uap->nsa) {
119 1.29 cgd if (error = copyin((caddr_t)uap->nsa, (caddr_t)sa,
120 1.29 cgd sizeof (vec)))
121 1.29 cgd return (error);
122 1.29 cgd setsigvec(p, signum, sa);
123 1.29 cgd }
124 1.29 cgd return (0);
125 1.29 cgd }
126 1.29 cgd
127 1.29 cgd setsigvec(p, signum, sa)
128 1.29 cgd register struct proc *p;
129 1.29 cgd int signum;
130 1.29 cgd register struct sigaction *sa;
131 1.29 cgd {
132 1.29 cgd register struct sigacts *ps = p->p_sigacts;
133 1.29 cgd register int bit;
134 1.29 cgd
135 1.29 cgd bit = sigmask(signum);
136 1.29 cgd /*
137 1.29 cgd * Change setting atomically.
138 1.29 cgd */
139 1.29 cgd (void) splhigh();
140 1.29 cgd ps->ps_sigact[signum] = sa->sa_handler;
141 1.29 cgd ps->ps_catchmask[signum] = sa->sa_mask &~ sigcantmask;
142 1.29 cgd if ((sa->sa_flags & SA_RESTART) == 0)
143 1.29 cgd ps->ps_sigintr |= bit;
144 1.29 cgd else
145 1.29 cgd ps->ps_sigintr &= ~bit;
146 1.29 cgd if (sa->sa_flags & SA_ONSTACK)
147 1.29 cgd ps->ps_sigonstack |= bit;
148 1.29 cgd else
149 1.29 cgd ps->ps_sigonstack &= ~bit;
150 1.29 cgd #ifdef COMPAT_SUNOS
151 1.29 cgd if (p->p_emul == EMUL_SUNOS && sa->sa_flags & SA_USERTRAMP)
152 1.29 cgd ps->ps_usertramp |= bit;
153 1.29 cgd else
154 1.29 cgd ps->ps_usertramp &= ~bit;
155 1.29 cgd #endif
156 1.29 cgd if (signum == SIGCHLD) {
157 1.29 cgd if (sa->sa_flags & SA_NOCLDSTOP)
158 1.29 cgd p->p_flag |= P_NOCLDSTOP;
159 1.29 cgd else
160 1.29 cgd p->p_flag &= ~P_NOCLDSTOP;
161 1.29 cgd }
162 1.29 cgd /*
163 1.29 cgd * Set bit in p_sigignore for signals that are set to SIG_IGN,
164 1.29 cgd * and for signals set to SIG_DFL where the default is to ignore.
165 1.29 cgd * However, don't put SIGCONT in p_sigignore,
166 1.29 cgd * as we have to restart the process.
167 1.29 cgd */
168 1.29 cgd if (sa->sa_handler == SIG_IGN ||
169 1.29 cgd (sigprop[signum] & SA_IGNORE && sa->sa_handler == SIG_DFL)) {
170 1.29 cgd p->p_siglist &= ~bit; /* never to be seen again */
171 1.29 cgd if (signum != SIGCONT)
172 1.29 cgd p->p_sigignore |= bit; /* easier in psignal */
173 1.29 cgd p->p_sigcatch &= ~bit;
174 1.29 cgd } else {
175 1.29 cgd p->p_sigignore &= ~bit;
176 1.29 cgd if (sa->sa_handler == SIG_DFL)
177 1.29 cgd p->p_sigcatch &= ~bit;
178 1.29 cgd else
179 1.29 cgd p->p_sigcatch |= bit;
180 1.29 cgd }
181 1.29 cgd (void) spl0();
182 1.29 cgd }
183 1.29 cgd
184 1.29 cgd /*
185 1.29 cgd * Initialize signal state for process 0;
186 1.29 cgd * set to ignore signals that are ignored by default.
187 1.29 cgd */
188 1.29 cgd void
189 1.29 cgd siginit(p)
190 1.29 cgd struct proc *p;
191 1.29 cgd {
192 1.29 cgd register int i;
193 1.29 cgd
194 1.29 cgd for (i = 0; i < NSIG; i++)
195 1.29 cgd if (sigprop[i] & SA_IGNORE && i != SIGCONT)
196 1.29 cgd p->p_sigignore |= sigmask(i);
197 1.29 cgd }
198 1.29 cgd
199 1.29 cgd /*
200 1.29 cgd * Reset signals for an exec of the specified process.
201 1.29 cgd */
202 1.29 cgd void
203 1.29 cgd execsigs(p)
204 1.29 cgd register struct proc *p;
205 1.29 cgd {
206 1.29 cgd register struct sigacts *ps = p->p_sigacts;
207 1.29 cgd register int nc, mask;
208 1.29 cgd
209 1.29 cgd /*
210 1.29 cgd * Reset caught signals. Held signals remain held
211 1.29 cgd * through p_sigmask (unless they were caught,
212 1.29 cgd * and are now ignored by default).
213 1.29 cgd */
214 1.29 cgd while (p->p_sigcatch) {
215 1.29 cgd nc = ffs((long)p->p_sigcatch);
216 1.29 cgd mask = sigmask(nc);
217 1.29 cgd p->p_sigcatch &= ~mask;
218 1.29 cgd if (sigprop[nc] & SA_IGNORE) {
219 1.29 cgd if (nc != SIGCONT)
220 1.29 cgd p->p_sigignore |= mask;
221 1.29 cgd p->p_siglist &= ~mask;
222 1.29 cgd }
223 1.29 cgd ps->ps_sigact[nc] = SIG_DFL;
224 1.29 cgd }
225 1.29 cgd /*
226 1.29 cgd * Reset stack state to the user stack.
227 1.29 cgd * Clear set of signals caught on the signal stack.
228 1.29 cgd */
229 1.29 cgd ps->ps_sigstk.ss_flags = SA_DISABLE;
230 1.29 cgd ps->ps_sigstk.ss_size = 0;
231 1.29 cgd ps->ps_sigstk.ss_base = 0;
232 1.29 cgd ps->ps_flags = 0;
233 1.29 cgd }
234 1.29 cgd
235 1.29 cgd /*
236 1.29 cgd * Manipulate signal mask.
237 1.29 cgd * Note that we receive new mask, not pointer,
238 1.29 cgd * and return old mask as return value;
239 1.29 cgd * the library stub does the rest.
240 1.29 cgd */
241 1.29 cgd struct sigprocmask_args {
242 1.29 cgd int how;
243 1.29 cgd sigset_t mask;
244 1.29 cgd };
245 1.29 cgd sigprocmask(p, uap, retval)
246 1.29 cgd register struct proc *p;
247 1.29 cgd struct sigprocmask_args *uap;
248 1.29 cgd int *retval;
249 1.29 cgd {
250 1.29 cgd int error = 0;
251 1.29 cgd
252 1.29 cgd *retval = p->p_sigmask;
253 1.29 cgd (void) splhigh();
254 1.29 cgd
255 1.29 cgd switch (uap->how) {
256 1.29 cgd case SIG_BLOCK:
257 1.29 cgd p->p_sigmask |= uap->mask &~ sigcantmask;
258 1.29 cgd break;
259 1.29 cgd
260 1.29 cgd case SIG_UNBLOCK:
261 1.29 cgd p->p_sigmask &= ~uap->mask;
262 1.29 cgd break;
263 1.29 cgd
264 1.29 cgd case SIG_SETMASK:
265 1.29 cgd p->p_sigmask = uap->mask &~ sigcantmask;
266 1.29 cgd break;
267 1.29 cgd
268 1.29 cgd default:
269 1.29 cgd error = EINVAL;
270 1.29 cgd break;
271 1.29 cgd }
272 1.29 cgd (void) spl0();
273 1.29 cgd return (error);
274 1.29 cgd }
275 1.29 cgd
276 1.29 cgd /* ARGSUSED */
277 1.29 cgd sigpending(p, uap, retval)
278 1.29 cgd struct proc *p;
279 1.29 cgd void *uap;
280 1.29 cgd int *retval;
281 1.29 cgd {
282 1.29 cgd
283 1.29 cgd *retval = p->p_siglist;
284 1.29 cgd return (0);
285 1.29 cgd }
286 1.29 cgd
287 1.29 cgd #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
288 1.29 cgd /*
289 1.29 cgd * Generalized interface signal handler, 4.3-compatible.
290 1.29 cgd */
291 1.29 cgd struct osigvec_args {
292 1.29 cgd int signum;
293 1.29 cgd struct sigvec *nsv;
294 1.29 cgd struct sigvec *osv;
295 1.29 cgd };
296 1.29 cgd /* ARGSUSED */
297 1.29 cgd osigvec(p, uap, retval)
298 1.29 cgd struct proc *p;
299 1.29 cgd register struct osigvec_args *uap;
300 1.29 cgd int *retval;
301 1.29 cgd {
302 1.29 cgd struct sigvec vec;
303 1.29 cgd register struct sigacts *ps = p->p_sigacts;
304 1.29 cgd register struct sigvec *sv;
305 1.29 cgd register int signum;
306 1.29 cgd int bit, error;
307 1.29 cgd
308 1.29 cgd signum = uap->signum;
309 1.29 cgd if (signum <= 0 || signum >= NSIG ||
310 1.29 cgd signum == SIGKILL || signum == SIGSTOP)
311 1.29 cgd return (EINVAL);
312 1.29 cgd sv = &vec;
313 1.29 cgd if (uap->osv) {
314 1.29 cgd *(sig_t *)&sv->sv_handler = ps->ps_sigact[signum];
315 1.29 cgd sv->sv_mask = ps->ps_catchmask[signum];
316 1.29 cgd bit = sigmask(signum);
317 1.29 cgd sv->sv_flags = 0;
318 1.29 cgd if ((ps->ps_sigonstack & bit) != 0)
319 1.29 cgd sv->sv_flags |= SV_ONSTACK;
320 1.29 cgd if ((ps->ps_sigintr & bit) != 0)
321 1.29 cgd sv->sv_flags |= SV_INTERRUPT;
322 1.29 cgd #ifdef COMPAT_SUNOS
323 1.29 cgd if (p->p_emul != EMUL_SUNOS)
324 1.29 cgd #endif
325 1.29 cgd if (p->p_flag & P_NOCLDSTOP)
326 1.29 cgd sv->sv_flags |= SA_NOCLDSTOP;
327 1.29 cgd if (error = copyout((caddr_t)sv, (caddr_t)uap->osv,
328 1.29 cgd sizeof (vec)))
329 1.29 cgd return (error);
330 1.29 cgd }
331 1.29 cgd if (uap->nsv) {
332 1.29 cgd if (error = copyin((caddr_t)uap->nsv, (caddr_t)sv,
333 1.29 cgd sizeof (vec)))
334 1.29 cgd return (error);
335 1.29 cgd #ifdef COMPAT_SUNOS
336 1.29 cgd /*
337 1.29 cgd * SunOS uses this bit (4, aka SA_DISABLE) as SV_RESETHAND,
338 1.29 cgd * `reset to SIG_DFL on delivery'. We have no such option
339 1.29 cgd * now or ever!
340 1.29 cgd */
341 1.29 cgd if (p->p_emul == EMUL_SUNOS) {
342 1.29 cgd if (sv->sv_flags & SA_DISABLE)
343 1.29 cgd return (EINVAL);
344 1.29 cgd sv->sv_flags |= SA_USERTRAMP;
345 1.29 cgd }
346 1.29 cgd #endif
347 1.29 cgd sv->sv_flags ^= SA_RESTART; /* opposite of SV_INTERRUPT */
348 1.29 cgd setsigvec(p, signum, (struct sigaction *)sv);
349 1.29 cgd }
350 1.29 cgd return (0);
351 1.29 cgd }
352 1.29 cgd
353 1.29 cgd struct osigblock_args {
354 1.29 cgd int mask;
355 1.29 cgd };
356 1.29 cgd osigblock(p, uap, retval)
357 1.29 cgd register struct proc *p;
358 1.29 cgd struct osigblock_args *uap;
359 1.29 cgd int *retval;
360 1.29 cgd {
361 1.29 cgd
362 1.29 cgd (void) splhigh();
363 1.29 cgd *retval = p->p_sigmask;
364 1.29 cgd p->p_sigmask |= uap->mask &~ sigcantmask;
365 1.29 cgd (void) spl0();
366 1.29 cgd return (0);
367 1.29 cgd }
368 1.29 cgd
369 1.29 cgd struct osigsetmask_args {
370 1.29 cgd int mask;
371 1.29 cgd };
372 1.29 cgd int
373 1.29 cgd osigsetmask(p, uap, retval)
374 1.29 cgd struct proc *p;
375 1.29 cgd struct osigsetmask_args *uap;
376 1.29 cgd int *retval;
377 1.29 cgd {
378 1.29 cgd
379 1.29 cgd (void) splhigh();
380 1.29 cgd *retval = p->p_sigmask;
381 1.29 cgd p->p_sigmask = uap->mask &~ sigcantmask;
382 1.29 cgd (void) spl0();
383 1.29 cgd return (0);
384 1.29 cgd }
385 1.29 cgd #endif /* COMPAT_43 || COMPAT_SUNOS */
386 1.29 cgd
387 1.29 cgd /*
388 1.29 cgd * Suspend process until signal, providing mask to be set
389 1.29 cgd * in the meantime. Note nonstandard calling convention:
390 1.29 cgd * libc stub passes mask, not pointer, to save a copyin.
391 1.29 cgd */
392 1.29 cgd struct sigsuspend_args {
393 1.29 cgd sigset_t mask;
394 1.29 cgd };
395 1.29 cgd /* ARGSUSED */
396 1.29 cgd int
397 1.29 cgd sigsuspend(p, uap, retval)
398 1.29 cgd register struct proc *p;
399 1.29 cgd struct sigsuspend_args *uap;
400 1.29 cgd int *retval;
401 1.29 cgd {
402 1.29 cgd register struct sigacts *ps = p->p_sigacts;
403 1.29 cgd
404 1.29 cgd /*
405 1.29 cgd * When returning from sigpause, we want
406 1.29 cgd * the old mask to be restored after the
407 1.29 cgd * signal handler has finished. Thus, we
408 1.29 cgd * save it here and mark the sigacts structure
409 1.29 cgd * to indicate this.
410 1.29 cgd */
411 1.29 cgd ps->ps_oldmask = p->p_sigmask;
412 1.29 cgd ps->ps_flags |= SAS_OLDMASK;
413 1.29 cgd p->p_sigmask = uap->mask &~ sigcantmask;
414 1.29 cgd while (tsleep((caddr_t) ps, PPAUSE|PCATCH, "pause", 0) == 0)
415 1.29 cgd /* void */;
416 1.29 cgd /* always return EINTR rather than ERESTART... */
417 1.29 cgd return (EINTR);
418 1.29 cgd }
419 1.29 cgd
420 1.29 cgd #if defined(COMPAT_43) || defined(COMPAT_SUNOS) || defined(COMPAT_HPUX)
421 1.29 cgd struct osigstack_args {
422 1.29 cgd struct sigstack *nss;
423 1.29 cgd struct sigstack *oss;
424 1.29 cgd };
425 1.29 cgd /* ARGSUSED */
426 1.29 cgd int
427 1.29 cgd osigstack(p, uap, retval)
428 1.29 cgd struct proc *p;
429 1.29 cgd register struct osigstack_args *uap;
430 1.29 cgd int *retval;
431 1.29 cgd {
432 1.29 cgd struct sigstack ss;
433 1.29 cgd struct sigacts *psp;
434 1.29 cgd int error = 0;
435 1.29 cgd
436 1.29 cgd psp = p->p_sigacts;
437 1.29 cgd ss.ss_sp = psp->ps_sigstk.ss_base;
438 1.29 cgd ss.ss_onstack = psp->ps_sigstk.ss_flags & SA_ONSTACK;
439 1.29 cgd if (uap->oss && (error = copyout((caddr_t)&ss, (caddr_t)uap->oss,
440 1.29 cgd sizeof (struct sigstack))))
441 1.29 cgd return (error);
442 1.29 cgd if (uap->nss && (error = copyin((caddr_t)uap->nss, (caddr_t)&ss,
443 1.29 cgd sizeof (ss))) == 0) {
444 1.29 cgd psp->ps_sigstk.ss_base = ss.ss_sp;
445 1.29 cgd psp->ps_sigstk.ss_size = 0;
446 1.29 cgd psp->ps_sigstk.ss_flags |= ss.ss_onstack & SA_ONSTACK;
447 1.29 cgd psp->ps_flags |= SAS_ALTSTACK;
448 1.29 cgd }
449 1.29 cgd return (error);
450 1.29 cgd }
451 1.29 cgd #endif /* COMPAT_43 || COMPAT_SUNOS || COMPAT_HPUX */
452 1.29 cgd
453 1.29 cgd struct sigaltstack_args {
454 1.29 cgd struct sigaltstack *nss;
455 1.29 cgd struct sigaltstack *oss;
456 1.29 cgd };
457 1.29 cgd /* ARGSUSED */
458 1.29 cgd sigaltstack(p, uap, retval)
459 1.29 cgd struct proc *p;
460 1.29 cgd register struct sigaltstack_args *uap;
461 1.29 cgd int *retval;
462 1.29 cgd {
463 1.29 cgd struct sigacts *psp;
464 1.29 cgd struct sigaltstack ss;
465 1.29 cgd int error;
466 1.29 cgd
467 1.29 cgd psp = p->p_sigacts;
468 1.29 cgd if ((psp->ps_flags & SAS_ALTSTACK) == 0)
469 1.29 cgd psp->ps_sigstk.ss_flags |= SA_DISABLE;
470 1.29 cgd if (uap->oss && (error = copyout((caddr_t)&psp->ps_sigstk,
471 1.29 cgd (caddr_t)uap->oss, sizeof (struct sigaltstack))))
472 1.29 cgd return (error);
473 1.29 cgd if (uap->nss == 0)
474 1.29 cgd return (0);
475 1.29 cgd if (error = copyin((caddr_t)uap->nss, (caddr_t)&ss, sizeof (ss)))
476 1.29 cgd return (error);
477 1.29 cgd if (ss.ss_flags & SA_DISABLE) {
478 1.29 cgd if (psp->ps_sigstk.ss_flags & SA_ONSTACK)
479 1.29 cgd return (EINVAL);
480 1.29 cgd psp->ps_flags &= ~SAS_ALTSTACK;
481 1.29 cgd psp->ps_sigstk.ss_flags = ss.ss_flags;
482 1.29 cgd return (0);
483 1.29 cgd }
484 1.29 cgd if (ss.ss_size < MINSIGSTKSZ)
485 1.29 cgd return (ENOMEM);
486 1.29 cgd psp->ps_flags |= SAS_ALTSTACK;
487 1.29 cgd psp->ps_sigstk= ss;
488 1.29 cgd return (0);
489 1.29 cgd }
490 1.29 cgd
491 1.29 cgd struct kill_args {
492 1.29 cgd pid_t pid;
493 1.29 cgd int signum;
494 1.29 cgd };
495 1.29 cgd /* ARGSUSED */
496 1.29 cgd int
497 1.29 cgd kill(cp, uap, retval)
498 1.29 cgd register struct proc *cp;
499 1.29 cgd register struct kill_args *uap;
500 1.29 cgd int *retval;
501 1.29 cgd {
502 1.29 cgd register struct proc *p;
503 1.29 cgd register struct pcred *pc = cp->p_cred;
504 1.29 cgd
505 1.29 cgd #ifdef COMPAT_09
506 1.29 cgd uap->pid = (short) uap->pid;
507 1.29 cgd #endif
508 1.29 cgd
509 1.29 cgd if ((u_int)uap->signum >= NSIG)
510 1.29 cgd return (EINVAL);
511 1.29 cgd if (uap->pid > 0) {
512 1.29 cgd /* kill single process */
513 1.29 cgd if ((p = pfind(uap->pid)) == NULL)
514 1.29 cgd return (ESRCH);
515 1.29 cgd if (!CANSIGNAL(cp, pc, p, uap->signum))
516 1.29 cgd return (EPERM);
517 1.29 cgd if (uap->signum)
518 1.29 cgd psignal(p, uap->signum);
519 1.29 cgd return (0);
520 1.29 cgd }
521 1.29 cgd switch (uap->pid) {
522 1.29 cgd case -1: /* broadcast signal */
523 1.29 cgd return (killpg1(cp, uap->signum, 0, 1));
524 1.29 cgd case 0: /* signal own process group */
525 1.29 cgd return (killpg1(cp, uap->signum, 0, 0));
526 1.29 cgd default: /* negative explicit process group */
527 1.29 cgd return (killpg1(cp, uap->signum, -uap->pid, 0));
528 1.29 cgd }
529 1.29 cgd /* NOTREACHED */
530 1.29 cgd }
531 1.29 cgd
532 1.29 cgd #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
533 1.29 cgd struct okillpg_args {
534 1.29 cgd int pgid;
535 1.29 cgd int signum;
536 1.29 cgd };
537 1.29 cgd /* ARGSUSED */
538 1.29 cgd okillpg(p, uap, retval)
539 1.29 cgd struct proc *p;
540 1.29 cgd register struct okillpg_args *uap;
541 1.29 cgd int *retval;
542 1.29 cgd {
543 1.29 cgd
544 1.29 cgd #ifdef COMPAT_09
545 1.29 cgd uap->pgid = (short) uap->pgid;
546 1.29 cgd #endif
547 1.29 cgd
548 1.29 cgd if ((u_int)uap->signum >= NSIG)
549 1.29 cgd return (EINVAL);
550 1.29 cgd return (killpg1(p, uap->signum, uap->pgid, 0));
551 1.29 cgd }
552 1.29 cgd #endif /* COMPAT_43 || COMPAT_SUNOS */
553 1.29 cgd
554 1.29 cgd /*
555 1.29 cgd * Common code for kill process group/broadcast kill.
556 1.29 cgd * cp is calling process.
557 1.29 cgd */
558 1.29 cgd killpg1(cp, signum, pgid, all)
559 1.29 cgd register struct proc *cp;
560 1.29 cgd int signum, pgid, all;
561 1.29 cgd {
562 1.29 cgd register struct proc *p;
563 1.29 cgd register struct pcred *pc = cp->p_cred;
564 1.29 cgd struct pgrp *pgrp;
565 1.29 cgd int nfound = 0;
566 1.29 cgd
567 1.29 cgd if (all)
568 1.29 cgd /*
569 1.29 cgd * broadcast
570 1.29 cgd */
571 1.31 mycroft for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
572 1.29 cgd if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
573 1.29 cgd p == cp || !CANSIGNAL(cp, pc, p, signum))
574 1.29 cgd continue;
575 1.29 cgd nfound++;
576 1.29 cgd if (signum)
577 1.29 cgd psignal(p, signum);
578 1.29 cgd }
579 1.29 cgd else {
580 1.29 cgd if (pgid == 0)
581 1.29 cgd /*
582 1.29 cgd * zero pgid means send to my process group.
583 1.29 cgd */
584 1.29 cgd pgrp = cp->p_pgrp;
585 1.29 cgd else {
586 1.29 cgd pgrp = pgfind(pgid);
587 1.29 cgd if (pgrp == NULL)
588 1.29 cgd return (ESRCH);
589 1.29 cgd }
590 1.31 mycroft for (p = pgrp->pg_members.lh_first; p != 0; p = p->p_pglist.le_next) {
591 1.29 cgd if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
592 1.29 cgd p->p_stat == SZOMB ||
593 1.29 cgd !CANSIGNAL(cp, pc, p, signum))
594 1.29 cgd continue;
595 1.29 cgd nfound++;
596 1.29 cgd if (signum)
597 1.29 cgd psignal(p, signum);
598 1.29 cgd }
599 1.29 cgd }
600 1.29 cgd return (nfound ? 0 : ESRCH);
601 1.29 cgd }
602 1.29 cgd
603 1.29 cgd /*
604 1.29 cgd * Send a signal to a process group.
605 1.29 cgd */
606 1.29 cgd void
607 1.29 cgd gsignal(pgid, signum)
608 1.29 cgd int pgid, signum;
609 1.29 cgd {
610 1.29 cgd struct pgrp *pgrp;
611 1.29 cgd
612 1.29 cgd if (pgid && (pgrp = pgfind(pgid)))
613 1.29 cgd pgsignal(pgrp, signum, 0);
614 1.29 cgd }
615 1.29 cgd
616 1.29 cgd /*
617 1.29 cgd * Send a signal to a process group. If checktty is 1,
618 1.29 cgd * limit to members which have a controlling terminal.
619 1.29 cgd */
620 1.29 cgd void
621 1.29 cgd pgsignal(pgrp, signum, checkctty)
622 1.29 cgd struct pgrp *pgrp;
623 1.29 cgd int signum, checkctty;
624 1.29 cgd {
625 1.29 cgd register struct proc *p;
626 1.29 cgd
627 1.29 cgd if (pgrp)
628 1.31 mycroft for (p = pgrp->pg_members.lh_first; p != 0; p = p->p_pglist.le_next)
629 1.29 cgd if (checkctty == 0 || p->p_flag & P_CONTROLT)
630 1.29 cgd psignal(p, signum);
631 1.29 cgd }
632 1.29 cgd
633 1.29 cgd /*
634 1.29 cgd * Send a signal caused by a trap to the current process.
635 1.29 cgd * If it will be caught immediately, deliver it with correct code.
636 1.29 cgd * Otherwise, post it normally.
637 1.29 cgd */
638 1.29 cgd void
639 1.29 cgd trapsignal(p, signum, code)
640 1.29 cgd struct proc *p;
641 1.29 cgd register int signum;
642 1.29 cgd u_int code;
643 1.29 cgd {
644 1.29 cgd register struct sigacts *ps = p->p_sigacts;
645 1.29 cgd int mask;
646 1.29 cgd
647 1.29 cgd mask = sigmask(signum);
648 1.29 cgd if ((p->p_flag & P_TRACED) == 0 && (p->p_sigcatch & mask) != 0 &&
649 1.29 cgd (p->p_sigmask & mask) == 0) {
650 1.29 cgd p->p_stats->p_ru.ru_nsignals++;
651 1.29 cgd #ifdef KTRACE
652 1.29 cgd if (KTRPOINT(p, KTR_PSIG))
653 1.29 cgd ktrpsig(p->p_tracep, signum, ps->ps_sigact[signum],
654 1.29 cgd p->p_sigmask, code);
655 1.29 cgd #endif
656 1.29 cgd sendsig(ps->ps_sigact[signum], signum, p->p_sigmask, code);
657 1.29 cgd p->p_sigmask |= ps->ps_catchmask[signum] | mask;
658 1.29 cgd } else {
659 1.29 cgd ps->ps_code = code; /* XXX for core dump/debugger */
660 1.29 cgd psignal(p, signum);
661 1.29 cgd }
662 1.29 cgd }
663 1.29 cgd
664 1.29 cgd /*
665 1.29 cgd * Send the signal to the process. If the signal has an action, the action
666 1.29 cgd * is usually performed by the target process rather than the caller; we add
667 1.29 cgd * the signal to the set of pending signals for the process.
668 1.29 cgd *
669 1.29 cgd * Exceptions:
670 1.29 cgd * o When a stop signal is sent to a sleeping process that takes the
671 1.29 cgd * default action, the process is stopped without awakening it.
672 1.29 cgd * o SIGCONT restarts stopped processes (or puts them back to sleep)
673 1.29 cgd * regardless of the signal action (eg, blocked or ignored).
674 1.29 cgd *
675 1.29 cgd * Other ignored signals are discarded immediately.
676 1.29 cgd */
677 1.29 cgd void
678 1.29 cgd psignal(p, signum)
679 1.29 cgd register struct proc *p;
680 1.29 cgd register int signum;
681 1.29 cgd {
682 1.29 cgd register int s, prop;
683 1.29 cgd register sig_t action;
684 1.29 cgd int mask;
685 1.29 cgd
686 1.29 cgd if ((u_int)signum >= NSIG || signum == 0)
687 1.29 cgd panic("psignal signal number");
688 1.29 cgd mask = sigmask(signum);
689 1.29 cgd prop = sigprop[signum];
690 1.29 cgd
691 1.29 cgd /*
692 1.29 cgd * If proc is traced, always give parent a chance.
693 1.29 cgd */
694 1.29 cgd if (p->p_flag & P_TRACED)
695 1.29 cgd action = SIG_DFL;
696 1.29 cgd else {
697 1.29 cgd /*
698 1.29 cgd * If the signal is being ignored,
699 1.29 cgd * then we forget about it immediately.
700 1.29 cgd * (Note: we don't set SIGCONT in p_sigignore,
701 1.29 cgd * and if it is set to SIG_IGN,
702 1.29 cgd * action will be SIG_DFL here.)
703 1.29 cgd */
704 1.29 cgd if (p->p_sigignore & mask)
705 1.29 cgd return;
706 1.29 cgd if (p->p_sigmask & mask)
707 1.29 cgd action = SIG_HOLD;
708 1.29 cgd else if (p->p_sigcatch & mask)
709 1.29 cgd action = SIG_CATCH;
710 1.29 cgd else
711 1.29 cgd action = SIG_DFL;
712 1.29 cgd }
713 1.29 cgd
714 1.29 cgd if (p->p_nice > NZERO && action == SIG_DFL && (prop & SA_KILL) &&
715 1.29 cgd (p->p_flag & P_TRACED) == 0)
716 1.29 cgd p->p_nice = NZERO;
717 1.29 cgd
718 1.29 cgd if (prop & SA_CONT)
719 1.29 cgd p->p_siglist &= ~stopsigmask;
720 1.29 cgd
721 1.29 cgd if (prop & SA_STOP) {
722 1.29 cgd /*
723 1.29 cgd * If sending a tty stop signal to a member of an orphaned
724 1.29 cgd * process group, discard the signal here if the action
725 1.29 cgd * is default; don't stop the process below if sleeping,
726 1.29 cgd * and don't clear any pending SIGCONT.
727 1.29 cgd */
728 1.29 cgd if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0 &&
729 1.29 cgd action == SIG_DFL)
730 1.29 cgd return;
731 1.29 cgd p->p_siglist &= ~contsigmask;
732 1.29 cgd }
733 1.29 cgd p->p_siglist |= mask;
734 1.29 cgd
735 1.29 cgd /*
736 1.29 cgd * Defer further processing for signals which are held,
737 1.29 cgd * except that stopped processes must be continued by SIGCONT.
738 1.29 cgd */
739 1.29 cgd if (action == SIG_HOLD && ((prop & SA_CONT) == 0 || p->p_stat != SSTOP))
740 1.29 cgd return;
741 1.29 cgd s = splhigh();
742 1.29 cgd switch (p->p_stat) {
743 1.29 cgd
744 1.29 cgd case SSLEEP:
745 1.29 cgd /*
746 1.29 cgd * If process is sleeping uninterruptibly
747 1.29 cgd * we can't interrupt the sleep... the signal will
748 1.29 cgd * be noticed when the process returns through
749 1.29 cgd * trap() or syscall().
750 1.29 cgd */
751 1.29 cgd if ((p->p_flag & P_SINTR) == 0)
752 1.29 cgd goto out;
753 1.29 cgd /*
754 1.29 cgd * Process is sleeping and traced... make it runnable
755 1.29 cgd * so it can discover the signal in issignal() and stop
756 1.29 cgd * for the parent.
757 1.29 cgd */
758 1.29 cgd if (p->p_flag & P_TRACED)
759 1.29 cgd goto run;
760 1.29 cgd /*
761 1.29 cgd * If SIGCONT is default (or ignored) and process is
762 1.29 cgd * asleep, we are finished; the process should not
763 1.29 cgd * be awakened.
764 1.29 cgd */
765 1.29 cgd if ((prop & SA_CONT) && action == SIG_DFL) {
766 1.29 cgd p->p_siglist &= ~mask;
767 1.29 cgd goto out;
768 1.29 cgd }
769 1.29 cgd /*
770 1.29 cgd * When a sleeping process receives a stop
771 1.29 cgd * signal, process immediately if possible.
772 1.29 cgd * All other (caught or default) signals
773 1.29 cgd * cause the process to run.
774 1.29 cgd */
775 1.29 cgd if (prop & SA_STOP) {
776 1.29 cgd if (action != SIG_DFL)
777 1.29 cgd goto runfast;
778 1.29 cgd /*
779 1.29 cgd * If a child holding parent blocked,
780 1.29 cgd * stopping could cause deadlock.
781 1.29 cgd */
782 1.29 cgd if (p->p_flag & P_PPWAIT)
783 1.29 cgd goto out;
784 1.29 cgd p->p_siglist &= ~mask;
785 1.29 cgd p->p_xstat = signum;
786 1.29 cgd if ((p->p_pptr->p_flag & P_NOCLDSTOP) == 0)
787 1.29 cgd psignal(p->p_pptr, SIGCHLD);
788 1.29 cgd stop(p);
789 1.29 cgd goto out;
790 1.29 cgd } else
791 1.29 cgd goto runfast;
792 1.29 cgd /*NOTREACHED*/
793 1.29 cgd
794 1.29 cgd case SSTOP:
795 1.29 cgd /*
796 1.29 cgd * If traced process is already stopped,
797 1.29 cgd * then no further action is necessary.
798 1.29 cgd */
799 1.29 cgd if (p->p_flag & P_TRACED)
800 1.29 cgd goto out;
801 1.29 cgd
802 1.29 cgd /*
803 1.29 cgd * Kill signal always sets processes running.
804 1.29 cgd */
805 1.29 cgd if (signum == SIGKILL)
806 1.29 cgd goto runfast;
807 1.29 cgd
808 1.29 cgd if (prop & SA_CONT) {
809 1.29 cgd /*
810 1.29 cgd * If SIGCONT is default (or ignored), we continue the
811 1.29 cgd * process but don't leave the signal in p_siglist, as
812 1.29 cgd * it has no further action. If SIGCONT is held, we
813 1.29 cgd * continue the process and leave the signal in
814 1.29 cgd * p_siglist. If the process catches SIGCONT, let it
815 1.29 cgd * handle the signal itself. If it isn't waiting on
816 1.29 cgd * an event, then it goes back to run state.
817 1.29 cgd * Otherwise, process goes back to sleep state.
818 1.29 cgd */
819 1.29 cgd if (action == SIG_DFL)
820 1.29 cgd p->p_siglist &= ~mask;
821 1.29 cgd if (action == SIG_CATCH)
822 1.29 cgd goto runfast;
823 1.29 cgd if (p->p_wchan == 0)
824 1.29 cgd goto run;
825 1.29 cgd p->p_stat = SSLEEP;
826 1.29 cgd goto out;
827 1.29 cgd }
828 1.29 cgd
829 1.29 cgd if (prop & SA_STOP) {
830 1.29 cgd /*
831 1.29 cgd * Already stopped, don't need to stop again.
832 1.29 cgd * (If we did the shell could get confused.)
833 1.29 cgd */
834 1.29 cgd p->p_siglist &= ~mask; /* take it away */
835 1.29 cgd goto out;
836 1.29 cgd }
837 1.29 cgd
838 1.29 cgd /*
839 1.29 cgd * If process is sleeping interruptibly, then simulate a
840 1.29 cgd * wakeup so that when it is continued, it will be made
841 1.29 cgd * runnable and can look at the signal. But don't make
842 1.29 cgd * the process runnable, leave it stopped.
843 1.29 cgd */
844 1.29 cgd if (p->p_wchan && p->p_flag & P_SINTR)
845 1.29 cgd unsleep(p);
846 1.29 cgd goto out;
847 1.29 cgd
848 1.29 cgd default:
849 1.29 cgd /*
850 1.29 cgd * SRUN, SIDL, SZOMB do nothing with the signal,
851 1.29 cgd * other than kicking ourselves if we are running.
852 1.29 cgd * It will either never be noticed, or noticed very soon.
853 1.29 cgd */
854 1.29 cgd if (p == curproc)
855 1.29 cgd signotify(p);
856 1.29 cgd goto out;
857 1.29 cgd }
858 1.29 cgd /*NOTREACHED*/
859 1.29 cgd
860 1.29 cgd runfast:
861 1.29 cgd /*
862 1.29 cgd * Raise priority to at least PUSER.
863 1.29 cgd */
864 1.29 cgd if (p->p_priority > PUSER)
865 1.29 cgd p->p_priority = PUSER;
866 1.29 cgd run:
867 1.29 cgd setrunnable(p);
868 1.29 cgd out:
869 1.29 cgd splx(s);
870 1.29 cgd }
871 1.29 cgd
872 1.29 cgd /*
873 1.29 cgd * If the current process has received a signal (should be caught or cause
874 1.29 cgd * termination, should interrupt current syscall), return the signal number.
875 1.29 cgd * Stop signals with default action are processed immediately, then cleared;
876 1.29 cgd * they aren't returned. This is checked after each entry to the system for
877 1.29 cgd * a syscall or trap (though this can usually be done without calling issignal
878 1.29 cgd * by checking the pending signal masks in the CURSIG macro.) The normal call
879 1.29 cgd * sequence is
880 1.29 cgd *
881 1.29 cgd * while (signum = CURSIG(curproc))
882 1.29 cgd * postsig(signum);
883 1.29 cgd */
884 1.29 cgd int
885 1.29 cgd issignal(p)
886 1.29 cgd register struct proc *p;
887 1.29 cgd {
888 1.29 cgd register int signum, mask, prop;
889 1.29 cgd
890 1.29 cgd for (;;) {
891 1.29 cgd mask = p->p_siglist & ~p->p_sigmask;
892 1.29 cgd if (p->p_flag & P_PPWAIT)
893 1.29 cgd mask &= ~stopsigmask;
894 1.29 cgd if (mask == 0) /* no signal to send */
895 1.29 cgd return (0);
896 1.29 cgd signum = ffs((long)mask);
897 1.29 cgd mask = sigmask(signum);
898 1.29 cgd prop = sigprop[signum];
899 1.29 cgd /*
900 1.29 cgd * We should see pending but ignored signals
901 1.29 cgd * only if P_TRACED was on when they were posted.
902 1.29 cgd */
903 1.29 cgd if (mask & p->p_sigignore && (p->p_flag & P_TRACED) == 0) {
904 1.29 cgd p->p_siglist &= ~mask;
905 1.29 cgd continue;
906 1.29 cgd }
907 1.29 cgd if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) {
908 1.29 cgd /*
909 1.29 cgd * If traced, always stop, and stay
910 1.29 cgd * stopped until released by the debugger.
911 1.29 cgd */
912 1.29 cgd p->p_xstat = signum;
913 1.29 cgd if (p->p_flag & P_FSTRACE) {
914 1.29 cgd #ifdef PROCFS
915 1.29 cgd /* procfs debugging */
916 1.29 cgd p->p_stat = SSTOP;
917 1.29 cgd wakeup((caddr_t)p);
918 1.29 cgd mi_switch();
919 1.29 cgd #else
920 1.29 cgd panic("procfs debugging");
921 1.29 cgd #endif
922 1.29 cgd } else {
923 1.29 cgd /* ptrace debugging */
924 1.29 cgd psignal(p->p_pptr, SIGCHLD);
925 1.29 cgd do {
926 1.29 cgd stop(p);
927 1.29 cgd mi_switch();
928 1.29 cgd } while (!trace_req(p) && p->p_flag & P_TRACED);
929 1.29 cgd }
930 1.29 cgd
931 1.29 cgd /*
932 1.29 cgd * If the traced bit got turned off, go back up
933 1.29 cgd * to the top to rescan signals. This ensures
934 1.29 cgd * that p_sig* and ps_sigact are consistent.
935 1.29 cgd */
936 1.29 cgd if ((p->p_flag & P_TRACED) == 0)
937 1.29 cgd continue;
938 1.29 cgd
939 1.29 cgd /*
940 1.29 cgd * If parent wants us to take the signal,
941 1.29 cgd * then it will leave it in p->p_xstat;
942 1.29 cgd * otherwise we just look for signals again.
943 1.29 cgd */
944 1.29 cgd p->p_siglist &= ~mask; /* clear the old signal */
945 1.29 cgd signum = p->p_xstat;
946 1.29 cgd if (signum == 0)
947 1.29 cgd continue;
948 1.29 cgd
949 1.29 cgd /*
950 1.29 cgd * Put the new signal into p_siglist. If the
951 1.29 cgd * signal is being masked, look for other signals.
952 1.29 cgd */
953 1.29 cgd mask = sigmask(signum);
954 1.29 cgd p->p_siglist |= mask;
955 1.29 cgd if (p->p_sigmask & mask)
956 1.29 cgd continue;
957 1.29 cgd }
958 1.29 cgd
959 1.29 cgd /*
960 1.29 cgd * Decide whether the signal should be returned.
961 1.29 cgd * Return the signal's number, or fall through
962 1.29 cgd * to clear it from the pending mask.
963 1.29 cgd */
964 1.29 cgd switch ((int)p->p_sigacts->ps_sigact[signum]) {
965 1.29 cgd
966 1.29 cgd case SIG_DFL:
967 1.29 cgd /*
968 1.29 cgd * Don't take default actions on system processes.
969 1.29 cgd */
970 1.29 cgd if (p->p_pid <= 1) {
971 1.29 cgd #ifdef DIAGNOSTIC
972 1.29 cgd /*
973 1.29 cgd * Are you sure you want to ignore SIGSEGV
974 1.29 cgd * in init? XXX
975 1.29 cgd */
976 1.29 cgd printf("Process (pid %d) got signal %d\n",
977 1.29 cgd p->p_pid, signum);
978 1.29 cgd #endif
979 1.29 cgd break; /* == ignore */
980 1.29 cgd }
981 1.29 cgd /*
982 1.29 cgd * If there is a pending stop signal to process
983 1.29 cgd * with default action, stop here,
984 1.29 cgd * then clear the signal. However,
985 1.29 cgd * if process is member of an orphaned
986 1.29 cgd * process group, ignore tty stop signals.
987 1.29 cgd */
988 1.29 cgd if (prop & SA_STOP) {
989 1.29 cgd if (p->p_flag & P_TRACED ||
990 1.29 cgd (p->p_pgrp->pg_jobc == 0 &&
991 1.29 cgd prop & SA_TTYSTOP))
992 1.29 cgd break; /* == ignore */
993 1.29 cgd p->p_xstat = signum;
994 1.29 cgd stop(p);
995 1.29 cgd if ((p->p_pptr->p_flag & P_NOCLDSTOP) == 0)
996 1.29 cgd psignal(p->p_pptr, SIGCHLD);
997 1.29 cgd mi_switch();
998 1.29 cgd break;
999 1.29 cgd } else if (prop & SA_IGNORE) {
1000 1.29 cgd /*
1001 1.29 cgd * Except for SIGCONT, shouldn't get here.
1002 1.29 cgd * Default action is to ignore; drop it.
1003 1.29 cgd */
1004 1.29 cgd break; /* == ignore */
1005 1.29 cgd } else
1006 1.29 cgd return (signum);
1007 1.29 cgd /*NOTREACHED*/
1008 1.29 cgd
1009 1.29 cgd case SIG_IGN:
1010 1.29 cgd /*
1011 1.29 cgd * Masking above should prevent us ever trying
1012 1.29 cgd * to take action on an ignored signal other
1013 1.29 cgd * than SIGCONT, unless process is traced.
1014 1.29 cgd */
1015 1.29 cgd if ((prop & SA_CONT) == 0 &&
1016 1.29 cgd (p->p_flag & P_TRACED) == 0)
1017 1.29 cgd printf("issignal\n");
1018 1.29 cgd break; /* == ignore */
1019 1.29 cgd
1020 1.29 cgd default:
1021 1.29 cgd /*
1022 1.29 cgd * This signal has an action, let
1023 1.29 cgd * postsig() process it.
1024 1.29 cgd */
1025 1.29 cgd return (signum);
1026 1.29 cgd }
1027 1.29 cgd p->p_siglist &= ~mask; /* take the signal! */
1028 1.29 cgd }
1029 1.29 cgd /* NOTREACHED */
1030 1.29 cgd }
1031 1.29 cgd
1032 1.29 cgd /*
1033 1.29 cgd * Put the argument process into the stopped state and notify the parent
1034 1.29 cgd * via wakeup. Signals are handled elsewhere. The process must not be
1035 1.29 cgd * on the run queue.
1036 1.29 cgd */
1037 1.29 cgd void
1038 1.29 cgd stop(p)
1039 1.29 cgd register struct proc *p;
1040 1.29 cgd {
1041 1.29 cgd
1042 1.29 cgd p->p_stat = SSTOP;
1043 1.29 cgd p->p_flag &= ~P_WAITED;
1044 1.29 cgd wakeup((caddr_t)p->p_pptr);
1045 1.29 cgd }
1046 1.29 cgd
1047 1.29 cgd /*
1048 1.29 cgd * Take the action for the specified signal
1049 1.29 cgd * from the current set of pending signals.
1050 1.29 cgd */
1051 1.29 cgd void
1052 1.29 cgd postsig(signum)
1053 1.29 cgd register int signum;
1054 1.29 cgd {
1055 1.29 cgd register struct proc *p = curproc;
1056 1.29 cgd register struct sigacts *ps = p->p_sigacts;
1057 1.29 cgd register sig_t action;
1058 1.29 cgd int code, mask, returnmask;
1059 1.29 cgd
1060 1.29 cgd #ifdef DIAGNOSTIC
1061 1.29 cgd if (signum == 0)
1062 1.29 cgd panic("postsig");
1063 1.29 cgd #endif
1064 1.29 cgd mask = sigmask(signum);
1065 1.29 cgd p->p_siglist &= ~mask;
1066 1.29 cgd action = ps->ps_sigact[signum];
1067 1.29 cgd #ifdef KTRACE
1068 1.29 cgd if (KTRPOINT(p, KTR_PSIG))
1069 1.29 cgd ktrpsig(p->p_tracep,
1070 1.29 cgd signum, action, ps->ps_flags & SAS_OLDMASK ?
1071 1.29 cgd ps->ps_oldmask : p->p_sigmask, 0);
1072 1.29 cgd #endif
1073 1.29 cgd if (action == SIG_DFL) {
1074 1.29 cgd /*
1075 1.29 cgd * Default action, where the default is to kill
1076 1.29 cgd * the process. (Other cases were ignored above.)
1077 1.29 cgd */
1078 1.29 cgd sigexit(p, signum);
1079 1.29 cgd /* NOTREACHED */
1080 1.29 cgd } else {
1081 1.29 cgd /*
1082 1.29 cgd * If we get here, the signal must be caught.
1083 1.29 cgd */
1084 1.29 cgd #ifdef DIAGNOSTIC
1085 1.29 cgd if (action == SIG_IGN || (p->p_sigmask & mask))
1086 1.29 cgd panic("postsig action");
1087 1.29 cgd #endif
1088 1.29 cgd /*
1089 1.29 cgd * Set the new mask value and also defer further
1090 1.29 cgd * occurences of this signal.
1091 1.29 cgd *
1092 1.29 cgd * Special case: user has done a sigpause. Here the
1093 1.29 cgd * current mask is not of interest, but rather the
1094 1.29 cgd * mask from before the sigpause is what we want
1095 1.29 cgd * restored after the signal processing is completed.
1096 1.29 cgd */
1097 1.29 cgd (void) splhigh();
1098 1.29 cgd if (ps->ps_flags & SAS_OLDMASK) {
1099 1.29 cgd returnmask = ps->ps_oldmask;
1100 1.29 cgd ps->ps_flags &= ~SAS_OLDMASK;
1101 1.29 cgd } else
1102 1.29 cgd returnmask = p->p_sigmask;
1103 1.29 cgd p->p_sigmask |= ps->ps_catchmask[signum] | mask;
1104 1.29 cgd (void) spl0();
1105 1.29 cgd p->p_stats->p_ru.ru_nsignals++;
1106 1.29 cgd if (ps->ps_sig != signum) {
1107 1.29 cgd code = 0;
1108 1.29 cgd } else {
1109 1.29 cgd code = ps->ps_code;
1110 1.29 cgd ps->ps_code = 0;
1111 1.29 cgd }
1112 1.29 cgd sendsig(action, signum, returnmask, code);
1113 1.29 cgd }
1114 1.29 cgd }
1115 1.29 cgd
1116 1.29 cgd /*
1117 1.29 cgd * Kill the current process for stated reason.
1118 1.29 cgd */
1119 1.29 cgd killproc(p, why)
1120 1.29 cgd struct proc *p;
1121 1.29 cgd char *why;
1122 1.29 cgd {
1123 1.29 cgd
1124 1.29 cgd log(LOG_ERR, "pid %d was killed: %s\n", p->p_pid, why);
1125 1.29 cgd uprintf("sorry, pid %d was killed: %s\n", p->p_pid, why);
1126 1.29 cgd psignal(p, SIGKILL);
1127 1.29 cgd }
1128 1.29 cgd
1129 1.29 cgd /*
1130 1.29 cgd * Force the current process to exit with the specified signal, dumping core
1131 1.29 cgd * if appropriate. We bypass the normal tests for masked and caught signals,
1132 1.29 cgd * allowing unrecoverable failures to terminate the process without changing
1133 1.29 cgd * signal state. Mark the accounting record with the signal termination.
1134 1.29 cgd * If dumping core, save the signal number for the debugger. Calls exit and
1135 1.29 cgd * does not return.
1136 1.29 cgd */
1137 1.29 cgd int
1138 1.29 cgd sigexit(p, signum)
1139 1.29 cgd register struct proc *p;
1140 1.29 cgd int signum;
1141 1.29 cgd {
1142 1.29 cgd
1143 1.29 cgd p->p_acflag |= AXSIG;
1144 1.29 cgd if (sigprop[signum] & SA_CORE) {
1145 1.29 cgd p->p_sigacts->ps_sig = signum;
1146 1.29 cgd if (coredump(p) == 0)
1147 1.29 cgd signum |= WCOREFLAG;
1148 1.29 cgd }
1149 1.29 cgd exit1(p, W_EXITCODE(0, signum));
1150 1.29 cgd /* NOTREACHED */
1151 1.29 cgd }
1152 1.29 cgd
1153 1.29 cgd /*
1154 1.29 cgd * Dump core, into a file named "progname.core", unless the process was
1155 1.29 cgd * setuid/setgid.
1156 1.29 cgd */
1157 1.29 cgd int
1158 1.29 cgd coredump(p)
1159 1.29 cgd register struct proc *p;
1160 1.29 cgd {
1161 1.29 cgd register struct vnode *vp;
1162 1.29 cgd register struct pcred *pcred = p->p_cred;
1163 1.29 cgd register struct ucred *cred = pcred->pc_ucred;
1164 1.29 cgd register struct vmspace *vm = p->p_vmspace;
1165 1.29 cgd struct nameidata nd;
1166 1.29 cgd struct vattr vattr;
1167 1.29 cgd int error, error1;
1168 1.29 cgd char name[MAXCOMLEN+6]; /* progname.core */
1169 1.29 cgd struct core core;
1170 1.29 cgd
1171 1.29 cgd if (pcred->p_svuid != pcred->p_ruid || pcred->p_svgid != pcred->p_rgid)
1172 1.29 cgd return (EFAULT);
1173 1.30 deraadt if (USPACE + ctob(vm->vm_dsize + vm->vm_ssize) >=
1174 1.29 cgd p->p_rlimit[RLIMIT_CORE].rlim_cur)
1175 1.29 cgd return (EFAULT);
1176 1.29 cgd sprintf(name, "%s.core", p->p_comm);
1177 1.29 cgd NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, name, p);
1178 1.29 cgd if (error = vn_open(&nd,
1179 1.29 cgd O_CREAT | FWRITE, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH))
1180 1.29 cgd return (error);
1181 1.29 cgd vp = nd.ni_vp;
1182 1.29 cgd
1183 1.29 cgd /* Don't dump to non-regular files or files with links. */
1184 1.29 cgd if (vp->v_type != VREG ||
1185 1.29 cgd VOP_GETATTR(vp, &vattr, cred, p) || vattr.va_nlink != 1) {
1186 1.29 cgd error = EFAULT;
1187 1.29 cgd goto out;
1188 1.29 cgd }
1189 1.29 cgd VATTR_NULL(&vattr);
1190 1.29 cgd vattr.va_size = 0;
1191 1.29 cgd LEASE_CHECK(vp, p, cred, LEASE_WRITE);
1192 1.29 cgd VOP_SETATTR(vp, &vattr, cred, p);
1193 1.29 cgd p->p_acflag |= ACORE;
1194 1.29 cgd bcopy(p, &p->p_addr->u_kproc.kp_proc, sizeof(struct proc));
1195 1.29 cgd fill_eproc(p, &p->p_addr->u_kproc.kp_eproc);
1196 1.29 cgd
1197 1.29 cgd core.c_midmag = 0;
1198 1.29 cgd strncpy(core.c_name, p->p_comm, MAXCOMLEN);
1199 1.29 cgd core.c_nseg = 0;
1200 1.29 cgd core.c_signo = p->p_sigacts->ps_sig;
1201 1.29 cgd core.c_ucode = p->p_sigacts->ps_code;
1202 1.29 cgd core.c_cpusize = 0;
1203 1.29 cgd core.c_tsize = (u_long)ctob(vm->vm_tsize);
1204 1.29 cgd core.c_dsize = (u_long)ctob(vm->vm_dsize);
1205 1.29 cgd core.c_ssize = (u_long)round_page(ctob(vm->vm_ssize));
1206 1.29 cgd error = cpu_coredump(p, vp, cred, &core);
1207 1.29 cgd if (error)
1208 1.29 cgd goto out;
1209 1.29 cgd if (core.c_midmag == 0) {
1210 1.29 cgd /* XXX
1211 1.29 cgd * cpu_coredump() didn't bother to set the magic; assume
1212 1.29 cgd * this is a request to do a traditional dump. cpu_coredump()
1213 1.29 cgd * is still responsible for setting sensible values in
1214 1.29 cgd * the core header.
1215 1.29 cgd */
1216 1.29 cgd if (core.c_cpusize == 0)
1217 1.30 deraadt core.c_cpusize = USPACE; /* Just in case */
1218 1.29 cgd error = vn_rdwr(UIO_WRITE, vp, vm->vm_daddr,
1219 1.29 cgd (int)core.c_dsize,
1220 1.29 cgd (off_t)core.c_cpusize, UIO_USERSPACE,
1221 1.29 cgd IO_NODELOCKED|IO_UNIT, cred, (int *) NULL, p);
1222 1.29 cgd if (error)
1223 1.29 cgd goto out;
1224 1.29 cgd error = vn_rdwr(UIO_WRITE, vp,
1225 1.29 cgd (caddr_t) trunc_page(USRSTACK - ctob(vm->vm_ssize)),
1226 1.29 cgd core.c_ssize,
1227 1.29 cgd (off_t)(core.c_cpusize + core.c_dsize), UIO_USERSPACE,
1228 1.29 cgd IO_NODELOCKED|IO_UNIT, cred, (int *) NULL, p);
1229 1.29 cgd } else {
1230 1.29 cgd /*
1231 1.29 cgd * vm_coredump() spits out all appropriate segments.
1232 1.29 cgd * All that's left to do is to write the core header.
1233 1.29 cgd */
1234 1.29 cgd error = vm_coredump(p, vp, cred, &core);
1235 1.29 cgd if (error)
1236 1.29 cgd goto out;
1237 1.29 cgd error = vn_rdwr(UIO_WRITE, vp, (caddr_t)&core,
1238 1.29 cgd (int)core.c_hdrsize, (off_t)0,
1239 1.29 cgd UIO_SYSSPACE, IO_NODELOCKED|IO_UNIT, cred, (int *) NULL, p);
1240 1.29 cgd }
1241 1.29 cgd out:
1242 1.29 cgd VOP_UNLOCK(vp);
1243 1.29 cgd error1 = vn_close(vp, FWRITE, cred, p);
1244 1.29 cgd if (error == 0)
1245 1.29 cgd error = error1;
1246 1.29 cgd return (error);
1247 1.29 cgd }
1248 1.29 cgd
1249 1.29 cgd /*
1250 1.29 cgd * Nonexistent system call-- signal process (may want to handle it).
1251 1.29 cgd * Flag error in case process won't see signal immediately (blocked or ignored).
1252 1.29 cgd */
1253 1.29 cgd /* ARGSUSED */
1254 1.29 cgd int
1255 1.29 cgd nosys(p, args, retval)
1256 1.29 cgd struct proc *p;
1257 1.29 cgd void *args;
1258 1.29 cgd int *retval;
1259 1.29 cgd {
1260 1.29 cgd
1261 1.29 cgd psignal(p, SIGSYS);
1262 1.29 cgd return (EINVAL);
1263 1.29 cgd }
1264