xref: /original-bsd/sys/kern/kern_proc.c (revision aba77441)
1 /*
2  * Copyright (c) 1982 Regents of the University of California.
3  * All rights reserved.  The Berkeley software License Agreement
4  * specifies the terms and conditions for redistribution.
5  *
6  *	@(#)kern_proc.c	6.6 (Berkeley) 06/08/85
7  */
8 
9 #include "../machine/reg.h"
10 #include "../machine/pte.h"
11 #include "../machine/psl.h"
12 
13 #include "param.h"
14 #include "systm.h"
15 #include "map.h"
16 #include "dir.h"
17 #include "user.h"
18 #include "kernel.h"
19 #include "proc.h"
20 #include "buf.h"
21 #include "inode.h"
22 #include "seg.h"
23 #include "acct.h"
24 #include "wait.h"
25 #include "vm.h"
26 #include "text.h"
27 #include "file.h"
28 #include "quota.h"
29 #include "uio.h"
30 #include "mbuf.h"
31 
32 /*
33  * Change the process group of top and all descendents to npgrp.
34  * If npgrp is -1, instead clear any pending stops.
35  */
36 spgrp(top, npgrp)
37 	struct proc *top;
38 {
39 	register struct proc *p;
40 	int f = 0;
41 
42 	p = top;
43 	for (;;) {
44 		if (npgrp == -1)
45 			p->p_sig &=
46 			  ~(sigmask(SIGTSTP)|sigmask(SIGTTIN)|sigmask(SIGTTOU));
47 		else
48 			p->p_pgrp = npgrp;
49 		f++;
50 		/*
51 		 * If this process has children, descend to them next,
52 		 * otherwise do any siblings, and if done with this level,
53 		 * follow back up the tree (but not past top).
54 		 */
55 		if (p->p_cptr)
56 			p = p->p_cptr;
57 		else if (p == top)
58 			return (f);
59 		else if (p->p_osptr)
60 			p = p->p_osptr;
61 		else for (;;) {
62 			p = p->p_pptr;
63 			if (p == top)
64 				return (f);
65 if (p == &proc[1])
66 	panic("spgrp");
67 			if (p->p_osptr) {
68 				p = p->p_osptr;
69 				break;
70 			}
71 		}
72 	}
73 }
74 
75 /*
76  * Is p an inferior of the current process?
77  */
78 inferior(p)
79 	register struct proc *p;
80 {
81 
82 	for (; p != u.u_procp; p = p->p_pptr)
83 		if (p->p_ppid == 0)
84 			return (0);
85 	return (1);
86 }
87 
88 struct proc *
89 pfind(pid)
90 	int pid;
91 {
92 	register struct proc *p;
93 
94 	for (p = &proc[pidhash[PIDHASH(pid)]]; p != &proc[0]; p = &proc[p->p_idhash])
95 		if (p->p_pid == pid)
96 			return (p);
97 	return ((struct proc *)0);
98 }
99 
100 /*
101  * init the process queues
102  */
103 pqinit()
104 {
105 	register struct proc *p;
106 
107 	/*
108 	 * most procs are initially on freequeue
109 	 *	nb: we place them there in their "natural" order.
110 	 */
111 
112 	freeproc = NULL;
113 	for (p = procNPROC; --p > proc; freeproc = p)
114 		p->p_nxt = freeproc;
115 
116 	/*
117 	 * but proc[0] is special ...
118 	 */
119 
120 	allproc = p;
121 	p->p_nxt = NULL;
122 	p->p_prev = &allproc;
123 
124 	zombproc = NULL;
125 }
126