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