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