xref: /original-bsd/sys/sys/proc.h (revision fac0c393)
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.13 (Berkeley) 01/09/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 	long	p_spare[3];		/* pad to 256, avoid shifting eproc. */
110 
111 /* End area that is zeroed on creation. */
112 #define	p_endzero	p_hash.le_next
113 
114 	/*
115 	 * Not copied, not zero'ed.
116 	 * Belongs after p_pid, but here to avoid shifting proc elements.
117 	 */
118 	LIST_ENTRY(proc) p_hash;	/* Hash chain. */
119 
120 /* The following fields are all copied upon creation in fork. */
121 #define	p_startcopy	p_sigmask
122 
123 	sigset_t p_sigmask;	/* Current signal mask. */
124 	sigset_t p_sigignore;	/* Signals being ignored. */
125 	sigset_t p_sigcatch;	/* Signals being caught by user. */
126 
127 	u_char	p_priority;	/* Process priority. */
128 	u_char	p_usrpri;	/* User-priority based on p_cpu and p_nice. */
129 	char	p_nice;		/* Process "nice" value. */
130 	char	p_comm[MAXCOMLEN+1];
131 
132 	struct 	pgrp *p_pgrp;	/* Pointer to process group. */
133 
134 /* End area that is copied on creation. */
135 #define	p_endcopy	p_thread
136 
137 	void	*p_thread;	/* Id for this "thread"; Mach glue. XXX */
138 	struct	user *p_addr;	/* Kernel virtual addr of u-area (PROC ONLY). */
139 	struct	mdproc p_md;	/* Any machine-dependent fields. */
140 
141 	u_short	p_xstat;	/* Exit status for wait; also stop signal. */
142 	u_short	p_acflag;	/* Accounting flags. */
143 	struct	rusage *p_ru;	/* Exit information. XXX */
144 };
145 
146 #define	p_session	p_pgrp->pg_session
147 #define	p_pgid		p_pgrp->pg_id
148 
149 /* Status values. */
150 #define	SIDL	1		/* Process being created by fork. */
151 #define	SRUN	2		/* Currently runnable. */
152 #define	SSLEEP	3		/* Sleeping on an address. */
153 #define	SSTOP	4		/* Process debugging or suspension. */
154 #define	SZOMB	5		/* Awaiting collection by parent. */
155 
156 /* These flags are kept in p_flags. */
157 #define	P_ADVLOCK	0x00001	/* Process may hold a POSIX advisory lock. */
158 #define	P_CONTROLT	0x00002	/* Has a controlling terminal. */
159 #define	P_INMEM		0x00004	/* Loaded into memory. */
160 #define	P_NOCLDSTOP	0x00008	/* No SIGCHLD when children stop. */
161 #define	P_PPWAIT	0x00010	/* Parent is waiting for child to exec/exit. */
162 #define	P_PROFIL	0x00020	/* Has started profiling. */
163 #define	P_SELECT	0x00040	/* Selecting; wakeup/waiting danger. */
164 #define	P_SINTR		0x00080	/* Sleep is interruptible. */
165 #define	P_SUGID		0x00100	/* Had set id privileges since last exec. */
166 #define	P_SYSTEM	0x00200	/* System proc: no sigs, stats or swapping. */
167 #define	P_TIMEOUT	0x00400	/* Timing out during sleep. */
168 #define	P_TRACED	0x00800	/* Debugged process being traced. */
169 #define	P_WAITED	0x01000	/* Debugging process has waited for child. */
170 #define	P_WEXIT		0x02000	/* Working on exiting. */
171 #define	P_EXEC		0x04000	/* Process called exec. */
172 
173 /* Should probably be changed into a hold count. */
174 #define	P_NOSWAP	0x08000	/* Another flag to prevent swap out. */
175 #define	P_PHYSIO	0x10000	/* Doing physical I/O. */
176 
177 /* Should be moved to machine-dependent areas. */
178 #define	P_OWEUPC	0x20000	/* Owe process an addupc() call at next ast. */
179 
180 /*
181  * MOVE TO ucred.h?
182  *
183  * Shareable process credentials (always resident).  This includes a reference
184  * to the current user credentials as well as real and saved ids that may be
185  * used to change ids.
186  */
187 struct	pcred {
188 	struct	ucred *pc_ucred;	/* Current credentials. */
189 	uid_t	p_ruid;			/* Real user id. */
190 	uid_t	p_svuid;		/* Saved effective user id. */
191 	gid_t	p_rgid;			/* Real group id. */
192 	gid_t	p_svgid;		/* Saved effective group id. */
193 	int	p_refcnt;		/* Number of references. */
194 };
195 
196 #ifdef KERNEL
197 /*
198  * We use process IDs <= PID_MAX; PID_MAX + 1 must also fit in a pid_t,
199  * as it is used to represent "no process group".
200  */
201 #define	PID_MAX		30000
202 #define	NO_PID		30001
203 
204 #define SESS_LEADER(p)	((p)->p_session->s_leader == (p))
205 #define	SESSHOLD(s)	((s)->s_count++)
206 #define	SESSRELE(s) {							\
207 	if (--(s)->s_count == 0)					\
208 		FREE(s, M_SESSION);					\
209 }
210 
211 #define	PIDHASH(pid)	(&pidhashtbl[(pid) & pidhash])
212 extern LIST_HEAD(pidhashhead, proc) *pidhashtbl;
213 extern u_long pidhash;
214 
215 #define	PGRPHASH(pgid)	(&pgrphashtbl[(pgid) & pgrphash])
216 extern LIST_HEAD(pgrphashhead, pgrp) *pgrphashtbl;
217 extern u_long pgrphash;
218 
219 extern struct proc *curproc;		/* Current running proc. */
220 extern struct proc proc0;		/* Process slot for swapper. */
221 extern int nprocs, maxproc;		/* Current and max number of procs. */
222 
223 LIST_HEAD(proclist, proc);
224 extern struct proclist allproc;		/* List of all processes. */
225 extern struct proclist zombproc;	/* List of zombie processes. */
226 struct proc *initproc, *pageproc;	/* Process slots for init, pager. */
227 
228 #define	NQS	32			/* 32 run queues. */
229 int	whichqs;			/* Bit mask summary of non-empty Q's. */
230 struct	prochd {
231 	struct	proc *ph_link;		/* Linked list of running processes. */
232 	struct	proc *ph_rlink;
233 } qs[NQS];
234 
235 struct proc *pfind __P((pid_t));	/* Find process by id. */
236 struct pgrp *pgfind __P((pid_t));	/* Find process group by id. */
237 
238 int	chgproccnt __P((uid_t uid, int diff));
239 int	enterpgrp __P((struct proc *p, pid_t pgid, int mksess));
240 void	fixjobc __P((struct proc *p, struct pgrp *pgrp, int entering));
241 int	inferior __P((struct proc *p));
242 int	leavepgrp __P((struct proc *p));
243 void	mi_switch __P((void));
244 void	pgdelete __P((struct pgrp *pgrp));
245 void	procinit __P((void));
246 void	resetpriority __P((struct proc *));
247 void	setrunnable __P((struct proc *));
248 void	setrunqueue __P((struct proc *));
249 void	sleep __P((void *chan, int pri));
250 int	tsleep __P((void *chan, int pri, char *wmesg, int timo));
251 void	unsleep __P((struct proc *));
252 void	wakeup __P((void *chan));
253 #endif	/* KERNEL */
254 #endif	/* !_SYS_PROC_H_ */
255