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