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