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