sleep.9: Use .Fn instead of .Nm
[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 $
fa2217dc 35 * $DragonFly: src/sys/kern/kern_proc.c,v 1.45 2008/06/12 23:25:02 dillon 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>
8c72e3d5 47#include <sys/dsched.h>
984263bc 48#include <sys/signalvar.h>
5bf0d9b5 49#include <sys/spinlock.h>
984263bc
MD
50#include <vm/vm.h>
51#include <sys/lock.h>
52#include <vm/pmap.h>
53#include <vm/vm_map.h>
54#include <sys/user.h>
630ccdeb 55#include <machine/smp.h>
984263bc 56
5bf0d9b5 57#include <sys/spinlock2.h>
684a93c4 58#include <sys/mplock2.h>
5bf0d9b5 59
984263bc
MD
60static MALLOC_DEFINE(M_PGRP, "pgrp", "process group header");
61MALLOC_DEFINE(M_SESSION, "session", "session header");
fb2a331e 62MALLOC_DEFINE(M_PROC, "proc", "Proc structures");
f6c36234 63MALLOC_DEFINE(M_LWP, "lwp", "lwp structures");
984263bc
MD
64MALLOC_DEFINE(M_SUBPROC, "subproc", "Proc sub-structures");
65
28c57d20 66int ps_showallprocs = 1;
73e4f7b9 67static int ps_showallthreads = 1;
43a0f7ae
MS
68SYSCTL_INT(_security, OID_AUTO, ps_showallprocs, CTLFLAG_RW,
69 &ps_showallprocs, 0,
70 "Unprivileged processes can see proccesses with different UID/GID");
71SYSCTL_INT(_security, OID_AUTO, ps_showallthreads, CTLFLAG_RW,
72 &ps_showallthreads, 0,
73 "Unprivileged processes can see kernel threads");
984263bc 74
51e64ff2
MD
75static void pgdelete(struct pgrp *);
76static void orphanpg(struct pgrp *pg);
77static pid_t proc_getnewpid_locked(int random_offset);
984263bc
MD
78
79/*
80 * Other process lists
81 */
82struct pidhashhead *pidhashtbl;
83u_long pidhash;
84struct pgrphashhead *pgrphashtbl;
85u_long pgrphash;
86struct proclist allproc;
87struct proclist zombproc;
5bf0d9b5 88struct spinlock allproc_spin;
984263bc 89
51e64ff2
MD
90/*
91 * Random component to nextpid generation. We mix in a random factor to make
92 * it a little harder to predict. We sanity check the modulus value to avoid
93 * doing it in critical paths. Don't let it be too small or we pointlessly
94 * waste randomness entropy, and don't let it be impossibly large. Using a
95 * modulus that is too big causes a LOT more process table scans and slows
96 * down fork processing as the pidchecked caching is defeated.
97 */
98static int randompid = 0;
99
100static int
101sysctl_kern_randompid(SYSCTL_HANDLER_ARGS)
102{
103 int error, pid;
104
105 pid = randompid;
106 error = sysctl_handle_int(oidp, &pid, 0, req);
107 if (error || !req->newptr)
108 return (error);
109 if (pid < 0 || pid > PID_MAX - 100) /* out of range */
110 pid = PID_MAX - 100;
111 else if (pid < 2) /* NOP */
112 pid = 0;
113 else if (pid < 100) /* Make it reasonable */
114 pid = 100;
115 randompid = pid;
116 return (error);
117}
118
119SYSCTL_PROC(_kern, OID_AUTO, randompid, CTLTYPE_INT|CTLFLAG_RW,
120 0, 0, sysctl_kern_randompid, "I", "Random PID modulus");
121
984263bc
MD
122/*
123 * Initialize global process hashing structures.
124 */
125void
77153250 126procinit(void)
984263bc 127{
984263bc
MD
128 LIST_INIT(&allproc);
129 LIST_INIT(&zombproc);
5bf0d9b5 130 spin_init(&allproc_spin);
40aaf5fc 131 lwkt_init();
984263bc
MD
132 pidhashtbl = hashinit(maxproc / 4, M_PROC, &pidhash);
133 pgrphashtbl = hashinit(maxproc / 4, M_PROC, &pgrphash);
984263bc
MD
134 uihashinit();
135}
136
137/*
138 * Is p an inferior of the current process?
139 */
140int
77153250 141inferior(struct proc *p)
984263bc 142{
984263bc
MD
143 for (; p != curproc; p = p->p_pptr)
144 if (p->p_pid == 0)
145 return (0);
146 return (1);
147}
148
149/*
150 * Locate a process by number
151 */
152struct proc *
77153250 153pfind(pid_t pid)
984263bc 154{
1fd87d54 155 struct proc *p;
984263bc 156
5bf0d9b5 157 LIST_FOREACH(p, PIDHASH(pid), p_hash) {
984263bc
MD
158 if (p->p_pid == pid)
159 return (p);
5bf0d9b5 160 }
984263bc
MD
161 return (NULL);
162}
163
164/*
165 * Locate a process group by number
166 */
167struct pgrp *
77153250 168pgfind(pid_t pgid)
984263bc 169{
1fd87d54 170 struct pgrp *pgrp;
984263bc 171
5bf0d9b5 172 LIST_FOREACH(pgrp, PGRPHASH(pgid), pg_hash) {
984263bc
MD
173 if (pgrp->pg_id == pgid)
174 return (pgrp);
5bf0d9b5 175 }
984263bc
MD
176 return (NULL);
177}
178
179/*
180 * Move p to a new or existing process group (and session)
181 */
182int
77153250 183enterpgrp(struct proc *p, pid_t pgid, int mksess)
984263bc 184{
1fd87d54 185 struct pgrp *pgrp = pgfind(pgid);
984263bc
MD
186
187 KASSERT(pgrp == NULL || !mksess,
188 ("enterpgrp: setsid into non-empty pgrp"));
189 KASSERT(!SESS_LEADER(p),
190 ("enterpgrp: session leader attempted setpgrp"));
191
192 if (pgrp == NULL) {
193 pid_t savepid = p->p_pid;
194 struct proc *np;
195 /*
196 * new process group
197 */
198 KASSERT(p->p_pid == pgid,
199 ("enterpgrp: new pgrp and pid != pgid"));
200 if ((np = pfind(savepid)) == NULL || np != p)
201 return (ESRCH);
202 MALLOC(pgrp, struct pgrp *, sizeof(struct pgrp), M_PGRP,
203 M_WAITOK);
204 if (mksess) {
1fd87d54 205 struct session *sess;
984263bc
MD
206
207 /*
208 * new session
209 */
210 MALLOC(sess, struct session *, sizeof(struct session),
211 M_SESSION, M_WAITOK);
212 sess->s_leader = p;
213 sess->s_sid = p->p_pid;
214 sess->s_count = 1;
215 sess->s_ttyvp = NULL;
216 sess->s_ttyp = NULL;
217 bcopy(p->p_session->s_login, sess->s_login,
218 sizeof(sess->s_login));
219 p->p_flag &= ~P_CONTROLT;
220 pgrp->pg_session = sess;
221 KASSERT(p == curproc,
222 ("enterpgrp: mksession and p != curproc"));
223 } else {
224 pgrp->pg_session = p->p_session;
8b90699b 225 sess_hold(pgrp->pg_session);
984263bc
MD
226 }
227 pgrp->pg_id = pgid;
228 LIST_INIT(&pgrp->pg_members);
229 LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash);
230 pgrp->pg_jobc = 0;
231 SLIST_INIT(&pgrp->pg_sigiolst);
167e6ecb 232 lockinit(&pgrp->pg_lock, "pgwt", 0, 0);
984263bc
MD
233 } else if (pgrp == p->p_pgrp)
234 return (0);
235
236 /*
237 * Adjust eligibility of affected pgrps to participate in job control.
238 * Increment eligibility counts before decrementing, otherwise we
239 * could reach 0 spuriously during the first call.
240 */
241 fixjobc(p, pgrp, 1);
242 fixjobc(p, p->p_pgrp, 0);
243
244 LIST_REMOVE(p, p_pglist);
245 if (LIST_EMPTY(&p->p_pgrp->pg_members))
246 pgdelete(p->p_pgrp);
247 p->p_pgrp = pgrp;
248 LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist);
249 return (0);
250}
251
252/*
253 * remove process from process group
254 */
255int
77153250 256leavepgrp(struct proc *p)
984263bc
MD
257{
258
259 LIST_REMOVE(p, p_pglist);
260 if (LIST_EMPTY(&p->p_pgrp->pg_members))
261 pgdelete(p->p_pgrp);
262 p->p_pgrp = 0;
263 return (0);
264}
265
266/*
267 * delete a process group
268 */
269static void
77153250 270pgdelete(struct pgrp *pgrp)
984263bc
MD
271{
272
273 /*
274 * Reset any sigio structures pointing to us as a result of
275 * F_SETOWN with our pgid.
276 */
277 funsetownlst(&pgrp->pg_sigiolst);
278
279 if (pgrp->pg_session->s_ttyp != NULL &&
280 pgrp->pg_session->s_ttyp->t_pgrp == pgrp)
281 pgrp->pg_session->s_ttyp->t_pgrp = NULL;
282 LIST_REMOVE(pgrp, pg_hash);
8b90699b 283 sess_rele(pgrp->pg_session);
efda3bd0 284 kfree(pgrp, M_PGRP);
8b90699b
MD
285}
286
287/*
288 * Adjust the ref count on a session structure. When the ref count falls to
289 * zero the tty is disassociated from the session and the session structure
290 * is freed. Note that tty assocation is not itself ref-counted.
291 */
292void
293sess_hold(struct session *sp)
294{
295 ++sp->s_count;
296}
297
298void
299sess_rele(struct session *sp)
300{
301 KKASSERT(sp->s_count > 0);
302 if (--sp->s_count == 0) {
303 if (sp->s_ttyp && sp->s_ttyp->t_session) {
304#ifdef TTY_DO_FULL_CLOSE
305 /* FULL CLOSE, see ttyclearsession() */
306 KKASSERT(sp->s_ttyp->t_session == sp);
307 sp->s_ttyp->t_session = NULL;
308#else
309 /* HALF CLOSE, see ttyclearsession() */
310 if (sp->s_ttyp->t_session == sp)
311 sp->s_ttyp->t_session = NULL;
312#endif
313 }
efda3bd0 314 kfree(sp, M_SESSION);
8b90699b 315 }
984263bc
MD
316}
317
318/*
319 * Adjust pgrp jobc counters when specified process changes process group.
320 * We count the number of processes in each process group that "qualify"
321 * the group for terminal job control (those with a parent in a different
322 * process group of the same session). If that count reaches zero, the
323 * process group becomes orphaned. Check both the specified process'
324 * process group and that of its children.
325 * entering == 0 => p is leaving specified group.
326 * entering == 1 => p is entering specified group.
327 */
328void
77153250 329fixjobc(struct proc *p, struct pgrp *pgrp, int entering)
984263bc 330{
1fd87d54
RG
331 struct pgrp *hispgrp;
332 struct session *mysession = pgrp->pg_session;
984263bc
MD
333
334 /*
335 * Check p's parent to see whether p qualifies its own process
336 * group; if so, adjust count for p's process group.
337 */
338 if ((hispgrp = p->p_pptr->p_pgrp) != pgrp &&
339 hispgrp->pg_session == mysession) {
340 if (entering)
341 pgrp->pg_jobc++;
342 else if (--pgrp->pg_jobc == 0)
343 orphanpg(pgrp);
344 }
345
346 /*
347 * Check this process' children to see whether they qualify
348 * their process groups; if so, adjust counts for children's
349 * process groups.
350 */
351 LIST_FOREACH(p, &p->p_children, p_sibling)
352 if ((hispgrp = p->p_pgrp) != pgrp &&
353 hispgrp->pg_session == mysession &&
416d05d7 354 p->p_stat != SZOMB) {
984263bc
MD
355 if (entering)
356 hispgrp->pg_jobc++;
357 else if (--hispgrp->pg_jobc == 0)
358 orphanpg(hispgrp);
359 }
360}
361
362/*
363 * A process group has become orphaned;
364 * if there are any stopped processes in the group,
365 * hang-up all process in that group.
366 */
367static void
77153250 368orphanpg(struct pgrp *pg)
984263bc 369{
1fd87d54 370 struct proc *p;
984263bc
MD
371
372 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
164b8401 373 if (p->p_stat == SSTOP) {
984263bc 374 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
84204577
MD
375 ksignal(p, SIGHUP);
376 ksignal(p, SIGCONT);
984263bc
MD
377 }
378 return;
379 }
380 }
381}
382
51e64ff2
MD
383/*
384 * Add a new process to the allproc list and the PID hash. This
385 * also assigns a pid to the new process.
386 *
0ced1954 387 * MPALMOSTSAFE - acquires mplock for karc4random() call
51e64ff2
MD
388 */
389void
390proc_add_allproc(struct proc *p)
391{
392 int random_offset;
393
394 if ((random_offset = randompid) != 0) {
395 get_mplock();
0ced1954 396 random_offset = karc4random() % random_offset;
51e64ff2
MD
397 rel_mplock();
398 }
399
400 spin_lock_wr(&allproc_spin);
401 p->p_pid = proc_getnewpid_locked(random_offset);
402 LIST_INSERT_HEAD(&allproc, p, p_list);
403 LIST_INSERT_HEAD(PIDHASH(p->p_pid), p, p_hash);
404 spin_unlock_wr(&allproc_spin);
405}
406
407/*
408 * Calculate a new process pid. This function is integrated into
409 * proc_add_allproc() to guarentee that the new pid is not reused before
410 * the new process can be added to the allproc list.
411 *
412 * MPSAFE - must be called with allproc_spin held.
413 */
414static
415pid_t
416proc_getnewpid_locked(int random_offset)
417{
418 static pid_t nextpid;
419 static pid_t pidchecked;
420 struct proc *p;
421
422 /*
423 * Find an unused process ID. We remember a range of unused IDs
424 * ready to use (from nextpid+1 through pidchecked-1).
425 */
426 nextpid = nextpid + 1 + random_offset;
427retry:
428 /*
429 * If the process ID prototype has wrapped around,
430 * restart somewhat above 0, as the low-numbered procs
431 * tend to include daemons that don't exit.
432 */
433 if (nextpid >= PID_MAX) {
434 nextpid = nextpid % PID_MAX;
435 if (nextpid < 100)
436 nextpid += 100;
437 pidchecked = 0;
438 }
439 if (nextpid >= pidchecked) {
440 int doingzomb = 0;
441
442 pidchecked = PID_MAX;
443 /*
444 * Scan the active and zombie procs to check whether this pid
445 * is in use. Remember the lowest pid that's greater
446 * than nextpid, so we can avoid checking for a while.
447 */
448 p = LIST_FIRST(&allproc);
449again:
450 for (; p != 0; p = LIST_NEXT(p, p_list)) {
451 while (p->p_pid == nextpid ||
452 p->p_pgrp->pg_id == nextpid ||
453 p->p_session->s_sid == nextpid) {
454 nextpid++;
455 if (nextpid >= pidchecked)
456 goto retry;
457 }
458 if (p->p_pid > nextpid && pidchecked > p->p_pid)
459 pidchecked = p->p_pid;
460 if (p->p_pgrp->pg_id > nextpid &&
461 pidchecked > p->p_pgrp->pg_id)
462 pidchecked = p->p_pgrp->pg_id;
463 if (p->p_session->s_sid > nextpid &&
464 pidchecked > p->p_session->s_sid)
465 pidchecked = p->p_session->s_sid;
466 }
467 if (!doingzomb) {
468 doingzomb = 1;
469 p = LIST_FIRST(&zombproc);
470 goto again;
471 }
472 }
473 return(nextpid);
474}
475
5bf0d9b5
MD
476/*
477 * Called from exit1 to remove a process from the allproc
478 * list and move it to the zombie list.
479 *
480 * MPSAFE
481 */
482void
483proc_move_allproc_zombie(struct proc *p)
484{
485 spin_lock_wr(&allproc_spin);
486 while (p->p_lock) {
487 spin_unlock_wr(&allproc_spin);
488 tsleep(p, 0, "reap1", hz / 10);
489 spin_lock_wr(&allproc_spin);
490 }
491 LIST_REMOVE(p, p_list);
492 LIST_INSERT_HEAD(&zombproc, p, p_list);
493 LIST_REMOVE(p, p_hash);
416d05d7 494 p->p_stat = SZOMB;
5bf0d9b5 495 spin_unlock_wr(&allproc_spin);
8c72e3d5 496 dsched_exit_proc(p);
5bf0d9b5
MD
497}
498
499/*
500 * This routine is called from kern_wait() and will remove the process
501 * from the zombie list and the sibling list. This routine will block
502 * if someone has a lock on the proces (p_lock).
503 *
504 * MPSAFE
505 */
506void
507proc_remove_zombie(struct proc *p)
508{
509 spin_lock_wr(&allproc_spin);
510 while (p->p_lock) {
511 spin_unlock_wr(&allproc_spin);
512 tsleep(p, 0, "reap1", hz / 10);
513 spin_lock_wr(&allproc_spin);
514 }
515 LIST_REMOVE(p, p_list); /* off zombproc */
516 LIST_REMOVE(p, p_sibling);
517 spin_unlock_wr(&allproc_spin);
518}
519
520/*
521 * Scan all processes on the allproc list. The process is automatically
522 * held for the callback. A return value of -1 terminates the loop.
523 *
524 * MPSAFE
525 */
526void
527allproc_scan(int (*callback)(struct proc *, void *), void *data)
528{
529 struct proc *p;
530 int r;
531
532 spin_lock_rd(&allproc_spin);
533 LIST_FOREACH(p, &allproc, p_list) {
534 PHOLD(p);
535 spin_unlock_rd(&allproc_spin);
536 r = callback(p, data);
537 spin_lock_rd(&allproc_spin);
538 PRELE(p);
539 if (r < 0)
540 break;
541 }
542 spin_unlock_rd(&allproc_spin);
543}
544
c7e98b2f
SS
545/*
546 * Scan all lwps of processes on the allproc list. The lwp is automatically
547 * held for the callback. A return value of -1 terminates the loop.
548 *
549 * possibly not MPSAFE, needs to access foreingn proc structures
550 */
551void
552alllwp_scan(int (*callback)(struct lwp *, void *), void *data)
553{
554 struct proc *p;
555 struct lwp *lp;
556 int r = 0;
557
558 spin_lock_rd(&allproc_spin);
559 LIST_FOREACH(p, &allproc, p_list) {
560 PHOLD(p);
561 spin_unlock_rd(&allproc_spin);
562 FOREACH_LWP_IN_PROC(lp, p) {
563 LWPHOLD(lp);
564 r = callback(lp, data);
565 LWPRELE(lp);
566 }
567 spin_lock_rd(&allproc_spin);
568 PRELE(p);
569 if (r < 0)
570 break;
571 }
572 spin_unlock_rd(&allproc_spin);
573}
574
5bf0d9b5
MD
575/*
576 * Scan all processes on the zombproc list. The process is automatically
577 * held for the callback. A return value of -1 terminates the loop.
578 *
579 * MPSAFE
580 */
581void
582zombproc_scan(int (*callback)(struct proc *, void *), void *data)
583{
584 struct proc *p;
585 int r;
586
587 spin_lock_rd(&allproc_spin);
588 LIST_FOREACH(p, &zombproc, p_list) {
589 PHOLD(p);
590 spin_unlock_rd(&allproc_spin);
591 r = callback(p, data);
592 spin_lock_rd(&allproc_spin);
593 PRELE(p);
594 if (r < 0)
595 break;
596 }
597 spin_unlock_rd(&allproc_spin);
598}
599
984263bc
MD
600#include "opt_ddb.h"
601#ifdef DDB
602#include <ddb/ddb.h>
603
604DB_SHOW_COMMAND(pgrpdump, pgrpdump)
605{
1fd87d54
RG
606 struct pgrp *pgrp;
607 struct proc *p;
608 int i;
984263bc
MD
609
610 for (i = 0; i <= pgrphash; i++) {
611 if (!LIST_EMPTY(&pgrphashtbl[i])) {
6ea70f76 612 kprintf("\tindx %d\n", i);
984263bc 613 LIST_FOREACH(pgrp, &pgrphashtbl[i], pg_hash) {
6ea70f76 614 kprintf(
984263bc
MD
615 "\tpgrp %p, pgid %ld, sess %p, sesscnt %d, mem %p\n",
616 (void *)pgrp, (long)pgrp->pg_id,
617 (void *)pgrp->pg_session,
618 pgrp->pg_session->s_count,
619 (void *)LIST_FIRST(&pgrp->pg_members));
620 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
6ea70f76 621 kprintf("\t\tpid %ld addr %p pgrp %p\n",
984263bc
MD
622 (long)p->p_pid, (void *)p,
623 (void *)p->p_pgrp);
624 }
625 }
626 }
627 }
628}
629#endif /* DDB */
630
5bf0d9b5
MD
631/*
632 * Locate a process on the zombie list. Return a held process or NULL.
633 */
984263bc
MD
634struct proc *
635zpfind(pid_t pid)
636{
637 struct proc *p;
638
639 LIST_FOREACH(p, &zombproc, p_list)
640 if (p->p_pid == pid)
641 return (p);
642 return (NULL);
643}
644
984263bc 645static int
5dfd06ac 646sysctl_out_proc(struct proc *p, struct sysctl_req *req, int flags)
984263bc 647{
5dfd06ac
SS
648 struct kinfo_proc ki;
649 struct lwp *lp;
fe14f34e 650 int skp = 0, had_output = 0;
984263bc 651 int error;
d9d6cb99 652
fa2217dc 653 bzero(&ki, sizeof(ki));
5dfd06ac
SS
654 fill_kinfo_proc(p, &ki);
655 if ((flags & KERN_PROC_FLAG_LWP) == 0)
656 skp = 1;
fa2217dc 657 error = 0;
5dfd06ac 658 FOREACH_LWP_IN_PROC(lp, p) {
fa2217dc 659 LWPHOLD(lp);
5dfd06ac 660 fill_kinfo_lwp(lp, &ki.kp_lwp);
5dfd06ac
SS
661 had_output = 1;
662 error = SYSCTL_OUT(req, &ki, sizeof(ki));
fa2217dc 663 LWPRELE(lp);
5dfd06ac 664 if (error)
fa2217dc 665 break;
5dfd06ac
SS
666 if (skp)
667 break;
73e4f7b9 668 }
5dfd06ac 669 /* We need to output at least the proc, even if there is no lwp. */
fa2217dc
MD
670 if (had_output == 0) {
671 error = SYSCTL_OUT(req, &ki, sizeof(ki));
672 }
673 return (error);
984263bc
MD
674}
675
ef02d0e1
TS
676static int
677sysctl_out_proc_kthread(struct thread *td, struct sysctl_req *req, int flags)
678{
679 struct kinfo_proc ki;
680 int error;
681
682 fill_kinfo_proc_kthread(td, &ki);
683 error = SYSCTL_OUT(req, &ki, sizeof(ki));
684 if (error)
685 return error;
686 return(0);
687}
688
984263bc
MD
689static int
690sysctl_kern_proc(SYSCTL_HANDLER_ARGS)
691{
692 int *name = (int*) arg1;
5dfd06ac 693 int oid = oidp->oid_number;
984263bc 694 u_int namelen = arg2;
7cd8fd20 695 struct proc *p;
5dfd06ac 696 struct proclist *plist;
ef02d0e1 697 struct thread *td;
5dfd06ac 698 int doingzomb, flags = 0;
984263bc 699 int error = 0;
ef02d0e1 700 int n;
630ccdeb 701 int origcpu;
41c20dac 702 struct ucred *cr1 = curproc->p_ucred;
984263bc 703
5dfd06ac
SS
704 flags = oid & KERN_PROC_FLAGMASK;
705 oid &= ~KERN_PROC_FLAGMASK;
706
707 if ((oid == KERN_PROC_ALL && namelen != 0) ||
708 (oid != KERN_PROC_ALL && namelen != 1))
709 return (EINVAL);
710
711 if (oid == KERN_PROC_PID) {
984263bc
MD
712 p = pfind((pid_t)name[0]);
713 if (!p)
714 return (0);
41c20dac 715 if (!PRISON_CHECK(cr1, p->p_ucred))
984263bc 716 return (0);
e7093b07 717 PHOLD(p);
5dfd06ac 718 error = sysctl_out_proc(p, req, flags);
e7093b07 719 PRELE(p);
984263bc
MD
720 return (error);
721 }
5dfd06ac 722
984263bc
MD
723 if (!req->oldptr) {
724 /* overestimate by 5 procs */
725 error = SYSCTL_OUT(req, 0, sizeof (struct kinfo_proc) * 5);
726 if (error)
727 return (error);
728 }
5dfd06ac
SS
729 for (doingzomb = 0; doingzomb <= 1; doingzomb++) {
730 if (doingzomb)
731 plist = &zombproc;
984263bc 732 else
5dfd06ac 733 plist = &allproc;
7cd8fd20 734 LIST_FOREACH(p, plist, p_list) {
984263bc
MD
735 /*
736 * Show a user only their processes.
737 */
41c20dac 738 if ((!ps_showallprocs) && p_trespass(cr1, p->p_ucred))
984263bc
MD
739 continue;
740 /*
741 * Skip embryonic processes.
742 */
743 if (p->p_stat == SIDL)
744 continue;
745 /*
746 * TODO - make more efficient (see notes below).
747 * do by session.
748 */
5dfd06ac 749 switch (oid) {
984263bc
MD
750 case KERN_PROC_PGRP:
751 /* could do this by traversing pgrp */
752 if (p->p_pgrp == NULL ||
753 p->p_pgrp->pg_id != (pid_t)name[0])
754 continue;
755 break;
756
757 case KERN_PROC_TTY:
758 if ((p->p_flag & P_CONTROLT) == 0 ||
759 p->p_session == NULL ||
760 p->p_session->s_ttyp == NULL ||
761 dev2udev(p->p_session->s_ttyp->t_dev) !=
762 (udev_t)name[0])
763 continue;
764 break;
765
766 case KERN_PROC_UID:
767 if (p->p_ucred == NULL ||
768 p->p_ucred->cr_uid != (uid_t)name[0])
769 continue;
770 break;
771
772 case KERN_PROC_RUID:
773 if (p->p_ucred == NULL ||
41c20dac 774 p->p_ucred->cr_ruid != (uid_t)name[0])
984263bc
MD
775 continue;
776 break;
777 }
778
41c20dac 779 if (!PRISON_CHECK(cr1, p->p_ucred))
984263bc 780 continue;
c008d3ad 781 PHOLD(p);
5dfd06ac 782 error = sysctl_out_proc(p, req, flags);
c008d3ad 783 PRELE(p);
984263bc
MD
784 if (error)
785 return (error);
786 }
787 }
630ccdeb
MD
788
789 /*
790 * Iterate over all active cpus and scan their thread list. Start
791 * with the next logical cpu and end with our original cpu. We
792 * migrate our own thread to each target cpu in order to safely scan
793 * its thread list. In the last loop we migrate back to our original
794 * cpu.
795 */
796 origcpu = mycpu->gd_cpuid;
56c703bd
JS
797 if (!ps_showallthreads || jailed(cr1))
798 goto post_threads;
799 for (n = 1; n <= ncpus; ++n) {
630ccdeb
MD
800 globaldata_t rgd;
801 int nid;
802
803 nid = (origcpu + n) % ncpus;
804 if ((smp_active_mask & (1 << nid)) == 0)
805 continue;
806 rgd = globaldata_find(nid);
807 lwkt_setcpu_self(rgd);
630ccdeb 808
73e4f7b9
MD
809 TAILQ_FOREACH(td, &mycpu->gd_tdallq, td_allq) {
810 if (td->td_proc)
811 continue;
ef02d0e1 812 switch (oid) {
73e4f7b9
MD
813 case KERN_PROC_PGRP:
814 case KERN_PROC_TTY:
815 case KERN_PROC_UID:
816 case KERN_PROC_RUID:
817 continue;
818 default:
819 break;
820 }
821 lwkt_hold(td);
ef02d0e1 822 error = sysctl_out_proc_kthread(td, req, doingzomb);
73e4f7b9
MD
823 lwkt_rele(td);
824 if (error)
825 return (error);
826 }
827 }
56c703bd 828post_threads:
984263bc
MD
829 return (0);
830}
831
832/*
833 * This sysctl allows a process to retrieve the argument list or process
834 * title for another process without groping around in the address space
835 * of the other process. It also allow a process to set its own "process
836 * title to a string of its own choice.
837 */
838static int
839sysctl_kern_proc_args(SYSCTL_HANDLER_ARGS)
840{
841 int *name = (int*) arg1;
842 u_int namelen = arg2;
843 struct proc *p;
844 struct pargs *pa;
845 int error = 0;
41c20dac 846 struct ucred *cr1 = curproc->p_ucred;
984263bc
MD
847
848 if (namelen != 1)
849 return (EINVAL);
850
851 p = pfind((pid_t)name[0]);
852 if (!p)
853 return (0);
854
41c20dac 855 if ((!ps_argsopen) && p_trespass(cr1, p->p_ucred))
984263bc
MD
856 return (0);
857
858 if (req->newptr && curproc != p)
859 return (EPERM);
860
861 if (req->oldptr && p->p_args != NULL)
862 error = SYSCTL_OUT(req, p->p_args->ar_args, p->p_args->ar_length);
863 if (req->newptr == NULL)
864 return (error);
865
866 if (p->p_args && --p->p_args->ar_ref == 0)
867 FREE(p->p_args, M_PARGS);
868 p->p_args = NULL;
869
870 if (req->newlen + sizeof(struct pargs) > ps_arg_cache_limit)
871 return (error);
872
873 MALLOC(pa, struct pargs *, sizeof(struct pargs) + req->newlen,
874 M_PARGS, M_WAITOK);
875 pa->ar_ref = 1;
876 pa->ar_length = req->newlen;
877 error = SYSCTL_IN(req, pa->ar_args, req->newlen);
878 if (!error)
879 p->p_args = pa;
880 else
881 FREE(pa, M_PARGS);
882 return (error);
883}
884
885SYSCTL_NODE(_kern, KERN_PROC, proc, CTLFLAG_RD, 0, "Process table");
886
887SYSCTL_PROC(_kern_proc, KERN_PROC_ALL, all, CTLFLAG_RD|CTLTYPE_STRUCT,
888 0, 0, sysctl_kern_proc, "S,proc", "Return entire process table");
889
890SYSCTL_NODE(_kern_proc, KERN_PROC_PGRP, pgrp, CTLFLAG_RD,
891 sysctl_kern_proc, "Process table");
892
893SYSCTL_NODE(_kern_proc, KERN_PROC_TTY, tty, CTLFLAG_RD,
894 sysctl_kern_proc, "Process table");
895
896SYSCTL_NODE(_kern_proc, KERN_PROC_UID, uid, CTLFLAG_RD,
897 sysctl_kern_proc, "Process table");
898
899SYSCTL_NODE(_kern_proc, KERN_PROC_RUID, ruid, CTLFLAG_RD,
900 sysctl_kern_proc, "Process table");
901
902SYSCTL_NODE(_kern_proc, KERN_PROC_PID, pid, CTLFLAG_RD,
903 sysctl_kern_proc, "Process table");
904
5dfd06ac
SS
905SYSCTL_NODE(_kern_proc, (KERN_PROC_ALL | KERN_PROC_FLAG_LWP), all_lwp, CTLFLAG_RD,
906 sysctl_kern_proc, "Process table");
907
908SYSCTL_NODE(_kern_proc, (KERN_PROC_PGRP | KERN_PROC_FLAG_LWP), pgrp_lwp, CTLFLAG_RD,
909 sysctl_kern_proc, "Process table");
910
911SYSCTL_NODE(_kern_proc, (KERN_PROC_TTY | KERN_PROC_FLAG_LWP), tty_lwp, CTLFLAG_RD,
912 sysctl_kern_proc, "Process table");
913
914SYSCTL_NODE(_kern_proc, (KERN_PROC_UID | KERN_PROC_FLAG_LWP), uid_lwp, CTLFLAG_RD,
915 sysctl_kern_proc, "Process table");
916
917SYSCTL_NODE(_kern_proc, (KERN_PROC_RUID | KERN_PROC_FLAG_LWP), ruid_lwp, CTLFLAG_RD,
918 sysctl_kern_proc, "Process table");
919
920SYSCTL_NODE(_kern_proc, (KERN_PROC_PID | KERN_PROC_FLAG_LWP), pid_lwp, CTLFLAG_RD,
921 sysctl_kern_proc, "Process table");
922
984263bc
MD
923SYSCTL_NODE(_kern_proc, KERN_PROC_ARGS, args, CTLFLAG_RW | CTLFLAG_ANYBODY,
924 sysctl_kern_proc_args, "Process argument list");