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