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))); 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))); 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))); 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))); 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))); 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, do not resume it now. Most likely * it will be unpausing, in which case the process must remain stopped. 274 * Otherwise, it will still be resumed once the VFS call returns. If the 275 * process has died, do not resume it either. 276 */ 277 if (rmp->mp_flags & (VFS_CALL | EXITING)) 278 return; 279 280 if ((r = sys_resume(rmp->mp_endpoint)) != OK) 281 panic("sys_resume failed: %d", r); 282 283 /* Also unset the unpaused flag. We can safely assume that a stopped process 284 * need only be unpaused once, but once it is resumed, all bets are off. 285 */ 286 rmp->mp_flags &= ~(PROC_STOPPED | UNPAUSED); 287 } 288 289 /*===========================================================================* 290 * process_ksig * 291 *===========================================================================*/ 292 int process_ksig(endpoint_t proc_nr_e, int signo) 293 { 294 register struct mproc *rmp; 295 int proc_nr; 296 pid_t proc_id, id; 297 298 if(pm_isokendpt(proc_nr_e, &proc_nr) != OK) { 299 printf("PM: process_ksig: %d?? not ok\n", proc_nr_e); 300 return EDEADEPT; /* process is gone. */ 301 } 302 rmp = &mproc[proc_nr]; 303 if ((rmp->mp_flags & (IN_USE | EXITING)) != IN_USE) { 304 #if 0 305 printf("PM: process_ksig: %d?? exiting / not in use\n", proc_nr_e); 306 #endif 307 return EDEADEPT; /* process is gone. */ 308 } 309 proc_id = rmp->mp_pid; 310 mp = &mproc[0]; /* pretend signals are from PM */ 311 mp->mp_procgrp = rmp->mp_procgrp; /* get process group right */ 312 313 /* For SIGVTALRM and SIGPROF, see if we need to restart a 314 * virtual timer. For SIGINT, SIGINFO, SIGWINCH and SIGQUIT, use proc_id 0 315 * to indicate a broadcast to the recipient's process group. For 316 * SIGKILL, use proc_id -1 to indicate a systemwide broadcast. 317 */ 318 switch (signo) { 319 case SIGINT: 320 case SIGQUIT: 321 case SIGWINCH: 322 case SIGINFO: 323 id = 0; break; /* broadcast to process group */ 324 case SIGVTALRM: 325 case SIGPROF: 326 check_vtimer(proc_nr, signo); 327 /* fall-through */ 328 default: 329 id = proc_id; 330 break; 331 } 332 check_sig(id, signo, TRUE /* ksig */); 333 mp->mp_procgrp = 0; /* restore proper PM process group */ 334 335 /* If SIGSNDELAY is set, an earlier sys_stop() failed because the process was 336 * still sending, and the kernel hereby tells us that the process is now done 337 * with that. We can now try to resume what we planned to do in the first 338 * place: set up a signal handler. However, the process's message may have 339 * been a call to PM, in which case the process may have changed any of its 340 * signal settings. The process may also have forked, exited etcetera. 341 */ 342 if (signo == SIGSNDELAY && (rmp->mp_flags & DELAY_CALL)) { 343 /* When getting SIGSNDELAY, the process is stopped at least until the 344 * receipt of the SIGSNDELAY signal is acknowledged to the kernel. The 345 * process is not stopped on PROC_STOP in the kernel. However, now that 346 * there is no longer a delay call, stop_proc() is guaranteed to 347 * succeed immediately. 348 */ 349 rmp->mp_flags &= ~DELAY_CALL; 350 351 assert(!(rmp->mp_flags & PROC_STOPPED)); 352 353 /* If the delay call was to PM, it may have resulted in a VFS call. In 354 * that case, we must wait with further signal processing until VFS has 355 * replied. Stop the process. 356 */ 357 if (rmp->mp_flags & VFS_CALL) { 358 stop_proc(rmp, FALSE /*may_delay*/); 359 360 return OK; 361 } 362 363 /* Process as many normal signals as possible. */ 364 check_pending(rmp); 365 366 assert(!(rmp->mp_flags & DELAY_CALL)); 367 } 368 369 /* See if the process is still alive */ 370 if ((mproc[proc_nr].mp_flags & (IN_USE | EXITING)) == IN_USE) { 371 return OK; /* signal has been delivered */ 372 } 373 else { 374 return EDEADEPT; /* process is gone */ 375 } 376 } 377 378 /*===========================================================================* 379 * sig_proc * 380 *===========================================================================*/ 381 void sig_proc(rmp, signo, trace, ksig) 382 register struct mproc *rmp; /* pointer to the process to be signaled */ 383 int signo; /* signal to send to process (1 to _NSIG-1) */ 384 int trace; /* pass signal to tracer first? */ 385 int ksig; /* non-zero means signal comes from kernel */ 386 { 387 /* Send a signal to a process. Check to see if the signal is to be caught, 388 * ignored, tranformed into a message (for system processes) or blocked. 389 * - If the signal is to be transformed into a message, request the KERNEL to 390 * send the target process a system notification with the pending signal as an 391 * argument. 392 * - If the signal is to be caught, request the KERNEL to push a sigcontext 393 * structure and a sigframe structure onto the catcher's stack. Also, KERNEL 394 * will reset the program counter and stack pointer, so that when the process 395 * next runs, it will be executing the signal handler. When the signal handler 396 * returns, sigreturn(2) will be called. Then KERNEL will restore the signal 397 * context from the sigcontext structure. 398 * If there is insufficient stack space, kill the process. 399 */ 400 int slot, badignore; 401 402 slot = (int) (rmp - mproc); 403 if ((rmp->mp_flags & (IN_USE | EXITING)) != IN_USE) { 404 panic("PM: signal %d sent to exiting process %d\n", signo, slot); 405 } 406 407 if (trace == TRUE && rmp->mp_tracer != NO_TRACER && signo != SIGKILL) { 408 /* Signal should be passed to the debugger first. 409 * This happens before any checks on block/ignore masks; otherwise, 410 * the process itself could block/ignore debugger signals. 411 */ 412 413 sigaddset(&rmp->mp_sigtrace, signo); 414 415 if (!(rmp->mp_flags & TRACE_STOPPED)) 416 trace_stop(rmp, signo); /* a signal causes it to stop */ 417 418 return; 419 } 420 421 if (rmp->mp_flags & VFS_CALL) { 422 sigaddset(&rmp->mp_sigpending, signo); 423 if(ksig) 424 sigaddset(&rmp->mp_ksigpending, signo); 425 426 /* Process the signal once VFS replies. Stop the process in the 427 * meantime, so that it cannot make another call after the VFS reply 428 * comes in but before we look at its signals again. Since we always 429 * stop the process to deliver signals during a VFS call, the 430 * PROC_STOPPED flag doubles as an indicator in restart_sigs() that 431 * signals must be rechecked after a VFS reply comes in. 432 */ 433 if (!(rmp->mp_flags & (PROC_STOPPED | DELAY_CALL))) { 434 /* If a VFS call is ongoing and the process is not yet stopped, 435 * the process must have made a call to PM. Therefore, there 436 * can be no delay calls in this case. 437 */ 438 stop_proc(rmp, FALSE /*delay_call*/); 439 } 440 return; 441 } 442 443 /* Handle system signals for system processes first. */ 444 if(rmp->mp_flags & PRIV_PROC) { 445 /* Always skip signals for PM (only necessary when broadcasting). */ 446 if(rmp->mp_endpoint == PM_PROC_NR) { 447 return; 448 } 449 450 /* System signals have always to go through the kernel first to let it 451 * pick the right signal manager. If PM is the assigned signal manager, 452 * the signal will come back and will actually be processed. 453 */ 454 if(!ksig) { 455 sys_kill(rmp->mp_endpoint, signo); 456 return; 457 } 458 459 /* Print stacktrace if necessary. */ 460 if(SIGS_IS_STACKTRACE(signo)) { 461 sys_diagctl_stacktrace(rmp->mp_endpoint); 462 } 463 464 if(!SIGS_IS_TERMINATION(signo)) { 465 /* Translate every non-termination sys signal into a message. */ 466 message m; 467 m.m_type = SIGS_SIGNAL_RECEIVED; 468 m.m_pm_lsys_sigs_signal.num = signo; 469 asynsend3(rmp->mp_endpoint, &m, AMF_NOREPLY); 470 } 471 else { 472 /* Exit the process in case of termination system signal. */ 473 sig_proc_exit(rmp, signo); 474 } 475 return; 476 } 477 478 /* Handle user processes now. See if the signal cannot be safely ignored. */ 479 badignore = ksig && sigismember(&noign_sset, signo) && ( 480 sigismember(&rmp->mp_ignore, signo) || 481 sigismember(&rmp->mp_sigmask, signo)); 482 483 if (!badignore && sigismember(&rmp->mp_ignore, signo)) { 484 /* Signal should be ignored. */ 485 return; 486 } 487 if (!badignore && sigismember(&rmp->mp_sigmask, signo)) { 488 /* Signal should be blocked. */ 489 sigaddset(&rmp->mp_sigpending, signo); 490 if(ksig) 491 sigaddset(&rmp->mp_ksigpending, signo); 492 return; 493 } 494 495 if ((rmp->mp_flags & TRACE_STOPPED) && signo != SIGKILL) { 496 /* If the process is stopped for a debugger, do not deliver any signals 497 * (except SIGKILL) in order not to confuse the debugger. The signals 498 * will be delivered using the check_pending() calls in do_trace(). 499 */ 500 sigaddset(&rmp->mp_sigpending, signo); 501 if(ksig) 502 sigaddset(&rmp->mp_ksigpending, signo); 503 return; 504 } 505 if (!badignore && sigismember(&rmp->mp_catch, signo)) { 506 /* Signal is caught. First interrupt the process's current call, if 507 * applicable. This may involve a roundtrip to VFS, in which case we'll 508 * have to check back later. 509 */ 510 if (!unpause(rmp)) { 511 /* not yet unpaused; continue later */ 512 sigaddset(&rmp->mp_sigpending, signo); 513 if(ksig) 514 sigaddset(&rmp->mp_ksigpending, signo); 515 516 return; 517 } 518 519 /* Then send the actual signal to the process, by setting up a signal 520 * handler. 521 */ 522 if (sig_send(rmp, signo)) 523 return; 524 525 /* We were unable to spawn a signal handler. Kill the process. */ 526 printf("PM: %d can't catch signal %d - killing\n", 527 rmp->mp_pid, signo); 528 } 529 else if (!badignore && sigismember(&ign_sset, signo)) { 530 /* Signal defaults to being ignored. */ 531 return; 532 } 533 534 /* Terminate process */ 535 sig_proc_exit(rmp, signo); 536 } 537 538 /*===========================================================================* 539 * sig_proc_exit * 540 *===========================================================================*/ 541 static void sig_proc_exit(rmp, signo) 542 struct mproc *rmp; /* process that must exit */ 543 int signo; /* signal that caused termination */ 544 { 545 rmp->mp_sigstatus = (char) signo; 546 if (sigismember(&core_sset, signo)) { 547 if(!(rmp->mp_flags & PRIV_PROC)) { 548 printf("PM: coredump signal %d for %d / %s\n", signo, 549 rmp->mp_pid, rmp->mp_name); 550 sys_diagctl_stacktrace(rmp->mp_endpoint); 551 } 552 exit_proc(rmp, 0, TRUE /*dump_core*/); 553 } 554 else { 555 exit_proc(rmp, 0, FALSE /*dump_core*/); 556 } 557 } 558 559 /*===========================================================================* 560 * check_sig * 561 *===========================================================================*/ 562 int check_sig(proc_id, signo, ksig) 563 pid_t proc_id; /* pid of proc to sig, or 0 or -1, or -pgrp */ 564 int signo; /* signal to send to process (0 to _NSIG-1) */ 565 int ksig; /* non-zero means signal comes from kernel */ 566 { 567 /* Check to see if it is possible to send a signal. The signal may have to be 568 * sent to a group of processes. This routine is invoked by the KILL system 569 * call, and also when the kernel catches a DEL or other signal. 570 */ 571 572 register struct mproc *rmp; 573 int count; /* count # of signals sent */ 574 int error_code; 575 576 if (signo < 0 || signo >= _NSIG) return(EINVAL); 577 578 /* Return EINVAL for attempts to send SIGKILL to INIT alone. */ 579 if (proc_id == INIT_PID && signo == SIGKILL) return(EINVAL); 580 581 /* Signal RS first when broadcasting SIGTERM. */ 582 if (proc_id == -1 && signo == SIGTERM) 583 sys_kill(RS_PROC_NR, signo); 584 585 /* Search the proc table for processes to signal. Start from the end of the 586 * table to analyze core system processes at the end when broadcasting. 587 * (See forkexit.c about pid magic.) 588 */ 589 count = 0; 590 error_code = ESRCH; 591 for (rmp = &mproc[NR_PROCS-1]; rmp >= &mproc[0]; rmp--) { 592 if (!(rmp->mp_flags & IN_USE)) continue; 593 594 /* Check for selection. */ 595 if (proc_id > 0 && proc_id != rmp->mp_pid) continue; 596 if (proc_id == 0 && mp->mp_procgrp != rmp->mp_procgrp) continue; 597 if (proc_id == -1 && rmp->mp_pid <= INIT_PID) continue; 598 if (proc_id < -1 && rmp->mp_procgrp != -proc_id) continue; 599 600 /* Do not kill servers and drivers when broadcasting SIGKILL. */ 601 if (proc_id == -1 && signo == SIGKILL && 602 (rmp->mp_flags & PRIV_PROC)) continue; 603 604 /* Skip VM entirely as it might lead to a deadlock with its signal 605 * manager if the manager page faults at the same time. 606 */ 607 if (rmp->mp_endpoint == VM_PROC_NR) continue; 608 609 /* Disallow lethal signals sent by user processes to sys processes. */ 610 if (!ksig && SIGS_IS_LETHAL(signo) && (rmp->mp_flags & PRIV_PROC)) { 611 error_code = EPERM; 612 continue; 613 } 614 615 /* Check for permission. */ 616 if (mp->mp_effuid != SUPER_USER 617 && mp->mp_realuid != rmp->mp_realuid 618 && mp->mp_effuid != rmp->mp_realuid 619 && mp->mp_realuid != rmp->mp_effuid 620 && mp->mp_effuid != rmp->mp_effuid) { 621 error_code = EPERM; 622 continue; 623 } 624 625 count++; 626 if (signo == 0 || (rmp->mp_flags & EXITING)) continue; 627 628 /* 'sig_proc' will handle the disposition of the signal. The 629 * signal may be caught, blocked, ignored, or cause process 630 * termination, possibly with core dump. 631 */ 632 sig_proc(rmp, signo, TRUE /*trace*/, ksig); 633 634 if (proc_id > 0) break; /* only one process being signaled */ 635 } 636 637 /* If the calling process has killed itself, don't reply. */ 638 if ((mp->mp_flags & (IN_USE | EXITING)) != IN_USE) return(SUSPEND); 639 return(count > 0 ? OK : error_code); 640 } 641 642 /*===========================================================================* 643 * check_pending * 644 *===========================================================================*/ 645 void check_pending(rmp) 646 register struct mproc *rmp; 647 { 648 /* Check to see if any pending signals have been unblocked. Deliver as many 649 * of them as we can, until we have to wait for a reply from VFS first. 650 * 651 * There are several places in this file where the signal mask is 652 * changed. At each such place, check_pending() should be called to 653 * check for newly unblocked signals. 654 */ 655 int i; 656 int ksig; 657 658 for (i = 1; i < _NSIG; i++) { 659 if (sigismember(&rmp->mp_sigpending, i) && 660 !sigismember(&rmp->mp_sigmask, i)) { 661 ksig = sigismember(&rmp->mp_ksigpending, i); 662 sigdelset(&rmp->mp_sigpending, i); 663 sigdelset(&rmp->mp_ksigpending, i); 664 sig_proc(rmp, i, FALSE /*trace*/, ksig); 665 666 if (rmp->mp_flags & VFS_CALL) { 667 /* Signals must be rechecked upon return from the new 668 * VFS call, unless the process was killed. In both 669 * cases, the process is stopped. 670 */ 671 assert(rmp->mp_flags & PROC_STOPPED); 672 break; 673 } 674 } 675 } 676 } 677 678 /*===========================================================================* 679 * restart_sigs * 680 *===========================================================================*/ 681 void restart_sigs(rmp) 682 struct mproc *rmp; 683 { 684 /* VFS has replied to a request from us; do signal-related work. 685 */ 686 687 if (rmp->mp_flags & (VFS_CALL | EXITING)) return; 688 689 if (rmp->mp_flags & TRACE_EXIT) { 690 /* Tracer requested exit with specific exit value */ 691 exit_proc(rmp, rmp->mp_exitstatus, FALSE /*dump_core*/); 692 } 693 else if (rmp->mp_flags & PROC_STOPPED) { 694 /* If a signal arrives while we are performing a VFS call, the process 695 * will always be stopped immediately. Thus, if the process is stopped 696 * once the reply from VFS arrives, we might have to check signals. 697 */ 698 assert(!(rmp->mp_flags & DELAY_CALL)); 699 700 /* We saved signal(s) for after finishing a VFS call. Deal with this. 701 * PROC_STOPPED remains set to indicate the process is still stopped. 702 */ 703 check_pending(rmp); 704 705 /* Resume the process now, unless there is a reason not to. */ 706 try_resume_proc(rmp); 707 } 708 } 709 710 /*===========================================================================* 711 * unpause * 712 *===========================================================================*/ 713 static int unpause(rmp) 714 struct mproc *rmp; /* which process */ 715 { 716 /* A signal is to be sent to a process. If that process is hanging on a 717 * system call, the system call must be terminated with EINTR. First check if 718 * the process is hanging on an PM call. If not, tell VFS, so it can check for 719 * interruptible calls such as READs and WRITEs from pipes, ttys and the like. 720 */ 721 message m; 722 723 assert(!(rmp->mp_flags & VFS_CALL)); 724 725 /* If the UNPAUSED flag is set, VFS replied to an earlier unpause request. */ 726 if (rmp->mp_flags & UNPAUSED) { 727 assert((rmp->mp_flags & (DELAY_CALL | PROC_STOPPED)) == PROC_STOPPED); 728 729 return TRUE; 730 } 731 732 /* If the process is already stopping, don't do anything now. */ 733 if (rmp->mp_flags & DELAY_CALL) 734 return FALSE; 735 736 /* Check to see if process is hanging on a WAIT or SIGSUSPEND call. */ 737 if (rmp->mp_flags & (WAITING | SIGSUSPENDED)) { 738 /* Stop the process from running. Do not interrupt the actual call yet. 739 * sig_send() will interrupt the call and resume the process afterward. 740 * No delay calls: we know for a fact that the process called us. 741 */ 742 stop_proc(rmp, FALSE /*may_delay*/); 743 744 return TRUE; 745 } 746 747 /* Not paused in PM. Let VFS try to unpause the process. The process needs to 748 * be stopped for this. If it is not already stopped, try to stop it now. If 749 * that does not succeed immediately, postpone signal delivery. 750 */ 751 if (!(rmp->mp_flags & PROC_STOPPED) && !stop_proc(rmp, TRUE /*may_delay*/)) 752 return FALSE; 753 754 memset(&m, 0, sizeof(m)); 755 m.m_type = VFS_PM_UNPAUSE; 756 m.VFS_PM_ENDPT = rmp->mp_endpoint; 757 758 tell_vfs(rmp, &m); 759 760 /* Also tell VM. */ 761 vm_notify_sig_wrapper(rmp->mp_endpoint); 762 763 return FALSE; 764 } 765 766 /*===========================================================================* 767 * sig_send * 768 *===========================================================================*/ 769 static int sig_send(rmp, signo) 770 struct mproc *rmp; /* what process to spawn a signal handler in */ 771 int signo; /* signal to send to process (1 to _NSIG-1) */ 772 { 773 /* The process is supposed to catch this signal. Spawn a signal handler. 774 * Return TRUE if this succeeded, FALSE otherwise. 775 */ 776 struct sigmsg sigmsg; 777 int i, r, sigflags, slot; 778 779 assert(rmp->mp_flags & PROC_STOPPED); 780 781 sigflags = rmp->mp_sigact[signo].sa_flags; 782 slot = (int) (rmp - mproc); 783 784 if (rmp->mp_flags & SIGSUSPENDED) 785 sigmsg.sm_mask = rmp->mp_sigmask2; 786 else 787 sigmsg.sm_mask = rmp->mp_sigmask; 788 sigmsg.sm_signo = signo; 789 sigmsg.sm_sighandler = 790 (vir_bytes) rmp->mp_sigact[signo].sa_handler; 791 sigmsg.sm_sigreturn = rmp->mp_sigreturn; 792 for (i = 1; i < _NSIG; i++) { 793 if (sigismember(&rmp->mp_sigact[signo].sa_mask, i)) 794 sigaddset(&rmp->mp_sigmask, i); 795 } 796 797 if (sigflags & SA_NODEFER) 798 sigdelset(&rmp->mp_sigmask, signo); 799 else 800 sigaddset(&rmp->mp_sigmask, signo); 801 802 if (sigflags & SA_RESETHAND) { 803 sigdelset(&rmp->mp_catch, signo); 804 rmp->mp_sigact[signo].sa_handler = SIG_DFL; 805 } 806 sigdelset(&rmp->mp_sigpending, signo); 807 sigdelset(&rmp->mp_ksigpending, signo); 808 809 /* Ask the kernel to deliver the signal */ 810 r = sys_sigsend(rmp->mp_endpoint, &sigmsg); 811 /* sys_sigsend can fail legitimately with EFAULT or ENOMEM if the process 812 * memory can't accommodate the signal handler. The target process will be 813 * killed in that case, so do not bother interrupting or resuming it. 814 */ 815 if(r == EFAULT || r == ENOMEM) { 816 return(FALSE); 817 } 818 /* Other errors are unexpected pm/kernel discrepancies. */ 819 if (r != OK) { 820 panic("sys_sigsend failed: %d", r); 821 } 822 823 /* Was the process suspended in PM? Then interrupt the blocking call. */ 824 if (rmp->mp_flags & (WAITING | SIGSUSPENDED)) { 825 rmp->mp_flags &= ~(WAITING | SIGSUSPENDED); 826 827 reply(slot, EINTR); 828 829 /* The process must just have been stopped by unpause(), which means 830 * that the UNPAUSE flag is not set. 831 */ 832 assert(!(rmp->mp_flags & UNPAUSED)); 833 834 try_resume_proc(rmp); 835 836 assert(!(rmp->mp_flags & PROC_STOPPED)); 837 } else { 838 /* If the process was not suspended in PM, VFS must first have 839 * confirmed that it has tried to unsuspend any blocking call. Thus, we 840 * got here from restart_sigs() as part of handling PM_UNPAUSE_REPLY, 841 * and restart_sigs() will resume the process later. 842 */ 843 assert(rmp->mp_flags & UNPAUSED); 844 } 845 846 return(TRUE); 847 } 848 849 /*===========================================================================* 850 * vm_notify_sig_wrapper * 851 *===========================================================================*/ 852 void vm_notify_sig_wrapper(endpoint_t ep) 853 { 854 /* get IPC's endpoint, 855 * the reason that we directly get the endpoint 856 * instead of from DS server is that otherwise 857 * it will cause deadlock between PM, VM and DS. 858 */ 859 struct mproc *rmp; 860 endpoint_t ipc_ep = 0; 861 862 for (rmp = &mproc[0]; rmp < &mproc[NR_PROCS]; rmp++) { 863 if (!(rmp->mp_flags & IN_USE)) 864 continue; 865 if (!strcmp(rmp->mp_name, "ipc")) { 866 ipc_ep = rmp->mp_endpoint; 867 vm_notify_sig(ep, ipc_ep); 868 869 return; 870 } 871 } 872 } 873