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