]> git.saurik.com Git - apple/xnu.git/blob - bsd/sys/proc_internal.h
4d5533da029e239974f807433a4e164bb3d37c8b
[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 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23 /* Copyright (c) 1995, 1997 Apple Computer, Inc. All Rights Reserved */
24 /*-
25 * Copyright (c) 1986, 1989, 1991, 1993
26 * The Regents of the University of California. All rights reserved.
27 * (c) UNIX System Laboratories, Inc.
28 * All or some portions of this file are derived from material licensed
29 * to the University of California by American Telephone and Telegraph
30 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
31 * the permission of UNIX System Laboratories, Inc.
32 *
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
35 * are met:
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 3. All advertising materials mentioning features or use of this software
42 * must display the following acknowledgement:
43 * This product includes software developed by the University of
44 * California, Berkeley and its contributors.
45 * 4. Neither the name of the University nor the names of its contributors
46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission.
48 *
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 * SUCH DAMAGE.
60 *
61 * @(#)proc_internal.h 8.15 (Berkeley) 5/19/95
62 */
63
64 #ifndef _SYS_PROC_INTERNAL_H_
65 #define _SYS_PROC_INTERNAL_H_
66
67 #include <sys/proc.h>
68 __BEGIN_DECLS
69 #include <kern/locks.h>
70 __END_DECLS
71
72 /*
73 * One structure allocated per session.
74 */
75 struct session {
76 int s_count; /* Ref cnt; pgrps in session. */
77 struct proc *s_leader; /* Session leader. */
78 struct vnode *s_ttyvp; /* Vnode of controlling terminal. */
79 struct tty *s_ttyp; /* Controlling terminal. */
80 pid_t s_sid; /* Session ID */
81 char s_login[MAXLOGNAME]; /* Setlogin() name. */
82 };
83
84 /*
85 * One structure allocated per process group.
86 */
87 struct pgrp {
88 LIST_ENTRY(pgrp) pg_hash; /* Hash chain. */
89 LIST_HEAD(, proc) pg_members; /* Pointer to pgrp members. */
90 struct session *pg_session; /* Pointer to session. */
91 pid_t pg_id; /* Pgrp id. */
92 int pg_jobc; /* # procs qualifying pgrp for job control */
93 };
94
95 struct proc;
96
97 #define PROC_NULL (struct proc *)0
98
99 #define p_session p_pgrp->pg_session
100 #define p_pgid p_pgrp->pg_id
101
102 /*
103 * Description of a process.
104 *
105 * This structure contains the information needed to manage a thread of
106 * control, known in UN*X as a process; it has references to substructures
107 * containing descriptions of things that the process uses, but may share
108 * with related processes. The process structure and the substructures
109 * are always addressible except for those marked "(PROC ONLY)" below,
110 * which might be addressible only on a processor on which the process
111 * is running.
112 */
113 struct proc {
114 LIST_ENTRY(proc) p_list; /* List of all processes. */
115
116 /* substructures: */
117 struct ucred *p_ucred; /* Process owner's identity. */
118 struct filedesc *p_fd; /* Ptr to open files structure. */
119 struct pstats *p_stats; /* Accounting/statistics (PROC ONLY). */
120 struct plimit *p_limit; /* Process limits. */
121 struct sigacts *p_sigacts; /* Signal actions, state (PROC ONLY). */
122
123 #define p_rlimit p_limit->pl_rlimit
124
125 int p_flag; /* P_* flags. */
126 char p_stat; /* S* process status. */
127 char p_shutdownstate;
128 char p_pad1[2];
129
130 pid_t p_pid; /* Process identifier. */
131 LIST_ENTRY(proc) p_pglist; /* List of processes in pgrp. */
132 struct proc *p_pptr; /* Pointer to parent process. */
133 LIST_ENTRY(proc) p_sibling; /* List of sibling processes. */
134 LIST_HEAD(, proc) p_children; /* Pointer to list of children. */
135
136 /* The following fields are all zeroed upon creation in fork. */
137 #define p_startzero p_oppid
138
139 pid_t p_oppid; /* Save parent pid during ptrace. XXX */
140 int p_dupfd; /* Sideways return value from fdopen. XXX */
141
142 /* scheduling */
143 u_int p_estcpu; /* Time averaged value of p_cpticks. */
144 int p_cpticks; /* Ticks of cpu time. */
145 fixpt_t p_pctcpu; /* %cpu for this process during p_swtime */
146 void *p_wchan; /* Sleep address. */
147 char *p_wmesg; /* Reason for sleep. */
148 u_int p_swtime; /* DEPRECATED (Time swapped in or out.) */
149 #define p_argslen p_swtime /* Length of process arguments. */
150 u_int p_slptime; /* Time since last blocked. */
151
152 struct itimerval p_realtimer; /* Alarm timer. */
153 struct timeval p_rtime; /* Real time. */
154 u_quad_t p_uticks; /* Statclock hits in user mode. */
155 u_quad_t p_sticks; /* Statclock hits in system mode. */
156 u_quad_t p_iticks; /* Statclock hits processing intr. */
157
158 int p_traceflag; /* Kernel trace points. */
159 struct vnode *p_tracep; /* Trace to vnode. */
160
161 sigset_t p_siglist; /* DEPRECATED. */
162
163 struct vnode *p_textvp; /* Vnode of executable. */
164
165 /* End area that is zeroed on creation. */
166 #define p_endzero p_hash.le_next
167
168 /*
169 * Not copied, not zero'ed.
170 * Belongs after p_pid, but here to avoid shifting proc elements.
171 */
172 LIST_ENTRY(proc) p_hash; /* Hash chain. */
173 TAILQ_HEAD( ,eventqelt) p_evlist;
174
175 /* The following fields are all copied upon creation in fork. */
176 #define p_startcopy p_sigmask
177
178 sigset_t p_sigmask; /* DEPRECATED */
179 sigset_t p_sigignore; /* Signals being ignored. */
180 sigset_t p_sigcatch; /* Signals being caught by user. */
181
182 u_char p_priority; /* Process priority. */
183 u_char p_usrpri; /* User-priority based on p_cpu and p_nice. */
184 char p_nice; /* Process "nice" value. */
185 char p_comm[MAXCOMLEN+1];
186
187 struct pgrp *p_pgrp; /* Pointer to process group. */
188
189 /* End area that is copied on creation. */
190 #define p_endcopy p_xstat
191
192 u_short p_xstat; /* Exit status for wait; also stop signal. */
193 u_short p_acflag; /* Accounting flags. */
194 struct rusage *p_ru; /* Exit information. XXX */
195
196 int p_debugger; /* 1: can exec set-bit programs if suser */
197
198 void *task; /* corresponding task */
199 void *sigwait_thread; /* 'thread' holding sigwait */
200 char signal_lock[72];
201 boolean_t sigwait; /* indication to suspend */
202 void *exit_thread; /* Which thread is exiting? */
203 user_addr_t user_stack; /* where user stack was allocated */
204 void * exitarg; /* exit arg for proc terminate */
205 void * vm_shm; /* for sysV shared memory */
206 int p_argc; /* saved argc for sysctl_procargs() */
207 int p_vforkcnt; /* number of outstanding vforks */
208 void * p_vforkact; /* activation running this vfork proc */
209 TAILQ_HEAD( , uthread) p_uthlist; /* List of uthreads */
210 /* Following fields are info from SIGCHLD */
211 pid_t si_pid;
212 u_short si_status;
213 u_short si_code;
214 uid_t si_uid;
215 TAILQ_HEAD( , aio_workq_entry ) aio_activeq; /* active async IO requests */
216 int aio_active_count; /* entries on aio_activeq */
217 TAILQ_HEAD( , aio_workq_entry ) aio_doneq; /* completed async IO requests */
218 int aio_done_count; /* entries on aio_doneq */
219
220 struct klist p_klist; /* knote list */
221 lck_mtx_t p_mlock; /* proc lock to protect evques */
222 lck_mtx_t p_fdmlock; /* proc lock to protect evques */
223 unsigned int p_fdlock_pc[4];
224 unsigned int p_fdunlock_pc[4];
225 int p_fpdrainwait;
226 unsigned int p_lflag; /* local flags */
227 unsigned int p_ladvflag; /* local adv flags*/
228 unsigned int p_internalref; /* temp refcount field */
229 #if DIAGNOSTIC
230 #if SIGNAL_DEBUG
231 unsigned int lockpc[8];
232 unsigned int unlockpc[8];
233 #endif /* SIGNAL_DEBUG */
234 #endif /* DIAGNOSTIC */
235 };
236
237
238 /* local flags */
239 #define P_LDELAYTERM 0x1 /* */
240 #define P_LNOZOMB 0x2 /* */
241 #define P_LLOW_PRI_IO 0x4
242 #define P_LPEXIT 0x8
243 #define P_LBACKGROUND_IO 0x10
244 #define P_LWAITING 0x20
245 #define P_LREFDRAIN 0x40
246 #define P_LREFDRAINWAIT 0x80
247 #define P_LREFDEAD 0x100
248
249 /* advisory flags in the proc */
250 #define P_LADVLOCK 0x01
251
252 // LP64todo - should this move?
253 /* LP64 version of extern_proc. all pointers
254 * grow when we're dealing with a 64-bit process.
255 * WARNING - keep in sync with extern_proc
256 * but use native alignment of 64-bit process.
257 */
258
259 #ifdef KERNEL
260 #include <sys/time.h> /* user_timeval, user_itimerval */
261
262 #if __DARWIN_ALIGN_NATURAL
263 #pragma options align=natural
264 #endif
265
266 struct user_extern_proc {
267 union {
268 struct {
269 user_addr_t __p_forw; /* Doubly-linked run/sleep queue. */
270 user_addr_t __p_back;
271 } p_st1;
272 struct user_timeval __p_starttime; /* process start time */
273 } p_un;
274 user_addr_t p_vmspace; /* Address space. */
275 user_addr_t p_sigacts; /* Signal actions, state (PROC ONLY). */
276 int p_flag; /* P_* flags. */
277 char p_stat; /* S* process status. */
278 pid_t p_pid; /* Process identifier. */
279 pid_t p_oppid; /* Save parent pid during ptrace. XXX */
280 int p_dupfd; /* Sideways return value from fdopen. XXX */
281 /* Mach related */
282 user_addr_t user_stack; /* where user stack was allocated */
283 user_addr_t exit_thread; /* XXX Which thread is exiting? */
284 int p_debugger; /* allow to debug */
285 boolean_t sigwait; /* indication to suspend */
286 /* scheduling */
287 u_int p_estcpu; /* Time averaged value of p_cpticks. */
288 int p_cpticks; /* Ticks of cpu time. */
289 fixpt_t p_pctcpu; /* %cpu for this process during p_swtime */
290 user_addr_t p_wchan; /* Sleep address. */
291 user_addr_t p_wmesg; /* Reason for sleep. */
292 u_int p_swtime; /* Time swapped in or out. */
293 u_int p_slptime; /* Time since last blocked. */
294 struct user_itimerval p_realtimer; /* Alarm timer. */
295 struct user_timeval p_rtime; /* Real time. */
296 u_quad_t p_uticks; /* Statclock hits in user mode. */
297 u_quad_t p_sticks; /* Statclock hits in system mode. */
298 u_quad_t p_iticks; /* Statclock hits processing intr. */
299 int p_traceflag; /* Kernel trace points. */
300 user_addr_t p_tracep; /* Trace to vnode. */
301 int p_siglist; /* DEPRECATED */
302 user_addr_t p_textvp; /* Vnode of executable. */
303 int p_holdcnt; /* If non-zero, don't swap. */
304 sigset_t p_sigmask; /* DEPRECATED. */
305 sigset_t p_sigignore; /* Signals being ignored. */
306 sigset_t p_sigcatch; /* Signals being caught by user. */
307 u_char p_priority; /* Process priority. */
308 u_char p_usrpri; /* User-priority based on p_cpu and p_nice. */
309 char p_nice; /* Process "nice" value. */
310 char p_comm[MAXCOMLEN+1];
311 user_addr_t p_pgrp; /* Pointer to process group. */
312 user_addr_t p_addr; /* Kernel virtual addr of u-area (PROC ONLY). */
313 u_short p_xstat; /* Exit status for wait; also stop signal. */
314 u_short p_acflag; /* Accounting flags. */
315 user_addr_t p_ru; /* Exit information. XXX */
316 };
317
318 #if __DARWIN_ALIGN_NATURAL
319 #pragma options align=reset
320 #endif
321 #endif /* KERNEL */
322
323 /*
324 * We use process IDs <= PID_MAX; PID_MAX + 1 must also fit in a pid_t,
325 * as it is used to represent "no process group".
326 */
327 extern int nprocs, maxproc; /* Current and max number of procs. */
328 __private_extern__ int hard_maxproc; /* hard limit */
329
330 #define PID_MAX 30000
331 #define NO_PID 30001
332
333 #define SESS_LEADER(p) ((p)->p_session->s_leader == (p))
334 #define SESSHOLD(s) ((s)->s_count++)
335 #define SESSRELE(s) sessrele(s)
336
337 #define PIDHASH(pid) (&pidhashtbl[(pid) & pidhash])
338 extern LIST_HEAD(pidhashhead, proc) *pidhashtbl;
339 extern u_long pidhash;
340
341 #define PGRPHASH(pgid) (&pgrphashtbl[(pgid) & pgrphash])
342 extern LIST_HEAD(pgrphashhead, pgrp) *pgrphashtbl;
343 extern u_long pgrphash;
344 extern lck_grp_t * proc_lck_grp;
345 extern lck_grp_attr_t * proc_lck_grp_attr;
346 extern lck_attr_t * proc_lck_attr;
347
348 LIST_HEAD(proclist, proc);
349 extern struct proclist allproc; /* List of all processes. */
350 extern struct proclist zombproc; /* List of zombie processes. */
351 extern struct proc *initproc;
352 extern void pgdelete(struct pgrp *pgrp);
353 extern void sessrele(struct session *sess);
354 extern void procinit(void);
355 extern void proc_lock(struct proc *);
356 extern void proc_unlock(struct proc *);
357 extern void proc_fdlock(struct proc *);
358 extern void proc_fdunlock(struct proc *);
359 __private_extern__ char *proc_core_name(const char *name, uid_t uid, pid_t pid);
360 extern int isinferior(struct proc *, struct proc *);
361 extern struct proc *pfind(pid_t); /* Find process by id. */
362 __private_extern__ struct proc *pzfind(pid_t); /* Find zombie by id. */
363 extern struct pgrp *pgfind(pid_t); /* Find process group by id. */
364
365 extern int chgproccnt(uid_t uid, int diff);
366 extern int enterpgrp(struct proc *p, pid_t pgid, int mksess);
367 extern void fixjobc(struct proc *p, struct pgrp *pgrp, int entering);
368 extern int inferior(struct proc *p);
369 extern int leavepgrp(struct proc *p);
370 extern void resetpriority(struct proc *);
371 extern void setrunnable(struct proc *);
372 extern void setrunqueue(struct proc *);
373 extern int sleep(void *chan, int pri);
374 extern int tsleep0(void *chan, int pri, const char *wmesg, int timo, int (*continuation)(int));
375 extern int tsleep1(void *chan, int pri, const char *wmesg, u_int64_t abstime, int (*continuation)(int));
376 extern int msleep0(void *chan, lck_mtx_t *mtx, int pri, const char *wmesg, int timo, int (*continuation)(int));
377 extern void vfork_return(thread_t th_act, struct proc *p, struct proc *p2, register_t *retval);
378 extern struct proc * proc_findref(pid_t pid);
379 extern void proc_dropref(struct proc * p);
380 extern struct proc * proc_refinternal(proc_t p, int funneled);
381 extern void proc_dropinternal(struct proc * p, int funneled);
382
383 #endif /* !_SYS_PROC_INTERNAL_H_ */