1 /* This file deals with creating processes (via FORK) and deleting them (via 2 * EXIT/WAIT4). When a process forks, a new slot in the 'mproc' table is 3 * allocated for it, and a copy of the parent's core image is made for the 4 * child. Then the kernel and file system are informed. A process is removed 5 * from the 'mproc' table when two events have occurred: (1) it has exited or 6 * been killed by a signal, and (2) the parent has done a WAIT4. If the 7 * process exits first, it continues to occupy a slot until the parent does a 8 * WAIT4. 9 * 10 * The entry points into this file are: 11 * do_fork: perform the FORK system call 12 * do_srv_fork: special FORK, used by RS to create sys services 13 * do_exit: perform the EXIT system call (by calling exit_proc()) 14 * exit_proc: actually do the exiting, and tell VFS about it 15 * exit_restart: continue exiting a process after VFS has replied 16 * do_wait4: perform the WAIT4 system call 17 * wait_test: check whether a parent is waiting for a child 18 */ 19 20 #include "pm.h" 21 #include <sys/wait.h> 22 #include <assert.h> 23 #include <minix/callnr.h> 24 #include <minix/com.h> 25 #include <minix/sched.h> 26 #include <minix/vm.h> 27 #include <sys/ptrace.h> 28 #include <sys/resource.h> 29 #include <signal.h> 30 #include "mproc.h" 31 32 #define LAST_FEW 2 /* last few slots reserved for superuser */ 33 34 static void zombify(struct mproc *rmp); 35 static void check_parent(struct mproc *child, int try_cleanup); 36 static int tell_parent(struct mproc *child, vir_bytes addr); 37 static void tell_tracer(struct mproc *child); 38 static void tracer_died(struct mproc *child); 39 static void cleanup(register struct mproc *rmp); 40 41 /*===========================================================================* 42 * do_fork * 43 *===========================================================================*/ 44 int do_fork() 45 { 46 /* The process pointed to by 'mp' has forked. Create a child process. */ 47 register struct mproc *rmp; /* pointer to parent */ 48 register struct mproc *rmc; /* pointer to child */ 49 pid_t new_pid; 50 static unsigned int next_child = 0; 51 int i, n = 0, s; 52 endpoint_t child_ep; 53 message m; 54 55 /* If tables might fill up during FORK, don't even start since recovery half 56 * way through is such a nuisance. 57 */ 58 rmp = mp; 59 if ((procs_in_use == NR_PROCS) || 60 (procs_in_use >= NR_PROCS-LAST_FEW && rmp->mp_effuid != 0)) 61 { 62 printf("PM: warning, process table is full!\n"); 63 return(EAGAIN); 64 } 65 66 /* Find a slot in 'mproc' for the child process. A slot must exist. */ 67 do { 68 next_child = (next_child+1) % NR_PROCS; 69 n++; 70 } while((mproc[next_child].mp_flags & IN_USE) && n <= NR_PROCS); 71 if(n > NR_PROCS) 72 panic("do_fork can't find child slot"); 73 if(next_child >= NR_PROCS || (mproc[next_child].mp_flags & IN_USE)) 74 panic("do_fork finds wrong child slot: %d", next_child); 75 76 /* Memory part of the forking. */ 77 if((s=vm_fork(rmp->mp_endpoint, next_child, &child_ep)) != OK) { 78 return s; 79 } 80 81 /* PM may not fail fork after call to vm_fork(), as VM calls sys_fork(). */ 82 83 rmc = &mproc[next_child]; 84 /* Set up the child and its memory map; copy its 'mproc' slot from parent. */ 85 procs_in_use++; 86 *rmc = *rmp; /* copy parent's process slot to child's */ 87 rmc->mp_parent = who_p; /* record child's parent */ 88 if (!(rmc->mp_trace_flags & TO_TRACEFORK)) { 89 rmc->mp_tracer = NO_TRACER; /* no tracer attached */ 90 rmc->mp_trace_flags = 0; 91 (void) sigemptyset(&rmc->mp_sigtrace); 92 } 93 94 /* Some system servers like to call regular fork, such as RS spawning 95 * recovery scripts; in this case PM will take care of their scheduling 96 * because RS cannot do so for non-system processes */ 97 if (rmc->mp_flags & PRIV_PROC) { 98 assert(rmc->mp_scheduler == NONE); 99 rmc->mp_scheduler = SCHED_PROC_NR; 100 } 101 102 /* Inherit only these flags. In normal fork(), PRIV_PROC is not inherited. */ 103 rmc->mp_flags &= (IN_USE|DELAY_CALL|TAINTED); 104 rmc->mp_child_utime = 0; /* reset administration */ 105 rmc->mp_child_stime = 0; /* reset administration */ 106 rmc->mp_exitstatus = 0; 107 rmc->mp_sigstatus = 0; 108 rmc->mp_endpoint = child_ep; /* passed back by VM */ 109 for (i = 0; i < NR_ITIMERS; i++) 110 rmc->mp_interval[i] = 0; /* reset timer intervals */ 111 rmc->mp_started = getticks(); /* remember start time, for ps(1) */ 112 113 /* Find a free pid for the child and put it in the table. */ 114 new_pid = get_free_pid(); 115 rmc->mp_pid = new_pid; /* assign pid to child */ 116 117 memset(&m, 0, sizeof(m)); 118 m.m_type = VFS_PM_FORK; 119 m.VFS_PM_ENDPT = rmc->mp_endpoint; 120 m.VFS_PM_PENDPT = rmp->mp_endpoint; 121 m.VFS_PM_CPID = rmc->mp_pid; 122 m.VFS_PM_REUID = -1; /* Not used by VFS_PM_FORK */ 123 m.VFS_PM_REGID = -1; /* Not used by VFS_PM_FORK */ 124 125 tell_vfs(rmc, &m); 126 127 /* Tell the tracer, if any, about the new child */ 128 if (rmc->mp_tracer != NO_TRACER) 129 sig_proc(rmc, SIGSTOP, TRUE /*trace*/, FALSE /* ksig */); 130 131 /* Do not reply until VFS is ready to process the fork 132 * request 133 */ 134 return SUSPEND; 135 } 136 137 /*===========================================================================* 138 * do_srv_fork * 139 *===========================================================================*/ 140 int do_srv_fork() 141 { 142 /* The process pointed to by 'mp' has forked. Create a child process. */ 143 register struct mproc *rmp; /* pointer to parent */ 144 register struct mproc *rmc; /* pointer to child */ 145 int s; 146 pid_t new_pid; 147 static unsigned int next_child = 0; 148 int i, n = 0; 149 endpoint_t child_ep; 150 message m; 151 152 /* Only RS is allowed to use srv_fork. */ 153 if (mp->mp_endpoint != RS_PROC_NR) 154 return EPERM; 155 156 /* If tables might fill up during FORK, don't even start since recovery half 157 * way through is such a nuisance. 158 */ 159 rmp = mp; 160 if ((procs_in_use == NR_PROCS) || 161 (procs_in_use >= NR_PROCS-LAST_FEW && rmp->mp_effuid != 0)) 162 { 163 printf("PM: warning, process table is full!\n"); 164 return(EAGAIN); 165 } 166 167 /* Find a slot in 'mproc' for the child process. A slot must exist. */ 168 do { 169 next_child = (next_child+1) % NR_PROCS; 170 n++; 171 } while((mproc[next_child].mp_flags & IN_USE) && n <= NR_PROCS); 172 if(n > NR_PROCS) 173 panic("do_fork can't find child slot"); 174 if(next_child >= NR_PROCS || (mproc[next_child].mp_flags & IN_USE)) 175 panic("do_fork finds wrong child slot: %d", next_child); 176 177 if((s=vm_fork(rmp->mp_endpoint, next_child, &child_ep)) != OK) { 178 return s; 179 } 180 181 rmc = &mproc[next_child]; 182 /* Set up the child and its memory map; copy its 'mproc' slot from parent. */ 183 procs_in_use++; 184 *rmc = *rmp; /* copy parent's process slot to child's */ 185 rmc->mp_parent = who_p; /* record child's parent */ 186 if (!(rmc->mp_trace_flags & TO_TRACEFORK)) { 187 rmc->mp_tracer = NO_TRACER; /* no tracer attached */ 188 rmc->mp_trace_flags = 0; 189 (void) sigemptyset(&rmc->mp_sigtrace); 190 } 191 /* inherit only these flags */ 192 rmc->mp_flags &= (IN_USE|PRIV_PROC|DELAY_CALL); 193 rmc->mp_child_utime = 0; /* reset administration */ 194 rmc->mp_child_stime = 0; /* reset administration */ 195 rmc->mp_exitstatus = 0; 196 rmc->mp_sigstatus = 0; 197 rmc->mp_endpoint = child_ep; /* passed back by VM */ 198 rmc->mp_realuid = m_in.m_lsys_pm_srv_fork.uid; 199 rmc->mp_effuid = m_in.m_lsys_pm_srv_fork.uid; 200 rmc->mp_realgid = m_in.m_lsys_pm_srv_fork.gid; 201 rmc->mp_effgid = m_in.m_lsys_pm_srv_fork.gid; 202 for (i = 0; i < NR_ITIMERS; i++) 203 rmc->mp_interval[i] = 0; /* reset timer intervals */ 204 rmc->mp_started = getticks(); /* remember start time, for ps(1) */ 205 206 /* Find a free pid for the child and put it in the table. */ 207 new_pid = get_free_pid(); 208 rmc->mp_pid = new_pid; /* assign pid to child */ 209 210 memset(&m, 0, sizeof(m)); 211 m.m_type = VFS_PM_SRV_FORK; 212 m.VFS_PM_ENDPT = rmc->mp_endpoint; 213 m.VFS_PM_PENDPT = rmp->mp_endpoint; 214 m.VFS_PM_CPID = rmc->mp_pid; 215 m.VFS_PM_REUID = m_in.m_lsys_pm_srv_fork.uid; 216 m.VFS_PM_REGID = m_in.m_lsys_pm_srv_fork.gid; 217 218 tell_vfs(rmc, &m); 219 220 /* Tell the tracer, if any, about the new child */ 221 if (rmc->mp_tracer != NO_TRACER) 222 sig_proc(rmc, SIGSTOP, TRUE /*trace*/, FALSE /* ksig */); 223 224 /* Wakeup the newly created process */ 225 reply(rmc-mproc, OK); 226 227 return rmc->mp_pid; 228 } 229 230 /*===========================================================================* 231 * do_exit * 232 *===========================================================================*/ 233 int do_exit() 234 { 235 /* Perform the exit(status) system call. The real work is done by exit_proc(), 236 * which is also called when a process is killed by a signal. System processes 237 * do not use PM's exit() to terminate. If they try to, we warn the user 238 * and send a SIGKILL signal to the system process. 239 */ 240 if(mp->mp_flags & PRIV_PROC) { 241 printf("PM: system process %d (%s) tries to exit(), sending SIGKILL\n", 242 mp->mp_endpoint, mp->mp_name); 243 sys_kill(mp->mp_endpoint, SIGKILL); 244 } 245 else { 246 exit_proc(mp, m_in.m_lc_pm_exit.status, FALSE /*dump_core*/); 247 } 248 return(SUSPEND); /* can't communicate from beyond the grave */ 249 } 250 251 /*===========================================================================* 252 * exit_proc * 253 *===========================================================================*/ 254 void exit_proc(rmp, exit_status, dump_core) 255 register struct mproc *rmp; /* pointer to the process to be terminated */ 256 int exit_status; /* the process' exit status (for parent) */ 257 int dump_core; /* flag indicating whether to dump core */ 258 { 259 /* A process is done. Release most of the process' possessions. If its 260 * parent is waiting, release the rest, else keep the process slot and 261 * become a zombie. 262 */ 263 register int proc_nr, proc_nr_e; 264 int r; 265 pid_t procgrp; 266 clock_t user_time, sys_time; 267 message m; 268 269 /* Do not create core files for set uid execution */ 270 if (dump_core && rmp->mp_realuid != rmp->mp_effuid) 271 dump_core = FALSE; 272 273 /* System processes are destroyed before informing VFS, meaning that VFS can 274 * not get their CPU state, so we can't generate a coredump for them either. 275 */ 276 if (dump_core && (rmp->mp_flags & PRIV_PROC)) 277 dump_core = FALSE; 278 279 proc_nr = (int) (rmp - mproc); /* get process slot number */ 280 proc_nr_e = rmp->mp_endpoint; 281 282 /* Remember a session leader's process group. */ 283 procgrp = (rmp->mp_pid == mp->mp_procgrp) ? mp->mp_procgrp : 0; 284 285 /* If the exited process has a timer pending, kill it. */ 286 if (rmp->mp_flags & ALARM_ON) set_alarm(rmp, (clock_t) 0); 287 288 /* Do accounting: fetch usage times and save with dead child process. 289 * POSIX forbids accumulation at parent until child has been waited for. 290 */ 291 if((r=sys_times(proc_nr_e, &user_time, &sys_time, NULL, NULL)) != OK) 292 panic("exit_proc: sys_times failed: %d", r); 293 rmp->mp_child_utime += user_time; /* add user time */ 294 rmp->mp_child_stime += sys_time; /* add system time */ 295 296 /* Tell the kernel the process is no longer runnable to prevent it from 297 * being scheduled in between the following steps. Then tell VFS that it 298 * the process has exited and finally, clean up the process at the kernel. 299 * This order is important so that VFS can tell drivers to cancel requests 300 * such as copying to/ from the exiting process, before it is gone. 301 */ 302 /* If the process is not yet stopped, we force a stop here. This means that 303 * the process may still have a delay call pending. For this reason, the main 304 * message loop discards requests from exiting processes. 305 */ 306 if (!(rmp->mp_flags & PROC_STOPPED)) { 307 if ((r = sys_stop(proc_nr_e)) != OK) /* stop the process */ 308 panic("sys_stop failed: %d", r); 309 rmp->mp_flags |= PROC_STOPPED; 310 } 311 312 if((r=vm_willexit(proc_nr_e)) != OK) { 313 panic("exit_proc: vm_willexit failed: %d", r); 314 } 315 vm_notify_sig_wrapper(rmp->mp_endpoint); 316 if (proc_nr_e == INIT_PROC_NR) 317 { 318 printf("PM: INIT died with exit status %d; showing stacktrace\n", exit_status); 319 sys_diagctl_stacktrace(proc_nr_e); 320 return; 321 } 322 if (proc_nr_e == VFS_PROC_NR) 323 { 324 panic("exit_proc: VFS died: %d", r); 325 } 326 327 /* Tell VFS about the exiting process. */ 328 memset(&m, 0, sizeof(m)); 329 m.m_type = dump_core ? VFS_PM_DUMPCORE : VFS_PM_EXIT; 330 m.VFS_PM_ENDPT = rmp->mp_endpoint; 331 332 if (dump_core) { 333 m.VFS_PM_TERM_SIG = rmp->mp_sigstatus; 334 m.VFS_PM_PATH = rmp->mp_name; 335 } 336 337 tell_vfs(rmp, &m); 338 339 if (rmp->mp_flags & PRIV_PROC) 340 { 341 /* Destroy system processes without waiting for VFS. This is 342 * needed because the system process might be a block device 343 * driver that VFS is blocked waiting on. 344 */ 345 if((r= sys_clear(rmp->mp_endpoint)) != OK) 346 panic("exit_proc: sys_clear failed: %d", r); 347 } 348 349 /* Clean up most of the flags describing the process's state before the exit, 350 * and mark it as exiting. 351 */ 352 rmp->mp_flags &= (IN_USE|VFS_CALL|PRIV_PROC|TRACE_EXIT|PROC_STOPPED); 353 rmp->mp_flags |= EXITING; 354 355 /* Keep the process around until VFS is finished with it. */ 356 357 rmp->mp_exitstatus = (char) exit_status; 358 359 /* For normal exits, try to notify the parent as soon as possible. 360 * For core dumps, notify the parent only once the core dump has been made. 361 */ 362 if (!dump_core) 363 zombify(rmp); 364 365 /* If the process has children, disinherit them. INIT is the new parent. */ 366 for (rmp = &mproc[0]; rmp < &mproc[NR_PROCS]; rmp++) { 367 if (!(rmp->mp_flags & IN_USE)) continue; 368 if (rmp->mp_tracer == proc_nr) { 369 /* This child's tracer died. Do something sensible. */ 370 tracer_died(rmp); 371 } 372 if (rmp->mp_parent == proc_nr) { 373 /* 'rmp' now points to a child to be disinherited. */ 374 rmp->mp_parent = INIT_PROC_NR; 375 376 /* If the process is making a VFS call, remember that we set 377 * a new parent. This prevents FORK from replying to the wrong 378 * parent upon completion. 379 */ 380 if (rmp->mp_flags & VFS_CALL) 381 rmp->mp_flags |= NEW_PARENT; 382 383 /* Notify new parent. */ 384 if (rmp->mp_flags & ZOMBIE) 385 check_parent(rmp, TRUE /*try_cleanup*/); 386 } 387 } 388 389 /* Send a hangup to the process' process group if it was a session leader. */ 390 if (procgrp != 0) check_sig(-procgrp, SIGHUP, FALSE /* ksig */); 391 } 392 393 /*===========================================================================* 394 * exit_restart * 395 *===========================================================================*/ 396 void exit_restart(rmp, dump_core) 397 struct mproc *rmp; /* pointer to the process being terminated */ 398 int dump_core; /* flag indicating whether to dump core */ 399 { 400 /* VFS replied to our exit or coredump request. Perform the second half of the 401 * exit code. 402 */ 403 int r; 404 405 if((r = sched_stop(rmp->mp_scheduler, rmp->mp_endpoint)) != OK) { 406 /* If the scheduler refuses to give up scheduling, there is 407 * little we can do, except report it. This may cause problems 408 * later on, if this scheduler is asked to schedule another proc 409 * that has an endpoint->schedproc mapping identical to the proc 410 * we just tried to stop scheduling. 411 */ 412 printf("PM: The scheduler did not want to give up " 413 "scheduling %s, ret=%d.\n", rmp->mp_name, r); 414 } 415 416 /* sched_stop is either called when the process is exiting or it is 417 * being moved between schedulers. If it is being moved between 418 * schedulers, we need to set the mp_scheduler to NONE so that PM 419 * doesn't forward messages to the process' scheduler while being moved 420 * (such as sched_nice). */ 421 rmp->mp_scheduler = NONE; 422 423 /* For core dumps, now is the right time to try to contact the parent. */ 424 if (dump_core) 425 zombify(rmp); 426 427 if (!(rmp->mp_flags & PRIV_PROC)) 428 { 429 /* destroy the (user) process */ 430 if((r=sys_clear(rmp->mp_endpoint)) != OK) 431 panic("exit_restart: sys_clear failed: %d", r); 432 } 433 434 /* Release the memory occupied by the child. */ 435 if((r=vm_exit(rmp->mp_endpoint)) != OK) { 436 panic("exit_restart: vm_exit failed: %d", r); 437 } 438 439 if (rmp->mp_flags & TRACE_EXIT) 440 { 441 /* Wake up the tracer, completing the ptrace(T_EXIT) call */ 442 mproc[rmp->mp_tracer].mp_reply.m_pm_lc_ptrace.data = 0; 443 reply(rmp->mp_tracer, OK); 444 } 445 446 /* Clean up if the parent has collected the exit status */ 447 if (rmp->mp_flags & TOLD_PARENT) 448 cleanup(rmp); 449 } 450 451 /*===========================================================================* 452 * do_wait4 * 453 *===========================================================================*/ 454 int do_wait4() 455 { 456 /* A process wants to wait for a child to terminate. If a child is already 457 * waiting, go clean it up and let this WAIT4 call terminate. Otherwise, 458 * really wait. 459 * A process calling WAIT4 never gets a reply in the usual way at the end 460 * of the main loop (unless WNOHANG is set or no qualifying child exists). 461 * If a child has already exited, the routine tell_parent() sends the reply 462 * to awaken the caller. 463 */ 464 register struct mproc *rp; 465 vir_bytes addr; 466 int i, pidarg, options, children, waited_for; 467 468 /* Set internal variables. */ 469 pidarg = m_in.m_lc_pm_wait4.pid; /* 1st param */ 470 options = m_in.m_lc_pm_wait4.options; /* 3rd param */ 471 addr = m_in.m_lc_pm_wait4.addr; /* 4th param */ 472 if (pidarg == 0) pidarg = -mp->mp_procgrp; /* pidarg < 0 ==> proc grp */ 473 474 /* Is there a child waiting to be collected? At this point, pidarg != 0: 475 * pidarg > 0 means pidarg is pid of a specific process to wait for 476 * pidarg == -1 means wait for any child 477 * pidarg < -1 means wait for any child whose process group = -pidarg 478 */ 479 children = 0; 480 for (rp = &mproc[0]; rp < &mproc[NR_PROCS]; rp++) { 481 if ((rp->mp_flags & (IN_USE | TOLD_PARENT)) != IN_USE) continue; 482 if (rp->mp_parent != who_p && rp->mp_tracer != who_p) continue; 483 if (rp->mp_parent != who_p && (rp->mp_flags & ZOMBIE)) continue; 484 485 /* The value of pidarg determines which children qualify. */ 486 if (pidarg > 0 && pidarg != rp->mp_pid) continue; 487 if (pidarg < -1 && -pidarg != rp->mp_procgrp) continue; 488 489 children++; /* this child is acceptable */ 490 491 if (rp->mp_tracer == who_p) { 492 if (rp->mp_flags & TRACE_ZOMBIE) { 493 /* Traced child meets the pid test and has exited. */ 494 tell_tracer(rp); 495 check_parent(rp, TRUE /*try_cleanup*/); 496 return(SUSPEND); 497 } 498 if (rp->mp_flags & TRACE_STOPPED) { 499 /* This child meets the pid test and is being traced. 500 * Deliver a signal to the tracer, if any. 501 */ 502 for (i = 1; i < _NSIG; i++) { 503 if (sigismember(&rp->mp_sigtrace, i)) { 504 /* TODO: rusage support */ 505 506 sigdelset(&rp->mp_sigtrace, i); 507 508 mp->mp_reply.m_pm_lc_wait4.status = 509 W_STOPCODE(i); 510 return(rp->mp_pid); 511 } 512 } 513 } 514 } 515 516 if (rp->mp_parent == who_p) { 517 if (rp->mp_flags & ZOMBIE) { 518 /* This child meets the pid test and has exited. */ 519 waited_for = tell_parent(rp, addr); 520 521 if (waited_for && !(rp->mp_flags & VFS_CALL)) 522 cleanup(rp); 523 return(SUSPEND); 524 } 525 } 526 } 527 528 /* No qualifying child has exited. Wait for one, unless none exists. */ 529 if (children > 0) { 530 /* At least 1 child meets the pid test exists, but has not exited. */ 531 if (options & WNOHANG) { 532 return(0); /* parent does not want to wait */ 533 } 534 mp->mp_flags |= WAITING; /* parent wants to wait */ 535 mp->mp_wpid = (pid_t) pidarg; /* save pid for later */ 536 mp->mp_waddr = addr; /* save rusage addr for later */ 537 return(SUSPEND); /* do not reply, let it wait */ 538 } else { 539 /* No child even meets the pid test. Return error immediately. */ 540 return(ECHILD); /* no - parent has no children */ 541 } 542 } 543 544 /*===========================================================================* 545 * wait_test * 546 *===========================================================================*/ 547 int wait_test(rmp, child) 548 struct mproc *rmp; /* process that may be waiting */ 549 struct mproc *child; /* process that may be waited for */ 550 { 551 /* See if a parent or tracer process is waiting for a child process. 552 * A tracer is considered to be a pseudo-parent. 553 */ 554 int parent_waiting, right_child; 555 pid_t pidarg; 556 557 pidarg = rmp->mp_wpid; /* who's being waited for? */ 558 parent_waiting = rmp->mp_flags & WAITING; 559 right_child = /* child meets one of the 3 tests? */ 560 (pidarg == -1 || pidarg == child->mp_pid || 561 -pidarg == child->mp_procgrp); 562 563 return (parent_waiting && right_child); 564 } 565 566 /*===========================================================================* 567 * zombify * 568 *===========================================================================*/ 569 static void zombify(rmp) 570 struct mproc *rmp; 571 { 572 /* Zombify a process. First check if the exiting process is traced by a process 573 * other than its parent; if so, the tracer must be notified about the exit 574 * first. Once that is done, the real parent may be notified about the exit of 575 * its child. 576 */ 577 struct mproc *t_mp; 578 579 if (rmp->mp_flags & (TRACE_ZOMBIE | ZOMBIE)) 580 panic("zombify: process was already a zombie"); 581 582 /* See if we have to notify a tracer process first. */ 583 if (rmp->mp_tracer != NO_TRACER && rmp->mp_tracer != rmp->mp_parent) { 584 rmp->mp_flags |= TRACE_ZOMBIE; 585 586 t_mp = &mproc[rmp->mp_tracer]; 587 588 /* Do not bother sending SIGCHLD signals to tracers. */ 589 if (!wait_test(t_mp, rmp)) 590 return; 591 592 tell_tracer(rmp); 593 } 594 else { 595 rmp->mp_flags |= ZOMBIE; 596 } 597 598 /* No tracer, or tracer is parent, or tracer has now been notified. */ 599 check_parent(rmp, FALSE /*try_cleanup*/); 600 } 601 602 /*===========================================================================* 603 * check_parent * 604 *===========================================================================*/ 605 static void check_parent(child, try_cleanup) 606 struct mproc *child; /* tells which process is exiting */ 607 int try_cleanup; /* clean up the child when done? */ 608 { 609 /* We would like to inform the parent of an exiting child about the child's 610 * death. If the parent is waiting for the child, tell it immediately; 611 * otherwise, send it a SIGCHLD signal. 612 * 613 * Note that we may call this function twice on a single child; first with 614 * its original parent, later (if the parent died) with INIT as its parent. 615 */ 616 struct mproc *p_mp; 617 618 p_mp = &mproc[child->mp_parent]; 619 620 if (p_mp->mp_flags & EXITING) { 621 /* This may trigger if the child of a dead parent dies. The child will 622 * be assigned to INIT and rechecked shortly after. Do nothing. 623 */ 624 } 625 else if (wait_test(p_mp, child)) { 626 if (!tell_parent(child, p_mp->mp_waddr)) 627 try_cleanup = FALSE; /* child is still there */ 628 629 /* The 'try_cleanup' flag merely saves us from having to be really 630 * careful with statement ordering in exit_proc() and exit_restart(). 631 */ 632 if (try_cleanup && !(child->mp_flags & VFS_CALL)) 633 cleanup(child); 634 } 635 else { 636 /* Parent is not waiting. */ 637 sig_proc(p_mp, SIGCHLD, TRUE /*trace*/, FALSE /* ksig */); 638 } 639 } 640 641 /*===========================================================================* 642 * tell_parent * 643 *===========================================================================*/ 644 static int tell_parent(struct mproc *child, vir_bytes addr) 645 { 646 /* Tell the parent of the given process that it has terminated, by satisfying 647 * the parent's ongoing wait4() call. If the parent has requested the child 648 * tree's resource usage, copy that information out first. The copy may fail; 649 * in that case, the parent's wait4() call will return with an error, but the 650 * child will remain a zombie. Return TRUE if the child is cleaned up, or 651 * FALSE if the child is still a zombie. 652 */ 653 struct rusage r_usage; 654 int mp_parent; 655 struct mproc *parent; 656 int r; 657 658 mp_parent= child->mp_parent; 659 if (mp_parent <= 0) 660 panic("tell_parent: bad value in mp_parent: %d", mp_parent); 661 if(!(child->mp_flags & ZOMBIE)) 662 panic("tell_parent: child not a zombie"); 663 if(child->mp_flags & TOLD_PARENT) 664 panic("tell_parent: telling parent again"); 665 parent = &mproc[mp_parent]; 666 667 /* See if we need to report resource usage to the parent. */ 668 if (addr) { 669 /* We report only user and system times for now. TODO: support other 670 * fields, although this is tricky since the child process is already 671 * gone as far as the kernel and other services are concerned.. 672 */ 673 memset(&r_usage, 0, sizeof(r_usage)); 674 set_rusage_times(&r_usage, child->mp_child_utime, 675 child->mp_child_stime); 676 677 if ((r = sys_datacopy(SELF, (vir_bytes)&r_usage, parent->mp_endpoint, 678 addr, sizeof(r_usage))) != OK) { 679 reply(child->mp_parent, r); 680 681 return FALSE; /* copy error - the child is still there */ 682 } 683 } 684 685 /* Wake up the parent by sending the reply message. */ 686 parent->mp_reply.m_pm_lc_wait4.status = 687 W_EXITCODE(child->mp_exitstatus, child->mp_sigstatus); 688 reply(child->mp_parent, child->mp_pid); 689 parent->mp_flags &= ~WAITING; /* parent no longer waiting */ 690 child->mp_flags &= ~ZOMBIE; /* child no longer a zombie */ 691 child->mp_flags |= TOLD_PARENT; /* avoid informing parent twice */ 692 693 /* Now that the child has been waited for, accumulate the times of the 694 * terminated child process at the parent. 695 */ 696 parent->mp_child_utime += child->mp_child_utime; 697 parent->mp_child_stime += child->mp_child_stime; 698 699 return TRUE; /* child has been waited for */ 700 } 701 702 /*===========================================================================* 703 * tell_tracer * 704 *===========================================================================*/ 705 static void tell_tracer(child) 706 struct mproc *child; /* tells which process is exiting */ 707 { 708 int mp_tracer; 709 struct mproc *tracer; 710 711 mp_tracer = child->mp_tracer; 712 if (mp_tracer <= 0) 713 panic("tell_tracer: bad value in mp_tracer: %d", mp_tracer); 714 if(!(child->mp_flags & TRACE_ZOMBIE)) 715 panic("tell_tracer: child not a zombie"); 716 tracer = &mproc[mp_tracer]; 717 718 /* TODO: rusage support */ 719 720 tracer->mp_reply.m_pm_lc_wait4.status = 721 W_EXITCODE(child->mp_exitstatus, (child->mp_sigstatus & 0377)); 722 reply(child->mp_tracer, child->mp_pid); 723 tracer->mp_flags &= ~WAITING; /* tracer no longer waiting */ 724 child->mp_flags &= ~TRACE_ZOMBIE; /* child no longer zombie to tracer */ 725 child->mp_flags |= ZOMBIE; /* child is now zombie to parent */ 726 } 727 728 /*===========================================================================* 729 * tracer_died * 730 *===========================================================================*/ 731 static void tracer_died(child) 732 struct mproc *child; /* process being traced */ 733 { 734 /* The process that was tracing the given child, has died for some reason. 735 * This is really the tracer's fault, but we can't let INIT deal with this. 736 */ 737 738 child->mp_tracer = NO_TRACER; 739 child->mp_flags &= ~TRACE_EXIT; 740 741 /* If the tracer died while the child was running or stopped, we have no 742 * idea what state the child is in. Avoid a trainwreck, by killing the child. 743 * Note that this may cause cascading exits. 744 */ 745 if (!(child->mp_flags & EXITING)) { 746 sig_proc(child, SIGKILL, TRUE /*trace*/, FALSE /* ksig */); 747 748 return; 749 } 750 751 /* If the tracer died while the child was telling it about its own death, 752 * forget about the tracer and notify the real parent instead. 753 */ 754 if (child->mp_flags & TRACE_ZOMBIE) { 755 child->mp_flags &= ~TRACE_ZOMBIE; 756 child->mp_flags |= ZOMBIE; 757 758 check_parent(child, TRUE /*try_cleanup*/); 759 } 760 } 761 762 /*===========================================================================* 763 * cleanup * 764 *===========================================================================*/ 765 static void cleanup(rmp) 766 register struct mproc *rmp; /* tells which process is exiting */ 767 { 768 /* Release the process table entry and reinitialize some field. */ 769 rmp->mp_pid = 0; 770 rmp->mp_flags = 0; 771 rmp->mp_child_utime = 0; 772 rmp->mp_child_stime = 0; 773 procs_in_use--; 774 } 775 776