xref: /original-bsd/sys/sys/proc.h (revision f17085de)
1 /*-
2  * Copyright (c) 1986, 1989, 1991, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  * (c) UNIX System Laboratories, Inc.
5  * All or some portions of this file are derived from material licensed
6  * to the University of California by American Telephone and Telegraph
7  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8  * the permission of UNIX System Laboratories, Inc.
9  *
10  * %sccs.include.redist.c%
11  *
12  *	@(#)proc.h	8.14 (Berkeley) 05/17/95
13  */
14 
15 #ifndef _SYS_PROC_H_
16 #define	_SYS_PROC_H_
17 
18 #include <machine/proc.h>		/* Machine-dependent proc substruct. */
19 #include <sys/select.h>			/* For struct selinfo. */
20 #include <sys/queue.h>
21 
22 /*
23  * One structure allocated per session.
24  */
25 struct	session {
26 	int	s_count;		/* Ref cnt; pgrps in session. */
27 	struct	proc *s_leader;		/* Session leader. */
28 	struct	vnode *s_ttyvp;		/* Vnode of controlling terminal. */
29 	struct	tty *s_ttyp;		/* Controlling terminal. */
30 	char	s_login[MAXLOGNAME];	/* Setlogin() name. */
31 };
32 
33 /*
34  * One structure allocated per process group.
35  */
36 struct	pgrp {
37 	LIST_ENTRY(pgrp) pg_hash;	/* Hash chain. */
38 	LIST_HEAD(, proc) pg_members;	/* Pointer to pgrp members. */
39 	struct	session *pg_session;	/* Pointer to session. */
40 	pid_t	pg_id;			/* Pgrp id. */
41 	int	pg_jobc;	/* # procs qualifying pgrp for job control */
42 };
43 
44 /*
45  * Description of a process.
46  *
47  * This structure contains the information needed to manage a thread of
48  * control, known in UN*X as a process; it has references to substructures
49  * containing descriptions of things that the process uses, but may share
50  * with related processes.  The process structure and the substructures
51  * are always addressible except for those marked "(PROC ONLY)" below,
52  * which might be addressible only on a processor on which the process
53  * is running.
54  */
55 struct	proc {
56 	struct	proc *p_forw;		/* Doubly-linked run/sleep queue. */
57 	struct	proc *p_back;
58 	LIST_ENTRY(proc) p_list;	/* List of all processes. */
59 
60 	/* substructures: */
61 	struct	pcred *p_cred;		/* Process owner's identity. */
62 	struct	filedesc *p_fd;		/* Ptr to open files structure. */
63 	struct	pstats *p_stats;	/* Accounting/statistics (PROC ONLY). */
64 	struct	plimit *p_limit;	/* Process limits. */
65 	struct	vmspace *p_vmspace;	/* Address space. */
66 	struct	sigacts *p_sigacts;	/* Signal actions, state (PROC ONLY). */
67 
68 #define	p_ucred		p_cred->pc_ucred
69 #define	p_rlimit	p_limit->pl_rlimit
70 
71 	int	p_flag;			/* P_* flags. */
72 	char	p_stat;			/* S* process status. */
73 	char	p_pad1[3];
74 
75 	pid_t	p_pid;			/* Process identifier. */
76 	LIST_ENTRY(proc) p_pglist;	/* List of processes in pgrp. */
77 	struct	proc *p_pptr;	 	/* Pointer to parent process. */
78 	LIST_ENTRY(proc) p_sibling;	/* List of sibling processes. */
79 	LIST_HEAD(, proc) p_children;	/* Pointer to list of children. */
80 
81 /* The following fields are all zeroed upon creation in fork. */
82 #define	p_startzero	p_oppid
83 
84 	pid_t	p_oppid;	 /* Save parent pid during ptrace. XXX */
85 	int	p_dupfd;	 /* Sideways return value from fdopen. XXX */
86 
87 	/* scheduling */
88 	u_int	p_estcpu;	 /* Time averaged value of p_cpticks. */
89 	int	p_cpticks;	 /* Ticks of cpu time. */
90 	fixpt_t	p_pctcpu;	 /* %cpu for this process during p_swtime */
91 	void	*p_wchan;	 /* Sleep address. */
92 	char	*p_wmesg;	 /* Reason for sleep. */
93 	u_int	p_swtime;	 /* Time swapped in or out. */
94 	u_int	p_slptime;	 /* Time since last blocked. */
95 
96 	struct	itimerval p_realtimer;	/* Alarm timer. */
97 	struct	timeval p_rtime;	/* Real time. */
98 	u_quad_t p_uticks;		/* Statclock hits in user mode. */
99 	u_quad_t p_sticks;		/* Statclock hits in system mode. */
100 	u_quad_t p_iticks;		/* Statclock hits processing intr. */
101 
102 	int	p_traceflag;		/* Kernel trace points. */
103 	struct	vnode *p_tracep;	/* Trace to vnode. */
104 
105 	int	p_siglist;		/* Signals arrived but not delivered. */
106 
107 	struct	vnode *p_textvp;	/* Vnode of executable. */
108 
109 	int	p_locks;		/* DEBUG: lockmgr count of held locks */
110 	long	p_spare[2];		/* pad to 256, avoid shifting eproc. */
111 
112 /* End area that is zeroed on creation. */
113 #define	p_endzero	p_hash.le_next
114 
115 	/*
116 	 * Not copied, not zero'ed.
117 	 * Belongs after p_pid, but here to avoid shifting proc elements.
118 	 */
119 	LIST_ENTRY(proc) p_hash;	/* Hash chain. */
120 
121 /* The following fields are all copied upon creation in fork. */
122 #define	p_startcopy	p_sigmask
123 
124 	sigset_t p_sigmask;	/* Current signal mask. */
125 	sigset_t p_sigignore;	/* Signals being ignored. */
126 	sigset_t p_sigcatch;	/* Signals being caught by user. */
127 
128 	u_char	p_priority;	/* Process priority. */
129 	u_char	p_usrpri;	/* User-priority based on p_cpu and p_nice. */
130 	char	p_nice;		/* Process "nice" value. */
131 	char	p_comm[MAXCOMLEN+1];
132 
133 	struct 	pgrp *p_pgrp;	/* Pointer to process group. */
134 
135 /* End area that is copied on creation. */
136 #define	p_endcopy	p_thread
137 
138 	void	*p_thread;	/* Id for this "thread"; Mach glue. XXX */
139 	struct	user *p_addr;	/* Kernel virtual addr of u-area (PROC ONLY). */
140 	struct	mdproc p_md;	/* Any machine-dependent fields. */
141 
142 	u_short	p_xstat;	/* Exit status for wait; also stop signal. */
143 	u_short	p_acflag;	/* Accounting flags. */
144 	struct	rusage *p_ru;	/* Exit information. XXX */
145 };
146 
147 #define	p_session	p_pgrp->pg_session
148 #define	p_pgid		p_pgrp->pg_id
149 
150 /* Status values. */
151 #define	SIDL	1		/* Process being created by fork. */
152 #define	SRUN	2		/* Currently runnable. */
153 #define	SSLEEP	3		/* Sleeping on an address. */
154 #define	SSTOP	4		/* Process debugging or suspension. */
155 #define	SZOMB	5		/* Awaiting collection by parent. */
156 
157 /* These flags are kept in p_flags. */
158 #define	P_ADVLOCK	0x00001	/* Process may hold a POSIX advisory lock. */
159 #define	P_CONTROLT	0x00002	/* Has a controlling terminal. */
160 #define	P_INMEM		0x00004	/* Loaded into memory. */
161 #define	P_NOCLDSTOP	0x00008	/* No SIGCHLD when children stop. */
162 #define	P_PPWAIT	0x00010	/* Parent is waiting for child to exec/exit. */
163 #define	P_PROFIL	0x00020	/* Has started profiling. */
164 #define	P_SELECT	0x00040	/* Selecting; wakeup/waiting danger. */
165 #define	P_SINTR		0x00080	/* Sleep is interruptible. */
166 #define	P_SUGID		0x00100	/* Had set id privileges since last exec. */
167 #define	P_SYSTEM	0x00200	/* System proc: no sigs, stats or swapping. */
168 #define	P_TIMEOUT	0x00400	/* Timing out during sleep. */
169 #define	P_TRACED	0x00800	/* Debugged process being traced. */
170 #define	P_WAITED	0x01000	/* Debugging process has waited for child. */
171 #define	P_WEXIT		0x02000	/* Working on exiting. */
172 #define	P_EXEC		0x04000	/* Process called exec. */
173 
174 /* Should probably be changed into a hold count. */
175 #define	P_NOSWAP	0x08000	/* Another flag to prevent swap out. */
176 #define	P_PHYSIO	0x10000	/* Doing physical I/O. */
177 
178 /* Should be moved to machine-dependent areas. */
179 #define	P_OWEUPC	0x20000	/* Owe process an addupc() call at next ast. */
180 
181 /*
182  * MOVE TO ucred.h?
183  *
184  * Shareable process credentials (always resident).  This includes a reference
185  * to the current user credentials as well as real and saved ids that may be
186  * used to change ids.
187  */
188 struct	pcred {
189 	struct	ucred *pc_ucred;	/* Current credentials. */
190 	uid_t	p_ruid;			/* Real user id. */
191 	uid_t	p_svuid;		/* Saved effective user id. */
192 	gid_t	p_rgid;			/* Real group id. */
193 	gid_t	p_svgid;		/* Saved effective group id. */
194 	int	p_refcnt;		/* Number of references. */
195 };
196 
197 #ifdef KERNEL
198 /*
199  * We use process IDs <= PID_MAX; PID_MAX + 1 must also fit in a pid_t,
200  * as it is used to represent "no process group".
201  */
202 #define	PID_MAX		30000
203 #define	NO_PID		30001
204 
205 #define SESS_LEADER(p)	((p)->p_session->s_leader == (p))
206 #define	SESSHOLD(s)	((s)->s_count++)
207 #define	SESSRELE(s) {							\
208 	if (--(s)->s_count == 0)					\
209 		FREE(s, M_SESSION);					\
210 }
211 
212 #define	PIDHASH(pid)	(&pidhashtbl[(pid) & pidhash])
213 extern LIST_HEAD(pidhashhead, proc) *pidhashtbl;
214 extern u_long pidhash;
215 
216 #define	PGRPHASH(pgid)	(&pgrphashtbl[(pgid) & pgrphash])
217 extern LIST_HEAD(pgrphashhead, pgrp) *pgrphashtbl;
218 extern u_long pgrphash;
219 
220 extern struct proc *curproc;		/* Current running proc. */
221 extern struct proc proc0;		/* Process slot for swapper. */
222 extern int nprocs, maxproc;		/* Current and max number of procs. */
223 
224 LIST_HEAD(proclist, proc);
225 extern struct proclist allproc;		/* List of all processes. */
226 extern struct proclist zombproc;	/* List of zombie processes. */
227 struct proc *initproc, *pageproc;	/* Process slots for init, pager. */
228 
229 #define	NQS	32			/* 32 run queues. */
230 int	whichqs;			/* Bit mask summary of non-empty Q's. */
231 struct	prochd {
232 	struct	proc *ph_link;		/* Linked list of running processes. */
233 	struct	proc *ph_rlink;
234 } qs[NQS];
235 
236 struct proc *pfind __P((pid_t));	/* Find process by id. */
237 struct pgrp *pgfind __P((pid_t));	/* Find process group by id. */
238 
239 int	chgproccnt __P((uid_t uid, int diff));
240 int	enterpgrp __P((struct proc *p, pid_t pgid, int mksess));
241 void	fixjobc __P((struct proc *p, struct pgrp *pgrp, int entering));
242 int	inferior __P((struct proc *p));
243 int	leavepgrp __P((struct proc *p));
244 void	mi_switch __P((void));
245 void	pgdelete __P((struct pgrp *pgrp));
246 void	procinit __P((void));
247 void	resetpriority __P((struct proc *));
248 void	setrunnable __P((struct proc *));
249 void	setrunqueue __P((struct proc *));
250 void	sleep __P((void *chan, int pri));
251 int	tsleep __P((void *chan, int pri, char *wmesg, int timo));
252 void	unsleep __P((struct proc *));
253 void	wakeup __P((void *chan));
254 #endif	/* KERNEL */
255 #endif	/* !_SYS_PROC_H_ */
256