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