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