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