1 /* $OpenBSD: proc.h,v 1.100 2007/07/25 23:11:53 art Exp $ */
2 /* $NetBSD: proc.h,v 1.44 1996/04/22 01:23:21 christos Exp $ */
3
4 /*-
5 * Copyright (c) 1986, 1989, 1991, 1993
6 * The Regents of the University of California. All rights reserved.
7 * (c) UNIX System Laboratories, Inc.
8 * All or some portions of this file are derived from material licensed
9 * to the University of California by American Telephone and Telegraph
10 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
11 * the permission of UNIX System Laboratories, Inc.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
36 *
37 * @(#)proc.h 8.8 (Berkeley) 1/21/94
38 */
39
40 #ifndef _SYS_PROC_H_
41 #define _SYS_PROC_H_
42
43 #include <machine/proc.h> /* Machine-dependent proc substruct. */
44 #include <sys/selinfo.h> /* For struct selinfo. */
45 #include <sys/queue.h>
46 #include <sys/timeout.h> /* For struct timeout. */
47 #include <sys/event.h> /* For struct klist */
48 #include <machine/atomic.h>
49
50 #define curproc curcpu()->ci_curproc
51 #ifdef _KERNEL
52 #define __need_process
53 #endif
54
55 /*
56 * One structure allocated per session.
57 */
58 struct session {
59 int s_count; /* Ref cnt; pgrps in session. */
60 struct proc *s_leader; /* Session leader. */
61 struct vnode *s_ttyvp; /* Vnode of controlling terminal. */
62 struct tty *s_ttyp; /* Controlling terminal. */
63 char s_login[MAXLOGNAME]; /* Setlogin() name. */
64 };
65
66 /*
67 * One structure allocated per process group.
68 */
69 struct pgrp {
70 LIST_ENTRY(pgrp) pg_hash; /* Hash chain. */
71 LIST_HEAD(, proc) pg_members; /* Pointer to pgrp members. */
72 struct session *pg_session; /* Pointer to session. */
73 pid_t pg_id; /* Pgrp id. */
74 int pg_jobc; /* # procs qualifying pgrp for job control */
75 };
76
77 /*
78 * One structure allocated per emulation.
79 */
80 struct exec_package;
81 struct ps_strings;
82 struct uvm_object;
83 union sigval;
84
85 struct emul {
86 char e_name[8]; /* Symbolic name */
87 int *e_errno; /* Errno array */
88 /* Signal sending function */
89 void (*e_sendsig)(sig_t, int, int, u_long, int, union sigval);
90 int e_nosys; /* Offset of the nosys() syscall */
91 int e_nsysent; /* Number of system call entries */
92 struct sysent *e_sysent; /* System call array */
93 char **e_syscallnames; /* System call name array */
94 int e_arglen; /* Extra argument size in words */
95 /* Copy arguments on the stack */
96 void *(*e_copyargs)(struct exec_package *, struct ps_strings *,
97 void *, void *);
98 /* Set registers before execution */
99 void (*e_setregs)(struct proc *, struct exec_package *,
100 u_long, register_t *);
101 int (*e_fixup)(struct proc *, struct exec_package *);
102 char *e_sigcode; /* Start of sigcode */
103 char *e_esigcode; /* End of sigcode */
104 int e_flags; /* Flags, see below */
105 struct uvm_object *e_sigobject; /* shared sigcode object */
106 /* Per-process hooks */
107 void (*e_proc_exec)(struct proc *, struct exec_package *);
108 void (*e_proc_fork)(struct proc *p, struct proc *parent);
109 void (*e_proc_exit)(struct proc *);
110 };
111 /* Flags for e_flags */
112 #define EMUL_ENABLED 0x0001 /* Allow exec to continue */
113 #define EMUL_NATIVE 0x0002 /* Always enabled */
114
115 extern struct emul *emulsw[]; /* All emuls in system */
116 extern int nemuls; /* Number of emuls */
117
118 /*
119 * Description of a process.
120 *
121 * These structures contain the information needed to manage a thread of
122 * control, known in UN*X as a process; it has references to substructures
123 * containing descriptions of things that the process uses, but may share
124 * with related processes.
125 *
126 * struct process is the higher level process containing information
127 * shared by all threads in a process, while struct proc contains the
128 * run-time information needed by threads.
129 */
130 #ifdef __need_process
131 struct process {
132 /*
133 * ps_mainproc is the main thread in the process.
134 * Ultimately, we shouldn't need that, threads should be able to exit
135 * at will. Unfortunately until the pid is moved into struct process
136 * we'll have to remember the main threads and abuse its pid as the
137 * the pid of the process. This is gross, but considering the horrible
138 * pid semantics we have right now, it's unavoidable.
139 */
140 struct proc *ps_mainproc;
141 struct pcred *ps_cred; /* Process owner's identity. */
142 struct plimit *ps_limit; /* Process limits. */
143
144 TAILQ_HEAD(,proc) ps_threads; /* Threads in this process. */
145 };
146 #else
147 struct process;
148 #endif
149
150 struct proc {
151 struct proc *p_forw; /* Doubly-linked run/sleep queue. */
152 struct proc *p_back;
153 LIST_ENTRY(proc) p_list; /* List of all processes. */
154
155 struct process *p_p; /* The process of this thread. */
156 TAILQ_ENTRY(proc) p_thr_link;/* Threads in a process linkage. */
157
158 /* substructures: */
159 struct filedesc *p_fd; /* Ptr to open files structure. */
160 struct pstats *p_stats; /* Accounting/statistics */
161 struct vmspace *p_vmspace; /* Address space. */
162 struct sigacts *p_sigacts; /* Signal actions, state */
163 #define p_cred p_p->ps_cred
164 #define p_ucred p_cred->pc_ucred
165 #define p_rlimit p_p->ps_limit->pl_rlimit
166
167 int p_exitsig; /* Signal to send to parent on exit. */
168 int p_flag; /* P_* flags. */
169 u_char p_os; /* OS tag */
170 char p_stat; /* S* process status. */
171 char p_pad1[1];
172 u_char p_descfd; /* if not 255, fdesc permits this fd */
173
174 pid_t p_pid; /* Process identifier. */
175 LIST_ENTRY(proc) p_hash; /* Hash chain. */
176 LIST_ENTRY(proc) p_pglist; /* List of processes in pgrp. */
177 struct proc *p_pptr; /* Pointer to parent process. */
178 LIST_ENTRY(proc) p_sibling; /* List of sibling processes. */
179 LIST_HEAD(, proc) p_children; /* Pointer to list of children. */
180
181 /* The following fields are all zeroed upon creation in fork. */
182 #define p_startzero p_oppid
183
184 pid_t p_oppid; /* Save parent pid during ptrace. XXX */
185 int p_dupfd; /* Sideways return value from filedescopen. XXX */
186
187 long p_thrslpid; /* for thrsleep syscall */
188
189
190 /* scheduling */
191 u_int p_estcpu; /* Time averaged value of p_cpticks. */
192 int p_cpticks; /* Ticks of cpu time. */
193 fixpt_t p_pctcpu; /* %cpu for this process during p_swtime */
194 void *p_wchan; /* Sleep address. */
195 struct timeout p_sleep_to;/* timeout for tsleep() */
196 const char *p_wmesg; /* Reason for sleep. */
197 u_int p_swtime; /* Time swapped in or out. */
198 u_int p_slptime; /* Time since last blocked. */
199 struct cpu_info * __volatile p_cpu; /* CPU we're running on. */
200
201 struct itimerval p_realtimer; /* Alarm timer. */
202 struct timeout p_realit_to; /* Alarm timeout. */
203 struct timeval p_rtime; /* Real time. */
204 u_quad_t p_uticks; /* Statclock hits in user mode. */
205 u_quad_t p_sticks; /* Statclock hits in system mode. */
206 u_quad_t p_iticks; /* Statclock hits processing intr. */
207
208 int p_traceflag; /* Kernel trace points. */
209 struct vnode *p_tracep; /* Trace to vnode. */
210
211 void *p_systrace; /* Back pointer to systrace */
212
213 int p_ptmask; /* Ptrace event mask */
214 struct ptrace_state *p_ptstat; /* Ptrace state */
215
216 int p_siglist; /* Signals arrived but not delivered. */
217
218 struct vnode *p_textvp; /* Vnode of executable. */
219
220 struct emul *p_emul; /* Emulation information */
221 void *p_emuldata; /* Per-process emulation data, or */
222 /* NULL. Malloc type M_EMULDATA */
223 struct klist p_klist; /* knotes attached to this process */
224 /* pad to 256, avoid shifting eproc. */
225
226 sigset_t p_sigdivert; /* Signals to be diverted to thread. */
227
228 /* End area that is zeroed on creation. */
229 #define p_endzero p_startcopy
230
231 /* The following fields are all copied upon creation in fork. */
232 #define p_startcopy p_sigmask
233
234 sigset_t p_sigmask; /* Current signal mask. */
235 sigset_t p_sigignore; /* Signals being ignored. */
236 sigset_t p_sigcatch; /* Signals being caught by user. */
237
238 u_char p_priority; /* Process priority. */
239 u_char p_usrpri; /* User-priority based on p_cpu and p_nice. */
240 char p_nice; /* Process "nice" value. */
241 char p_comm[MAXCOMLEN+1];
242
243 struct pgrp *p_pgrp; /* Pointer to process group. */
244 vaddr_t p_sigcode; /* user pointer to the signal code. */
245
246 /* End area that is copied on creation. */
247 #define p_endcopy p_addr
248
249 struct user *p_addr; /* Kernel virtual addr of u-area */
250 struct mdproc p_md; /* Any machine-dependent fields. */
251
252 u_short p_xstat; /* Exit status for wait; also stop signal. */
253 u_short p_acflag; /* Accounting flags. */
254 struct rusage *p_ru; /* Exit information. XXX */
255 };
256
257 #define p_session p_pgrp->pg_session
258 #define p_pgid p_pgrp->pg_id
259
260 /* Status values. */
261 #define SIDL 1 /* Process being created by fork. */
262 #define SRUN 2 /* Currently runnable. */
263 #define SSLEEP 3 /* Sleeping on an address. */
264 #define SSTOP 4 /* Process debugging or suspension. */
265 #define SZOMB 5 /* Awaiting collection by parent. */
266 #define SDEAD 6 /* Process is almost a zombie. */
267 #define SONPROC 7 /* Process is currently on a CPU. */
268
269 #define P_ZOMBIE(p) ((p)->p_stat == SZOMB || (p)->p_stat == SDEAD)
270
271 /* These flags are kept in p_flag. */
272 #define P_ADVLOCK 0x000001 /* Proc may hold a POSIX adv. lock. */
273 #define P_CONTROLT 0x000002 /* Has a controlling terminal. */
274 #define P_INMEM 0x000004 /* Loaded into memory. UNUSED */
275 #define P_NOCLDSTOP 0x000008 /* No SIGCHLD when children stop. */
276 #define P_PPWAIT 0x000010 /* Parent waits for child exec/exit. */
277 #define P_PROFIL 0x000020 /* Has started profiling. */
278 #define P_SELECT 0x000040 /* Selecting; wakeup/waiting danger. */
279 #define P_SINTR 0x000080 /* Sleep is interruptible. */
280 #define P_SUGID 0x000100 /* Had set id privs since last exec. */
281 #define P_SYSTEM 0x000200 /* No sigs, stats or swapping. */
282 #define P_TIMEOUT 0x000400 /* Timing out during sleep. */
283 #define P_TRACED 0x000800 /* Debugged process being traced. */
284 #define P_WAITED 0x001000 /* Debugging proc has waited for child. */
285 /* XXX - Should be merged with INEXEC */
286 #define P_WEXIT 0x002000 /* Working on exiting. */
287 #define P_EXEC 0x004000 /* Process called exec. */
288
289 /* Should be moved to machine-dependent areas. */
290 #define P_OWEUPC 0x008000 /* Owe proc an addupc() at next ast. */
291
292 /* XXX Not sure what to do with these, yet. */
293 #define P_SSTEP 0x020000 /* proc needs single-step fixup ??? */
294 #define P_SUGIDEXEC 0x040000 /* last execve() was set[ug]id */
295
296 #define P_NOCLDWAIT 0x080000 /* Let pid 1 wait for my children */
297 #define P_NOZOMBIE 0x100000 /* Pid 1 waits for me instead of dad */
298 #define P_INEXEC 0x200000 /* Process is doing an exec right now */
299 #define P_SYSTRACE 0x400000 /* Process system call tracing active*/
300 #define P_CONTINUED 0x800000 /* Proc has continued from a stopped state. */
301 #define P_BIGLOCK 0x2000000 /* Process needs kernel "big lock" to run */
302 #define P_THREAD 0x4000000 /* Only a thread, not a real process */
303 #define P_IGNEXITRV 0x8000000 /* For thread kills */
304 #define P_SOFTDEP 0x10000000 /* Stuck processing softdep worklist */
305 #define P_STOPPED 0x20000000 /* Just stopped. */
306
307 #define P_BITS \
308 ("\20\01ADVLOCK\02CTTY\04NOCLDSTOP\05PPWAIT\06PROFIL\07SELECT" \
309 "\010SINTR\011SUGID\012SYSTEM\013TIMEOUT\014TRACED\015WAITED\016WEXIT" \
310 "\017EXEC\020PWEUPC\022SSTEP\023SUGIDEXEC\024NOCLDWAIT" \
311 "\025NOZOMBIE\026INEXEC\027SYSTRACE\030CONTINUED\032BIGLOCK" \
312 "\033THREAD\034IGNEXITRV\035SOFTDEP\036STOPPED")
313
314 /* Macro to compute the exit signal to be delivered. */
315 #define P_EXITSIG(p) \
316 (((p)->p_flag & P_TRACED) ? SIGCHLD : (p)->p_exitsig)
317
318 /*
319 * MOVE TO ucred.h?
320 *
321 * Shareable process credentials (always resident). This includes a reference
322 * to the current user credentials as well as real and saved ids that may be
323 * used to change ids.
324 */
325 struct pcred {
326 struct ucred *pc_ucred; /* Current credentials. */
327 uid_t p_ruid; /* Real user id. */
328 uid_t p_svuid; /* Saved effective user id. */
329 gid_t p_rgid; /* Real group id. */
330 gid_t p_svgid; /* Saved effective group id. */
331 int p_refcnt; /* Number of references. */
332 };
333
334 #ifdef _KERNEL
335
336 struct uidinfo {
337 LIST_ENTRY(uidinfo) ui_hash;
338 uid_t ui_uid;
339 long ui_proccnt; /* proc structs */
340 long ui_lockcnt; /* lockf structs */
341 };
342
343 struct uidinfo *uid_find(uid_t);
344
345 /*
346 * We use process IDs <= PID_MAX; PID_MAX + 1 must also fit in a pid_t,
347 * as it is used to represent "no process group".
348 * We set PID_MAX to (SHRT_MAX - 1) so we don't break sys/compat.
349 */
350 #define PID_MAX 32766
351 #define NO_PID (PID_MAX+1)
352
353 #define SESS_LEADER(p) ((p)->p_session->s_leader == (p))
354 #define SESSHOLD(s) ((s)->s_count++)
355 #define SESSRELE(s) { \
356 if (--(s)->s_count == 0) \
357 pool_put(&session_pool, s); \
358 }
359
360 /*
361 * Flags to fork1().
362 */
363 #define FORK_FORK 0x00000001
364 #define FORK_VFORK 0x00000002
365 #define FORK_RFORK 0x00000004
366 #define FORK_PPWAIT 0x00000008
367 #define FORK_SHAREFILES 0x00000010
368 #define FORK_CLEANFILES 0x00000020
369 #define FORK_NOZOMBIE 0x00000040
370 #define FORK_SHAREVM 0x00000080
371 #define FORK_SIGHAND 0x00000200
372 #define FORK_PTRACE 0x00000400
373 #define FORK_THREAD 0x00000800
374
375 #define EXIT_NORMAL 0x00000001
376 #define EXIT_THREAD 0x00000002
377
378 #define PIDHASH(pid) (&pidhashtbl[(pid) & pidhash])
379 extern LIST_HEAD(pidhashhead, proc) *pidhashtbl;
380 extern u_long pidhash;
381
382 #define PGRPHASH(pgid) (&pgrphashtbl[(pgid) & pgrphash])
383 extern LIST_HEAD(pgrphashhead, pgrp) *pgrphashtbl;
384 extern u_long pgrphash;
385
386 extern struct proc proc0; /* Process slot for swapper. */
387 extern int nprocs, maxproc; /* Current and max number of procs. */
388 extern int randompid; /* fork() should create random pid's */
389
390 LIST_HEAD(proclist, proc);
391 extern struct proclist allproc; /* List of all processes. */
392 extern struct proclist zombproc; /* List of zombie processes. */
393
394 extern struct proc *initproc; /* Process slots for init, pager. */
395 extern struct proc *syncerproc; /* filesystem syncer daemon */
396
397 extern struct pool process_pool; /* memory pool for processes */
398 extern struct pool proc_pool; /* memory pool for procs */
399 extern struct pool rusage_pool; /* memory pool for zombies */
400 extern struct pool ucred_pool; /* memory pool for ucreds */
401 extern struct pool session_pool; /* memory pool for sessions */
402 extern struct pool pcred_pool; /* memory pool for pcreds */
403
404 #define NQS 32 /* 32 run queues. */
405 extern int whichqs; /* Bit mask summary of non-empty Q's. */
406 struct prochd {
407 struct proc *ph_link; /* Linked list of running processes. */
408 struct proc *ph_rlink;
409 };
410 extern struct prochd qs[NQS];
411
412 struct simplelock;
413
414 struct proc *pfind(pid_t); /* Find process by id. */
415 struct pgrp *pgfind(pid_t); /* Find process group by id. */
416 void proc_printit(struct proc *p, const char *modif,
417 int (*pr)(const char *, ...));
418
419 int chgproccnt(uid_t uid, int diff);
420 int enterpgrp(struct proc *p, pid_t pgid, int mksess);
421 void fixjobc(struct proc *p, struct pgrp *pgrp, int entering);
422 int inferior(struct proc *p);
423 int leavepgrp(struct proc *p);
424 void yield(void);
425 void preempt(struct proc *);
426 void mi_switch(void);
427 void pgdelete(struct pgrp *pgrp);
428 void procinit(void);
429 #if !defined(remrunqueue)
430 void remrunqueue(struct proc *);
431 #endif
432 void resetpriority(struct proc *);
433 void setrunnable(struct proc *);
434 #if !defined(setrunqueue)
435 void setrunqueue(struct proc *);
436 #endif
437 void unsleep(struct proc *);
438 void wakeup_n(void *chan, int);
439 void wakeup(void *chan);
440 #define wakeup_one(c) wakeup_n((c), 1)
441 void reaper(void);
442 void exit1(struct proc *, int, int);
443 void exit2(struct proc *);
444 int fork1(struct proc *, int, int, void *, size_t, void (*)(void *),
445 void *, register_t *, struct proc **);
446 void rqinit(void);
447 int groupmember(gid_t, struct ucred *);
448 #if !defined(cpu_switch)
449 void cpu_switch(struct proc *);
450 #endif
451 #if !defined(cpu_wait)
452 void cpu_wait(struct proc *);
453 #endif
454 void cpu_exit(struct proc *);
455
456 void child_return(void *);
457
458 int proc_cansugid(struct proc *);
459 void proc_zap(struct proc *);
460
461 struct sleep_state {
462 int sls_s;
463 int sls_catch;
464 int sls_do_sleep;
465 int sls_sig;
466 };
467
468 void sleep_setup(struct sleep_state *, void *, int, const char *);
469 void sleep_setup_timeout(struct sleep_state *, int);
470 void sleep_setup_signal(struct sleep_state *, int);
471 void sleep_finish(struct sleep_state *, int);
472 int sleep_finish_timeout(struct sleep_state *);
473 int sleep_finish_signal(struct sleep_state *);
474
475 int tsleep(void *, int, const char *, int);
476 #define ltsleep(c, p, w, t, l) tsleep(c, p, w, t)
477
478 #if defined(MULTIPROCESSOR)
479 void proc_trampoline_mp(void); /* XXX */
480 #endif
481
482 int proc_isunder(struct proc *, struct proc *);
483
484 #endif /* _KERNEL */
485 #endif /* !_SYS_PROC_H_ */
486