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