xref: /386bsd/usr/include/nonstd/bsd/sys/proc.h (revision a2142627)
1 /*-
2  * Copyright (c) 1986, 1989, 1991 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *	$Id$
34  */
35 
36 #ifndef _PROC_H_
37 #define	_PROC_H_
38 
39 #include <sys/time.h>
40 #include "ucred.h"
41 #include <machine/proc.h>		/* machine-dependent proc substruct */
42 
43 /*
44  * One structure allocated per session.
45  */
46 struct	session {
47 	int	s_count;		/* ref cnt; pgrps in session */
48 	struct	proc *s_leader;		/* session leader */
49 	struct	vnode *s_ttyvp;		/* vnode of controlling terminal */
50 	struct	tty *s_ttyp;		/* controlling terminal */
51 	char	s_login[MAXLOGNAME];	/* setlogin() name */
52 };
53 
54 /*
55  * One structure allocated per process group.
56  */
57 struct	pgrp {
58 	struct	pgrp *pg_hforw;		/* forward link in hash bucket */
59 	struct	proc *pg_mem;		/* pointer to pgrp members */
60 	struct	session *pg_session;	/* pointer to session */
61 	pid_t	pg_id;			/* pgrp id */
62 	int	pg_jobc;	/* # procs qualifying pgrp for job control */
63 };
64 
65 /*
66  * Description of a process.
67  * This structure contains the information needed to manage a thread
68  * of control, known in UN*X as a process; it has references to substructures
69  * containing descriptions of things that the process uses, but may share
70  * with related processes.  The process structure and the substructures
71  * are always addressible except for those marked "(PROC ONLY)" below,
72  * which might be addressible only on a processor on which the process
73  * is running.
74  */
75 struct	proc {
76 	struct	proc *p_link;		/* doubly-linked run/sleep queue */
77 	struct	proc *p_rlink;
78 	struct	proc *p_nxt;		/* linked list of active procs */
79 	struct	proc **p_prev;		/*    and zombies */
80 
81 	/* substructures: */
82 	struct	filedesc *p_fd;		/* ptr to open files structure */
83 	struct	pstats *p_stats;	/* accounting/statistics (PROC ONLY) */
84 	struct	plimit *p_limit;	/* process limits */
85 	struct	vmspace *p_vmspace;	/* address space */
86 	struct	sigacts *p_sigacts;	/* signal actions, state (PROC ONLY) */
87 
88 #define	p_ucred		p_cred->pc_ucred
89 #define	p_rlimit	p_limit->pl_rlimit
90 
91 	int	p_flag;
92 	char	p_stat;
93 /*	char	p_space; */
94 
95 	pid_t	p_pid;		/* unique process id */
96 	struct	proc *p_hash;	/* hashed based on p_pid for kill+exit+... */
97 	struct	proc *p_pptr;	/* pointer to process structure of parent */
98 	struct	proc *p_osptr;	/* pointer to older sibling processes */
99 
100 /* The following fields are all zeroed upon creation in fork */
101 #define	p_startzero	p_ysptr
102 	struct	proc *p_ysptr;	/* pointer to younger siblings */
103 	struct	proc *p_cptr;	/* pointer to youngest living child */
104 
105 	/* scheduling */
106 	u_int	p_cpu;		/* cpu usage for scheduling */
107 	int	p_cpticks;	/* ticks of cpu time */
108 	fixpt_t	p_pctcpu;	/* %cpu for this process during p_time */
109 	caddr_t p_wchan;	/* event process is awaiting */
110 	u_int	p_time;		/* resident/nonresident time for swapping */
111 	u_int	p_slptime;	/* time since last block */
112 
113 	struct	itimerval p_realtimer;	/* alarm timer */
114 	struct	timeval p_utime;	/* user time */
115 	struct	timeval p_stime;	/* system time */
116 
117 	int	p_traceflag;	/* kernel trace points */
118 	struct	vnode *p_tracep;/* trace to vnode */
119 
120 	int	p_sig;		/* signals pending to this process */
121 
122 /* end area that is zeroed on creation */
123 #define	p_endzero	p_startcopy
124 
125 /* The following fields are all copied upon creation in fork */
126 	sigset_t p_sigmask;	/* current signal mask */
127 #define	p_startcopy	p_sigmask
128 	sigset_t p_sigignore;	/* signals being ignored */
129 	sigset_t p_sigcatch;	/* signals being caught by user */
130 
131 	u_char	p_pri;		/* priority, negative is high */
132 	u_char	p_usrpri;	/* user-priority based on p_cpu and p_nice */
133 	char	p_nice;		/* nice for cpu usage */
134 /*	char	p_space1; */
135 
136 	struct 	pgrp *p_pgrp;	/* pointer to process group */
137 	struct	proc *p_pgrpnxt; /* pointer to next process in process group */
138 	struct	pcred *p_cred;		/* process owner's identity */
139 	char	p_comm[MAXCOMLEN+1];
140 
141 /* end area that is copied on creation */
142 #define	p_endcopy	p_wmesg
143 	char	*p_wmesg;	/* reason for sleep */
144 	/* int	p_thread;	/* id for this "thread" (Mach glue) XXX */
145 	struct	user *p_addr;	/* kernel virtual addr of u-area (PROC ONLY) */
146 	/* swblk_t	p_swaddr;	/* disk address of u area when swapped */
147 	struct	mdproc p_md;	/* any machine-dependent fields */
148 
149 	/* u_short	p_xstat;	/* Exit status for wait; also stop signal */
150 	short	p_dupfd;	/* sideways return value from fdopen XXX */
151 	/* u_short	p_acflag;	/* accounting flags */
152 	u_short	p_stksz;	/* size of per process at p_addr */
153 
154 	long	p_spare[6];	/* tmp spares to avoid shifting eproc */
155 };
156 
157 #define	p_session	p_pgrp->pg_session
158 #define	p_pgid		p_pgrp->pg_id
159 
160 /*
161  * Shareable credentials of process (always resident).
162  * This includes a reference to the current user credentials used by
163  * kernel services, as well as real and saved ids that may be used
164  * to change ids.
165  */
166 struct	pcred {
167 	struct	ucred *pc_ucred;	/* current credentials */
168 	uid_t	p_ruid;			/* real user id */
169 	uid_t	p_svuid;		/* saved effective user id */
170 	gid_t	p_rgid;			/* real group id */
171 	gid_t	p_svgid;		/* saved effective group id */
172 	int	p_refcnt;		/* number of references */
173 };
174 
175 /* stat codes */
176 #define	SSLEEP	1		/* awaiting an event */
177 #define	SWAIT	2		/* (abandoned state) */
178 #define	SRUN	3		/* running */
179 #define	SIDL	4		/* intermediate state in process creation */
180 #define	SZOMB	5		/* intermediate state in process termination */
181 #define	SSTOP	6		/* process being traced */
182 
183 /* flag codes */
184 #define	SLOAD	0x0000001	/* process context is resident */
185 #define	SSYS	0x0000002	/* bootstrap processes (swapper & pager) */
186 #define	SSINTR	0x0000004	/* sleep is interruptible */
187 #define	SCTTY	0x0000008	/* has a controlling terminal */
188 #define	SPPWAIT	0x0000010	/* parent is waiting for child to exec/exit */
189 #define SEXEC	0x0000020	/* process called exec */
190 #define	STIMO	0x0000040	/* timed out during tsleep() */
191 #define	SSEL	0x0000080	/* selecting; wakeup/waiting danger */
192 #define	SWEXIT	0x0000100	/* working on exiting */
193 #define	SNOCLDSTOP \
194 		0x0000200	/* no SIGCHLD when children stop */
195 /* the following three should probably be changed into a hold count */
196 #define	SLOCK	0x0000400	/* process being swapped out */
197 #define	SKEEP	0x0000800	/* another flag to prevent swap out */
198 #define	SPHYSIO	0x0001000	/* doing physical i/o */
199 #define	STRC	0x0004000	/* process is being traced */
200 #define	SWTED	0x0008000	/* stopped/traced process been wait()ed for */
201 #define	SPAGE	0x0020000	/* process in page wait state */
202 #define	SADVLCK	0x0040000	/* process may hold a POSIX advisory lock */
203 #define	SZCHILD	0x0800000	/* process has a zombie child */
204 
205 /*
206  * Priorities.  Note that with 32 run queues, priorities that differ
207  * by less than 4 (128/32) are actually identical.
208  */
209 #define	PSWP	0
210 #define	PVM	4
211 #define	PINOD	8
212 #define	PRIBIO	16
213 #define	PVFS	20
214 #define	PSOCK	24
215 #define	PWAIT	32
216 #define	PLOCK	36
217 #define	PPAUSE	40
218 #define	PUSER	50
219 #define	MAXPRI	127		/* Priorities range from 0 through MAXPRI. */
220 
221 #define	PRIMASK	0x07f
222 #define	PCATCH	0x100		/* OR'd with pri for tsleep to check signals */
223 
224 #define	NZERO	0		/* default "nice" */
225 
226 #ifdef KERNEL
227 /*
228  * We use process IDs <= PID_MAX;
229  * PID_MAX + 1 must also fit in a pid_t
230  * (used to represent "no process group").
231  */
232 #define	PID_MAX		30000
233 #define	NO_PID		30001
234 
235 /* process id hash */
236 #define	PIDHASH(pid)	((pid) & pidhashmask)
237 extern	int pidhashmask;
238 extern	struct proc *pidhash[];
239 void enterpidhash(struct proc *p);
240 void leavepidhash(struct proc *p);
241 struct proc *pfind(pid_t pid);
242 
243 /* enter process into the process id hash table so pfind() can locate it */
244 extern inline void
245 enterpidhash(struct proc *p)
246 {
247 	struct proc **hash = &pidhash[PIDHASH(p->p_pid)];
248 
249 	p->p_hash = *hash;
250 	*hash = p;
251 }
252 
253 /* process to leave pid locate hash table, and is no longer pfind()able. */
254 extern inline void
255 leavepidhash(struct proc *p)
256 {
257 	struct proc **pp;
258 	extern void panic(char *);
259 
260 	for (pp = &pidhash[PIDHASH(p->p_pid)]; *pp; pp = &(*pp)->p_hash)
261 		if (*pp == p) {
262 			*pp = p->p_hash;
263 			return;
264 		}
265 #ifdef	DIAGNOSTIC
266 	panic("leavepidhash");
267 #endif
268 }
269 
270 /* Locate a process by id, returning process pointer if valid process. */
271 extern inline struct proc *
272 pfind(pid_t pid)
273 {
274 	struct proc *p = pidhash[PIDHASH(pid)];
275 
276 	for (; p; p = p->p_hash)
277 		if (p->p_pid == pid)
278 			return (p);
279 	return ((struct proc *)0);
280 }
281 
282 /* process sessions */
283 #define SESS_LEADER(p)	((p)->p_session->s_leader == (p))
284 #define	SESSHOLD(s)	((s)->s_count++)
285 #define	SESSRELE(s)	{ \
286 		if (--(s)->s_count == 0) \
287 			FREE(s, M_SESSION); \
288 	}
289 
290 /* process groups */
291 extern	struct pgrp *pgrphash[];
292 struct pgrp *pgfind(pid_t pgid);
293 void enterpgrp(struct proc *p, pid_t pgid, int mksess);
294 void leavepgrp(struct proc *p);
295 void pgdelete(struct pgrp *pgrp);
296 void fixjobc(struct proc *p, struct pgrp *pgrp, int entering);
297 void pgrpdump(void);
298 
299 int inferior(struct proc *p);
300 void roundrobin(void);
301 void schedcpu(void);
302 void updatepri(struct proc *p);
303 int tsleep(caddr_t chan, int pri, char *wmesg, int timo);
304 void unsleep(struct proc *p);
305 void wakeup(caddr_t chan);
306 void rqinit(void);
307 void setrun(struct proc *p);
308 void setpri(struct proc *p);
309 
310 /* process credentials */
311 struct pcred *modpcred(struct proc *);
312 
313 /* process creation/destruction */
314 int fork1(struct proc *p1, int isvfork, int *retval);
315 void volatile exit(struct proc *p, int rv);
316 
317 struct	proc *zombproc, *allproc;	/* lists of procs in various states */
318 extern	struct proc proc0;		/* process slot for swapper */
319 struct	proc *initproc, *pageproc;	/* process slots for init, pager */
320 extern	struct proc *curproc;		/* current running proc */
321 extern	int nprocs, maxproc;		/* current and max number of procs */
322 
323 #define	NQS	32			/* 32 run queues */
324 #define	PPQ	((MAXPRI+1) / NQS)	/* priorities per queue */
325 struct	prochd {
326 	struct	proc *ph_link;	/* linked list of running processes */
327 	struct	proc *ph_rlink;
328 } qs[NQS];
329 
330 int	whichqs;		/* bit mask summarizing non-empty qs's */
331 #endif	/* KERNEL */
332 
333 #endif	/* !_PROC_H_ */
334