Start consolidating process related code into kern_proc.c. Implement
[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 $
5bf0d9b5 35 * $DragonFly: src/sys/kern/kern_proc.c,v 1.24 2006/05/24 17:44: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>
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
402ed7e1 71static void pgdelete (struct pgrp *);
984263bc 72
402ed7e1 73static void orphanpg (struct pgrp *pg);
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
MD
87
88/*
89 * Initialize global process hashing structures.
90 */
91void
77153250 92procinit(void)
984263bc 93{
984263bc
MD
94 LIST_INIT(&allproc);
95 LIST_INIT(&zombproc);
5bf0d9b5 96 spin_init(&allproc_spin);
984263bc
MD
97 pidhashtbl = hashinit(maxproc / 4, M_PROC, &pidhash);
98 pgrphashtbl = hashinit(maxproc / 4, M_PROC, &pgrphash);
99 proc_zone = zinit("PROC", sizeof (struct proc), 0, 0, 5);
263e4574 100 thread_zone = zinit("THREAD", sizeof (struct thread), 0, 0, 5);
984263bc
MD
101 uihashinit();
102}
103
104/*
105 * Is p an inferior of the current process?
106 */
107int
77153250 108inferior(struct proc *p)
984263bc 109{
984263bc
MD
110 for (; p != curproc; p = p->p_pptr)
111 if (p->p_pid == 0)
112 return (0);
113 return (1);
114}
115
116/*
117 * Locate a process by number
118 */
119struct proc *
77153250 120pfind(pid_t pid)
984263bc 121{
1fd87d54 122 struct proc *p;
984263bc 123
5bf0d9b5 124 LIST_FOREACH(p, PIDHASH(pid), p_hash) {
984263bc
MD
125 if (p->p_pid == pid)
126 return (p);
5bf0d9b5 127 }
984263bc
MD
128 return (NULL);
129}
130
131/*
132 * Locate a process group by number
133 */
134struct pgrp *
77153250 135pgfind(pid_t pgid)
984263bc 136{
1fd87d54 137 struct pgrp *pgrp;
984263bc 138
5bf0d9b5 139 LIST_FOREACH(pgrp, PGRPHASH(pgid), pg_hash) {
984263bc
MD
140 if (pgrp->pg_id == pgid)
141 return (pgrp);
5bf0d9b5 142 }
984263bc
MD
143 return (NULL);
144}
145
146/*
147 * Move p to a new or existing process group (and session)
148 */
149int
77153250 150enterpgrp(struct proc *p, pid_t pgid, int mksess)
984263bc 151{
1fd87d54 152 struct pgrp *pgrp = pgfind(pgid);
984263bc
MD
153
154 KASSERT(pgrp == NULL || !mksess,
155 ("enterpgrp: setsid into non-empty pgrp"));
156 KASSERT(!SESS_LEADER(p),
157 ("enterpgrp: session leader attempted setpgrp"));
158
159 if (pgrp == NULL) {
160 pid_t savepid = p->p_pid;
161 struct proc *np;
162 /*
163 * new process group
164 */
165 KASSERT(p->p_pid == pgid,
166 ("enterpgrp: new pgrp and pid != pgid"));
167 if ((np = pfind(savepid)) == NULL || np != p)
168 return (ESRCH);
169 MALLOC(pgrp, struct pgrp *, sizeof(struct pgrp), M_PGRP,
170 M_WAITOK);
171 if (mksess) {
1fd87d54 172 struct session *sess;
984263bc
MD
173
174 /*
175 * new session
176 */
177 MALLOC(sess, struct session *, sizeof(struct session),
178 M_SESSION, M_WAITOK);
179 sess->s_leader = p;
180 sess->s_sid = p->p_pid;
181 sess->s_count = 1;
182 sess->s_ttyvp = NULL;
183 sess->s_ttyp = NULL;
184 bcopy(p->p_session->s_login, sess->s_login,
185 sizeof(sess->s_login));
186 p->p_flag &= ~P_CONTROLT;
187 pgrp->pg_session = sess;
188 KASSERT(p == curproc,
189 ("enterpgrp: mksession and p != curproc"));
190 } else {
191 pgrp->pg_session = p->p_session;
8b90699b 192 sess_hold(pgrp->pg_session);
984263bc
MD
193 }
194 pgrp->pg_id = pgid;
195 LIST_INIT(&pgrp->pg_members);
196 LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash);
197 pgrp->pg_jobc = 0;
198 SLIST_INIT(&pgrp->pg_sigiolst);
199 } else if (pgrp == p->p_pgrp)
200 return (0);
201
202 /*
203 * Adjust eligibility of affected pgrps to participate in job control.
204 * Increment eligibility counts before decrementing, otherwise we
205 * could reach 0 spuriously during the first call.
206 */
207 fixjobc(p, pgrp, 1);
208 fixjobc(p, p->p_pgrp, 0);
209
210 LIST_REMOVE(p, p_pglist);
211 if (LIST_EMPTY(&p->p_pgrp->pg_members))
212 pgdelete(p->p_pgrp);
213 p->p_pgrp = pgrp;
214 LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist);
215 return (0);
216}
217
218/*
219 * remove process from process group
220 */
221int
77153250 222leavepgrp(struct proc *p)
984263bc
MD
223{
224
225 LIST_REMOVE(p, p_pglist);
226 if (LIST_EMPTY(&p->p_pgrp->pg_members))
227 pgdelete(p->p_pgrp);
228 p->p_pgrp = 0;
229 return (0);
230}
231
232/*
233 * delete a process group
234 */
235static void
77153250 236pgdelete(struct pgrp *pgrp)
984263bc
MD
237{
238
239 /*
240 * Reset any sigio structures pointing to us as a result of
241 * F_SETOWN with our pgid.
242 */
243 funsetownlst(&pgrp->pg_sigiolst);
244
245 if (pgrp->pg_session->s_ttyp != NULL &&
246 pgrp->pg_session->s_ttyp->t_pgrp == pgrp)
247 pgrp->pg_session->s_ttyp->t_pgrp = NULL;
248 LIST_REMOVE(pgrp, pg_hash);
8b90699b
MD
249 sess_rele(pgrp->pg_session);
250 free(pgrp, M_PGRP);
251}
252
253/*
254 * Adjust the ref count on a session structure. When the ref count falls to
255 * zero the tty is disassociated from the session and the session structure
256 * is freed. Note that tty assocation is not itself ref-counted.
257 */
258void
259sess_hold(struct session *sp)
260{
261 ++sp->s_count;
262}
263
264void
265sess_rele(struct session *sp)
266{
267 KKASSERT(sp->s_count > 0);
268 if (--sp->s_count == 0) {
269 if (sp->s_ttyp && sp->s_ttyp->t_session) {
270#ifdef TTY_DO_FULL_CLOSE
271 /* FULL CLOSE, see ttyclearsession() */
272 KKASSERT(sp->s_ttyp->t_session == sp);
273 sp->s_ttyp->t_session = NULL;
274#else
275 /* HALF CLOSE, see ttyclearsession() */
276 if (sp->s_ttyp->t_session == sp)
277 sp->s_ttyp->t_session = NULL;
278#endif
279 }
280 free(sp, M_SESSION);
281 }
984263bc
MD
282}
283
284/*
285 * Adjust pgrp jobc counters when specified process changes process group.
286 * We count the number of processes in each process group that "qualify"
287 * the group for terminal job control (those with a parent in a different
288 * process group of the same session). If that count reaches zero, the
289 * process group becomes orphaned. Check both the specified process'
290 * process group and that of its children.
291 * entering == 0 => p is leaving specified group.
292 * entering == 1 => p is entering specified group.
293 */
294void
77153250 295fixjobc(struct proc *p, struct pgrp *pgrp, int entering)
984263bc 296{
1fd87d54
RG
297 struct pgrp *hispgrp;
298 struct session *mysession = pgrp->pg_session;
984263bc
MD
299
300 /*
301 * Check p's parent to see whether p qualifies its own process
302 * group; if so, adjust count for p's process group.
303 */
304 if ((hispgrp = p->p_pptr->p_pgrp) != pgrp &&
305 hispgrp->pg_session == mysession) {
306 if (entering)
307 pgrp->pg_jobc++;
308 else if (--pgrp->pg_jobc == 0)
309 orphanpg(pgrp);
310 }
311
312 /*
313 * Check this process' children to see whether they qualify
314 * their process groups; if so, adjust counts for children's
315 * process groups.
316 */
317 LIST_FOREACH(p, &p->p_children, p_sibling)
318 if ((hispgrp = p->p_pgrp) != pgrp &&
319 hispgrp->pg_session == mysession &&
f026f3b0 320 (p->p_flag & P_ZOMBIE) == 0) {
984263bc
MD
321 if (entering)
322 hispgrp->pg_jobc++;
323 else if (--hispgrp->pg_jobc == 0)
324 orphanpg(hispgrp);
325 }
326}
327
328/*
329 * A process group has become orphaned;
330 * if there are any stopped processes in the group,
331 * hang-up all process in that group.
332 */
333static void
77153250 334orphanpg(struct pgrp *pg)
984263bc 335{
1fd87d54 336 struct proc *p;
984263bc
MD
337
338 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
344ad853 339 if (p->p_flag & P_STOPPED) {
984263bc
MD
340 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
341 psignal(p, SIGHUP);
342 psignal(p, SIGCONT);
343 }
344 return;
345 }
346 }
347}
348
5bf0d9b5
MD
349/*
350 * Called from exit1 to remove a process from the allproc
351 * list and move it to the zombie list.
352 *
353 * MPSAFE
354 */
355void
356proc_move_allproc_zombie(struct proc *p)
357{
358 spin_lock_wr(&allproc_spin);
359 while (p->p_lock) {
360 spin_unlock_wr(&allproc_spin);
361 tsleep(p, 0, "reap1", hz / 10);
362 spin_lock_wr(&allproc_spin);
363 }
364 LIST_REMOVE(p, p_list);
365 LIST_INSERT_HEAD(&zombproc, p, p_list);
366 LIST_REMOVE(p, p_hash);
367 p->p_flag |= P_ZOMBIE;
368 spin_unlock_wr(&allproc_spin);
369}
370
371/*
372 * This routine is called from kern_wait() and will remove the process
373 * from the zombie list and the sibling list. This routine will block
374 * if someone has a lock on the proces (p_lock).
375 *
376 * MPSAFE
377 */
378void
379proc_remove_zombie(struct proc *p)
380{
381 spin_lock_wr(&allproc_spin);
382 while (p->p_lock) {
383 spin_unlock_wr(&allproc_spin);
384 tsleep(p, 0, "reap1", hz / 10);
385 spin_lock_wr(&allproc_spin);
386 }
387 LIST_REMOVE(p, p_list); /* off zombproc */
388 LIST_REMOVE(p, p_sibling);
389 spin_unlock_wr(&allproc_spin);
390}
391
392/*
393 * Scan all processes on the allproc list. The process is automatically
394 * held for the callback. A return value of -1 terminates the loop.
395 *
396 * MPSAFE
397 */
398void
399allproc_scan(int (*callback)(struct proc *, void *), void *data)
400{
401 struct proc *p;
402 int r;
403
404 spin_lock_rd(&allproc_spin);
405 LIST_FOREACH(p, &allproc, p_list) {
406 PHOLD(p);
407 spin_unlock_rd(&allproc_spin);
408 r = callback(p, data);
409 spin_lock_rd(&allproc_spin);
410 PRELE(p);
411 if (r < 0)
412 break;
413 }
414 spin_unlock_rd(&allproc_spin);
415}
416
417/*
418 * Scan all processes on the zombproc list. The process is automatically
419 * held for the callback. A return value of -1 terminates the loop.
420 *
421 * MPSAFE
422 */
423void
424zombproc_scan(int (*callback)(struct proc *, void *), void *data)
425{
426 struct proc *p;
427 int r;
428
429 spin_lock_rd(&allproc_spin);
430 LIST_FOREACH(p, &zombproc, p_list) {
431 PHOLD(p);
432 spin_unlock_rd(&allproc_spin);
433 r = callback(p, data);
434 spin_lock_rd(&allproc_spin);
435 PRELE(p);
436 if (r < 0)
437 break;
438 }
439 spin_unlock_rd(&allproc_spin);
440}
441
984263bc
MD
442#include "opt_ddb.h"
443#ifdef DDB
444#include <ddb/ddb.h>
445
446DB_SHOW_COMMAND(pgrpdump, pgrpdump)
447{
1fd87d54
RG
448 struct pgrp *pgrp;
449 struct proc *p;
450 int i;
984263bc
MD
451
452 for (i = 0; i <= pgrphash; i++) {
453 if (!LIST_EMPTY(&pgrphashtbl[i])) {
454 printf("\tindx %d\n", i);
455 LIST_FOREACH(pgrp, &pgrphashtbl[i], pg_hash) {
456 printf(
457 "\tpgrp %p, pgid %ld, sess %p, sesscnt %d, mem %p\n",
458 (void *)pgrp, (long)pgrp->pg_id,
459 (void *)pgrp->pg_session,
460 pgrp->pg_session->s_count,
461 (void *)LIST_FIRST(&pgrp->pg_members));
462 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
463 printf("\t\tpid %ld addr %p pgrp %p\n",
464 (long)p->p_pid, (void *)p,
465 (void *)p->p_pgrp);
466 }
467 }
468 }
469 }
470}
471#endif /* DDB */
472
73e4f7b9
MD
473/*
474 * Fill in an eproc structure for the specified thread.
475 */
476void
477fill_eproc_td(thread_t td, struct eproc *ep, struct proc *xp)
478{
479 bzero(ep, sizeof(*ep));
480
481 ep->e_uticks = td->td_uticks;
482 ep->e_sticks = td->td_sticks;
483 ep->e_iticks = td->td_iticks;
484 ep->e_tdev = NOUDEV;
a72187e9 485 ep->e_cpuid = td->td_gd->gd_cpuid;
73e4f7b9
MD
486 if (td->td_wmesg) {
487 strncpy(ep->e_wmesg, td->td_wmesg, WMESGLEN);
488 ep->e_wmesg[WMESGLEN] = 0;
489 }
490
491 /*
492 * Fake up portions of the proc structure copied out by the sysctl
493 * to return useful information. Note that using td_pri directly
494 * is messy because it includes critial section data so we fake
495 * up an rtprio.prio for threads.
496 */
497 if (xp) {
498 *xp = *initproc;
499 xp->p_rtprio.type = RTP_PRIO_THREAD;
500 xp->p_rtprio.prio = td->td_pri & TDPRI_MASK;
501 xp->p_pid = -1;
502 }
503}
504
984263bc
MD
505/*
506 * Fill in an eproc structure for the specified process.
507 */
508void
73e4f7b9 509fill_eproc(struct proc *p, struct eproc *ep)
984263bc 510{
73e4f7b9 511 struct tty *tp;
984263bc 512
73e4f7b9 513 fill_eproc_td(p->p_thread, ep, NULL);
984263bc
MD
514
515 ep->e_paddr = p;
41c20dac
MD
516 if (p->p_ucred) {
517 ep->e_ucred = *p->p_ucred;
984263bc
MD
518 }
519 if (p->p_procsig) {
520 ep->e_procsig = *p->p_procsig;
521 }
f026f3b0
MD
522 if (p->p_stat != SIDL && (p->p_flag & P_ZOMBIE) == 0 &&
523 p->p_vmspace != NULL) {
1fd87d54 524 struct vmspace *vm = p->p_vmspace;
984263bc
MD
525 ep->e_vm = *vm;
526 ep->e_vm.vm_rssize = vmspace_resident_count(vm); /*XXX*/
527 }
344ad853 528 if ((p->p_flag & P_SWAPPEDOUT) == 0 && p->p_stats)
984263bc
MD
529 ep->e_stats = *p->p_stats;
530 if (p->p_pptr)
531 ep->e_ppid = p->p_pptr->p_pid;
532 if (p->p_pgrp) {
533 ep->e_pgid = p->p_pgrp->pg_id;
534 ep->e_jobc = p->p_pgrp->pg_jobc;
535 ep->e_sess = p->p_pgrp->pg_session;
536
537 if (ep->e_sess) {
538 bcopy(ep->e_sess->s_login, ep->e_login, sizeof(ep->e_login));
539 if (ep->e_sess->s_ttyvp)
540 ep->e_flag = EPROC_CTTY;
541 if (p->p_session && SESS_LEADER(p))
542 ep->e_flag |= EPROC_SLEADER;
543 }
544 }
545 if ((p->p_flag & P_CONTROLT) &&
546 (ep->e_sess != NULL) &&
547 ((tp = ep->e_sess->s_ttyp) != NULL)) {
548 ep->e_tdev = dev2udev(tp->t_dev);
549 ep->e_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID;
550 ep->e_tsess = tp->t_session;
73e4f7b9 551 } else {
984263bc 552 ep->e_tdev = NOUDEV;
984263bc 553 }
35048f4d
JS
554 if (p->p_ucred->cr_prison)
555 ep->e_jailid = p->p_ucred->cr_prison->pr_id;
984263bc
MD
556}
557
5bf0d9b5
MD
558/*
559 * Locate a process on the zombie list. Return a held process or NULL.
560 */
984263bc
MD
561struct proc *
562zpfind(pid_t pid)
563{
564 struct proc *p;
565
566 LIST_FOREACH(p, &zombproc, p_list)
567 if (p->p_pid == pid)
568 return (p);
569 return (NULL);
570}
571
984263bc 572static int
73e4f7b9 573sysctl_out_proc(struct proc *p, struct thread *td, struct sysctl_req *req, int doingzomb)
984263bc
MD
574{
575 struct eproc eproc;
73e4f7b9 576 struct proc xproc;
984263bc 577 int error;
73e4f7b9 578#if 0
984263bc 579 pid_t pid = p->p_pid;
73e4f7b9
MD
580#endif
581
582 if (p) {
583 td = p->p_thread;
584 fill_eproc(p, &eproc);
585 xproc = *p;
d9d6cb99
MD
586
587 /*
344ad853
MD
588 * p_stat fixup. If we are in a thread sleep mark p_stat
589 * as sleeping if the thread is blocked.
d9d6cb99 590 */
8ec60c3f 591 if (p->p_stat == SRUN && td && (td->td_flags & TDF_BLOCKED)) {
d9d6cb99
MD
592 xproc.p_stat = SSLEEP;
593 }
344ad853
MD
594 /*
595 * If the process is being stopped but is in a normal tsleep,
596 * mark it as being SSTOP.
597 */
598 if (p->p_stat == SSLEEP && (p->p_flag & P_STOPPED))
599 xproc.p_stat = SSTOP;
f026f3b0
MD
600 if (p->p_flag & P_ZOMBIE)
601 xproc.p_stat = SZOMB;
73e4f7b9
MD
602 } else if (td) {
603 fill_eproc_td(td, &eproc, &xproc);
604 }
605 error = SYSCTL_OUT(req,(caddr_t)&xproc, sizeof(struct proc));
984263bc
MD
606 if (error)
607 return (error);
608 error = SYSCTL_OUT(req,(caddr_t)&eproc, sizeof(eproc));
b8acf5a9
MD
609 if (error)
610 return (error);
73e4f7b9 611 error = SYSCTL_OUT(req,(caddr_t)td, sizeof(struct thread));
984263bc
MD
612 if (error)
613 return (error);
73e4f7b9 614#if 0
984263bc
MD
615 if (!doingzomb && pid && (pfind(pid) != p))
616 return EAGAIN;
617 if (doingzomb && zpfind(pid) != p)
618 return EAGAIN;
73e4f7b9 619#endif
984263bc
MD
620 return (0);
621}
622
623static int
624sysctl_kern_proc(SYSCTL_HANDLER_ARGS)
625{
626 int *name = (int*) arg1;
627 u_int namelen = arg2;
628 struct proc *p;
73e4f7b9 629 struct thread *td;
984263bc
MD
630 int doingzomb;
631 int error = 0;
630ccdeb
MD
632 int n;
633 int origcpu;
41c20dac 634 struct ucred *cr1 = curproc->p_ucred;
984263bc
MD
635
636 if (oidp->oid_number == KERN_PROC_PID) {
637 if (namelen != 1)
638 return (EINVAL);
639 p = pfind((pid_t)name[0]);
640 if (!p)
641 return (0);
41c20dac 642 if (!PRISON_CHECK(cr1, p->p_ucred))
984263bc 643 return (0);
73e4f7b9 644 error = sysctl_out_proc(p, NULL, req, 0);
984263bc
MD
645 return (error);
646 }
647 if (oidp->oid_number == KERN_PROC_ALL && !namelen)
648 ;
649 else if (oidp->oid_number != KERN_PROC_ALL && namelen == 1)
650 ;
651 else
652 return (EINVAL);
653
654 if (!req->oldptr) {
655 /* overestimate by 5 procs */
656 error = SYSCTL_OUT(req, 0, sizeof (struct kinfo_proc) * 5);
657 if (error)
658 return (error);
659 }
660 for (doingzomb=0 ; doingzomb < 2 ; doingzomb++) {
661 if (!doingzomb)
662 p = LIST_FIRST(&allproc);
663 else
664 p = LIST_FIRST(&zombproc);
665 for (; p != 0; p = LIST_NEXT(p, p_list)) {
666 /*
667 * Show a user only their processes.
668 */
41c20dac 669 if ((!ps_showallprocs) && p_trespass(cr1, p->p_ucred))
984263bc
MD
670 continue;
671 /*
672 * Skip embryonic processes.
673 */
674 if (p->p_stat == SIDL)
675 continue;
676 /*
677 * TODO - make more efficient (see notes below).
678 * do by session.
679 */
680 switch (oidp->oid_number) {
984263bc
MD
681 case KERN_PROC_PGRP:
682 /* could do this by traversing pgrp */
683 if (p->p_pgrp == NULL ||
684 p->p_pgrp->pg_id != (pid_t)name[0])
685 continue;
686 break;
687
688 case KERN_PROC_TTY:
689 if ((p->p_flag & P_CONTROLT) == 0 ||
690 p->p_session == NULL ||
691 p->p_session->s_ttyp == NULL ||
692 dev2udev(p->p_session->s_ttyp->t_dev) !=
693 (udev_t)name[0])
694 continue;
695 break;
696
697 case KERN_PROC_UID:
698 if (p->p_ucred == NULL ||
699 p->p_ucred->cr_uid != (uid_t)name[0])
700 continue;
701 break;
702
703 case KERN_PROC_RUID:
704 if (p->p_ucred == NULL ||
41c20dac 705 p->p_ucred->cr_ruid != (uid_t)name[0])
984263bc
MD
706 continue;
707 break;
708 }
709
41c20dac 710 if (!PRISON_CHECK(cr1, p->p_ucred))
984263bc 711 continue;
c008d3ad 712 PHOLD(p);
73e4f7b9 713 error = sysctl_out_proc(p, NULL, req, doingzomb);
c008d3ad 714 PRELE(p);
984263bc
MD
715 if (error)
716 return (error);
717 }
718 }
630ccdeb
MD
719
720 /*
721 * Iterate over all active cpus and scan their thread list. Start
722 * with the next logical cpu and end with our original cpu. We
723 * migrate our own thread to each target cpu in order to safely scan
724 * its thread list. In the last loop we migrate back to our original
725 * cpu.
726 */
727 origcpu = mycpu->gd_cpuid;
56c703bd
JS
728 if (!ps_showallthreads || jailed(cr1))
729 goto post_threads;
730 for (n = 1; n <= ncpus; ++n) {
630ccdeb
MD
731 globaldata_t rgd;
732 int nid;
733
734 nid = (origcpu + n) % ncpus;
735 if ((smp_active_mask & (1 << nid)) == 0)
736 continue;
737 rgd = globaldata_find(nid);
738 lwkt_setcpu_self(rgd);
630ccdeb 739
73e4f7b9
MD
740 TAILQ_FOREACH(td, &mycpu->gd_tdallq, td_allq) {
741 if (td->td_proc)
742 continue;
743 switch (oidp->oid_number) {
744 case KERN_PROC_PGRP:
745 case KERN_PROC_TTY:
746 case KERN_PROC_UID:
747 case KERN_PROC_RUID:
748 continue;
749 default:
750 break;
751 }
752 lwkt_hold(td);
753 error = sysctl_out_proc(NULL, td, req, doingzomb);
754 lwkt_rele(td);
755 if (error)
756 return (error);
757 }
758 }
56c703bd 759post_threads:
984263bc
MD
760 return (0);
761}
762
763/*
764 * This sysctl allows a process to retrieve the argument list or process
765 * title for another process without groping around in the address space
766 * of the other process. It also allow a process to set its own "process
767 * title to a string of its own choice.
768 */
769static int
770sysctl_kern_proc_args(SYSCTL_HANDLER_ARGS)
771{
772 int *name = (int*) arg1;
773 u_int namelen = arg2;
774 struct proc *p;
775 struct pargs *pa;
776 int error = 0;
41c20dac 777 struct ucred *cr1 = curproc->p_ucred;
984263bc
MD
778
779 if (namelen != 1)
780 return (EINVAL);
781
782 p = pfind((pid_t)name[0]);
783 if (!p)
784 return (0);
785
41c20dac 786 if ((!ps_argsopen) && p_trespass(cr1, p->p_ucred))
984263bc
MD
787 return (0);
788
789 if (req->newptr && curproc != p)
790 return (EPERM);
791
792 if (req->oldptr && p->p_args != NULL)
793 error = SYSCTL_OUT(req, p->p_args->ar_args, p->p_args->ar_length);
794 if (req->newptr == NULL)
795 return (error);
796
797 if (p->p_args && --p->p_args->ar_ref == 0)
798 FREE(p->p_args, M_PARGS);
799 p->p_args = NULL;
800
801 if (req->newlen + sizeof(struct pargs) > ps_arg_cache_limit)
802 return (error);
803
804 MALLOC(pa, struct pargs *, sizeof(struct pargs) + req->newlen,
805 M_PARGS, M_WAITOK);
806 pa->ar_ref = 1;
807 pa->ar_length = req->newlen;
808 error = SYSCTL_IN(req, pa->ar_args, req->newlen);
809 if (!error)
810 p->p_args = pa;
811 else
812 FREE(pa, M_PARGS);
813 return (error);
814}
815
816SYSCTL_NODE(_kern, KERN_PROC, proc, CTLFLAG_RD, 0, "Process table");
817
818SYSCTL_PROC(_kern_proc, KERN_PROC_ALL, all, CTLFLAG_RD|CTLTYPE_STRUCT,
819 0, 0, sysctl_kern_proc, "S,proc", "Return entire process table");
820
821SYSCTL_NODE(_kern_proc, KERN_PROC_PGRP, pgrp, CTLFLAG_RD,
822 sysctl_kern_proc, "Process table");
823
824SYSCTL_NODE(_kern_proc, KERN_PROC_TTY, tty, CTLFLAG_RD,
825 sysctl_kern_proc, "Process table");
826
827SYSCTL_NODE(_kern_proc, KERN_PROC_UID, uid, CTLFLAG_RD,
828 sysctl_kern_proc, "Process table");
829
830SYSCTL_NODE(_kern_proc, KERN_PROC_RUID, ruid, CTLFLAG_RD,
831 sysctl_kern_proc, "Process table");
832
833SYSCTL_NODE(_kern_proc, KERN_PROC_PID, pid, CTLFLAG_RD,
834 sysctl_kern_proc, "Process table");
835
836SYSCTL_NODE(_kern_proc, KERN_PROC_ARGS, args, CTLFLAG_RW | CTLFLAG_ANYBODY,
837 sysctl_kern_proc_args, "Process argument list");