]> git.saurik.com Git - apple/xnu.git/blame - bsd/sys/proc_internal.h
xnu-792.tar.gz
[apple/xnu.git] / bsd / sys / proc_internal.h
CommitLineData
91447636
A
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 */
74struct 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 */
86struct 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
94struct 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 */
112struct 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 int p_lflag; /* local flags */
226#if DIAGNOSTIC
227#if SIGNAL_DEBUG
228 unsigned int lockpc[8];
229 unsigned int unlockpc[8];
230#endif /* SIGNAL_DEBUG */
231#endif /* DIAGNOSTIC */
232};
233
234
235#define P_LDELAYTERM 0x1 /* */
236#define P_LNOZOMB 0x2 /* */
237#define P_LLOW_PRI_IO 0x4
238#define P_LPEXIT 0x8
239#define P_LBACKGROUND_IO 0x10
240
241// LP64todo - should this move?
242/* LP64 version of extern_proc. all pointers
243 * grow when we're dealing with a 64-bit process.
244 * WARNING - keep in sync with extern_proc
245 * but use native alignment of 64-bit process.
246 */
247
248#ifdef KERNEL
249#include <sys/time.h> /* user_timeval, user_itimerval */
250
251#if __DARWIN_ALIGN_NATURAL
252#pragma options align=natural
253#endif
254
255struct user_extern_proc {
256 union {
257 struct {
258 user_addr_t __p_forw; /* Doubly-linked run/sleep queue. */
259 user_addr_t __p_back;
260 } p_st1;
261 struct user_timeval __p_starttime; /* process start time */
262 } p_un;
263 user_addr_t p_vmspace; /* Address space. */
264 user_addr_t p_sigacts; /* Signal actions, state (PROC ONLY). */
265 int p_flag; /* P_* flags. */
266 char p_stat; /* S* process status. */
267 pid_t p_pid; /* Process identifier. */
268 pid_t p_oppid; /* Save parent pid during ptrace. XXX */
269 int p_dupfd; /* Sideways return value from fdopen. XXX */
270 /* Mach related */
271 user_addr_t user_stack; /* where user stack was allocated */
272 user_addr_t exit_thread; /* XXX Which thread is exiting? */
273 int p_debugger; /* allow to debug */
274 boolean_t sigwait; /* indication to suspend */
275 /* scheduling */
276 u_int p_estcpu; /* Time averaged value of p_cpticks. */
277 int p_cpticks; /* Ticks of cpu time. */
278 fixpt_t p_pctcpu; /* %cpu for this process during p_swtime */
279 user_addr_t p_wchan; /* Sleep address. */
280 user_addr_t p_wmesg; /* Reason for sleep. */
281 u_int p_swtime; /* Time swapped in or out. */
282 u_int p_slptime; /* Time since last blocked. */
283 struct user_itimerval p_realtimer; /* Alarm timer. */
284 struct user_timeval p_rtime; /* Real time. */
285 u_quad_t p_uticks; /* Statclock hits in user mode. */
286 u_quad_t p_sticks; /* Statclock hits in system mode. */
287 u_quad_t p_iticks; /* Statclock hits processing intr. */
288 int p_traceflag; /* Kernel trace points. */
289 user_addr_t p_tracep; /* Trace to vnode. */
290 int p_siglist; /* DEPRECATED */
291 user_addr_t p_textvp; /* Vnode of executable. */
292 int p_holdcnt; /* If non-zero, don't swap. */
293 sigset_t p_sigmask; /* DEPRECATED. */
294 sigset_t p_sigignore; /* Signals being ignored. */
295 sigset_t p_sigcatch; /* Signals being caught by user. */
296 u_char p_priority; /* Process priority. */
297 u_char p_usrpri; /* User-priority based on p_cpu and p_nice. */
298 char p_nice; /* Process "nice" value. */
299 char p_comm[MAXCOMLEN+1];
300 user_addr_t p_pgrp; /* Pointer to process group. */
301 user_addr_t p_addr; /* Kernel virtual addr of u-area (PROC ONLY). */
302 u_short p_xstat; /* Exit status for wait; also stop signal. */
303 u_short p_acflag; /* Accounting flags. */
304 user_addr_t p_ru; /* Exit information. XXX */
305};
306
307#if __DARWIN_ALIGN_NATURAL
308#pragma options align=reset
309#endif
310#endif /* KERNEL */
311
312/*
313 * We use process IDs <= PID_MAX; PID_MAX + 1 must also fit in a pid_t,
314 * as it is used to represent "no process group".
315 */
316extern int nprocs, maxproc; /* Current and max number of procs. */
317__private_extern__ int hard_maxproc; /* hard limit */
318
319#define PID_MAX 30000
320#define NO_PID 30001
321
322#define SESS_LEADER(p) ((p)->p_session->s_leader == (p))
323#define SESSHOLD(s) ((s)->s_count++)
324#define SESSRELE(s) sessrele(s)
325
326#define PIDHASH(pid) (&pidhashtbl[(pid) & pidhash])
327extern LIST_HEAD(pidhashhead, proc) *pidhashtbl;
328extern u_long pidhash;
329
330#define PGRPHASH(pgid) (&pgrphashtbl[(pgid) & pgrphash])
331extern LIST_HEAD(pgrphashhead, pgrp) *pgrphashtbl;
332extern u_long pgrphash;
333extern lck_grp_t * proc_lck_grp;
334extern lck_grp_attr_t * proc_lck_grp_attr;
335extern lck_attr_t * proc_lck_attr;
336
337LIST_HEAD(proclist, proc);
338extern struct proclist allproc; /* List of all processes. */
339extern struct proclist zombproc; /* List of zombie processes. */
340extern struct proc *initproc;
341extern void pgdelete(struct pgrp *pgrp);
342extern void sessrele(struct session *sess);
343extern void procinit(void);
344extern void proc_lock(struct proc *);
345extern void proc_unlock(struct proc *);
346extern void proc_fdlock(struct proc *);
347extern void proc_fdunlock(struct proc *);
348__private_extern__ char *proc_core_name(const char *name, uid_t uid, pid_t pid);
349extern int isinferior(struct proc *, struct proc *);
350extern struct proc *pfind(pid_t); /* Find process by id. */
351__private_extern__ struct proc *pzfind(pid_t); /* Find zombie by id. */
352extern struct pgrp *pgfind(pid_t); /* Find process group by id. */
353
354extern int chgproccnt(uid_t uid, int diff);
355extern int enterpgrp(struct proc *p, pid_t pgid, int mksess);
356extern void fixjobc(struct proc *p, struct pgrp *pgrp, int entering);
357extern int inferior(struct proc *p);
358extern int leavepgrp(struct proc *p);
359extern void resetpriority(struct proc *);
360extern void setrunnable(struct proc *);
361extern void setrunqueue(struct proc *);
362extern int sleep(void *chan, int pri);
363extern int tsleep0(void *chan, int pri, const char *wmesg, int timo, int (*continuation)(int));
364extern int tsleep1(void *chan, int pri, const char *wmesg, u_int64_t abstime, int (*continuation)(int));
365extern int msleep0(void *chan, lck_mtx_t *mtx, int pri, const char *wmesg, int timo, int (*continuation)(int));
366extern void vfork_return(thread_t th_act, struct proc *p, struct proc *p2, register_t *retval);
367
368
369#endif /* !_SYS_PROC_INTERNAL_H_ */