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