1 /* This file handles signals, which are asynchronous events and are generally 2 * a messy and unpleasant business. Signals can be generated by the KILL 3 * system call, or from the keyboard (SIGINT) or from the clock (SIGALRM). 4 * In all cases control eventually passes to check_sig() to see which processes 5 * can be signaled. The actual signaling is done by sig_proc(). 6 * 7 * The entry points into this file are: 8 * do_sigaction: perform the SIGACTION system call 9 * do_sigpending: perform the SIGPENDING system call 10 * do_sigprocmask: perform the SIGPROCMASK system call 11 * do_sigreturn: perform the SIGRETURN system call 12 * do_sigsuspend: perform the SIGSUSPEND system call 13 * do_kill: perform the KILL system call 14 * process_ksig: process a signal an behalf of the kernel 15 * sig_proc: interrupt or terminate a signaled process 16 * check_sig: check which processes to signal with sig_proc() 17 * check_pending: check if a pending signal can now be delivered 18 * restart_sigs: restart signal work after finishing a VFS call 19 */ 20 21 #include "pm.h" 22 #include <sys/stat.h> 23 #include <sys/ptrace.h> 24 #include <minix/callnr.h> 25 #include <minix/endpoint.h> 26 #include <minix/com.h> 27 #include <minix/vm.h> 28 #include <signal.h> 29 #include <sys/resource.h> 30 #include <assert.h> 31 #include "mproc.h" 32 33 static int unpause(struct mproc *rmp); 34 static int sig_send(struct mproc *rmp, int signo); 35 static void sig_proc_exit(struct mproc *rmp, int signo); 36 37 /*===========================================================================* 38 * do_sigaction * 39 *===========================================================================*/ 40 int do_sigaction(void) 41 { 42 int r, sig_nr; 43 struct sigaction svec; 44 struct sigaction *svp; 45 46 assert(!(mp->mp_flags & (PROC_STOPPED | VFS_CALL | UNPAUSED | EVENT_CALL))); 47 48 sig_nr = m_in.m_lc_pm_sig.nr; 49 if (sig_nr == SIGKILL) return(OK); 50 if (sig_nr < 1 || sig_nr >= _NSIG) return(EINVAL); 51 52 svp = &mp->mp_sigact[sig_nr]; 53 if (m_in.m_lc_pm_sig.oact != 0) { 54 r = sys_datacopy(PM_PROC_NR,(vir_bytes) svp, who_e, 55 m_in.m_lc_pm_sig.oact, (phys_bytes) sizeof(svec)); 56 if (r != OK) return(r); 57 } 58 59 if (m_in.m_lc_pm_sig.act == 0) 60 return(OK); 61 62 /* Read in the sigaction structure. */ 63 r = sys_datacopy(who_e, m_in.m_lc_pm_sig.act, PM_PROC_NR, (vir_bytes) &svec, 64 (phys_bytes) sizeof(svec)); 65 if (r != OK) return(r); 66 67 if (svec.sa_handler == SIG_IGN) { 68 sigaddset(&mp->mp_ignore, sig_nr); 69 sigdelset(&mp->mp_sigpending, sig_nr); 70 sigdelset(&mp->mp_ksigpending, sig_nr); 71 sigdelset(&mp->mp_catch, sig_nr); 72 } else if (svec.sa_handler == SIG_DFL) { 73 sigdelset(&mp->mp_ignore, sig_nr); 74 sigdelset(&mp->mp_catch, sig_nr); 75 } else { 76 sigdelset(&mp->mp_ignore, sig_nr); 77 sigaddset(&mp->mp_catch, sig_nr); 78 } 79 mp->mp_sigact[sig_nr].sa_handler = svec.sa_handler; 80 sigdelset(&svec.sa_mask, SIGKILL); 81 sigdelset(&svec.sa_mask, SIGSTOP); 82 mp->mp_sigact[sig_nr].sa_mask = svec.sa_mask; 83 mp->mp_sigact[sig_nr].sa_flags = svec.sa_flags; 84 mp->mp_sigreturn = m_in.m_lc_pm_sig.ret; 85 return(OK); 86 } 87 88 /*===========================================================================* 89 * do_sigpending * 90 *===========================================================================*/ 91 int do_sigpending(void) 92 { 93 assert(!(mp->mp_flags & (PROC_STOPPED | VFS_CALL | UNPAUSED | EVENT_CALL))); 94 95 mp->mp_reply.m_pm_lc_sigset.set = mp->mp_sigpending; 96 return OK; 97 } 98 99 /*===========================================================================* 100 * do_sigprocmask * 101 *===========================================================================*/ 102 int do_sigprocmask(void) 103 { 104 /* Note that the library interface passes the actual mask in sigmask_set, 105 * not a pointer to the mask, in order to save a copy. Similarly, 106 * the old mask is placed in the return message which the library 107 * interface copies (if requested) to the user specified address. 108 * 109 * The library interface must set SIG_INQUIRE if the 'act' argument 110 * is NULL. 111 * 112 * KILL and STOP can't be masked. 113 */ 114 sigset_t set; 115 int i; 116 117 assert(!(mp->mp_flags & (PROC_STOPPED | VFS_CALL | UNPAUSED | EVENT_CALL))); 118 119 set = m_in.m_lc_pm_sigset.set; 120 mp->mp_reply.m_pm_lc_sigset.set = mp->mp_sigmask; 121 122 switch (m_in.m_lc_pm_sigset.how) { 123 case SIG_BLOCK: 124 sigdelset(&set, SIGKILL); 125 sigdelset(&set, SIGSTOP); 126 for (i = 1; i < _NSIG; i++) { 127 if (sigismember(&set, i)) 128 sigaddset(&mp->mp_sigmask, i); 129 } 130 break; 131 132 case SIG_UNBLOCK: 133 for (i = 1; i < _NSIG; i++) { 134 if (sigismember(&set, i)) 135 sigdelset(&mp->mp_sigmask, i); 136 } 137 check_pending(mp); 138 break; 139 140 case SIG_SETMASK: 141 sigdelset(&set, SIGKILL); 142 sigdelset(&set, SIGSTOP); 143 mp->mp_sigmask = set; 144 check_pending(mp); 145 break; 146 147 case SIG_INQUIRE: 148 break; 149 150 default: 151 return(EINVAL); 152 break; 153 } 154 return OK; 155 } 156 157 /*===========================================================================* 158 * do_sigsuspend * 159 *===========================================================================*/ 160 int do_sigsuspend(void) 161 { 162 assert(!(mp->mp_flags & (PROC_STOPPED | VFS_CALL | UNPAUSED | EVENT_CALL))); 163 164 mp->mp_sigmask2 = mp->mp_sigmask; /* save the old mask */ 165 mp->mp_sigmask = m_in.m_lc_pm_sigset.set; 166 sigdelset(&mp->mp_sigmask, SIGKILL); 167 sigdelset(&mp->mp_sigmask, SIGSTOP); 168 mp->mp_flags |= SIGSUSPENDED; 169 check_pending(mp); 170 return(SUSPEND); 171 } 172 173 /*===========================================================================* 174 * do_sigreturn * 175 *===========================================================================*/ 176 int do_sigreturn(void) 177 { 178 /* A user signal handler is done. Restore context and check for 179 * pending unblocked signals. 180 */ 181 int r; 182 183 assert(!(mp->mp_flags & (PROC_STOPPED | VFS_CALL | UNPAUSED | EVENT_CALL))); 184 185 mp->mp_sigmask = m_in.m_lc_pm_sigset.set; 186 sigdelset(&mp->mp_sigmask, SIGKILL); 187 sigdelset(&mp->mp_sigmask, SIGSTOP); 188 189 r = sys_sigreturn(who_e, (struct sigmsg *)m_in.m_lc_pm_sigset.ctx); 190 check_pending(mp); 191 return(r); 192 } 193 194 /*===========================================================================* 195 * do_kill * 196 *===========================================================================*/ 197 int do_kill(void) 198 { 199 /* Perform the kill(pid, signo) system call. */ 200 201 return check_sig(m_in.m_lc_pm_sig.pid, m_in.m_lc_pm_sig.nr, FALSE /* ksig */); 202 } 203 204 /*===========================================================================* 205 * do_srv_kill * 206 *===========================================================================*/ 207 int do_srv_kill(void) 208 { 209 /* Perform the srv_kill(pid, signo) system call. */ 210 211 /* Only RS is allowed to use srv_kill. */ 212 if (mp->mp_endpoint != RS_PROC_NR) 213 return EPERM; 214 215 /* Pretend the signal comes from the kernel when RS wants to deliver a signal 216 * to a system process. RS sends a SIGKILL when it wants to perform cleanup. 217 * In that case, ksig == TRUE forces PM to exit the process immediately. 218 */ 219 return check_sig(m_in.m_rs_pm_srv_kill.pid, m_in.m_rs_pm_srv_kill.nr, 220 TRUE /* ksig */); 221 } 222 223 /*===========================================================================* 224 * stop_proc * 225 *===========================================================================*/ 226 static int stop_proc(struct mproc *rmp, int may_delay) 227 { 228 /* Try to stop the given process in the kernel. If successful, mark the process 229 * as stopped and return TRUE. If the process is still busy sending a message, 230 * the behavior depends on the 'may_delay' parameter. If set, the process will 231 * be marked as having a delay call pending, and the function returns FALSE. If 232 * not set, the caller already knows that the process has no delay call, and PM 233 * will panic. 234 */ 235 int r; 236 237 assert(!(rmp->mp_flags & (PROC_STOPPED | DELAY_CALL | UNPAUSED))); 238 239 r = sys_delay_stop(rmp->mp_endpoint); 240 241 /* If the process is still busy sending a message, the kernel will give us 242 * EBUSY now and send a SIGSNDELAY to the process as soon as sending is done. 243 */ 244 switch (r) { 245 case OK: 246 rmp->mp_flags |= PROC_STOPPED; 247 248 return TRUE; 249 250 case EBUSY: 251 if (!may_delay) 252 panic("stop_proc: unexpected delay call"); 253 254 rmp->mp_flags |= DELAY_CALL; 255 256 return FALSE; 257 258 default: 259 panic("sys_delay_stop failed: %d", r); 260 } 261 } 262 263 /*===========================================================================* 264 * try_resume_proc * 265 *===========================================================================*/ 266 static void try_resume_proc(struct mproc *rmp) 267 { 268 /* Resume the given process if possible. */ 269 int r; 270 271 assert(rmp->mp_flags & PROC_STOPPED); 272 273 /* If the process is blocked on a VFS call or a process event notification, 274 * do not resume it now. Most likely it will be unpausing, in which case the 275 * process must remain stopped. Otherwise, it will still be resumed once the 276 * VFS or event call is replied to. If the process has died, do not resume 277 * it either. 278 */ 279 if (rmp->mp_flags & (VFS_CALL | EVENT_CALL | EXITING)) 280 return; 281 282 if ((r = sys_resume(rmp->mp_endpoint)) != OK) 283 panic("sys_resume failed: %d", r); 284 285 /* Also unset the unpaused flag. We can safely assume that a stopped process 286 * need only be unpaused once, but once it is resumed, all bets are off. 287 */ 288 rmp->mp_flags &= ~(PROC_STOPPED | UNPAUSED); 289 } 290 291 /*===========================================================================* 292 * process_ksig * 293 *===========================================================================*/ 294 int process_ksig(endpoint_t proc_nr_e, int signo) 295 { 296 register struct mproc *rmp; 297 int proc_nr; 298 pid_t proc_id, id; 299 300 if(pm_isokendpt(proc_nr_e, &proc_nr) != OK) { 301 printf("PM: process_ksig: %d?? not ok\n", proc_nr_e); 302 return EDEADEPT; /* process is gone. */ 303 } 304 rmp = &mproc[proc_nr]; 305 if ((rmp->mp_flags & (IN_USE | EXITING)) != IN_USE) { 306 #if 0 307 printf("PM: process_ksig: %d?? exiting / not in use\n", proc_nr_e); 308 #endif 309 return EDEADEPT; /* process is gone. */ 310 } 311 proc_id = rmp->mp_pid; 312 mp = &mproc[0]; /* pretend signals are from PM */ 313 mp->mp_procgrp = rmp->mp_procgrp; /* get process group right */ 314 315 /* For SIGVTALRM and SIGPROF, see if we need to restart a 316 * virtual timer. For SIGINT, SIGINFO, SIGWINCH and SIGQUIT, use proc_id 0 317 * to indicate a broadcast to the recipient's process group. For 318 * SIGKILL, use proc_id -1 to indicate a systemwide broadcast. 319 */ 320 switch (signo) { 321 case SIGINT: 322 case SIGQUIT: 323 case SIGWINCH: 324 case SIGINFO: 325 id = 0; break; /* broadcast to process group */ 326 case SIGVTALRM: 327 case SIGPROF: 328 check_vtimer(proc_nr, signo); 329 /* fall-through */ 330 default: 331 id = proc_id; 332 break; 333 } 334 check_sig(id, signo, TRUE /* ksig */); 335 mp->mp_procgrp = 0; /* restore proper PM process group */ 336 337 /* If SIGSNDELAY is set, an earlier sys_stop() failed because the process was 338 * still sending, and the kernel hereby tells us that the process is now done 339 * with that. We can now try to resume what we planned to do in the first 340 * place: set up a signal handler. However, the process's message may have 341 * been a call to PM, in which case the process may have changed any of its 342 * signal settings. The process may also have forked, exited etcetera. 343 */ 344 if (signo == SIGSNDELAY && (rmp->mp_flags & DELAY_CALL)) { 345 /* When getting SIGSNDELAY, the process is stopped at least until the 346 * receipt of the SIGSNDELAY signal is acknowledged to the kernel. The 347 * process is not stopped on PROC_STOP in the kernel. However, now that 348 * there is no longer a delay call, stop_proc() is guaranteed to 349 * succeed immediately. 350 */ 351 rmp->mp_flags &= ~DELAY_CALL; 352 353 assert(!(rmp->mp_flags & PROC_STOPPED)); 354 355 /* If the delay call was to PM, it may have resulted in a VFS call. In 356 * that case, we must wait with further signal processing until VFS has 357 * replied. Stop the process. 358 */ 359 if (rmp->mp_flags & (VFS_CALL | EVENT_CALL)) { 360 stop_proc(rmp, FALSE /*may_delay*/); 361 362 return OK; 363 } 364 365 /* Process as many normal signals as possible. */ 366 check_pending(rmp); 367 368 assert(!(rmp->mp_flags & DELAY_CALL)); 369 } 370 371 /* See if the process is still alive */ 372 if ((mproc[proc_nr].mp_flags & (IN_USE | EXITING)) == IN_USE) { 373 return OK; /* signal has been delivered */ 374 } 375 else { 376 return EDEADEPT; /* process is gone */ 377 } 378 } 379 380 /*===========================================================================* 381 * sig_proc * 382 *===========================================================================*/ 383 void 384 sig_proc( 385 register struct mproc *rmp, /* pointer to the process to be signaled */ 386 int signo, /* signal to send to process (1 to _NSIG-1) */ 387 int trace, /* pass signal to tracer first? */ 388 int ksig /* non-zero means signal comes from kernel */ 389 ) 390 { 391 /* Send a signal to a process. Check to see if the signal is to be caught, 392 * ignored, tranformed into a message (for system processes) or blocked. 393 * - If the signal is to be transformed into a message, request the KERNEL to 394 * send the target process a system notification with the pending signal as an 395 * argument. 396 * - If the signal is to be caught, request the KERNEL to push a sigcontext 397 * structure and a sigframe structure onto the catcher's stack. Also, KERNEL 398 * will reset the program counter and stack pointer, so that when the process 399 * next runs, it will be executing the signal handler. When the signal handler 400 * returns, sigreturn(2) will be called. Then KERNEL will restore the signal 401 * context from the sigcontext structure. 402 * If there is insufficient stack space, kill the process. 403 */ 404 int slot, badignore; 405 406 slot = (int) (rmp - mproc); 407 if ((rmp->mp_flags & (IN_USE | EXITING)) != IN_USE) { 408 panic("PM: signal %d sent to exiting process %d\n", signo, slot); 409 } 410 411 if (trace == TRUE && rmp->mp_tracer != NO_TRACER && signo != SIGKILL) { 412 /* Signal should be passed to the debugger first. 413 * This happens before any checks on block/ignore masks; otherwise, 414 * the process itself could block/ignore debugger signals. 415 */ 416 417 sigaddset(&rmp->mp_sigtrace, signo); 418 419 if (!(rmp->mp_flags & TRACE_STOPPED)) 420 trace_stop(rmp, signo); /* a signal causes it to stop */ 421 422 return; 423 } 424 425 if (rmp->mp_flags & (VFS_CALL | EVENT_CALL)) { 426 sigaddset(&rmp->mp_sigpending, signo); 427 if(ksig) 428 sigaddset(&rmp->mp_ksigpending, signo); 429 430 /* Process the signal once VFS and process event subscribers reply. 431 * Stop the process in the meantime, so that it cannot make another 432 * call after the VFS reply comes in but before we look at its signals 433 * again. Since we always stop the process to deliver signals during a 434 * VFS or event call, the PROC_STOPPED flag doubles as an indicator in 435 * restart_sigs() that signals must be rechecked after a reply arrives. 436 */ 437 if (!(rmp->mp_flags & (PROC_STOPPED | DELAY_CALL))) { 438 /* If a VFS call is ongoing and the process is not yet stopped, 439 * the process must have made a call to PM. Therefore, there 440 * can be no delay calls in this case. 441 */ 442 stop_proc(rmp, FALSE /*delay_call*/); 443 } 444 return; 445 } 446 447 /* Handle system signals for system processes first. */ 448 if(rmp->mp_flags & PRIV_PROC) { 449 /* Always skip signals for PM (only necessary when broadcasting). */ 450 if(rmp->mp_endpoint == PM_PROC_NR) { 451 return; 452 } 453 454 /* System signals have always to go through the kernel first to let it 455 * pick the right signal manager. If PM is the assigned signal manager, 456 * the signal will come back and will actually be processed. 457 */ 458 if(!ksig) { 459 sys_kill(rmp->mp_endpoint, signo); 460 return; 461 } 462 463 /* Print stacktrace if necessary. */ 464 if(SIGS_IS_STACKTRACE(signo)) { 465 sys_diagctl_stacktrace(rmp->mp_endpoint); 466 } 467 468 if(!SIGS_IS_TERMINATION(signo)) { 469 /* Translate every non-termination sys signal into a message. */ 470 message m; 471 m.m_type = SIGS_SIGNAL_RECEIVED; 472 m.m_pm_lsys_sigs_signal.num = signo; 473 asynsend3(rmp->mp_endpoint, &m, AMF_NOREPLY); 474 } 475 else { 476 /* Exit the process in case of termination system signal. */ 477 sig_proc_exit(rmp, signo); 478 } 479 return; 480 } 481 482 /* Handle user processes now. See if the signal cannot be safely ignored. */ 483 badignore = ksig && sigismember(&noign_sset, signo) && ( 484 sigismember(&rmp->mp_ignore, signo) || 485 sigismember(&rmp->mp_sigmask, signo)); 486 487 if (!badignore && sigismember(&rmp->mp_ignore, signo)) { 488 /* Signal should be ignored. */ 489 return; 490 } 491 if (!badignore && sigismember(&rmp->mp_sigmask, signo)) { 492 /* Signal should be blocked. */ 493 sigaddset(&rmp->mp_sigpending, signo); 494 if(ksig) 495 sigaddset(&rmp->mp_ksigpending, signo); 496 return; 497 } 498 499 if ((rmp->mp_flags & TRACE_STOPPED) && signo != SIGKILL) { 500 /* If the process is stopped for a debugger, do not deliver any signals 501 * (except SIGKILL) in order not to confuse the debugger. The signals 502 * will be delivered using the check_pending() calls in do_trace(). 503 */ 504 sigaddset(&rmp->mp_sigpending, signo); 505 if(ksig) 506 sigaddset(&rmp->mp_ksigpending, signo); 507 return; 508 } 509 if (!badignore && sigismember(&rmp->mp_catch, signo)) { 510 /* Signal is caught. First interrupt the process's current call, if 511 * applicable. This may involve a roundtrip to VFS, in which case we'll 512 * have to check back later. 513 */ 514 if (!unpause(rmp)) { 515 /* not yet unpaused; continue later */ 516 sigaddset(&rmp->mp_sigpending, signo); 517 if(ksig) 518 sigaddset(&rmp->mp_ksigpending, signo); 519 520 return; 521 } 522 523 /* Then send the actual signal to the process, by setting up a signal 524 * handler. 525 */ 526 if (sig_send(rmp, signo)) 527 return; 528 529 /* We were unable to spawn a signal handler. Kill the process. */ 530 printf("PM: %d can't catch signal %d - killing\n", 531 rmp->mp_pid, signo); 532 } 533 else if (!badignore && sigismember(&ign_sset, signo)) { 534 /* Signal defaults to being ignored. */ 535 return; 536 } 537 538 /* Terminate process */ 539 sig_proc_exit(rmp, signo); 540 } 541 542 /*===========================================================================* 543 * sig_proc_exit * 544 *===========================================================================*/ 545 static void 546 sig_proc_exit( 547 struct mproc *rmp, /* process that must exit */ 548 int signo /* signal that caused termination */ 549 ) 550 { 551 rmp->mp_sigstatus = (char) signo; 552 if (sigismember(&core_sset, signo)) { 553 if(!(rmp->mp_flags & PRIV_PROC)) { 554 printf("PM: coredump signal %d for %d / %s\n", signo, 555 rmp->mp_pid, rmp->mp_name); 556 sys_diagctl_stacktrace(rmp->mp_endpoint); 557 } 558 exit_proc(rmp, 0, TRUE /*dump_core*/); 559 } 560 else { 561 exit_proc(rmp, 0, FALSE /*dump_core*/); 562 } 563 } 564 565 /*===========================================================================* 566 * check_sig * 567 *===========================================================================*/ 568 int check_sig(proc_id, signo, ksig) 569 pid_t proc_id; /* pid of proc to sig, or 0 or -1, or -pgrp */ 570 int signo; /* signal to send to process (0 to _NSIG-1) */ 571 int ksig; /* non-zero means signal comes from kernel */ 572 { 573 /* Check to see if it is possible to send a signal. The signal may have to be 574 * sent to a group of processes. This routine is invoked by the KILL system 575 * call, and also when the kernel catches a DEL or other signal. 576 */ 577 578 register struct mproc *rmp; 579 int count; /* count # of signals sent */ 580 int error_code; 581 582 if (signo < 0 || signo >= _NSIG) return(EINVAL); 583 584 /* Return EINVAL for attempts to send SIGKILL to INIT alone. */ 585 if (proc_id == INIT_PID && signo == SIGKILL) return(EINVAL); 586 587 /* Signal RS first when broadcasting SIGTERM. */ 588 if (proc_id == -1 && signo == SIGTERM) 589 sys_kill(RS_PROC_NR, signo); 590 591 /* Search the proc table for processes to signal. Start from the end of the 592 * table to analyze core system processes at the end when broadcasting. 593 * (See forkexit.c about pid magic.) 594 */ 595 count = 0; 596 error_code = ESRCH; 597 for (rmp = &mproc[NR_PROCS-1]; rmp >= &mproc[0]; rmp--) { 598 if (!(rmp->mp_flags & IN_USE)) continue; 599 600 /* Check for selection. */ 601 if (proc_id > 0 && proc_id != rmp->mp_pid) continue; 602 if (proc_id == 0 && mp->mp_procgrp != rmp->mp_procgrp) continue; 603 if (proc_id == -1 && rmp->mp_pid <= INIT_PID) continue; 604 if (proc_id < -1 && rmp->mp_procgrp != -proc_id) continue; 605 606 /* Do not kill servers and drivers when broadcasting SIGKILL. */ 607 if (proc_id == -1 && signo == SIGKILL && 608 (rmp->mp_flags & PRIV_PROC)) continue; 609 610 /* Skip VM entirely as it might lead to a deadlock with its signal 611 * manager if the manager page faults at the same time. 612 */ 613 if (rmp->mp_endpoint == VM_PROC_NR) continue; 614 615 /* Disallow lethal signals sent by user processes to sys processes. */ 616 if (!ksig && SIGS_IS_LETHAL(signo) && (rmp->mp_flags & PRIV_PROC)) { 617 error_code = EPERM; 618 continue; 619 } 620 621 /* Check for permission. */ 622 if (mp->mp_effuid != SUPER_USER 623 && mp->mp_realuid != rmp->mp_realuid 624 && mp->mp_effuid != rmp->mp_realuid 625 && mp->mp_realuid != rmp->mp_effuid 626 && mp->mp_effuid != rmp->mp_effuid) { 627 error_code = EPERM; 628 continue; 629 } 630 631 count++; 632 if (signo == 0 || (rmp->mp_flags & EXITING)) continue; 633 634 /* 'sig_proc' will handle the disposition of the signal. The 635 * signal may be caught, blocked, ignored, or cause process 636 * termination, possibly with core dump. 637 */ 638 sig_proc(rmp, signo, TRUE /*trace*/, ksig); 639 640 if (proc_id > 0) break; /* only one process being signaled */ 641 } 642 643 /* If the calling process has killed itself, don't reply. */ 644 if ((mp->mp_flags & (IN_USE | EXITING)) != IN_USE) return(SUSPEND); 645 return(count > 0 ? OK : error_code); 646 } 647 648 /*===========================================================================* 649 * check_pending * 650 *===========================================================================*/ 651 void 652 check_pending(register struct mproc *rmp) 653 { 654 /* Check to see if any pending signals have been unblocked. Deliver as many 655 * of them as we can, until we have to wait for a reply from VFS first. 656 * 657 * There are several places in this file where the signal mask is 658 * changed. At each such place, check_pending() should be called to 659 * check for newly unblocked signals. 660 */ 661 int i; 662 int ksig; 663 664 for (i = 1; i < _NSIG; i++) { 665 if (sigismember(&rmp->mp_sigpending, i) && 666 !sigismember(&rmp->mp_sigmask, i)) { 667 ksig = sigismember(&rmp->mp_ksigpending, i); 668 sigdelset(&rmp->mp_sigpending, i); 669 sigdelset(&rmp->mp_ksigpending, i); 670 sig_proc(rmp, i, FALSE /*trace*/, ksig); 671 672 if (rmp->mp_flags & (VFS_CALL | EVENT_CALL)) { 673 /* Signals must be rechecked upon return from the new 674 * VFS call, unless the process was killed. In both 675 * cases, the process is stopped. 676 */ 677 assert(rmp->mp_flags & PROC_STOPPED); 678 break; 679 } 680 } 681 } 682 } 683 684 /*===========================================================================* 685 * restart_sigs * 686 *===========================================================================*/ 687 void 688 restart_sigs(struct mproc *rmp) 689 { 690 /* VFS has replied to a request from us; do signal-related work. 691 */ 692 693 if (rmp->mp_flags & (VFS_CALL | EVENT_CALL | EXITING)) return; 694 695 if (rmp->mp_flags & TRACE_EXIT) { 696 /* Tracer requested exit with specific exit value */ 697 exit_proc(rmp, rmp->mp_exitstatus, FALSE /*dump_core*/); 698 } 699 else if (rmp->mp_flags & PROC_STOPPED) { 700 /* If a signal arrives while we are performing a VFS call, the process 701 * will always be stopped immediately. Thus, if the process is stopped 702 * once the reply from VFS arrives, we might have to check signals. 703 */ 704 assert(!(rmp->mp_flags & DELAY_CALL)); 705 706 /* We saved signal(s) for after finishing a VFS call. Deal with this. 707 * PROC_STOPPED remains set to indicate the process is still stopped. 708 */ 709 check_pending(rmp); 710 711 /* Resume the process now, unless there is a reason not to. */ 712 try_resume_proc(rmp); 713 } 714 } 715 716 /*===========================================================================* 717 * unpause * 718 *===========================================================================*/ 719 static int 720 unpause( 721 struct mproc *rmp /* which process */ 722 ) 723 { 724 /* A signal is to be sent to a process. If that process is hanging on a 725 * system call, the system call must be terminated with EINTR. First check if 726 * the process is hanging on an PM call. If not, tell VFS, so it can check for 727 * interruptible calls such as READs and WRITEs from pipes, ttys and the like. 728 */ 729 message m; 730 731 assert(!(rmp->mp_flags & (VFS_CALL | EVENT_CALL))); 732 733 /* If the UNPAUSED flag is set, VFS replied to an earlier unpause request. */ 734 if (rmp->mp_flags & UNPAUSED) { 735 assert((rmp->mp_flags & (DELAY_CALL | PROC_STOPPED)) == PROC_STOPPED); 736 737 return TRUE; 738 } 739 740 /* If the process is already stopping, don't do anything now. */ 741 if (rmp->mp_flags & DELAY_CALL) 742 return FALSE; 743 744 /* Check to see if process is hanging on a WAIT or SIGSUSPEND call. */ 745 if (rmp->mp_flags & (WAITING | SIGSUSPENDED)) { 746 /* Stop the process from running. Do not interrupt the actual call yet. 747 * sig_send() will interrupt the call and resume the process afterward. 748 * No delay calls: we know for a fact that the process called us. 749 */ 750 stop_proc(rmp, FALSE /*may_delay*/); 751 752 return TRUE; 753 } 754 755 /* Not paused in PM. Let VFS, and after that any matching process event 756 * subscribers, try to unpause the process. The process needs to be stopped 757 * for this. If it is not already stopped, try to stop it now. If that does 758 * not succeed immediately, postpone signal delivery. 759 */ 760 if (!(rmp->mp_flags & PROC_STOPPED) && !stop_proc(rmp, TRUE /*may_delay*/)) 761 return FALSE; 762 763 memset(&m, 0, sizeof(m)); 764 m.m_type = VFS_PM_UNPAUSE; 765 m.VFS_PM_ENDPT = rmp->mp_endpoint; 766 767 tell_vfs(rmp, &m); 768 769 return FALSE; 770 } 771 772 /*===========================================================================* 773 * sig_send * 774 *===========================================================================*/ 775 static int 776 sig_send( 777 struct mproc *rmp, /* what process to spawn a signal handler in */ 778 int signo /* signal to send to process (1 to _NSIG-1) */ 779 ) 780 { 781 /* The process is supposed to catch this signal. Spawn a signal handler. 782 * Return TRUE if this succeeded, FALSE otherwise. 783 */ 784 struct sigmsg sigmsg; 785 int i, r, sigflags, slot; 786 787 assert(rmp->mp_flags & PROC_STOPPED); 788 789 sigflags = rmp->mp_sigact[signo].sa_flags; 790 slot = (int) (rmp - mproc); 791 792 if (rmp->mp_flags & SIGSUSPENDED) 793 sigmsg.sm_mask = rmp->mp_sigmask2; 794 else 795 sigmsg.sm_mask = rmp->mp_sigmask; 796 sigmsg.sm_signo = signo; 797 sigmsg.sm_sighandler = 798 (vir_bytes) rmp->mp_sigact[signo].sa_handler; 799 sigmsg.sm_sigreturn = rmp->mp_sigreturn; 800 for (i = 1; i < _NSIG; i++) { 801 if (sigismember(&rmp->mp_sigact[signo].sa_mask, i)) 802 sigaddset(&rmp->mp_sigmask, i); 803 } 804 805 if (sigflags & SA_NODEFER) 806 sigdelset(&rmp->mp_sigmask, signo); 807 else 808 sigaddset(&rmp->mp_sigmask, signo); 809 810 if (sigflags & SA_RESETHAND) { 811 sigdelset(&rmp->mp_catch, signo); 812 rmp->mp_sigact[signo].sa_handler = SIG_DFL; 813 } 814 sigdelset(&rmp->mp_sigpending, signo); 815 sigdelset(&rmp->mp_ksigpending, signo); 816 817 /* Ask the kernel to deliver the signal */ 818 r = sys_sigsend(rmp->mp_endpoint, &sigmsg); 819 /* sys_sigsend can fail legitimately with EFAULT or ENOMEM if the process 820 * memory can't accommodate the signal handler. The target process will be 821 * killed in that case, so do not bother interrupting or resuming it. 822 */ 823 if(r == EFAULT || r == ENOMEM) { 824 return(FALSE); 825 } 826 /* Other errors are unexpected pm/kernel discrepancies. */ 827 if (r != OK) { 828 panic("sys_sigsend failed: %d", r); 829 } 830 831 /* Was the process suspended in PM? Then interrupt the blocking call. */ 832 if (rmp->mp_flags & (WAITING | SIGSUSPENDED)) { 833 rmp->mp_flags &= ~(WAITING | SIGSUSPENDED); 834 835 reply(slot, EINTR); 836 837 /* The process must just have been stopped by unpause(), which means 838 * that the UNPAUSE flag is not set. 839 */ 840 assert(!(rmp->mp_flags & UNPAUSED)); 841 842 try_resume_proc(rmp); 843 844 assert(!(rmp->mp_flags & PROC_STOPPED)); 845 } else { 846 /* If the process was not suspended in PM, VFS must first have 847 * confirmed that it has tried to unsuspend any blocking call. Thus, we 848 * got here from restart_sigs() as part of handling PM_UNPAUSE_REPLY, 849 * and restart_sigs() will resume the process later. 850 */ 851 assert(rmp->mp_flags & UNPAUSED); 852 } 853 854 return(TRUE); 855 } 856