xref: /original-bsd/sys/sys/proc.h (revision 333da485)
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.8 (Berkeley) 01/21/94
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 
21 /*
22  * One structure allocated per session.
23  */
24 struct	session {
25 	int	s_count;		/* Ref cnt; pgrps in session. */
26 	struct	proc *s_leader;		/* Session leader. */
27 	struct	vnode *s_ttyvp;		/* Vnode of controlling terminal. */
28 	struct	tty *s_ttyp;		/* Controlling terminal. */
29 	char	s_login[MAXLOGNAME];	/* Setlogin() name. */
30 };
31 
32 /*
33  * One structure allocated per process group.
34  */
35 struct	pgrp {
36 	struct	pgrp *pg_hforw;		/* Forward link in hash bucket. */
37 	struct	proc *pg_mem;		/* Pointer to pgrp members. */
38 	struct	session *pg_session;	/* Pointer to session. */
39 	pid_t	pg_id;			/* Pgrp id. */
40 	int	pg_jobc;	/* # procs qualifying pgrp for job control */
41 };
42 
43 /*
44  * Description of a process.
45  *
46  * This structure contains the information needed to manage a thread of
47  * control, known in UN*X as a process; it has references to substructures
48  * containing descriptions of things that the process uses, but may share
49  * with related processes.  The process structure and the substructures
50  * are always addressible except for those marked "(PROC ONLY)" below,
51  * which might be addressible only on a processor on which the process
52  * is running.
53  */
54 struct	proc {
55 	struct	proc *p_forw;		/* Doubly-linked run/sleep queue. */
56 	struct	proc *p_back;
57 	struct	proc *p_next;		/* Linked list of active procs */
58 	struct	proc **p_prev;		/*    and zombies. */
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 	struct	proc *p_hash;	 /* Hashed based on p_pid for kill+exit+... */
77 	struct	proc *p_pgrpnxt; /* Pointer to next process in process group. */
78 	struct	proc *p_pptr;	 /* Pointer to process structure of parent. */
79 	struct	proc *p_osptr;	 /* Pointer to older sibling processes. */
80 
81 /* The following fields are all zeroed upon creation in fork. */
82 #define	p_startzero	p_ysptr
83 	struct	proc *p_ysptr;	 /* Pointer to younger siblings. */
84 	struct	proc *p_cptr;	 /* Pointer to youngest living child. */
85 	pid_t	p_oppid;	 /* Save parent pid during ptrace. XXX */
86 	int	p_dupfd;	 /* Sideways return value from fdopen. XXX */
87 
88 	/* scheduling */
89 	u_int	p_estcpu;	 /* Time averaged value of p_cpticks. */
90 	int	p_cpticks;	 /* Ticks of cpu time. */
91 	fixpt_t	p_pctcpu;	 /* %cpu for this process during p_swtime */
92 	void	*p_wchan;	 /* Sleep address. */
93 	char	*p_wmesg;	 /* Reason for sleep. */
94 	u_int	p_swtime;	 /* Time swapped in or out. */
95 	u_int	p_slptime;	 /* Time since last blocked. */
96 
97 	struct	itimerval p_realtimer;	/* Alarm timer. */
98 	struct	timeval p_rtime;	/* Real time. */
99 	u_quad_t p_uticks;		/* Statclock hits in user mode. */
100 	u_quad_t p_sticks;		/* Statclock hits in system mode. */
101 	u_quad_t p_iticks;		/* Statclock hits processing intr. */
102 
103 	int	p_traceflag;		/* Kernel trace points. */
104 	struct	vnode *p_tracep;	/* Trace to vnode. */
105 
106 	int	p_siglist;		/* Signals arrived but not delivered. */
107 
108 	struct	vnode *p_textvp;	/* Vnode of executable. */
109 
110 	long	p_spare[5];		/* pad to 256, avoid shifting eproc. */
111 
112 /* End area that is zeroed on creation. */
113 #define	p_endzero	p_startcopy
114 
115 /* The following fields are all copied upon creation in fork. */
116 #define	p_startcopy	p_sigmask
117 
118 	sigset_t p_sigmask;	/* Current signal mask. */
119 	sigset_t p_sigignore;	/* Signals being ignored. */
120 	sigset_t p_sigcatch;	/* Signals being caught by user. */
121 
122 	u_char	p_priority;	/* Process priority. */
123 	u_char	p_usrpri;	/* User-priority based on p_cpu and p_nice. */
124 	char	p_nice;		/* Process "nice" value. */
125 	char	p_comm[MAXCOMLEN+1];
126 
127 	struct 	pgrp *p_pgrp;	/* Pointer to process group. */
128 
129 /* End area that is copied on creation. */
130 #define	p_endcopy	p_thread
131 	int	p_thread;	/* Id for this "thread"; Mach glue. XXX */
132 	struct	user *p_addr;	/* Kernel virtual addr of u-area (PROC ONLY). */
133 	struct	mdproc p_md;	/* Any machine-dependent fields. */
134 
135 	u_short	p_xstat;	/* Exit status for wait; also stop signal. */
136 	u_short	p_acflag;	/* Accounting flags. */
137 	struct	rusage *p_ru;	/* Exit information. XXX */
138 
139 };
140 
141 #define	p_session	p_pgrp->pg_session
142 #define	p_pgid		p_pgrp->pg_id
143 
144 /* Status values. */
145 #define	SIDL	1		/* Process being created by fork. */
146 #define	SRUN	2		/* Currently runnable. */
147 #define	SSLEEP	3		/* Sleeping on an address. */
148 #define	SSTOP	4		/* Process debugging or suspension. */
149 #define	SZOMB	5		/* Awaiting collection by parent. */
150 
151 /* These flags are kept in p_flags. */
152 #define	P_ADVLOCK	0x00001	/* Process may hold a POSIX advisory lock. */
153 #define	P_CONTROLT	0x00002	/* Has a controlling terminal. */
154 #define	P_INMEM		0x00004	/* Loaded into memory. */
155 #define	P_NOCLDSTOP	0x00008	/* No SIGCHLD when children stop. */
156 #define	P_PPWAIT	0x00010	/* Parent is waiting for child to exec/exit. */
157 #define	P_PROFIL	0x00020	/* Has started profiling. */
158 #define	P_SELECT	0x00040	/* Selecting; wakeup/waiting danger. */
159 #define	P_SINTR		0x00080	/* Sleep is interruptible. */
160 #define	P_SUGID		0x00100	/* Had set id privileges since last exec. */
161 #define	P_SYSTEM	0x00200	/* System proc: no sigs, stats or swapping. */
162 #define	P_TIMEOUT	0x00400	/* Timing out during sleep. */
163 #define	P_TRACED	0x00800	/* Debugged process being traced. */
164 #define	P_WAITED	0x01000	/* Debugging process has waited for child. */
165 #define	P_WEXIT		0x02000	/* Working on exiting. */
166 #define P_EXEC		0x04000	/* Process called exec. */
167 
168 /* Should probably be changed into a hold count. */
169 #define	P_NOSWAP	0x08000	/* Another flag to prevent swap out. */
170 #define	P_PHYSIO	0x10000	/* Doing physical I/O. */
171 
172 /* Should be moved to machine-dependent areas. */
173 #define	P_OWEUPC	0x20000	/* Owe process an addupc() call at next ast. */
174 
175 /*
176  * MOVE TO ucred.h?
177  *
178  * Shareable process credentials (always resident).  This includes a reference
179  * to the current user credentials as well as real and saved ids that may be
180  * used to change ids.
181  */
182 struct	pcred {
183 	struct	ucred *pc_ucred;	/* Current credentials. */
184 	uid_t	p_ruid;			/* Real user id. */
185 	uid_t	p_svuid;		/* Saved effective user id. */
186 	gid_t	p_rgid;			/* Real group id. */
187 	gid_t	p_svgid;		/* Saved effective group id. */
188 	int	p_refcnt;		/* Number of references. */
189 };
190 
191 #ifdef KERNEL
192 /*
193  * We use process IDs <= PID_MAX; PID_MAX + 1 must also fit in a pid_t,
194  * as it is used to represent "no process group".
195  */
196 #define	PID_MAX		30000
197 #define	NO_PID		30001
198 #define	PIDHASH(pid)	((pid) & pidhashmask)
199 
200 #define SESS_LEADER(p)	((p)->p_session->s_leader == (p))
201 #define	SESSHOLD(s)	((s)->s_count++)
202 #define	SESSRELE(s) {							\
203 	if (--(s)->s_count == 0)					\
204 		FREE(s, M_SESSION);					\
205 }
206 
207 extern struct proc *pidhash[];		/* In param.c. */
208 extern struct pgrp *pgrphash[];		/* In param.c. */
209 extern struct proc *curproc;		/* Current running proc. */
210 extern struct proc proc0;		/* Process slot for swapper. */
211 extern int nprocs, maxproc;		/* Current and max number of procs. */
212 extern int pidhashmask;			/* In param.c. */
213 
214 volatile struct proc *allproc; 		/* List of active procs. */
215 struct proc *zombproc;			/* List of zombie procs. */
216 struct proc *initproc, *pageproc;	/* Process slots for init, pager. */
217 
218 #define	NQS	32			/* 32 run queues. */
219 int	whichqs;			/* Bit mask summary of non-empty Q's. */
220 struct	prochd {
221 	struct	proc *ph_link;		/* Linked list of running processes. */
222 	struct	proc *ph_rlink;
223 } qs[NQS];
224 
225 struct proc *pfind __P((pid_t));	/* Find process by id. */
226 struct pgrp *pgfind __P((pid_t));	/* Find process group by id. */
227 
228 void	mi_switch __P((void));
229 void	resetpriority __P((struct proc *));
230 void	setrunnable __P((struct proc *));
231 void	setrunqueue __P((struct proc *));
232 void	sleep __P((void *chan, int pri));
233 int	tsleep __P((void *chan, int pri, char *wmesg, int timo));
234 void	unsleep __P((struct proc *));
235 void	wakeup __P((void *chan));
236 #endif	/* KERNEL */
237 #endif	/* !_SYS_PROC_H_ */
238