VM: static data structure for mem allocation
[minix3.git] / servers / pm / forkexit.c
blob345f386c6a09d28fbf0bb5e74cc16f90a308baa8
1 /* This file deals with creating processes (via FORK) and deleting them (via
2 * EXIT/WAIT). 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 WAIT. If the process
7 * exits first, it continues to occupy a slot until the parent does a WAIT.
9 * The entry points into this file are:
10 * do_fork: perform the FORK system call
11 * do_srv_fork: special FORK, used by RS to create sys services
12 * do_exit: perform the EXIT system call (by calling exit_proc())
13 * exit_proc: actually do the exiting, and tell VFS about it
14 * exit_restart: continue exiting a process after VFS has replied
15 * do_waitpid: perform the WAITPID or WAIT system call
16 * wait_test: check whether a parent is waiting for a child
19 #include "pm.h"
20 #include <sys/wait.h>
21 #include <assert.h>
22 #include <minix/callnr.h>
23 #include <minix/com.h>
24 #include <minix/sched.h>
25 #include <minix/vm.h>
26 #include <sys/ptrace.h>
27 #include <sys/resource.h>
28 #include <signal.h>
29 #include "mproc.h"
30 #include "param.h"
32 #define LAST_FEW 2 /* last few slots reserved for superuser */
34 static void zombify(struct mproc *rmp);
35 static void check_parent(struct mproc *child, int try_cleanup);
36 static void tell_parent(struct mproc *child);
37 static void tell_tracer(struct mproc *child);
38 static void tracer_died(struct mproc *child);
39 static void cleanup(register struct mproc *rmp);
41 /*===========================================================================*
42 * do_fork *
43 *===========================================================================*/
44 int do_fork()
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;
55 /* If tables might fill up during FORK, don't even start since recovery half
56 * way through is such a nuisance.
58 rmp = mp;
59 if ((procs_in_use == NR_PROCS) ||
60 (procs_in_use >= NR_PROCS-LAST_FEW && rmp->mp_effuid != 0))
62 printf("PM: warning, process table is full!\n");
63 return(EAGAIN);
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);
76 /* Memory part of the forking. */
77 if((s=vm_fork(rmp->mp_endpoint, next_child, &child_ep)) != OK) {
78 printf("PM: vm_fork failed: %d\n", s);
79 return s;
82 /* PM may not fail fork after call to vm_fork(), as VM calls sys_fork(). */
84 rmc = &mproc[next_child];
85 /* Set up the child and its memory map; copy its 'mproc' slot from parent. */
86 procs_in_use++;
87 *rmc = *rmp; /* copy parent's process slot to child's */
88 rmc->mp_parent = who_p; /* record child's parent */
89 if (!(rmc->mp_trace_flags & TO_TRACEFORK)) {
90 rmc->mp_tracer = NO_TRACER; /* no tracer attached */
91 rmc->mp_trace_flags = 0;
92 (void) sigemptyset(&rmc->mp_sigtrace);
95 /* Some system servers like to call regular fork, such as RS spawning
96 * recovery scripts; in this case PM will take care of their scheduling
97 * because RS cannot do so for non-system processes */
98 if (rmc->mp_flags & PRIV_PROC) {
99 assert(rmc->mp_scheduler == NONE);
100 rmc->mp_scheduler = SCHED_PROC_NR;
103 /* Inherit only these flags. In normal fork(), PRIV_PROC is not inherited. */
104 rmc->mp_flags &= (IN_USE|DELAY_CALL|TAINTED);
105 rmc->mp_child_utime = 0; /* reset administration */
106 rmc->mp_child_stime = 0; /* reset administration */
107 rmc->mp_exitstatus = 0;
108 rmc->mp_sigstatus = 0;
109 rmc->mp_endpoint = child_ep; /* passed back by VM */
110 for (i = 0; i < NR_ITIMERS; i++)
111 rmc->mp_interval[i] = 0; /* reset timer intervals */
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 */
117 m.m_type = PM_FORK;
118 m.PM_PROC = rmc->mp_endpoint;
119 m.PM_PPROC = rmp->mp_endpoint;
120 m.PM_CPID = rmc->mp_pid;
121 m.PM_REUID = -1; /* Not used by PM_FORK */
122 m.PM_REGID = -1; /* Not used by PM_FORK */
124 tell_vfs(rmc, &m);
126 #if USE_TRACE
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 #endif /* USE_TRACE */
132 /* Do not reply until VFS is ready to process the fork
133 * request
135 return SUSPEND;
138 /*===========================================================================*
139 * do_srv_fork *
140 *===========================================================================*/
141 int do_srv_fork()
143 /* The process pointed to by 'mp' has forked. Create a child process. */
144 register struct mproc *rmp; /* pointer to parent */
145 register struct mproc *rmc; /* pointer to child */
146 int s;
147 pid_t new_pid;
148 static unsigned int next_child = 0;
149 int i, n = 0;
150 endpoint_t child_ep;
151 message m;
153 /* Only RS is allowed to use srv_fork. */
154 if (mp->mp_endpoint != RS_PROC_NR)
155 return EPERM;
157 /* If tables might fill up during FORK, don't even start since recovery half
158 * way through is such a nuisance.
160 rmp = mp;
161 if ((procs_in_use == NR_PROCS) ||
162 (procs_in_use >= NR_PROCS-LAST_FEW && rmp->mp_effuid != 0))
164 printf("PM: warning, process table is full!\n");
165 return(EAGAIN);
168 /* Find a slot in 'mproc' for the child process. A slot must exist. */
169 do {
170 next_child = (next_child+1) % NR_PROCS;
171 n++;
172 } while((mproc[next_child].mp_flags & IN_USE) && n <= NR_PROCS);
173 if(n > NR_PROCS)
174 panic("do_fork can't find child slot");
175 if(next_child >= NR_PROCS || (mproc[next_child].mp_flags & IN_USE))
176 panic("do_fork finds wrong child slot: %d", next_child);
178 if((s=vm_fork(rmp->mp_endpoint, next_child, &child_ep)) != OK) {
179 printf("PM: vm_fork failed: %d\n", s);
180 return s;
183 rmc = &mproc[next_child];
184 /* Set up the child and its memory map; copy its 'mproc' slot from parent. */
185 procs_in_use++;
186 *rmc = *rmp; /* copy parent's process slot to child's */
187 rmc->mp_parent = who_p; /* record child's parent */
188 if (!(rmc->mp_trace_flags & TO_TRACEFORK)) {
189 rmc->mp_tracer = NO_TRACER; /* no tracer attached */
190 rmc->mp_trace_flags = 0;
191 (void) sigemptyset(&rmc->mp_sigtrace);
193 /* inherit only these flags */
194 rmc->mp_flags &= (IN_USE|PRIV_PROC|DELAY_CALL);
195 rmc->mp_child_utime = 0; /* reset administration */
196 rmc->mp_child_stime = 0; /* reset administration */
197 rmc->mp_exitstatus = 0;
198 rmc->mp_sigstatus = 0;
199 rmc->mp_endpoint = child_ep; /* passed back by VM */
200 rmc->mp_realuid = (uid_t) m_in.m1_i1;
201 rmc->mp_effuid = (uid_t) m_in.m1_i1;
202 rmc->mp_realgid = (uid_t) m_in.m1_i2;
203 rmc->mp_effgid = (uid_t) m_in.m1_i2;
204 for (i = 0; i < NR_ITIMERS; i++)
205 rmc->mp_interval[i] = 0; /* reset timer intervals */
207 /* Find a free pid for the child and put it in the table. */
208 new_pid = get_free_pid();
209 rmc->mp_pid = new_pid; /* assign pid to child */
211 m.m_type = PM_SRV_FORK;
212 m.PM_PROC = rmc->mp_endpoint;
213 m.PM_PPROC = rmp->mp_endpoint;
214 m.PM_CPID = rmc->mp_pid;
215 m.PM_REUID = m_in.m1_i1;
216 m.PM_REGID = m_in.m1_i2;
218 tell_vfs(rmc, &m);
220 #if USE_TRACE
221 /* Tell the tracer, if any, about the new child */
222 if (rmc->mp_tracer != NO_TRACER)
223 sig_proc(rmc, SIGSTOP, TRUE /*trace*/, FALSE /* ksig */);
224 #endif /* USE_TRACE */
226 /* Wakeup the newly created process */
227 setreply(rmc-mproc, OK);
229 return rmc->mp_pid;
232 /*===========================================================================*
233 * do_exit *
234 *===========================================================================*/
235 int do_exit()
237 /* Perform the exit(status) system call. The real work is done by exit_proc(),
238 * which is also called when a process is killed by a signal. System processes
239 * do not use PM's exit() to terminate. If they try to, we warn the user
240 * and send a SIGKILL signal to the system process.
242 if(mp->mp_flags & PRIV_PROC) {
243 printf("PM: system process %d (%s) tries to exit(), sending SIGKILL\n",
244 mp->mp_endpoint, mp->mp_name);
245 sys_kill(mp->mp_endpoint, SIGKILL);
247 else {
248 exit_proc(mp, m_in.status, FALSE /*dump_core*/);
250 return(SUSPEND); /* can't communicate from beyond the grave */
253 /*===========================================================================*
254 * exit_proc *
255 *===========================================================================*/
256 void exit_proc(rmp, exit_status, dump_core)
257 register struct mproc *rmp; /* pointer to the process to be terminated */
258 int exit_status; /* the process' exit status (for parent) */
259 int dump_core; /* flag indicating whether to dump core */
261 /* A process is done. Release most of the process' possessions. If its
262 * parent is waiting, release the rest, else keep the process slot and
263 * become a zombie.
265 register int proc_nr, proc_nr_e;
266 int r;
267 pid_t procgrp;
268 struct mproc *p_mp;
269 clock_t user_time, sys_time;
270 message m;
272 /* Do not create core files for set uid execution */
273 if (dump_core && rmp->mp_realuid != rmp->mp_effuid)
274 dump_core = FALSE;
276 /* System processes are destroyed before informing VFS, meaning that VFS can
277 * not get their CPU state, so we can't generate a coredump for them either.
279 if (dump_core && (rmp->mp_flags & PRIV_PROC))
280 dump_core = FALSE;
282 proc_nr = (int) (rmp - mproc); /* get process slot number */
283 proc_nr_e = rmp->mp_endpoint;
285 /* Remember a session leader's process group. */
286 procgrp = (rmp->mp_pid == mp->mp_procgrp) ? mp->mp_procgrp : 0;
288 /* If the exited process has a timer pending, kill it. */
289 if (rmp->mp_flags & ALARM_ON) set_alarm(rmp, (clock_t) 0);
291 /* Do accounting: fetch usage times and accumulate at parent. */
292 if((r=sys_times(proc_nr_e, &user_time, &sys_time, NULL, NULL)) != OK)
293 panic("exit_proc: sys_times failed: %d", r);
295 p_mp = &mproc[rmp->mp_parent]; /* process' parent */
296 p_mp->mp_child_utime += user_time + rmp->mp_child_utime; /* add user time */
297 p_mp->mp_child_stime += sys_time + rmp->mp_child_stime; /* add system time */
299 /* Tell the kernel the process is no longer runnable to prevent it from
300 * being scheduled in between the following steps. Then tell VFS that it
301 * the process has exited and finally, clean up the process at the kernel.
302 * This order is important so that VFS can tell drivers to cancel requests
303 * such as copying to/ from the exiting process, before it is gone.
305 if ((r = sys_stop(proc_nr_e)) != OK) /* stop the process */
306 panic("sys_stop failed: %d", r);
308 if((r=vm_willexit(proc_nr_e)) != OK) {
309 panic("exit_proc: vm_willexit failed: %d", r);
311 vm_notify_sig_wrapper(rmp->mp_endpoint);
312 if (proc_nr_e == INIT_PROC_NR)
314 printf("PM: INIT died\n");
315 return;
317 if (proc_nr_e == VFS_PROC_NR)
319 panic("exit_proc: VFS died: %d", r);
322 /* Tell VFS about the exiting process. */
323 m.m_type = dump_core ? PM_DUMPCORE : PM_EXIT;
324 m.PM_PROC = rmp->mp_endpoint;
325 m.PM_TRACED_PROC = rmp->mp_endpoint;
327 if (dump_core) {
328 m.PM_TERM_SIG = rmp->mp_sigstatus;
329 m.PM_PATH = rmp->mp_name;
332 tell_vfs(rmp, &m);
334 if (rmp->mp_flags & PRIV_PROC)
336 /* Destroy system processes without waiting for VFS. This is
337 * needed because the system process might be a block device
338 * driver that VFS is blocked waiting on.
340 if((r= sys_clear(rmp->mp_endpoint)) != OK)
341 panic("exit_proc: sys_clear failed: %d", r);
344 /* Clean up most of the flags describing the process's state before the exit,
345 * and mark it as exiting.
347 rmp->mp_flags &= (IN_USE|VFS_CALL|PRIV_PROC|TRACE_EXIT);
348 rmp->mp_flags |= EXITING;
350 /* Keep the process around until VFS is finished with it. */
352 rmp->mp_exitstatus = (char) exit_status;
354 /* For normal exits, try to notify the parent as soon as possible.
355 * For core dumps, notify the parent only once the core dump has been made.
357 if (!dump_core)
358 zombify(rmp);
360 /* If the process has children, disinherit them. INIT is the new parent. */
361 for (rmp = &mproc[0]; rmp < &mproc[NR_PROCS]; rmp++) {
362 if (!(rmp->mp_flags & IN_USE)) continue;
363 #if USE_TRACE
364 if (rmp->mp_tracer == proc_nr) {
365 /* This child's tracer died. Do something sensible. */
366 tracer_died(rmp);
368 #endif /* USE_TRACE */
369 if (rmp->mp_parent == proc_nr) {
370 /* 'rmp' now points to a child to be disinherited. */
371 rmp->mp_parent = INIT_PROC_NR;
373 /* Notify new parent. */
374 if (rmp->mp_flags & ZOMBIE)
375 check_parent(rmp, TRUE /*try_cleanup*/);
379 /* Send a hangup to the process' process group if it was a session leader. */
380 if (procgrp != 0) check_sig(-procgrp, SIGHUP, FALSE /* ksig */);
383 /*===========================================================================*
384 * exit_restart *
385 *===========================================================================*/
386 void exit_restart(rmp, dump_core)
387 struct mproc *rmp; /* pointer to the process being terminated */
388 int dump_core; /* flag indicating whether to dump core */
390 /* VFS replied to our exit or coredump request. Perform the second half of the
391 * exit code.
393 int r;
395 if((r = sched_stop(rmp->mp_scheduler, rmp->mp_endpoint)) != OK) {
396 /* If the scheduler refuses to give up scheduling, there is
397 * little we can do, except report it. This may cause problems
398 * later on, if this scheduler is asked to schedule another proc
399 * that has an endpoint->schedproc mapping identical to the proc
400 * we just tried to stop scheduling.
402 printf("PM: The scheduler did not want to give up "
403 "scheduling %s, ret=%d.\n", rmp->mp_name, r);
406 /* sched_stop is either called when the process is exiting or it is
407 * being moved between schedulers. If it is being moved between
408 * schedulers, we need to set the mp_scheduler to NONE so that PM
409 * doesn't forward messages to the process' scheduler while being moved
410 * (such as sched_nice). */
411 rmp->mp_scheduler = NONE;
413 /* For core dumps, now is the right time to try to contact the parent. */
414 if (dump_core)
415 zombify(rmp);
417 if (!(rmp->mp_flags & PRIV_PROC))
419 /* destroy the (user) process */
420 if((r=sys_clear(rmp->mp_endpoint)) != OK)
421 panic("exit_restart: sys_clear failed: %d", r);
424 /* Release the memory occupied by the child. */
425 if((r=vm_exit(rmp->mp_endpoint)) != OK) {
426 panic("exit_restart: vm_exit failed: %d", r);
429 #if USE_TRACE
430 if (rmp->mp_flags & TRACE_EXIT)
432 /* Wake up the tracer, completing the ptrace(T_EXIT) call */
433 mproc[rmp->mp_tracer].mp_reply.reply_trace = 0;
434 setreply(rmp->mp_tracer, OK);
436 #endif /* USE_TRACE */
438 /* Clean up if the parent has collected the exit status */
439 if (rmp->mp_flags & TOLD_PARENT)
440 cleanup(rmp);
443 /*===========================================================================*
444 * do_waitpid *
445 *===========================================================================*/
446 int do_waitpid()
448 /* A process wants to wait for a child to terminate. If a child is already
449 * waiting, go clean it up and let this WAIT call terminate. Otherwise,
450 * really wait.
451 * A process calling WAIT never gets a reply in the usual way at the end
452 * of the main loop (unless WNOHANG is set or no qualifying child exists).
453 * If a child has already exited, the routine tell_parent() sends the reply
454 * to awaken the caller.
455 * Both WAIT and WAITPID are handled by this code.
457 register struct mproc *rp;
458 int i, pidarg, options, children;
460 /* Set internal variables, depending on whether this is WAIT or WAITPID. */
461 pidarg = (call_nr == WAIT ? -1 : m_in.pid); /* 1st param of waitpid */
462 options = (call_nr == WAIT ? 0 : m_in.sig_nr); /* 3rd param of waitpid */
463 if (pidarg == 0) pidarg = -mp->mp_procgrp; /* pidarg < 0 ==> proc grp */
465 /* Is there a child waiting to be collected? At this point, pidarg != 0:
466 * pidarg > 0 means pidarg is pid of a specific process to wait for
467 * pidarg == -1 means wait for any child
468 * pidarg < -1 means wait for any child whose process group = -pidarg
470 children = 0;
471 for (rp = &mproc[0]; rp < &mproc[NR_PROCS]; rp++) {
472 if ((rp->mp_flags & (IN_USE | TOLD_PARENT)) != IN_USE) continue;
473 if (rp->mp_parent != who_p && rp->mp_tracer != who_p) continue;
474 if (rp->mp_parent != who_p && (rp->mp_flags & ZOMBIE)) continue;
476 /* The value of pidarg determines which children qualify. */
477 if (pidarg > 0 && pidarg != rp->mp_pid) continue;
478 if (pidarg < -1 && -pidarg != rp->mp_procgrp) continue;
480 children++; /* this child is acceptable */
482 #if USE_TRACE
483 if (rp->mp_tracer == who_p) {
484 if (rp->mp_flags & TRACE_ZOMBIE) {
485 /* Traced child meets the pid test and has exited. */
486 tell_tracer(rp);
487 check_parent(rp, TRUE /*try_cleanup*/);
488 return(SUSPEND);
490 if (rp->mp_flags & STOPPED) {
491 /* This child meets the pid test and is being traced.
492 * Deliver a signal to the tracer, if any.
494 for (i = 1; i < _NSIG; i++) {
495 if (sigismember(&rp->mp_sigtrace, i)) {
496 sigdelset(&rp->mp_sigtrace, i);
498 mp->mp_reply.reply_res2 =
499 0177 | (i << 8);
500 return(rp->mp_pid);
505 #endif /* USE_TRACE */
507 if (rp->mp_parent == who_p) {
508 if (rp->mp_flags & ZOMBIE) {
509 /* This child meets the pid test and has exited. */
510 tell_parent(rp); /* this child has already exited */
511 if (!(rp->mp_flags & VFS_CALL))
512 cleanup(rp);
513 return(SUSPEND);
518 /* No qualifying child has exited. Wait for one, unless none exists. */
519 if (children > 0) {
520 /* At least 1 child meets the pid test exists, but has not exited. */
521 if (options & WNOHANG) {
522 return(0); /* parent does not want to wait */
524 mp->mp_flags |= WAITING; /* parent wants to wait */
525 mp->mp_wpid = (pid_t) pidarg; /* save pid for later */
526 return(SUSPEND); /* do not reply, let it wait */
527 } else {
528 /* No child even meets the pid test. Return error immediately. */
529 return(ECHILD); /* no - parent has no children */
533 /*===========================================================================*
534 * wait_test *
535 *===========================================================================*/
536 int wait_test(rmp, child)
537 struct mproc *rmp; /* process that may be waiting */
538 struct mproc *child; /* process that may be waited for */
540 /* See if a parent or tracer process is waiting for a child process.
541 * A tracer is considered to be a pseudo-parent.
543 int parent_waiting, right_child;
544 pid_t pidarg;
546 pidarg = rmp->mp_wpid; /* who's being waited for? */
547 parent_waiting = rmp->mp_flags & WAITING;
548 right_child = /* child meets one of the 3 tests? */
549 (pidarg == -1 || pidarg == child->mp_pid ||
550 -pidarg == child->mp_procgrp);
552 return (parent_waiting && right_child);
555 /*===========================================================================*
556 * zombify *
557 *===========================================================================*/
558 static void zombify(rmp)
559 struct mproc *rmp;
561 /* Zombify a process. First check if the exiting process is traced by a process
562 * other than its parent; if so, the tracer must be notified about the exit
563 * first. Once that is done, the real parent may be notified about the exit of
564 * its child.
566 struct mproc *t_mp;
568 if (rmp->mp_flags & (TRACE_ZOMBIE | ZOMBIE))
569 panic("zombify: process was already a zombie");
571 /* See if we have to notify a tracer process first. */
572 if (rmp->mp_tracer != NO_TRACER && rmp->mp_tracer != rmp->mp_parent) {
573 #if USE_TRACE
574 rmp->mp_flags |= TRACE_ZOMBIE;
576 t_mp = &mproc[rmp->mp_tracer];
578 /* Do not bother sending SIGCHLD signals to tracers. */
579 if (!wait_test(t_mp, rmp))
580 return;
582 tell_tracer(rmp);
583 #endif /* USE_TRACE */
585 else {
586 rmp->mp_flags |= ZOMBIE;
589 /* No tracer, or tracer is parent, or tracer has now been notified. */
590 check_parent(rmp, FALSE /*try_cleanup*/);
593 /*===========================================================================*
594 * check_parent *
595 *===========================================================================*/
596 static void check_parent(child, try_cleanup)
597 struct mproc *child; /* tells which process is exiting */
598 int try_cleanup; /* clean up the child when done? */
600 /* We would like to inform the parent of an exiting child about the child's
601 * death. If the parent is waiting for the child, tell it immediately;
602 * otherwise, send it a SIGCHLD signal.
604 * Note that we may call this function twice on a single child; first with
605 * its original parent, later (if the parent died) with INIT as its parent.
607 struct mproc *p_mp;
609 p_mp = &mproc[child->mp_parent];
611 if (p_mp->mp_flags & EXITING) {
612 /* This may trigger if the child of a dead parent dies. The child will
613 * be assigned to INIT and rechecked shortly after. Do nothing.
616 else if (wait_test(p_mp, child)) {
617 tell_parent(child);
619 /* The 'try_cleanup' flag merely saves us from having to be really
620 * careful with statement ordering in exit_proc() and exit_restart().
622 if (try_cleanup && !(child->mp_flags & VFS_CALL))
623 cleanup(child);
625 else {
626 /* Parent is not waiting. */
627 sig_proc(p_mp, SIGCHLD, TRUE /*trace*/, FALSE /* ksig */);
631 /*===========================================================================*
632 * tell_parent *
633 *===========================================================================*/
634 static void tell_parent(child)
635 register struct mproc *child; /* tells which process is exiting */
637 int exitstatus, mp_parent;
638 struct mproc *parent;
640 mp_parent= child->mp_parent;
641 if (mp_parent <= 0)
642 panic("tell_parent: bad value in mp_parent: %d", mp_parent);
643 if(!(child->mp_flags & ZOMBIE))
644 panic("tell_parent: child not a zombie");
645 if(child->mp_flags & TOLD_PARENT)
646 panic("tell_parent: telling parent again");
647 parent = &mproc[mp_parent];
649 /* Wake up the parent by sending the reply message. */
650 exitstatus = (child->mp_exitstatus << 8) | (child->mp_sigstatus & 0377);
651 parent->mp_reply.reply_res2 = exitstatus;
652 setreply(child->mp_parent, child->mp_pid);
653 parent->mp_flags &= ~WAITING; /* parent no longer waiting */
654 child->mp_flags &= ~ZOMBIE; /* child no longer a zombie */
655 child->mp_flags |= TOLD_PARENT; /* avoid informing parent twice */
658 #if USE_TRACE
659 /*===========================================================================*
660 * tell_tracer *
661 *===========================================================================*/
662 static void tell_tracer(child)
663 struct mproc *child; /* tells which process is exiting */
665 int exitstatus, mp_tracer;
666 struct mproc *tracer;
668 mp_tracer = child->mp_tracer;
669 if (mp_tracer <= 0)
670 panic("tell_tracer: bad value in mp_tracer: %d", mp_tracer);
671 if(!(child->mp_flags & TRACE_ZOMBIE))
672 panic("tell_tracer: child not a zombie");
673 tracer = &mproc[mp_tracer];
675 exitstatus = (child->mp_exitstatus << 8) | (child->mp_sigstatus & 0377);
676 tracer->mp_reply.reply_res2 = exitstatus;
677 setreply(child->mp_tracer, child->mp_pid);
678 tracer->mp_flags &= ~WAITING; /* tracer no longer waiting */
679 child->mp_flags &= ~TRACE_ZOMBIE; /* child no longer zombie to tracer */
680 child->mp_flags |= ZOMBIE; /* child is now zombie to parent */
683 /*===========================================================================*
684 * tracer_died *
685 *===========================================================================*/
686 static void tracer_died(child)
687 struct mproc *child; /* process being traced */
689 /* The process that was tracing the given child, has died for some reason.
690 * This is really the tracer's fault, but we can't let INIT deal with this.
693 child->mp_tracer = NO_TRACER;
694 child->mp_flags &= ~TRACE_EXIT;
696 /* If the tracer died while the child was running or stopped, we have no
697 * idea what state the child is in. Avoid a trainwreck, by killing the child.
698 * Note that this may cause cascading exits.
700 if (!(child->mp_flags & EXITING)) {
701 sig_proc(child, SIGKILL, TRUE /*trace*/, FALSE /* ksig */);
703 return;
706 /* If the tracer died while the child was telling it about its own death,
707 * forget about the tracer and notify the real parent instead.
709 if (child->mp_flags & TRACE_ZOMBIE) {
710 child->mp_flags &= ~TRACE_ZOMBIE;
711 child->mp_flags |= ZOMBIE;
713 check_parent(child, TRUE /*try_cleanup*/);
716 #endif /* USE_TRACE */
718 /*===========================================================================*
719 * cleanup *
720 *===========================================================================*/
721 static void cleanup(rmp)
722 register struct mproc *rmp; /* tells which process is exiting */
724 /* Release the process table entry and reinitialize some field. */
725 rmp->mp_pid = 0;
726 rmp->mp_flags = 0;
727 rmp->mp_child_utime = 0;
728 rmp->mp_child_stime = 0;
729 procs_in_use--;