kern_sig.c revision 1.121 1 /* $NetBSD: kern_sig.c,v 1.121 2002/07/04 23:32:14 thorpej Exp $ */
2
3 /*
4 * Copyright (c) 1982, 1986, 1989, 1991, 1993
5 * The Regents of the University of California. All rights reserved.
6 * (c) UNIX System Laboratories, Inc.
7 * All or some portions of this file are derived from material licensed
8 * to the University of California by American Telephone and Telegraph
9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10 * the permission of UNIX System Laboratories, Inc.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the University of
23 * California, Berkeley and its contributors.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * SUCH DAMAGE.
39 *
40 * @(#)kern_sig.c 8.14 (Berkeley) 5/14/95
41 */
42
43 #include <sys/cdefs.h>
44 __KERNEL_RCSID(0, "$NetBSD: kern_sig.c,v 1.121 2002/07/04 23:32:14 thorpej Exp $");
45
46 #include "opt_ktrace.h"
47 #include "opt_compat_sunos.h"
48 #include "opt_compat_netbsd32.h"
49
50 #define SIGPROP /* include signal properties table */
51 #include <sys/param.h>
52 #include <sys/signalvar.h>
53 #include <sys/resourcevar.h>
54 #include <sys/namei.h>
55 #include <sys/vnode.h>
56 #include <sys/proc.h>
57 #include <sys/systm.h>
58 #include <sys/timeb.h>
59 #include <sys/times.h>
60 #include <sys/buf.h>
61 #include <sys/acct.h>
62 #include <sys/file.h>
63 #include <sys/kernel.h>
64 #include <sys/wait.h>
65 #include <sys/ktrace.h>
66 #include <sys/syslog.h>
67 #include <sys/stat.h>
68 #include <sys/core.h>
69 #include <sys/filedesc.h>
70 #include <sys/malloc.h>
71 #include <sys/pool.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 proc_stop(struct proc *p);
84 void killproc(struct proc *, char *);
85 static int build_corename(struct proc *, char [MAXPATHLEN]);
86 sigset_t contsigmask, stopsigmask, sigcantmask;
87
88 struct pool sigacts_pool; /* memory pool for sigacts structures */
89
90 /*
91 * Can process p, with pcred pc, send the signal signum to process q?
92 */
93 #define CANSIGNAL(p, pc, q, signum) \
94 ((pc)->pc_ucred->cr_uid == 0 || \
95 (pc)->p_ruid == (q)->p_cred->p_ruid || \
96 (pc)->pc_ucred->cr_uid == (q)->p_cred->p_ruid || \
97 (pc)->p_ruid == (q)->p_ucred->cr_uid || \
98 (pc)->pc_ucred->cr_uid == (q)->p_ucred->cr_uid || \
99 ((signum) == SIGCONT && (q)->p_session == (p)->p_session))
100
101 /*
102 * Initialize signal-related data structures.
103 */
104 void
105 signal_init(void)
106 {
107
108 pool_init(&sigacts_pool, sizeof(struct sigacts), 0, 0, 0, "sigapl",
109 &pool_allocator_nointr);
110 }
111
112 /*
113 * Create an initial sigctx structure, using the same signal state
114 * as p. If 'share' is set, share the sigctx_proc part, otherwise just
115 * copy it from parent.
116 */
117 void
118 sigactsinit(struct proc *np, struct proc *pp, int share)
119 {
120 struct sigacts *ps;
121
122 if (share) {
123 np->p_sigacts = pp->p_sigacts;
124 pp->p_sigacts->sa_refcnt++;
125 } else {
126 ps = pool_get(&sigacts_pool, PR_WAITOK);
127 if (pp)
128 memcpy(ps, pp->p_sigacts, sizeof(struct sigacts));
129 else
130 memset(ps, '\0', sizeof(struct sigacts));
131 ps->sa_refcnt = 1;
132 np->p_sigacts = ps;
133 }
134 }
135
136 /*
137 * Make this process not share its sigctx, maintaining all
138 * signal state.
139 */
140 void
141 sigactsunshare(struct proc *p)
142 {
143 struct sigacts *oldps;
144
145 if (p->p_sigacts->sa_refcnt == 1)
146 return;
147
148 oldps = p->p_sigacts;
149 sigactsinit(p, NULL, 0);
150
151 if (--oldps->sa_refcnt == 0)
152 pool_put(&sigacts_pool, oldps);
153 }
154
155 /*
156 * Release a sigctx structure.
157 */
158 void
159 sigactsfree(struct proc *p)
160 {
161 struct sigacts *ps;
162
163 ps = p->p_sigacts;
164 if (--ps->sa_refcnt > 0)
165 return;
166
167 pool_put(&sigacts_pool, ps);
168 }
169
170 int
171 sigaction1(struct proc *p, int signum, const struct sigaction *nsa,
172 struct sigaction *osa, void *tramp, int vers)
173 {
174 struct sigacts *ps;
175 int prop;
176
177 ps = p->p_sigacts;
178 if (signum <= 0 || signum >= NSIG)
179 return (EINVAL);
180
181 /*
182 * Trampoline ABI version 0 is reserved for the legacy
183 * kernel-provided on-stack trampoline. Conversely, if
184 * we are using a non-0 ABI version, we must have a
185 * trampoline.
186 */
187 if ((vers != 0 && tramp == NULL) ||
188 (vers == 0 && tramp != NULL))
189 return (EINVAL);
190
191 if (osa)
192 *osa = SIGACTION_PS(ps, signum);
193
194 if (nsa) {
195 if (nsa->sa_flags & ~SA_ALLBITS)
196 return (EINVAL);
197
198 prop = sigprop[signum];
199 if (prop & SA_CANTMASK)
200 return (EINVAL);
201
202 (void) splsched(); /* XXXSMP */
203 SIGACTION_PS(ps, signum) = *nsa;
204 ps->sa_sigdesc[signum].sd_tramp = tramp;
205 ps->sa_sigdesc[signum].sd_vers = vers;
206 sigminusset(&sigcantmask, &SIGACTION_PS(ps, signum).sa_mask);
207 if ((prop & SA_NORESET) != 0)
208 SIGACTION_PS(ps, signum).sa_flags &= ~SA_RESETHAND;
209 if (signum == SIGCHLD) {
210 if (nsa->sa_flags & SA_NOCLDSTOP)
211 p->p_flag |= P_NOCLDSTOP;
212 else
213 p->p_flag &= ~P_NOCLDSTOP;
214 if (nsa->sa_flags & SA_NOCLDWAIT) {
215 /*
216 * Paranoia: since SA_NOCLDWAIT is implemented
217 * by reparenting the dying child to PID 1 (and
218 * trust it to reap the zombie), PID 1 itself
219 * is forbidden to set SA_NOCLDWAIT.
220 */
221 if (p->p_pid == 1)
222 p->p_flag &= ~P_NOCLDWAIT;
223 else
224 p->p_flag |= P_NOCLDWAIT;
225 } else
226 p->p_flag &= ~P_NOCLDWAIT;
227 }
228 if ((nsa->sa_flags & SA_NODEFER) == 0)
229 sigaddset(&SIGACTION_PS(ps, signum).sa_mask, signum);
230 else
231 sigdelset(&SIGACTION_PS(ps, signum).sa_mask, signum);
232 /*
233 * Set bit in p_sigctx.ps_sigignore for signals that are set to
234 * SIG_IGN, and for signals set to SIG_DFL where the default is
235 * to ignore. However, don't put SIGCONT in
236 * p_sigctx.ps_sigignore, as we have to restart the process.
237 */
238 if (nsa->sa_handler == SIG_IGN ||
239 (nsa->sa_handler == SIG_DFL && (prop & SA_IGNORE) != 0)) {
240 /* never to be seen again */
241 sigdelset(&p->p_sigctx.ps_siglist, signum);
242 if (signum != SIGCONT) {
243 /* easier in psignal */
244 sigaddset(&p->p_sigctx.ps_sigignore, signum);
245 }
246 sigdelset(&p->p_sigctx.ps_sigcatch, signum);
247 } else {
248 sigdelset(&p->p_sigctx.ps_sigignore, signum);
249 if (nsa->sa_handler == SIG_DFL)
250 sigdelset(&p->p_sigctx.ps_sigcatch, signum);
251 else
252 sigaddset(&p->p_sigctx.ps_sigcatch, signum);
253 }
254 (void) spl0();
255 }
256
257 return (0);
258 }
259
260 /* ARGSUSED */
261 int
262 sys___sigaction14(struct proc *p, void *v, register_t *retval)
263 {
264 struct sys___sigaction14_args /* {
265 syscallarg(int) signum;
266 syscallarg(const struct sigaction *) nsa;
267 syscallarg(struct sigaction *) osa;
268 } */ *uap = v;
269 struct sigaction nsa, osa;
270 int error;
271
272 if (SCARG(uap, nsa)) {
273 error = copyin(SCARG(uap, nsa), &nsa, sizeof(nsa));
274 if (error)
275 return (error);
276 }
277 error = sigaction1(p, SCARG(uap, signum),
278 SCARG(uap, nsa) ? &nsa : 0, SCARG(uap, osa) ? &osa : 0,
279 NULL, 0);
280 if (error)
281 return (error);
282 if (SCARG(uap, osa)) {
283 error = copyout(&osa, SCARG(uap, osa), sizeof(osa));
284 if (error)
285 return (error);
286 }
287 return (0);
288 }
289
290 /* ARGSUSED */
291 int
292 sys___sigaction_sigtramp(struct proc *p, void *v, register_t *retval)
293 {
294 struct sys___sigaction_sigtramp_args /* {
295 syscallarg(int) signum;
296 syscallarg(const struct sigaction *) nsa;
297 syscallarg(struct sigaction *) osa;
298 syscallarg(void *) tramp;
299 syscallarg(int) vers;
300 } */ *uap = v;
301 struct sigaction nsa, osa;
302 int error;
303
304 if (SCARG(uap, nsa)) {
305 error = copyin(SCARG(uap, nsa), &nsa, sizeof(nsa));
306 if (error)
307 return (error);
308 }
309 error = sigaction1(p, SCARG(uap, signum),
310 SCARG(uap, nsa) ? &nsa : 0, SCARG(uap, osa) ? &osa : 0,
311 SCARG(uap, tramp), SCARG(uap, vers));
312 if (error)
313 return (error);
314 if (SCARG(uap, osa)) {
315 error = copyout(&osa, SCARG(uap, osa), sizeof(osa));
316 if (error)
317 return (error);
318 }
319 return (0);
320 }
321
322 /*
323 * Initialize signal state for process 0;
324 * set to ignore signals that are ignored by default and disable the signal
325 * stack.
326 */
327 void
328 siginit(struct proc *p)
329 {
330 struct sigacts *ps;
331 int signum, prop;
332
333 ps = p->p_sigacts;
334 sigemptyset(&contsigmask);
335 sigemptyset(&stopsigmask);
336 sigemptyset(&sigcantmask);
337 for (signum = 1; signum < NSIG; signum++) {
338 prop = sigprop[signum];
339 if (prop & SA_CONT)
340 sigaddset(&contsigmask, signum);
341 if (prop & SA_STOP)
342 sigaddset(&stopsigmask, signum);
343 if (prop & SA_CANTMASK)
344 sigaddset(&sigcantmask, signum);
345 if (prop & SA_IGNORE && signum != SIGCONT)
346 sigaddset(&p->p_sigctx.ps_sigignore, signum);
347 sigemptyset(&SIGACTION_PS(ps, signum).sa_mask);
348 SIGACTION_PS(ps, signum).sa_flags = SA_RESTART;
349 }
350 sigemptyset(&p->p_sigctx.ps_sigcatch);
351 p->p_flag &= ~P_NOCLDSTOP;
352
353 /*
354 * Reset stack state to the user stack.
355 */
356 p->p_sigctx.ps_sigstk.ss_flags = SS_DISABLE;
357 p->p_sigctx.ps_sigstk.ss_size = 0;
358 p->p_sigctx.ps_sigstk.ss_sp = 0;
359
360 /* One reference. */
361 ps->sa_refcnt = 1;
362 }
363
364 /*
365 * Reset signals for an exec of the specified process.
366 */
367 void
368 execsigs(struct proc *p)
369 {
370 struct sigacts *ps;
371 int signum, prop;
372
373 sigactsunshare(p);
374
375 ps = p->p_sigacts;
376
377 /*
378 * Reset caught signals. Held signals remain held
379 * through p_sigctx.ps_sigmask (unless they were caught,
380 * and are now ignored by default).
381 */
382 for (signum = 1; signum < NSIG; signum++) {
383 if (sigismember(&p->p_sigctx.ps_sigcatch, signum)) {
384 prop = sigprop[signum];
385 if (prop & SA_IGNORE) {
386 if ((prop & SA_CONT) == 0)
387 sigaddset(&p->p_sigctx.ps_sigignore,
388 signum);
389 sigdelset(&p->p_sigctx.ps_siglist, signum);
390 }
391 SIGACTION_PS(ps, signum).sa_handler = SIG_DFL;
392 }
393 sigemptyset(&SIGACTION_PS(ps, signum).sa_mask);
394 SIGACTION_PS(ps, signum).sa_flags = SA_RESTART;
395 }
396 sigemptyset(&p->p_sigctx.ps_sigcatch);
397 p->p_flag &= ~P_NOCLDSTOP;
398
399 /*
400 * Reset stack state to the user stack.
401 */
402 p->p_sigctx.ps_sigstk.ss_flags = SS_DISABLE;
403 p->p_sigctx.ps_sigstk.ss_size = 0;
404 p->p_sigctx.ps_sigstk.ss_sp = 0;
405 }
406
407 int
408 sigprocmask1(struct proc *p, int how, const sigset_t *nss, sigset_t *oss)
409 {
410
411 if (oss)
412 *oss = p->p_sigctx.ps_sigmask;
413
414 if (nss) {
415 (void)splsched(); /* XXXSMP */
416 switch (how) {
417 case SIG_BLOCK:
418 sigplusset(nss, &p->p_sigctx.ps_sigmask);
419 break;
420 case SIG_UNBLOCK:
421 sigminusset(nss, &p->p_sigctx.ps_sigmask);
422 CHECKSIGS(p);
423 break;
424 case SIG_SETMASK:
425 p->p_sigctx.ps_sigmask = *nss;
426 CHECKSIGS(p);
427 break;
428 default:
429 (void)spl0(); /* XXXSMP */
430 return (EINVAL);
431 }
432 sigminusset(&sigcantmask, &p->p_sigctx.ps_sigmask);
433 (void)spl0(); /* XXXSMP */
434 }
435
436 return (0);
437 }
438
439 /*
440 * Manipulate signal mask.
441 * Note that we receive new mask, not pointer,
442 * and return old mask as return value;
443 * the library stub does the rest.
444 */
445 int
446 sys___sigprocmask14(struct proc *p, void *v, register_t *retval)
447 {
448 struct sys___sigprocmask14_args /* {
449 syscallarg(int) how;
450 syscallarg(const sigset_t *) set;
451 syscallarg(sigset_t *) oset;
452 } */ *uap = v;
453 sigset_t nss, oss;
454 int error;
455
456 if (SCARG(uap, set)) {
457 error = copyin(SCARG(uap, set), &nss, sizeof(nss));
458 if (error)
459 return (error);
460 }
461 error = sigprocmask1(p, SCARG(uap, how),
462 SCARG(uap, set) ? &nss : 0, SCARG(uap, oset) ? &oss : 0);
463 if (error)
464 return (error);
465 if (SCARG(uap, oset)) {
466 error = copyout(&oss, SCARG(uap, oset), sizeof(oss));
467 if (error)
468 return (error);
469 }
470 return (0);
471 }
472
473 void
474 sigpending1(struct proc *p, sigset_t *ss)
475 {
476
477 *ss = p->p_sigctx.ps_siglist;
478 sigminusset(&p->p_sigctx.ps_sigmask, ss);
479 }
480
481 /* ARGSUSED */
482 int
483 sys___sigpending14(struct proc *p, void *v, register_t *retval)
484 {
485 struct sys___sigpending14_args /* {
486 syscallarg(sigset_t *) set;
487 } */ *uap = v;
488 sigset_t ss;
489
490 sigpending1(p, &ss);
491 return (copyout(&ss, SCARG(uap, set), sizeof(ss)));
492 }
493
494 int
495 sigsuspend1(struct proc *p, const sigset_t *ss)
496 {
497 struct sigacts *ps;
498
499 ps = p->p_sigacts;
500 if (ss) {
501 /*
502 * When returning from sigpause, we want
503 * the old mask to be restored after the
504 * signal handler has finished. Thus, we
505 * save it here and mark the sigctx structure
506 * to indicate this.
507 */
508 p->p_sigctx.ps_oldmask = p->p_sigctx.ps_sigmask;
509 p->p_sigctx.ps_flags |= SAS_OLDMASK;
510 (void) splsched(); /* XXXSMP */
511 p->p_sigctx.ps_sigmask = *ss;
512 CHECKSIGS(p);
513 sigminusset(&sigcantmask, &p->p_sigctx.ps_sigmask);
514 (void) spl0(); /* XXXSMP */
515 }
516
517 while (tsleep((caddr_t) ps, PPAUSE|PCATCH, "pause", 0) == 0)
518 /* void */;
519 /* always return EINTR rather than ERESTART... */
520 return (EINTR);
521 }
522
523 /*
524 * Suspend process until signal, providing mask to be set
525 * in the meantime. Note nonstandard calling convention:
526 * libc stub passes mask, not pointer, to save a copyin.
527 */
528 /* ARGSUSED */
529 int
530 sys___sigsuspend14(struct proc *p, void *v, register_t *retval)
531 {
532 struct sys___sigsuspend14_args /* {
533 syscallarg(const sigset_t *) set;
534 } */ *uap = v;
535 sigset_t ss;
536 int error;
537
538 if (SCARG(uap, set)) {
539 error = copyin(SCARG(uap, set), &ss, sizeof(ss));
540 if (error)
541 return (error);
542 }
543
544 return (sigsuspend1(p, SCARG(uap, set) ? &ss : 0));
545 }
546
547 int
548 sigaltstack1(struct proc *p, const struct sigaltstack *nss,
549 struct sigaltstack *oss)
550 {
551
552 if (oss)
553 *oss = p->p_sigctx.ps_sigstk;
554
555 if (nss) {
556 if (nss->ss_flags & ~SS_ALLBITS)
557 return (EINVAL);
558
559 if (nss->ss_flags & SS_DISABLE) {
560 if (p->p_sigctx.ps_sigstk.ss_flags & SS_ONSTACK)
561 return (EINVAL);
562 } else {
563 if (nss->ss_size < MINSIGSTKSZ)
564 return (ENOMEM);
565 }
566 p->p_sigctx.ps_sigstk = *nss;
567 }
568
569 return (0);
570 }
571
572 /* ARGSUSED */
573 int
574 sys___sigaltstack14(struct proc *p, void *v, register_t *retval)
575 {
576 struct sys___sigaltstack14_args /* {
577 syscallarg(const struct sigaltstack *) nss;
578 syscallarg(struct sigaltstack *) oss;
579 } */ *uap = v;
580 struct sigaltstack nss, oss;
581 int error;
582
583 if (SCARG(uap, nss)) {
584 error = copyin(SCARG(uap, nss), &nss, sizeof(nss));
585 if (error)
586 return (error);
587 }
588 error = sigaltstack1(p,
589 SCARG(uap, nss) ? &nss : 0, SCARG(uap, oss) ? &oss : 0);
590 if (error)
591 return (error);
592 if (SCARG(uap, oss)) {
593 error = copyout(&oss, SCARG(uap, oss), sizeof(oss));
594 if (error)
595 return (error);
596 }
597 return (0);
598 }
599
600 /* ARGSUSED */
601 int
602 sys_kill(struct proc *cp, void *v, register_t *retval)
603 {
604 struct sys_kill_args /* {
605 syscallarg(int) pid;
606 syscallarg(int) signum;
607 } */ *uap = v;
608 struct proc *p;
609 struct pcred *pc;
610
611 pc = cp->p_cred;
612 if ((u_int)SCARG(uap, signum) >= NSIG)
613 return (EINVAL);
614 if (SCARG(uap, pid) > 0) {
615 /* kill single process */
616 if ((p = pfind(SCARG(uap, pid))) == NULL)
617 return (ESRCH);
618 if (!CANSIGNAL(cp, pc, p, SCARG(uap, signum)))
619 return (EPERM);
620 if (SCARG(uap, signum))
621 psignal(p, SCARG(uap, signum));
622 return (0);
623 }
624 switch (SCARG(uap, pid)) {
625 case -1: /* broadcast signal */
626 return (killpg1(cp, SCARG(uap, signum), 0, 1));
627 case 0: /* signal own process group */
628 return (killpg1(cp, SCARG(uap, signum), 0, 0));
629 default: /* negative explicit process group */
630 return (killpg1(cp, SCARG(uap, signum), -SCARG(uap, pid), 0));
631 }
632 /* NOTREACHED */
633 }
634
635 /*
636 * Common code for kill process group/broadcast kill.
637 * cp is calling process.
638 */
639 int
640 killpg1(struct proc *cp, int signum, int pgid, int all)
641 {
642 struct proc *p;
643 struct pcred *pc;
644 struct pgrp *pgrp;
645 int nfound;
646
647 pc = cp->p_cred;
648 nfound = 0;
649 if (all) {
650 /*
651 * broadcast
652 */
653 proclist_lock_read();
654 for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
655 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
656 p == cp || !CANSIGNAL(cp, pc, p, signum))
657 continue;
658 nfound++;
659 if (signum)
660 psignal(p, signum);
661 }
662 proclist_unlock_read();
663 } else {
664 if (pgid == 0)
665 /*
666 * zero pgid means send to my process group.
667 */
668 pgrp = cp->p_pgrp;
669 else {
670 pgrp = pgfind(pgid);
671 if (pgrp == NULL)
672 return (ESRCH);
673 }
674 for (p = pgrp->pg_members.lh_first;
675 p != 0;
676 p = p->p_pglist.le_next) {
677 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
678 !CANSIGNAL(cp, pc, p, signum))
679 continue;
680 nfound++;
681 if (signum && P_ZOMBIE(p) == 0)
682 psignal(p, signum);
683 }
684 }
685 return (nfound ? 0 : ESRCH);
686 }
687
688 /*
689 * Send a signal to a process group.
690 */
691 void
692 gsignal(int pgid, int signum)
693 {
694 struct pgrp *pgrp;
695
696 if (pgid && (pgrp = pgfind(pgid)))
697 pgsignal(pgrp, signum, 0);
698 }
699
700 /*
701 * Send a signal to a process group. If checktty is 1,
702 * limit to members which have a controlling terminal.
703 */
704 void
705 pgsignal(struct pgrp *pgrp, int signum, int checkctty)
706 {
707 struct proc *p;
708
709 if (pgrp)
710 for (p = pgrp->pg_members.lh_first; p != 0;
711 p = p->p_pglist.le_next)
712 if (checkctty == 0 || p->p_flag & P_CONTROLT)
713 psignal(p, signum);
714 }
715
716 /*
717 * Send a signal caused by a trap to the current process.
718 * If it will be caught immediately, deliver it with correct code.
719 * Otherwise, post it normally.
720 */
721 void
722 trapsignal(struct proc *p, int signum, u_long code)
723 {
724 struct sigacts *ps;
725
726 ps = p->p_sigacts;
727 if ((p->p_flag & P_TRACED) == 0 &&
728 sigismember(&p->p_sigctx.ps_sigcatch, signum) &&
729 !sigismember(&p->p_sigctx.ps_sigmask, signum)) {
730 p->p_stats->p_ru.ru_nsignals++;
731 #ifdef KTRACE
732 if (KTRPOINT(p, KTR_PSIG))
733 ktrpsig(p, signum,
734 SIGACTION_PS(ps, signum).sa_handler,
735 &p->p_sigctx.ps_sigmask, code);
736 #endif
737 (*p->p_emul->e_sendsig)(signum, &p->p_sigctx.ps_sigmask,
738 code);
739 (void) splsched(); /* XXXSMP */
740 sigplusset(&SIGACTION_PS(ps, signum).sa_mask,
741 &p->p_sigctx.ps_sigmask);
742 if (SIGACTION_PS(ps, signum).sa_flags & SA_RESETHAND) {
743 sigdelset(&p->p_sigctx.ps_sigcatch, signum);
744 if (signum != SIGCONT && sigprop[signum] & SA_IGNORE)
745 sigaddset(&p->p_sigctx.ps_sigignore, signum);
746 SIGACTION_PS(ps, signum).sa_handler = SIG_DFL;
747 }
748 (void) spl0(); /* XXXSMP */
749 } else {
750 p->p_sigctx.ps_code = code; /* XXX for core dump/debugger */
751 p->p_sigctx.ps_sig = signum; /* XXX to verify code */
752 psignal(p, signum);
753 }
754 }
755
756 /*
757 * Send the signal to the process. If the signal has an action, the action
758 * is usually performed by the target process rather than the caller; we add
759 * the signal to the set of pending signals for the process.
760 *
761 * Exceptions:
762 * o When a stop signal is sent to a sleeping process that takes the
763 * default action, the process is stopped without awakening it.
764 * o SIGCONT restarts stopped processes (or puts them back to sleep)
765 * regardless of the signal action (eg, blocked or ignored).
766 *
767 * Other ignored signals are discarded immediately.
768 *
769 * XXXSMP: Invoked as psignal() or sched_psignal().
770 */
771 void
772 psignal1(struct proc *p, int signum,
773 int dolock) /* XXXSMP: works, but icky */
774 {
775 int s, prop;
776 sig_t action;
777
778 #ifdef DIAGNOSTIC
779 if (signum <= 0 || signum >= NSIG)
780 panic("psignal signal number");
781
782 /* XXXSMP: works, but icky */
783 if (dolock)
784 SCHED_ASSERT_UNLOCKED();
785 else
786 SCHED_ASSERT_LOCKED();
787 #endif
788 prop = sigprop[signum];
789
790 /*
791 * If proc is traced, always give parent a chance.
792 */
793 if (p->p_flag & P_TRACED)
794 action = SIG_DFL;
795 else {
796 /*
797 * If the signal is being ignored,
798 * then we forget about it immediately.
799 * (Note: we don't set SIGCONT in p_sigctx.ps_sigignore,
800 * and if it is set to SIG_IGN,
801 * action will be SIG_DFL here.)
802 */
803 if (sigismember(&p->p_sigctx.ps_sigignore, signum))
804 return;
805 if (sigismember(&p->p_sigctx.ps_sigmask, signum))
806 action = SIG_HOLD;
807 else if (sigismember(&p->p_sigctx.ps_sigcatch, signum))
808 action = SIG_CATCH;
809 else {
810 action = SIG_DFL;
811
812 if (prop & SA_KILL && p->p_nice > NZERO)
813 p->p_nice = NZERO;
814
815 /*
816 * If sending a tty stop signal to a member of an
817 * orphaned process group, discard the signal here if
818 * the action is default; don't stop the process below
819 * if sleeping, and don't clear any pending SIGCONT.
820 */
821 if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0)
822 return;
823 }
824 }
825
826 if (prop & SA_CONT)
827 sigminusset(&stopsigmask, &p->p_sigctx.ps_siglist);
828
829 if (prop & SA_STOP)
830 sigminusset(&contsigmask, &p->p_sigctx.ps_siglist);
831
832 sigaddset(&p->p_sigctx.ps_siglist, signum);
833
834 /* CHECKSIGS() is "inlined" here. */
835 p->p_sigctx.ps_sigcheck = 1;
836
837 /*
838 * Defer further processing for signals which are held,
839 * except that stopped processes must be continued by SIGCONT.
840 */
841 if (action == SIG_HOLD && ((prop & SA_CONT) == 0 || p->p_stat != SSTOP))
842 return;
843
844 /* XXXSMP: works, but icky */
845 if (dolock)
846 SCHED_LOCK(s);
847
848 switch (p->p_stat) {
849 case SSLEEP:
850 /*
851 * If process is sleeping uninterruptibly
852 * we can't interrupt the sleep... the signal will
853 * be noticed when the process returns through
854 * trap() or syscall().
855 */
856 if ((p->p_flag & P_SINTR) == 0)
857 goto out;
858 /*
859 * Process is sleeping and traced... make it runnable
860 * so it can discover the signal in issignal() and stop
861 * for the parent.
862 */
863 if (p->p_flag & P_TRACED)
864 goto run;
865 /*
866 * If SIGCONT is default (or ignored) and process is
867 * asleep, we are finished; the process should not
868 * be awakened.
869 */
870 if ((prop & SA_CONT) && action == SIG_DFL) {
871 sigdelset(&p->p_sigctx.ps_siglist, signum);
872 goto out;
873 }
874 /*
875 * When a sleeping process receives a stop
876 * signal, process immediately if possible.
877 */
878 if ((prop & SA_STOP) && action == SIG_DFL) {
879 /*
880 * If a child holding parent blocked,
881 * stopping could cause deadlock.
882 */
883 if (p->p_flag & P_PPWAIT)
884 goto out;
885 sigdelset(&p->p_sigctx.ps_siglist, signum);
886 p->p_xstat = signum;
887 if ((p->p_pptr->p_flag & P_NOCLDSTOP) == 0) {
888 /*
889 * XXXSMP: recursive call; don't lock
890 * the second time around.
891 */
892 sched_psignal(p->p_pptr, SIGCHLD);
893 }
894 proc_stop(p); /* XXXSMP: recurse? */
895 goto out;
896 }
897 /*
898 * All other (caught or default) signals
899 * cause the process to run.
900 */
901 goto runfast;
902 /*NOTREACHED*/
903
904 case SSTOP:
905 /*
906 * If traced process is already stopped,
907 * then no further action is necessary.
908 */
909 if (p->p_flag & P_TRACED)
910 goto out;
911
912 /*
913 * Kill signal always sets processes running.
914 */
915 if (signum == SIGKILL)
916 goto runfast;
917
918 if (prop & SA_CONT) {
919 /*
920 * If SIGCONT is default (or ignored), we continue the
921 * process but don't leave the signal in p_sigctx.ps_siglist, as
922 * it has no further action. If SIGCONT is held, we
923 * continue the process and leave the signal in
924 * p_sigctx.ps_siglist. If the process catches SIGCONT, let it
925 * handle the signal itself. If it isn't waiting on
926 * an event, then it goes back to run state.
927 * Otherwise, process goes back to sleep state.
928 */
929 if (action == SIG_DFL)
930 sigdelset(&p->p_sigctx.ps_siglist, signum);
931 if (action == SIG_CATCH)
932 goto runfast;
933 if (p->p_wchan == 0)
934 goto run;
935 p->p_stat = SSLEEP;
936 goto out;
937 }
938
939 if (prop & SA_STOP) {
940 /*
941 * Already stopped, don't need to stop again.
942 * (If we did the shell could get confused.)
943 */
944 sigdelset(&p->p_sigctx.ps_siglist, signum);
945 goto out;
946 }
947
948 /*
949 * If process is sleeping interruptibly, then simulate a
950 * wakeup so that when it is continued, it will be made
951 * runnable and can look at the signal. But don't make
952 * the process runnable, leave it stopped.
953 */
954 if (p->p_wchan && p->p_flag & P_SINTR)
955 unsleep(p);
956 goto out;
957 #ifdef __HAVE_AST_PERPROC
958 case SONPROC:
959 case SRUN:
960 case SIDL:
961 /*
962 * SONPROC: We're running, notice the signal when
963 * we return back to userspace.
964 *
965 * SRUN, SIDL: Notice the signal when we run again
966 * and return to back to userspace.
967 */
968 signotify(p);
969 goto out;
970
971 default:
972 /*
973 * SDEAD, SZOMB: The signal will never be noticed.
974 */
975 goto out;
976 #else /* ! __HAVE_AST_PERPROC */
977 case SONPROC:
978 /*
979 * We're running; notice the signal.
980 */
981 signotify(p);
982 goto out;
983
984 default:
985 /*
986 * SRUN, SIDL, SDEAD, SZOMB do nothing with the signal.
987 * It will either never be noticed, or noticed very soon.
988 */
989 goto out;
990 #endif /* __HAVE_AST_PERPROC */
991 }
992 /*NOTREACHED*/
993
994 runfast:
995 /*
996 * Raise priority to at least PUSER.
997 */
998 if (p->p_priority > PUSER)
999 p->p_priority = PUSER;
1000 run:
1001 setrunnable(p); /* XXXSMP: recurse? */
1002 out:
1003 /* XXXSMP: works, but icky */
1004 if (dolock)
1005 SCHED_UNLOCK(s);
1006 }
1007
1008 static __inline int firstsig(const sigset_t *);
1009
1010 static __inline int
1011 firstsig(const sigset_t *ss)
1012 {
1013 int sig;
1014
1015 sig = ffs(ss->__bits[0]);
1016 if (sig != 0)
1017 return (sig);
1018 #if NSIG > 33
1019 sig = ffs(ss->__bits[1]);
1020 if (sig != 0)
1021 return (sig + 32);
1022 #endif
1023 #if NSIG > 65
1024 sig = ffs(ss->__bits[2]);
1025 if (sig != 0)
1026 return (sig + 64);
1027 #endif
1028 #if NSIG > 97
1029 sig = ffs(ss->__bits[3]);
1030 if (sig != 0)
1031 return (sig + 96);
1032 #endif
1033 return (0);
1034 }
1035
1036 /*
1037 * If the current process has received a signal (should be caught or cause
1038 * termination, should interrupt current syscall), return the signal number.
1039 * Stop signals with default action are processed immediately, then cleared;
1040 * they aren't returned. This is checked after each entry to the system for
1041 * a syscall or trap (though this can usually be done without calling issignal
1042 * by checking the pending signal masks in the CURSIG macro.) The normal call
1043 * sequence is
1044 *
1045 * while (signum = CURSIG(curproc))
1046 * postsig(signum);
1047 */
1048 int
1049 issignal(struct proc *p)
1050 {
1051 int s, signum, prop;
1052 int dolock = (p->p_flag & P_SINTR) == 0, locked = !dolock;
1053 sigset_t ss;
1054
1055 for (;;) {
1056 sigpending1(p, &ss);
1057 if (p->p_flag & P_PPWAIT)
1058 sigminusset(&stopsigmask, &ss);
1059 signum = firstsig(&ss);
1060 if (signum == 0) { /* no signal to send */
1061 p->p_sigctx.ps_sigcheck = 0;
1062 if (locked && dolock)
1063 SCHED_LOCK(s);
1064 return (0);
1065 }
1066 /* take the signal! */
1067 sigdelset(&p->p_sigctx.ps_siglist, signum);
1068
1069 /*
1070 * We should see pending but ignored signals
1071 * only if P_TRACED was on when they were posted.
1072 */
1073 if (sigismember(&p->p_sigctx.ps_sigignore, signum) &&
1074 (p->p_flag & P_TRACED) == 0)
1075 continue;
1076
1077 if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) {
1078 /*
1079 * If traced, always stop, and stay
1080 * stopped until released by the debugger.
1081 */
1082 p->p_xstat = signum;
1083 if ((p->p_flag & P_FSTRACE) == 0)
1084 psignal1(p->p_pptr, SIGCHLD, dolock);
1085 if (dolock)
1086 SCHED_LOCK(s);
1087 proc_stop(p);
1088 mi_switch(p);
1089 SCHED_ASSERT_UNLOCKED();
1090 if (dolock)
1091 splx(s);
1092 else
1093 dolock = 1;
1094
1095 /*
1096 * If we are no longer being traced, or the parent
1097 * didn't give us a signal, look for more signals.
1098 */
1099 if ((p->p_flag & P_TRACED) == 0 || p->p_xstat == 0)
1100 continue;
1101
1102 /*
1103 * If the new signal is being masked, look for other
1104 * signals.
1105 */
1106 signum = p->p_xstat;
1107 /*
1108 * `p->p_sigctx.ps_siglist |= mask' is done
1109 * in setrunnable().
1110 */
1111 if (sigismember(&p->p_sigctx.ps_sigmask, signum))
1112 continue;
1113 /* take the signal! */
1114 sigdelset(&p->p_sigctx.ps_siglist, signum);
1115 }
1116
1117 prop = sigprop[signum];
1118
1119 /*
1120 * Decide whether the signal should be returned.
1121 * Return the signal's number, or fall through
1122 * to clear it from the pending mask.
1123 */
1124 switch ((long)SIGACTION(p, signum).sa_handler) {
1125
1126 case (long)SIG_DFL:
1127 /*
1128 * Don't take default actions on system processes.
1129 */
1130 if (p->p_pid <= 1) {
1131 #ifdef DIAGNOSTIC
1132 /*
1133 * Are you sure you want to ignore SIGSEGV
1134 * in init? XXX
1135 */
1136 printf("Process (pid %d) got signal %d\n",
1137 p->p_pid, signum);
1138 #endif
1139 break; /* == ignore */
1140 }
1141 /*
1142 * If there is a pending stop signal to process
1143 * with default action, stop here,
1144 * then clear the signal. However,
1145 * if process is member of an orphaned
1146 * process group, ignore tty stop signals.
1147 */
1148 if (prop & SA_STOP) {
1149 if (p->p_flag & P_TRACED ||
1150 (p->p_pgrp->pg_jobc == 0 &&
1151 prop & SA_TTYSTOP))
1152 break; /* == ignore */
1153 p->p_xstat = signum;
1154 if ((p->p_pptr->p_flag & P_NOCLDSTOP) == 0)
1155 psignal1(p->p_pptr, SIGCHLD, dolock);
1156 if (dolock)
1157 SCHED_LOCK(s);
1158 proc_stop(p);
1159 mi_switch(p);
1160 SCHED_ASSERT_UNLOCKED();
1161 if (dolock)
1162 splx(s);
1163 else
1164 dolock = 1;
1165 break;
1166 } else if (prop & SA_IGNORE) {
1167 /*
1168 * Except for SIGCONT, shouldn't get here.
1169 * Default action is to ignore; drop it.
1170 */
1171 break; /* == ignore */
1172 } else
1173 goto keep;
1174 /*NOTREACHED*/
1175
1176 case (long)SIG_IGN:
1177 /*
1178 * Masking above should prevent us ever trying
1179 * to take action on an ignored signal other
1180 * than SIGCONT, unless process is traced.
1181 */
1182 if ((prop & SA_CONT) == 0 &&
1183 (p->p_flag & P_TRACED) == 0)
1184 printf("issignal\n");
1185 break; /* == ignore */
1186
1187 default:
1188 /*
1189 * This signal has an action, let
1190 * postsig() process it.
1191 */
1192 goto keep;
1193 }
1194 }
1195 /* NOTREACHED */
1196
1197 keep:
1198 /* leave the signal for later */
1199 sigaddset(&p->p_sigctx.ps_siglist, signum);
1200 CHECKSIGS(p);
1201 if (locked && dolock)
1202 SCHED_LOCK(s);
1203 return (signum);
1204 }
1205
1206 /*
1207 * Put the argument process into the stopped state and notify the parent
1208 * via wakeup. Signals are handled elsewhere. The process must not be
1209 * on the run queue.
1210 */
1211 static void
1212 proc_stop(struct proc *p)
1213 {
1214
1215 SCHED_ASSERT_LOCKED();
1216
1217 p->p_stat = SSTOP;
1218 p->p_flag &= ~P_WAITED;
1219 sched_wakeup((caddr_t)p->p_pptr);
1220 }
1221
1222 /*
1223 * Take the action for the specified signal
1224 * from the current set of pending signals.
1225 */
1226 void
1227 postsig(int signum)
1228 {
1229 struct proc *p;
1230 struct sigacts *ps;
1231 sig_t action;
1232 u_long code;
1233 sigset_t *returnmask;
1234
1235 p = curproc;
1236 ps = p->p_sigacts;
1237 #ifdef DIAGNOSTIC
1238 if (signum == 0)
1239 panic("postsig");
1240 #endif
1241
1242 KERNEL_PROC_LOCK(p);
1243
1244 sigdelset(&p->p_sigctx.ps_siglist, signum);
1245 action = SIGACTION_PS(ps, signum).sa_handler;
1246 #ifdef KTRACE
1247 if (KTRPOINT(p, KTR_PSIG))
1248 ktrpsig(p,
1249 signum, action, p->p_sigctx.ps_flags & SAS_OLDMASK ?
1250 &p->p_sigctx.ps_oldmask : &p->p_sigctx.ps_sigmask, 0);
1251 #endif
1252 if (action == SIG_DFL) {
1253 /*
1254 * Default action, where the default is to kill
1255 * the process. (Other cases were ignored above.)
1256 */
1257 sigexit(p, signum);
1258 /* NOTREACHED */
1259 } else {
1260 /*
1261 * If we get here, the signal must be caught.
1262 */
1263 #ifdef DIAGNOSTIC
1264 if (action == SIG_IGN ||
1265 sigismember(&p->p_sigctx.ps_sigmask, signum))
1266 panic("postsig action");
1267 #endif
1268 /*
1269 * Set the new mask value and also defer further
1270 * occurences of this signal.
1271 *
1272 * Special case: user has done a sigpause. Here the
1273 * current mask is not of interest, but rather the
1274 * mask from before the sigpause is what we want
1275 * restored after the signal processing is completed.
1276 */
1277 if (p->p_sigctx.ps_flags & SAS_OLDMASK) {
1278 returnmask = &p->p_sigctx.ps_oldmask;
1279 p->p_sigctx.ps_flags &= ~SAS_OLDMASK;
1280 } else
1281 returnmask = &p->p_sigctx.ps_sigmask;
1282 p->p_stats->p_ru.ru_nsignals++;
1283 if (p->p_sigctx.ps_sig != signum) {
1284 code = 0;
1285 } else {
1286 code = p->p_sigctx.ps_code;
1287 p->p_sigctx.ps_code = 0;
1288 p->p_sigctx.ps_sig = 0;
1289 }
1290 (*p->p_emul->e_sendsig)(signum, returnmask, code);
1291 (void) splsched(); /* XXXSMP */
1292 sigplusset(&SIGACTION_PS(ps, signum).sa_mask,
1293 &p->p_sigctx.ps_sigmask);
1294 if (SIGACTION_PS(ps, signum).sa_flags & SA_RESETHAND) {
1295 sigdelset(&p->p_sigctx.ps_sigcatch, signum);
1296 if (signum != SIGCONT && sigprop[signum] & SA_IGNORE)
1297 sigaddset(&p->p_sigctx.ps_sigignore, signum);
1298 SIGACTION_PS(ps, signum).sa_handler = SIG_DFL;
1299 }
1300 (void) spl0(); /* XXXSMP */
1301 }
1302
1303 KERNEL_PROC_UNLOCK(p);
1304 }
1305
1306 /*
1307 * Kill the current process for stated reason.
1308 */
1309 void
1310 killproc(struct proc *p, char *why)
1311 {
1312
1313 log(LOG_ERR, "pid %d was killed: %s\n", p->p_pid, why);
1314 uprintf("sorry, pid %d was killed: %s\n", p->p_pid, why);
1315 psignal(p, SIGKILL);
1316 }
1317
1318 /*
1319 * Force the current process to exit with the specified signal, dumping core
1320 * if appropriate. We bypass the normal tests for masked and caught signals,
1321 * allowing unrecoverable failures to terminate the process without changing
1322 * signal state. Mark the accounting record with the signal termination.
1323 * If dumping core, save the signal number for the debugger. Calls exit and
1324 * does not return.
1325 */
1326
1327 #if defined(DEBUG)
1328 int kern_logsigexit = 1; /* not static to make public for sysctl */
1329 #else
1330 int kern_logsigexit = 0; /* not static to make public for sysctl */
1331 #endif
1332
1333 static const char logcoredump[] =
1334 "pid %d (%s), uid %d: exited on signal %d (core dumped)\n";
1335 static const char lognocoredump[] =
1336 "pid %d (%s), uid %d: exited on signal %d (core not dumped, err = %d)\n";
1337
1338 void
1339 sigexit(struct proc *p, int signum)
1340 {
1341 int error, exitsig;
1342
1343 exitsig = signum;
1344 p->p_acflag |= AXSIG;
1345 if (sigprop[signum] & SA_CORE) {
1346 p->p_sigctx.ps_sig = signum;
1347 if ((error = coredump(p)) == 0)
1348 exitsig |= WCOREFLAG;
1349
1350 if (kern_logsigexit) {
1351 int uid = p->p_cred && p->p_ucred ?
1352 p->p_ucred->cr_uid : -1;
1353
1354 if (error)
1355 log(LOG_INFO, lognocoredump, p->p_pid,
1356 p->p_comm, uid, signum, error);
1357 else
1358 log(LOG_INFO, logcoredump, p->p_pid,
1359 p->p_comm, uid, signum);
1360 }
1361
1362 }
1363
1364 exit1(p, W_EXITCODE(0, exitsig));
1365 /* NOTREACHED */
1366 }
1367
1368 /*
1369 * Dump core, into a file named "progname.core" or "core" (depending on the
1370 * value of shortcorename), unless the process was setuid/setgid.
1371 */
1372 int
1373 coredump(struct proc *p)
1374 {
1375 struct vnode *vp;
1376 struct vmspace *vm;
1377 struct ucred *cred;
1378 struct nameidata nd;
1379 struct vattr vattr;
1380 int error, error1;
1381 char name[MAXPATHLEN];
1382
1383 vm = p->p_vmspace;
1384 cred = p->p_cred->pc_ucred;
1385
1386 /*
1387 * Make sure the process has not set-id, to prevent data leaks.
1388 */
1389 if (p->p_flag & P_SUGID)
1390 return (EPERM);
1391
1392 /*
1393 * Refuse to core if the data + stack + user size is larger than
1394 * the core dump limit. XXX THIS IS WRONG, because of mapped
1395 * data.
1396 */
1397 if (USPACE + ctob(vm->vm_dsize + vm->vm_ssize) >=
1398 p->p_rlimit[RLIMIT_CORE].rlim_cur)
1399 return (EFBIG); /* better error code? */
1400
1401 /*
1402 * The core dump will go in the current working directory. Make
1403 * sure that the directory is still there and that the mount flags
1404 * allow us to write core dumps there.
1405 */
1406 vp = p->p_cwdi->cwdi_cdir;
1407 if (vp->v_mount == NULL ||
1408 (vp->v_mount->mnt_flag & MNT_NOCOREDUMP) != 0)
1409 return (EPERM);
1410
1411 error = build_corename(p, name);
1412 if (error)
1413 return error;
1414
1415 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, p);
1416 error = vn_open(&nd, O_CREAT | FWRITE | FNOSYMLINK, S_IRUSR | S_IWUSR);
1417 if (error)
1418 return (error);
1419 vp = nd.ni_vp;
1420
1421 /* Don't dump to non-regular files or files with links. */
1422 if (vp->v_type != VREG ||
1423 VOP_GETATTR(vp, &vattr, cred, p) || vattr.va_nlink != 1) {
1424 error = EINVAL;
1425 goto out;
1426 }
1427 VATTR_NULL(&vattr);
1428 vattr.va_size = 0;
1429 VOP_LEASE(vp, p, cred, LEASE_WRITE);
1430 VOP_SETATTR(vp, &vattr, cred, p);
1431 p->p_acflag |= ACORE;
1432
1433 /* Now dump the actual core file. */
1434 error = (*p->p_execsw->es_coredump)(p, vp, cred);
1435 out:
1436 VOP_UNLOCK(vp, 0);
1437 error1 = vn_close(vp, FWRITE, cred, p);
1438 if (error == 0)
1439 error = error1;
1440 return (error);
1441 }
1442
1443 /*
1444 * Nonexistent system call-- signal process (may want to handle it).
1445 * Flag error in case process won't see signal immediately (blocked or ignored).
1446 */
1447 /* ARGSUSED */
1448 int
1449 sys_nosys(struct proc *p, void *v, register_t *retval)
1450 {
1451
1452 psignal(p, SIGSYS);
1453 return (ENOSYS);
1454 }
1455
1456 static int
1457 build_corename(struct proc *p, char dst[MAXPATHLEN])
1458 {
1459 const char *s;
1460 char *d, *end;
1461 int i;
1462
1463 for (s = p->p_limit->pl_corename, d = dst, end = d + MAXPATHLEN;
1464 *s != '\0'; s++) {
1465 if (*s == '%') {
1466 switch (*(s + 1)) {
1467 case 'n':
1468 i = snprintf(d, end - d, "%s", p->p_comm);
1469 break;
1470 case 'p':
1471 i = snprintf(d, end - d, "%d", p->p_pid);
1472 break;
1473 case 'u':
1474 i = snprintf(d, end - d, "%s",
1475 p->p_pgrp->pg_session->s_login);
1476 break;
1477 case 't':
1478 i = snprintf(d, end - d, "%ld",
1479 p->p_stats->p_start.tv_sec);
1480 break;
1481 default:
1482 goto copy;
1483 }
1484 d += i;
1485 s++;
1486 } else {
1487 copy: *d = *s;
1488 d++;
1489 }
1490 if (d >= end)
1491 return (ENAMETOOLONG);
1492 }
1493 *d = '\0';
1494 return (0);
1495 }
1496
1497 /*
1498 * Returns true if signal is ignored or masked for passed process.
1499 */
1500 int
1501 sigismasked(struct proc *p, int sig)
1502 {
1503
1504 return (sigismember(&p->p_sigctx.ps_sigignore, sig) ||
1505 sigismember(&p->p_sigctx.ps_sigmask, sig));
1506 }
1507