]> git.saurik.com Git - apple/xnu.git/blob - bsd/sys/proc_internal.h
xnu-792.24.17.tar.gz
[apple/xnu.git] / bsd / sys / proc_internal.h
1 /*
2 * Copyright (c) 2000-2005 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_internal.h 8.15 (Berkeley) 5/19/95
61 */
62
63 #ifndef _SYS_PROC_INTERNAL_H_
64 #define _SYS_PROC_INTERNAL_H_
65
66 #include <sys/proc.h>
67 __BEGIN_DECLS
68 #include <kern/locks.h>
69 __END_DECLS
70
71 /*
72 * One structure allocated per session.
73 */
74 struct session {
75 int s_count; /* Ref cnt; pgrps in session. */
76 struct proc *s_leader; /* Session leader. */
77 struct vnode *s_ttyvp; /* Vnode of controlling terminal. */
78 struct tty *s_ttyp; /* Controlling terminal. */
79 pid_t s_sid; /* Session ID */
80 char s_login[MAXLOGNAME]; /* Setlogin() name. */
81 };
82
83 /*
84 * One structure allocated per process group.
85 */
86 struct pgrp {
87 LIST_ENTRY(pgrp) pg_hash; /* Hash chain. */
88 LIST_HEAD(, proc) pg_members; /* Pointer to pgrp members. */
89 struct session *pg_session; /* Pointer to session. */
90 pid_t pg_id; /* Pgrp id. */
91 int pg_jobc; /* # procs qualifying pgrp for job control */
92 };
93
94 struct proc;
95
96 #define PROC_NULL (struct proc *)0
97
98 #define p_session p_pgrp->pg_session
99 #define p_pgid p_pgrp->pg_id
100
101 /*
102 * Description of a process.
103 *
104 * This structure contains the information needed to manage a thread of
105 * control, known in UN*X as a process; it has references to substructures
106 * containing descriptions of things that the process uses, but may share
107 * with related processes. The process structure and the substructures
108 * are always addressible except for those marked "(PROC ONLY)" below,
109 * which might be addressible only on a processor on which the process
110 * is running.
111 */
112 struct proc {
113 LIST_ENTRY(proc) p_list; /* List of all processes. */
114
115 /* substructures: */
116 struct ucred *p_ucred; /* Process owner's identity. */
117 struct filedesc *p_fd; /* Ptr to open files structure. */
118 struct pstats *p_stats; /* Accounting/statistics (PROC ONLY). */
119 struct plimit *p_limit; /* Process limits. */
120 struct sigacts *p_sigacts; /* Signal actions, state (PROC ONLY). */
121
122 #define p_rlimit p_limit->pl_rlimit
123
124 int p_flag; /* P_* flags. */
125 char p_stat; /* S* process status. */
126 char p_shutdownstate;
127 char p_pad1[2];
128
129 pid_t p_pid; /* Process identifier. */
130 LIST_ENTRY(proc) p_pglist; /* List of processes in pgrp. */
131 struct proc *p_pptr; /* Pointer to parent process. */
132 LIST_ENTRY(proc) p_sibling; /* List of sibling processes. */
133 LIST_HEAD(, proc) p_children; /* Pointer to list of children. */
134
135 /* The following fields are all zeroed upon creation in fork. */
136 #define p_startzero p_oppid
137
138 pid_t p_oppid; /* Save parent pid during ptrace. XXX */
139 int p_dupfd; /* Sideways return value from fdopen. XXX */
140
141 /* scheduling */
142 u_int p_estcpu; /* Time averaged value of p_cpticks. */
143 int p_cpticks; /* Ticks of cpu time. */
144 fixpt_t p_pctcpu; /* %cpu for this process during p_swtime */
145 void *p_wchan; /* Sleep address. */
146 char *p_wmesg; /* Reason for sleep. */
147 u_int p_swtime; /* DEPRECATED (Time swapped in or out.) */
148 #define p_argslen p_swtime /* Length of process arguments. */
149 u_int p_slptime; /* Time since last blocked. */
150
151 struct itimerval p_realtimer; /* Alarm timer. */
152 struct timeval p_rtime; /* Real time. */
153 u_quad_t p_uticks; /* Statclock hits in user mode. */
154 u_quad_t p_sticks; /* Statclock hits in system mode. */
155 u_quad_t p_iticks; /* Statclock hits processing intr. */
156
157 int p_traceflag; /* Kernel trace points. */
158 struct vnode *p_tracep; /* Trace to vnode. */
159
160 sigset_t p_siglist; /* DEPRECATED. */
161
162 struct vnode *p_textvp; /* Vnode of executable. */
163
164 /* End area that is zeroed on creation. */
165 #define p_endzero p_hash.le_next
166
167 /*
168 * Not copied, not zero'ed.
169 * Belongs after p_pid, but here to avoid shifting proc elements.
170 */
171 LIST_ENTRY(proc) p_hash; /* Hash chain. */
172 TAILQ_HEAD( ,eventqelt) p_evlist;
173
174 /* The following fields are all copied upon creation in fork. */
175 #define p_startcopy p_sigmask
176
177 sigset_t p_sigmask; /* DEPRECATED */
178 sigset_t p_sigignore; /* Signals being ignored. */
179 sigset_t p_sigcatch; /* Signals being caught by user. */
180
181 u_char p_priority; /* Process priority. */
182 u_char p_usrpri; /* User-priority based on p_cpu and p_nice. */
183 char p_nice; /* Process "nice" value. */
184 char p_comm[MAXCOMLEN+1];
185
186 struct pgrp *p_pgrp; /* Pointer to process group. */
187
188 /* End area that is copied on creation. */
189 #define p_endcopy p_xstat
190
191 u_short p_xstat; /* Exit status for wait; also stop signal. */
192 u_short p_acflag; /* Accounting flags. */
193 struct rusage *p_ru; /* Exit information. XXX */
194
195 int p_debugger; /* 1: can exec set-bit programs if suser */
196
197 void *task; /* corresponding task */
198 void *sigwait_thread; /* 'thread' holding sigwait */
199 char signal_lock[72];
200 boolean_t sigwait; /* indication to suspend */
201 void *exit_thread; /* Which thread is exiting? */
202 user_addr_t user_stack; /* where user stack was allocated */
203 void * exitarg; /* exit arg for proc terminate */
204 void * vm_shm; /* for sysV shared memory */
205 int p_argc; /* saved argc for sysctl_procargs() */
206 int p_vforkcnt; /* number of outstanding vforks */
207 void * p_vforkact; /* activation running this vfork proc */
208 TAILQ_HEAD( , uthread) p_uthlist; /* List of uthreads */
209 /* Following fields are info from SIGCHLD */
210 pid_t si_pid;
211 u_short si_status;
212 u_short si_code;
213 uid_t si_uid;
214 TAILQ_HEAD( , aio_workq_entry ) aio_activeq; /* active async IO requests */
215 int aio_active_count; /* entries on aio_activeq */
216 TAILQ_HEAD( , aio_workq_entry ) aio_doneq; /* completed async IO requests */
217 int aio_done_count; /* entries on aio_doneq */
218
219 struct klist p_klist; /* knote list */
220 lck_mtx_t p_mlock; /* proc lock to protect evques */
221 lck_mtx_t p_fdmlock; /* proc lock to protect evques */
222 unsigned int p_fdlock_pc[4];
223 unsigned int p_fdunlock_pc[4];
224 int p_fpdrainwait;
225 unsigned int p_lflag; /* local flags */
226 unsigned int p_ladvflag; /* local adv flags*/
227 unsigned int p_internalref; /* temp refcount field */
228 #if DIAGNOSTIC
229 #if SIGNAL_DEBUG
230 unsigned int lockpc[8];
231 unsigned int unlockpc[8];
232 #endif /* SIGNAL_DEBUG */
233 #endif /* DIAGNOSTIC */
234 };
235
236
237 /* local flags */
238 #define P_LDELAYTERM 0x1 /* */
239 #define P_LNOZOMB 0x2 /* */
240 #define P_LLOW_PRI_IO 0x4
241 #define P_LPEXIT 0x8
242 #define P_LBACKGROUND_IO 0x10
243 #define P_LWAITING 0x20
244 #define P_LREFDRAIN 0x40
245 #define P_LREFDRAINWAIT 0x80
246 #define P_LREFDEAD 0x100
247
248 /* advisory flags in the proc */
249 #define P_LADVLOCK 0x01
250
251 // LP64todo - should this move?
252 /* LP64 version of extern_proc. all pointers
253 * grow when we're dealing with a 64-bit process.
254 * WARNING - keep in sync with extern_proc
255 * but use native alignment of 64-bit process.
256 */
257
258 #ifdef KERNEL
259 #include <sys/time.h> /* user_timeval, user_itimerval */
260
261 #if __DARWIN_ALIGN_NATURAL
262 #pragma options align=natural
263 #endif
264
265 struct user_extern_proc {
266 union {
267 struct {
268 user_addr_t __p_forw; /* Doubly-linked run/sleep queue. */
269 user_addr_t __p_back;
270 } p_st1;
271 struct user_timeval __p_starttime; /* process start time */
272 } p_un;
273 user_addr_t p_vmspace; /* Address space. */
274 user_addr_t p_sigacts; /* Signal actions, state (PROC ONLY). */
275 int p_flag; /* P_* flags. */
276 char p_stat; /* S* process status. */
277 pid_t p_pid; /* Process identifier. */
278 pid_t p_oppid; /* Save parent pid during ptrace. XXX */
279 int p_dupfd; /* Sideways return value from fdopen. XXX */
280 /* Mach related */
281 user_addr_t user_stack; /* where user stack was allocated */
282 user_addr_t exit_thread; /* XXX Which thread is exiting? */
283 int p_debugger; /* allow to debug */
284 boolean_t sigwait; /* indication to suspend */
285 /* scheduling */
286 u_int p_estcpu; /* Time averaged value of p_cpticks. */
287 int p_cpticks; /* Ticks of cpu time. */
288 fixpt_t p_pctcpu; /* %cpu for this process during p_swtime */
289 user_addr_t p_wchan; /* Sleep address. */
290 user_addr_t p_wmesg; /* Reason for sleep. */
291 u_int p_swtime; /* Time swapped in or out. */
292 u_int p_slptime; /* Time since last blocked. */
293 struct user_itimerval p_realtimer; /* Alarm timer. */
294 struct user_timeval p_rtime; /* Real time. */
295 u_quad_t p_uticks; /* Statclock hits in user mode. */
296 u_quad_t p_sticks; /* Statclock hits in system mode. */
297 u_quad_t p_iticks; /* Statclock hits processing intr. */
298 int p_traceflag; /* Kernel trace points. */
299 user_addr_t p_tracep; /* Trace to vnode. */
300 int p_siglist; /* DEPRECATED */
301 user_addr_t p_textvp; /* Vnode of executable. */
302 int p_holdcnt; /* If non-zero, don't swap. */
303 sigset_t p_sigmask; /* DEPRECATED. */
304 sigset_t p_sigignore; /* Signals being ignored. */
305 sigset_t p_sigcatch; /* Signals being caught by user. */
306 u_char p_priority; /* Process priority. */
307 u_char p_usrpri; /* User-priority based on p_cpu and p_nice. */
308 char p_nice; /* Process "nice" value. */
309 char p_comm[MAXCOMLEN+1];
310 user_addr_t p_pgrp; /* Pointer to process group. */
311 user_addr_t p_addr; /* Kernel virtual addr of u-area (PROC ONLY). */
312 u_short p_xstat; /* Exit status for wait; also stop signal. */
313 u_short p_acflag; /* Accounting flags. */
314 user_addr_t p_ru; /* Exit information. XXX */
315 };
316
317 #if __DARWIN_ALIGN_NATURAL
318 #pragma options align=reset
319 #endif
320 #endif /* KERNEL */
321
322 /*
323 * We use process IDs <= PID_MAX; PID_MAX + 1 must also fit in a pid_t,
324 * as it is used to represent "no process group".
325 */
326 extern int nprocs, maxproc; /* Current and max number of procs. */
327 __private_extern__ int hard_maxproc; /* hard limit */
328
329 #define PID_MAX 30000
330 #define NO_PID 30001
331
332 #define SESS_LEADER(p) ((p)->p_session->s_leader == (p))
333 #define SESSHOLD(s) ((s)->s_count++)
334 #define SESSRELE(s) sessrele(s)
335
336 #define PIDHASH(pid) (&pidhashtbl[(pid) & pidhash])
337 extern LIST_HEAD(pidhashhead, proc) *pidhashtbl;
338 extern u_long pidhash;
339
340 #define PGRPHASH(pgid) (&pgrphashtbl[(pgid) & pgrphash])
341 extern LIST_HEAD(pgrphashhead, pgrp) *pgrphashtbl;
342 extern u_long pgrphash;
343 extern lck_grp_t * proc_lck_grp;
344 extern lck_grp_attr_t * proc_lck_grp_attr;
345 extern lck_attr_t * proc_lck_attr;
346
347 LIST_HEAD(proclist, proc);
348 extern struct proclist allproc; /* List of all processes. */
349 extern struct proclist zombproc; /* List of zombie processes. */
350 extern struct proc *initproc;
351 extern void pgdelete(struct pgrp *pgrp);
352 extern void sessrele(struct session *sess);
353 extern void procinit(void);
354 extern void proc_lock(struct proc *);
355 extern void proc_unlock(struct proc *);
356 extern void proc_fdlock(struct proc *);
357 extern void proc_fdunlock(struct proc *);
358 __private_extern__ char *proc_core_name(const char *name, uid_t uid, pid_t pid);
359 extern int isinferior(struct proc *, struct proc *);
360 extern struct proc *pfind(pid_t); /* Find process by id. */
361 __private_extern__ struct proc *pzfind(pid_t); /* Find zombie by id. */
362 extern struct pgrp *pgfind(pid_t); /* Find process group by id. */
363
364 extern int chgproccnt(uid_t uid, int diff);
365 extern int enterpgrp(struct proc *p, pid_t pgid, int mksess);
366 extern void fixjobc(struct proc *p, struct pgrp *pgrp, int entering);
367 extern int inferior(struct proc *p);
368 extern int leavepgrp(struct proc *p);
369 extern void resetpriority(struct proc *);
370 extern void setrunnable(struct proc *);
371 extern void setrunqueue(struct proc *);
372 extern int sleep(void *chan, int pri);
373 extern int tsleep0(void *chan, int pri, const char *wmesg, int timo, int (*continuation)(int));
374 extern int tsleep1(void *chan, int pri, const char *wmesg, u_int64_t abstime, int (*continuation)(int));
375 extern int msleep0(void *chan, lck_mtx_t *mtx, int pri, const char *wmesg, int timo, int (*continuation)(int));
376 extern void vfork_return(thread_t th_act, struct proc *p, struct proc *p2, register_t *retval);
377 extern struct proc * proc_findref(pid_t pid);
378 extern void proc_dropref(struct proc * p);
379 extern struct proc * proc_refinternal(proc_t p, int funneled);
380 extern void proc_dropinternal(struct proc * p, int funneled);
381
382 #endif /* !_SYS_PROC_INTERNAL_H_ */