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