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