]> git.saurik.com Git - apple/xnu.git/blob - bsd/kern/kern_proc.c
125fde90a30ec564f9733dcda1bf16f1a3282be8
[apple/xnu.git] / bsd / kern / kern_proc.c
1 /*
2 * Copyright (c) 2000-2002 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
26 /*
27 * Copyright (c) 1982, 1986, 1989, 1991, 1993
28 * The Regents of the University of California. All rights reserved.
29 *
30 * Redistribution and use in source and binary forms, with or without
31 * modification, are permitted provided that the following conditions
32 * are met:
33 * 1. Redistributions of source code must retain the above copyright
34 * notice, this list of conditions and the following disclaimer.
35 * 2. Redistributions in binary form must reproduce the above copyright
36 * notice, this list of conditions and the following disclaimer in the
37 * documentation and/or other materials provided with the distribution.
38 * 3. All advertising materials mentioning features or use of this software
39 * must display the following acknowledgement:
40 * This product includes software developed by the University of
41 * California, Berkeley and its contributors.
42 * 4. Neither the name of the University nor the names of its contributors
43 * may be used to endorse or promote products derived from this software
44 * without specific prior written permission.
45 *
46 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
47 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
50 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
52 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
54 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
55 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56 * SUCH DAMAGE.
57 *
58 * @(#)kern_proc.c 8.4 (Berkeley) 1/4/94
59 */
60 /* HISTORY
61 * 04-Aug-97 Umesh Vaishampayan (umeshv@apple.com)
62 * Added current_proc_EXTERNAL() function for the use of kernel
63 * lodable modules.
64 *
65 * 05-Jun-95 Mac Gillon (mgillon) at NeXT
66 * New version based on 3.3NS and 4.4
67 */
68
69
70 #include <sys/param.h>
71 #include <sys/systm.h>
72 #include <sys/kernel.h>
73 #include <sys/proc.h>
74 #include <sys/buf.h>
75 #include <sys/acct.h>
76 #include <sys/wait.h>
77 #include <sys/file.h>
78 #include <ufs/ufs/quota.h>
79 #include <sys/uio.h>
80 #include <sys/malloc.h>
81 #include <sys/mbuf.h>
82 #include <sys/ioctl.h>
83 #include <sys/tty.h>
84 #include <sys/signalvar.h>
85
86 /*
87 * Structure associated with user cacheing.
88 */
89 struct uidinfo {
90 LIST_ENTRY(uidinfo) ui_hash;
91 uid_t ui_uid;
92 long ui_proccnt;
93 };
94 #define UIHASH(uid) (&uihashtbl[(uid) & uihash])
95 LIST_HEAD(uihashhead, uidinfo) *uihashtbl;
96 u_long uihash; /* size of hash table - 1 */
97
98 /*
99 * Other process lists
100 */
101 struct pidhashhead *pidhashtbl;
102 u_long pidhash;
103 struct pgrphashhead *pgrphashtbl;
104 u_long pgrphash;
105 struct proclist allproc;
106 struct proclist zombproc;
107
108 /*
109 * Initialize global process hashing structures.
110 */
111 void
112 procinit()
113 {
114
115 LIST_INIT(&allproc);
116 LIST_INIT(&zombproc);
117 pidhashtbl = hashinit(maxproc / 4, M_PROC, &pidhash);
118 pgrphashtbl = hashinit(maxproc / 4, M_PROC, &pgrphash);
119 uihashtbl = hashinit(maxproc / 16, M_PROC, &uihash);
120 }
121
122 /*
123 * Change the count associated with number of processes
124 * a given user is using.
125 */
126 int
127 chgproccnt(uid, diff)
128 uid_t uid;
129 int diff;
130 {
131 register struct uidinfo *uip;
132 register struct uihashhead *uipp;
133
134 uipp = UIHASH(uid);
135 for (uip = uipp->lh_first; uip != 0; uip = uip->ui_hash.le_next)
136 if (uip->ui_uid == uid)
137 break;
138 if (uip) {
139 uip->ui_proccnt += diff;
140 if (uip->ui_proccnt > 0)
141 return (uip->ui_proccnt);
142 if (uip->ui_proccnt < 0)
143 panic("chgproccnt: procs < 0");
144 LIST_REMOVE(uip, ui_hash);
145 FREE_ZONE(uip, sizeof *uip, M_PROC);
146 return (0);
147 }
148 if (diff <= 0) {
149 if (diff == 0)
150 return(0);
151 panic("chgproccnt: lost user");
152 }
153 MALLOC_ZONE(uip, struct uidinfo *, sizeof(*uip), M_PROC, M_WAITOK);
154 LIST_INSERT_HEAD(uipp, uip, ui_hash);
155 uip->ui_uid = uid;
156 uip->ui_proccnt = diff;
157 return (diff);
158 }
159
160 /*
161 * Is p an inferior of the current process?
162 */
163 int
164 inferior(p)
165 register struct proc *p;
166 {
167
168 for (; p != current_proc(); p = p->p_pptr)
169 if (p->p_pid == 0)
170 return (0);
171 return (1);
172 }
173 /*
174 * Is p an inferior of t ?
175 */
176 int
177 isinferior(p, t)
178 register struct proc *p;
179 register struct proc *t;
180 {
181
182 /* if p==t they are not inferior */
183 if (p == t)
184 return(0);
185 for (; p != t; p = p->p_pptr)
186 if (p->p_pid == 0)
187 return (0);
188 return (1);
189 }
190
191 /*
192 * Locate a process by number
193 */
194 struct proc *
195 pfind(pid)
196 register pid_t pid;
197 {
198 register struct proc *p;
199
200 if (!pid)
201 return (kernproc);
202
203 for (p = PIDHASH(pid)->lh_first; p != 0; p = p->p_hash.le_next)
204 if (p->p_pid == pid)
205 return (p);
206 return (NULL);
207 }
208
209 /*
210 * Locate a process group by number
211 */
212 struct pgrp *
213 pgfind(pgid)
214 register pid_t pgid;
215 {
216 register struct pgrp *pgrp;
217
218 for (pgrp = PGRPHASH(pgid)->lh_first; pgrp != 0; pgrp = pgrp->pg_hash.le_next)
219 if (pgrp->pg_id == pgid)
220 return (pgrp);
221 return (NULL);
222 }
223
224
225 /*
226 * Move p to a new or existing process group (and session)
227 */
228 int
229 enterpgrp(p, pgid, mksess)
230 register struct proc *p;
231 pid_t pgid;
232 int mksess;
233 {
234 register struct pgrp *pgrp = pgfind(pgid);
235
236 #if DIAGNOSTIC
237 if (pgrp != NULL && mksess) /* firewalls */
238 panic("enterpgrp: setsid into non-empty pgrp");
239 if (SESS_LEADER(p))
240 panic("enterpgrp: session leader attempted setpgrp");
241 #endif
242 if (pgrp == NULL) {
243 pid_t savepid = p->p_pid;
244 struct proc *np;
245 /*
246 * new process group
247 */
248 #if DIAGNOSTIC
249 if (p->p_pid != pgid)
250 panic("enterpgrp: new pgrp and pid != pgid");
251 #endif
252 MALLOC_ZONE(pgrp, struct pgrp *, sizeof(struct pgrp), M_PGRP,
253 M_WAITOK);
254 if ((np = pfind(savepid)) == NULL || np != p) {
255 FREE_ZONE(pgrp, sizeof(struct pgrp), M_PGRP);
256 return (ESRCH);
257 }
258 if (mksess) {
259 register struct session *sess;
260
261 /*
262 * new session
263 */
264 MALLOC_ZONE(sess, struct session *,
265 sizeof(struct session), M_SESSION, M_WAITOK);
266 sess->s_leader = p;
267 sess->s_sid = p->p_pid;
268 sess->s_count = 1;
269 sess->s_ttyvp = NULL;
270 sess->s_ttyp = NULL;
271 bcopy(p->p_session->s_login, sess->s_login,
272 sizeof(sess->s_login));
273 p->p_flag &= ~P_CONTROLT;
274 pgrp->pg_session = sess;
275 #if DIAGNOSTIC
276 if (p != current_proc())
277 panic("enterpgrp: mksession and p != curproc");
278 #endif
279 } else {
280 pgrp->pg_session = p->p_session;
281 pgrp->pg_session->s_count++;
282 }
283 pgrp->pg_id = pgid;
284 LIST_INIT(&pgrp->pg_members);
285 LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash);
286 pgrp->pg_jobc = 0;
287 } else if (pgrp == p->p_pgrp)
288 return (0);
289
290 /*
291 * Adjust eligibility of affected pgrps to participate in job control.
292 * Increment eligibility counts before decrementing, otherwise we
293 * could reach 0 spuriously during the first call.
294 */
295 fixjobc(p, pgrp, 1);
296 fixjobc(p, p->p_pgrp, 0);
297
298 LIST_REMOVE(p, p_pglist);
299 if (p->p_pgrp->pg_members.lh_first == 0)
300 pgdelete(p->p_pgrp);
301 p->p_pgrp = pgrp;
302 LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist);
303 return (0);
304 }
305
306 /*
307 * remove process from process group
308 */
309 int
310 leavepgrp(p)
311 register struct proc *p;
312 {
313
314 LIST_REMOVE(p, p_pglist);
315 if (p->p_pgrp->pg_members.lh_first == 0)
316 pgdelete(p->p_pgrp);
317 p->p_pgrp = 0;
318 return (0);
319 }
320
321 /*
322 * delete a process group
323 */
324 void
325 pgdelete(pgrp)
326 register struct pgrp *pgrp;
327 {
328
329 if (pgrp->pg_session->s_ttyp != NULL &&
330 pgrp->pg_session->s_ttyp->t_pgrp == pgrp)
331 pgrp->pg_session->s_ttyp->t_pgrp = NULL;
332 LIST_REMOVE(pgrp, pg_hash);
333 if (--pgrp->pg_session->s_count == 0)
334 FREE_ZONE(pgrp->pg_session, sizeof(struct session), M_SESSION);
335 FREE_ZONE(pgrp, sizeof *pgrp, M_PGRP);
336 }
337
338 void
339 sessrele(sess)
340 struct session *sess;
341 {
342 if (--sess->s_count == 0)
343 FREE_ZONE(sess, sizeof (struct session), M_SESSION);
344 }
345
346 static void orphanpg();
347
348 /*
349 * Adjust pgrp jobc counters when specified process changes process group.
350 * We count the number of processes in each process group that "qualify"
351 * the group for terminal job control (those with a parent in a different
352 * process group of the same session). If that count reaches zero, the
353 * process group becomes orphaned. Check both the specified process'
354 * process group and that of its children.
355 * entering == 0 => p is leaving specified group.
356 * entering == 1 => p is entering specified group.
357 */
358 void
359 fixjobc(p, pgrp, entering)
360 register struct proc *p;
361 register struct pgrp *pgrp;
362 int entering;
363 {
364 register struct pgrp *hispgrp;
365 register struct session *mysession = pgrp->pg_session;
366
367 /*
368 * Check p's parent to see whether p qualifies its own process
369 * group; if so, adjust count for p's process group.
370 */
371 if ((hispgrp = p->p_pptr->p_pgrp) != pgrp &&
372 hispgrp->pg_session == mysession)
373 if (entering)
374 pgrp->pg_jobc++;
375 else if (--pgrp->pg_jobc == 0)
376 orphanpg(pgrp);
377
378 /*
379 * Check this process' children to see whether they qualify
380 * their process groups; if so, adjust counts for children's
381 * process groups.
382 */
383 for (p = p->p_children.lh_first; p != 0; p = p->p_sibling.le_next)
384 if ((hispgrp = p->p_pgrp) != pgrp &&
385 hispgrp->pg_session == mysession &&
386 p->p_stat != SZOMB)
387 if (entering)
388 hispgrp->pg_jobc++;
389 else if (--hispgrp->pg_jobc == 0)
390 orphanpg(hispgrp);
391 }
392
393 /*
394 * A process group has become orphaned;
395 * if there are any stopped processes in the group,
396 * hang-up all process in that group.
397 */
398 static void
399 orphanpg(pg)
400 struct pgrp *pg;
401 {
402 register struct proc *p;
403
404 for (p = pg->pg_members.lh_first; p != 0; p = p->p_pglist.le_next) {
405 if (p->p_stat == SSTOP) {
406 for (p = pg->pg_members.lh_first; p != 0;
407 p = p->p_pglist.le_next) {
408 pt_setrunnable(p);
409 psignal(p, SIGHUP);
410 psignal(p, SIGCONT);
411 }
412 return;
413 }
414 }
415 }
416
417 #ifdef DEBUG
418 void
419 pgrpdump()
420 {
421 register struct pgrp *pgrp;
422 register struct proc *p;
423 register i;
424
425 for (i = 0; i <= pgrphash; i++) {
426 if (pgrp = pgrphashtbl[i].lh_first) {
427 printf("\tindx %d\n", i);
428 for (; pgrp != 0; pgrp = pgrp->pg_hash.le_next) {
429 printf("\tpgrp 0x%08x, pgid %d, sess %p, sesscnt %d, mem %p\n",
430 pgrp, pgrp->pg_id, pgrp->pg_session,
431 pgrp->pg_session->s_count,
432 pgrp->pg_members.lh_first);
433 for (p = pgrp->pg_members.lh_first; p != 0;
434 p = p->p_pglist.le_next) {
435 printf("\t\tpid %d addr 0x%08x pgrp 0x%08x\n",
436 p->p_pid, p, p->p_pgrp);
437 }
438 }
439 }
440 }
441 }
442 #endif /* DEBUG */
443
444 struct proc * current_proc_EXTERNAL()
445 {
446 return (current_proc());
447 }