proc.h revision 1.218
1/* $NetBSD: proc.h,v 1.218 2006/03/29 23:02:31 cube 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. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 * 36 * @(#)proc.h 8.15 (Berkeley) 5/19/95 37 */ 38 39#ifndef _SYS_PROC_H_ 40#define _SYS_PROC_H_ 41 42#if defined(_KERNEL_OPT) 43#include "opt_multiprocessor.h" 44#include "opt_kstack.h" 45#endif 46 47#include <machine/proc.h> /* Machine-dependent proc substruct */ 48#include <sys/lock.h> 49#include <sys/lwp.h> 50#include <sys/queue.h> 51#include <sys/callout.h> 52#include <sys/signalvar.h> 53#include <sys/siginfo.h> 54#include <sys/event.h> 55 56/* 57 * One structure allocated per session. 58 */ 59struct session { 60 int s_count; /* Ref cnt; pgrps in session */ 61 u_int s_flags; 62#define S_LOGIN_SET 1 /* s_login set in this session */ 63 struct proc *s_leader; /* Session leader */ 64 struct vnode *s_ttyvp; /* Vnode of controlling terminal */ 65 struct tty *s_ttyp; /* Controlling terminal */ 66 char s_login[MAXLOGNAME]; /* Setlogin() name */ 67 pid_t s_sid; /* Session ID (pid of leader) */ 68}; 69 70/* 71 * One structure allocated per process group. 72 */ 73struct pgrp { 74 LIST_HEAD(, proc) pg_members; /* Pointer to pgrp members */ 75 struct session *pg_session; /* Pointer to session */ 76 pid_t pg_id; /* Pgrp id */ 77 int pg_jobc; /* 78 * Number of processes qualifying 79 * pgrp for job control 80 */ 81}; 82 83/* 84 * One structure allocated per emulation. 85 */ 86struct exec_package; 87struct ps_strings; 88struct ras; 89struct sa_emul; 90 91struct emul { 92 const char *e_name; /* Symbolic name */ 93 const char *e_path; /* Extra emulation path (NULL if none)*/ 94#ifndef __HAVE_MINIMAL_EMUL 95 int e_flags; /* Miscellaneous flags, see above */ 96 /* Syscall handling function */ 97 const int *e_errno; /* Errno array */ 98 int e_nosys; /* Offset of the nosys() syscall */ 99 int e_nsysent; /* Number of system call entries */ 100#endif 101 const struct sysent *e_sysent; /* System call array */ 102 const char * const *e_syscallnames; /* System call name array */ 103 /* Signal sending function */ 104 void (*e_sendsig)(const struct ksiginfo *, 105 const sigset_t *); 106 void (*e_trapsignal)(struct lwp *, const struct ksiginfo *); 107 int (*e_tracesig)(struct proc *, int); 108 char *e_sigcode; /* Start of sigcode */ 109 char *e_esigcode; /* End of sigcode */ 110 /* Set registers before execution */ 111 struct uvm_object **e_sigobject;/* shared sigcode object */ 112 void (*e_setregs)(struct lwp *, struct exec_package *, 113 u_long); 114 115 /* Per-process hooks */ 116 void (*e_proc_exec)(struct proc *, struct exec_package *); 117 void (*e_proc_fork)(struct proc *, struct proc *, int); 118 void (*e_proc_exit)(struct proc *); 119 void (*e_lwp_fork)(struct lwp *, struct lwp *); 120 void (*e_lwp_exit)(struct lwp *); 121 122#ifdef __HAVE_SYSCALL_INTERN 123 void (*e_syscall_intern)(struct proc *); 124#else 125 void (*e_syscall)(void); 126#endif 127 /* Emulation specific sysctl data */ 128 struct sysctlnode *e_sysctlovly; 129 int (*e_fault)(struct proc *, vaddr_t, int); 130 131 vaddr_t (*e_vm_default_addr)(struct proc *, vaddr_t, vsize_t); 132 133 /* Emulation-specific hook for userspace page faults */ 134 int (*e_usertrap)(struct lwp *, vaddr_t, void *); 135 136 /* SA-related information */ 137 const struct sa_emul *e_sa; 138}; 139 140/* 141 * Emulation miscelaneous flags 142 */ 143#define EMUL_HAS_SYS___syscall 0x001 /* Has SYS___syscall */ 144 145/* 146 * Description of a process. 147 * 148 * This structure contains the information needed to manage a thread of 149 * control, known in UN*X as a process; it has references to substructures 150 * containing descriptions of things that the process uses, but may share 151 * with related processes. The process structure and the substructures 152 * are always addressible except for those marked "(PROC ONLY)" below, 153 * which might be addressible only on a processor on which the process 154 * is running. 155 * 156 * Fields marked 'p:' are protected by the process's own p_lock. 157 * Fields marked 'l:' are protected by the proclist_lock 158 * Fields marked 's:' are protected by the SCHED_LOCK. 159 */ 160struct proc { 161 LIST_ENTRY(proc) p_list; /* List of all processes */ 162 163 /* Substructures: */ 164 struct pcred *p_cred; /* Process owner's identity */ 165 struct filedesc *p_fd; /* Ptr to open files structure */ 166 struct cwdinfo *p_cwdi; /* cdir/rdir/cmask info */ 167 struct pstats *p_stats; /* Accounting/statistics (PROC ONLY) */ 168 struct plimit *p_limit; /* Process limits */ 169 struct vmspace *p_vmspace; /* Address space */ 170 struct sigacts *p_sigacts; /* Process sigactions (state is below)*/ 171 172 void *p_ksems; /* p1003.1b semaphores */ 173 174#define p_ucred p_cred->pc_ucred 175#define p_rlimit p_limit->pl_rlimit 176 177 int p_exitsig; /* signal to send to parent on exit */ 178 int p_flag; /* P_* flags. */ 179 char p_stat; /* S* process status. */ 180 char p_pad1[3]; 181 182 pid_t p_pid; /* Process identifier. */ 183 LIST_ENTRY(proc) p_pglist; /* l: List of processes in pgrp. */ 184 struct proc *p_pptr; /* l: Pointer to parent process. */ 185 LIST_ENTRY(proc) p_sibling; /* l: List of sibling processes. */ 186 LIST_HEAD(, proc) p_children; /* l: Pointer to list of children. */ 187 188 struct simplelock p_lock; /* Lock on proc state (p:) */ 189 190 /* XXX dsl: locking of LWP info is suspect in schedcpu and kpsignal2 */ 191 LIST_HEAD(, lwp) p_lwps; /* p: Pointer to list of LWPs. */ 192 193 LIST_HEAD(, ras) p_raslist; /* p: Pointer to RAS queue */ 194 195/* The following fields are all zeroed upon creation in fork. */ 196#define p_startzero p_nlwps 197 198 int p_nlwps; /* p: Number of LWPs */ 199 int p_nrlwps; /* s: Number of running LWPs */ 200 int p_nzlwps; /* p: Number of zombie LWPs */ 201 int p_nlwpid; /* p: Next LWP ID */ 202 203 u_int p_nstopchild; /* l: Count of stopped/dead children */ 204 205 struct sadata *p_sa; /* Scheduler activation information */ 206 207 /* scheduling */ 208 fixpt_t p_estcpu; /* Time averaged value of p_cpticks XXX belongs in p_startcopy section */ 209 fixpt_t p_estcpu_inherited; 210 unsigned int p_forktime; 211 int p_cpticks; /* Ticks of CPU time */ 212 fixpt_t p_pctcpu; /* %cpu for this process during p_swtime */ 213 214 struct proc *p_opptr; /* Save parent during ptrace. */ 215 struct ptimers *p_timers; /* Timers: real, virtual, profiling */ 216 struct timeval p_rtime; /* Real time */ 217 u_quad_t p_uticks; /* Statclock hits in user mode */ 218 u_quad_t p_sticks; /* Statclock hits in system mode */ 219 u_quad_t p_iticks; /* Statclock hits processing intr */ 220 221 int p_traceflag; /* Kernel trace points */ 222 void *p_tracep; /* Trace private data */ 223 void *p_systrace; /* Back pointer to systrace */ 224 225 struct vnode *p_textvp; /* Vnode of executable */ 226 227 const struct emul *p_emul; /* Emulation information */ 228 void *p_emuldata; /* Per-process emulation data, or NULL. 229 * Malloc type M_EMULDATA 230 */ 231 232 void (*p_userret)(struct lwp *, void *); 233 /* Function to call at userret(). */ 234 void *p_userret_arg; 235 236 const struct execsw *p_execsw; /* Exec package information */ 237 struct klist p_klist; /* Knotes attached to this process */ 238 239/* 240 * End area that is zeroed on creation 241 */ 242#define p_endzero p_startcopy 243 244/* 245 * The following fields are all copied upon creation in fork. 246 */ 247#define p_startcopy p_sigctx.ps_startcopy 248 249 struct sigctx p_sigctx; /* Signal state */ 250 251 u_char p_nice; /* Process "nice" value */ 252 char p_comm[MAXCOMLEN+1]; /* basename of last exec file */ 253 254 struct pgrp *p_pgrp; /* Pointer to process group */ 255 256 struct ps_strings *p_psstr; /* address of process's ps_strings */ 257 size_t p_psargv; /* offset of ps_argvstr in above */ 258 size_t p_psnargv; /* offset of ps_nargvstr in above */ 259 size_t p_psenv; /* offset of ps_envstr in above */ 260 size_t p_psnenv; /* offset of ps_nenvstr in above */ 261 262/* 263 * End area that is copied on creation 264 */ 265#define p_endcopy p_xstat 266 267 u_short p_xstat; /* Exit status for wait; also stop signal */ 268 u_short p_acflag; /* Accounting flags */ 269 struct rusage *p_ru; /* Exit information. XXX */ 270 271 struct mdproc p_md; /* Any machine-dependent fields */ 272}; 273 274#define p_session p_pgrp->pg_session 275#define p_pgid p_pgrp->pg_id 276 277/* 278 * Status values. 279 * 280 */ 281#define SIDL 1 /* Process being created by fork */ 282#define SACTIVE 2 /* Process is not stopped */ 283#define SSTOP 4 /* Process debugging or suspension */ 284#define SZOMB 5 /* Awaiting collection by parent */ 285 286#define P_ZOMBIE(p) ((p)->p_stat == SZOMB) 287 288/* These flags are kept in p_flag. */ 289#define P_ADVLOCK 0x00000001 /* Process may hold a POSIX advisory lock */ 290#define P_CONTROLT 0x00000002 /* Has a controlling terminal */ 291#define P_SYSCALL 0x00000004 /* process has PT_SYSCALL enabled */ 292#define P_NOCLDSTOP 0x00000008 /* No SIGCHLD when children stop */ 293#define P_PPWAIT 0x00000010 /* Parent is waiting for child exec/exit */ 294#define P_PROFIL 0x00000020 /* Has started profiling */ 295#define P_SUGID 0x00000100 /* Had set id privileges since last exec */ 296#define P_SYSTEM 0x00000200 /* System proc: no sigs, stats or swapping */ 297#define P_SA 0x00000400 /* Using scheduler activations */ 298#define P_TRACED 0x00000800 /* Debugged process being traced */ 299#define P_WAITED 0x00001000 /* Debugging process has waited for child */ 300#define P_WEXIT 0x00002000 /* Working on exiting */ 301#define P_EXEC 0x00004000 /* Process called exec */ 302#define P_OWEUPC 0x00008000 /* Owe process an addupc() at next ast */ 303#define P_FSTRACE 0x00010000 /* Debugger process being traced by procfs */ 304#define P_NOCLDWAIT 0x00020000 /* No zombies if child dies */ 305#define P_32 0x00040000 /* 32-bit process (used on 64-bit kernels) */ 306#define P_CLDSIGIGN 0x00080000 /* Process is ignoring SIGCHLD */ 307#define P_INEXEC 0x00100000 /* Process is exec'ing and can't be traced */ 308#define P_SYSTRACE 0x00200000 /* Process system call tracing active */ 309#define P_CHTRACED 0x00400000 /* Child has been traced & reparented */ 310#define P_STOPFORK 0x00800000 /* Child will be stopped on fork(2) */ 311#define P_STOPEXEC 0x01000000 /* Will be stopped on exec(2) */ 312#define P_STOPEXIT 0x02000000 /* Will be stopped at process exit */ 313#define P_MARKER 0x80000000 /* Is a dummy marker process */ 314 315/* 316 * Macro to compute the exit signal to be delivered. 317 */ 318#define P_EXITSIG(p) (((p)->p_flag & (P_TRACED|P_FSTRACE)) ? SIGCHLD : \ 319 p->p_exitsig) 320 321/* 322 * MOVE TO ucred.h? 323 * 324 * Shareable process credentials (always resident). This includes a reference 325 * to the current user credentials as well as real and saved ids that may be 326 * used to change ids. 327 */ 328struct pcred { 329 struct ucred *pc_ucred; /* Current credentials */ 330 uid_t p_ruid; /* Real user id */ 331 uid_t p_svuid; /* Saved effective user id */ 332 gid_t p_rgid; /* Real group id */ 333 gid_t p_svgid; /* Saved effective group id */ 334 int p_refcnt; /* Number of references */ 335}; 336 337LIST_HEAD(proclist, proc); /* A list of processes */ 338 339/* 340 * This structure associates a proclist with its lock. 341 */ 342struct proclist_desc { 343 struct proclist *pd_list; /* The list */ 344 /* 345 * XXX Add a pointer to the proclist's lock eventually. 346 */ 347}; 348 349#ifdef _KERNEL 350#include <sys/mallocvar.h> 351MALLOC_DECLARE(M_EMULDATA); 352MALLOC_DECLARE(M_PROC); 353MALLOC_DECLARE(M_SESSION); 354MALLOC_DECLARE(M_SUBPROC); /* XXX - only used by sparc/sparc64 */ 355 356/* 357 * We use process IDs <= PID_MAX until there are > 16k processes. 358 * NO_PGID is used to represent "no process group" for a tty. 359 */ 360#define PID_MAX 30000 361#define NO_PGID ((pid_t)-1) 362 363#define SESS_LEADER(p) ((p)->p_session->s_leader == (p)) 364#define SESSHOLD(s) ((s)->s_count++) 365#define SESSRELE(s) \ 366do { \ 367 if (--(s)->s_count == 0) \ 368 sessdelete(s); \ 369} while (/* CONSTCOND */ 0) 370 371 372/* 373 * Flags passed to fork1(). 374 */ 375#define FORK_PPWAIT 0x01 /* Block parent until child exit */ 376#define FORK_SHAREVM 0x02 /* Share vmspace with parent */ 377#define FORK_SHARECWD 0x04 /* Share cdir/rdir/cmask */ 378#define FORK_SHAREFILES 0x08 /* Share file descriptors */ 379#define FORK_SHARESIGS 0x10 /* Share signal actions */ 380#define FORK_NOWAIT 0x20 /* Make init the parent of the child */ 381#define FORK_CLEANFILES 0x40 /* Start with a clean descriptor set */ 382 383/* 384 * Allow machine-dependent code to override curproc in <machine/cpu.h> for 385 * its own convenience. Otherwise, we declare it as appropriate. 386 */ 387#if !defined(curlwp) 388#if defined(MULTIPROCESSOR) 389#define curlwp curcpu()->ci_curlwp /* Current running LWP */ 390#else 391extern struct lwp *curlwp; /* Current running LWP */ 392#endif /* MULTIPROCESSOR */ 393#endif /* ! curproc */ 394 395static struct proc *__curproc(void); 396 397static __inline struct proc * 398__curproc() 399{ 400 struct lwp *l = curlwp; 401 402 if (l == NULL) 403 return NULL; 404 return l->l_proc; 405} 406#define curproc __curproc() 407 408extern struct proc proc0; /* Process slot for swapper */ 409extern int nprocs, maxproc; /* Current and max number of procs */ 410#define vmspace_kernel() (proc0.p_vmspace) 411 412/* Process list lock; see kern_proc.c for locking protocol details */ 413extern struct lock proclist_lock; 414 415extern struct proclist allproc; /* List of all processes */ 416extern struct proclist zombproc; /* List of zombie processes */ 417 418extern SLIST_HEAD(deadprocs, proc) deadprocs; /* List of dead processes */ 419extern struct simplelock deadproc_slock; 420 421extern struct proc *initproc; /* Process slots for init, pager */ 422 423extern const struct proclist_desc proclists[]; 424 425extern struct pool pcred_pool; /* Memory pool for pcreds */ 426extern struct pool plimit_pool; /* Memory pool for plimits */ 427extern struct pool pstats_pool; /* memory pool for pstats */ 428extern struct pool rusage_pool; /* Memory pool for rusages */ 429extern struct pool ptimer_pool; /* Memory pool for ptimers */ 430 431struct proc *p_find(pid_t, uint); /* Find process by id */ 432struct pgrp *pg_find(pid_t, uint); /* Find process group by id */ 433/* Flags values for p_find() and pg_find(). */ 434#define PFIND_ZOMBIE 1 /* look for zombies as well */ 435#define PFIND_LOCKED 2 /* proclist locked on entry */ 436#define PFIND_UNLOCK_FAIL 4 /* unlock proclist on failure */ 437#define PFIND_UNLOCK_OK 8 /* unlock proclist on success */ 438#define PFIND_UNLOCK (PFIND_UNLOCK_OK | PFIND_UNLOCK_FAIL) 439/* For source compatibility. but UNLOCK_OK gives a stale answer... */ 440#define pfind(pid) p_find((pid), PFIND_UNLOCK) 441#define pgfind(pgid) pg_find((pgid), PFIND_UNLOCK) 442 443struct simplelock; 444int enterpgrp(struct proc *, pid_t, int); 445void fixjobc(struct proc *, struct pgrp *, int); 446int inferior(struct proc *, struct proc *); 447int leavepgrp(struct proc *); 448void sessdelete(struct session *); 449void yield(void); 450struct lwp *chooselwp(void); 451void pgdelete(struct pgrp *); 452void procinit(void); 453void resetprocpriority(struct proc *); 454void suspendsched(void); 455int ltsleep(volatile const void *, int, const char *, int, 456 volatile struct simplelock *); 457void wakeup(volatile const void *); 458void wakeup_one(volatile const void *); 459void exit1(struct lwp *, int); 460int find_stopped_child(struct proc *, pid_t, int, struct proc **); 461struct proc *proc_alloc(void); 462void proc0_init(void); 463void proc_free(struct proc *); 464void proc_free_mem(struct proc *); 465void exit_lwps(struct lwp *l); 466int fork1(struct lwp *, int, int, void *, size_t, 467 void (*)(void *), void *, register_t *, struct proc **); 468void rqinit(void); 469int groupmember(gid_t, const struct ucred *); 470int pgid_in_session(struct proc *, pid_t); 471#ifndef cpu_idle 472void cpu_idle(void); 473#endif 474void cpu_exit(struct lwp *); 475void cpu_lwp_fork(struct lwp *, struct lwp *, void *, size_t, 476 void (*)(void *), void *); 477#ifndef cpu_lwp_free 478void cpu_lwp_free(struct lwp *, int); 479#endif 480 481#ifdef __HAVE_SYSCALL_INTERN 482void syscall_intern(struct proc *); 483#endif 484 485void child_return(void *); 486 487int proc_isunder(struct proc *, struct lwp *); 488void proc_stop(struct proc *, int); 489 490void proclist_lock_read(void); 491void proclist_unlock_read(void); 492int proclist_lock_write(void); 493void proclist_unlock_write(int); 494void p_sugid(struct proc *); 495 496int proc_vmspace_getref(struct proc *, struct vmspace **); 497 498int proclist_foreach_call(struct proclist *, 499 int (*)(struct proc *, void *arg), void *); 500static __inline struct proc *_proclist_skipmarker(struct proc *); 501 502static __inline struct proc * 503_proclist_skipmarker(struct proc *p0) 504{ 505 struct proc *p = p0; 506 507 while (p != NULL && p->p_flag & P_MARKER) 508 p = LIST_NEXT(p, p_list); 509 510 return p; 511} 512#define PROCLIST_FOREACH(var, head) \ 513 for ((var) = LIST_FIRST(head); \ 514 ((var) = _proclist_skipmarker(var)) != NULL; \ 515 (var) = LIST_NEXT(var, p_list)) 516 517/* Compatibility with old, non-interlocked tsleep call */ 518#define tsleep(chan, pri, wmesg, timo) \ 519 ltsleep(chan, pri, wmesg, timo, NULL) 520 521#if defined(MULTIPROCESSOR) 522void proc_trampoline_mp(void); /* XXX */ 523#endif 524 525#ifdef KSTACK_CHECK_MAGIC 526void kstack_setup_magic(const struct lwp *); 527void kstack_check_magic(const struct lwp *); 528#endif 529 530/* 531 * kernel stack paramaters 532 * XXX require sizeof(struct user) 533 */ 534/* the lowest address of kernel stack */ 535#ifndef KSTACK_LOWEST_ADDR 536#define KSTACK_LOWEST_ADDR(l) ((caddr_t)ALIGN((l)->l_addr + 1)) 537#endif 538/* size of kernel stack */ 539#ifndef KSTACK_SIZE 540#define KSTACK_SIZE (USPACE - ALIGN(sizeof(struct user))) 541#endif 542 543#endif /* _KERNEL */ 544#endif /* !_SYS_PROC_H_ */ 545