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