proc.h revision 1.73
1/* $NetBSD: proc.h,v 1.73 1999/03/24 05:51:29 mrg Exp $ */ 2 3/*- 4 * Copyright (c) 1986, 1989, 1991, 1993 5 * The Regents of the University of California. All rights reserved. 6 * (c) UNIX System Laboratories, Inc. 7 * All or some portions of this file are derived from material licensed 8 * to the University of California by American Telephone and Telegraph 9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 10 * the permission of UNIX System Laboratories, Inc. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. All advertising materials mentioning features or use of this software 21 * must display the following acknowledgement: 22 * This product includes software developed by the University of 23 * California, Berkeley and its contributors. 24 * 4. Neither the name of the University nor the names of its contributors 25 * may be used to endorse or promote products derived from this software 26 * without specific prior written permission. 27 * 28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 38 * SUCH DAMAGE. 39 * 40 * @(#)proc.h 8.15 (Berkeley) 5/19/95 41 */ 42 43#ifndef _SYS_PROC_H_ 44#define _SYS_PROC_H_ 45 46#include <machine/proc.h> /* Machine-dependent proc substruct. */ 47#include <sys/queue.h> 48 49/* 50 * One structure allocated per session. 51 */ 52struct session { 53 int s_count; /* Ref cnt; pgrps in session. */ 54 struct proc *s_leader; /* Session leader. */ 55 struct vnode *s_ttyvp; /* Vnode of controlling terminal. */ 56 struct tty *s_ttyp; /* Controlling terminal. */ 57 char s_login[MAXLOGNAME]; /* Setlogin() name. */ 58 pid_t s_sid; /* session ID (pid of leader) */ 59}; 60 61/* 62 * One structure allocated per process group. 63 */ 64struct pgrp { 65 LIST_ENTRY(pgrp) pg_hash; /* Hash chain. */ 66 LIST_HEAD(, proc) pg_members; /* Pointer to pgrp members. */ 67 struct session *pg_session; /* Pointer to session. */ 68 pid_t pg_id; /* Pgrp id. */ 69 int pg_jobc; /* # procs qualifying pgrp for job control */ 70}; 71 72/* 73 * One structure allocated per emulation. 74 */ 75struct exec_package; 76struct ps_strings; 77 78struct emul { 79 char e_name[8]; /* Symbolic name */ 80 int *e_errno; /* Errno array */ 81 /* Signal sending function */ 82 void (*e_sendsig) __P((sig_t, int, sigset_t *, u_long)); 83 int e_nosys; /* Offset of the nosys() syscall */ 84 int e_nsysent; /* Number of system call entries */ 85 struct sysent *e_sysent; /* System call array */ 86 char **e_syscallnames; /* System call name array */ 87 int e_arglen; /* Extra argument size in words */ 88 /* Copy arguments on the stack */ 89 void *(*e_copyargs) __P((struct exec_package *, struct ps_strings *, 90 void *, void *)); 91 /* Set registers before execution */ 92 void (*e_setregs) __P((struct proc *, struct exec_package *, 93 u_long)); 94 char *e_sigcode; /* Start of sigcode */ 95 char *e_esigcode; /* End of sigcode */ 96}; 97 98/* 99 * Description of a process. 100 * 101 * This structure contains the information needed to manage a thread of 102 * control, known in UN*X as a process; it has references to substructures 103 * containing descriptions of things that the process uses, but may share 104 * with related processes. The process structure and the substructures 105 * are always addressible except for those marked "(PROC ONLY)" below, 106 * which might be addressible only on a processor on which the process 107 * is running. 108 */ 109struct proc { 110 struct proc *p_forw; /* Doubly-linked run/sleep queue. */ 111 struct proc *p_back; 112 LIST_ENTRY(proc) p_list; /* List of all processes. */ 113 114 /* substructures: */ 115 struct pcred *p_cred; /* Process owner's identity. */ 116 struct filedesc *p_fd; /* Ptr to open files structure. */ 117 struct pstats *p_stats; /* Accounting/statistics (PROC ONLY). */ 118 struct plimit *p_limit; /* Process limits. */ 119 struct vmspace *p_vmspace; /* Address space. */ 120 struct sigacts *p_sigacts; /* Signal actions, state (PROC ONLY). */ 121 122#define p_ucred p_cred->pc_ucred 123#define p_rlimit p_limit->pl_rlimit 124 125 int p_flag; /* P_* flags. */ 126 u_char p_unused; /* XXX: used to be emulation flag */ 127 char p_stat; /* S* process status. */ 128 char p_pad1[2]; 129 130 pid_t p_pid; /* Process identifier. */ 131 LIST_ENTRY(proc) p_hash; /* Hash chain. */ 132 LIST_ENTRY(proc) p_pglist; /* List of processes in pgrp. */ 133 struct proc *p_pptr; /* Pointer to parent process. */ 134 LIST_ENTRY(proc) p_sibling; /* List of sibling processes. */ 135 LIST_HEAD(, proc) p_children; /* Pointer to list of children. */ 136 137/* The following fields are all zeroed upon creation in fork. */ 138#define p_startzero p_oppid 139 140 pid_t p_oppid; /* Save parent pid during ptrace. XXX */ 141 int p_dupfd; /* Sideways return value from filedescopen. XXX */ 142 143 /* scheduling */ 144 u_int p_estcpu; /* Time averaged value of p_cpticks. XXX belongs in p_startcopy section */ 145 int p_cpticks; /* Ticks of cpu time. */ 146 fixpt_t p_pctcpu; /* %cpu for this process during p_swtime */ 147 void *p_wchan; /* Sleep address. */ 148 const char *p_wmesg; /* Reason for sleep. */ 149 u_int p_swtime; /* Time swapped in or out. */ 150 u_int p_slptime; /* Time since last blocked. */ 151 152 struct itimerval p_realtimer; /* Alarm timer. */ 153 struct timeval p_rtime; /* Real time. */ 154 u_quad_t p_uticks; /* Statclock hits in user mode. */ 155 u_quad_t p_sticks; /* Statclock hits in system mode. */ 156 u_quad_t p_iticks; /* Statclock hits processing intr. */ 157 158 int p_traceflag; /* Kernel trace points. */ 159 void *p_tracep; /* Trace to vnode or file */ 160 161 sigset_t p_siglist; /* Signals arrived but not delivered. */ 162 char p_sigcheck; /* May have deliverable signals. */ 163 164 struct vnode *p_textvp; /* Vnode of executable. */ 165 166 short p_locks; /* DEBUG: lockmgr count of held locks */ 167 short p_simple_locks; /* DEBUG: count of held simple locks */ 168 169 int p_holdcnt; /* If non-zero, don't swap. */ 170 struct emul *p_emul; /* Emulation information */ 171 172/* End area that is zeroed on creation. */ 173#define p_endzero p_startcopy 174 175/* The following fields are all copied upon creation in fork. */ 176#define p_startcopy p_sigmask 177 178 sigset_t p_sigmask; /* Current signal mask. */ 179 sigset_t p_sigignore; /* Signals being ignored. */ 180 sigset_t p_sigcatch; /* Signals being caught by user. */ 181 182 u_char p_priority; /* Process priority. */ 183 u_char p_usrpri; /* User-priority based on p_cpu and p_nice. */ 184 u_char p_nice; /* Process "nice" value. */ 185 char p_comm[MAXCOMLEN+1]; 186 187 struct pgrp *p_pgrp; /* Pointer to process group. */ 188 void *p_ctxlink; /* uc_link {get,set}context */ 189 190/* End area that is copied on creation. */ 191#define p_endcopy p_thread 192 193 void *p_thread; /* Id for this "thread"; Mach glue. XXX */ 194 struct user *p_addr; /* Kernel virtual addr of u-area (PROC ONLY). */ 195 struct mdproc p_md; /* Any machine-dependent fields. */ 196 197 u_short p_xstat; /* Exit status for wait; also stop signal. */ 198 u_short p_acflag; /* Accounting flags. */ 199 struct rusage *p_ru; /* Exit information. XXX */ 200}; 201 202#define p_session p_pgrp->pg_session 203#define p_pgid p_pgrp->pg_id 204 205/* Status values. */ 206#define SIDL 1 /* Process being created by fork. */ 207#define SRUN 2 /* Currently runnable. */ 208#define SSLEEP 3 /* Sleeping on an address. */ 209#define SSTOP 4 /* Process debugging or suspension. */ 210#define SZOMB 5 /* Awaiting collection by parent. */ 211 212/* These flags are kept in p_flag. */ 213#define P_ADVLOCK 0x00001 /* Process may hold a POSIX advisory lock. */ 214#define P_CONTROLT 0x00002 /* Has a controlling terminal. */ 215#define P_INMEM 0x00004 /* Loaded into memory. */ 216#define P_NOCLDSTOP 0x00008 /* No SIGCHLD when children stop. */ 217#define P_PPWAIT 0x00010 /* Parent is waiting for child to exec/exit. */ 218#define P_PROFIL 0x00020 /* Has started profiling. */ 219#define P_SELECT 0x00040 /* Selecting; wakeup/waiting danger. */ 220#define P_SINTR 0x00080 /* Sleep is interruptible. */ 221#define P_SUGID 0x00100 /* Had set id privileges since last exec. */ 222#define P_SYSTEM 0x00200 /* System proc: no sigs, stats or swapping. */ 223#define P_TIMEOUT 0x00400 /* Timing out during sleep. */ 224#define P_TRACED 0x00800 /* Debugged process being traced. */ 225#define P_WAITED 0x01000 /* Debugging process has waited for child. */ 226#define P_WEXIT 0x02000 /* Working on exiting. */ 227#define P_EXEC 0x04000 /* Process called exec. */ 228#define P_OWEUPC 0x08000 /* Owe process an addupc() call at next ast. */ 229#define P_FSTRACE 0x10000 /* Debugger process being traced by procfs */ 230#define P_NOCLDWAIT 0x20000 /* No zombies if child dies */ 231 232/* 233 * MOVE TO ucred.h? 234 * 235 * Shareable process credentials (always resident). This includes a reference 236 * to the current user credentials as well as real and saved ids that may be 237 * used to change ids. 238 */ 239struct pcred { 240 struct ucred *pc_ucred; /* Current credentials. */ 241 uid_t p_ruid; /* Real user id. */ 242 uid_t p_svuid; /* Saved effective user id. */ 243 gid_t p_rgid; /* Real group id. */ 244 gid_t p_svgid; /* Saved effective group id. */ 245 int p_refcnt; /* Number of references. */ 246}; 247 248LIST_HEAD(proclist, proc); /* a list of processes */ 249 250/* 251 * This structure associates a proclist with its lock. 252 */ 253struct proclist_desc { 254 struct proclist *pd_list; /* the list */ 255 /* 256 * XXX Add a pointer to the proclist's lock eventually. 257 */ 258}; 259 260#ifdef _KERNEL 261/* 262 * We use process IDs <= PID_MAX; PID_MAX + 1 must also fit in a pid_t, 263 * as it is used to represent "no process group". 264 */ 265#define PID_MAX 30000 266#define NO_PID 30001 267 268#define SESS_LEADER(p) ((p)->p_session->s_leader == (p)) 269#define SESSHOLD(s) ((s)->s_count++) 270#define SESSRELE(s) { \ 271 if (--(s)->s_count == 0) \ 272 FREE(s, M_SESSION); \ 273} 274 275#define PHOLD(p) { \ 276 if ((p)->p_holdcnt++ == 0 && ((p)->p_flag & P_INMEM) == 0) \ 277 uvm_swapin(p); \ 278} 279#define PRELE(p) (--(p)->p_holdcnt) 280 281/* 282 * Flags passed to fork1(). 283 */ 284#define FORK_PPWAIT 0x01 /* block parent until child exit */ 285#define FORK_SHAREVM 0x02 /* share vmspace with parent */ 286 287#define PIDHASH(pid) (&pidhashtbl[(pid) & pidhash]) 288extern LIST_HEAD(pidhashhead, proc) *pidhashtbl; 289extern u_long pidhash; 290 291#define PGRPHASH(pgid) (&pgrphashtbl[(pgid) & pgrphash]) 292extern LIST_HEAD(pgrphashhead, pgrp) *pgrphashtbl; 293extern u_long pgrphash; 294 295/* 296 * Note: <machine/proc.h> may provide a definition of `curproc' while 297 * transition to multi processor support is in progress. 298 */ 299#ifndef curproc 300extern struct proc *curproc; /* Current running proc. */ 301#endif 302extern struct proc proc0; /* Process slot for swapper. */ 303extern int nprocs, maxproc; /* Current and max number of procs. */ 304 305extern struct proclist allproc; /* List of all processes. */ 306extern struct proclist deadproc; /* List of dead processes. */ 307extern struct proclist zombproc; /* List of zombie processes. */ 308 309struct proc *initproc; /* Process slots for init, pager. */ 310 311extern const struct proclist_desc proclists[]; 312 313extern struct pool proc_pool; /* memory pool for procs */ 314extern struct pool pcred_pool; /* memory pool for pcreds */ 315extern struct pool plimit_pool; /* memory pool for plimits */ 316extern struct pool rusage_pool; /* memory pool for rusages */ 317 318#define NQS 32 /* 32 run queues. */ 319int whichqs; /* Bit mask summary of non-empty Q's. */ 320struct prochd { 321 struct proc *ph_link; /* Linked list of running processes. */ 322 struct proc *ph_rlink; 323} qs[NQS]; 324 325struct proc *pfind __P((pid_t)); /* Find process by id. */ 326struct pgrp *pgfind __P((pid_t)); /* Find process group by id. */ 327 328int chgproccnt __P((uid_t uid, int diff)); 329int enterpgrp __P((struct proc *p, pid_t pgid, int mksess)); 330void fixjobc __P((struct proc *p, struct pgrp *pgrp, int entering)); 331int inferior __P((struct proc *p)); 332int leavepgrp __P((struct proc *p)); 333void mi_switch __P((void)); 334void pgdelete __P((struct pgrp *pgrp)); 335void procinit __P((void)); 336void remrunqueue __P((struct proc *)); 337void resetpriority __P((struct proc *)); 338void setrunnable __P((struct proc *)); 339void setrunqueue __P((struct proc *)); 340void sleep __P((void *chan, int pri)); 341void uvm_swapin __P((struct proc *)); /* XXX: uvm_extern.h? */ 342int tsleep __P((void *chan, int pri, const char *wmesg, int timo)); 343void unsleep __P((struct proc *)); 344void wakeup __P((void *chan)); 345void reaper __P((void)); 346void exit1 __P((struct proc *, int)); 347void exit2 __P((struct proc *)); 348int fork1 __P((struct proc *, int, register_t *, struct proc **)); 349void kmeminit __P((void)); 350void rqinit __P((void)); 351int groupmember __P((gid_t, struct ucred *)); 352void cpu_switch __P((struct proc *)); 353void cpu_wait __P((struct proc *)); 354void cpu_exit __P((struct proc *)); 355#endif /* _KERNEL */ 356#endif /* !_SYS_PROC_H_ */ 357