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