]> git.saurik.com Git - apple/xnu.git/blob - bsd/sys/proc_internal.h
xnu-792.13.8.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_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 char p_name[(2*MAXCOMLEN)+1];
194
195 struct pgrp *p_pgrp; /* Pointer to process group. */
196
197 /* End area that is copied on creation. */
198 #define p_endcopy p_xstat
199
200 u_short p_xstat; /* Exit status for wait; also stop signal. */
201 u_short p_acflag; /* Accounting flags. */
202 struct rusage *p_ru; /* Exit information. XXX */
203
204 int p_debugger; /* 1: can exec set-bit programs if suser */
205
206 void *task; /* corresponding task */
207 void *sigwait_thread; /* 'thread' holding sigwait */
208 char signal_lock[72];
209 boolean_t sigwait; /* indication to suspend */
210 void *exit_thread; /* Which thread is exiting? */
211 user_addr_t user_stack; /* where user stack was allocated */
212 void * exitarg; /* exit arg for proc terminate */
213 void * vm_shm; /* for sysV shared memory */
214 int p_argc; /* saved argc for sysctl_procargs() */
215 int p_vforkcnt; /* number of outstanding vforks */
216 void * p_vforkact; /* activation running this vfork proc */
217 TAILQ_HEAD( , uthread) p_uthlist; /* List of uthreads */
218 /* Following fields are info from SIGCHLD */
219 pid_t si_pid;
220 u_short si_status;
221 u_short si_code;
222 uid_t si_uid;
223 TAILQ_HEAD( , aio_workq_entry ) aio_activeq; /* active async IO requests */
224 int aio_active_count; /* entries on aio_activeq */
225 TAILQ_HEAD( , aio_workq_entry ) aio_doneq; /* completed async IO requests */
226 int aio_done_count; /* entries on aio_doneq */
227
228 struct klist p_klist; /* knote list */
229 lck_mtx_t p_mlock; /* proc lock to protect evques */
230 lck_mtx_t p_fdmlock; /* proc lock to protect evques */
231 unsigned int p_fdlock_pc[4];
232 unsigned int p_fdunlock_pc[4];
233 int p_fpdrainwait;
234 unsigned int p_lflag; /* local flags */
235 unsigned int p_ladvflag; /* local adv flags*/
236 unsigned int p_internalref; /* temp refcount field */
237 #if DIAGNOSTIC
238 #if SIGNAL_DEBUG
239 unsigned int lockpc[8];
240 unsigned int unlockpc[8];
241 #endif /* SIGNAL_DEBUG */
242 #endif /* DIAGNOSTIC */
243 };
244
245
246 /* local flags */
247 #define P_LDELAYTERM 0x1 /* */
248 #define P_LNOZOMB 0x2 /* */
249 #define P_LLOW_PRI_IO 0x4
250 #define P_LPEXIT 0x8
251 #define P_LBACKGROUND_IO 0x10
252 #define P_LWAITING 0x20
253 #define P_LREFDRAIN 0x40
254 #define P_LREFDRAINWAIT 0x80
255 #define P_LREFDEAD 0x100
256 #define P_LTHSIGSTACK 0x200
257
258 /* advisory flags in the proc */
259 #define P_LADVLOCK 0x01
260
261 // LP64todo - should this move?
262 /* LP64 version of extern_proc. all pointers
263 * grow when we're dealing with a 64-bit process.
264 * WARNING - keep in sync with extern_proc
265 * but use native alignment of 64-bit process.
266 */
267
268 #ifdef KERNEL
269 #include <sys/time.h> /* user_timeval, user_itimerval */
270
271 struct user_extern_proc {
272 union {
273 struct {
274 user_addr_t __p_forw; /* Doubly-linked run/sleep queue. */
275 user_addr_t __p_back;
276 } p_st1;
277 struct user_timeval __p_starttime; /* process start time */
278 } p_un;
279 user_addr_t p_vmspace; /* Address space. */
280 user_addr_t p_sigacts; /* Signal actions, state (PROC ONLY). */
281 int p_flag; /* P_* flags. */
282 char p_stat; /* S* process status. */
283 pid_t p_pid; /* Process identifier. */
284 pid_t p_oppid; /* Save parent pid during ptrace. XXX */
285 int p_dupfd; /* Sideways return value from fdopen. XXX */
286 /* Mach related */
287 user_addr_t user_stack __attribute((aligned(8))); /* where user stack was allocated */
288 user_addr_t exit_thread; /* XXX Which thread is exiting? */
289 int p_debugger; /* allow to debug */
290 boolean_t sigwait; /* indication to suspend */
291 /* scheduling */
292 u_int p_estcpu; /* Time averaged value of p_cpticks. */
293 int p_cpticks; /* Ticks of cpu time. */
294 fixpt_t p_pctcpu; /* %cpu for this process during p_swtime */
295 user_addr_t p_wchan __attribute((aligned(8))); /* Sleep address. */
296 user_addr_t p_wmesg; /* Reason for sleep. */
297 u_int p_swtime; /* Time swapped in or out. */
298 u_int p_slptime; /* Time since last blocked. */
299 struct user_itimerval p_realtimer; /* Alarm timer. */
300 struct user_timeval p_rtime; /* Real time. */
301 u_quad_t p_uticks; /* Statclock hits in user mode. */
302 u_quad_t p_sticks; /* Statclock hits in system mode. */
303 u_quad_t p_iticks; /* Statclock hits processing intr. */
304 int p_traceflag; /* Kernel trace points. */
305 user_addr_t p_tracep __attribute((aligned(8))); /* Trace to vnode. */
306 int p_siglist; /* DEPRECATED */
307 user_addr_t p_textvp __attribute((aligned(8))); /* Vnode of executable. */
308 int p_holdcnt; /* If non-zero, don't swap. */
309 sigset_t p_sigmask; /* DEPRECATED. */
310 sigset_t p_sigignore; /* Signals being ignored. */
311 sigset_t p_sigcatch; /* Signals being caught by user. */
312 u_char p_priority; /* Process priority. */
313 u_char p_usrpri; /* User-priority based on p_cpu and p_nice. */
314 char p_nice; /* Process "nice" value. */
315 char p_comm[MAXCOMLEN+1];
316 user_addr_t p_pgrp __attribute((aligned(8))); /* Pointer to process group. */
317 user_addr_t p_addr; /* Kernel virtual addr of u-area (PROC ONLY). */
318 u_short p_xstat; /* Exit status for wait; also stop signal. */
319 u_short p_acflag; /* Accounting flags. */
320 user_addr_t p_ru __attribute((aligned(8))); /* Exit information. XXX */
321 };
322 #endif /* KERNEL */
323
324 /*
325 * We use process IDs <= PID_MAX; PID_MAX + 1 must also fit in a pid_t,
326 * as it is used to represent "no process group".
327 */
328 extern int nprocs, maxproc; /* Current and max number of procs. */
329 extern int maxprocperuid; /* Current number of procs per uid */
330 __private_extern__ int hard_maxproc; /* hard limit */
331
332 #define PID_MAX 30000
333 #define NO_PID 30001
334
335 #define SESS_LEADER(p) ((p)->p_session->s_leader == (p))
336 #define SESSHOLD(s) ((s)->s_count++)
337 #define SESSRELE(s) sessrele(s)
338
339 #define PIDHASH(pid) (&pidhashtbl[(pid) & pidhash])
340 extern LIST_HEAD(pidhashhead, proc) *pidhashtbl;
341 extern u_long pidhash;
342
343 #define PGRPHASH(pgid) (&pgrphashtbl[(pgid) & pgrphash])
344 extern LIST_HEAD(pgrphashhead, pgrp) *pgrphashtbl;
345 extern u_long pgrphash;
346 extern lck_grp_t * proc_lck_grp;
347 extern lck_grp_attr_t * proc_lck_grp_attr;
348 extern lck_attr_t * proc_lck_attr;
349
350 LIST_HEAD(proclist, proc);
351 extern struct proclist allproc; /* List of all processes. */
352 extern struct proclist zombproc; /* List of zombie processes. */
353 extern struct proc *initproc;
354 extern void pgdelete(struct pgrp *pgrp);
355 extern void sessrele(struct session *sess);
356 extern void procinit(void);
357 extern void proc_lock(struct proc *);
358 extern void proc_unlock(struct proc *);
359 extern void proc_fdlock(struct proc *);
360 extern void proc_fdunlock(struct proc *);
361 __private_extern__ char *proc_core_name(const char *name, uid_t uid, pid_t pid);
362 extern int isinferior(struct proc *, struct proc *);
363 extern struct proc *pfind(pid_t); /* Find process by id. */
364 __private_extern__ struct proc *pzfind(pid_t); /* Find zombie by id. */
365 extern struct pgrp *pgfind(pid_t); /* Find process group by id. */
366
367 extern int chgproccnt(uid_t uid, int diff);
368 extern int enterpgrp(struct proc *p, pid_t pgid, int mksess);
369 extern void fixjobc(struct proc *p, struct pgrp *pgrp, int entering);
370 extern int inferior(struct proc *p);
371 extern int leavepgrp(struct proc *p);
372 extern void resetpriority(struct proc *);
373 extern void setrunnable(struct proc *);
374 extern void setrunqueue(struct proc *);
375 extern int sleep(void *chan, int pri);
376 extern int tsleep0(void *chan, int pri, const char *wmesg, int timo, int (*continuation)(int));
377 extern int tsleep1(void *chan, int pri, const char *wmesg, u_int64_t abstime, int (*continuation)(int));
378 extern int msleep0(void *chan, lck_mtx_t *mtx, int pri, const char *wmesg, int timo, int (*continuation)(int));
379 extern void vfork_return(thread_t th_act, struct proc *p, struct proc *p2, register_t *retval);
380 extern struct proc * proc_findref(pid_t pid);
381 extern void proc_dropref(struct proc * p);
382 extern struct proc * proc_refinternal(proc_t p, int funneled);
383 extern void proc_dropinternal(struct proc * p, int funneled);
384
385 #endif /* !_SYS_PROC_INTERNAL_H_ */