xref: /386bsd/usr/src/kernel/kern/proc.c (revision dc8b130e)
1 /*
2  * Copyright (c) 1982, 1986, 1989, 1991 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * $Id: proc.c,v 1.1 94/10/20 00:03:07 bill Exp $
34  *
35  * Process hierarchy and control.
36  */
37 
38 #include "sys/param.h"
39 #include "sys/wait.h"
40 #include "sys/file.h"
41 #include "sys/ioctl.h"
42 #include "sys/errno.h"
43 #include "tty.h"
44 #include "proc.h"
45 #include "malloc.h"
46 #include "uio.h"
47 
48 /*#include "quota.h"*/
49 
50 #include "prototypes.h"
51 
52 struct proc *initproc, *pageproc, *zombproc, *allproc;
53 int whichqs;
54 struct prochd qs[NQS];
55 
56 /*
57  * Is p an inferior of the current process?
58  */
59 int
inferior(struct proc * p)60 inferior(struct proc *p)
61 {
62 
63 	for (; p != curproc; p = p->p_pptr)
64 		if (p->p_pid == 0)
65 			return (0);
66 	return (1);
67 }
68 
69 /*
70  * Locate a process group by number
71  */
72 struct pgrp *
pgfind(pid_t pgid)73 pgfind(pid_t pgid)
74 {
75 	struct pgrp *pgrp = pgrphash[PIDHASH(pgid)];
76 
77 	for (; pgrp; pgrp = pgrp->pg_hforw)
78 		if (pgrp->pg_id == pgid)
79 			return (pgrp);
80 	return ((struct pgrp *)0);
81 }
82 
83 /*
84  * Move p to a new or existing process group (and session)
85  */
86 void
enterpgrp(struct proc * p,pid_t pgid,int mksess)87 enterpgrp(struct proc *p, pid_t pgid, int mksess)
88 {
89 	struct pgrp *pgrp = pgfind(pgid);
90 	struct proc **pp;
91 	struct proc *cp;
92 	int n;
93 
94 #ifdef DIAGNOSTIC
95 	if (pgrp && mksess)	/* firewalls */
96 		panic("enterpgrp: setsid into non-empty pgrp");
97 	if (SESS_LEADER(p))
98 		panic("enterpgrp: session leader attempted setpgrp");
99 #endif
100 	if (pgrp == NULL) {
101 		/*
102 		 * new process group
103 		 */
104 #ifdef DIAGNOSTIC
105 		if (p->p_pid != pgid)
106 			panic("enterpgrp: new pgrp and pid != pgid");
107 #endif
108 		MALLOC(pgrp, struct pgrp *, sizeof(struct pgrp), M_PGRP,
109 		       M_WAITOK);
110 		if (mksess) {
111 			register struct session *sess;
112 
113 			/*
114 			 * new session
115 			 */
116 			MALLOC(sess, struct session *, sizeof(struct session),
117 				M_SESSION, M_WAITOK);
118 			sess->s_leader = p;
119 			sess->s_count = 1;
120 			sess->s_ttyvp = NULL;
121 			sess->s_ttyp = NULL;
122 			memcpy(sess->s_login, p->p_session->s_login,
123 			    sizeof(sess->s_login));
124 			p->p_flag &= ~SCTTY;
125 			pgrp->pg_session = sess;
126 #ifdef DIAGNOSTIC
127 			if (p != curproc)
128 				panic("enterpgrp: mksession and p != curproc");
129 #endif
130 		} else {
131 			pgrp->pg_session = p->p_session;
132 			pgrp->pg_session->s_count++;
133 		}
134 		pgrp->pg_id = pgid;
135 		pgrp->pg_hforw = pgrphash[n = PIDHASH(pgid)];
136 		pgrphash[n] = pgrp;
137 		pgrp->pg_jobc = 0;
138 		pgrp->pg_mem = NULL;
139 	} else if (pgrp == p->p_pgrp)
140 		return;
141 
142 	/*
143 	 * Adjust eligibility of affected pgrps to participate in job control.
144 	 * Increment eligibility counts before decrementing, otherwise we
145 	 * could reach 0 spuriously during the first call.
146 	 */
147 	fixjobc(p, pgrp, 1);
148 	fixjobc(p, p->p_pgrp, 0);
149 
150 	/*
151 	 * unlink p from old process group
152 	 */
153 	for (pp = &p->p_pgrp->pg_mem; *pp; pp = &(*pp)->p_pgrpnxt)
154 		if (*pp == p) {
155 			*pp = p->p_pgrpnxt;
156 			goto done;
157 		}
158 	panic("enterpgrp: can't find p on old pgrp");
159 done:
160 	/*
161 	 * delete old if empty
162 	 */
163 	if (p->p_pgrp->pg_mem == 0)
164 		pgdelete(p->p_pgrp);
165 	/*
166 	 * link into new one
167 	 */
168 	p->p_pgrp = pgrp;
169 	p->p_pgrpnxt = pgrp->pg_mem;
170 	pgrp->pg_mem = p;
171 }
172 
173 /*
174  * Session leader exits..
175  */
176 void
exitsessleader(struct proc * p)177 exitsessleader(struct proc *p)
178 {
179 	struct session *sp = p->p_session;
180 
181 	/* is there a controlling terminal? */
182 	if (sp->s_ttyvp) {
183 
184 		/* if we are the controlling process */
185 		if (sp->s_ttyp->t_session == sp) {
186 
187 			/* signal any foreground processes */
188 			if (sp->s_ttyp->t_pgrp)
189 				pgsignal(sp->s_ttyp->t_pgrp, SIGHUP, 1);
190 
191 			/* wait for output to drain */
192 			(void) ttywait(sp->s_ttyp);
193 
194 			/* revoke further access to terminal */
195 			vgoneall(sp->s_ttyvp);
196 		}
197 		vrele(sp->s_ttyvp);
198 		sp->s_ttyvp = NULL;
199 		/*
200 		 * s_ttyp is not zero'd; we use this to indicate
201 		 * that the session once had a controlling terminal.
202 		 * (for logging and informational purposes)
203 		 */
204 	}
205 	sp->s_leader = NULL;
206 }
207 
208 
209 /*
210  * remove process from process group
211  */
212 void
leavepgrp(struct proc * p)213 leavepgrp(struct proc *p)
214 {
215 	struct proc **pp = &p->p_pgrp->pg_mem;
216 
217 	for (; *pp; pp = &(*pp)->p_pgrpnxt)
218 		if (*pp == p) {
219 			*pp = p->p_pgrpnxt;
220 			goto done;
221 		}
222 	panic("leavepgrp: can't find p in pgrp");
223 done:
224 	if (!p->p_pgrp->pg_mem)
225 		pgdelete(p->p_pgrp);
226 	p->p_pgrp = 0;
227 }
228 
229 /*
230  * delete a process group
231  */
232 void
pgdelete(struct pgrp * pgrp)233 pgdelete(struct pgrp *pgrp)
234 {
235 	struct pgrp **pgp = &pgrphash[PIDHASH(pgrp->pg_id)];
236 
237 	if (pgrp->pg_session->s_ttyp != NULL &&
238 	    pgrp->pg_session->s_ttyp->t_pgrp == pgrp)
239 		pgrp->pg_session->s_ttyp->t_pgrp = NULL;
240 	for (; *pgp; pgp = &(*pgp)->pg_hforw)
241 		if (*pgp == pgrp) {
242 			*pgp = pgrp->pg_hforw;
243 			goto done;
244 		}
245 	panic("pgdelete: can't find pgrp on hash chain");
246 done:
247 	if (--pgrp->pg_session->s_count == 0)
248 		FREE(pgrp->pg_session, M_SESSION);
249 	FREE(pgrp, M_PGRP);
250 }
251 
252 static void orphanpg(struct pgrp *pg);
253 
254 /*
255  * Adjust pgrp jobc counters when specified process changes process group.
256  * We count the number of processes in each process group that "qualify"
257  * the group for terminal job control (those with a parent in a different
258  * process group of the same session).  If that count reaches zero, the
259  * process group becomes orphaned.  Check both the specified process'
260  * process group and that of its children.
261  * entering == 0 => p is leaving specified group.
262  * entering == 1 => p is entering specified group.
263  */
264 void
fixjobc(struct proc * p,struct pgrp * pgrp,int entering)265 fixjobc(struct proc *p, struct pgrp *pgrp, int entering)
266 {
267 	struct pgrp *hispgrp;
268 	struct session *mysession = pgrp->pg_session;
269 
270 	/*
271 	 * Check p's parent to see whether p qualifies its own process
272 	 * group; if so, adjust count for p's process group.
273 	 */
274 	if ((hispgrp = p->p_pptr->p_pgrp) != pgrp &&
275 	    hispgrp->pg_session == mysession)
276 		if (entering)
277 			pgrp->pg_jobc++;
278 		else if (--pgrp->pg_jobc == 0)
279 			orphanpg(pgrp);
280 
281 	/*
282 	 * Check this process' children to see whether they qualify
283 	 * their process groups; if so, adjust counts for children's
284 	 * process groups.
285 	 */
286 	for (p = p->p_cptr; p; p = p->p_osptr)
287 		if ((hispgrp = p->p_pgrp) != pgrp &&
288 		    hispgrp->pg_session == mysession &&
289 		    p->p_stat != SZOMB)
290 			if (entering)
291 				hispgrp->pg_jobc++;
292 			else if (--hispgrp->pg_jobc == 0)
293 				orphanpg(hispgrp);
294 }
295 
296 /*
297  * A process group has become orphaned;
298  * if there are any stopped processes in the group,
299  * hang-up all process in that group.
300  */
301 static void
orphanpg(struct pgrp * pg)302 orphanpg(struct pgrp *pg)
303 {
304 	register struct proc *p;
305 
306 	for (p = pg->pg_mem; p; p = p->p_pgrpnxt) {
307 		if (p->p_stat == SSTOP) {
308 			for (p = pg->pg_mem; p; p = p->p_pgrpnxt) {
309 				psignal(p, SIGHUP);
310 				psignal(p, SIGCONT);
311 			}
312 			return;
313 		}
314 	}
315 }
316 
317 
318 #ifdef DEBUG
319 void
pgrpdump(void)320 pgrpdump(void)
321 {
322 	register struct pgrp *pgrp;
323 	register struct proc *p;
324 	register i;
325 
326 	for (i=0; i<pidhashmask+1; i++) {
327 		if (pgrphash[i]) {
328 		  printf("\tindx %d\n", i);
329 		  for (pgrp=pgrphash[i]; pgrp; pgrp=pgrp->pg_hforw) {
330 		    printf("\tpgrp %x, pgid %d, sess %x, sesscnt %d, mem %x\n",
331 			pgrp, pgrp->pg_id, pgrp->pg_session,
332 			pgrp->pg_session->s_count, pgrp->pg_mem);
333 		    for (p=pgrp->pg_mem; p; p=p->p_pgrpnxt) {
334 			printf("\t\tpid %d addr %x pgrp %x\n",
335 				p->p_pid, p, p->p_pgrp);
336 		    }
337 		  }
338 
339 		}
340 	}
341 }
342 #endif /* DEBUG */
343 
344 /* POSIX set session ID */
setsid(p,uap,retval)345 setsid(p, uap, retval)
346 	register struct proc *p;
347 	void *uap;
348 	int *retval;
349 {
350 
351 	if (p->p_pgid == p->p_pid || pgfind(p->p_pid)) {
352 		return (EPERM);
353 	} else {
354 		enterpgrp(p, p->p_pid, 1);
355 		*retval = p->p_pid;
356 		return (0);
357 	}
358 }
359 
360 /* POSIX insert a specified process into a specified process group */
361 setpgid(curp, uap, retval)
362 	struct proc *curp;
363 	struct args {
364 		int	pid;	/* target process id */
365 		int	pgid;	/* target pgrp id */
366 	} *uap;
367 	int *retval;
368 {
369 	struct proc *targp;		/* target process */
370 	struct pgrp *pgrp;		/* target pgrp */
371 
372 	/* if target is not this process, check if it is an acceptable child */
373 	if (uap->pid != 0 && uap->pid != curp->p_pid) {
374 
375 		/* does the process exist, and is it a child of the current? */
376 		if ((targp = pfind(uap->pid)) == 0 || !inferior(targp))
377 			return (ESRCH);
378 
379 		/* is the target process in the same session as the current? */
380 		if (targp->p_session != curp->p_session)
381 			return (EPERM);
382 
383 		/* has the process done an execve()? */
384 		if (targp->p_flag&SEXEC)
385 			return (EACCES);
386 	} else
387 		targp = curp;
388 
389 	/* is the target process the session leader */
390 	if (SESS_LEADER(targp))
391 		return (EPERM);
392 
393 	/* use current processes process group, or one in current session */
394 	if (uap->pgid == 0)
395 		uap->pgid = targp->p_pid;
396 	else if (uap->pgid != targp->p_pid)
397 		if ((pgrp = pgfind(uap->pgid)) == 0 ||
398 	            pgrp->pg_session != curp->p_session)
399 			return (EPERM);
400 
401 	/* put target process into requested process group */
402 	enterpgrp(targp, uap->pgid, 0);
403 	return (0);
404 }
405 
406 /* Get process group ID; note that POSIX getpgrp takes no parameter */
407 getpgrp(p, uap, retval)
408 	struct proc *p;
409 	void *uap;
410 	int *retval;
411 {
412 
413 	*retval = p->p_pgrp->pg_id;
414 	return (0);
415 }
416 
417 /* POSIX get current processes process ID */
418 getpid(p, uap, retval)
419 	struct proc *p;
420 	void *uap;
421 	int *retval;
422 {
423 
424 	*retval = p->p_pid;
425 	return (0);
426 }
427 
428 /* POSIX get parent processes process ID */
429 getppid(p, uap, retval)
430 	struct proc *p;
431 	void *uap;
432 	int *retval;
433 {
434 
435 	*retval = p->p_pptr->p_pid;
436 	return (0);
437 }
438