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