xref: /freebsd/sys/kern/kern_proc.c (revision 82d9ae4e32fad7f55867e1e63054aef03b0a5fc8)
1df8bae1dSRodney W. Grimes /*
2df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1986, 1989, 1991, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  *
5df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
6df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
7df8bae1dSRodney W. Grimes  * are met:
8df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
9df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
10df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
11df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
12df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
13df8bae1dSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
14df8bae1dSRodney W. Grimes  *    must display the following acknowledgement:
15df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
16df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
17df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
18df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
19df8bae1dSRodney W. Grimes  *    without specific prior written permission.
20df8bae1dSRodney W. Grimes  *
21df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
32df8bae1dSRodney W. Grimes  *
33b75356e1SJeffrey Hsu  *	@(#)kern_proc.c	8.7 (Berkeley) 2/14/95
34c3aac50fSPeter Wemm  * $FreeBSD$
35df8bae1dSRodney W. Grimes  */
36df8bae1dSRodney W. Grimes 
37df8bae1dSRodney W. Grimes #include <sys/param.h>
38df8bae1dSRodney W. Grimes #include <sys/systm.h>
39df8bae1dSRodney W. Grimes #include <sys/kernel.h>
40972f9b20SPoul-Henning Kamp #include <sys/sysctl.h>
41df8bae1dSRodney W. Grimes #include <sys/malloc.h>
42b9df5231SPoul-Henning Kamp #include <sys/proc.h>
4362d6ce3aSDon Lewis #include <sys/filedesc.h>
44df8bae1dSRodney W. Grimes #include <sys/tty.h>
45bb56ec4aSPoul-Henning Kamp #include <sys/signalvar.h>
46efeaf95aSDavid Greenman #include <vm/vm.h>
47996c772fSJohn Dyson #include <sys/lock.h>
48efeaf95aSDavid Greenman #include <vm/pmap.h>
49efeaf95aSDavid Greenman #include <vm/vm_map.h>
50efeaf95aSDavid Greenman #include <sys/user.h>
512d8acc0fSJohn Dyson #include <vm/vm_zone.h>
52df8bae1dSRodney W. Grimes 
53a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_PGRP, "pgrp", "process group header");
54a1c995b6SPoul-Henning Kamp MALLOC_DEFINE(M_SESSION, "session", "session header");
55876a94eeSBruce Evans static MALLOC_DEFINE(M_PROC, "proc", "Proc structures");
56a1c995b6SPoul-Henning Kamp MALLOC_DEFINE(M_SUBPROC, "subproc", "Proc sub-structures");
5755166637SPoul-Henning Kamp 
5887b6de2bSPoul-Henning Kamp static void pgdelete	__P((struct pgrp *));
5926f9a767SRodney W. Grimes 
60df8bae1dSRodney W. Grimes /*
61df8bae1dSRodney W. Grimes  * Structure associated with user cacheing.
62df8bae1dSRodney W. Grimes  */
63b75356e1SJeffrey Hsu struct uidinfo {
64e3975643SJake Burkholder 	LIST_ENTRY(uidinfo) ui_hash;
65df8bae1dSRodney W. Grimes 	uid_t	ui_uid;
66df8bae1dSRodney W. Grimes 	long	ui_proccnt;
67ecf72308SBrian Feldman 	rlim_t	ui_sbsize;
68b75356e1SJeffrey Hsu };
69b75356e1SJeffrey Hsu #define	UIHASH(uid)	(&uihashtbl[(uid) & uihash])
70e3975643SJake Burkholder static LIST_HEAD(uihashhead, uidinfo) *uihashtbl;
7187b6de2bSPoul-Henning Kamp static u_long uihash;		/* size of hash table - 1 */
72df8bae1dSRodney W. Grimes 
7398d93822SBruce Evans static void	orphanpg __P((struct pgrp *pg));
7498d93822SBruce Evans 
75c6362551SAlfred Perlstein static struct uidinfo	*uifind(uid_t uid);
76c6362551SAlfred Perlstein static struct uidinfo	*uicreate(uid_t uid);
77c6362551SAlfred Perlstein static int 	uifree(struct uidinfo *uip);
78c6362551SAlfred Perlstein 
79df8bae1dSRodney W. Grimes /*
80b75356e1SJeffrey Hsu  * Other process lists
81b75356e1SJeffrey Hsu  */
82b75356e1SJeffrey Hsu struct pidhashhead *pidhashtbl;
83b75356e1SJeffrey Hsu u_long pidhash;
84b75356e1SJeffrey Hsu struct pgrphashhead *pgrphashtbl;
85b75356e1SJeffrey Hsu u_long pgrphash;
86b75356e1SJeffrey Hsu struct proclist allproc;
87b75356e1SJeffrey Hsu struct proclist zombproc;
882d8acc0fSJohn Dyson vm_zone_t proc_zone;
89b75356e1SJeffrey Hsu 
90b75356e1SJeffrey Hsu /*
91b75356e1SJeffrey Hsu  * Initialize global process hashing structures.
92df8bae1dSRodney W. Grimes  */
9326f9a767SRodney W. Grimes void
94b75356e1SJeffrey Hsu procinit()
95df8bae1dSRodney W. Grimes {
96df8bae1dSRodney W. Grimes 
97b75356e1SJeffrey Hsu 	LIST_INIT(&allproc);
98b75356e1SJeffrey Hsu 	LIST_INIT(&zombproc);
99b75356e1SJeffrey Hsu 	pidhashtbl = hashinit(maxproc / 4, M_PROC, &pidhash);
100b75356e1SJeffrey Hsu 	pgrphashtbl = hashinit(maxproc / 4, M_PROC, &pgrphash);
101df8bae1dSRodney W. Grimes 	uihashtbl = hashinit(maxproc / 16, M_PROC, &uihash);
1022d8acc0fSJohn Dyson 	proc_zone = zinit("PROC", sizeof (struct proc), 0, 0, 5);
103df8bae1dSRodney W. Grimes }
104df8bae1dSRodney W. Grimes 
105df8bae1dSRodney W. Grimes /*
106c6362551SAlfred Perlstein  * find/create a uidinfo struct for the uid passed in
107df8bae1dSRodney W. Grimes  */
108c6362551SAlfred Perlstein static struct uidinfo *
109c6362551SAlfred Perlstein uifind(uid)
110df8bae1dSRodney W. Grimes 	uid_t uid;
111df8bae1dSRodney W. Grimes {
112c6362551SAlfred Perlstein 	struct uihashhead *uipp;
113c6362551SAlfred Perlstein 	struct uidinfo *uip;
114df8bae1dSRodney W. Grimes 
115b75356e1SJeffrey Hsu 	uipp = UIHASH(uid);
1161b727751SPoul-Henning Kamp 	LIST_FOREACH(uip, uipp, ui_hash)
117df8bae1dSRodney W. Grimes 		if (uip->ui_uid == uid)
118df8bae1dSRodney W. Grimes 			break;
119c6362551SAlfred Perlstein 
120c6362551SAlfred Perlstein 	return (uip);
121c6362551SAlfred Perlstein }
122c6362551SAlfred Perlstein 
123c6362551SAlfred Perlstein static struct uidinfo *
124c6362551SAlfred Perlstein uicreate(uid)
125c6362551SAlfred Perlstein 	uid_t uid;
126c6362551SAlfred Perlstein {
127c6362551SAlfred Perlstein 	struct uidinfo *uip, *norace;
128c6362551SAlfred Perlstein 
129c6362551SAlfred Perlstein 	MALLOC(uip, struct uidinfo *, sizeof(*uip), M_PROC, M_NOWAIT);
130c6362551SAlfred Perlstein 	if (uip == NULL) {
131c6362551SAlfred Perlstein 		MALLOC(uip, struct uidinfo *, sizeof(*uip), M_PROC, M_WAITOK);
132c6362551SAlfred Perlstein 		/*
133c6362551SAlfred Perlstein 		 * if we M_WAITOK we must look afterwards or risk
134c6362551SAlfred Perlstein 		 * redundant entries
135c6362551SAlfred Perlstein 		 */
136c6362551SAlfred Perlstein 		norace = uifind(uid);
137c6362551SAlfred Perlstein 		if (norace != NULL) {
138c6362551SAlfred Perlstein 			FREE(uip, M_PROC);
139c6362551SAlfred Perlstein 			return (norace);
140c6362551SAlfred Perlstein 		}
141c6362551SAlfred Perlstein 	}
142c6362551SAlfred Perlstein 	LIST_INSERT_HEAD(UIHASH(uid), uip, ui_hash);
143c6362551SAlfred Perlstein 	uip->ui_uid = uid;
144c6362551SAlfred Perlstein 	uip->ui_proccnt = 0;
145c6362551SAlfred Perlstein 	uip->ui_sbsize = 0;
146c6362551SAlfred Perlstein 	return (uip);
147c6362551SAlfred Perlstein }
148c6362551SAlfred Perlstein 
149c6362551SAlfred Perlstein static int
150c6362551SAlfred Perlstein uifree(uip)
151c6362551SAlfred Perlstein 	struct uidinfo *uip;
152c6362551SAlfred Perlstein {
153c6362551SAlfred Perlstein 
154c6362551SAlfred Perlstein 	if (uip->ui_sbsize == 0 && uip->ui_proccnt == 0) {
155b75356e1SJeffrey Hsu 		LIST_REMOVE(uip, ui_hash);
156df8bae1dSRodney W. Grimes 		FREE(uip, M_PROC);
157c6362551SAlfred Perlstein 		return (1);
158c6362551SAlfred Perlstein 	}
159df8bae1dSRodney W. Grimes 	return (0);
160df8bae1dSRodney W. Grimes }
161c6362551SAlfred Perlstein 
162c6362551SAlfred Perlstein /*
163c6362551SAlfred Perlstein  * Change the count associated with number of processes
164c6362551SAlfred Perlstein  * a given user is using.  When 'max' is 0, don't enforce a limit
165c6362551SAlfred Perlstein  */
166c6362551SAlfred Perlstein int
167c6362551SAlfred Perlstein chgproccnt(uid, diff, max)
168c6362551SAlfred Perlstein 	uid_t	uid;
169c6362551SAlfred Perlstein 	int	diff;
170c6362551SAlfred Perlstein 	int	max;
171c6362551SAlfred Perlstein {
172c6362551SAlfred Perlstein 	struct uidinfo *uip;
173c6362551SAlfred Perlstein 
174c6362551SAlfred Perlstein 	uip = uifind(uid);
175c6362551SAlfred Perlstein 	if (diff < 0)
176c6362551SAlfred Perlstein 		KASSERT(uip != NULL, ("reducing proccnt: lost count, uid = %d", uid));
177c6362551SAlfred Perlstein 	if (uip == NULL)
178c6362551SAlfred Perlstein 		uip = uicreate(uid);
179c6362551SAlfred Perlstein 	/* don't allow them to exceed max, but allow subtraction */
180c6362551SAlfred Perlstein 	if (diff > 0 && uip->ui_proccnt + diff > max && max != 0) {
181c6362551SAlfred Perlstein 		(void)uifree(uip);
182df8bae1dSRodney W. Grimes 		return (0);
183df8bae1dSRodney W. Grimes 	}
184c6362551SAlfred Perlstein 	uip->ui_proccnt += diff;
185c6362551SAlfred Perlstein 	(void)uifree(uip);
186c6362551SAlfred Perlstein 	return (1);
187ecf72308SBrian Feldman }
188ecf72308SBrian Feldman 
189ecf72308SBrian Feldman /*
190ecf72308SBrian Feldman  * Change the total socket buffer size a user has used.
191ecf72308SBrian Feldman  */
192c6362551SAlfred Perlstein int
193c6362551SAlfred Perlstein chgsbsize(uid, diff, max)
194ecf72308SBrian Feldman 	uid_t	uid;
195ecf72308SBrian Feldman 	rlim_t	diff;
196c6362551SAlfred Perlstein 	rlim_t	max;
197ecf72308SBrian Feldman {
198c6362551SAlfred Perlstein 	struct uidinfo *uip;
199ecf72308SBrian Feldman 
200c6362551SAlfred Perlstein 	uip = uifind(uid);
201c6362551SAlfred Perlstein 	if (diff < 0)
202c6362551SAlfred Perlstein 		KASSERT(uip != NULL, ("reducing sbsize: lost count, uid = %d", uid));
203c6362551SAlfred Perlstein 	if (uip == NULL)
204c6362551SAlfred Perlstein 		uip = uicreate(uid);
205c6362551SAlfred Perlstein 	/* don't allow them to exceed max, but allow subtraction */
206c6362551SAlfred Perlstein 	if (diff > 0 && uip->ui_sbsize + diff > max) {
207c6362551SAlfred Perlstein 		(void)uifree(uip);
208ecf72308SBrian Feldman 		return (0);
209ecf72308SBrian Feldman 	}
210c6362551SAlfred Perlstein 	uip->ui_sbsize += diff;
211c6362551SAlfred Perlstein 	(void)uifree(uip);
212c6362551SAlfred Perlstein 	return (1);
213df8bae1dSRodney W. Grimes }
214df8bae1dSRodney W. Grimes 
215df8bae1dSRodney W. Grimes /*
216df8bae1dSRodney W. Grimes  * Is p an inferior of the current process?
217df8bae1dSRodney W. Grimes  */
2181a432a2fSDima Ruban int
219df8bae1dSRodney W. Grimes inferior(p)
220df8bae1dSRodney W. Grimes 	register struct proc *p;
221df8bae1dSRodney W. Grimes {
222df8bae1dSRodney W. Grimes 
223df8bae1dSRodney W. Grimes 	for (; p != curproc; p = p->p_pptr)
224df8bae1dSRodney W. Grimes 		if (p->p_pid == 0)
225df8bae1dSRodney W. Grimes 			return (0);
226df8bae1dSRodney W. Grimes 	return (1);
227df8bae1dSRodney W. Grimes }
228df8bae1dSRodney W. Grimes 
229df8bae1dSRodney W. Grimes /*
230df8bae1dSRodney W. Grimes  * Locate a process by number
231df8bae1dSRodney W. Grimes  */
232df8bae1dSRodney W. Grimes struct proc *
233df8bae1dSRodney W. Grimes pfind(pid)
234df8bae1dSRodney W. Grimes 	register pid_t pid;
235df8bae1dSRodney W. Grimes {
236df8bae1dSRodney W. Grimes 	register struct proc *p;
237df8bae1dSRodney W. Grimes 
2381b727751SPoul-Henning Kamp 	LIST_FOREACH(p, PIDHASH(pid), p_hash)
239df8bae1dSRodney W. Grimes 		if (p->p_pid == pid)
240df8bae1dSRodney W. Grimes 			return (p);
241df8bae1dSRodney W. Grimes 	return (NULL);
242df8bae1dSRodney W. Grimes }
243df8bae1dSRodney W. Grimes 
244df8bae1dSRodney W. Grimes /*
245df8bae1dSRodney W. Grimes  * Locate a process group by number
246df8bae1dSRodney W. Grimes  */
247df8bae1dSRodney W. Grimes struct pgrp *
248df8bae1dSRodney W. Grimes pgfind(pgid)
249df8bae1dSRodney W. Grimes 	register pid_t pgid;
250df8bae1dSRodney W. Grimes {
251df8bae1dSRodney W. Grimes 	register struct pgrp *pgrp;
252df8bae1dSRodney W. Grimes 
2531b727751SPoul-Henning Kamp 	LIST_FOREACH(pgrp, PGRPHASH(pgid), pg_hash)
254df8bae1dSRodney W. Grimes 		if (pgrp->pg_id == pgid)
255df8bae1dSRodney W. Grimes 			return (pgrp);
256df8bae1dSRodney W. Grimes 	return (NULL);
257df8bae1dSRodney W. Grimes }
258df8bae1dSRodney W. Grimes 
259df8bae1dSRodney W. Grimes /*
260df8bae1dSRodney W. Grimes  * Move p to a new or existing process group (and session)
261df8bae1dSRodney W. Grimes  */
26226f9a767SRodney W. Grimes int
263df8bae1dSRodney W. Grimes enterpgrp(p, pgid, mksess)
264df8bae1dSRodney W. Grimes 	register struct proc *p;
265df8bae1dSRodney W. Grimes 	pid_t pgid;
266df8bae1dSRodney W. Grimes 	int mksess;
267df8bae1dSRodney W. Grimes {
268df8bae1dSRodney W. Grimes 	register struct pgrp *pgrp = pgfind(pgid);
269df8bae1dSRodney W. Grimes 
2705526d2d9SEivind Eklund 	KASSERT(pgrp == NULL || !mksess,
2715526d2d9SEivind Eklund 	    ("enterpgrp: setsid into non-empty pgrp"));
2725526d2d9SEivind Eklund 	KASSERT(!SESS_LEADER(p),
2735526d2d9SEivind Eklund 	    ("enterpgrp: session leader attempted setpgrp"));
274219cbf59SEivind Eklund 
275df8bae1dSRodney W. Grimes 	if (pgrp == NULL) {
276df8bae1dSRodney W. Grimes 		pid_t savepid = p->p_pid;
277df8bae1dSRodney W. Grimes 		struct proc *np;
278df8bae1dSRodney W. Grimes 		/*
279df8bae1dSRodney W. Grimes 		 * new process group
280df8bae1dSRodney W. Grimes 		 */
2815526d2d9SEivind Eklund 		KASSERT(p->p_pid == pgid,
2825526d2d9SEivind Eklund 		    ("enterpgrp: new pgrp and pid != pgid"));
283df8bae1dSRodney W. Grimes 		MALLOC(pgrp, struct pgrp *, sizeof(struct pgrp), M_PGRP,
284df8bae1dSRodney W. Grimes 		    M_WAITOK);
285df8bae1dSRodney W. Grimes 		if ((np = pfind(savepid)) == NULL || np != p)
286df8bae1dSRodney W. Grimes 			return (ESRCH);
287df8bae1dSRodney W. Grimes 		if (mksess) {
288df8bae1dSRodney W. Grimes 			register struct session *sess;
289df8bae1dSRodney W. Grimes 
290df8bae1dSRodney W. Grimes 			/*
291df8bae1dSRodney W. Grimes 			 * new session
292df8bae1dSRodney W. Grimes 			 */
293df8bae1dSRodney W. Grimes 			MALLOC(sess, struct session *, sizeof(struct session),
294df8bae1dSRodney W. Grimes 			    M_SESSION, M_WAITOK);
295df8bae1dSRodney W. Grimes 			sess->s_leader = p;
296643a8daaSDon Lewis 			sess->s_sid = p->p_pid;
297df8bae1dSRodney W. Grimes 			sess->s_count = 1;
298df8bae1dSRodney W. Grimes 			sess->s_ttyvp = NULL;
299df8bae1dSRodney W. Grimes 			sess->s_ttyp = NULL;
300df8bae1dSRodney W. Grimes 			bcopy(p->p_session->s_login, sess->s_login,
301df8bae1dSRodney W. Grimes 			    sizeof(sess->s_login));
302df8bae1dSRodney W. Grimes 			p->p_flag &= ~P_CONTROLT;
303df8bae1dSRodney W. Grimes 			pgrp->pg_session = sess;
3045526d2d9SEivind Eklund 			KASSERT(p == curproc,
3055526d2d9SEivind Eklund 			    ("enterpgrp: mksession and p != curproc"));
306df8bae1dSRodney W. Grimes 		} else {
307df8bae1dSRodney W. Grimes 			pgrp->pg_session = p->p_session;
308df8bae1dSRodney W. Grimes 			pgrp->pg_session->s_count++;
309df8bae1dSRodney W. Grimes 		}
310df8bae1dSRodney W. Grimes 		pgrp->pg_id = pgid;
311b75356e1SJeffrey Hsu 		LIST_INIT(&pgrp->pg_members);
312b75356e1SJeffrey Hsu 		LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash);
313df8bae1dSRodney W. Grimes 		pgrp->pg_jobc = 0;
314831d27a9SDon Lewis 		SLIST_INIT(&pgrp->pg_sigiolst);
315df8bae1dSRodney W. Grimes 	} else if (pgrp == p->p_pgrp)
316df8bae1dSRodney W. Grimes 		return (0);
317df8bae1dSRodney W. Grimes 
318df8bae1dSRodney W. Grimes 	/*
319df8bae1dSRodney W. Grimes 	 * Adjust eligibility of affected pgrps to participate in job control.
320df8bae1dSRodney W. Grimes 	 * Increment eligibility counts before decrementing, otherwise we
321df8bae1dSRodney W. Grimes 	 * could reach 0 spuriously during the first call.
322df8bae1dSRodney W. Grimes 	 */
323df8bae1dSRodney W. Grimes 	fixjobc(p, pgrp, 1);
324df8bae1dSRodney W. Grimes 	fixjobc(p, p->p_pgrp, 0);
325df8bae1dSRodney W. Grimes 
326b75356e1SJeffrey Hsu 	LIST_REMOVE(p, p_pglist);
3271b727751SPoul-Henning Kamp 	if (LIST_EMPTY(&p->p_pgrp->pg_members))
328df8bae1dSRodney W. Grimes 		pgdelete(p->p_pgrp);
329df8bae1dSRodney W. Grimes 	p->p_pgrp = pgrp;
330b75356e1SJeffrey Hsu 	LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist);
331df8bae1dSRodney W. Grimes 	return (0);
332df8bae1dSRodney W. Grimes }
333df8bae1dSRodney W. Grimes 
334df8bae1dSRodney W. Grimes /*
335df8bae1dSRodney W. Grimes  * remove process from process group
336df8bae1dSRodney W. Grimes  */
33726f9a767SRodney W. Grimes int
338df8bae1dSRodney W. Grimes leavepgrp(p)
339df8bae1dSRodney W. Grimes 	register struct proc *p;
340df8bae1dSRodney W. Grimes {
341df8bae1dSRodney W. Grimes 
342b75356e1SJeffrey Hsu 	LIST_REMOVE(p, p_pglist);
3431b727751SPoul-Henning Kamp 	if (LIST_EMPTY(&p->p_pgrp->pg_members))
344df8bae1dSRodney W. Grimes 		pgdelete(p->p_pgrp);
345df8bae1dSRodney W. Grimes 	p->p_pgrp = 0;
346df8bae1dSRodney W. Grimes 	return (0);
347df8bae1dSRodney W. Grimes }
348df8bae1dSRodney W. Grimes 
349df8bae1dSRodney W. Grimes /*
350df8bae1dSRodney W. Grimes  * delete a process group
351df8bae1dSRodney W. Grimes  */
35287b6de2bSPoul-Henning Kamp static void
353df8bae1dSRodney W. Grimes pgdelete(pgrp)
354df8bae1dSRodney W. Grimes 	register struct pgrp *pgrp;
355df8bae1dSRodney W. Grimes {
356df8bae1dSRodney W. Grimes 
357831d27a9SDon Lewis 	/*
358831d27a9SDon Lewis 	 * Reset any sigio structures pointing to us as a result of
359831d27a9SDon Lewis 	 * F_SETOWN with our pgid.
360831d27a9SDon Lewis 	 */
361831d27a9SDon Lewis 	funsetownlst(&pgrp->pg_sigiolst);
362831d27a9SDon Lewis 
363df8bae1dSRodney W. Grimes 	if (pgrp->pg_session->s_ttyp != NULL &&
364df8bae1dSRodney W. Grimes 	    pgrp->pg_session->s_ttyp->t_pgrp == pgrp)
365df8bae1dSRodney W. Grimes 		pgrp->pg_session->s_ttyp->t_pgrp = NULL;
366b75356e1SJeffrey Hsu 	LIST_REMOVE(pgrp, pg_hash);
367df8bae1dSRodney W. Grimes 	if (--pgrp->pg_session->s_count == 0)
368df8bae1dSRodney W. Grimes 		FREE(pgrp->pg_session, M_SESSION);
369df8bae1dSRodney W. Grimes 	FREE(pgrp, M_PGRP);
370df8bae1dSRodney W. Grimes }
371df8bae1dSRodney W. Grimes 
372df8bae1dSRodney W. Grimes /*
373df8bae1dSRodney W. Grimes  * Adjust pgrp jobc counters when specified process changes process group.
374df8bae1dSRodney W. Grimes  * We count the number of processes in each process group that "qualify"
375df8bae1dSRodney W. Grimes  * the group for terminal job control (those with a parent in a different
376df8bae1dSRodney W. Grimes  * process group of the same session).  If that count reaches zero, the
377df8bae1dSRodney W. Grimes  * process group becomes orphaned.  Check both the specified process'
378df8bae1dSRodney W. Grimes  * process group and that of its children.
379df8bae1dSRodney W. Grimes  * entering == 0 => p is leaving specified group.
380df8bae1dSRodney W. Grimes  * entering == 1 => p is entering specified group.
381df8bae1dSRodney W. Grimes  */
38226f9a767SRodney W. Grimes void
383df8bae1dSRodney W. Grimes fixjobc(p, pgrp, entering)
384df8bae1dSRodney W. Grimes 	register struct proc *p;
385df8bae1dSRodney W. Grimes 	register struct pgrp *pgrp;
386df8bae1dSRodney W. Grimes 	int entering;
387df8bae1dSRodney W. Grimes {
388df8bae1dSRodney W. Grimes 	register struct pgrp *hispgrp;
389df8bae1dSRodney W. Grimes 	register struct session *mysession = pgrp->pg_session;
390df8bae1dSRodney W. Grimes 
391df8bae1dSRodney W. Grimes 	/*
392df8bae1dSRodney W. Grimes 	 * Check p's parent to see whether p qualifies its own process
393df8bae1dSRodney W. Grimes 	 * group; if so, adjust count for p's process group.
394df8bae1dSRodney W. Grimes 	 */
395df8bae1dSRodney W. Grimes 	if ((hispgrp = p->p_pptr->p_pgrp) != pgrp &&
396dfd5dee1SPeter Wemm 	    hispgrp->pg_session == mysession) {
397df8bae1dSRodney W. Grimes 		if (entering)
398df8bae1dSRodney W. Grimes 			pgrp->pg_jobc++;
399df8bae1dSRodney W. Grimes 		else if (--pgrp->pg_jobc == 0)
400df8bae1dSRodney W. Grimes 			orphanpg(pgrp);
401dfd5dee1SPeter Wemm 	}
402df8bae1dSRodney W. Grimes 
403df8bae1dSRodney W. Grimes 	/*
404df8bae1dSRodney W. Grimes 	 * Check this process' children to see whether they qualify
405df8bae1dSRodney W. Grimes 	 * their process groups; if so, adjust counts for children's
406df8bae1dSRodney W. Grimes 	 * process groups.
407df8bae1dSRodney W. Grimes 	 */
4081b727751SPoul-Henning Kamp 	LIST_FOREACH(p, &p->p_children, p_sibling)
409df8bae1dSRodney W. Grimes 		if ((hispgrp = p->p_pgrp) != pgrp &&
410df8bae1dSRodney W. Grimes 		    hispgrp->pg_session == mysession &&
411dfd5dee1SPeter Wemm 		    p->p_stat != SZOMB) {
412df8bae1dSRodney W. Grimes 			if (entering)
413df8bae1dSRodney W. Grimes 				hispgrp->pg_jobc++;
414df8bae1dSRodney W. Grimes 			else if (--hispgrp->pg_jobc == 0)
415df8bae1dSRodney W. Grimes 				orphanpg(hispgrp);
416df8bae1dSRodney W. Grimes 		}
417dfd5dee1SPeter Wemm }
418df8bae1dSRodney W. Grimes 
419df8bae1dSRodney W. Grimes /*
420df8bae1dSRodney W. Grimes  * A process group has become orphaned;
421df8bae1dSRodney W. Grimes  * if there are any stopped processes in the group,
422df8bae1dSRodney W. Grimes  * hang-up all process in that group.
423df8bae1dSRodney W. Grimes  */
424df8bae1dSRodney W. Grimes static void
425df8bae1dSRodney W. Grimes orphanpg(pg)
426df8bae1dSRodney W. Grimes 	struct pgrp *pg;
427df8bae1dSRodney W. Grimes {
428df8bae1dSRodney W. Grimes 	register struct proc *p;
429df8bae1dSRodney W. Grimes 
4301b727751SPoul-Henning Kamp 	LIST_FOREACH(p, &pg->pg_members, p_pglist) {
431df8bae1dSRodney W. Grimes 		if (p->p_stat == SSTOP) {
4321b727751SPoul-Henning Kamp 			LIST_FOREACH(p, &pg->pg_members, p_pglist) {
433df8bae1dSRodney W. Grimes 				psignal(p, SIGHUP);
434df8bae1dSRodney W. Grimes 				psignal(p, SIGCONT);
435df8bae1dSRodney W. Grimes 			}
436df8bae1dSRodney W. Grimes 			return;
437df8bae1dSRodney W. Grimes 		}
438df8bae1dSRodney W. Grimes 	}
439df8bae1dSRodney W. Grimes }
440df8bae1dSRodney W. Grimes 
441831031ceSBruce Evans #include "opt_ddb.h"
442831031ceSBruce Evans #ifdef DDB
443831031ceSBruce Evans #include <ddb/ddb.h>
444831031ceSBruce Evans 
445831031ceSBruce Evans DB_SHOW_COMMAND(pgrpdump, pgrpdump)
446df8bae1dSRodney W. Grimes {
447df8bae1dSRodney W. Grimes 	register struct pgrp *pgrp;
448df8bae1dSRodney W. Grimes 	register struct proc *p;
449876a94eeSBruce Evans 	register int i;
450df8bae1dSRodney W. Grimes 
451b75356e1SJeffrey Hsu 	for (i = 0; i <= pgrphash; i++) {
4521b727751SPoul-Henning Kamp 		if (!LIST_EMPTY(&pgrphashtbl[i])) {
453df8bae1dSRodney W. Grimes 			printf("\tindx %d\n", i);
4541b727751SPoul-Henning Kamp 			LIST_FOREACH(pgrp, &pgrphashtbl[i], pg_hash) {
455ac1e407bSBruce Evans 				printf(
456ac1e407bSBruce Evans 			"\tpgrp %p, pgid %ld, sess %p, sesscnt %d, mem %p\n",
457ac1e407bSBruce Evans 				    (void *)pgrp, (long)pgrp->pg_id,
458ac1e407bSBruce Evans 				    (void *)pgrp->pg_session,
459b75356e1SJeffrey Hsu 				    pgrp->pg_session->s_count,
4601b727751SPoul-Henning Kamp 				    (void *)LIST_FIRST(&pgrp->pg_members));
4611b727751SPoul-Henning Kamp 				LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
462ac1e407bSBruce Evans 					printf("\t\tpid %ld addr %p pgrp %p\n",
463ac1e407bSBruce Evans 					    (long)p->p_pid, (void *)p,
464ac1e407bSBruce Evans 					    (void *)p->p_pgrp);
465df8bae1dSRodney W. Grimes 				}
466df8bae1dSRodney W. Grimes 			}
467df8bae1dSRodney W. Grimes 		}
468df8bae1dSRodney W. Grimes 	}
469df8bae1dSRodney W. Grimes }
470831031ceSBruce Evans #endif /* DDB */
471972f9b20SPoul-Henning Kamp 
472972f9b20SPoul-Henning Kamp /*
473972f9b20SPoul-Henning Kamp  * Fill in an eproc structure for the specified process.
474972f9b20SPoul-Henning Kamp  */
475972f9b20SPoul-Henning Kamp void
476972f9b20SPoul-Henning Kamp fill_eproc(p, ep)
477972f9b20SPoul-Henning Kamp 	register struct proc *p;
478972f9b20SPoul-Henning Kamp 	register struct eproc *ep;
479972f9b20SPoul-Henning Kamp {
480972f9b20SPoul-Henning Kamp 	register struct tty *tp;
481972f9b20SPoul-Henning Kamp 
482972f9b20SPoul-Henning Kamp 	bzero(ep, sizeof(*ep));
483972f9b20SPoul-Henning Kamp 
484972f9b20SPoul-Henning Kamp 	ep->e_paddr = p;
485972f9b20SPoul-Henning Kamp 	if (p->p_cred) {
486972f9b20SPoul-Henning Kamp 		ep->e_pcred = *p->p_cred;
487972f9b20SPoul-Henning Kamp 		if (p->p_ucred)
488972f9b20SPoul-Henning Kamp 			ep->e_ucred = *p->p_ucred;
489972f9b20SPoul-Henning Kamp 	}
490d8c85307SJulian Elischer 	if (p->p_procsig) {
491d8c85307SJulian Elischer 		ep->e_procsig = *p->p_procsig;
492d8c85307SJulian Elischer 	}
493972f9b20SPoul-Henning Kamp 	if (p->p_stat != SIDL && p->p_stat != SZOMB && p->p_vmspace != NULL) {
494972f9b20SPoul-Henning Kamp 		register struct vmspace *vm = p->p_vmspace;
495b1028ad1SLuoqi Chen 		ep->e_vm = *vm;
496b1028ad1SLuoqi Chen 		ep->e_vm.vm_rssize = vmspace_resident_count(vm); /*XXX*/
497972f9b20SPoul-Henning Kamp 	}
498e29e202cSPeter Wemm 	if ((p->p_flag & P_INMEM) && p->p_stats)
499e29e202cSPeter Wemm 		ep->e_stats = *p->p_stats;
500972f9b20SPoul-Henning Kamp 	if (p->p_pptr)
501972f9b20SPoul-Henning Kamp 		ep->e_ppid = p->p_pptr->p_pid;
502972f9b20SPoul-Henning Kamp 	if (p->p_pgrp) {
503972f9b20SPoul-Henning Kamp 		ep->e_pgid = p->p_pgrp->pg_id;
504972f9b20SPoul-Henning Kamp 		ep->e_jobc = p->p_pgrp->pg_jobc;
505cd73303cSDavid Greenman 		ep->e_sess = p->p_pgrp->pg_session;
506cd73303cSDavid Greenman 
507cd73303cSDavid Greenman 		if (ep->e_sess) {
5088735cebcSPeter Wemm 			bcopy(ep->e_sess->s_login, ep->e_login, sizeof(ep->e_login));
509cd73303cSDavid Greenman 			if (ep->e_sess->s_ttyvp)
510cd73303cSDavid Greenman 				ep->e_flag = EPROC_CTTY;
511cd73303cSDavid Greenman 			if (p->p_session && SESS_LEADER(p))
512cd73303cSDavid Greenman 				ep->e_flag |= EPROC_SLEADER;
513cd73303cSDavid Greenman 		}
514cd73303cSDavid Greenman 	}
515972f9b20SPoul-Henning Kamp 	if ((p->p_flag & P_CONTROLT) &&
516972f9b20SPoul-Henning Kamp 	    (ep->e_sess != NULL) &&
517972f9b20SPoul-Henning Kamp 	    ((tp = ep->e_sess->s_ttyp) != NULL)) {
518f40ddd55SDoug Rabson 		ep->e_tdev = dev2udev(tp->t_dev);
519972f9b20SPoul-Henning Kamp 		ep->e_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID;
520972f9b20SPoul-Henning Kamp 		ep->e_tsess = tp->t_session;
521972f9b20SPoul-Henning Kamp 	} else
5226a0ce002SPoul-Henning Kamp 		ep->e_tdev = NOUDEV;
523972f9b20SPoul-Henning Kamp 	if (p->p_wmesg) {
524972f9b20SPoul-Henning Kamp 		strncpy(ep->e_wmesg, p->p_wmesg, WMESGLEN);
525972f9b20SPoul-Henning Kamp 		ep->e_wmesg[WMESGLEN] = 0;
526972f9b20SPoul-Henning Kamp 	}
527972f9b20SPoul-Henning Kamp }
528972f9b20SPoul-Henning Kamp 
5293ce93e4eSPoul-Henning Kamp static struct proc *
5303ce93e4eSPoul-Henning Kamp zpfind(pid_t pid)
5313ce93e4eSPoul-Henning Kamp {
5323ce93e4eSPoul-Henning Kamp 	struct proc *p;
5333ce93e4eSPoul-Henning Kamp 
5341b727751SPoul-Henning Kamp 	LIST_FOREACH(p, &zombproc, p_list)
5353ce93e4eSPoul-Henning Kamp 		if (p->p_pid == pid)
5363ce93e4eSPoul-Henning Kamp 			return (p);
5373ce93e4eSPoul-Henning Kamp 	return (NULL);
5383ce93e4eSPoul-Henning Kamp }
5393ce93e4eSPoul-Henning Kamp 
5403ce93e4eSPoul-Henning Kamp 
5413ce93e4eSPoul-Henning Kamp static int
5423ce93e4eSPoul-Henning Kamp sysctl_out_proc(struct proc *p, struct sysctl_req *req, int doingzomb)
5433ce93e4eSPoul-Henning Kamp {
5443ce93e4eSPoul-Henning Kamp 	struct eproc eproc;
5453ce93e4eSPoul-Henning Kamp 	int error;
5463ce93e4eSPoul-Henning Kamp 	pid_t pid = p->p_pid;
5473ce93e4eSPoul-Henning Kamp 
5483ce93e4eSPoul-Henning Kamp 	fill_eproc(p, &eproc);
5493ce93e4eSPoul-Henning Kamp 	error = SYSCTL_OUT(req,(caddr_t)p, sizeof(struct proc));
5503ce93e4eSPoul-Henning Kamp 	if (error)
5513ce93e4eSPoul-Henning Kamp 		return (error);
5523ce93e4eSPoul-Henning Kamp 	error = SYSCTL_OUT(req,(caddr_t)&eproc, sizeof(eproc));
5533ce93e4eSPoul-Henning Kamp 	if (error)
5543ce93e4eSPoul-Henning Kamp 		return (error);
5553ce93e4eSPoul-Henning Kamp 	if (!doingzomb && pid && (pfind(pid) != p))
5563ce93e4eSPoul-Henning Kamp 		return EAGAIN;
5573ce93e4eSPoul-Henning Kamp 	if (doingzomb && zpfind(pid) != p)
5583ce93e4eSPoul-Henning Kamp 		return EAGAIN;
5593ce93e4eSPoul-Henning Kamp 	return (0);
5603ce93e4eSPoul-Henning Kamp }
5613ce93e4eSPoul-Henning Kamp 
562972f9b20SPoul-Henning Kamp static int
56382d9ae4eSPoul-Henning Kamp sysctl_kern_proc (SYSCTL_HANDLER_ARGS)
564972f9b20SPoul-Henning Kamp {
565972f9b20SPoul-Henning Kamp 	int *name = (int*) arg1;
566972f9b20SPoul-Henning Kamp 	u_int namelen = arg2;
567972f9b20SPoul-Henning Kamp 	struct proc *p;
568972f9b20SPoul-Henning Kamp 	int doingzomb;
569972f9b20SPoul-Henning Kamp 	int error = 0;
570972f9b20SPoul-Henning Kamp 
5713ce93e4eSPoul-Henning Kamp 	if (oidp->oid_number == KERN_PROC_PID) {
5723ce93e4eSPoul-Henning Kamp 		if (namelen != 1)
573972f9b20SPoul-Henning Kamp 			return (EINVAL);
5743ce93e4eSPoul-Henning Kamp 		p = pfind((pid_t)name[0]);
5753ce93e4eSPoul-Henning Kamp 		if (!p)
5763ce93e4eSPoul-Henning Kamp 			return (0);
57775c13541SPoul-Henning Kamp 		if (!PRISON_CHECK(curproc, p))
57875c13541SPoul-Henning Kamp 			return (0);
5793ce93e4eSPoul-Henning Kamp 		error = sysctl_out_proc(p, req, 0);
5803ce93e4eSPoul-Henning Kamp 		return (error);
5813ce93e4eSPoul-Henning Kamp 	}
5823ce93e4eSPoul-Henning Kamp 	if (oidp->oid_number == KERN_PROC_ALL && !namelen)
5833ce93e4eSPoul-Henning Kamp 		;
5843ce93e4eSPoul-Henning Kamp 	else if (oidp->oid_number != KERN_PROC_ALL && namelen == 1)
5853ce93e4eSPoul-Henning Kamp 		;
5863ce93e4eSPoul-Henning Kamp 	else
5873ce93e4eSPoul-Henning Kamp 		return (EINVAL);
5883ce93e4eSPoul-Henning Kamp 
589972f9b20SPoul-Henning Kamp 	if (!req->oldptr) {
5903ce93e4eSPoul-Henning Kamp 		/* overestimate by 5 procs */
591972f9b20SPoul-Henning Kamp 		error = SYSCTL_OUT(req, 0, sizeof (struct kinfo_proc) * 5);
592972f9b20SPoul-Henning Kamp 		if (error)
593972f9b20SPoul-Henning Kamp 			return (error);
594972f9b20SPoul-Henning Kamp 	}
5953ce93e4eSPoul-Henning Kamp 	for (doingzomb=0 ; doingzomb < 2 ; doingzomb++) {
5963ce93e4eSPoul-Henning Kamp 		if (!doingzomb)
5971b727751SPoul-Henning Kamp 			p = LIST_FIRST(&allproc);
5983ce93e4eSPoul-Henning Kamp 		else
5991b727751SPoul-Henning Kamp 			p = LIST_FIRST(&zombproc);
6001b727751SPoul-Henning Kamp 		for (; p != 0; p = LIST_NEXT(p, p_list)) {
601972f9b20SPoul-Henning Kamp 			/*
602972f9b20SPoul-Henning Kamp 			 * Skip embryonic processes.
603972f9b20SPoul-Henning Kamp 			 */
604972f9b20SPoul-Henning Kamp 			if (p->p_stat == SIDL)
605972f9b20SPoul-Henning Kamp 				continue;
606972f9b20SPoul-Henning Kamp 			/*
607972f9b20SPoul-Henning Kamp 			 * TODO - make more efficient (see notes below).
608972f9b20SPoul-Henning Kamp 			 * do by session.
609972f9b20SPoul-Henning Kamp 			 */
6103ce93e4eSPoul-Henning Kamp 			switch (oidp->oid_number) {
611972f9b20SPoul-Henning Kamp 
612972f9b20SPoul-Henning Kamp 			case KERN_PROC_PGRP:
613972f9b20SPoul-Henning Kamp 				/* could do this by traversing pgrp */
6143ce93e4eSPoul-Henning Kamp 				if (p->p_pgrp == NULL ||
6153ce93e4eSPoul-Henning Kamp 				    p->p_pgrp->pg_id != (pid_t)name[0])
616972f9b20SPoul-Henning Kamp 					continue;
617972f9b20SPoul-Henning Kamp 				break;
618972f9b20SPoul-Henning Kamp 
619972f9b20SPoul-Henning Kamp 			case KERN_PROC_TTY:
620972f9b20SPoul-Henning Kamp 				if ((p->p_flag & P_CONTROLT) == 0 ||
621972f9b20SPoul-Henning Kamp 				    p->p_session == NULL ||
622972f9b20SPoul-Henning Kamp 				    p->p_session->s_ttyp == NULL ||
62368f7448fSPoul-Henning Kamp 				    dev2udev(p->p_session->s_ttyp->t_dev) !=
62468f7448fSPoul-Henning Kamp 					(udev_t)name[0])
625972f9b20SPoul-Henning Kamp 					continue;
626972f9b20SPoul-Henning Kamp 				break;
627972f9b20SPoul-Henning Kamp 
628972f9b20SPoul-Henning Kamp 			case KERN_PROC_UID:
6293ce93e4eSPoul-Henning Kamp 				if (p->p_ucred == NULL ||
6303ce93e4eSPoul-Henning Kamp 				    p->p_ucred->cr_uid != (uid_t)name[0])
631972f9b20SPoul-Henning Kamp 					continue;
632972f9b20SPoul-Henning Kamp 				break;
633972f9b20SPoul-Henning Kamp 
634972f9b20SPoul-Henning Kamp 			case KERN_PROC_RUID:
6353ce93e4eSPoul-Henning Kamp 				if (p->p_ucred == NULL ||
6363ce93e4eSPoul-Henning Kamp 				    p->p_cred->p_ruid != (uid_t)name[0])
637972f9b20SPoul-Henning Kamp 					continue;
638972f9b20SPoul-Henning Kamp 				break;
639972f9b20SPoul-Henning Kamp 			}
640972f9b20SPoul-Henning Kamp 
64175c13541SPoul-Henning Kamp 			if (!PRISON_CHECK(curproc, p))
64275c13541SPoul-Henning Kamp 				continue;
64375c13541SPoul-Henning Kamp 
6443ce93e4eSPoul-Henning Kamp 			error = sysctl_out_proc(p, req, doingzomb);
645972f9b20SPoul-Henning Kamp 			if (error)
646972f9b20SPoul-Henning Kamp 				return (error);
647972f9b20SPoul-Henning Kamp 		}
648972f9b20SPoul-Henning Kamp 	}
649972f9b20SPoul-Henning Kamp 	return (0);
650972f9b20SPoul-Henning Kamp }
651972f9b20SPoul-Henning Kamp 
652b9df5231SPoul-Henning Kamp /*
653b9df5231SPoul-Henning Kamp  * This sysctl allows a process to retrieve the argument list or process
654b9df5231SPoul-Henning Kamp  * title for another process without groping around in the address space
655b9df5231SPoul-Henning Kamp  * of the other process.  It also allow a process to set its own "process
656b9df5231SPoul-Henning Kamp  * title to a string of its own choice.
657b9df5231SPoul-Henning Kamp  */
658b9df5231SPoul-Henning Kamp static int
65982d9ae4eSPoul-Henning Kamp sysctl_kern_proc_args (SYSCTL_HANDLER_ARGS)
660b9df5231SPoul-Henning Kamp {
661b9df5231SPoul-Henning Kamp 	int *name = (int*) arg1;
662b9df5231SPoul-Henning Kamp 	u_int namelen = arg2;
663b9df5231SPoul-Henning Kamp 	struct proc *p;
664b9df5231SPoul-Henning Kamp 	struct pargs *pa;
665b9df5231SPoul-Henning Kamp 	int error = 0;
666b9df5231SPoul-Henning Kamp 
667b9df5231SPoul-Henning Kamp 	if (namelen != 1)
668b9df5231SPoul-Henning Kamp 		return (EINVAL);
669b9df5231SPoul-Henning Kamp 
670b9df5231SPoul-Henning Kamp 	p = pfind((pid_t)name[0]);
671b9df5231SPoul-Henning Kamp 	if (!p)
672b9df5231SPoul-Henning Kamp 		return (0);
673b9df5231SPoul-Henning Kamp 
674a8704f89SPoul-Henning Kamp 	if ((!ps_argsopen) && p_trespass(curproc, p))
675b9df5231SPoul-Henning Kamp 		return (0);
676b9df5231SPoul-Henning Kamp 
677b9df5231SPoul-Henning Kamp 	if (req->newptr && curproc != p)
678b9df5231SPoul-Henning Kamp 		return (EPERM);
679b9df5231SPoul-Henning Kamp 
680b9df5231SPoul-Henning Kamp 	if (req->oldptr && p->p_args != NULL)
681b9df5231SPoul-Henning Kamp 		error = SYSCTL_OUT(req, p->p_args->ar_args, p->p_args->ar_length);
682b9df5231SPoul-Henning Kamp 	if (req->newptr == NULL)
683b9df5231SPoul-Henning Kamp 		return (error);
684b9df5231SPoul-Henning Kamp 
685b9df5231SPoul-Henning Kamp 	if (p->p_args && --p->p_args->ar_ref == 0)
686b9df5231SPoul-Henning Kamp 		FREE(p->p_args, M_PARGS);
687b9df5231SPoul-Henning Kamp 	p->p_args = NULL;
688b9df5231SPoul-Henning Kamp 
689b9df5231SPoul-Henning Kamp 	if (req->newlen + sizeof(struct pargs) > ps_arg_cache_limit)
690b9df5231SPoul-Henning Kamp 		return (error);
691b9df5231SPoul-Henning Kamp 
692b9df5231SPoul-Henning Kamp 	MALLOC(pa, struct pargs *, sizeof(struct pargs) + req->newlen,
693b9df5231SPoul-Henning Kamp 	    M_PARGS, M_WAITOK);
694b9df5231SPoul-Henning Kamp 	pa->ar_ref = 1;
695b9df5231SPoul-Henning Kamp 	pa->ar_length = req->newlen;
696b9df5231SPoul-Henning Kamp 	error = SYSCTL_IN(req, pa->ar_args, req->newlen);
697b9df5231SPoul-Henning Kamp 	if (!error)
698b9df5231SPoul-Henning Kamp 		p->p_args = pa;
699b9df5231SPoul-Henning Kamp 	else
700b9df5231SPoul-Henning Kamp 		FREE(pa, M_PARGS);
701b9df5231SPoul-Henning Kamp 	return (error);
702b9df5231SPoul-Henning Kamp }
7033ce93e4eSPoul-Henning Kamp 
7043ce93e4eSPoul-Henning Kamp SYSCTL_NODE(_kern, KERN_PROC, proc, CTLFLAG_RD,  0, "Process table");
7053ce93e4eSPoul-Henning Kamp 
7063ce93e4eSPoul-Henning Kamp SYSCTL_PROC(_kern_proc, KERN_PROC_ALL, all, CTLFLAG_RD|CTLTYPE_STRUCT,
7073d177f46SBill Fumerola 	0, 0, sysctl_kern_proc, "S,proc", "Return entire process table");
7083ce93e4eSPoul-Henning Kamp 
7093ce93e4eSPoul-Henning Kamp SYSCTL_NODE(_kern_proc, KERN_PROC_PGRP, pgrp, CTLFLAG_RD,
7103ce93e4eSPoul-Henning Kamp 	sysctl_kern_proc, "Process table");
7113ce93e4eSPoul-Henning Kamp 
7123ce93e4eSPoul-Henning Kamp SYSCTL_NODE(_kern_proc, KERN_PROC_TTY, tty, CTLFLAG_RD,
7133ce93e4eSPoul-Henning Kamp 	sysctl_kern_proc, "Process table");
7143ce93e4eSPoul-Henning Kamp 
7153ce93e4eSPoul-Henning Kamp SYSCTL_NODE(_kern_proc, KERN_PROC_UID, uid, CTLFLAG_RD,
7163ce93e4eSPoul-Henning Kamp 	sysctl_kern_proc, "Process table");
7173ce93e4eSPoul-Henning Kamp 
7183ce93e4eSPoul-Henning Kamp SYSCTL_NODE(_kern_proc, KERN_PROC_RUID, ruid, CTLFLAG_RD,
7193ce93e4eSPoul-Henning Kamp 	sysctl_kern_proc, "Process table");
7203ce93e4eSPoul-Henning Kamp 
7213ce93e4eSPoul-Henning Kamp SYSCTL_NODE(_kern_proc, KERN_PROC_PID, pid, CTLFLAG_RD,
722972f9b20SPoul-Henning Kamp 	sysctl_kern_proc, "Process table");
723b9df5231SPoul-Henning Kamp 
7249b6d9dbaSPoul-Henning Kamp SYSCTL_NODE(_kern_proc, KERN_PROC_ARGS, args, CTLFLAG_RW | CTLFLAG_ANYBODY,
7259b6d9dbaSPoul-Henning Kamp 	sysctl_kern_proc_args, "Process argument list");
726