Do not build bus modules for vkernels.
[dragonfly.git] / sys / kern / kern_proc.c
CommitLineData
984263bc
MD
1/*
2 * Copyright (c) 1982, 1986, 1989, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * @(#)kern_proc.c 8.7 (Berkeley) 2/14/95
34 * $FreeBSD: src/sys/kern/kern_proc.c,v 1.63.2.9 2003/05/08 07:47:16 kbyanc Exp $
fde7ac71 35 * $DragonFly: src/sys/kern/kern_proc.c,v 1.31 2007/01/01 22:51:17 corecode Exp $
984263bc
MD
36 */
37
38#include <sys/param.h>
39#include <sys/systm.h>
40#include <sys/kernel.h>
41#include <sys/sysctl.h>
42#include <sys/malloc.h>
43#include <sys/proc.h>
56c703bd 44#include <sys/jail.h>
984263bc
MD
45#include <sys/filedesc.h>
46#include <sys/tty.h>
47#include <sys/signalvar.h>
5bf0d9b5 48#include <sys/spinlock.h>
984263bc
MD
49#include <vm/vm.h>
50#include <sys/lock.h>
51#include <vm/pmap.h>
52#include <vm/vm_map.h>
53#include <sys/user.h>
54#include <vm/vm_zone.h>
630ccdeb 55#include <machine/smp.h>
984263bc 56
5bf0d9b5
MD
57#include <sys/spinlock2.h>
58
984263bc
MD
59static MALLOC_DEFINE(M_PGRP, "pgrp", "process group header");
60MALLOC_DEFINE(M_SESSION, "session", "session header");
61static MALLOC_DEFINE(M_PROC, "proc", "Proc structures");
62MALLOC_DEFINE(M_SUBPROC, "subproc", "Proc sub-structures");
63
28c57d20 64int ps_showallprocs = 1;
73e4f7b9 65static int ps_showallthreads = 1;
984263bc
MD
66SYSCTL_INT(_kern, OID_AUTO, ps_showallprocs, CTLFLAG_RW,
67 &ps_showallprocs, 0, "");
73e4f7b9
MD
68SYSCTL_INT(_kern, OID_AUTO, ps_showallthreads, CTLFLAG_RW,
69 &ps_showallthreads, 0, "");
984263bc 70
51e64ff2
MD
71static void pgdelete(struct pgrp *);
72static void orphanpg(struct pgrp *pg);
73static pid_t proc_getnewpid_locked(int random_offset);
984263bc
MD
74
75/*
76 * Other process lists
77 */
78struct pidhashhead *pidhashtbl;
79u_long pidhash;
80struct pgrphashhead *pgrphashtbl;
81u_long pgrphash;
82struct proclist allproc;
83struct proclist zombproc;
5bf0d9b5 84struct spinlock allproc_spin;
984263bc 85vm_zone_t proc_zone;
263e4574 86vm_zone_t thread_zone;
984263bc 87
51e64ff2
MD
88/*
89 * Random component to nextpid generation. We mix in a random factor to make
90 * it a little harder to predict. We sanity check the modulus value to avoid
91 * doing it in critical paths. Don't let it be too small or we pointlessly
92 * waste randomness entropy, and don't let it be impossibly large. Using a
93 * modulus that is too big causes a LOT more process table scans and slows
94 * down fork processing as the pidchecked caching is defeated.
95 */
96static int randompid = 0;
97
98static int
99sysctl_kern_randompid(SYSCTL_HANDLER_ARGS)
100{
101 int error, pid;
102
103 pid = randompid;
104 error = sysctl_handle_int(oidp, &pid, 0, req);
105 if (error || !req->newptr)
106 return (error);
107 if (pid < 0 || pid > PID_MAX - 100) /* out of range */
108 pid = PID_MAX - 100;
109 else if (pid < 2) /* NOP */
110 pid = 0;
111 else if (pid < 100) /* Make it reasonable */
112 pid = 100;
113 randompid = pid;
114 return (error);
115}
116
117SYSCTL_PROC(_kern, OID_AUTO, randompid, CTLTYPE_INT|CTLFLAG_RW,
118 0, 0, sysctl_kern_randompid, "I", "Random PID modulus");
119
984263bc
MD
120/*
121 * Initialize global process hashing structures.
122 */
123void
77153250 124procinit(void)
984263bc 125{
984263bc
MD
126 LIST_INIT(&allproc);
127 LIST_INIT(&zombproc);
5bf0d9b5 128 spin_init(&allproc_spin);
984263bc
MD
129 pidhashtbl = hashinit(maxproc / 4, M_PROC, &pidhash);
130 pgrphashtbl = hashinit(maxproc / 4, M_PROC, &pgrphash);
131 proc_zone = zinit("PROC", sizeof (struct proc), 0, 0, 5);
263e4574 132 thread_zone = zinit("THREAD", sizeof (struct thread), 0, 0, 5);
984263bc
MD
133 uihashinit();
134}
135
136/*
137 * Is p an inferior of the current process?
138 */
139int
77153250 140inferior(struct proc *p)
984263bc 141{
984263bc
MD
142 for (; p != curproc; p = p->p_pptr)
143 if (p->p_pid == 0)
144 return (0);
145 return (1);
146}
147
148/*
149 * Locate a process by number
150 */
151struct proc *
77153250 152pfind(pid_t pid)
984263bc 153{
1fd87d54 154 struct proc *p;
984263bc 155
5bf0d9b5 156 LIST_FOREACH(p, PIDHASH(pid), p_hash) {
984263bc
MD
157 if (p->p_pid == pid)
158 return (p);
5bf0d9b5 159 }
984263bc
MD
160 return (NULL);
161}
162
163/*
164 * Locate a process group by number
165 */
166struct pgrp *
77153250 167pgfind(pid_t pgid)
984263bc 168{
1fd87d54 169 struct pgrp *pgrp;
984263bc 170
5bf0d9b5 171 LIST_FOREACH(pgrp, PGRPHASH(pgid), pg_hash) {
984263bc
MD
172 if (pgrp->pg_id == pgid)
173 return (pgrp);
5bf0d9b5 174 }
984263bc
MD
175 return (NULL);
176}
177
178/*
179 * Move p to a new or existing process group (and session)
180 */
181int
77153250 182enterpgrp(struct proc *p, pid_t pgid, int mksess)
984263bc 183{
1fd87d54 184 struct pgrp *pgrp = pgfind(pgid);
984263bc
MD
185
186 KASSERT(pgrp == NULL || !mksess,
187 ("enterpgrp: setsid into non-empty pgrp"));
188 KASSERT(!SESS_LEADER(p),
189 ("enterpgrp: session leader attempted setpgrp"));
190
191 if (pgrp == NULL) {
192 pid_t savepid = p->p_pid;
193 struct proc *np;
194 /*
195 * new process group
196 */
197 KASSERT(p->p_pid == pgid,
198 ("enterpgrp: new pgrp and pid != pgid"));
199 if ((np = pfind(savepid)) == NULL || np != p)
200 return (ESRCH);
201 MALLOC(pgrp, struct pgrp *, sizeof(struct pgrp), M_PGRP,
202 M_WAITOK);
203 if (mksess) {
1fd87d54 204 struct session *sess;
984263bc
MD
205
206 /*
207 * new session
208 */
209 MALLOC(sess, struct session *, sizeof(struct session),
210 M_SESSION, M_WAITOK);
211 sess->s_leader = p;
212 sess->s_sid = p->p_pid;
213 sess->s_count = 1;
214 sess->s_ttyvp = NULL;
215 sess->s_ttyp = NULL;
216 bcopy(p->p_session->s_login, sess->s_login,
217 sizeof(sess->s_login));
218 p->p_flag &= ~P_CONTROLT;
219 pgrp->pg_session = sess;
220 KASSERT(p == curproc,
221 ("enterpgrp: mksession and p != curproc"));
222 } else {
223 pgrp->pg_session = p->p_session;
8b90699b 224 sess_hold(pgrp->pg_session);
984263bc
MD
225 }
226 pgrp->pg_id = pgid;
227 LIST_INIT(&pgrp->pg_members);
228 LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash);
229 pgrp->pg_jobc = 0;
230 SLIST_INIT(&pgrp->pg_sigiolst);
167e6ecb 231 lockinit(&pgrp->pg_lock, "pgwt", 0, 0);
984263bc
MD
232 } else if (pgrp == p->p_pgrp)
233 return (0);
234
235 /*
236 * Adjust eligibility of affected pgrps to participate in job control.
237 * Increment eligibility counts before decrementing, otherwise we
238 * could reach 0 spuriously during the first call.
239 */
240 fixjobc(p, pgrp, 1);
241 fixjobc(p, p->p_pgrp, 0);
242
243 LIST_REMOVE(p, p_pglist);
244 if (LIST_EMPTY(&p->p_pgrp->pg_members))
245 pgdelete(p->p_pgrp);
246 p->p_pgrp = pgrp;
247 LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist);
248 return (0);
249}
250
251/*
252 * remove process from process group
253 */
254int
77153250 255leavepgrp(struct proc *p)
984263bc
MD
256{
257
258 LIST_REMOVE(p, p_pglist);
259 if (LIST_EMPTY(&p->p_pgrp->pg_members))
260 pgdelete(p->p_pgrp);
261 p->p_pgrp = 0;
262 return (0);
263}
264
265/*
266 * delete a process group
267 */
268static void
77153250 269pgdelete(struct pgrp *pgrp)
984263bc
MD
270{
271
272 /*
273 * Reset any sigio structures pointing to us as a result of
274 * F_SETOWN with our pgid.
275 */
276 funsetownlst(&pgrp->pg_sigiolst);
277
278 if (pgrp->pg_session->s_ttyp != NULL &&
279 pgrp->pg_session->s_ttyp->t_pgrp == pgrp)
280 pgrp->pg_session->s_ttyp->t_pgrp = NULL;
281 LIST_REMOVE(pgrp, pg_hash);
8b90699b 282 sess_rele(pgrp->pg_session);
efda3bd0 283 kfree(pgrp, M_PGRP);
8b90699b
MD
284}
285
286/*
287 * Adjust the ref count on a session structure. When the ref count falls to
288 * zero the tty is disassociated from the session and the session structure
289 * is freed. Note that tty assocation is not itself ref-counted.
290 */
291void
292sess_hold(struct session *sp)
293{
294 ++sp->s_count;
295}
296
297void
298sess_rele(struct session *sp)
299{
300 KKASSERT(sp->s_count > 0);
301 if (--sp->s_count == 0) {
302 if (sp->s_ttyp && sp->s_ttyp->t_session) {
303#ifdef TTY_DO_FULL_CLOSE
304 /* FULL CLOSE, see ttyclearsession() */
305 KKASSERT(sp->s_ttyp->t_session == sp);
306 sp->s_ttyp->t_session = NULL;
307#else
308 /* HALF CLOSE, see ttyclearsession() */
309 if (sp->s_ttyp->t_session == sp)
310 sp->s_ttyp->t_session = NULL;
311#endif
312 }
efda3bd0 313 kfree(sp, M_SESSION);
8b90699b 314 }
984263bc
MD
315}
316
317/*
318 * Adjust pgrp jobc counters when specified process changes process group.
319 * We count the number of processes in each process group that "qualify"
320 * the group for terminal job control (those with a parent in a different
321 * process group of the same session). If that count reaches zero, the
322 * process group becomes orphaned. Check both the specified process'
323 * process group and that of its children.
324 * entering == 0 => p is leaving specified group.
325 * entering == 1 => p is entering specified group.
326 */
327void
77153250 328fixjobc(struct proc *p, struct pgrp *pgrp, int entering)
984263bc 329{
1fd87d54
RG
330 struct pgrp *hispgrp;
331 struct session *mysession = pgrp->pg_session;
984263bc
MD
332
333 /*
334 * Check p's parent to see whether p qualifies its own process
335 * group; if so, adjust count for p's process group.
336 */
337 if ((hispgrp = p->p_pptr->p_pgrp) != pgrp &&
338 hispgrp->pg_session == mysession) {
339 if (entering)
340 pgrp->pg_jobc++;
341 else if (--pgrp->pg_jobc == 0)
342 orphanpg(pgrp);
343 }
344
345 /*
346 * Check this process' children to see whether they qualify
347 * their process groups; if so, adjust counts for children's
348 * process groups.
349 */
350 LIST_FOREACH(p, &p->p_children, p_sibling)
351 if ((hispgrp = p->p_pgrp) != pgrp &&
352 hispgrp->pg_session == mysession &&
f026f3b0 353 (p->p_flag & P_ZOMBIE) == 0) {
984263bc
MD
354 if (entering)
355 hispgrp->pg_jobc++;
356 else if (--hispgrp->pg_jobc == 0)
357 orphanpg(hispgrp);
358 }
359}
360
361/*
362 * A process group has become orphaned;
363 * if there are any stopped processes in the group,
364 * hang-up all process in that group.
365 */
366static void
77153250 367orphanpg(struct pgrp *pg)
984263bc 368{
1fd87d54 369 struct proc *p;
984263bc
MD
370
371 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
344ad853 372 if (p->p_flag & P_STOPPED) {
984263bc 373 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
84204577
MD
374 ksignal(p, SIGHUP);
375 ksignal(p, SIGCONT);
984263bc
MD
376 }
377 return;
378 }
379 }
380}
381
51e64ff2
MD
382/*
383 * Add a new process to the allproc list and the PID hash. This
384 * also assigns a pid to the new process.
385 *
0ced1954 386 * MPALMOSTSAFE - acquires mplock for karc4random() call
51e64ff2
MD
387 */
388void
389proc_add_allproc(struct proc *p)
390{
391 int random_offset;
392
393 if ((random_offset = randompid) != 0) {
394 get_mplock();
0ced1954 395 random_offset = karc4random() % random_offset;
51e64ff2
MD
396 rel_mplock();
397 }
398
399 spin_lock_wr(&allproc_spin);
400 p->p_pid = proc_getnewpid_locked(random_offset);
401 LIST_INSERT_HEAD(&allproc, p, p_list);
402 LIST_INSERT_HEAD(PIDHASH(p->p_pid), p, p_hash);
403 spin_unlock_wr(&allproc_spin);
404}
405
406/*
407 * Calculate a new process pid. This function is integrated into
408 * proc_add_allproc() to guarentee that the new pid is not reused before
409 * the new process can be added to the allproc list.
410 *
411 * MPSAFE - must be called with allproc_spin held.
412 */
413static
414pid_t
415proc_getnewpid_locked(int random_offset)
416{
417 static pid_t nextpid;
418 static pid_t pidchecked;
419 struct proc *p;
420
421 /*
422 * Find an unused process ID. We remember a range of unused IDs
423 * ready to use (from nextpid+1 through pidchecked-1).
424 */
425 nextpid = nextpid + 1 + random_offset;
426retry:
427 /*
428 * If the process ID prototype has wrapped around,
429 * restart somewhat above 0, as the low-numbered procs
430 * tend to include daemons that don't exit.
431 */
432 if (nextpid >= PID_MAX) {
433 nextpid = nextpid % PID_MAX;
434 if (nextpid < 100)
435 nextpid += 100;
436 pidchecked = 0;
437 }
438 if (nextpid >= pidchecked) {
439 int doingzomb = 0;
440
441 pidchecked = PID_MAX;
442 /*
443 * Scan the active and zombie procs to check whether this pid
444 * is in use. Remember the lowest pid that's greater
445 * than nextpid, so we can avoid checking for a while.
446 */
447 p = LIST_FIRST(&allproc);
448again:
449 for (; p != 0; p = LIST_NEXT(p, p_list)) {
450 while (p->p_pid == nextpid ||
451 p->p_pgrp->pg_id == nextpid ||
452 p->p_session->s_sid == nextpid) {
453 nextpid++;
454 if (nextpid >= pidchecked)
455 goto retry;
456 }
457 if (p->p_pid > nextpid && pidchecked > p->p_pid)
458 pidchecked = p->p_pid;
459 if (p->p_pgrp->pg_id > nextpid &&
460 pidchecked > p->p_pgrp->pg_id)
461 pidchecked = p->p_pgrp->pg_id;
462 if (p->p_session->s_sid > nextpid &&
463 pidchecked > p->p_session->s_sid)
464 pidchecked = p->p_session->s_sid;
465 }
466 if (!doingzomb) {
467 doingzomb = 1;
468 p = LIST_FIRST(&zombproc);
469 goto again;
470 }
471 }
472 return(nextpid);
473}
474
5bf0d9b5
MD
475/*
476 * Called from exit1 to remove a process from the allproc
477 * list and move it to the zombie list.
478 *
479 * MPSAFE
480 */
481void
482proc_move_allproc_zombie(struct proc *p)
483{
484 spin_lock_wr(&allproc_spin);
485 while (p->p_lock) {
486 spin_unlock_wr(&allproc_spin);
487 tsleep(p, 0, "reap1", hz / 10);
488 spin_lock_wr(&allproc_spin);
489 }
490 LIST_REMOVE(p, p_list);
491 LIST_INSERT_HEAD(&zombproc, p, p_list);
492 LIST_REMOVE(p, p_hash);
493 p->p_flag |= P_ZOMBIE;
494 spin_unlock_wr(&allproc_spin);
495}
496
497/*
498 * This routine is called from kern_wait() and will remove the process
499 * from the zombie list and the sibling list. This routine will block
500 * if someone has a lock on the proces (p_lock).
501 *
502 * MPSAFE
503 */
504void
505proc_remove_zombie(struct proc *p)
506{
507 spin_lock_wr(&allproc_spin);
508 while (p->p_lock) {
509 spin_unlock_wr(&allproc_spin);
510 tsleep(p, 0, "reap1", hz / 10);
511 spin_lock_wr(&allproc_spin);
512 }
513 LIST_REMOVE(p, p_list); /* off zombproc */
514 LIST_REMOVE(p, p_sibling);
515 spin_unlock_wr(&allproc_spin);
516}
517
518/*
519 * Scan all processes on the allproc list. The process is automatically
520 * held for the callback. A return value of -1 terminates the loop.
521 *
522 * MPSAFE
523 */
524void
525allproc_scan(int (*callback)(struct proc *, void *), void *data)
526{
527 struct proc *p;
528 int r;
529
530 spin_lock_rd(&allproc_spin);
531 LIST_FOREACH(p, &allproc, p_list) {
532 PHOLD(p);
533 spin_unlock_rd(&allproc_spin);
534 r = callback(p, data);
535 spin_lock_rd(&allproc_spin);
536 PRELE(p);
537 if (r < 0)
538 break;
539 }
540 spin_unlock_rd(&allproc_spin);
541}
542
543/*
544 * Scan all processes on the zombproc list. The process is automatically
545 * held for the callback. A return value of -1 terminates the loop.
546 *
547 * MPSAFE
548 */
549void
550zombproc_scan(int (*callback)(struct proc *, void *), void *data)
551{
552 struct proc *p;
553 int r;
554
555 spin_lock_rd(&allproc_spin);
556 LIST_FOREACH(p, &zombproc, p_list) {
557 PHOLD(p);
558 spin_unlock_rd(&allproc_spin);
559 r = callback(p, data);
560 spin_lock_rd(&allproc_spin);
561 PRELE(p);
562 if (r < 0)
563 break;
564 }
565 spin_unlock_rd(&allproc_spin);
566}
567
984263bc
MD
568#include "opt_ddb.h"
569#ifdef DDB
570#include <ddb/ddb.h>
571
572DB_SHOW_COMMAND(pgrpdump, pgrpdump)
573{
1fd87d54
RG
574 struct pgrp *pgrp;
575 struct proc *p;
576 int i;
984263bc
MD
577
578 for (i = 0; i <= pgrphash; i++) {
579 if (!LIST_EMPTY(&pgrphashtbl[i])) {
6ea70f76 580 kprintf("\tindx %d\n", i);
984263bc 581 LIST_FOREACH(pgrp, &pgrphashtbl[i], pg_hash) {
6ea70f76 582 kprintf(
984263bc
MD
583 "\tpgrp %p, pgid %ld, sess %p, sesscnt %d, mem %p\n",
584 (void *)pgrp, (long)pgrp->pg_id,
585 (void *)pgrp->pg_session,
586 pgrp->pg_session->s_count,
587 (void *)LIST_FIRST(&pgrp->pg_members));
588 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
6ea70f76 589 kprintf("\t\tpid %ld addr %p pgrp %p\n",
984263bc
MD
590 (long)p->p_pid, (void *)p,
591 (void *)p->p_pgrp);
592 }
593 }
594 }
595 }
596}
597#endif /* DDB */
598
73e4f7b9
MD
599/*
600 * Fill in an eproc structure for the specified thread.
601 */
602void
603fill_eproc_td(thread_t td, struct eproc *ep, struct proc *xp)
604{
605 bzero(ep, sizeof(*ep));
606
607 ep->e_uticks = td->td_uticks;
608 ep->e_sticks = td->td_sticks;
609 ep->e_iticks = td->td_iticks;
610 ep->e_tdev = NOUDEV;
a72187e9 611 ep->e_cpuid = td->td_gd->gd_cpuid;
73e4f7b9
MD
612 if (td->td_wmesg) {
613 strncpy(ep->e_wmesg, td->td_wmesg, WMESGLEN);
614 ep->e_wmesg[WMESGLEN] = 0;
615 }
616
617 /*
618 * Fake up portions of the proc structure copied out by the sysctl
619 * to return useful information. Note that using td_pri directly
620 * is messy because it includes critial section data so we fake
621 * up an rtprio.prio for threads.
622 */
623 if (xp) {
624 *xp = *initproc;
625 xp->p_rtprio.type = RTP_PRIO_THREAD;
626 xp->p_rtprio.prio = td->td_pri & TDPRI_MASK;
627 xp->p_pid = -1;
628 }
629}
630
984263bc
MD
631/*
632 * Fill in an eproc structure for the specified process.
633 */
634void
73e4f7b9 635fill_eproc(struct proc *p, struct eproc *ep)
984263bc 636{
73e4f7b9 637 struct tty *tp;
984263bc 638
73e4f7b9 639 fill_eproc_td(p->p_thread, ep, NULL);
984263bc
MD
640
641 ep->e_paddr = p;
41c20dac
MD
642 if (p->p_ucred) {
643 ep->e_ucred = *p->p_ucred;
984263bc
MD
644 }
645 if (p->p_procsig) {
646 ep->e_procsig = *p->p_procsig;
647 }
f026f3b0
MD
648 if (p->p_stat != SIDL && (p->p_flag & P_ZOMBIE) == 0 &&
649 p->p_vmspace != NULL) {
1fd87d54 650 struct vmspace *vm = p->p_vmspace;
984263bc
MD
651 ep->e_vm = *vm;
652 ep->e_vm.vm_rssize = vmspace_resident_count(vm); /*XXX*/
653 }
984263bc
MD
654 if (p->p_pptr)
655 ep->e_ppid = p->p_pptr->p_pid;
656 if (p->p_pgrp) {
657 ep->e_pgid = p->p_pgrp->pg_id;
658 ep->e_jobc = p->p_pgrp->pg_jobc;
659 ep->e_sess = p->p_pgrp->pg_session;
660
661 if (ep->e_sess) {
662 bcopy(ep->e_sess->s_login, ep->e_login, sizeof(ep->e_login));
663 if (ep->e_sess->s_ttyvp)
664 ep->e_flag = EPROC_CTTY;
665 if (p->p_session && SESS_LEADER(p))
666 ep->e_flag |= EPROC_SLEADER;
667 }
668 }
669 if ((p->p_flag & P_CONTROLT) &&
670 (ep->e_sess != NULL) &&
671 ((tp = ep->e_sess->s_ttyp) != NULL)) {
672 ep->e_tdev = dev2udev(tp->t_dev);
673 ep->e_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID;
674 ep->e_tsess = tp->t_session;
73e4f7b9 675 } else {
984263bc 676 ep->e_tdev = NOUDEV;
984263bc 677 }
35048f4d
JS
678 if (p->p_ucred->cr_prison)
679 ep->e_jailid = p->p_ucred->cr_prison->pr_id;
984263bc
MD
680}
681
5bf0d9b5
MD
682/*
683 * Locate a process on the zombie list. Return a held process or NULL.
684 */
984263bc
MD
685struct proc *
686zpfind(pid_t pid)
687{
688 struct proc *p;
689
690 LIST_FOREACH(p, &zombproc, p_list)
691 if (p->p_pid == pid)
692 return (p);
693 return (NULL);
694}
695
984263bc 696static int
73e4f7b9 697sysctl_out_proc(struct proc *p, struct thread *td, struct sysctl_req *req, int doingzomb)
984263bc
MD
698{
699 struct eproc eproc;
73e4f7b9 700 struct proc xproc;
984263bc 701 int error;
73e4f7b9 702#if 0
984263bc 703 pid_t pid = p->p_pid;
73e4f7b9
MD
704#endif
705
706 if (p) {
707 td = p->p_thread;
708 fill_eproc(p, &eproc);
709 xproc = *p;
d9d6cb99 710
fde7ac71
SS
711 /*
712 * Aggregate rusage information
713 */
714 calcru_proc(p, &xproc.p_ru);
715
d9d6cb99 716 /*
344ad853
MD
717 * p_stat fixup. If we are in a thread sleep mark p_stat
718 * as sleeping if the thread is blocked.
d9d6cb99 719 */
8ec60c3f 720 if (p->p_stat == SRUN && td && (td->td_flags & TDF_BLOCKED)) {
d9d6cb99
MD
721 xproc.p_stat = SSLEEP;
722 }
344ad853
MD
723 /*
724 * If the process is being stopped but is in a normal tsleep,
725 * mark it as being SSTOP.
726 */
727 if (p->p_stat == SSLEEP && (p->p_flag & P_STOPPED))
728 xproc.p_stat = SSTOP;
f026f3b0
MD
729 if (p->p_flag & P_ZOMBIE)
730 xproc.p_stat = SZOMB;
73e4f7b9
MD
731 } else if (td) {
732 fill_eproc_td(td, &eproc, &xproc);
733 }
734 error = SYSCTL_OUT(req,(caddr_t)&xproc, sizeof(struct proc));
984263bc
MD
735 if (error)
736 return (error);
737 error = SYSCTL_OUT(req,(caddr_t)&eproc, sizeof(eproc));
b8acf5a9
MD
738 if (error)
739 return (error);
73e4f7b9 740 error = SYSCTL_OUT(req,(caddr_t)td, sizeof(struct thread));
984263bc
MD
741 if (error)
742 return (error);
73e4f7b9 743#if 0
984263bc
MD
744 if (!doingzomb && pid && (pfind(pid) != p))
745 return EAGAIN;
746 if (doingzomb && zpfind(pid) != p)
747 return EAGAIN;
73e4f7b9 748#endif
984263bc
MD
749 return (0);
750}
751
752static int
753sysctl_kern_proc(SYSCTL_HANDLER_ARGS)
754{
755 int *name = (int*) arg1;
756 u_int namelen = arg2;
757 struct proc *p;
73e4f7b9 758 struct thread *td;
984263bc
MD
759 int doingzomb;
760 int error = 0;
630ccdeb
MD
761 int n;
762 int origcpu;
41c20dac 763 struct ucred *cr1 = curproc->p_ucred;
984263bc
MD
764
765 if (oidp->oid_number == KERN_PROC_PID) {
766 if (namelen != 1)
767 return (EINVAL);
768 p = pfind((pid_t)name[0]);
769 if (!p)
770 return (0);
41c20dac 771 if (!PRISON_CHECK(cr1, p->p_ucred))
984263bc 772 return (0);
73e4f7b9 773 error = sysctl_out_proc(p, NULL, req, 0);
984263bc
MD
774 return (error);
775 }
776 if (oidp->oid_number == KERN_PROC_ALL && !namelen)
777 ;
778 else if (oidp->oid_number != KERN_PROC_ALL && namelen == 1)
779 ;
780 else
781 return (EINVAL);
782
783 if (!req->oldptr) {
784 /* overestimate by 5 procs */
785 error = SYSCTL_OUT(req, 0, sizeof (struct kinfo_proc) * 5);
786 if (error)
787 return (error);
788 }
789 for (doingzomb=0 ; doingzomb < 2 ; doingzomb++) {
790 if (!doingzomb)
791 p = LIST_FIRST(&allproc);
792 else
793 p = LIST_FIRST(&zombproc);
794 for (; p != 0; p = LIST_NEXT(p, p_list)) {
795 /*
796 * Show a user only their processes.
797 */
41c20dac 798 if ((!ps_showallprocs) && p_trespass(cr1, p->p_ucred))
984263bc
MD
799 continue;
800 /*
801 * Skip embryonic processes.
802 */
803 if (p->p_stat == SIDL)
804 continue;
805 /*
806 * TODO - make more efficient (see notes below).
807 * do by session.
808 */
809 switch (oidp->oid_number) {
984263bc
MD
810 case KERN_PROC_PGRP:
811 /* could do this by traversing pgrp */
812 if (p->p_pgrp == NULL ||
813 p->p_pgrp->pg_id != (pid_t)name[0])
814 continue;
815 break;
816
817 case KERN_PROC_TTY:
818 if ((p->p_flag & P_CONTROLT) == 0 ||
819 p->p_session == NULL ||
820 p->p_session->s_ttyp == NULL ||
821 dev2udev(p->p_session->s_ttyp->t_dev) !=
822 (udev_t)name[0])
823 continue;
824 break;
825
826 case KERN_PROC_UID:
827 if (p->p_ucred == NULL ||
828 p->p_ucred->cr_uid != (uid_t)name[0])
829 continue;
830 break;
831
832 case KERN_PROC_RUID:
833 if (p->p_ucred == NULL ||
41c20dac 834 p->p_ucred->cr_ruid != (uid_t)name[0])
984263bc
MD
835 continue;
836 break;
837 }
838
41c20dac 839 if (!PRISON_CHECK(cr1, p->p_ucred))
984263bc 840 continue;
c008d3ad 841 PHOLD(p);
73e4f7b9 842 error = sysctl_out_proc(p, NULL, req, doingzomb);
c008d3ad 843 PRELE(p);
984263bc
MD
844 if (error)
845 return (error);
846 }
847 }
630ccdeb
MD
848
849 /*
850 * Iterate over all active cpus and scan their thread list. Start
851 * with the next logical cpu and end with our original cpu. We
852 * migrate our own thread to each target cpu in order to safely scan
853 * its thread list. In the last loop we migrate back to our original
854 * cpu.
855 */
856 origcpu = mycpu->gd_cpuid;
56c703bd
JS
857 if (!ps_showallthreads || jailed(cr1))
858 goto post_threads;
859 for (n = 1; n <= ncpus; ++n) {
630ccdeb
MD
860 globaldata_t rgd;
861 int nid;
862
863 nid = (origcpu + n) % ncpus;
864 if ((smp_active_mask & (1 << nid)) == 0)
865 continue;
866 rgd = globaldata_find(nid);
867 lwkt_setcpu_self(rgd);
630ccdeb 868
73e4f7b9
MD
869 TAILQ_FOREACH(td, &mycpu->gd_tdallq, td_allq) {
870 if (td->td_proc)
871 continue;
872 switch (oidp->oid_number) {
873 case KERN_PROC_PGRP:
874 case KERN_PROC_TTY:
875 case KERN_PROC_UID:
876 case KERN_PROC_RUID:
877 continue;
878 default:
879 break;
880 }
881 lwkt_hold(td);
882 error = sysctl_out_proc(NULL, td, req, doingzomb);
883 lwkt_rele(td);
884 if (error)
885 return (error);
886 }
887 }
56c703bd 888post_threads:
984263bc
MD
889 return (0);
890}
891
892/*
893 * This sysctl allows a process to retrieve the argument list or process
894 * title for another process without groping around in the address space
895 * of the other process. It also allow a process to set its own "process
896 * title to a string of its own choice.
897 */
898static int
899sysctl_kern_proc_args(SYSCTL_HANDLER_ARGS)
900{
901 int *name = (int*) arg1;
902 u_int namelen = arg2;
903 struct proc *p;
904 struct pargs *pa;
905 int error = 0;
41c20dac 906 struct ucred *cr1 = curproc->p_ucred;
984263bc
MD
907
908 if (namelen != 1)
909 return (EINVAL);
910
911 p = pfind((pid_t)name[0]);
912 if (!p)
913 return (0);
914
41c20dac 915 if ((!ps_argsopen) && p_trespass(cr1, p->p_ucred))
984263bc
MD
916 return (0);
917
918 if (req->newptr && curproc != p)
919 return (EPERM);
920
921 if (req->oldptr && p->p_args != NULL)
922 error = SYSCTL_OUT(req, p->p_args->ar_args, p->p_args->ar_length);
923 if (req->newptr == NULL)
924 return (error);
925
926 if (p->p_args && --p->p_args->ar_ref == 0)
927 FREE(p->p_args, M_PARGS);
928 p->p_args = NULL;
929
930 if (req->newlen + sizeof(struct pargs) > ps_arg_cache_limit)
931 return (error);
932
933 MALLOC(pa, struct pargs *, sizeof(struct pargs) + req->newlen,
934 M_PARGS, M_WAITOK);
935 pa->ar_ref = 1;
936 pa->ar_length = req->newlen;
937 error = SYSCTL_IN(req, pa->ar_args, req->newlen);
938 if (!error)
939 p->p_args = pa;
940 else
941 FREE(pa, M_PARGS);
942 return (error);
943}
944
945SYSCTL_NODE(_kern, KERN_PROC, proc, CTLFLAG_RD, 0, "Process table");
946
947SYSCTL_PROC(_kern_proc, KERN_PROC_ALL, all, CTLFLAG_RD|CTLTYPE_STRUCT,
948 0, 0, sysctl_kern_proc, "S,proc", "Return entire process table");
949
950SYSCTL_NODE(_kern_proc, KERN_PROC_PGRP, pgrp, CTLFLAG_RD,
951 sysctl_kern_proc, "Process table");
952
953SYSCTL_NODE(_kern_proc, KERN_PROC_TTY, tty, CTLFLAG_RD,
954 sysctl_kern_proc, "Process table");
955
956SYSCTL_NODE(_kern_proc, KERN_PROC_UID, uid, CTLFLAG_RD,
957 sysctl_kern_proc, "Process table");
958
959SYSCTL_NODE(_kern_proc, KERN_PROC_RUID, ruid, CTLFLAG_RD,
960 sysctl_kern_proc, "Process table");
961
962SYSCTL_NODE(_kern_proc, KERN_PROC_PID, pid, CTLFLAG_RD,
963 sysctl_kern_proc, "Process table");
964
965SYSCTL_NODE(_kern_proc, KERN_PROC_ARGS, args, CTLFLAG_RW | CTLFLAG_ANYBODY,
966 sysctl_kern_proc_args, "Process argument list");