kern_sig.c revision 1.192 1 /* $NetBSD: kern_sig.c,v 1.192 2004/04/03 19:43:08 matt Exp $ */
2
3 /*
4 * Copyright (c) 1982, 1986, 1989, 1991, 1993
5 * The Regents of the University of California. All rights reserved.
6 * (c) UNIX System Laboratories, Inc.
7 * All or some portions of this file are derived from material licensed
8 * to the University of California by American Telephone and Telegraph
9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10 * the permission of UNIX System Laboratories, Inc.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 * @(#)kern_sig.c 8.14 (Berkeley) 5/14/95
37 */
38
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: kern_sig.c,v 1.192 2004/04/03 19:43:08 matt Exp $");
41
42 #include "opt_ktrace.h"
43 #include "opt_compat_sunos.h"
44 #include "opt_compat_netbsd.h"
45 #include "opt_compat_netbsd32.h"
46
47 #define SIGPROP /* include signal properties table */
48 #include <sys/param.h>
49 #include <sys/signalvar.h>
50 #include <sys/resourcevar.h>
51 #include <sys/namei.h>
52 #include <sys/vnode.h>
53 #include <sys/proc.h>
54 #include <sys/systm.h>
55 #include <sys/timeb.h>
56 #include <sys/times.h>
57 #include <sys/buf.h>
58 #include <sys/acct.h>
59 #include <sys/file.h>
60 #include <sys/kernel.h>
61 #include <sys/wait.h>
62 #include <sys/ktrace.h>
63 #include <sys/syslog.h>
64 #include <sys/stat.h>
65 #include <sys/core.h>
66 #include <sys/filedesc.h>
67 #include <sys/malloc.h>
68 #include <sys/pool.h>
69 #include <sys/ucontext.h>
70 #include <sys/sa.h>
71 #include <sys/savar.h>
72 #include <sys/exec.h>
73
74 #include <sys/mount.h>
75 #include <sys/syscallargs.h>
76
77 #include <machine/cpu.h>
78
79 #include <sys/user.h> /* for coredump */
80
81 #include <uvm/uvm_extern.h>
82
83 static void child_psignal(struct proc *, int);
84 static int build_corename(struct proc *, char [MAXPATHLEN]);
85 static void ksiginfo_exithook(struct proc *, void *);
86 static void ksiginfo_put(struct proc *, const ksiginfo_t *);
87 static ksiginfo_t *ksiginfo_get(struct proc *, int);
88 static void kpsignal2(struct proc *, const ksiginfo_t *, int);
89
90 sigset_t contsigmask, stopsigmask, sigcantmask, sigtrapmask;
91
92 struct pool sigacts_pool; /* memory pool for sigacts structures */
93 struct pool siginfo_pool; /* memory pool for siginfo structures */
94 struct pool ksiginfo_pool; /* memory pool for ksiginfo structures */
95
96 /*
97 * Can process p, with pcred pc, send the signal signum to process q?
98 */
99 #define CANSIGNAL(p, pc, q, signum) \
100 ((pc)->pc_ucred->cr_uid == 0 || \
101 (pc)->p_ruid == (q)->p_cred->p_ruid || \
102 (pc)->pc_ucred->cr_uid == (q)->p_cred->p_ruid || \
103 (pc)->p_ruid == (q)->p_ucred->cr_uid || \
104 (pc)->pc_ucred->cr_uid == (q)->p_ucred->cr_uid || \
105 ((signum) == SIGCONT && (q)->p_session == (p)->p_session))
106
107 /*
108 * Remove and return the first ksiginfo element that matches our requested
109 * signal, or return NULL if one not found.
110 */
111 static ksiginfo_t *
112 ksiginfo_get(struct proc *p, int signo)
113 {
114 ksiginfo_t *ksi;
115 int s;
116
117 s = splsoftclock();
118 simple_lock(&p->p_sigctx.ps_silock);
119 CIRCLEQ_FOREACH(ksi, &p->p_sigctx.ps_siginfo, ksi_list) {
120 if (ksi->ksi_signo == signo) {
121 CIRCLEQ_REMOVE(&p->p_sigctx.ps_siginfo, ksi, ksi_list);
122 goto out;
123 }
124 }
125 ksi = NULL;
126 out:
127 simple_unlock(&p->p_sigctx.ps_silock);
128 splx(s);
129 return ksi;
130 }
131
132 /*
133 * Append a new ksiginfo element to the list of pending ksiginfo's, if
134 * we need to (SA_SIGINFO was requested). We replace non RT signals if
135 * they already existed in the queue and we add new entries for RT signals,
136 * or for non RT signals with non-existing entries.
137 */
138 static void
139 ksiginfo_put(struct proc *p, const ksiginfo_t *ksi)
140 {
141 ksiginfo_t *kp;
142 struct sigaction *sa = &SIGACTION_PS(p->p_sigacts, ksi->ksi_signo);
143 int s;
144
145 if ((sa->sa_flags & SA_SIGINFO) == 0)
146 return;
147 /*
148 * If there's no info, don't save it.
149 */
150 if (KSI_EMPTY_P(ksi))
151 return;
152
153 s = splsoftclock();
154 simple_lock(&p->p_sigctx.ps_silock);
155 #ifdef notyet /* XXX: QUEUING */
156 if (ksi->ksi_signo < SIGRTMIN)
157 #endif
158 {
159 CIRCLEQ_FOREACH(kp, &p->p_sigctx.ps_siginfo, ksi_list) {
160 if (kp->ksi_signo == ksi->ksi_signo) {
161 KSI_COPY(ksi, kp);
162 goto out;
163 }
164 }
165 }
166 kp = pool_get(&ksiginfo_pool, PR_NOWAIT);
167 if (kp == NULL) {
168 #ifdef DIAGNOSTIC
169 printf("Out of memory allocating siginfo for pid %d\n",
170 p->p_pid);
171 #endif
172 goto out;
173 }
174 *kp = *ksi;
175 CIRCLEQ_INSERT_TAIL(&p->p_sigctx.ps_siginfo, kp, ksi_list);
176 out:
177 simple_unlock(&p->p_sigctx.ps_silock);
178 splx(s);
179 }
180
181 /*
182 * free all pending ksiginfo on exit
183 */
184 static void
185 ksiginfo_exithook(struct proc *p, void *v)
186 {
187 int s;
188
189 s = splsoftclock();
190 simple_lock(&p->p_sigctx.ps_silock);
191 while (!CIRCLEQ_EMPTY(&p->p_sigctx.ps_siginfo)) {
192 ksiginfo_t *ksi = CIRCLEQ_FIRST(&p->p_sigctx.ps_siginfo);
193 CIRCLEQ_REMOVE(&p->p_sigctx.ps_siginfo, ksi, ksi_list);
194 pool_put(&ksiginfo_pool, ksi);
195 }
196 simple_unlock(&p->p_sigctx.ps_silock);
197 splx(s);
198 }
199
200 /*
201 * Initialize signal-related data structures.
202 */
203 void
204 signal_init(void)
205 {
206 pool_init(&sigacts_pool, sizeof(struct sigacts), 0, 0, 0, "sigapl",
207 &pool_allocator_nointr);
208 pool_init(&siginfo_pool, sizeof(siginfo_t), 0, 0, 0, "siginfo",
209 &pool_allocator_nointr);
210 pool_init(&ksiginfo_pool, sizeof(ksiginfo_t), 0, 0, 0, "ksiginfo",
211 NULL);
212 exithook_establish(ksiginfo_exithook, NULL);
213 exechook_establish(ksiginfo_exithook, NULL);
214
215 sigaddset(&sigtrapmask, SIGSEGV);
216 sigaddset(&sigtrapmask, SIGBUS);
217 sigaddset(&sigtrapmask, SIGILL);
218 sigaddset(&sigtrapmask, SIGFPE);
219 sigaddset(&sigtrapmask, SIGTRAP);
220 }
221
222 /*
223 * Create an initial sigctx structure, using the same signal state
224 * as p. If 'share' is set, share the sigctx_proc part, otherwise just
225 * copy it from parent.
226 */
227 void
228 sigactsinit(struct proc *np, struct proc *pp, int share)
229 {
230 struct sigacts *ps;
231
232 if (share) {
233 np->p_sigacts = pp->p_sigacts;
234 pp->p_sigacts->sa_refcnt++;
235 } else {
236 ps = pool_get(&sigacts_pool, PR_WAITOK);
237 if (pp)
238 memcpy(ps, pp->p_sigacts, sizeof(struct sigacts));
239 else
240 memset(ps, '\0', sizeof(struct sigacts));
241 ps->sa_refcnt = 1;
242 np->p_sigacts = ps;
243 }
244 }
245
246 /*
247 * Make this process not share its sigctx, maintaining all
248 * signal state.
249 */
250 void
251 sigactsunshare(struct proc *p)
252 {
253 struct sigacts *oldps;
254
255 if (p->p_sigacts->sa_refcnt == 1)
256 return;
257
258 oldps = p->p_sigacts;
259 sigactsinit(p, NULL, 0);
260
261 if (--oldps->sa_refcnt == 0)
262 pool_put(&sigacts_pool, oldps);
263 }
264
265 /*
266 * Release a sigctx structure.
267 */
268 void
269 sigactsfree(struct proc *p)
270 {
271 struct sigacts *ps;
272
273 ps = p->p_sigacts;
274 if (--ps->sa_refcnt > 0)
275 return;
276
277 pool_put(&sigacts_pool, ps);
278 }
279
280 int
281 sigaction1(struct proc *p, int signum, const struct sigaction *nsa,
282 struct sigaction *osa, const void *tramp, int vers)
283 {
284 struct sigacts *ps;
285 int prop;
286
287 ps = p->p_sigacts;
288 if (signum <= 0 || signum >= NSIG)
289 return (EINVAL);
290
291 /*
292 * Trampoline ABI version 0 is reserved for the legacy
293 * kernel-provided on-stack trampoline. Conversely, if we are
294 * using a non-0 ABI version, we must have a trampoline. Only
295 * validate the vers if a new sigaction was supplied. Emulations
296 * use legacy kernel trampolines with version 0, alternatively
297 * check for that too.
298 */
299 if ((vers != 0 && tramp == NULL) ||
300 #ifdef SIGTRAMP_VALID
301 (nsa != NULL &&
302 ((vers == 0) ?
303 (p->p_emul->e_sigcode == NULL) :
304 !SIGTRAMP_VALID(vers))) ||
305 #endif
306 (vers == 0 && tramp != NULL))
307 return (EINVAL);
308
309 if (osa)
310 *osa = SIGACTION_PS(ps, signum);
311
312 if (nsa) {
313 if (nsa->sa_flags & ~SA_ALLBITS)
314 return (EINVAL);
315
316 prop = sigprop[signum];
317 if (prop & SA_CANTMASK)
318 return (EINVAL);
319
320 (void) splsched(); /* XXXSMP */
321 SIGACTION_PS(ps, signum) = *nsa;
322 ps->sa_sigdesc[signum].sd_tramp = tramp;
323 ps->sa_sigdesc[signum].sd_vers = vers;
324 sigminusset(&sigcantmask, &SIGACTION_PS(ps, signum).sa_mask);
325 if ((prop & SA_NORESET) != 0)
326 SIGACTION_PS(ps, signum).sa_flags &= ~SA_RESETHAND;
327 if (signum == SIGCHLD) {
328 if (nsa->sa_flags & SA_NOCLDSTOP)
329 p->p_flag |= P_NOCLDSTOP;
330 else
331 p->p_flag &= ~P_NOCLDSTOP;
332 if (nsa->sa_flags & SA_NOCLDWAIT) {
333 /*
334 * Paranoia: since SA_NOCLDWAIT is implemented
335 * by reparenting the dying child to PID 1 (and
336 * trust it to reap the zombie), PID 1 itself
337 * is forbidden to set SA_NOCLDWAIT.
338 */
339 if (p->p_pid == 1)
340 p->p_flag &= ~P_NOCLDWAIT;
341 else
342 p->p_flag |= P_NOCLDWAIT;
343 } else
344 p->p_flag &= ~P_NOCLDWAIT;
345 }
346 if ((nsa->sa_flags & SA_NODEFER) == 0)
347 sigaddset(&SIGACTION_PS(ps, signum).sa_mask, signum);
348 else
349 sigdelset(&SIGACTION_PS(ps, signum).sa_mask, signum);
350 /*
351 * Set bit in p_sigctx.ps_sigignore for signals that are set to
352 * SIG_IGN, and for signals set to SIG_DFL where the default is
353 * to ignore. However, don't put SIGCONT in
354 * p_sigctx.ps_sigignore, as we have to restart the process.
355 */
356 if (nsa->sa_handler == SIG_IGN ||
357 (nsa->sa_handler == SIG_DFL && (prop & SA_IGNORE) != 0)) {
358 /* never to be seen again */
359 sigdelset(&p->p_sigctx.ps_siglist, signum);
360 if (signum != SIGCONT) {
361 /* easier in psignal */
362 sigaddset(&p->p_sigctx.ps_sigignore, signum);
363 }
364 sigdelset(&p->p_sigctx.ps_sigcatch, signum);
365 } else {
366 sigdelset(&p->p_sigctx.ps_sigignore, signum);
367 if (nsa->sa_handler == SIG_DFL)
368 sigdelset(&p->p_sigctx.ps_sigcatch, signum);
369 else
370 sigaddset(&p->p_sigctx.ps_sigcatch, signum);
371 }
372 (void) spl0();
373 }
374
375 return (0);
376 }
377
378 #ifdef COMPAT_16
379 /* ARGSUSED */
380 int
381 compat_16_sys___sigaction14(struct lwp *l, void *v, register_t *retval)
382 {
383 struct compat_16_sys___sigaction14_args /* {
384 syscallarg(int) signum;
385 syscallarg(const struct sigaction *) nsa;
386 syscallarg(struct sigaction *) osa;
387 } */ *uap = v;
388 struct proc *p;
389 struct sigaction nsa, osa;
390 int error;
391
392 if (SCARG(uap, nsa)) {
393 error = copyin(SCARG(uap, nsa), &nsa, sizeof(nsa));
394 if (error)
395 return (error);
396 }
397 p = l->l_proc;
398 error = sigaction1(p, SCARG(uap, signum),
399 SCARG(uap, nsa) ? &nsa : 0, SCARG(uap, osa) ? &osa : 0,
400 NULL, 0);
401 if (error)
402 return (error);
403 if (SCARG(uap, osa)) {
404 error = copyout(&osa, SCARG(uap, osa), sizeof(osa));
405 if (error)
406 return (error);
407 }
408 return (0);
409 }
410 #endif
411
412 /* ARGSUSED */
413 int
414 sys___sigaction_sigtramp(struct lwp *l, void *v, register_t *retval)
415 {
416 struct sys___sigaction_sigtramp_args /* {
417 syscallarg(int) signum;
418 syscallarg(const struct sigaction *) nsa;
419 syscallarg(struct sigaction *) osa;
420 syscallarg(void *) tramp;
421 syscallarg(int) vers;
422 } */ *uap = v;
423 struct proc *p = l->l_proc;
424 struct sigaction nsa, osa;
425 int error;
426
427 if (SCARG(uap, nsa)) {
428 error = copyin(SCARG(uap, nsa), &nsa, sizeof(nsa));
429 if (error)
430 return (error);
431 }
432 error = sigaction1(p, SCARG(uap, signum),
433 SCARG(uap, nsa) ? &nsa : 0, SCARG(uap, osa) ? &osa : 0,
434 SCARG(uap, tramp), SCARG(uap, vers));
435 if (error)
436 return (error);
437 if (SCARG(uap, osa)) {
438 error = copyout(&osa, SCARG(uap, osa), sizeof(osa));
439 if (error)
440 return (error);
441 }
442 return (0);
443 }
444
445 /*
446 * Initialize signal state for process 0;
447 * set to ignore signals that are ignored by default and disable the signal
448 * stack.
449 */
450 void
451 siginit(struct proc *p)
452 {
453 struct sigacts *ps;
454 int signum, prop;
455
456 ps = p->p_sigacts;
457 sigemptyset(&contsigmask);
458 sigemptyset(&stopsigmask);
459 sigemptyset(&sigcantmask);
460 for (signum = 1; signum < NSIG; signum++) {
461 prop = sigprop[signum];
462 if (prop & SA_CONT)
463 sigaddset(&contsigmask, signum);
464 if (prop & SA_STOP)
465 sigaddset(&stopsigmask, signum);
466 if (prop & SA_CANTMASK)
467 sigaddset(&sigcantmask, signum);
468 if (prop & SA_IGNORE && signum != SIGCONT)
469 sigaddset(&p->p_sigctx.ps_sigignore, signum);
470 sigemptyset(&SIGACTION_PS(ps, signum).sa_mask);
471 SIGACTION_PS(ps, signum).sa_flags = SA_RESTART;
472 }
473 sigemptyset(&p->p_sigctx.ps_sigcatch);
474 p->p_sigctx.ps_sigwaited = NULL;
475 p->p_flag &= ~P_NOCLDSTOP;
476
477 /*
478 * Reset stack state to the user stack.
479 */
480 p->p_sigctx.ps_sigstk.ss_flags = SS_DISABLE;
481 p->p_sigctx.ps_sigstk.ss_size = 0;
482 p->p_sigctx.ps_sigstk.ss_sp = 0;
483
484 /* One reference. */
485 ps->sa_refcnt = 1;
486 }
487
488 /*
489 * Reset signals for an exec of the specified process.
490 */
491 void
492 execsigs(struct proc *p)
493 {
494 struct sigacts *ps;
495 int signum, prop;
496
497 sigactsunshare(p);
498
499 ps = p->p_sigacts;
500
501 /*
502 * Reset caught signals. Held signals remain held
503 * through p_sigctx.ps_sigmask (unless they were caught,
504 * and are now ignored by default).
505 */
506 for (signum = 1; signum < NSIG; signum++) {
507 if (sigismember(&p->p_sigctx.ps_sigcatch, signum)) {
508 prop = sigprop[signum];
509 if (prop & SA_IGNORE) {
510 if ((prop & SA_CONT) == 0)
511 sigaddset(&p->p_sigctx.ps_sigignore,
512 signum);
513 sigdelset(&p->p_sigctx.ps_siglist, signum);
514 }
515 SIGACTION_PS(ps, signum).sa_handler = SIG_DFL;
516 }
517 sigemptyset(&SIGACTION_PS(ps, signum).sa_mask);
518 SIGACTION_PS(ps, signum).sa_flags = SA_RESTART;
519 }
520 sigemptyset(&p->p_sigctx.ps_sigcatch);
521 p->p_sigctx.ps_sigwaited = NULL;
522 p->p_flag &= ~P_NOCLDSTOP;
523
524 /*
525 * Reset stack state to the user stack.
526 */
527 p->p_sigctx.ps_sigstk.ss_flags = SS_DISABLE;
528 p->p_sigctx.ps_sigstk.ss_size = 0;
529 p->p_sigctx.ps_sigstk.ss_sp = 0;
530 }
531
532 int
533 sigprocmask1(struct proc *p, int how, const sigset_t *nss, sigset_t *oss)
534 {
535
536 if (oss)
537 *oss = p->p_sigctx.ps_sigmask;
538
539 if (nss) {
540 (void)splsched(); /* XXXSMP */
541 switch (how) {
542 case SIG_BLOCK:
543 sigplusset(nss, &p->p_sigctx.ps_sigmask);
544 break;
545 case SIG_UNBLOCK:
546 sigminusset(nss, &p->p_sigctx.ps_sigmask);
547 CHECKSIGS(p);
548 break;
549 case SIG_SETMASK:
550 p->p_sigctx.ps_sigmask = *nss;
551 CHECKSIGS(p);
552 break;
553 default:
554 (void)spl0(); /* XXXSMP */
555 return (EINVAL);
556 }
557 sigminusset(&sigcantmask, &p->p_sigctx.ps_sigmask);
558 (void)spl0(); /* XXXSMP */
559 }
560
561 return (0);
562 }
563
564 /*
565 * Manipulate signal mask.
566 * Note that we receive new mask, not pointer,
567 * and return old mask as return value;
568 * the library stub does the rest.
569 */
570 int
571 sys___sigprocmask14(struct lwp *l, void *v, register_t *retval)
572 {
573 struct sys___sigprocmask14_args /* {
574 syscallarg(int) how;
575 syscallarg(const sigset_t *) set;
576 syscallarg(sigset_t *) oset;
577 } */ *uap = v;
578 struct proc *p;
579 sigset_t nss, oss;
580 int error;
581
582 if (SCARG(uap, set)) {
583 error = copyin(SCARG(uap, set), &nss, sizeof(nss));
584 if (error)
585 return (error);
586 }
587 p = l->l_proc;
588 error = sigprocmask1(p, SCARG(uap, how),
589 SCARG(uap, set) ? &nss : 0, SCARG(uap, oset) ? &oss : 0);
590 if (error)
591 return (error);
592 if (SCARG(uap, oset)) {
593 error = copyout(&oss, SCARG(uap, oset), sizeof(oss));
594 if (error)
595 return (error);
596 }
597 return (0);
598 }
599
600 void
601 sigpending1(struct proc *p, sigset_t *ss)
602 {
603
604 *ss = p->p_sigctx.ps_siglist;
605 sigminusset(&p->p_sigctx.ps_sigmask, ss);
606 }
607
608 /* ARGSUSED */
609 int
610 sys___sigpending14(struct lwp *l, void *v, register_t *retval)
611 {
612 struct sys___sigpending14_args /* {
613 syscallarg(sigset_t *) set;
614 } */ *uap = v;
615 struct proc *p;
616 sigset_t ss;
617
618 p = l->l_proc;
619 sigpending1(p, &ss);
620 return (copyout(&ss, SCARG(uap, set), sizeof(ss)));
621 }
622
623 int
624 sigsuspend1(struct proc *p, const sigset_t *ss)
625 {
626 struct sigacts *ps;
627
628 ps = p->p_sigacts;
629 if (ss) {
630 /*
631 * When returning from sigpause, we want
632 * the old mask to be restored after the
633 * signal handler has finished. Thus, we
634 * save it here and mark the sigctx structure
635 * to indicate this.
636 */
637 p->p_sigctx.ps_oldmask = p->p_sigctx.ps_sigmask;
638 p->p_sigctx.ps_flags |= SAS_OLDMASK;
639 (void) splsched(); /* XXXSMP */
640 p->p_sigctx.ps_sigmask = *ss;
641 CHECKSIGS(p);
642 sigminusset(&sigcantmask, &p->p_sigctx.ps_sigmask);
643 (void) spl0(); /* XXXSMP */
644 }
645
646 while (tsleep((caddr_t) ps, PPAUSE|PCATCH, "pause", 0) == 0)
647 /* void */;
648
649 /* always return EINTR rather than ERESTART... */
650 return (EINTR);
651 }
652
653 /*
654 * Suspend process until signal, providing mask to be set
655 * in the meantime. Note nonstandard calling convention:
656 * libc stub passes mask, not pointer, to save a copyin.
657 */
658 /* ARGSUSED */
659 int
660 sys___sigsuspend14(struct lwp *l, void *v, register_t *retval)
661 {
662 struct sys___sigsuspend14_args /* {
663 syscallarg(const sigset_t *) set;
664 } */ *uap = v;
665 struct proc *p;
666 sigset_t ss;
667 int error;
668
669 if (SCARG(uap, set)) {
670 error = copyin(SCARG(uap, set), &ss, sizeof(ss));
671 if (error)
672 return (error);
673 }
674
675 p = l->l_proc;
676 return (sigsuspend1(p, SCARG(uap, set) ? &ss : 0));
677 }
678
679 int
680 sigaltstack1(struct proc *p, const struct sigaltstack *nss,
681 struct sigaltstack *oss)
682 {
683
684 if (oss)
685 *oss = p->p_sigctx.ps_sigstk;
686
687 if (nss) {
688 if (nss->ss_flags & ~SS_ALLBITS)
689 return (EINVAL);
690
691 if (nss->ss_flags & SS_DISABLE) {
692 if (p->p_sigctx.ps_sigstk.ss_flags & SS_ONSTACK)
693 return (EINVAL);
694 } else {
695 if (nss->ss_size < MINSIGSTKSZ)
696 return (ENOMEM);
697 }
698 p->p_sigctx.ps_sigstk = *nss;
699 }
700
701 return (0);
702 }
703
704 /* ARGSUSED */
705 int
706 sys___sigaltstack14(struct lwp *l, void *v, register_t *retval)
707 {
708 struct sys___sigaltstack14_args /* {
709 syscallarg(const struct sigaltstack *) nss;
710 syscallarg(struct sigaltstack *) oss;
711 } */ *uap = v;
712 struct proc *p;
713 struct sigaltstack nss, oss;
714 int error;
715
716 if (SCARG(uap, nss)) {
717 error = copyin(SCARG(uap, nss), &nss, sizeof(nss));
718 if (error)
719 return (error);
720 }
721 p = l->l_proc;
722 error = sigaltstack1(p,
723 SCARG(uap, nss) ? &nss : 0, SCARG(uap, oss) ? &oss : 0);
724 if (error)
725 return (error);
726 if (SCARG(uap, oss)) {
727 error = copyout(&oss, SCARG(uap, oss), sizeof(oss));
728 if (error)
729 return (error);
730 }
731 return (0);
732 }
733
734 /* ARGSUSED */
735 int
736 sys_kill(struct lwp *l, void *v, register_t *retval)
737 {
738 struct sys_kill_args /* {
739 syscallarg(int) pid;
740 syscallarg(int) signum;
741 } */ *uap = v;
742 struct proc *cp, *p;
743 struct pcred *pc;
744 ksiginfo_t ksi;
745
746 cp = l->l_proc;
747 pc = cp->p_cred;
748 if ((u_int)SCARG(uap, signum) >= NSIG)
749 return (EINVAL);
750 KSI_INIT(&ksi);
751 ksi.ksi_signo = SCARG(uap, signum);
752 ksi.ksi_code = SI_USER;
753 ksi.ksi_pid = cp->p_pid;
754 ksi.ksi_uid = cp->p_ucred->cr_uid;
755 if (SCARG(uap, pid) > 0) {
756 /* kill single process */
757 if ((p = pfind(SCARG(uap, pid))) == NULL)
758 return (ESRCH);
759 if (!CANSIGNAL(cp, pc, p, SCARG(uap, signum)))
760 return (EPERM);
761 if (SCARG(uap, signum))
762 kpsignal2(p, &ksi, 1);
763 return (0);
764 }
765 switch (SCARG(uap, pid)) {
766 case -1: /* broadcast signal */
767 return (killpg1(cp, &ksi, 0, 1));
768 case 0: /* signal own process group */
769 return (killpg1(cp, &ksi, 0, 0));
770 default: /* negative explicit process group */
771 return (killpg1(cp, &ksi, -SCARG(uap, pid), 0));
772 }
773 /* NOTREACHED */
774 }
775
776 /*
777 * Common code for kill process group/broadcast kill.
778 * cp is calling process.
779 */
780 int
781 killpg1(struct proc *cp, ksiginfo_t *ksi, int pgid, int all)
782 {
783 struct proc *p;
784 struct pcred *pc;
785 struct pgrp *pgrp;
786 int nfound;
787 int signum = ksi->ksi_signo;
788
789 pc = cp->p_cred;
790 nfound = 0;
791 if (all) {
792 /*
793 * broadcast
794 */
795 proclist_lock_read();
796 LIST_FOREACH(p, &allproc, p_list) {
797 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
798 p == cp || !CANSIGNAL(cp, pc, p, signum))
799 continue;
800 nfound++;
801 if (signum)
802 kpsignal2(p, ksi, 1);
803 }
804 proclist_unlock_read();
805 } else {
806 if (pgid == 0)
807 /*
808 * zero pgid means send to my process group.
809 */
810 pgrp = cp->p_pgrp;
811 else {
812 pgrp = pgfind(pgid);
813 if (pgrp == NULL)
814 return (ESRCH);
815 }
816 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
817 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
818 !CANSIGNAL(cp, pc, p, signum))
819 continue;
820 nfound++;
821 if (signum && P_ZOMBIE(p) == 0)
822 kpsignal2(p, ksi, 1);
823 }
824 }
825 return (nfound ? 0 : ESRCH);
826 }
827
828 /*
829 * Send a signal to a process group.
830 */
831 void
832 gsignal(int pgid, int signum)
833 {
834 ksiginfo_t ksi;
835 KSI_INIT_EMPTY(&ksi);
836 ksi.ksi_signo = signum;
837 kgsignal(pgid, &ksi, NULL);
838 }
839
840 void
841 kgsignal(int pgid, ksiginfo_t *ksi, void *data)
842 {
843 struct pgrp *pgrp;
844
845 if (pgid && (pgrp = pgfind(pgid)))
846 kpgsignal(pgrp, ksi, data, 0);
847 }
848
849 /*
850 * Send a signal to a process group. If checktty is 1,
851 * limit to members which have a controlling terminal.
852 */
853 void
854 pgsignal(struct pgrp *pgrp, int sig, int checkctty)
855 {
856 ksiginfo_t ksi;
857 KSI_INIT_EMPTY(&ksi);
858 ksi.ksi_signo = sig;
859 kpgsignal(pgrp, &ksi, NULL, checkctty);
860 }
861
862 void
863 kpgsignal(struct pgrp *pgrp, ksiginfo_t *ksi, void *data, int checkctty)
864 {
865 struct proc *p;
866
867 if (pgrp)
868 LIST_FOREACH(p, &pgrp->pg_members, p_pglist)
869 if (checkctty == 0 || p->p_flag & P_CONTROLT)
870 kpsignal(p, ksi, data);
871 }
872
873 /*
874 * Send a signal caused by a trap to the current process.
875 * If it will be caught immediately, deliver it with correct code.
876 * Otherwise, post it normally.
877 */
878 void
879 trapsignal(struct lwp *l, const ksiginfo_t *ksi)
880 {
881 struct proc *p;
882 struct sigacts *ps;
883 int signum = ksi->ksi_signo;
884
885 KASSERT(KSI_TRAP_P(ksi));
886
887 p = l->l_proc;
888 ps = p->p_sigacts;
889 if ((p->p_flag & P_TRACED) == 0 &&
890 sigismember(&p->p_sigctx.ps_sigcatch, signum) &&
891 !sigismember(&p->p_sigctx.ps_sigmask, signum)) {
892 p->p_stats->p_ru.ru_nsignals++;
893 #ifdef KTRACE
894 if (KTRPOINT(p, KTR_PSIG))
895 ktrpsig(p, signum, SIGACTION_PS(ps, signum).sa_handler,
896 &p->p_sigctx.ps_sigmask, ksi);
897 #endif
898 kpsendsig(l, ksi, &p->p_sigctx.ps_sigmask);
899 (void) splsched(); /* XXXSMP */
900 sigplusset(&SIGACTION_PS(ps, signum).sa_mask,
901 &p->p_sigctx.ps_sigmask);
902 if (SIGACTION_PS(ps, signum).sa_flags & SA_RESETHAND) {
903 sigdelset(&p->p_sigctx.ps_sigcatch, signum);
904 if (signum != SIGCONT && sigprop[signum] & SA_IGNORE)
905 sigaddset(&p->p_sigctx.ps_sigignore, signum);
906 SIGACTION_PS(ps, signum).sa_handler = SIG_DFL;
907 }
908 (void) spl0(); /* XXXSMP */
909 } else {
910 p->p_sigctx.ps_lwp = l->l_lid;
911 /* XXX for core dump/debugger */
912 p->p_sigctx.ps_signo = ksi->ksi_signo;
913 p->p_sigctx.ps_code = ksi->ksi_trap;
914 kpsignal2(p, ksi, 1);
915 }
916 }
917
918 /*
919 * Fill in signal information and signal the parent for a child status change.
920 */
921 static void
922 child_psignal(struct proc *p, int dolock)
923 {
924 ksiginfo_t ksi;
925
926 KSI_INIT(&ksi);
927 ksi.ksi_signo = SIGCHLD;
928 ksi.ksi_code = p->p_xstat == SIGCONT ? CLD_CONTINUED : CLD_STOPPED;
929 ksi.ksi_pid = p->p_pid;
930 ksi.ksi_uid = p->p_ucred->cr_uid;
931 ksi.ksi_status = p->p_xstat;
932 ksi.ksi_utime = p->p_stats->p_ru.ru_utime.tv_sec;
933 ksi.ksi_stime = p->p_stats->p_ru.ru_stime.tv_sec;
934 kpsignal2(p->p_pptr, &ksi, dolock);
935 }
936
937 /*
938 * Send the signal to the process. If the signal has an action, the action
939 * is usually performed by the target process rather than the caller; we add
940 * the signal to the set of pending signals for the process.
941 *
942 * Exceptions:
943 * o When a stop signal is sent to a sleeping process that takes the
944 * default action, the process is stopped without awakening it.
945 * o SIGCONT restarts stopped processes (or puts them back to sleep)
946 * regardless of the signal action (eg, blocked or ignored).
947 *
948 * Other ignored signals are discarded immediately.
949 *
950 * XXXSMP: Invoked as psignal() or sched_psignal().
951 */
952 void
953 psignal1(struct proc *p, int signum, int dolock)
954 {
955 ksiginfo_t ksi;
956
957 KSI_INIT_EMPTY(&ksi);
958 ksi.ksi_signo = signum;
959 kpsignal2(p, &ksi, dolock);
960 }
961
962 void
963 kpsignal1(struct proc *p, ksiginfo_t *ksi, void *data, int dolock)
964 {
965
966 if ((p->p_flag & P_WEXIT) == 0 && data) {
967 size_t fd;
968 struct filedesc *fdp = p->p_fd;
969
970 ksi->ksi_fd = -1;
971 for (fd = 0; fd < fdp->fd_nfiles; fd++) {
972 struct file *fp = fdp->fd_ofiles[fd];
973 /* XXX: lock? */
974 if (fp && fp->f_data == data) {
975 ksi->ksi_fd = fd;
976 break;
977 }
978 }
979 }
980 kpsignal2(p, ksi, dolock);
981 }
982
983 static void
984 kpsignal2(struct proc *p, const ksiginfo_t *ksi, int dolock)
985 {
986 struct lwp *l, *suspended = NULL;
987 struct sadata_vp *vp;
988 int s = 0, prop, allsusp;
989 sig_t action;
990 int signum = ksi->ksi_signo;
991
992 #ifdef DIAGNOSTIC
993 if (signum <= 0 || signum >= NSIG)
994 panic("psignal signal number %d", signum);
995
996 /* XXXSMP: works, but icky */
997 if (dolock)
998 SCHED_ASSERT_UNLOCKED();
999 else
1000 SCHED_ASSERT_LOCKED();
1001 #endif
1002
1003 /*
1004 * Notify any interested parties in the signal.
1005 */
1006 KNOTE(&p->p_klist, NOTE_SIGNAL | signum);
1007
1008 prop = sigprop[signum];
1009
1010 /*
1011 * If proc is traced, always give parent a chance.
1012 */
1013 action = SIG_DFL;
1014 if ((p->p_flag & P_TRACED) == 0) {
1015 if (KSI_TRAP_P(ksi)) {
1016 /*
1017 * If the signal was the result of a trap, only catch
1018 * the signal if it isn't masked and there is a
1019 * non-default non-ignore handler installed for it.
1020 * Otherwise take the default action.
1021 */
1022 if (!sigismember(&p->p_sigctx.ps_sigmask, signum) &&
1023 sigismember(&p->p_sigctx.ps_sigcatch, signum))
1024 action = SIG_CATCH;
1025 /*
1026 * If we are to take the default action, reset the
1027 * signal back to its defaults.
1028 */
1029 if (action == SIG_DFL) {
1030 sigdelset(&p->p_sigctx.ps_sigignore, signum);
1031 sigdelset(&p->p_sigctx.ps_sigcatch, signum);
1032 sigdelset(&p->p_sigctx.ps_sigmask, signum);
1033 SIGACTION(p, signum).sa_handler = SIG_DFL;
1034 }
1035 } else {
1036 /*
1037 * If the signal is being ignored,
1038 * then we forget about it immediately.
1039 * (Note: we don't set SIGCONT in p_sigctx.ps_sigignore,
1040 * and if it is set to SIG_IGN,
1041 * action will be SIG_DFL here.)
1042 */
1043 if (sigismember(&p->p_sigctx.ps_sigignore, signum))
1044 return;
1045 if (sigismember(&p->p_sigctx.ps_sigmask, signum))
1046 action = SIG_HOLD;
1047 else if (sigismember(&p->p_sigctx.ps_sigcatch, signum))
1048 action = SIG_CATCH;
1049 }
1050 if (action == SIG_DFL) {
1051 if (prop & SA_KILL && p->p_nice > NZERO)
1052 p->p_nice = NZERO;
1053
1054 /*
1055 * If sending a tty stop signal to a member of an
1056 * orphaned process group, discard the signal here if
1057 * the action is default; don't stop the process below
1058 * if sleeping, and don't clear any pending SIGCONT.
1059 */
1060 if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0)
1061 return;
1062 }
1063 }
1064
1065 if (prop & SA_CONT)
1066 sigminusset(&stopsigmask, &p->p_sigctx.ps_siglist);
1067
1068 if (prop & SA_STOP)
1069 sigminusset(&contsigmask, &p->p_sigctx.ps_siglist);
1070
1071 /*
1072 * If the signal doesn't have SA_CANTMASK (no override for SIGKILL,
1073 * please!), check if anything waits on it. If yes, save the
1074 * info into provided ps_sigwaited, and wake-up the waiter.
1075 * The signal won't be processed further here.
1076 */
1077 if ((prop & SA_CANTMASK) == 0
1078 && p->p_sigctx.ps_sigwaited
1079 && sigismember(p->p_sigctx.ps_sigwait, signum)
1080 && p->p_stat != SSTOP) {
1081 p->p_sigctx.ps_sigwaited->ksi_info = ksi->ksi_info;
1082 p->p_sigctx.ps_sigwaited = NULL;
1083 if (dolock)
1084 wakeup_one(&p->p_sigctx.ps_sigwait);
1085 else
1086 sched_wakeup(&p->p_sigctx.ps_sigwait);
1087 return;
1088 }
1089
1090 sigaddset(&p->p_sigctx.ps_siglist, signum);
1091
1092 /* CHECKSIGS() is "inlined" here. */
1093 p->p_sigctx.ps_sigcheck = 1;
1094
1095 /*
1096 * Defer further processing for signals which are held,
1097 * except that stopped processes must be continued by SIGCONT.
1098 */
1099 if (action == SIG_HOLD &&
1100 ((prop & SA_CONT) == 0 || p->p_stat != SSTOP)) {
1101 ksiginfo_put(p, ksi);
1102 return;
1103 }
1104 /* XXXSMP: works, but icky */
1105 if (dolock)
1106 SCHED_LOCK(s);
1107
1108 if (p->p_flag & P_SA) {
1109 allsusp = 0;
1110 l = NULL;
1111 if (p->p_stat == SACTIVE) {
1112 SLIST_FOREACH(vp, &p->p_sa->sa_vps, savp_next) {
1113 l = vp->savp_lwp;
1114 KDASSERT(l != NULL);
1115 if (l->l_flag & L_SA_IDLE) {
1116 /* wakeup idle LWP */
1117 goto found;
1118 /*NOTREACHED*/
1119 } else if (l->l_flag & L_SA_YIELD) {
1120 /* idle LWP is already waking up */
1121 goto out;
1122 /*NOTREACHED*/
1123 }
1124 }
1125 SLIST_FOREACH(vp, &p->p_sa->sa_vps, savp_next) {
1126 l = vp->savp_lwp;
1127 if (l->l_stat == LSRUN ||
1128 l->l_stat == LSONPROC) {
1129 signotify(p);
1130 goto out;
1131 /*NOTREACHED*/
1132 }
1133 if (l->l_stat == LSSLEEP &&
1134 l->l_flag & L_SINTR) {
1135 /* ok to signal vp lwp */
1136 } else
1137 l = NULL;
1138 }
1139 if (l == NULL)
1140 allsusp = 1;
1141 } else if (p->p_stat == SSTOP) {
1142 SLIST_FOREACH(vp, &p->p_sa->sa_vps, savp_next) {
1143 l = vp->savp_lwp;
1144 if (l->l_stat == LSSLEEP && (l->l_flag & L_SINTR) != 0)
1145 break;
1146 l = NULL;
1147 }
1148 }
1149 } else if (p->p_nrlwps > 0 && (p->p_stat != SSTOP)) {
1150 /*
1151 * At least one LWP is running or on a run queue.
1152 * The signal will be noticed when one of them returns
1153 * to userspace.
1154 */
1155 signotify(p);
1156 /*
1157 * The signal will be noticed very soon.
1158 */
1159 goto out;
1160 /*NOTREACHED*/
1161 } else {
1162 /*
1163 * Find out if any of the sleeps are interruptable,
1164 * and if all the live LWPs remaining are suspended.
1165 */
1166 allsusp = 1;
1167 LIST_FOREACH(l, &p->p_lwps, l_sibling) {
1168 if (l->l_stat == LSSLEEP &&
1169 l->l_flag & L_SINTR)
1170 break;
1171 if (l->l_stat == LSSUSPENDED)
1172 suspended = l;
1173 else if ((l->l_stat != LSZOMB) &&
1174 (l->l_stat != LSDEAD))
1175 allsusp = 0;
1176 }
1177 }
1178
1179 found:
1180 switch (p->p_stat) {
1181 case SACTIVE:
1182
1183 if (l != NULL && (p->p_flag & P_TRACED))
1184 goto run;
1185
1186 /*
1187 * If SIGCONT is default (or ignored) and process is
1188 * asleep, we are finished; the process should not
1189 * be awakened.
1190 */
1191 if ((prop & SA_CONT) && action == SIG_DFL) {
1192 sigdelset(&p->p_sigctx.ps_siglist, signum);
1193 goto done;
1194 }
1195
1196 /*
1197 * When a sleeping process receives a stop
1198 * signal, process immediately if possible.
1199 */
1200 if ((prop & SA_STOP) && action == SIG_DFL) {
1201 /*
1202 * If a child holding parent blocked,
1203 * stopping could cause deadlock.
1204 */
1205 if (p->p_flag & P_PPWAIT) {
1206 goto out;
1207 }
1208 sigdelset(&p->p_sigctx.ps_siglist, signum);
1209 p->p_xstat = signum;
1210 if ((p->p_pptr->p_flag & P_NOCLDSTOP) == 0) {
1211 /*
1212 * XXXSMP: recursive call; don't lock
1213 * the second time around.
1214 */
1215 child_psignal(p, 0);
1216 }
1217 proc_stop(p, 1); /* XXXSMP: recurse? */
1218 goto done;
1219 }
1220
1221 if (l == NULL) {
1222 /*
1223 * Special case: SIGKILL of a process
1224 * which is entirely composed of
1225 * suspended LWPs should succeed. We
1226 * make this happen by unsuspending one of
1227 * them.
1228 */
1229 if (allsusp && (signum == SIGKILL)) {
1230 if (p->p_flag & P_SA) {
1231 /*
1232 * get a suspended lwp from
1233 * the cache to send KILL
1234 * signal
1235 * XXXcl add signal checks at resume points
1236 */
1237 suspended = sa_getcachelwp
1238 (SLIST_FIRST(&p->p_sa->sa_vps));
1239 }
1240 lwp_continue(suspended);
1241 }
1242 goto done;
1243 }
1244 /*
1245 * All other (caught or default) signals
1246 * cause the process to run.
1247 */
1248 goto runfast;
1249 /*NOTREACHED*/
1250 case SSTOP:
1251 /* Process is stopped */
1252 /*
1253 * If traced process is already stopped,
1254 * then no further action is necessary.
1255 */
1256 if (p->p_flag & P_TRACED)
1257 goto done;
1258
1259 /*
1260 * Kill signal always sets processes running,
1261 * if possible.
1262 */
1263 if (signum == SIGKILL) {
1264 l = proc_unstop(p);
1265 if (l)
1266 goto runfast;
1267 goto done;
1268 }
1269
1270 if (prop & SA_CONT) {
1271 /*
1272 * If SIGCONT is default (or ignored),
1273 * we continue the process but don't
1274 * leave the signal in ps_siglist, as
1275 * it has no further action. If
1276 * SIGCONT is held, we continue the
1277 * process and leave the signal in
1278 * ps_siglist. If the process catches
1279 * SIGCONT, let it handle the signal
1280 * itself. If it isn't waiting on an
1281 * event, then it goes back to run
1282 * state. Otherwise, process goes
1283 * back to sleep state.
1284 */
1285 if (action == SIG_DFL)
1286 sigdelset(&p->p_sigctx.ps_siglist,
1287 signum);
1288 l = proc_unstop(p);
1289 if (l && (action == SIG_CATCH))
1290 goto runfast;
1291 goto out;
1292 }
1293
1294 if (prop & SA_STOP) {
1295 /*
1296 * Already stopped, don't need to stop again.
1297 * (If we did the shell could get confused.)
1298 */
1299 sigdelset(&p->p_sigctx.ps_siglist, signum);
1300 goto done;
1301 }
1302
1303 /*
1304 * If a lwp is sleeping interruptibly, then
1305 * wake it up; it will run until the kernel
1306 * boundary, where it will stop in issignal(),
1307 * since p->p_stat is still SSTOP. When the
1308 * process is continued, it will be made
1309 * runnable and can look at the signal.
1310 */
1311 if (l)
1312 goto run;
1313 goto out;
1314 case SIDL:
1315 /* Process is being created by fork */
1316 /* XXX: We are not ready to receive signals yet */
1317 goto done;
1318 default:
1319 /* Else what? */
1320 panic("psignal: Invalid process state %d.", p->p_stat);
1321 }
1322 /*NOTREACHED*/
1323
1324 runfast:
1325 if (action == SIG_CATCH) {
1326 ksiginfo_put(p, ksi);
1327 action = SIG_HOLD;
1328 }
1329 /*
1330 * Raise priority to at least PUSER.
1331 */
1332 if (l->l_priority > PUSER)
1333 l->l_priority = PUSER;
1334 run:
1335 if (action == SIG_CATCH) {
1336 ksiginfo_put(p, ksi);
1337 action = SIG_HOLD;
1338 }
1339
1340 setrunnable(l); /* XXXSMP: recurse? */
1341 out:
1342 if (action == SIG_CATCH)
1343 ksiginfo_put(p, ksi);
1344 done:
1345 /* XXXSMP: works, but icky */
1346 if (dolock)
1347 SCHED_UNLOCK(s);
1348 }
1349
1350 void
1351 kpsendsig(struct lwp *l, const ksiginfo_t *ksi, const sigset_t *mask)
1352 {
1353 struct proc *p = l->l_proc;
1354 struct lwp *le, *li;
1355 siginfo_t *si;
1356 int f;
1357
1358 if (p->p_flag & P_SA) {
1359
1360 /* XXXUPSXXX What if not on sa_vp ? */
1361
1362 f = l->l_flag & L_SA;
1363 l->l_flag &= ~L_SA;
1364 si = pool_get(&siginfo_pool, PR_WAITOK);
1365 si->_info = ksi->ksi_info;
1366 le = li = NULL;
1367 if (KSI_TRAP_P(ksi))
1368 le = l;
1369 else
1370 li = l;
1371
1372 sa_upcall(l, SA_UPCALL_SIGNAL | SA_UPCALL_DEFER, le, li,
1373 sizeof(siginfo_t), si);
1374 l->l_flag |= f;
1375 return;
1376 }
1377
1378 (*p->p_emul->e_sendsig)(ksi, mask);
1379 }
1380
1381 static __inline int firstsig(const sigset_t *);
1382
1383 static __inline int
1384 firstsig(const sigset_t *ss)
1385 {
1386 int sig;
1387
1388 sig = ffs(ss->__bits[0]);
1389 if (sig != 0)
1390 return (sig);
1391 #if NSIG > 33
1392 sig = ffs(ss->__bits[1]);
1393 if (sig != 0)
1394 return (sig + 32);
1395 #endif
1396 #if NSIG > 65
1397 sig = ffs(ss->__bits[2]);
1398 if (sig != 0)
1399 return (sig + 64);
1400 #endif
1401 #if NSIG > 97
1402 sig = ffs(ss->__bits[3]);
1403 if (sig != 0)
1404 return (sig + 96);
1405 #endif
1406 return (0);
1407 }
1408
1409 /*
1410 * If the current process has received a signal (should be caught or cause
1411 * termination, should interrupt current syscall), return the signal number.
1412 * Stop signals with default action are processed immediately, then cleared;
1413 * they aren't returned. This is checked after each entry to the system for
1414 * a syscall or trap (though this can usually be done without calling issignal
1415 * by checking the pending signal masks in the CURSIG macro.) The normal call
1416 * sequence is
1417 *
1418 * while (signum = CURSIG(curlwp))
1419 * postsig(signum);
1420 */
1421 int
1422 issignal(struct lwp *l)
1423 {
1424 struct proc *p = l->l_proc;
1425 int s = 0, signum, prop;
1426 int dolock = (l->l_flag & L_SINTR) == 0, locked = !dolock;
1427 sigset_t ss;
1428
1429 /* Bail out if we do not own the virtual processor */
1430 if (l->l_flag & L_SA && l->l_savp->savp_lwp != l)
1431 return 0;
1432
1433 if (p->p_stat == SSTOP) {
1434 /*
1435 * The process is stopped/stopping. Stop ourselves now that
1436 * we're on the kernel/userspace boundary.
1437 */
1438 if (dolock)
1439 SCHED_LOCK(s);
1440 l->l_stat = LSSTOP;
1441 p->p_nrlwps--;
1442 if (p->p_flag & P_TRACED)
1443 goto sigtraceswitch;
1444 else
1445 goto sigswitch;
1446 }
1447 for (;;) {
1448 sigpending1(p, &ss);
1449 if (p->p_flag & P_PPWAIT)
1450 sigminusset(&stopsigmask, &ss);
1451 signum = firstsig(&ss);
1452 if (signum == 0) { /* no signal to send */
1453 p->p_sigctx.ps_sigcheck = 0;
1454 if (locked && dolock)
1455 SCHED_LOCK(s);
1456 return (0);
1457 }
1458 /* take the signal! */
1459 sigdelset(&p->p_sigctx.ps_siglist, signum);
1460
1461 /*
1462 * We should see pending but ignored signals
1463 * only if P_TRACED was on when they were posted.
1464 */
1465 if (sigismember(&p->p_sigctx.ps_sigignore, signum) &&
1466 (p->p_flag & P_TRACED) == 0)
1467 continue;
1468
1469 if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) {
1470 /*
1471 * If traced, always stop, and stay
1472 * stopped until released by the debugger.
1473 */
1474 p->p_xstat = signum;
1475
1476 /* Emulation-specific handling of signal trace */
1477 if ((p->p_emul->e_tracesig != NULL) &&
1478 ((*p->p_emul->e_tracesig)(p, signum) != 0))
1479 goto childresumed;
1480
1481 if ((p->p_flag & P_FSTRACE) == 0)
1482 child_psignal(p, dolock);
1483 if (dolock)
1484 SCHED_LOCK(s);
1485 proc_stop(p, 1);
1486 sigtraceswitch:
1487 mi_switch(l, NULL);
1488 SCHED_ASSERT_UNLOCKED();
1489 if (dolock)
1490 splx(s);
1491 else
1492 dolock = 1;
1493
1494 childresumed:
1495 /*
1496 * If we are no longer being traced, or the parent
1497 * didn't give us a signal, look for more signals.
1498 */
1499 if ((p->p_flag & P_TRACED) == 0 || p->p_xstat == 0)
1500 continue;
1501
1502 /*
1503 * If the new signal is being masked, look for other
1504 * signals.
1505 */
1506 signum = p->p_xstat;
1507 p->p_xstat = 0;
1508 /*
1509 * `p->p_sigctx.ps_siglist |= mask' is done
1510 * in setrunnable().
1511 */
1512 if (sigismember(&p->p_sigctx.ps_sigmask, signum))
1513 continue;
1514 /* take the signal! */
1515 sigdelset(&p->p_sigctx.ps_siglist, signum);
1516 }
1517
1518 prop = sigprop[signum];
1519
1520 /*
1521 * Decide whether the signal should be returned.
1522 * Return the signal's number, or fall through
1523 * to clear it from the pending mask.
1524 */
1525 switch ((long)SIGACTION(p, signum).sa_handler) {
1526
1527 case (long)SIG_DFL:
1528 /*
1529 * Don't take default actions on system processes.
1530 */
1531 if (p->p_pid <= 1) {
1532 #ifdef DIAGNOSTIC
1533 /*
1534 * Are you sure you want to ignore SIGSEGV
1535 * in init? XXX
1536 */
1537 printf("Process (pid %d) got signal %d\n",
1538 p->p_pid, signum);
1539 #endif
1540 break; /* == ignore */
1541 }
1542 /*
1543 * If there is a pending stop signal to process
1544 * with default action, stop here,
1545 * then clear the signal. However,
1546 * if process is member of an orphaned
1547 * process group, ignore tty stop signals.
1548 */
1549 if (prop & SA_STOP) {
1550 if (p->p_flag & P_TRACED ||
1551 (p->p_pgrp->pg_jobc == 0 &&
1552 prop & SA_TTYSTOP))
1553 break; /* == ignore */
1554 p->p_xstat = signum;
1555 if ((p->p_pptr->p_flag & P_NOCLDSTOP) == 0)
1556 child_psignal(p, dolock);
1557 if (dolock)
1558 SCHED_LOCK(s);
1559 proc_stop(p, 1);
1560 sigswitch:
1561 mi_switch(l, NULL);
1562 SCHED_ASSERT_UNLOCKED();
1563 if (dolock)
1564 splx(s);
1565 else
1566 dolock = 1;
1567 break;
1568 } else if (prop & SA_IGNORE) {
1569 /*
1570 * Except for SIGCONT, shouldn't get here.
1571 * Default action is to ignore; drop it.
1572 */
1573 break; /* == ignore */
1574 } else
1575 goto keep;
1576 /*NOTREACHED*/
1577
1578 case (long)SIG_IGN:
1579 /*
1580 * Masking above should prevent us ever trying
1581 * to take action on an ignored signal other
1582 * than SIGCONT, unless process is traced.
1583 */
1584 #ifdef DEBUG_ISSIGNAL
1585 if ((prop & SA_CONT) == 0 &&
1586 (p->p_flag & P_TRACED) == 0)
1587 printf("issignal\n");
1588 #endif
1589 break; /* == ignore */
1590
1591 default:
1592 /*
1593 * This signal has an action, let
1594 * postsig() process it.
1595 */
1596 goto keep;
1597 }
1598 }
1599 /* NOTREACHED */
1600
1601 keep:
1602 /* leave the signal for later */
1603 sigaddset(&p->p_sigctx.ps_siglist, signum);
1604 CHECKSIGS(p);
1605 if (locked && dolock)
1606 SCHED_LOCK(s);
1607 return (signum);
1608 }
1609
1610 /*
1611 * Put the argument process into the stopped state and notify the parent
1612 * via wakeup. Signals are handled elsewhere. The process must not be
1613 * on the run queue.
1614 */
1615 void
1616 proc_stop(struct proc *p, int wakeup)
1617 {
1618 struct lwp *l;
1619 struct proc *parent;
1620 struct sadata_vp *vp;
1621
1622 SCHED_ASSERT_LOCKED();
1623
1624 /* XXX lock process LWP state */
1625 p->p_flag &= ~P_WAITED;
1626 p->p_stat = SSTOP;
1627 parent = p->p_pptr;
1628 parent->p_nstopchild++;
1629
1630 if (p->p_flag & P_SA) {
1631 /*
1632 * Only (try to) put the LWP on the VP in stopped
1633 * state.
1634 * All other LWPs will suspend in sa_setwoken()
1635 * because the VP-LWP in stopped state cannot be
1636 * repossessed.
1637 */
1638 SLIST_FOREACH(vp, &p->p_sa->sa_vps, savp_next) {
1639 l = vp->savp_lwp;
1640 if (l->l_stat == LSONPROC && l->l_cpu == curcpu()) {
1641 l->l_stat = LSSTOP;
1642 p->p_nrlwps--;
1643 } else if (l->l_stat == LSRUN) {
1644 /* Remove LWP from the run queue */
1645 remrunqueue(l);
1646 l->l_stat = LSSTOP;
1647 p->p_nrlwps--;
1648 } else if (l->l_stat == LSSLEEP &&
1649 l->l_flag & L_SA_IDLE) {
1650 l->l_flag &= ~L_SA_IDLE;
1651 l->l_stat = LSSTOP;
1652 }
1653 }
1654 goto out;
1655 }
1656
1657 /*
1658 * Put as many LWP's as possible in stopped state.
1659 * Sleeping ones will notice the stopped state as they try to
1660 * return to userspace.
1661 */
1662
1663 LIST_FOREACH(l, &p->p_lwps, l_sibling) {
1664 if (l->l_stat == LSONPROC) {
1665 /* XXX SMP this assumes that a LWP that is LSONPROC
1666 * is curlwp and hence is about to be mi_switched
1667 * away; the only callers of proc_stop() are:
1668 * - psignal
1669 * - issignal()
1670 * For the former, proc_stop() is only called when
1671 * no processes are running, so we don't worry.
1672 * For the latter, proc_stop() is called right
1673 * before mi_switch().
1674 */
1675 l->l_stat = LSSTOP;
1676 p->p_nrlwps--;
1677 } else if (l->l_stat == LSRUN) {
1678 /* Remove LWP from the run queue */
1679 remrunqueue(l);
1680 l->l_stat = LSSTOP;
1681 p->p_nrlwps--;
1682 } else if ((l->l_stat == LSSLEEP) ||
1683 (l->l_stat == LSSUSPENDED) ||
1684 (l->l_stat == LSZOMB) ||
1685 (l->l_stat == LSDEAD)) {
1686 /*
1687 * Don't do anything; let sleeping LWPs
1688 * discover the stopped state of the process
1689 * on their way out of the kernel; otherwise,
1690 * things like NFS threads that sleep with
1691 * locks will block the rest of the system
1692 * from getting any work done.
1693 *
1694 * Suspended/dead/zombie LWPs aren't going
1695 * anywhere, so we don't need to touch them.
1696 */
1697 }
1698 #ifdef DIAGNOSTIC
1699 else {
1700 panic("proc_stop: process %d lwp %d "
1701 "in unstoppable state %d.\n",
1702 p->p_pid, l->l_lid, l->l_stat);
1703 }
1704 #endif
1705 }
1706
1707 out:
1708 /* XXX unlock process LWP state */
1709
1710 if (wakeup)
1711 sched_wakeup((caddr_t)p->p_pptr);
1712 }
1713
1714 /*
1715 * Given a process in state SSTOP, set the state back to SACTIVE and
1716 * move LSSTOP'd LWPs to LSSLEEP or make them runnable.
1717 *
1718 * If no LWPs ended up runnable (and therefore able to take a signal),
1719 * return a LWP that is sleeping interruptably. The caller can wake
1720 * that LWP up to take a signal.
1721 */
1722 struct lwp *
1723 proc_unstop(struct proc *p)
1724 {
1725 struct lwp *l, *lr = NULL;
1726 struct sadata_vp *vp;
1727 int cantake = 0;
1728
1729 SCHED_ASSERT_LOCKED();
1730
1731 /*
1732 * Our caller wants to be informed if there are only sleeping
1733 * and interruptable LWPs left after we have run so that it
1734 * can invoke setrunnable() if required - return one of the
1735 * interruptable LWPs if this is the case.
1736 */
1737
1738 if (!(p->p_flag & P_WAITED))
1739 p->p_pptr->p_nstopchild--;
1740 p->p_stat = SACTIVE;
1741 LIST_FOREACH(l, &p->p_lwps, l_sibling) {
1742 if (l->l_stat == LSRUN) {
1743 lr = NULL;
1744 cantake = 1;
1745 }
1746 if (l->l_stat != LSSTOP)
1747 continue;
1748
1749 if (l->l_wchan != NULL) {
1750 l->l_stat = LSSLEEP;
1751 if ((cantake == 0) && (l->l_flag & L_SINTR)) {
1752 lr = l;
1753 cantake = 1;
1754 }
1755 } else {
1756 setrunnable(l);
1757 lr = NULL;
1758 cantake = 1;
1759 }
1760 }
1761 if (p->p_flag & P_SA) {
1762 /* Only consider returning the LWP on the VP. */
1763 SLIST_FOREACH(vp, &p->p_sa->sa_vps, savp_next) {
1764 lr = vp->savp_lwp;
1765 if (lr->l_stat == LSSLEEP) {
1766 if (lr->l_flag & L_SA_YIELD) {
1767 setrunnable(lr);
1768 break;
1769 } else if (lr->l_flag & L_SINTR)
1770 return lr;
1771 }
1772 }
1773 return NULL;
1774 }
1775 return lr;
1776 }
1777
1778 /*
1779 * Take the action for the specified signal
1780 * from the current set of pending signals.
1781 */
1782 void
1783 postsig(int signum)
1784 {
1785 struct lwp *l;
1786 struct proc *p;
1787 struct sigacts *ps;
1788 sig_t action;
1789 sigset_t *returnmask;
1790
1791 l = curlwp;
1792 p = l->l_proc;
1793 ps = p->p_sigacts;
1794 #ifdef DIAGNOSTIC
1795 if (signum == 0)
1796 panic("postsig");
1797 #endif
1798
1799 KERNEL_PROC_LOCK(l);
1800
1801 #ifdef MULTIPROCESSOR
1802 /*
1803 * On MP, issignal() can return the same signal to multiple
1804 * LWPs. The LWPs will block above waiting for the kernel
1805 * lock and the first LWP which gets through will then remove
1806 * the signal from ps_siglist. All other LWPs exit here.
1807 */
1808 if (!sigismember(&p->p_sigctx.ps_siglist, signum)) {
1809 KERNEL_PROC_UNLOCK(l);
1810 return;
1811 }
1812 #endif
1813 sigdelset(&p->p_sigctx.ps_siglist, signum);
1814 action = SIGACTION_PS(ps, signum).sa_handler;
1815 if (action == SIG_DFL) {
1816 #ifdef KTRACE
1817 if (KTRPOINT(p, KTR_PSIG))
1818 ktrpsig(p, signum, action,
1819 p->p_sigctx.ps_flags & SAS_OLDMASK ?
1820 &p->p_sigctx.ps_oldmask : &p->p_sigctx.ps_sigmask,
1821 NULL);
1822 #endif
1823 /*
1824 * Default action, where the default is to kill
1825 * the process. (Other cases were ignored above.)
1826 */
1827 sigexit(l, signum);
1828 /* NOTREACHED */
1829 } else {
1830 ksiginfo_t *ksi;
1831 /*
1832 * If we get here, the signal must be caught.
1833 */
1834 #ifdef DIAGNOSTIC
1835 if (action == SIG_IGN ||
1836 sigismember(&p->p_sigctx.ps_sigmask, signum))
1837 panic("postsig action");
1838 #endif
1839 /*
1840 * Set the new mask value and also defer further
1841 * occurrences of this signal.
1842 *
1843 * Special case: user has done a sigpause. Here the
1844 * current mask is not of interest, but rather the
1845 * mask from before the sigpause is what we want
1846 * restored after the signal processing is completed.
1847 */
1848 if (p->p_sigctx.ps_flags & SAS_OLDMASK) {
1849 returnmask = &p->p_sigctx.ps_oldmask;
1850 p->p_sigctx.ps_flags &= ~SAS_OLDMASK;
1851 } else
1852 returnmask = &p->p_sigctx.ps_sigmask;
1853 p->p_stats->p_ru.ru_nsignals++;
1854 ksi = ksiginfo_get(p, signum);
1855 #ifdef KTRACE
1856 if (KTRPOINT(p, KTR_PSIG))
1857 ktrpsig(p, signum, action,
1858 p->p_sigctx.ps_flags & SAS_OLDMASK ?
1859 &p->p_sigctx.ps_oldmask : &p->p_sigctx.ps_sigmask,
1860 ksi);
1861 #endif
1862 if (ksi == NULL) {
1863 ksiginfo_t ksi1;
1864 /*
1865 * we did not save any siginfo for this, either
1866 * because the signal was not caught, or because the
1867 * user did not request SA_SIGINFO
1868 */
1869 KSI_INIT_EMPTY(&ksi1);
1870 ksi1.ksi_signo = signum;
1871 kpsendsig(l, &ksi1, returnmask);
1872 } else {
1873 kpsendsig(l, ksi, returnmask);
1874 pool_put(&ksiginfo_pool, ksi);
1875 }
1876 p->p_sigctx.ps_lwp = 0;
1877 p->p_sigctx.ps_code = 0;
1878 p->p_sigctx.ps_signo = 0;
1879 (void) splsched(); /* XXXSMP */
1880 sigplusset(&SIGACTION_PS(ps, signum).sa_mask,
1881 &p->p_sigctx.ps_sigmask);
1882 if (SIGACTION_PS(ps, signum).sa_flags & SA_RESETHAND) {
1883 sigdelset(&p->p_sigctx.ps_sigcatch, signum);
1884 if (signum != SIGCONT && sigprop[signum] & SA_IGNORE)
1885 sigaddset(&p->p_sigctx.ps_sigignore, signum);
1886 SIGACTION_PS(ps, signum).sa_handler = SIG_DFL;
1887 }
1888 (void) spl0(); /* XXXSMP */
1889 }
1890
1891 KERNEL_PROC_UNLOCK(l);
1892 }
1893
1894 /*
1895 * Kill the current process for stated reason.
1896 */
1897 void
1898 killproc(struct proc *p, const char *why)
1899 {
1900 log(LOG_ERR, "pid %d was killed: %s\n", p->p_pid, why);
1901 uprintf("sorry, pid %d was killed: %s\n", p->p_pid, why);
1902 psignal(p, SIGKILL);
1903 }
1904
1905 /*
1906 * Force the current process to exit with the specified signal, dumping core
1907 * if appropriate. We bypass the normal tests for masked and caught signals,
1908 * allowing unrecoverable failures to terminate the process without changing
1909 * signal state. Mark the accounting record with the signal termination.
1910 * If dumping core, save the signal number for the debugger. Calls exit and
1911 * does not return.
1912 */
1913
1914 #if defined(DEBUG)
1915 int kern_logsigexit = 1; /* not static to make public for sysctl */
1916 #else
1917 int kern_logsigexit = 0; /* not static to make public for sysctl */
1918 #endif
1919
1920 static const char logcoredump[] =
1921 "pid %d (%s), uid %d: exited on signal %d (core dumped)\n";
1922 static const char lognocoredump[] =
1923 "pid %d (%s), uid %d: exited on signal %d (core not dumped, err = %d)\n";
1924
1925 /* Wrapper function for use in p_userret */
1926 static void
1927 lwp_coredump_hook(struct lwp *l, void *arg)
1928 {
1929 int s;
1930
1931 /*
1932 * Suspend ourselves, so that the kernel stack and therefore
1933 * the userland registers saved in the trapframe are around
1934 * for coredump() to write them out.
1935 */
1936 KERNEL_PROC_LOCK(l);
1937 l->l_flag &= ~L_DETACHED;
1938 SCHED_LOCK(s);
1939 l->l_stat = LSSUSPENDED;
1940 l->l_proc->p_nrlwps--;
1941 /* XXX NJWLWP check if this makes sense here: */
1942 l->l_proc->p_stats->p_ru.ru_nvcsw++;
1943 mi_switch(l, NULL);
1944 SCHED_ASSERT_UNLOCKED();
1945 splx(s);
1946
1947 lwp_exit(l);
1948 }
1949
1950 void
1951 sigexit(struct lwp *l, int signum)
1952 {
1953 struct proc *p;
1954 #if 0
1955 struct lwp *l2;
1956 #endif
1957 int error, exitsig;
1958
1959 p = l->l_proc;
1960
1961 /*
1962 * Don't permit coredump() or exit1() multiple times
1963 * in the same process.
1964 */
1965 if (p->p_flag & P_WEXIT) {
1966 KERNEL_PROC_UNLOCK(l);
1967 (*p->p_userret)(l, p->p_userret_arg);
1968 }
1969 p->p_flag |= P_WEXIT;
1970 /* We don't want to switch away from exiting. */
1971 /* XXX multiprocessor: stop LWPs on other processors. */
1972 #if 0
1973 if (p->p_flag & P_SA) {
1974 LIST_FOREACH(l2, &p->p_lwps, l_sibling)
1975 l2->l_flag &= ~L_SA;
1976 p->p_flag &= ~P_SA;
1977 }
1978 #endif
1979
1980 /* Make other LWPs stick around long enough to be dumped */
1981 p->p_userret = lwp_coredump_hook;
1982 p->p_userret_arg = NULL;
1983
1984 exitsig = signum;
1985 p->p_acflag |= AXSIG;
1986 if (sigprop[signum] & SA_CORE) {
1987 p->p_sigctx.ps_signo = signum;
1988 if ((error = coredump(l)) == 0)
1989 exitsig |= WCOREFLAG;
1990
1991 if (kern_logsigexit) {
1992 /* XXX What if we ever have really large UIDs? */
1993 int uid = p->p_cred && p->p_ucred ?
1994 (int) p->p_ucred->cr_uid : -1;
1995
1996 if (error)
1997 log(LOG_INFO, lognocoredump, p->p_pid,
1998 p->p_comm, uid, signum, error);
1999 else
2000 log(LOG_INFO, logcoredump, p->p_pid,
2001 p->p_comm, uid, signum);
2002 }
2003
2004 }
2005
2006 exit1(l, W_EXITCODE(0, exitsig));
2007 /* NOTREACHED */
2008 }
2009
2010 /*
2011 * Dump core, into a file named "progname.core" or "core" (depending on the
2012 * value of shortcorename), unless the process was setuid/setgid.
2013 */
2014 int
2015 coredump(struct lwp *l)
2016 {
2017 struct vnode *vp;
2018 struct proc *p;
2019 struct vmspace *vm;
2020 struct ucred *cred;
2021 struct nameidata nd;
2022 struct vattr vattr;
2023 struct mount *mp;
2024 int error, error1;
2025 char name[MAXPATHLEN];
2026
2027 p = l->l_proc;
2028 vm = p->p_vmspace;
2029 cred = p->p_cred->pc_ucred;
2030
2031 /*
2032 * Make sure the process has not set-id, to prevent data leaks.
2033 */
2034 if (p->p_flag & P_SUGID)
2035 return (EPERM);
2036
2037 /*
2038 * Refuse to core if the data + stack + user size is larger than
2039 * the core dump limit. XXX THIS IS WRONG, because of mapped
2040 * data.
2041 */
2042 if (USPACE + ctob(vm->vm_dsize + vm->vm_ssize) >=
2043 p->p_rlimit[RLIMIT_CORE].rlim_cur)
2044 return (EFBIG); /* better error code? */
2045
2046 restart:
2047 /*
2048 * The core dump will go in the current working directory. Make
2049 * sure that the directory is still there and that the mount flags
2050 * allow us to write core dumps there.
2051 */
2052 vp = p->p_cwdi->cwdi_cdir;
2053 if (vp->v_mount == NULL ||
2054 (vp->v_mount->mnt_flag & MNT_NOCOREDUMP) != 0)
2055 return (EPERM);
2056
2057 error = build_corename(p, name);
2058 if (error)
2059 return error;
2060
2061 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, p);
2062 error = vn_open(&nd, O_CREAT | O_NOFOLLOW | FWRITE, S_IRUSR | S_IWUSR);
2063 if (error)
2064 return (error);
2065 vp = nd.ni_vp;
2066
2067 if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
2068 VOP_UNLOCK(vp, 0);
2069 if ((error = vn_close(vp, FWRITE, cred, p)) != 0)
2070 return (error);
2071 if ((error = vn_start_write(NULL, &mp,
2072 V_WAIT | V_SLEEPONLY | V_PCATCH)) != 0)
2073 return (error);
2074 goto restart;
2075 }
2076
2077 /* Don't dump to non-regular files or files with links. */
2078 if (vp->v_type != VREG ||
2079 VOP_GETATTR(vp, &vattr, cred, p) || vattr.va_nlink != 1) {
2080 error = EINVAL;
2081 goto out;
2082 }
2083 VATTR_NULL(&vattr);
2084 vattr.va_size = 0;
2085 VOP_LEASE(vp, p, cred, LEASE_WRITE);
2086 VOP_SETATTR(vp, &vattr, cred, p);
2087 p->p_acflag |= ACORE;
2088
2089 /* Now dump the actual core file. */
2090 error = (*p->p_execsw->es_coredump)(l, vp, cred);
2091 out:
2092 VOP_UNLOCK(vp, 0);
2093 vn_finished_write(mp, 0);
2094 error1 = vn_close(vp, FWRITE, cred, p);
2095 if (error == 0)
2096 error = error1;
2097 return (error);
2098 }
2099
2100 /*
2101 * Nonexistent system call-- signal process (may want to handle it).
2102 * Flag error in case process won't see signal immediately (blocked or ignored).
2103 */
2104 /* ARGSUSED */
2105 int
2106 sys_nosys(struct lwp *l, void *v, register_t *retval)
2107 {
2108 struct proc *p;
2109
2110 p = l->l_proc;
2111 psignal(p, SIGSYS);
2112 return (ENOSYS);
2113 }
2114
2115 static int
2116 build_corename(struct proc *p, char dst[MAXPATHLEN])
2117 {
2118 const char *s;
2119 char *d, *end;
2120 int i;
2121
2122 for (s = p->p_limit->pl_corename, d = dst, end = d + MAXPATHLEN;
2123 *s != '\0'; s++) {
2124 if (*s == '%') {
2125 switch (*(s + 1)) {
2126 case 'n':
2127 i = snprintf(d, end - d, "%s", p->p_comm);
2128 break;
2129 case 'p':
2130 i = snprintf(d, end - d, "%d", p->p_pid);
2131 break;
2132 case 'u':
2133 i = snprintf(d, end - d, "%.*s",
2134 (int)sizeof p->p_pgrp->pg_session->s_login,
2135 p->p_pgrp->pg_session->s_login);
2136 break;
2137 case 't':
2138 i = snprintf(d, end - d, "%ld",
2139 p->p_stats->p_start.tv_sec);
2140 break;
2141 default:
2142 goto copy;
2143 }
2144 d += i;
2145 s++;
2146 } else {
2147 copy: *d = *s;
2148 d++;
2149 }
2150 if (d >= end)
2151 return (ENAMETOOLONG);
2152 }
2153 *d = '\0';
2154 return 0;
2155 }
2156
2157 void
2158 getucontext(struct lwp *l, ucontext_t *ucp)
2159 {
2160 struct proc *p;
2161
2162 p = l->l_proc;
2163
2164 ucp->uc_flags = 0;
2165 ucp->uc_link = l->l_ctxlink;
2166
2167 (void)sigprocmask1(p, 0, NULL, &ucp->uc_sigmask);
2168 ucp->uc_flags |= _UC_SIGMASK;
2169
2170 /*
2171 * The (unsupplied) definition of the `current execution stack'
2172 * in the System V Interface Definition appears to allow returning
2173 * the main context stack.
2174 */
2175 if ((p->p_sigctx.ps_sigstk.ss_flags & SS_ONSTACK) == 0) {
2176 ucp->uc_stack.ss_sp = (void *)USRSTACK;
2177 ucp->uc_stack.ss_size = ctob(p->p_vmspace->vm_ssize);
2178 ucp->uc_stack.ss_flags = 0; /* XXX, def. is Very Fishy */
2179 } else {
2180 /* Simply copy alternate signal execution stack. */
2181 ucp->uc_stack = p->p_sigctx.ps_sigstk;
2182 }
2183 ucp->uc_flags |= _UC_STACK;
2184
2185 cpu_getmcontext(l, &ucp->uc_mcontext, &ucp->uc_flags);
2186 }
2187
2188 /* ARGSUSED */
2189 int
2190 sys_getcontext(struct lwp *l, void *v, register_t *retval)
2191 {
2192 struct sys_getcontext_args /* {
2193 syscallarg(struct __ucontext *) ucp;
2194 } */ *uap = v;
2195 ucontext_t uc;
2196
2197 getucontext(l, &uc);
2198
2199 return (copyout(&uc, SCARG(uap, ucp), sizeof (*SCARG(uap, ucp))));
2200 }
2201
2202 int
2203 setucontext(struct lwp *l, const ucontext_t *ucp)
2204 {
2205 struct proc *p;
2206 int error;
2207
2208 p = l->l_proc;
2209 if ((error = cpu_setmcontext(l, &ucp->uc_mcontext, ucp->uc_flags)) != 0)
2210 return (error);
2211 l->l_ctxlink = ucp->uc_link;
2212
2213 if ((ucp->uc_flags & _UC_SIGMASK) != 0)
2214 sigprocmask1(p, SIG_SETMASK, &ucp->uc_sigmask, NULL);
2215
2216 /*
2217 * If there was stack information, update whether or not we are
2218 * still running on an alternate signal stack.
2219 */
2220 if ((ucp->uc_flags & _UC_STACK) != 0) {
2221 if (ucp->uc_stack.ss_flags & SS_ONSTACK)
2222 p->p_sigctx.ps_sigstk.ss_flags |= SS_ONSTACK;
2223 else
2224 p->p_sigctx.ps_sigstk.ss_flags &= ~SS_ONSTACK;
2225 }
2226
2227 return 0;
2228 }
2229
2230 /* ARGSUSED */
2231 int
2232 sys_setcontext(struct lwp *l, void *v, register_t *retval)
2233 {
2234 struct sys_setcontext_args /* {
2235 syscallarg(const ucontext_t *) ucp;
2236 } */ *uap = v;
2237 ucontext_t uc;
2238 int error;
2239
2240 if (SCARG(uap, ucp) == NULL) /* i.e. end of uc_link chain */
2241 exit1(l, W_EXITCODE(0, 0));
2242 else if ((error = copyin(SCARG(uap, ucp), &uc, sizeof (uc))) != 0 ||
2243 (error = setucontext(l, &uc)) != 0)
2244 return (error);
2245
2246 return (EJUSTRETURN);
2247 }
2248
2249 /*
2250 * sigtimedwait(2) system call, used also for implementation
2251 * of sigwaitinfo() and sigwait().
2252 *
2253 * This only handles single LWP in signal wait. libpthread provides
2254 * it's own sigtimedwait() wrapper to DTRT WRT individual threads.
2255 */
2256 int
2257 sys___sigtimedwait(struct lwp *l, void *v, register_t *retval)
2258 {
2259 struct sys___sigtimedwait_args /* {
2260 syscallarg(const sigset_t *) set;
2261 syscallarg(siginfo_t *) info;
2262 syscallarg(struct timespec *) timeout;
2263 } */ *uap = v;
2264 sigset_t *waitset, twaitset;
2265 struct proc *p = l->l_proc;
2266 int error, signum, s;
2267 int timo = 0;
2268 struct timeval tvstart;
2269 struct timespec ts;
2270 ksiginfo_t *ksi;
2271
2272 MALLOC(waitset, sigset_t *, sizeof(sigset_t), M_TEMP, M_WAITOK);
2273
2274 if ((error = copyin(SCARG(uap, set), waitset, sizeof(sigset_t)))) {
2275 FREE(waitset, M_TEMP);
2276 return (error);
2277 }
2278
2279 /*
2280 * Silently ignore SA_CANTMASK signals. psignal1() would
2281 * ignore SA_CANTMASK signals in waitset, we do this
2282 * only for the below siglist check.
2283 */
2284 sigminusset(&sigcantmask, waitset);
2285
2286 /*
2287 * First scan siglist and check if there is signal from
2288 * our waitset already pending.
2289 */
2290 twaitset = *waitset;
2291 __sigandset(&p->p_sigctx.ps_siglist, &twaitset);
2292 if ((signum = firstsig(&twaitset))) {
2293 /* found pending signal */
2294 sigdelset(&p->p_sigctx.ps_siglist, signum);
2295 ksi = ksiginfo_get(p, signum);
2296 if (!ksi) {
2297 /* No queued siginfo, manufacture one */
2298 ksi = pool_get(&ksiginfo_pool, PR_WAITOK);
2299 KSI_INIT(ksi);
2300 ksi->ksi_info._signo = signum;
2301 ksi->ksi_info._code = SI_USER;
2302 }
2303
2304 goto sig;
2305 }
2306
2307 /*
2308 * Calculate timeout, if it was specified.
2309 */
2310 if (SCARG(uap, timeout)) {
2311 uint64_t ms;
2312
2313 if ((error = copyin(SCARG(uap, timeout), &ts, sizeof(ts))))
2314 return (error);
2315
2316 ms = (ts.tv_sec * 1000) + (ts.tv_nsec / 1000000);
2317 timo = mstohz(ms);
2318 if (timo == 0 && ts.tv_sec == 0 && ts.tv_nsec > 0)
2319 timo = 1;
2320 if (timo <= 0)
2321 return (EAGAIN);
2322
2323 /*
2324 * Remember current mono_time, it would be used in
2325 * ECANCELED/ERESTART case.
2326 */
2327 s = splclock();
2328 tvstart = mono_time;
2329 splx(s);
2330 }
2331
2332 /*
2333 * Setup ps_sigwait list. Pass pointer to malloced memory
2334 * here; it's not possible to pass pointer to a structure
2335 * on current process's stack, the current process might
2336 * be swapped out at the time the signal would get delivered.
2337 */
2338 ksi = pool_get(&ksiginfo_pool, PR_WAITOK);
2339 p->p_sigctx.ps_sigwaited = ksi;
2340 p->p_sigctx.ps_sigwait = waitset;
2341
2342 /*
2343 * Wait for signal to arrive. We can either be woken up or
2344 * time out.
2345 */
2346 error = tsleep(&p->p_sigctx.ps_sigwait, PPAUSE|PCATCH, "sigwait", timo);
2347
2348 /*
2349 * Need to find out if we woke as a result of lwp_wakeup()
2350 * or a signal outside our wait set.
2351 */
2352 if (error == EINTR && p->p_sigctx.ps_sigwaited
2353 && !firstsig(&p->p_sigctx.ps_siglist)) {
2354 /* wakeup via _lwp_wakeup() */
2355 error = ECANCELED;
2356 } else if (!error && p->p_sigctx.ps_sigwaited) {
2357 /* spurious wakeup - arrange for syscall restart */
2358 error = ERESTART;
2359 goto fail;
2360 }
2361
2362 /*
2363 * On error, clear sigwait indication. psignal1() clears it
2364 * in !error case.
2365 */
2366 if (error) {
2367 p->p_sigctx.ps_sigwaited = NULL;
2368
2369 /*
2370 * If the sleep was interrupted (either by signal or wakeup),
2371 * update the timeout and copyout new value back.
2372 * It would be used when the syscall would be restarted
2373 * or called again.
2374 */
2375 if (timo && (error == ERESTART || error == ECANCELED)) {
2376 struct timeval tvnow, tvtimo;
2377 int err;
2378
2379 s = splclock();
2380 tvnow = mono_time;
2381 splx(s);
2382
2383 TIMESPEC_TO_TIMEVAL(&tvtimo, &ts);
2384
2385 /* compute how much time has passed since start */
2386 timersub(&tvnow, &tvstart, &tvnow);
2387 /* substract passed time from timeout */
2388 timersub(&tvtimo, &tvnow, &tvtimo);
2389
2390 if (tvtimo.tv_sec < 0) {
2391 error = EAGAIN;
2392 goto fail;
2393 }
2394
2395 TIMEVAL_TO_TIMESPEC(&tvtimo, &ts);
2396
2397 /* copy updated timeout to userland */
2398 if ((err = copyout(&ts, SCARG(uap, timeout), sizeof(ts)))) {
2399 error = err;
2400 goto fail;
2401 }
2402 }
2403
2404 goto fail;
2405 }
2406
2407 /*
2408 * If a signal from the wait set arrived, copy it to userland.
2409 * Copy only the used part of siginfo, the padding part is
2410 * left unchanged (userland is not supposed to touch it anyway).
2411 */
2412 sig:
2413 error = copyout(&ksi->ksi_info, SCARG(uap, info), sizeof(ksi->ksi_info));
2414
2415 fail:
2416 FREE(waitset, M_TEMP);
2417 pool_put(&ksiginfo_pool, ksi);
2418 p->p_sigctx.ps_sigwait = NULL;
2419
2420 return (error);
2421 }
2422
2423 /*
2424 * Returns true if signal is ignored or masked for passed process.
2425 */
2426 int
2427 sigismasked(struct proc *p, int sig)
2428 {
2429
2430 return (sigismember(&p->p_sigctx.ps_sigignore, sig) ||
2431 sigismember(&p->p_sigctx.ps_sigmask, sig));
2432 }
2433
2434 static int
2435 filt_sigattach(struct knote *kn)
2436 {
2437 struct proc *p = curproc;
2438
2439 kn->kn_ptr.p_proc = p;
2440 kn->kn_flags |= EV_CLEAR; /* automatically set */
2441
2442 SLIST_INSERT_HEAD(&p->p_klist, kn, kn_selnext);
2443
2444 return (0);
2445 }
2446
2447 static void
2448 filt_sigdetach(struct knote *kn)
2449 {
2450 struct proc *p = kn->kn_ptr.p_proc;
2451
2452 SLIST_REMOVE(&p->p_klist, kn, knote, kn_selnext);
2453 }
2454
2455 /*
2456 * signal knotes are shared with proc knotes, so we apply a mask to
2457 * the hint in order to differentiate them from process hints. This
2458 * could be avoided by using a signal-specific knote list, but probably
2459 * isn't worth the trouble.
2460 */
2461 static int
2462 filt_signal(struct knote *kn, long hint)
2463 {
2464
2465 if (hint & NOTE_SIGNAL) {
2466 hint &= ~NOTE_SIGNAL;
2467
2468 if (kn->kn_id == hint)
2469 kn->kn_data++;
2470 }
2471 return (kn->kn_data != 0);
2472 }
2473
2474 const struct filterops sig_filtops = {
2475 0, filt_sigattach, filt_sigdetach, filt_signal
2476 };
2477