FreeBSD/Linux Kernel Cross Reference
sys/kern/kern_proc.c
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 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * @(#)kern_proc.c 8.7 (Berkeley) 2/14/95
30 * $FreeBSD: releng/5.4/sys/kern/kern_proc.c 145335 2005-04-20 19:11:07Z cvs2svn $
31 */
32
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD: releng/5.4/sys/kern/kern_proc.c 145335 2005-04-20 19:11:07Z cvs2svn $");
35
36 #include "opt_ktrace.h"
37 #include "opt_kstack_pages.h"
38
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/lock.h>
43 #include <sys/malloc.h>
44 #include <sys/mutex.h>
45 #include <sys/proc.h>
46 #include <sys/sysent.h>
47 #include <sys/sched.h>
48 #include <sys/smp.h>
49 #include <sys/sysctl.h>
50 #include <sys/filedesc.h>
51 #include <sys/tty.h>
52 #include <sys/signalvar.h>
53 #include <sys/sx.h>
54 #include <sys/user.h>
55 #include <sys/jail.h>
56 #ifdef KTRACE
57 #include <sys/uio.h>
58 #include <sys/ktrace.h>
59 #endif
60
61 #include <vm/vm.h>
62 #include <vm/vm_extern.h>
63 #include <vm/pmap.h>
64 #include <vm/vm_map.h>
65 #include <vm/uma.h>
66 #include <machine/critical.h>
67
68 MALLOC_DEFINE(M_PGRP, "pgrp", "process group header");
69 MALLOC_DEFINE(M_SESSION, "session", "session header");
70 static MALLOC_DEFINE(M_PROC, "proc", "Proc structures");
71 MALLOC_DEFINE(M_SUBPROC, "subproc", "Proc sub-structures");
72
73 static void doenterpgrp(struct proc *, struct pgrp *);
74 static void orphanpg(struct pgrp *pg);
75 static void pgadjustjobc(struct pgrp *pgrp, int entering);
76 static void pgdelete(struct pgrp *);
77 static int proc_ctor(void *mem, int size, void *arg, int flags);
78 static void proc_dtor(void *mem, int size, void *arg);
79 static int proc_init(void *mem, int size, int flags);
80 static void proc_fini(void *mem, int size);
81
82 /*
83 * Other process lists
84 */
85 struct pidhashhead *pidhashtbl;
86 u_long pidhash;
87 struct pgrphashhead *pgrphashtbl;
88 u_long pgrphash;
89 struct proclist allproc;
90 struct proclist zombproc;
91 struct sx allproc_lock;
92 struct sx proctree_lock;
93 struct mtx pargs_ref_lock;
94 struct mtx ppeers_lock;
95 uma_zone_t proc_zone;
96 uma_zone_t ithread_zone;
97
98 int kstack_pages = KSTACK_PAGES;
99 SYSCTL_INT(_kern, OID_AUTO, kstack_pages, CTLFLAG_RD, &kstack_pages, 0, "");
100
101 CTASSERT(sizeof(struct kinfo_proc) == KINFO_PROC_SIZE);
102
103 /*
104 * Initialize global process hashing structures.
105 */
106 void
107 procinit()
108 {
109
110 sx_init(&allproc_lock, "allproc");
111 sx_init(&proctree_lock, "proctree");
112 mtx_init(&pargs_ref_lock, "struct pargs.ref", NULL, MTX_DEF);
113 mtx_init(&ppeers_lock, "p_peers", NULL, MTX_DEF);
114 LIST_INIT(&allproc);
115 LIST_INIT(&zombproc);
116 pidhashtbl = hashinit(maxproc / 4, M_PROC, &pidhash);
117 pgrphashtbl = hashinit(maxproc / 4, M_PROC, &pgrphash);
118 proc_zone = uma_zcreate("PROC", sched_sizeof_proc(),
119 proc_ctor, proc_dtor, proc_init, proc_fini,
120 UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
121 uihashinit();
122 }
123
124 /*
125 * Prepare a proc for use.
126 */
127 static int
128 proc_ctor(void *mem, int size, void *arg, int flags)
129 {
130 struct proc *p;
131
132 p = (struct proc *)mem;
133 return (0);
134 }
135
136 /*
137 * Reclaim a proc after use.
138 */
139 static void
140 proc_dtor(void *mem, int size, void *arg)
141 {
142 struct proc *p;
143 struct thread *td;
144 #ifdef INVARIANTS
145 struct ksegrp *kg;
146 #endif
147
148 /* INVARIANTS checks go here */
149 p = (struct proc *)mem;
150 td = FIRST_THREAD_IN_PROC(p);
151 #ifdef INVARIANTS
152 KASSERT((p->p_numthreads == 1),
153 ("bad number of threads in exiting process"));
154 KASSERT((td != NULL), ("proc_dtor: bad thread pointer"));
155 kg = FIRST_KSEGRP_IN_PROC(p);
156 KASSERT((kg != NULL), ("proc_dtor: bad kg pointer"));
157 #endif
158
159 /* Dispose of an alternate kstack, if it exists.
160 * XXX What if there are more than one thread in the proc?
161 * The first thread in the proc is special and not
162 * freed, so you gotta do this here.
163 */
164 if (((p->p_flag & P_KTHREAD) != 0) && (td->td_altkstack != 0))
165 vm_thread_dispose_altkstack(td);
166 }
167
168 /*
169 * Initialize type-stable parts of a proc (when newly created).
170 */
171 static int
172 proc_init(void *mem, int size, int flags)
173 {
174 struct proc *p;
175 struct thread *td;
176 struct ksegrp *kg;
177
178 p = (struct proc *)mem;
179 p->p_sched = (struct p_sched *)&p[1];
180 td = thread_alloc();
181 kg = ksegrp_alloc();
182 bzero(&p->p_mtx, sizeof(struct mtx));
183 mtx_init(&p->p_mtx, "process lock", NULL, MTX_DEF | MTX_DUPOK);
184 p->p_stats = pstats_alloc();
185 proc_linkup(p, kg, td);
186 sched_newproc(p, kg, td);
187 return (0);
188 }
189
190 /*
191 * Tear down type-stable parts of a proc (just before being discarded)
192 */
193 static void
194 proc_fini(void *mem, int size)
195 {
196 struct proc *p;
197 struct thread *td;
198 struct ksegrp *kg;
199
200 p = (struct proc *)mem;
201 KASSERT((p->p_numthreads == 1),
202 ("bad number of threads in freeing process"));
203 td = FIRST_THREAD_IN_PROC(p);
204 KASSERT((td != NULL), ("proc_fini: bad thread pointer"));
205 kg = FIRST_KSEGRP_IN_PROC(p);
206 KASSERT((kg != NULL), ("proc_fini: bad kg pointer"));
207 sched_destroyproc(p);
208 thread_free(td);
209 ksegrp_free(kg);
210 mtx_destroy(&p->p_mtx);
211 }
212
213 /*
214 * Is p an inferior of the current process?
215 */
216 int
217 inferior(p)
218 register struct proc *p;
219 {
220
221 sx_assert(&proctree_lock, SX_LOCKED);
222 for (; p != curproc; p = p->p_pptr)
223 if (p->p_pid == 0)
224 return (0);
225 return (1);
226 }
227
228 /*
229 * Locate a process by number; return only "live" processes -- i.e., neither
230 * zombies nor newly born but incompletely initialized processes. By not
231 * returning processes in the PRS_NEW state, we allow callers to avoid
232 * testing for that condition to avoid dereferencing p_ucred, et al.
233 */
234 struct proc *
235 pfind(pid)
236 register pid_t pid;
237 {
238 register struct proc *p;
239
240 sx_slock(&allproc_lock);
241 LIST_FOREACH(p, PIDHASH(pid), p_hash)
242 if (p->p_pid == pid) {
243 if (p->p_state == PRS_NEW) {
244 p = NULL;
245 break;
246 }
247 PROC_LOCK(p);
248 break;
249 }
250 sx_sunlock(&allproc_lock);
251 return (p);
252 }
253
254 /*
255 * Locate a process group by number.
256 * The caller must hold proctree_lock.
257 */
258 struct pgrp *
259 pgfind(pgid)
260 register pid_t pgid;
261 {
262 register struct pgrp *pgrp;
263
264 sx_assert(&proctree_lock, SX_LOCKED);
265
266 LIST_FOREACH(pgrp, PGRPHASH(pgid), pg_hash) {
267 if (pgrp->pg_id == pgid) {
268 PGRP_LOCK(pgrp);
269 return (pgrp);
270 }
271 }
272 return (NULL);
273 }
274
275 /*
276 * Create a new process group.
277 * pgid must be equal to the pid of p.
278 * Begin a new session if required.
279 */
280 int
281 enterpgrp(p, pgid, pgrp, sess)
282 register struct proc *p;
283 pid_t pgid;
284 struct pgrp *pgrp;
285 struct session *sess;
286 {
287 struct pgrp *pgrp2;
288
289 sx_assert(&proctree_lock, SX_XLOCKED);
290
291 KASSERT(pgrp != NULL, ("enterpgrp: pgrp == NULL"));
292 KASSERT(p->p_pid == pgid,
293 ("enterpgrp: new pgrp and pid != pgid"));
294
295 pgrp2 = pgfind(pgid);
296
297 KASSERT(pgrp2 == NULL,
298 ("enterpgrp: pgrp with pgid exists"));
299 KASSERT(!SESS_LEADER(p),
300 ("enterpgrp: session leader attempted setpgrp"));
301
302 mtx_init(&pgrp->pg_mtx, "process group", NULL, MTX_DEF | MTX_DUPOK);
303
304 if (sess != NULL) {
305 /*
306 * new session
307 */
308 mtx_init(&sess->s_mtx, "session", NULL, MTX_DEF);
309 PROC_LOCK(p);
310 p->p_flag &= ~P_CONTROLT;
311 PROC_UNLOCK(p);
312 PGRP_LOCK(pgrp);
313 sess->s_leader = p;
314 sess->s_sid = p->p_pid;
315 sess->s_count = 1;
316 sess->s_ttyvp = NULL;
317 sess->s_ttyp = NULL;
318 bcopy(p->p_session->s_login, sess->s_login,
319 sizeof(sess->s_login));
320 pgrp->pg_session = sess;
321 KASSERT(p == curproc,
322 ("enterpgrp: mksession and p != curproc"));
323 } else {
324 pgrp->pg_session = p->p_session;
325 SESS_LOCK(pgrp->pg_session);
326 pgrp->pg_session->s_count++;
327 SESS_UNLOCK(pgrp->pg_session);
328 PGRP_LOCK(pgrp);
329 }
330 pgrp->pg_id = pgid;
331 LIST_INIT(&pgrp->pg_members);
332
333 /*
334 * As we have an exclusive lock of proctree_lock,
335 * this should not deadlock.
336 */
337 LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash);
338 pgrp->pg_jobc = 0;
339 SLIST_INIT(&pgrp->pg_sigiolst);
340 PGRP_UNLOCK(pgrp);
341
342 doenterpgrp(p, pgrp);
343
344 return (0);
345 }
346
347 /*
348 * Move p to an existing process group
349 */
350 int
351 enterthispgrp(p, pgrp)
352 register struct proc *p;
353 struct pgrp *pgrp;
354 {
355
356 sx_assert(&proctree_lock, SX_XLOCKED);
357 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
358 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
359 PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED);
360 SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED);
361 KASSERT(pgrp->pg_session == p->p_session,
362 ("%s: pgrp's session %p, p->p_session %p.\n",
363 __func__,
364 pgrp->pg_session,
365 p->p_session));
366 KASSERT(pgrp != p->p_pgrp,
367 ("%s: p belongs to pgrp.", __func__));
368
369 doenterpgrp(p, pgrp);
370
371 return (0);
372 }
373
374 /*
375 * Move p to a process group
376 */
377 static void
378 doenterpgrp(p, pgrp)
379 struct proc *p;
380 struct pgrp *pgrp;
381 {
382 struct pgrp *savepgrp;
383
384 sx_assert(&proctree_lock, SX_XLOCKED);
385 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
386 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
387 PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED);
388 SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED);
389
390 savepgrp = p->p_pgrp;
391
392 /*
393 * Adjust eligibility of affected pgrps to participate in job control.
394 * Increment eligibility counts before decrementing, otherwise we
395 * could reach 0 spuriously during the first call.
396 */
397 fixjobc(p, pgrp, 1);
398 fixjobc(p, p->p_pgrp, 0);
399
400 PGRP_LOCK(pgrp);
401 PGRP_LOCK(savepgrp);
402 PROC_LOCK(p);
403 LIST_REMOVE(p, p_pglist);
404 p->p_pgrp = pgrp;
405 PROC_UNLOCK(p);
406 LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist);
407 PGRP_UNLOCK(savepgrp);
408 PGRP_UNLOCK(pgrp);
409 if (LIST_EMPTY(&savepgrp->pg_members))
410 pgdelete(savepgrp);
411 }
412
413 /*
414 * remove process from process group
415 */
416 int
417 leavepgrp(p)
418 register struct proc *p;
419 {
420 struct pgrp *savepgrp;
421
422 sx_assert(&proctree_lock, SX_XLOCKED);
423 savepgrp = p->p_pgrp;
424 PGRP_LOCK(savepgrp);
425 PROC_LOCK(p);
426 LIST_REMOVE(p, p_pglist);
427 p->p_pgrp = NULL;
428 PROC_UNLOCK(p);
429 PGRP_UNLOCK(savepgrp);
430 if (LIST_EMPTY(&savepgrp->pg_members))
431 pgdelete(savepgrp);
432 return (0);
433 }
434
435 /*
436 * delete a process group
437 */
438 static void
439 pgdelete(pgrp)
440 register struct pgrp *pgrp;
441 {
442 struct session *savesess;
443 int i;
444
445 sx_assert(&proctree_lock, SX_XLOCKED);
446 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
447 SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED);
448
449 /*
450 * Reset any sigio structures pointing to us as a result of
451 * F_SETOWN with our pgid.
452 */
453 funsetownlst(&pgrp->pg_sigiolst);
454
455 PGRP_LOCK(pgrp);
456 if (pgrp->pg_session->s_ttyp != NULL &&
457 pgrp->pg_session->s_ttyp->t_pgrp == pgrp)
458 pgrp->pg_session->s_ttyp->t_pgrp = NULL;
459 LIST_REMOVE(pgrp, pg_hash);
460 savesess = pgrp->pg_session;
461 SESS_LOCK(savesess);
462 i = --savesess->s_count;
463 SESS_UNLOCK(savesess);
464 PGRP_UNLOCK(pgrp);
465 if (i == 0) {
466 if (savesess->s_ttyp != NULL)
467 ttyrel(savesess->s_ttyp);
468 mtx_destroy(&savesess->s_mtx);
469 FREE(savesess, M_SESSION);
470 }
471 mtx_destroy(&pgrp->pg_mtx);
472 FREE(pgrp, M_PGRP);
473 }
474
475 static void
476 pgadjustjobc(pgrp, entering)
477 struct pgrp *pgrp;
478 int entering;
479 {
480
481 PGRP_LOCK(pgrp);
482 if (entering)
483 pgrp->pg_jobc++;
484 else {
485 --pgrp->pg_jobc;
486 if (pgrp->pg_jobc == 0)
487 orphanpg(pgrp);
488 }
489 PGRP_UNLOCK(pgrp);
490 }
491
492 /*
493 * Adjust pgrp jobc counters when specified process changes process group.
494 * We count the number of processes in each process group that "qualify"
495 * the group for terminal job control (those with a parent in a different
496 * process group of the same session). If that count reaches zero, the
497 * process group becomes orphaned. Check both the specified process'
498 * process group and that of its children.
499 * entering == 0 => p is leaving specified group.
500 * entering == 1 => p is entering specified group.
501 */
502 void
503 fixjobc(p, pgrp, entering)
504 register struct proc *p;
505 register struct pgrp *pgrp;
506 int entering;
507 {
508 register struct pgrp *hispgrp;
509 register struct session *mysession;
510
511 sx_assert(&proctree_lock, SX_LOCKED);
512 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
513 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
514 SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED);
515
516 /*
517 * Check p's parent to see whether p qualifies its own process
518 * group; if so, adjust count for p's process group.
519 */
520 mysession = pgrp->pg_session;
521 if ((hispgrp = p->p_pptr->p_pgrp) != pgrp &&
522 hispgrp->pg_session == mysession)
523 pgadjustjobc(pgrp, entering);
524
525 /*
526 * Check this process' children to see whether they qualify
527 * their process groups; if so, adjust counts for children's
528 * process groups.
529 */
530 LIST_FOREACH(p, &p->p_children, p_sibling) {
531 hispgrp = p->p_pgrp;
532 if (hispgrp == pgrp ||
533 hispgrp->pg_session != mysession)
534 continue;
535 PROC_LOCK(p);
536 if (p->p_state == PRS_ZOMBIE) {
537 PROC_UNLOCK(p);
538 continue;
539 }
540 PROC_UNLOCK(p);
541 pgadjustjobc(hispgrp, entering);
542 }
543 }
544
545 /*
546 * A process group has become orphaned;
547 * if there are any stopped processes in the group,
548 * hang-up all process in that group.
549 */
550 static void
551 orphanpg(pg)
552 struct pgrp *pg;
553 {
554 register struct proc *p;
555
556 PGRP_LOCK_ASSERT(pg, MA_OWNED);
557
558 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
559 PROC_LOCK(p);
560 if (P_SHOULDSTOP(p)) {
561 PROC_UNLOCK(p);
562 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
563 PROC_LOCK(p);
564 psignal(p, SIGHUP);
565 psignal(p, SIGCONT);
566 PROC_UNLOCK(p);
567 }
568 return;
569 }
570 PROC_UNLOCK(p);
571 }
572 }
573
574 #include "opt_ddb.h"
575 #ifdef DDB
576 #include <ddb/ddb.h>
577
578 DB_SHOW_COMMAND(pgrpdump, pgrpdump)
579 {
580 register struct pgrp *pgrp;
581 register struct proc *p;
582 register int i;
583
584 for (i = 0; i <= pgrphash; i++) {
585 if (!LIST_EMPTY(&pgrphashtbl[i])) {
586 printf("\tindx %d\n", i);
587 LIST_FOREACH(pgrp, &pgrphashtbl[i], pg_hash) {
588 printf(
589 "\tpgrp %p, pgid %ld, sess %p, sesscnt %d, mem %p\n",
590 (void *)pgrp, (long)pgrp->pg_id,
591 (void *)pgrp->pg_session,
592 pgrp->pg_session->s_count,
593 (void *)LIST_FIRST(&pgrp->pg_members));
594 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
595 printf("\t\tpid %ld addr %p pgrp %p\n",
596 (long)p->p_pid, (void *)p,
597 (void *)p->p_pgrp);
598 }
599 }
600 }
601 }
602 }
603 #endif /* DDB */
604 void
605 fill_kinfo_thread(struct thread *td, struct kinfo_proc *kp);
606
607 /*
608 * Fill in a kinfo_proc structure for the specified process.
609 * Must be called with the target process locked.
610 */
611 void
612 fill_kinfo_proc(struct proc *p, struct kinfo_proc *kp)
613 {
614 fill_kinfo_thread(FIRST_THREAD_IN_PROC(p), kp);
615 }
616
617 void
618 fill_kinfo_thread(struct thread *td, struct kinfo_proc *kp)
619 {
620 struct proc *p;
621 struct thread *td0;
622 struct ksegrp *kg;
623 struct tty *tp;
624 struct session *sp;
625 struct timeval tv;
626 struct ucred *cred;
627 struct sigacts *ps;
628
629 p = td->td_proc;
630
631 bzero(kp, sizeof(*kp));
632
633 kp->ki_structsize = sizeof(*kp);
634 kp->ki_paddr = p;
635 PROC_LOCK_ASSERT(p, MA_OWNED);
636 kp->ki_addr =/* p->p_addr; */0; /* XXXKSE */
637 kp->ki_args = p->p_args;
638 kp->ki_textvp = p->p_textvp;
639 #ifdef KTRACE
640 kp->ki_tracep = p->p_tracevp;
641 mtx_lock(&ktrace_mtx);
642 kp->ki_traceflag = p->p_traceflag;
643 mtx_unlock(&ktrace_mtx);
644 #endif
645 kp->ki_fd = p->p_fd;
646 kp->ki_vmspace = p->p_vmspace;
647 kp->ki_flag = p->p_flag;
648 cred = p->p_ucred;
649 if (cred) {
650 kp->ki_uid = cred->cr_uid;
651 kp->ki_ruid = cred->cr_ruid;
652 kp->ki_svuid = cred->cr_svuid;
653 /* XXX bde doesn't like KI_NGROUPS */
654 kp->ki_ngroups = min(cred->cr_ngroups, KI_NGROUPS);
655 bcopy(cred->cr_groups, kp->ki_groups,
656 kp->ki_ngroups * sizeof(gid_t));
657 kp->ki_rgid = cred->cr_rgid;
658 kp->ki_svgid = cred->cr_svgid;
659 /* If jailed(cred), emulate the old P_JAILED flag. */
660 if (jailed(cred))
661 kp->ki_flag |= P_JAILED;
662 }
663 ps = p->p_sigacts;
664 if (ps) {
665 mtx_lock(&ps->ps_mtx);
666 kp->ki_sigignore = ps->ps_sigignore;
667 kp->ki_sigcatch = ps->ps_sigcatch;
668 mtx_unlock(&ps->ps_mtx);
669 }
670 mtx_lock_spin(&sched_lock);
671 if (p->p_state != PRS_NEW &&
672 p->p_state != PRS_ZOMBIE &&
673 p->p_vmspace != NULL) {
674 struct vmspace *vm = p->p_vmspace;
675
676 kp->ki_size = vm->vm_map.size;
677 kp->ki_rssize = vmspace_resident_count(vm); /*XXX*/
678 FOREACH_THREAD_IN_PROC(p, td0) {
679 if (!TD_IS_SWAPPED(td0))
680 kp->ki_rssize += td0->td_kstack_pages;
681 if (td0->td_altkstack_obj != NULL)
682 kp->ki_rssize += td0->td_altkstack_pages;
683 }
684 kp->ki_swrss = vm->vm_swrss;
685 kp->ki_tsize = vm->vm_tsize;
686 kp->ki_dsize = vm->vm_dsize;
687 kp->ki_ssize = vm->vm_ssize;
688 }
689 if ((p->p_sflag & PS_INMEM) && p->p_stats) {
690 kp->ki_start = p->p_stats->p_start;
691 timevaladd(&kp->ki_start, &boottime);
692 kp->ki_rusage = p->p_stats->p_ru;
693 calcru(p, &kp->ki_rusage.ru_utime, &kp->ki_rusage.ru_stime,
694 NULL);
695 kp->ki_childstime = p->p_stats->p_cru.ru_stime;
696 kp->ki_childutime = p->p_stats->p_cru.ru_utime;
697 /* Some callers want child-times in a single value */
698 kp->ki_childtime = kp->ki_childstime;
699 timevaladd(&kp->ki_childtime, &kp->ki_childutime);
700 }
701 kp->ki_sflag = p->p_sflag;
702 kp->ki_swtime = p->p_swtime;
703 kp->ki_pid = p->p_pid;
704 kp->ki_nice = p->p_nice;
705 bintime2timeval(&p->p_runtime, &tv);
706 kp->ki_runtime = tv.tv_sec * (u_int64_t)1000000 + tv.tv_usec;
707 if (p->p_state != PRS_ZOMBIE) {
708 #if 0
709 if (td == NULL) {
710 /* XXXKSE: This should never happen. */
711 printf("fill_kinfo_proc(): pid %d has no threads!\n",
712 p->p_pid);
713 mtx_unlock_spin(&sched_lock);
714 return;
715 }
716 #endif
717 if (td->td_wmesg != NULL) {
718 strlcpy(kp->ki_wmesg, td->td_wmesg,
719 sizeof(kp->ki_wmesg));
720 }
721 if (TD_ON_LOCK(td)) {
722 kp->ki_kiflag |= KI_LOCKBLOCK;
723 strlcpy(kp->ki_lockname, td->td_lockname,
724 sizeof(kp->ki_lockname));
725 }
726
727 if (p->p_state == PRS_NORMAL) { /* XXXKSE very approximate */
728 if (TD_ON_RUNQ(td) ||
729 TD_CAN_RUN(td) ||
730 TD_IS_RUNNING(td)) {
731 kp->ki_stat = SRUN;
732 } else if (P_SHOULDSTOP(p)) {
733 kp->ki_stat = SSTOP;
734 } else if (TD_IS_SLEEPING(td)) {
735 kp->ki_stat = SSLEEP;
736 } else if (TD_ON_LOCK(td)) {
737 kp->ki_stat = SLOCK;
738 } else {
739 kp->ki_stat = SWAIT;
740 }
741 } else {
742 kp->ki_stat = SIDL;
743 }
744
745 kg = td->td_ksegrp;
746
747 /* things in the KSE GROUP */
748 kp->ki_estcpu = kg->kg_estcpu;
749 kp->ki_slptime = kg->kg_slptime;
750 kp->ki_pri.pri_user = kg->kg_user_pri;
751 kp->ki_pri.pri_class = kg->kg_pri_class;
752
753 /* Things in the thread */
754 kp->ki_wchan = td->td_wchan;
755 kp->ki_pri.pri_level = td->td_priority;
756 kp->ki_pri.pri_native = td->td_base_pri;
757 kp->ki_lastcpu = td->td_lastcpu;
758 kp->ki_oncpu = td->td_oncpu;
759 kp->ki_tdflags = td->td_flags;
760 kp->ki_tid = td->td_tid;
761 kp->ki_numthreads = p->p_numthreads;
762 kp->ki_pcb = td->td_pcb;
763 kp->ki_kstack = (void *)td->td_kstack;
764 kp->ki_pctcpu = sched_pctcpu(td);
765
766 /* We can't get this anymore but ps etc never used it anyway. */
767 kp->ki_rqindex = 0;
768
769 } else {
770 kp->ki_stat = SZOMB;
771 }
772 mtx_unlock_spin(&sched_lock);
773 tp = NULL;
774 if (p->p_pgrp) {
775 kp->ki_pgid = p->p_pgrp->pg_id;
776 kp->ki_jobc = p->p_pgrp->pg_jobc;
777 sp = p->p_pgrp->pg_session;
778
779 if (sp != NULL) {
780 kp->ki_sid = sp->s_sid;
781 SESS_LOCK(sp);
782 strlcpy(kp->ki_login, sp->s_login,
783 sizeof(kp->ki_login));
784 if (sp->s_ttyvp)
785 kp->ki_kiflag |= KI_CTTY;
786 if (SESS_LEADER(p))
787 kp->ki_kiflag |= KI_SLEADER;
788 tp = sp->s_ttyp;
789 SESS_UNLOCK(sp);
790 }
791 }
792 if ((p->p_flag & P_CONTROLT) && tp != NULL) {
793 kp->ki_tdev = dev2udev(tp->t_dev);
794 kp->ki_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID;
795 if (tp->t_session)
796 kp->ki_tsid = tp->t_session->s_sid;
797 } else
798 kp->ki_tdev = NODEV;
799 if (p->p_comm[0] != '\0') {
800 strlcpy(kp->ki_comm, p->p_comm, sizeof(kp->ki_comm));
801 strlcpy(kp->ki_ocomm, p->p_comm, sizeof(kp->ki_ocomm));
802 }
803 if (p->p_sysent && p->p_sysent->sv_name != NULL &&
804 p->p_sysent->sv_name[0] != '\0')
805 strlcpy(kp->ki_emul, p->p_sysent->sv_name, sizeof(kp->ki_emul));
806 kp->ki_siglist = p->p_siglist;
807 SIGSETOR(kp->ki_siglist, td->td_siglist);
808 kp->ki_sigmask = td->td_sigmask;
809 kp->ki_xstat = p->p_xstat;
810 kp->ki_acflag = p->p_acflag;
811 kp->ki_lock = p->p_lock;
812 if (p->p_pptr)
813 kp->ki_ppid = p->p_pptr->p_pid;
814 }
815
816 struct pstats *
817 pstats_alloc(void)
818 {
819
820 return (malloc(sizeof(struct pstats), M_SUBPROC, M_ZERO|M_WAITOK));
821 }
822
823 /*
824 * Copy parts of p_stats; zero the rest of p_stats (statistics).
825 */
826 void
827 pstats_fork(struct pstats *src, struct pstats *dst)
828 {
829
830 bzero(&dst->pstat_startzero,
831 __rangeof(struct pstats, pstat_startzero, pstat_endzero));
832 bcopy(&src->pstat_startcopy, &dst->pstat_startcopy,
833 __rangeof(struct pstats, pstat_startcopy, pstat_endcopy));
834 }
835
836 void
837 pstats_free(struct pstats *ps)
838 {
839
840 free(ps, M_SUBPROC);
841 }
842
843 /*
844 * Locate a zombie process by number
845 */
846 struct proc *
847 zpfind(pid_t pid)
848 {
849 struct proc *p;
850
851 sx_slock(&allproc_lock);
852 LIST_FOREACH(p, &zombproc, p_list)
853 if (p->p_pid == pid) {
854 PROC_LOCK(p);
855 break;
856 }
857 sx_sunlock(&allproc_lock);
858 return (p);
859 }
860
861 #define KERN_PROC_ZOMBMASK 0x3
862 #define KERN_PROC_NOTHREADS 0x4
863
864 /*
865 * Must be called with the process locked and will return with it unlocked.
866 */
867 static int
868 sysctl_out_proc(struct proc *p, struct sysctl_req *req, int flags)
869 {
870 struct thread *td;
871 struct kinfo_proc kinfo_proc;
872 int error = 0;
873 struct proc *np;
874 pid_t pid = p->p_pid;
875
876 PROC_LOCK_ASSERT(p, MA_OWNED);
877
878 if (flags & KERN_PROC_NOTHREADS) {
879 fill_kinfo_proc(p, &kinfo_proc);
880 PROC_UNLOCK(p);
881 error = SYSCTL_OUT(req, (caddr_t)&kinfo_proc,
882 sizeof(kinfo_proc));
883 PROC_LOCK(p);
884 } else {
885 _PHOLD(p);
886 FOREACH_THREAD_IN_PROC(p, td) {
887 fill_kinfo_thread(td, &kinfo_proc);
888 PROC_UNLOCK(p);
889 error = SYSCTL_OUT(req, (caddr_t)&kinfo_proc,
890 sizeof(kinfo_proc));
891 PROC_LOCK(p);
892 if (error)
893 break;
894 }
895 _PRELE(p);
896 }
897 PROC_UNLOCK(p);
898 if (error)
899 return (error);
900 if (flags & KERN_PROC_ZOMBMASK)
901 np = zpfind(pid);
902 else {
903 if (pid == 0)
904 return (0);
905 np = pfind(pid);
906 }
907 if (np == NULL)
908 return EAGAIN;
909 if (np != p) {
910 PROC_UNLOCK(np);
911 return EAGAIN;
912 }
913 PROC_UNLOCK(np);
914 return (0);
915 }
916
917 static int
918 sysctl_kern_proc(SYSCTL_HANDLER_ARGS)
919 {
920 int *name = (int*) arg1;
921 u_int namelen = arg2;
922 struct proc *p;
923 int flags, doingzomb, oid_number;
924 int error = 0;
925
926 oid_number = oidp->oid_number;
927 if (oid_number != KERN_PROC_ALL &&
928 (oid_number & KERN_PROC_INC_THREAD) == 0)
929 flags = KERN_PROC_NOTHREADS;
930 else {
931 flags = 0;
932 oid_number &= ~KERN_PROC_INC_THREAD;
933 }
934 if (oid_number == KERN_PROC_PID) {
935 if (namelen != 1)
936 return (EINVAL);
937 p = pfind((pid_t)name[0]);
938 if (!p)
939 return (ESRCH);
940 if ((error = p_cansee(curthread, p))) {
941 PROC_UNLOCK(p);
942 return (error);
943 }
944 error = sysctl_out_proc(p, req, flags);
945 return (error);
946 }
947
948 switch (oid_number) {
949 case KERN_PROC_ALL:
950 if (namelen != 0)
951 return (EINVAL);
952 break;
953 case KERN_PROC_PROC:
954 if (namelen != 0 && namelen != 1)
955 return (EINVAL);
956 break;
957 default:
958 if (namelen != 1)
959 return (EINVAL);
960 break;
961 }
962
963 if (!req->oldptr) {
964 /* overestimate by 5 procs */
965 error = SYSCTL_OUT(req, 0, sizeof (struct kinfo_proc) * 5);
966 if (error)
967 return (error);
968 }
969 error = sysctl_wire_old_buffer(req, 0);
970 if (error != 0)
971 return (error);
972 sx_slock(&allproc_lock);
973 for (doingzomb=0 ; doingzomb < 2 ; doingzomb++) {
974 if (!doingzomb)
975 p = LIST_FIRST(&allproc);
976 else
977 p = LIST_FIRST(&zombproc);
978 for (; p != 0; p = LIST_NEXT(p, p_list)) {
979 /*
980 * Skip embryonic processes.
981 */
982 mtx_lock_spin(&sched_lock);
983 if (p->p_state == PRS_NEW) {
984 mtx_unlock_spin(&sched_lock);
985 continue;
986 }
987 mtx_unlock_spin(&sched_lock);
988 PROC_LOCK(p);
989 /*
990 * Show a user only appropriate processes.
991 */
992 if (p_cansee(curthread, p)) {
993 PROC_UNLOCK(p);
994 continue;
995 }
996 /*
997 * TODO - make more efficient (see notes below).
998 * do by session.
999 */
1000 switch (oid_number) {
1001
1002 case KERN_PROC_GID:
1003 if (p->p_ucred == NULL ||
1004 p->p_ucred->cr_gid != (gid_t)name[0]) {
1005 PROC_UNLOCK(p);
1006 continue;
1007 }
1008 break;
1009
1010 case KERN_PROC_PGRP:
1011 /* could do this by traversing pgrp */
1012 if (p->p_pgrp == NULL ||
1013 p->p_pgrp->pg_id != (pid_t)name[0]) {
1014 PROC_UNLOCK(p);
1015 continue;
1016 }
1017 break;
1018
1019 case KERN_PROC_RGID:
1020 if (p->p_ucred == NULL ||
1021 p->p_ucred->cr_rgid != (gid_t)name[0]) {
1022 PROC_UNLOCK(p);
1023 continue;
1024 }
1025 break;
1026
1027 case KERN_PROC_SESSION:
1028 if (p->p_session == NULL ||
1029 p->p_session->s_sid != (pid_t)name[0]) {
1030 PROC_UNLOCK(p);
1031 continue;
1032 }
1033 break;
1034
1035 case KERN_PROC_TTY:
1036 if ((p->p_flag & P_CONTROLT) == 0 ||
1037 p->p_session == NULL) {
1038 PROC_UNLOCK(p);
1039 continue;
1040 }
1041 SESS_LOCK(p->p_session);
1042 if (p->p_session->s_ttyp == NULL ||
1043 dev2udev(p->p_session->s_ttyp->t_dev) !=
1044 (dev_t)name[0]) {
1045 SESS_UNLOCK(p->p_session);
1046 PROC_UNLOCK(p);
1047 continue;
1048 }
1049 SESS_UNLOCK(p->p_session);
1050 break;
1051
1052 case KERN_PROC_UID:
1053 if (p->p_ucred == NULL ||
1054 p->p_ucred->cr_uid != (uid_t)name[0]) {
1055 PROC_UNLOCK(p);
1056 continue;
1057 }
1058 break;
1059
1060 case KERN_PROC_RUID:
1061 if (p->p_ucred == NULL ||
1062 p->p_ucred->cr_ruid != (uid_t)name[0]) {
1063 PROC_UNLOCK(p);
1064 continue;
1065 }
1066 break;
1067
1068 case KERN_PROC_PROC:
1069 break;
1070
1071 default:
1072 break;
1073
1074 }
1075
1076 error = sysctl_out_proc(p, req, flags | doingzomb);
1077 if (error) {
1078 sx_sunlock(&allproc_lock);
1079 return (error);
1080 }
1081 }
1082 }
1083 sx_sunlock(&allproc_lock);
1084 return (0);
1085 }
1086
1087 struct pargs *
1088 pargs_alloc(int len)
1089 {
1090 struct pargs *pa;
1091
1092 MALLOC(pa, struct pargs *, sizeof(struct pargs) + len, M_PARGS,
1093 M_WAITOK);
1094 pa->ar_ref = 1;
1095 pa->ar_length = len;
1096 return (pa);
1097 }
1098
1099 void
1100 pargs_free(struct pargs *pa)
1101 {
1102
1103 FREE(pa, M_PARGS);
1104 }
1105
1106 void
1107 pargs_hold(struct pargs *pa)
1108 {
1109
1110 if (pa == NULL)
1111 return;
1112 PARGS_LOCK(pa);
1113 pa->ar_ref++;
1114 PARGS_UNLOCK(pa);
1115 }
1116
1117 void
1118 pargs_drop(struct pargs *pa)
1119 {
1120
1121 if (pa == NULL)
1122 return;
1123 PARGS_LOCK(pa);
1124 if (--pa->ar_ref == 0) {
1125 PARGS_UNLOCK(pa);
1126 pargs_free(pa);
1127 } else
1128 PARGS_UNLOCK(pa);
1129 }
1130
1131 /*
1132 * This sysctl allows a process to retrieve the argument list or process
1133 * title for another process without groping around in the address space
1134 * of the other process. It also allow a process to set its own "process
1135 * title to a string of its own choice.
1136 */
1137 static int
1138 sysctl_kern_proc_args(SYSCTL_HANDLER_ARGS)
1139 {
1140 int *name = (int*) arg1;
1141 u_int namelen = arg2;
1142 struct pargs *newpa, *pa;
1143 struct proc *p;
1144 int error = 0;
1145
1146 if (namelen != 1)
1147 return (EINVAL);
1148
1149 p = pfind((pid_t)name[0]);
1150 if (!p)
1151 return (ESRCH);
1152
1153 if ((error = p_cansee(curthread, p)) != 0) {
1154 PROC_UNLOCK(p);
1155 return (error);
1156 }
1157
1158 if (req->newptr && curproc != p) {
1159 PROC_UNLOCK(p);
1160 return (EPERM);
1161 }
1162
1163 pa = p->p_args;
1164 pargs_hold(pa);
1165 PROC_UNLOCK(p);
1166 if (req->oldptr != NULL && pa != NULL)
1167 error = SYSCTL_OUT(req, pa->ar_args, pa->ar_length);
1168 pargs_drop(pa);
1169 if (error != 0 || req->newptr == NULL)
1170 return (error);
1171
1172 if (req->newlen + sizeof(struct pargs) > ps_arg_cache_limit)
1173 return (ENOMEM);
1174 newpa = pargs_alloc(req->newlen);
1175 error = SYSCTL_IN(req, newpa->ar_args, req->newlen);
1176 if (error != 0) {
1177 pargs_free(newpa);
1178 return (error);
1179 }
1180 PROC_LOCK(p);
1181 pa = p->p_args;
1182 p->p_args = newpa;
1183 PROC_UNLOCK(p);
1184 pargs_drop(pa);
1185 return (0);
1186 }
1187
1188 static int
1189 sysctl_kern_proc_sv_name(SYSCTL_HANDLER_ARGS)
1190 {
1191 struct proc *p;
1192 char *sv_name;
1193 int *name;
1194 int namelen;
1195 int error;
1196
1197 namelen = arg2;
1198 if (namelen != 1)
1199 return (EINVAL);
1200
1201 name = (int *)arg1;
1202 if ((p = pfind((pid_t)name[0])) == NULL)
1203 return (ESRCH);
1204 if ((error = p_cansee(curthread, p))) {
1205 PROC_UNLOCK(p);
1206 return (error);
1207 }
1208 sv_name = p->p_sysent->sv_name;
1209 PROC_UNLOCK(p);
1210 return (sysctl_handle_string(oidp, sv_name, 0, req));
1211 }
1212
1213
1214 SYSCTL_NODE(_kern, KERN_PROC, proc, CTLFLAG_RD, 0, "Process table");
1215
1216 SYSCTL_PROC(_kern_proc, KERN_PROC_ALL, all, CTLFLAG_RD|CTLTYPE_STRUCT,
1217 0, 0, sysctl_kern_proc, "S,proc", "Return entire process table");
1218
1219 SYSCTL_NODE(_kern_proc, KERN_PROC_GID, gid, CTLFLAG_RD,
1220 sysctl_kern_proc, "Process table");
1221
1222 SYSCTL_NODE(_kern_proc, KERN_PROC_PGRP, pgrp, CTLFLAG_RD,
1223 sysctl_kern_proc, "Process table");
1224
1225 SYSCTL_NODE(_kern_proc, KERN_PROC_RGID, rgid, CTLFLAG_RD,
1226 sysctl_kern_proc, "Process table");
1227
1228 SYSCTL_NODE(_kern_proc, KERN_PROC_SESSION, sid, CTLFLAG_RD,
1229 sysctl_kern_proc, "Process table");
1230
1231 SYSCTL_NODE(_kern_proc, KERN_PROC_TTY, tty, CTLFLAG_RD,
1232 sysctl_kern_proc, "Process table");
1233
1234 SYSCTL_NODE(_kern_proc, KERN_PROC_UID, uid, CTLFLAG_RD,
1235 sysctl_kern_proc, "Process table");
1236
1237 SYSCTL_NODE(_kern_proc, KERN_PROC_RUID, ruid, CTLFLAG_RD,
1238 sysctl_kern_proc, "Process table");
1239
1240 SYSCTL_NODE(_kern_proc, KERN_PROC_PID, pid, CTLFLAG_RD,
1241 sysctl_kern_proc, "Process table");
1242
1243 SYSCTL_NODE(_kern_proc, KERN_PROC_PROC, proc, CTLFLAG_RD,
1244 sysctl_kern_proc, "Return process table, no threads");
1245
1246 SYSCTL_NODE(_kern_proc, KERN_PROC_ARGS, args, CTLFLAG_RW | CTLFLAG_ANYBODY,
1247 sysctl_kern_proc_args, "Process argument list");
1248
1249 SYSCTL_NODE(_kern_proc, KERN_PROC_SV_NAME, sv_name, CTLFLAG_RD,
1250 sysctl_kern_proc_sv_name, "Process syscall vector name (ABI type)");
1251
1252 SYSCTL_NODE(_kern_proc, (KERN_PROC_GID | KERN_PROC_INC_THREAD), gid_td,
1253 CTLFLAG_RD, sysctl_kern_proc, "Process table");
1254
1255 SYSCTL_NODE(_kern_proc, (KERN_PROC_PGRP | KERN_PROC_INC_THREAD), pgrp_td,
1256 CTLFLAG_RD, sysctl_kern_proc, "Process table");
1257
1258 SYSCTL_NODE(_kern_proc, (KERN_PROC_RGID | KERN_PROC_INC_THREAD), rgid_td,
1259 CTLFLAG_RD, sysctl_kern_proc, "Process table");
1260
1261 SYSCTL_NODE(_kern_proc, (KERN_PROC_SESSION | KERN_PROC_INC_THREAD), sid_td,
1262 CTLFLAG_RD, sysctl_kern_proc, "Process table");
1263
1264 SYSCTL_NODE(_kern_proc, (KERN_PROC_TTY | KERN_PROC_INC_THREAD), tty_td,
1265 CTLFLAG_RD, sysctl_kern_proc, "Process table");
1266
1267 SYSCTL_NODE(_kern_proc, (KERN_PROC_UID | KERN_PROC_INC_THREAD), uid_td,
1268 CTLFLAG_RD, sysctl_kern_proc, "Process table");
1269
1270 SYSCTL_NODE(_kern_proc, (KERN_PROC_RUID | KERN_PROC_INC_THREAD), ruid_td,
1271 CTLFLAG_RD, sysctl_kern_proc, "Process table");
1272
1273 SYSCTL_NODE(_kern_proc, (KERN_PROC_PID | KERN_PROC_INC_THREAD), pid_td,
1274 CTLFLAG_RD, sysctl_kern_proc, "Process table");
1275
1276 SYSCTL_NODE(_kern_proc, (KERN_PROC_PROC | KERN_PROC_INC_THREAD), proc_td,
1277 CTLFLAG_RD, sysctl_kern_proc, "Return process table, no threads");
Cache object: 0b4aadb86f0252f8454f423613306a65
|