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