Fix LWP support on exec. exec now properly kills all LWPs.
[dragonfly.git] / sys / sys / proc.h
1 /*-
2  * Copyright (c) 1986, 1989, 1991, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  * (c) UNIX System Laboratories, Inc.
5  * All or some portions of this file are derived from material licensed
6  * to the University of California by American Telephone and Telegraph
7  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8  * the permission of UNIX System Laboratories, Inc.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *      This product includes software developed by the University of
21  *      California, Berkeley and its contributors.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  *      @(#)proc.h      8.15 (Berkeley) 5/19/95
39  * $FreeBSD: src/sys/sys/proc.h,v 1.99.2.9 2003/06/06 20:21:32 tegge Exp $
40  * $DragonFly: src/sys/sys/proc.h,v 1.111 2007/07/12 21:56:20 dillon Exp $
41  */
42
43 #ifndef _SYS_PROC_H_
44 #define _SYS_PROC_H_
45
46 #if !defined(_KERNEL) && !defined(_KERNEL_STRUCTURES)
47
48 #error "Userland must include sys/user.h instead of sys/proc.h"
49
50 #else
51
52 #include <sys/callout.h>                /* For struct callout_handle. */
53 #include <sys/filedesc.h>
54 #include <sys/queue.h>
55 #include <sys/rtprio.h>                 /* For struct rtprio. */
56 #include <sys/signal.h>
57 #include <sys/lock.h>
58 #ifndef _KERNEL
59 #include <sys/time.h>                   /* For structs itimerval, timeval. */
60 #endif
61 #include <sys/ucred.h>
62 #include <sys/event.h>                  /* For struct klist */
63 #include <sys/thread.h>
64 #include <sys/varsym.h>
65 #include <sys/upcall.h>
66 #include <sys/resourcevar.h>
67 #ifdef _KERNEL
68 #include <sys/globaldata.h>
69 #endif
70 #include <sys/systimer.h>
71 #include <sys/usched.h>
72 #include <machine/proc.h>               /* Machine-dependent proc substruct. */
73 #include <sys/signalvar.h>
74
75 LIST_HEAD(proclist, proc);
76 LIST_HEAD(lwplist, lwp);
77
78 /*
79  * One structure allocated per session.
80  */
81 struct  session {
82         int     s_count;                /* Ref cnt; pgrps in session. */
83         struct  proc *s_leader;         /* Session leader. */
84         struct  vnode *s_ttyvp;         /* Vnode of controlling terminal. */
85         struct  tty *s_ttyp;            /* Controlling terminal. */
86         pid_t   s_sid;                  /* Session ID */
87         char    s_login[roundup(MAXLOGNAME, sizeof(long))];     /* Setlogin() name. */
88 };
89
90 /*
91  * One structure allocated per process group.
92  */
93 struct  pgrp {
94         LIST_ENTRY(pgrp) pg_hash;       /* Hash chain. */
95         struct proclist pg_members;     /* Pointer to pgrp members. */
96         struct  session *pg_session;    /* Pointer to session. */
97         struct  sigiolst pg_sigiolst;   /* List of sigio sources. */
98         pid_t   pg_id;                  /* Pgrp id. */
99         int     pg_jobc;        /* # procs qualifying pgrp for job control */
100         struct lock pg_lock;            /* Lock during fork */
101 };
102
103 #define PS_NOCLDWAIT    0x0001  /* No zombies if child dies */
104 #define PS_NOCLDSTOP    0x0002  /* No SIGCHLD when children stop. */
105
106 /*
107  * pargs, used to hold a copy of the command line, if it had a sane
108  * length
109  */
110 struct  pargs {
111         u_int   ar_ref;         /* Reference count */
112         u_int   ar_length;      /* Length */
113         u_char  ar_args[0];     /* Arguments */
114 };
115
116 /*
117  * Description of a process.
118  *
119  * This structure contains the information needed to manage a thread of
120  * control, known in UN*X as a process; it has references to substructures
121  * containing descriptions of things that the process uses, but may share
122  * with related processes.  The process structure and the substructures
123  * are always addressable except for those marked "(PROC ONLY)" below,
124  * which might be addressable only on a processor on which the process
125  * is running.
126  *
127  * NOTE!  The process start time is stored in the thread structure associated
128  * with the process.  If the process is a Zombie, then this field will be
129  * inaccessible due to the thread structure being free'd in kern_wait1().
130  */
131
132 struct jail;
133 struct vkernel_proc;
134 struct vkernel_lwp;
135 struct vmspace_entry;
136 struct ktrace_node;
137
138 enum lwpstat {
139         LSRUN = 1,
140         LSSTOP = 2,
141         LSSLEEP = 3,
142 };
143
144 enum procstat {
145         SIDL = 1,
146         SACTIVE = 2,
147         SSTOP = 3,
148         SZOMB = 4,
149 };
150
151 struct lwp {
152         TAILQ_ENTRY(lwp) lwp_procq;     /* run/sleep queue. */
153         LIST_ENTRY(lwp) lwp_list;       /* List of all threads in the proc. */
154
155         struct proc     *lwp_proc;      /* Link to our proc. */
156         struct vmspace  *lwp_vmspace;   /* Inherited from p_vmspace */
157         struct vkernel_lwp *lwp_vkernel;/* VKernel support, lwp part */
158
159         lwpid_t         lwp_tid;        /* Our thread id . */
160
161         int             lwp_flag;       /* LWP_* flags. */
162         enum lwpstat    lwp_stat;       /* LS* lwp status. */
163         int             lwp_lock;       /* lwp lock (prevent destruct) count */
164
165         int             lwp_dupfd;      /* Sideways return value from fdopen. XXX */
166
167         /*
168          * The following two fields are marked XXX since (at least) the
169          * 4.4BSD-Lite2 import.  I can only guess the reason:  It is ugly.
170          * These fields are used to pass the trap code from trapsignal() to
171          * postsig(), which gets called later from userret().
172          *
173          * The correct "fix" for these XXX is to convert our signal system
174          * to use signal queues, where each signal can carry its own meta
175          * data.
176          */
177         int             lwp_sig;        /* for core dump/debugger XXX */
178         u_long          lwp_code;       /* for core dump/debugger XXX */
179
180         /*
181          * Scheduling.
182          */
183         sysclock_t      lwp_cpticks;    /* cpu used in sched clock ticks */
184         sysclock_t      lwp_cpbase;     /* Measurement base */
185         fixpt_t         lwp_pctcpu;     /* %cpu for this process */
186         u_int           lwp_slptime;    /* Time since last blocked. */
187
188         int             lwp_traceflag;  /* Kernel trace points. */
189
190         struct rusage   lwp_ru;         /* stats for this lwp */
191
192         union usched_data lwp_usdata;   /* User scheduler specific */
193
194 #define lwp_startcopy   lwp_cpumask
195         cpumask_t       lwp_cpumask;
196         sigset_t        lwp_siglist;    /* Signals arrived but not delivered. */
197         sigset_t        lwp_oldsigmask; /* saved mask from before sigpause */
198         sigset_t        lwp_sigmask;    /* Current signal mask. */
199         stack_t         lwp_sigstk;     /* sp & on stack state variable */
200
201         struct rtprio   lwp_rtprio;     /* Realtime priority. */
202 #define lwp_endcopy     lwp_md
203
204         struct mdproc   lwp_md;         /* Any machine-dependent fields. */
205
206         struct thread   *lwp_thread;    /* backpointer to proc's thread */
207         struct upcall   *lwp_upcall;    /* REGISTERED USERLAND POINTER! */
208 };
209
210 struct  proc {
211         LIST_ENTRY(proc) p_list;        /* List of all processes. */
212
213         /* substructures: */
214         struct ucred    *p_ucred;       /* Process owner's identity. */
215         struct filedesc *p_fd;          /* Ptr to open files structure. */
216         struct filedesc_to_leader *p_fdtol; /* Ptr to tracking node XXX lwp */
217         struct plimit   *p_limit;       /* Process limits. */
218         struct pstats   *p_stats;
219         void            *p_pad0;
220         struct sigacts  *p_sigacts;
221 #define p_sigignore     p_sigacts->ps_sigignore
222 #define p_sigcatch      p_sigacts->ps_sigcatch
223 #define p_rlimit        p_limit->pl_rlimit
224
225         int             p_flag;         /* P_* flags. */
226         enum procstat   p_stat;         /* S* process status. */
227         char            p_pad1[3];
228
229         pid_t           p_pid;          /* Process identifier. */
230         LIST_ENTRY(proc) p_hash;        /* Hash chain. */
231         LIST_ENTRY(proc) p_pglist;      /* List of processes in pgrp. */
232         struct proc     *p_pptr;        /* Pointer to parent process. */
233         LIST_ENTRY(proc) p_sibling;     /* List of sibling processes. */
234         struct proclist p_children;     /* Pointer to list of children. */
235         struct callout  p_ithandle;     /* for scheduling p_realtimer */
236         struct varsymset p_varsymset;
237
238         pid_t           p_oppid;        /* Save parent pid during ptrace. XXX */
239
240         struct vmspace  *p_vmspace;     /* Current address space. */
241
242         unsigned int    p_swtime;       /* Time swapped in or out */
243
244         struct itimerval p_realtimer;   /* Alarm timer. */
245         struct itimerval p_timer[3];    /* Virtual-time timers. */
246
247         int             p_traceflag;    /* Kernel trace points. */
248         struct ktrace_node *p_tracenode; /* Trace to vnode. */
249
250         sigset_t        p_siglist;      /* Signals arrived but not delivered. */
251
252         struct vnode    *p_textvp;      /* Vnode of executable. */
253
254         unsigned int    p_stops;        /* procfs event bitmask */
255         unsigned int    p_stype;        /* procfs stop event type */
256         char            p_step;         /* procfs stop *once* flag */
257         unsigned char   p_pfsflags;     /* procfs flags */
258         char            p_pad2[2];      /* padding for alignment */
259         struct          sigiolst p_sigiolst;    /* list of sigio sources */
260         int             p_sigparent;    /* signal to parent on exit */
261         struct klist    p_klist;        /* knotes attached to this process */
262
263         struct timeval  p_start;        /* start time for a process */
264
265         struct rusage   p_ru;           /* stats for this proc */
266         struct rusage   p_cru;          /* sum of stats for reaped children */
267
268 /* The following fields are all copied upon creation in fork. */
269 #define p_startcopy     p_comm
270
271         char            p_comm[MAXCOMLEN+1]; /* typ 16+1 bytes */
272         char            p_lock;         /* Process lock (prevent destruct) count. */
273         char            p_nice;         /* Process "nice" value. */
274         char            p_pad3;
275
276         struct pgrp     *p_pgrp;        /* Pointer to process group. */
277
278         struct sysentvec *p_sysent;     /* System call dispatch information. */
279
280         struct uprof    p_prof;         /* Profiling arguments. */
281         struct rtprio   p_rtprio;       /* Realtime priority. */
282         struct pargs    *p_args;
283 /* End area that is copied on creation. */
284 #define p_endcopy       p_xstat
285
286         u_short         p_xstat;        /* Exit status or last stop signal */
287         u_short         p_acflag;       /* Accounting flags. */
288
289         int             p_nthreads;     /* Number of threads in this process. */
290         int             p_nstopped;     /* Number of stopped threads. */
291         int             p_lasttid;      /* Last tid used. */
292         struct lwplist  p_lwps; /* List of threads in this process. */
293         void            *p_aioinfo;     /* ASYNC I/O info */
294         int             p_wakeup;       /* thread id XXX lwp */
295         struct proc     *p_peers;       /* XXX lwp */
296         struct proc     *p_leader;      /* XXX lwp */
297         void            *p_emuldata;    /* process-specific emulator state */
298         struct usched   *p_usched;      /* Userland scheduling control */
299         struct vkernel_proc *p_vkernel; /* VKernel support, proc part */
300         int             p_numposixlocks; /* number of POSIX locks */
301
302         struct spinlock p_spin;         /* Spinlock for LWP access to proc */
303 };
304
305 #define lwp_wchan       lwp_thread->td_wchan
306 #define lwp_wmesg       lwp_thread->td_wmesg
307 #define p_session       p_pgrp->pg_session
308 #define p_pgid          p_pgrp->pg_id
309
310 /* These flags are kept in p_flags. */
311 #define P_ADVLOCK       0x00001 /* Process may hold a POSIX advisory lock. */
312 #define P_CONTROLT      0x00002 /* Has a controlling terminal. */
313 #define P_SWAPPEDOUT    0x00004 /* Swapped out of memory */
314 #define P_UNUSED3       0x00008 /* was: Event pending, break tsleep on sigcont */
315 #define P_PPWAIT        0x00010 /* Parent is waiting for child to exec/exit. */
316 #define P_PROFIL        0x00020 /* Has started profiling. */
317 #define P_UNUSED5       0x00040 /* was: Selecting; wakeup/waiting danger. */
318 #define P_UNUSED4       0x00080 /* was: Sleep is interruptible. */
319 #define P_SUGID         0x00100 /* Had set id privileges since last exec. */
320 #define P_SYSTEM        0x00200 /* System proc: no sigs, stats or swapping. */
321 #define P_UNUSED2       0x00400 /* was: SIGSTOP status */
322 #define P_TRACED        0x00800 /* Debugged process being traced. */
323 #define P_WAITED        0x01000 /* SIGSTOP status was returned by wait3/4 */
324 #define P_WEXIT         0x02000 /* Working on exiting (master exit) */
325 #define P_EXEC          0x04000 /* Process called exec. */
326
327 /* Should probably be changed into a hold count. */
328 /* was  P_NOSWAP        0x08000 was: Do not swap upages; p->p_hold */
329 #define P_MAILBOX       0x10000 /* Possible mailbox signal pending */
330
331 #define P_UPCALLPEND    0x20000 /* an upcall is pending */
332
333 #define P_SWAPWAIT      0x40000 /* Waiting for a swapin */
334 #define P_UNUSED6       0x80000 /* was: Now in a zombied state */
335
336 /* Marked a kernel thread */
337 #define P_UNUSED07      0x100000 /* was: on a user scheduling run queue */
338 #define P_KTHREADP      0x200000 /* Process is really a kernel thread */
339 #define P_IDLESWAP      0x400000 /* Swapout was due to idleswap, not load */
340 #define P_DEADLKTREAT   0x800000 /* lock aquisition - deadlock treatment */
341
342 #define P_JAILED        0x1000000 /* Process is in jail */
343 #define P_UNUSED0       0x2000000 /* need to restore mask before pause */
344 #define P_UNUSED1       0x4000000 /* have alternate signal stack */
345 #define P_INEXEC        0x8000000 /* Process is in execve(). */
346 #define P_PASSIVE_ACQ   0x10000000 /* Passive acquire cpu (see kern_switch) */
347 #define P_UPCALLWAIT    0x20000000 /* Wait for upcall or signal */
348 #define P_XCPU          0x40000000 /* SIGXCPU */
349
350 #define LWP_ALTSTACK    0x0000001 /* have alternate signal stack */
351 #define LWP_OLDMASK     0x0000002 /* need to restore mask before pause */
352 #define LWP_BREAKTSLEEP 0x0000004 /* Event pending, break tsleep on sigcont */
353 #define LWP_SINTR       0x0000008 /* Sleep is interruptible. */
354 #define LWP_SELECT      0x0000010 /* Selecting; wakeup/waiting danger. */
355 #define LWP_ONRUNQ      0x0000020 /* on a user scheduling run queue */
356 #define LWP_WEXIT       0x0000040 /* working on exiting */
357 #define LWP_WSTOP       0x0000080 /* working on stopping */
358
359 #define FIRST_LWP_IN_PROC(p)            LIST_FIRST(&(p)->p_lwps)
360 #define FOREACH_LWP_IN_PROC(lp, p)      \
361         LIST_FOREACH((lp), &(p)->p_lwps, lwp_list)
362 #define ONLY_LWP_IN_PROC(p)             \
363         (p->p_nthreads != 1 &&          \
364         (panic("%s: proc %p (pid %d cmd %s) has more than one thread",  \
365                __func__, p, p->p_pid, p->p_comm), 1),   \
366         FIRST_LWP_IN_PROC(p))
367
368 /*
369  * We use process IDs <= PID_MAX; PID_MAX + 1 must also fit in a pid_t,
370  * as it is used to represent "no process group".
371  */
372 #define PID_MAX         99999
373 #define NO_PID          100000
374
375 #define SESS_LEADER(p)  ((p)->p_session->s_leader == (p))
376
377 #ifdef _KERNEL
378
379 #ifdef MALLOC_DECLARE
380 MALLOC_DECLARE(M_SESSION);
381 MALLOC_DECLARE(M_SUBPROC);
382 MALLOC_DECLARE(M_PARGS);
383 #endif
384
385 /* flags for suser_xxx() */
386 #define PRISON_ROOT     0x1
387 #define NULL_CRED_OKAY  0x2
388
389 /* Handy macro to determine if p1 can mangle p2 */
390
391 #define PRISON_CHECK(cr1, cr2) \
392         ((!(cr1)->cr_prison) || (cr1)->cr_prison == (cr2)->cr_prison)
393
394 /*
395  * STOPEVENT
396  */
397 extern void stopevent(struct proc*, unsigned int, unsigned int);
398 #define STOPEVENT(p,e,v)                        \
399         do {                                    \
400                 if ((p)->p_stops & (e)) {       \
401                         stopevent(p,e,v);       \
402                 }                               \
403         } while (0)
404
405 /* hold process in memory, don't destruct , normally for ptrace/procfs work */
406 #define PHOLD(p)        (++(p)->p_lock)
407 #define PRELE(p)        (--(p)->p_lock)
408
409 /* hold lwp in memory, don't destruct , normally for ptrace/procfs work */
410 #define LWPHOLD(lp)     (++(lp)->lwp_lock)
411 #define LWPRELE(lp)     (--(lp)->lwp_lock)
412
413 #define PIDHASH(pid)    (&pidhashtbl[(pid) & pidhash])
414 extern LIST_HEAD(pidhashhead, proc) *pidhashtbl;
415 extern u_long pidhash;
416
417 #define PGRPHASH(pgid)  (&pgrphashtbl[(pgid) & pgrphash])
418 extern LIST_HEAD(pgrphashhead, pgrp) *pgrphashtbl;
419 extern u_long pgrphash;
420
421 #if 0 
422 #ifndef SET_CURPROC
423 #define SET_CURPROC(p)  (curproc = (p))
424 #endif
425 #endif
426
427 extern struct proc proc0;               /* Process slot for swapper. */
428 extern struct lwp lwp0;                 /* LWP slot for swapper. */
429 extern struct thread thread0;           /* Thread slot for swapper. */
430 extern int hogticks;                    /* Limit on kernel cpu hogs. */
431 extern int nprocs, maxproc;             /* Current and max number of procs. */
432 extern int maxprocperuid;               /* Max procs per uid. */
433 extern int sched_quantum;               /* Scheduling quantum in ticks */
434
435 extern struct proclist allproc;         /* List of all processes. */
436 extern struct proclist zombproc;        /* List of zombie processes. */
437 extern struct proc *initproc;           /* Process slot for init */
438 extern struct thread *pagethread, *updatethread;
439
440 /*
441  * Scheduler independant variables.  The primary scheduler polling frequency,
442  * the maximum ESTCPU value, and the weighting factor for nice values.  A
443  * cpu bound program's estcpu will increase to ESTCPUMAX - 1.
444  */
445 #define ESTCPUFREQ      50
446
447 extern  u_long ps_arg_cache_limit;
448 extern  int ps_argsopen;
449 extern  int ps_showallprocs;
450
451 struct proc *pfind (pid_t);     /* Find process by id. */
452 struct pgrp *pgfind (pid_t);    /* Find process group by id. */
453 struct proc *zpfind (pid_t);    /* Find zombie process by id. */
454
455 struct vm_zone;
456 struct globaldata;
457 struct lwp_params;
458 extern struct vm_zone *proc_zone;
459 extern struct vm_zone *lwp_zone;
460
461 int     enterpgrp (struct proc *p, pid_t pgid, int mksess);
462 void    proc_add_allproc(struct proc *p);
463 void    proc_move_allproc_zombie(struct proc *);
464 void    proc_remove_zombie(struct proc *);
465 void    allproc_scan(int (*callback)(struct proc *, void *), void *data);
466 void    alllwp_scan(int (*callback)(struct lwp *, void *), void *data);
467 void    zombproc_scan(int (*callback)(struct proc *, void *), void *data);
468 void    fixjobc (struct proc *p, struct pgrp *pgrp, int entering);
469 void    updatepcpu(struct lwp *, int, int);
470 int     inferior (struct proc *p);
471 int     leavepgrp (struct proc *p);
472 void    sess_hold(struct session *sp);
473 void    sess_rele(struct session *sp);
474 void    procinit (void);
475 void    relscurproc(struct proc *curp);
476 int     p_trespass (struct ucred *cr1, struct ucred *cr2);
477 void    setrunnable (struct lwp *);
478 void    proc_stop (struct proc *);
479 void    proc_unstop (struct proc *);
480 void    sleep_gdinit (struct globaldata *);
481 int     suser (struct thread *td);
482 int     suser_cred (struct ucred *cred, int flag);
483 void    cpu_heavy_switch (struct thread *);
484 void    cpu_lwkt_switch (struct thread *);
485
486 void    cpu_lwp_exit (void) __dead2;
487 void    cpu_thread_exit (void) __dead2;
488 void    lwp_exit (int masterexit) __dead2;
489 void    lwp_dispose (struct lwp *);
490 int     killalllwps (int);
491 void    exit1 (int) __dead2;
492 void    cpu_fork (struct lwp *, struct lwp *, int);
493 int     cpu_prepare_lwp(struct lwp *, struct lwp_params *);
494 void    cpu_set_fork_handler (struct lwp *, void (*)(void *, struct trapframe *), void *);
495 void    cpu_set_thread_handler(struct thread *td, void (*retfunc)(void), void *func, void *arg);
496 int     fork1 (struct lwp *, int, struct proc **);
497 void    start_forked_proc (struct lwp *, struct proc *);
498 int     trace_req (struct proc *);
499 void    cpu_proc_wait (struct proc *);
500 void    cpu_thread_wait (struct thread *);
501 int     cpu_coredump (struct thread *, struct vnode *, struct ucred *);
502 void    setsugid (void);
503 void    faultin (struct proc *p);
504 void    swapin_request (void);
505
506 u_int32_t       procrunnable (void);
507
508 #endif  /* _KERNEL */
509
510 #endif  /* _KERNEL || _KERNEL_STRUCTURES */
511 #endif  /* !_SYS_PROC_H_ */