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