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