]> git.saurik.com Git - apple/xnu.git/blob - bsd/sys/proc.h
xnu-344.tar.gz
[apple/xnu.git] / bsd / sys / proc.h
1 /*
2 * Copyright (c) 2000-2002 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22 /* Copyright (c) 1995, 1997 Apple Computer, Inc. All Rights Reserved */
23 /*-
24 * Copyright (c) 1986, 1989, 1991, 1993
25 * The Regents of the University of California. All rights reserved.
26 * (c) UNIX System Laboratories, Inc.
27 * All or some portions of this file are derived from material licensed
28 * to the University of California by American Telephone and Telegraph
29 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
30 * the permission of UNIX System Laboratories, Inc.
31 *
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
34 * are met:
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by the University of
43 * California, Berkeley and its contributors.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
47 *
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * SUCH DAMAGE.
59 *
60 * @(#)proc.h 8.15 (Berkeley) 5/19/95
61 */
62
63 #ifndef _SYS_PROC_H_
64 #define _SYS_PROC_H_
65
66 #include <sys/appleapiopts.h>
67 #include <sys/cdefs.h>
68 #include <sys/select.h> /* For struct selinfo. */
69 #include <sys/queue.h>
70 #include <sys/lock.h>
71 #include <sys/param.h>
72
73 #ifdef __APPLE_API_PRIVATE
74
75 /*
76 * One structure allocated per session.
77 */
78 struct session {
79 int s_count; /* Ref cnt; pgrps in session. */
80 struct proc *s_leader; /* Session leader. */
81 struct vnode *s_ttyvp; /* Vnode of controlling terminal. */
82 struct tty *s_ttyp; /* Controlling terminal. */
83 pid_t s_sid; /* Session ID */
84 char s_login[MAXLOGNAME]; /* Setlogin() name. */
85 };
86
87 /*
88 * One structure allocated per process group.
89 */
90 struct pgrp {
91 LIST_ENTRY(pgrp) pg_hash; /* Hash chain. */
92 LIST_HEAD(, proc) pg_members; /* Pointer to pgrp members. */
93 struct session *pg_session; /* Pointer to session. */
94 pid_t pg_id; /* Pgrp id. */
95 int pg_jobc; /* # procs qualifying pgrp for job control */
96 };
97
98 /*
99 * Description of a process.
100 *
101 * This structure contains the information needed to manage a thread of
102 * control, known in UN*X as a process; it has references to substructures
103 * containing descriptions of things that the process uses, but may share
104 * with related processes. The process structure and the substructures
105 * are always addressible except for those marked "(PROC ONLY)" below,
106 * which might be addressible only on a processor on which the process
107 * is running.
108 */
109 struct proc {
110 LIST_ENTRY(proc) p_list; /* List of all processes. */
111
112 /* substructures: */
113 struct pcred *p_cred; /* Process owner's identity. */
114 struct filedesc *p_fd; /* Ptr to open files structure. */
115 struct pstats *p_stats; /* Accounting/statistics (PROC ONLY). */
116 struct plimit *p_limit; /* Process limits. */
117 struct sigacts *p_sigacts; /* Signal actions, state (PROC ONLY). */
118
119 #define p_ucred p_cred->pc_ucred
120 #define p_rlimit p_limit->pl_rlimit
121
122 int p_flag; /* P_* flags. */
123 char p_stat; /* S* process status. */
124 char p_pad1[3];
125
126 pid_t p_pid; /* Process identifier. */
127 LIST_ENTRY(proc) p_pglist; /* List of processes in pgrp. */
128 struct proc *p_pptr; /* Pointer to parent process. */
129 LIST_ENTRY(proc) p_sibling; /* List of sibling processes. */
130 LIST_HEAD(, proc) p_children; /* Pointer to list of children. */
131
132 /* The following fields are all zeroed upon creation in fork. */
133 #define p_startzero p_oppid
134
135 pid_t p_oppid; /* Save parent pid during ptrace. XXX */
136 int p_dupfd; /* Sideways return value from fdopen. XXX */
137
138 /* scheduling */
139 u_int p_estcpu; /* Time averaged value of p_cpticks. */
140 int p_cpticks; /* Ticks of cpu time. */
141 fixpt_t p_pctcpu; /* %cpu for this process during p_swtime */
142 void *p_wchan; /* Sleep address. */
143 char *p_wmesg; /* Reason for sleep. */
144 u_int p_swtime; /* Time swapped in or out. */
145 u_int p_slptime; /* Time since last blocked. */
146
147 struct itimerval p_realtimer; /* Alarm timer. */
148 struct timeval p_rtime; /* Real time. */
149 u_quad_t p_uticks; /* Statclock hits in user mode. */
150 u_quad_t p_sticks; /* Statclock hits in system mode. */
151 u_quad_t p_iticks; /* Statclock hits processing intr. */
152
153 int p_traceflag; /* Kernel trace points. */
154 struct vnode *p_tracep; /* Trace to vnode. */
155
156 sigset_t p_siglist; /* DEPRECATED. */
157
158 struct vnode *p_textvp; /* Vnode of executable. */
159
160 /* End area that is zeroed on creation. */
161 #define p_endzero p_hash.le_next
162
163 /*
164 * Not copied, not zero'ed.
165 * Belongs after p_pid, but here to avoid shifting proc elements.
166 */
167 LIST_ENTRY(proc) p_hash; /* Hash chain. */
168 TAILQ_HEAD( ,eventqelt) p_evlist;
169
170 /* The following fields are all copied upon creation in fork. */
171 #define p_startcopy p_sigmask
172
173 sigset_t p_sigmask; /* DEPRECATED */
174 sigset_t p_sigignore; /* Signals being ignored. */
175 sigset_t p_sigcatch; /* Signals being caught by user. */
176
177 u_char p_priority; /* Process priority. */
178 u_char p_usrpri; /* User-priority based on p_cpu and p_nice. */
179 char p_nice; /* Process "nice" value. */
180 char p_comm[MAXCOMLEN+1];
181
182 struct pgrp *p_pgrp; /* Pointer to process group. */
183
184 /* End area that is copied on creation. */
185 #define p_endcopy p_xstat
186
187 u_short p_xstat; /* Exit status for wait; also stop signal. */
188 u_short p_acflag; /* Accounting flags. */
189 struct rusage *p_ru; /* Exit information. XXX */
190
191 int p_debugger; /* 1: can exec set-bit programs if suser */
192
193 void *task; /* corresponding task */
194 void *sigwait_thread; /* 'thread' holding sigwait */
195 struct lock__bsd__ signal_lock; /* multilple thread prot for signals*/
196 boolean_t sigwait; /* indication to suspend */
197 void *exit_thread; /* Which thread is exiting? */
198 caddr_t user_stack; /* where user stack was allocated */
199 void * exitarg; /* exit arg for proc terminate */
200 void * vm_shm; /* for sysV shared memory */
201 sigset_t p_xxxsigpending; /* DEPRECATED . */
202 int p_vforkcnt; /* number of outstanding vforks */
203 void * p_vforkact; /* activation running this vfork proc */
204 TAILQ_HEAD( , uthread) p_uthlist; /* List of uthreads */
205 /* Following fields are info from SIGCHLD */
206 pid_t si_pid;
207 u_short si_status;
208 u_short si_code;
209 uid_t si_uid;
210 #if DIAGNOSTIC
211 #if SIGNAL_DEBUG
212 unsigned int lockpc[8];
213 unsigned int unlockpc[8];
214 #endif /* SIGNAL_DEBUG */
215 #endif /* DIAGNOSTIC */
216 };
217
218 #else /* __APPLE_API_PRIVATE */
219 struct session;
220 struct pgrp;
221 struct proc;
222 #endif /* __APPLE_API_PRIVATE */
223
224 #ifdef __APPLE_API_UNSTABLE
225 /* Exported fields for kern sysctls */
226 struct extern_proc {
227 union {
228 struct {
229 struct proc *__p_forw; /* Doubly-linked run/sleep queue. */
230 struct proc *__p_back;
231 } p_st1;
232 struct timeval __p_starttime; /* process start time */
233 } p_un;
234 #define p_forw p_un.p_st1.__p_forw
235 #define p_back p_un.p_st1.__p_back
236 #define p_starttime p_un.__p_starttime
237 struct vmspace *p_vmspace; /* Address space. */
238 struct sigacts *p_sigacts; /* Signal actions, state (PROC ONLY). */
239 int p_flag; /* P_* flags. */
240 char p_stat; /* S* process status. */
241 pid_t p_pid; /* Process identifier. */
242 pid_t p_oppid; /* Save parent pid during ptrace. XXX */
243 int p_dupfd; /* Sideways return value from fdopen. XXX */
244 /* Mach related */
245 caddr_t user_stack; /* where user stack was allocated */
246 void *exit_thread; /* XXX Which thread is exiting? */
247 int p_debugger; /* allow to debug */
248 boolean_t sigwait; /* indication to suspend */
249 /* scheduling */
250 u_int p_estcpu; /* Time averaged value of p_cpticks. */
251 int p_cpticks; /* Ticks of cpu time. */
252 fixpt_t p_pctcpu; /* %cpu for this process during p_swtime */
253 void *p_wchan; /* Sleep address. */
254 char *p_wmesg; /* Reason for sleep. */
255 u_int p_swtime; /* Time swapped in or out. */
256 u_int p_slptime; /* Time since last blocked. */
257 struct itimerval p_realtimer; /* Alarm timer. */
258 struct timeval p_rtime; /* Real time. */
259 u_quad_t p_uticks; /* Statclock hits in user mode. */
260 u_quad_t p_sticks; /* Statclock hits in system mode. */
261 u_quad_t p_iticks; /* Statclock hits processing intr. */
262 int p_traceflag; /* Kernel trace points. */
263 struct vnode *p_tracep; /* Trace to vnode. */
264 int p_siglist; /* DEPRECATED */
265 struct vnode *p_textvp; /* Vnode of executable. */
266 int p_holdcnt; /* If non-zero, don't swap. */
267 sigset_t p_sigmask; /* DEPRECATED. */
268 sigset_t p_sigignore; /* Signals being ignored. */
269 sigset_t p_sigcatch; /* Signals being caught by user. */
270 u_char p_priority; /* Process priority. */
271 u_char p_usrpri; /* User-priority based on p_cpu and p_nice. */
272 char p_nice; /* Process "nice" value. */
273 char p_comm[MAXCOMLEN+1];
274 struct pgrp *p_pgrp; /* Pointer to process group. */
275 struct user *p_addr; /* Kernel virtual addr of u-area (PROC ONLY). */
276 u_short p_xstat; /* Exit status for wait; also stop signal. */
277 u_short p_acflag; /* Accounting flags. */
278 struct rusage *p_ru; /* Exit information. XXX */
279 };
280
281 #define p_session p_pgrp->pg_session
282 #define p_pgid p_pgrp->pg_id
283
284 /* Status values. */
285 #define SIDL 1 /* Process being created by fork. */
286 #define SRUN 2 /* Currently runnable. */
287 #define SSLEEP 3 /* Sleeping on an address. */
288 #define SSTOP 4 /* Process debugging or suspension. */
289 #define SZOMB 5 /* Awaiting collection by parent. */
290
291 /* These flags are kept in p_flags. */
292 #define P_ADVLOCK 0x00001 /* Process may hold a POSIX advisory lock. */
293 #define P_CONTROLT 0x00002 /* Has a controlling terminal. */
294 #define P_INMEM 0x00004 /* Loaded into memory. */
295 #define P_NOCLDSTOP 0x00008 /* No SIGCHLD when children stop. */
296 #define P_PPWAIT 0x00010 /* Parent is waiting for child to exec/exit. */
297 #define P_PROFIL 0x00020 /* Has started profiling. */
298 #define P_SELECT 0x00040 /* Selecting; wakeup/waiting danger. */
299 #define P_SINTR 0x00080 /* Sleep is interruptible. */
300 #define P_SUGID 0x00100 /* Had set id privileges since last exec. */
301 #define P_SYSTEM 0x00200 /* System proc: no sigs, stats or swapping. */
302 #define P_TIMEOUT 0x00400 /* Timing out during sleep. */
303 #define P_TRACED 0x00800 /* Debugged process being traced. */
304 #define P_WAITED 0x01000 /* Debugging process has waited for child. */
305 #define P_WEXIT 0x02000 /* Working on exiting. */
306 #define P_EXEC 0x04000 /* Process called exec. */
307
308 /* Should be moved to machine-dependent areas. */
309 #define P_OWEUPC 0x08000 /* Owe process an addupc() call at next ast. */
310
311 /* XXX Not sure what to do with these, yet. */
312 #define P_FSTRACE 0x10000 /* tracing via file system (elsewhere?) */
313 #define P_SSTEP 0x20000 /* process needs single-step fixup ??? */
314
315 #define P_WAITING 0x0040000 /* process has a wait() in progress */
316 #define P_KDEBUG 0x0080000 /* kdebug tracing is on for this process */
317 #define P_TTYSLEEP 0x0100000 /* blocked due to SIGTTOU or SIGTTIN */
318 #define P_REBOOT 0x0200000 /* Process called reboot() */
319 #define P_TBE 0x0400000 /* Process is TBE */
320 #define P_SIGEXC 0x0800000 /* signal exceptions */
321 #define P_BTRACE 0x1000000 /* process is being branch traced */
322 #define P_VFORK 0x2000000 /* process has vfork children */
323 #define P_NOATTACH 0x4000000
324 #define P_INVFORK 0x8000000 /* proc in vfork */
325 #define P_NOSHLIB 0x10000000 /* no shared libs are in use for proc */
326 /* flag set on exec */
327 #define P_FORCEQUOTA 0x20000000 /* Force quota for root */
328 #define P_NOCLDWAIT 0x40000000 /* No zombies when chil procs exit */
329
330 #define P_NOSWAP 0 /* Obsolete: retained so that nothing breaks */
331 #define P_PHYSIO 0 /* Obsolete: retained so that nothing breaks */
332
333 /*
334 * Shareable process credentials (always resident). This includes a reference
335 * to the current user credentials as well as real and saved ids that may be
336 * used to change ids.
337 */
338 struct pcred {
339 struct lock__bsd__ pc_lock;
340 struct ucred *pc_ucred; /* Current credentials. */
341 uid_t p_ruid; /* Real user id. */
342 uid_t p_svuid; /* Saved effective user id. */
343 gid_t p_rgid; /* Real group id. */
344 gid_t p_svgid; /* Saved effective group id. */
345 int p_refcnt; /* Number of references. */
346 };
347
348 #define pcred_readlock(p) lockmgr(&(p)->p_cred->pc_lock, \
349 LK_SHARED, 0, (p))
350 #define pcred_writelock(p) lockmgr(&(p)->p_cred->pc_lock, \
351 LK_EXCLUSIVE, 0, (p))
352 #define pcred_unlock(p) lockmgr(&(p)->p_cred->pc_lock, \
353 LK_RELEASE, 0, (p))
354 #endif /* __APPLE_API_UNSTABLE */
355
356 #ifdef KERNEL
357
358 __BEGIN_DECLS
359 #ifdef __APPLE_API_PRIVATE
360 /*
361 * We use process IDs <= PID_MAX; PID_MAX + 1 must also fit in a pid_t,
362 * as it is used to represent "no process group".
363 */
364 extern int nprocs, maxproc; /* Current and max number of procs. */
365
366 #define PID_MAX 30000
367 #define NO_PID 30001
368
369 #define SESS_LEADER(p) ((p)->p_session->s_leader == (p))
370 #define SESSHOLD(s) ((s)->s_count++)
371 #define SESSRELE(s) sessrele(s)
372
373 #define PIDHASH(pid) (&pidhashtbl[(pid) & pidhash])
374 extern LIST_HEAD(pidhashhead, proc) *pidhashtbl;
375 extern u_long pidhash;
376
377 #define PGRPHASH(pgid) (&pgrphashtbl[(pgid) & pgrphash])
378 extern LIST_HEAD(pgrphashhead, pgrp) *pgrphashtbl;
379 extern u_long pgrphash;
380
381 LIST_HEAD(proclist, proc);
382 extern struct proclist allproc; /* List of all processes. */
383 extern struct proclist zombproc; /* List of zombie processes. */
384 extern struct proc *initproc, *kernproc;
385 extern void pgdelete __P((struct pgrp *pgrp));
386 extern void sessrele __P((struct session *sess));
387 extern void procinit __P((void));
388 #endif /* __APPLE_API_PRIVATE */
389
390 #ifdef __APPLE_API_UNSTABLE
391
392 extern struct proc *pfind __P((pid_t)); /* Find process by id. */
393 extern struct pgrp *pgfind __P((pid_t)); /* Find process group by id. */
394
395 extern int chgproccnt __P((uid_t uid, int diff));
396 extern int enterpgrp __P((struct proc *p, pid_t pgid, int mksess));
397 extern void fixjobc __P((struct proc *p, struct pgrp *pgrp, int entering));
398 extern int inferior __P((struct proc *p));
399 extern int leavepgrp __P((struct proc *p));
400 #ifdef __APPLE_API_OBSOLETE
401 extern void mi_switch __P((void));
402 #endif /* __APPLE_API_OBSOLETE */
403 extern void resetpriority __P((struct proc *));
404 extern void setrunnable __P((struct proc *));
405 extern void setrunqueue __P((struct proc *));
406 extern int sleep __P((void *chan, int pri));
407 extern int tsleep __P((void *chan, int pri, char *wmesg, int timo));
408 extern int tsleep0 __P((void *chan, int pri, char *wmesg, int timo, int (*continuation)(int)));
409 extern int tsleep1 __P((void *chan, int pri, char *wmesg, u_int64_t abstime, int (*continuation)(int)));
410 extern void unsleep __P((struct proc *));
411 extern void wakeup __P((void *chan));
412 #endif /* __APPLE_API_UNSTABLE */
413
414 __END_DECLS
415
416 #ifdef __APPLE_API_OBSOLETE
417 /* FreeBSD source compatibility macro */
418 #define PRISON_CHECK(p1, p2) (1)
419 #endif /* __APPLE_API_OBSOLETE */
420
421 #endif /* KERNEL */
422
423 #endif /* !_SYS_PROC_H_ */