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