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 */
31
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD: releng/10.2/sys/kern/kern_proc.c 279926 2015-03-12 16:05:52Z kib $");
34
35 #include "opt_compat.h"
36 #include "opt_ddb.h"
37 #include "opt_kdtrace.h"
38 #include "opt_ktrace.h"
39 #include "opt_kstack_pages.h"
40 #include "opt_stack.h"
41
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/elf.h>
45 #include <sys/exec.h>
46 #include <sys/kernel.h>
47 #include <sys/limits.h>
48 #include <sys/lock.h>
49 #include <sys/loginclass.h>
50 #include <sys/malloc.h>
51 #include <sys/mman.h>
52 #include <sys/mount.h>
53 #include <sys/mutex.h>
54 #include <sys/proc.h>
55 #include <sys/ptrace.h>
56 #include <sys/refcount.h>
57 #include <sys/resourcevar.h>
58 #include <sys/rwlock.h>
59 #include <sys/sbuf.h>
60 #include <sys/sysent.h>
61 #include <sys/sched.h>
62 #include <sys/smp.h>
63 #include <sys/stack.h>
64 #include <sys/stat.h>
65 #include <sys/sysctl.h>
66 #include <sys/filedesc.h>
67 #include <sys/tty.h>
68 #include <sys/signalvar.h>
69 #include <sys/sdt.h>
70 #include <sys/sx.h>
71 #include <sys/user.h>
72 #include <sys/jail.h>
73 #include <sys/vnode.h>
74 #include <sys/eventhandler.h>
75
76 #ifdef DDB
77 #include <ddb/ddb.h>
78 #endif
79
80 #include <vm/vm.h>
81 #include <vm/vm_param.h>
82 #include <vm/vm_extern.h>
83 #include <vm/pmap.h>
84 #include <vm/vm_map.h>
85 #include <vm/vm_object.h>
86 #include <vm/vm_page.h>
87 #include <vm/uma.h>
88
89 #ifdef COMPAT_FREEBSD32
90 #include <compat/freebsd32/freebsd32.h>
91 #include <compat/freebsd32/freebsd32_util.h>
92 #endif
93
94 SDT_PROVIDER_DEFINE(proc);
95 SDT_PROBE_DEFINE4(proc, kernel, ctor, entry, "struct proc *", "int",
96 "void *", "int");
97 SDT_PROBE_DEFINE4(proc, kernel, ctor, return, "struct proc *", "int",
98 "void *", "int");
99 SDT_PROBE_DEFINE4(proc, kernel, dtor, entry, "struct proc *", "int",
100 "void *", "struct thread *");
101 SDT_PROBE_DEFINE3(proc, kernel, dtor, return, "struct proc *", "int",
102 "void *");
103 SDT_PROBE_DEFINE3(proc, kernel, init, entry, "struct proc *", "int",
104 "int");
105 SDT_PROBE_DEFINE3(proc, kernel, init, return, "struct proc *", "int",
106 "int");
107
108 MALLOC_DEFINE(M_PGRP, "pgrp", "process group header");
109 MALLOC_DEFINE(M_SESSION, "session", "session header");
110 static MALLOC_DEFINE(M_PROC, "proc", "Proc structures");
111 MALLOC_DEFINE(M_SUBPROC, "subproc", "Proc sub-structures");
112
113 static void doenterpgrp(struct proc *, struct pgrp *);
114 static void orphanpg(struct pgrp *pg);
115 static void fill_kinfo_aggregate(struct proc *p, struct kinfo_proc *kp);
116 static void fill_kinfo_proc_only(struct proc *p, struct kinfo_proc *kp);
117 static void fill_kinfo_thread(struct thread *td, struct kinfo_proc *kp,
118 int preferthread);
119 static void pgadjustjobc(struct pgrp *pgrp, int entering);
120 static void pgdelete(struct pgrp *);
121 static int proc_ctor(void *mem, int size, void *arg, int flags);
122 static void proc_dtor(void *mem, int size, void *arg);
123 static int proc_init(void *mem, int size, int flags);
124 static void proc_fini(void *mem, int size);
125 static void pargs_free(struct pargs *pa);
126 static struct proc *zpfind_locked(pid_t pid);
127
128 /*
129 * Other process lists
130 */
131 struct pidhashhead *pidhashtbl;
132 u_long pidhash;
133 struct pgrphashhead *pgrphashtbl;
134 u_long pgrphash;
135 struct proclist allproc;
136 struct proclist zombproc;
137 struct sx allproc_lock;
138 struct sx proctree_lock;
139 struct mtx ppeers_lock;
140 uma_zone_t proc_zone;
141
142 int kstack_pages = KSTACK_PAGES;
143 SYSCTL_INT(_kern, OID_AUTO, kstack_pages, CTLFLAG_RD, &kstack_pages, 0,
144 "Kernel stack size in pages");
145 static int vmmap_skip_res_cnt = 0;
146 SYSCTL_INT(_kern, OID_AUTO, proc_vmmap_skip_resident_count, CTLFLAG_RW,
147 &vmmap_skip_res_cnt, 0,
148 "Skip calculation of the pages resident count in kern.proc.vmmap");
149
150 CTASSERT(sizeof(struct kinfo_proc) == KINFO_PROC_SIZE);
151 #ifdef COMPAT_FREEBSD32
152 CTASSERT(sizeof(struct kinfo_proc32) == KINFO_PROC32_SIZE);
153 #endif
154
155 /*
156 * Initialize global process hashing structures.
157 */
158 void
159 procinit()
160 {
161
162 sx_init(&allproc_lock, "allproc");
163 sx_init(&proctree_lock, "proctree");
164 mtx_init(&ppeers_lock, "p_peers", NULL, MTX_DEF);
165 LIST_INIT(&allproc);
166 LIST_INIT(&zombproc);
167 pidhashtbl = hashinit(maxproc / 4, M_PROC, &pidhash);
168 pgrphashtbl = hashinit(maxproc / 4, M_PROC, &pgrphash);
169 proc_zone = uma_zcreate("PROC", sched_sizeof_proc(),
170 proc_ctor, proc_dtor, proc_init, proc_fini,
171 UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
172 uihashinit();
173 }
174
175 /*
176 * Prepare a proc for use.
177 */
178 static int
179 proc_ctor(void *mem, int size, void *arg, int flags)
180 {
181 struct proc *p;
182
183 p = (struct proc *)mem;
184 SDT_PROBE(proc, kernel, ctor , entry, p, size, arg, flags, 0);
185 EVENTHANDLER_INVOKE(process_ctor, p);
186 SDT_PROBE(proc, kernel, ctor , return, p, size, arg, flags, 0);
187 return (0);
188 }
189
190 /*
191 * Reclaim a proc after use.
192 */
193 static void
194 proc_dtor(void *mem, int size, void *arg)
195 {
196 struct proc *p;
197 struct thread *td;
198
199 /* INVARIANTS checks go here */
200 p = (struct proc *)mem;
201 td = FIRST_THREAD_IN_PROC(p);
202 SDT_PROBE(proc, kernel, dtor, entry, p, size, arg, td, 0);
203 if (td != NULL) {
204 #ifdef INVARIANTS
205 KASSERT((p->p_numthreads == 1),
206 ("bad number of threads in exiting process"));
207 KASSERT(STAILQ_EMPTY(&p->p_ktr), ("proc_dtor: non-empty p_ktr"));
208 #endif
209 /* Free all OSD associated to this thread. */
210 osd_thread_exit(td);
211 }
212 EVENTHANDLER_INVOKE(process_dtor, p);
213 if (p->p_ksi != NULL)
214 KASSERT(! KSI_ONQ(p->p_ksi), ("SIGCHLD queue"));
215 SDT_PROBE(proc, kernel, dtor, return, p, size, arg, 0, 0);
216 }
217
218 /*
219 * Initialize type-stable parts of a proc (when newly created).
220 */
221 static int
222 proc_init(void *mem, int size, int flags)
223 {
224 struct proc *p;
225
226 p = (struct proc *)mem;
227 SDT_PROBE(proc, kernel, init, entry, p, size, flags, 0, 0);
228 p->p_sched = (struct p_sched *)&p[1];
229 bzero(&p->p_mtx, sizeof(struct mtx));
230 mtx_init(&p->p_mtx, "process lock", NULL, MTX_DEF | MTX_DUPOK);
231 mtx_init(&p->p_slock, "process slock", NULL, MTX_SPIN | MTX_RECURSE);
232 cv_init(&p->p_pwait, "ppwait");
233 cv_init(&p->p_dbgwait, "dbgwait");
234 TAILQ_INIT(&p->p_threads); /* all threads in proc */
235 EVENTHANDLER_INVOKE(process_init, p);
236 p->p_stats = pstats_alloc();
237 SDT_PROBE(proc, kernel, init, return, p, size, flags, 0, 0);
238 return (0);
239 }
240
241 /*
242 * UMA should ensure that this function is never called.
243 * Freeing a proc structure would violate type stability.
244 */
245 static void
246 proc_fini(void *mem, int size)
247 {
248 #ifdef notnow
249 struct proc *p;
250
251 p = (struct proc *)mem;
252 EVENTHANDLER_INVOKE(process_fini, p);
253 pstats_free(p->p_stats);
254 thread_free(FIRST_THREAD_IN_PROC(p));
255 mtx_destroy(&p->p_mtx);
256 if (p->p_ksi != NULL)
257 ksiginfo_free(p->p_ksi);
258 #else
259 panic("proc reclaimed");
260 #endif
261 }
262
263 /*
264 * Is p an inferior of the current process?
265 */
266 int
267 inferior(struct proc *p)
268 {
269
270 sx_assert(&proctree_lock, SX_LOCKED);
271 PROC_LOCK_ASSERT(p, MA_OWNED);
272 for (; p != curproc; p = proc_realparent(p)) {
273 if (p->p_pid == 0)
274 return (0);
275 }
276 return (1);
277 }
278
279 struct proc *
280 pfind_locked(pid_t pid)
281 {
282 struct proc *p;
283
284 sx_assert(&allproc_lock, SX_LOCKED);
285 LIST_FOREACH(p, PIDHASH(pid), p_hash) {
286 if (p->p_pid == pid) {
287 PROC_LOCK(p);
288 if (p->p_state == PRS_NEW) {
289 PROC_UNLOCK(p);
290 p = NULL;
291 }
292 break;
293 }
294 }
295 return (p);
296 }
297
298 /*
299 * Locate a process by number; return only "live" processes -- i.e., neither
300 * zombies nor newly born but incompletely initialized processes. By not
301 * returning processes in the PRS_NEW state, we allow callers to avoid
302 * testing for that condition to avoid dereferencing p_ucred, et al.
303 */
304 struct proc *
305 pfind(pid_t pid)
306 {
307 struct proc *p;
308
309 sx_slock(&allproc_lock);
310 p = pfind_locked(pid);
311 sx_sunlock(&allproc_lock);
312 return (p);
313 }
314
315 static struct proc *
316 pfind_tid_locked(pid_t tid)
317 {
318 struct proc *p;
319 struct thread *td;
320
321 sx_assert(&allproc_lock, SX_LOCKED);
322 FOREACH_PROC_IN_SYSTEM(p) {
323 PROC_LOCK(p);
324 if (p->p_state == PRS_NEW) {
325 PROC_UNLOCK(p);
326 continue;
327 }
328 FOREACH_THREAD_IN_PROC(p, td) {
329 if (td->td_tid == tid)
330 goto found;
331 }
332 PROC_UNLOCK(p);
333 }
334 found:
335 return (p);
336 }
337
338 /*
339 * Locate a process group by number.
340 * The caller must hold proctree_lock.
341 */
342 struct pgrp *
343 pgfind(pgid)
344 register pid_t pgid;
345 {
346 register struct pgrp *pgrp;
347
348 sx_assert(&proctree_lock, SX_LOCKED);
349
350 LIST_FOREACH(pgrp, PGRPHASH(pgid), pg_hash) {
351 if (pgrp->pg_id == pgid) {
352 PGRP_LOCK(pgrp);
353 return (pgrp);
354 }
355 }
356 return (NULL);
357 }
358
359 /*
360 * Locate process and do additional manipulations, depending on flags.
361 */
362 int
363 pget(pid_t pid, int flags, struct proc **pp)
364 {
365 struct proc *p;
366 int error;
367
368 sx_slock(&allproc_lock);
369 if (pid <= PID_MAX) {
370 p = pfind_locked(pid);
371 if (p == NULL && (flags & PGET_NOTWEXIT) == 0)
372 p = zpfind_locked(pid);
373 } else if ((flags & PGET_NOTID) == 0) {
374 p = pfind_tid_locked(pid);
375 } else {
376 p = NULL;
377 }
378 sx_sunlock(&allproc_lock);
379 if (p == NULL)
380 return (ESRCH);
381 if ((flags & PGET_CANSEE) != 0) {
382 error = p_cansee(curthread, p);
383 if (error != 0)
384 goto errout;
385 }
386 if ((flags & PGET_CANDEBUG) != 0) {
387 error = p_candebug(curthread, p);
388 if (error != 0)
389 goto errout;
390 }
391 if ((flags & PGET_ISCURRENT) != 0 && curproc != p) {
392 error = EPERM;
393 goto errout;
394 }
395 if ((flags & PGET_NOTWEXIT) != 0 && (p->p_flag & P_WEXIT) != 0) {
396 error = ESRCH;
397 goto errout;
398 }
399 if ((flags & PGET_NOTINEXEC) != 0 && (p->p_flag & P_INEXEC) != 0) {
400 /*
401 * XXXRW: Not clear ESRCH is the right error during proc
402 * execve().
403 */
404 error = ESRCH;
405 goto errout;
406 }
407 if ((flags & PGET_HOLD) != 0) {
408 _PHOLD(p);
409 PROC_UNLOCK(p);
410 }
411 *pp = p;
412 return (0);
413 errout:
414 PROC_UNLOCK(p);
415 return (error);
416 }
417
418 /*
419 * Create a new process group.
420 * pgid must be equal to the pid of p.
421 * Begin a new session if required.
422 */
423 int
424 enterpgrp(p, pgid, pgrp, sess)
425 register struct proc *p;
426 pid_t pgid;
427 struct pgrp *pgrp;
428 struct session *sess;
429 {
430
431 sx_assert(&proctree_lock, SX_XLOCKED);
432
433 KASSERT(pgrp != NULL, ("enterpgrp: pgrp == NULL"));
434 KASSERT(p->p_pid == pgid,
435 ("enterpgrp: new pgrp and pid != pgid"));
436 KASSERT(pgfind(pgid) == NULL,
437 ("enterpgrp: pgrp with pgid exists"));
438 KASSERT(!SESS_LEADER(p),
439 ("enterpgrp: session leader attempted setpgrp"));
440
441 mtx_init(&pgrp->pg_mtx, "process group", NULL, MTX_DEF | MTX_DUPOK);
442
443 if (sess != NULL) {
444 /*
445 * new session
446 */
447 mtx_init(&sess->s_mtx, "session", NULL, MTX_DEF);
448 PROC_LOCK(p);
449 p->p_flag &= ~P_CONTROLT;
450 PROC_UNLOCK(p);
451 PGRP_LOCK(pgrp);
452 sess->s_leader = p;
453 sess->s_sid = p->p_pid;
454 refcount_init(&sess->s_count, 1);
455 sess->s_ttyvp = NULL;
456 sess->s_ttydp = NULL;
457 sess->s_ttyp = NULL;
458 bcopy(p->p_session->s_login, sess->s_login,
459 sizeof(sess->s_login));
460 pgrp->pg_session = sess;
461 KASSERT(p == curproc,
462 ("enterpgrp: mksession and p != curproc"));
463 } else {
464 pgrp->pg_session = p->p_session;
465 sess_hold(pgrp->pg_session);
466 PGRP_LOCK(pgrp);
467 }
468 pgrp->pg_id = pgid;
469 LIST_INIT(&pgrp->pg_members);
470
471 /*
472 * As we have an exclusive lock of proctree_lock,
473 * this should not deadlock.
474 */
475 LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash);
476 pgrp->pg_jobc = 0;
477 SLIST_INIT(&pgrp->pg_sigiolst);
478 PGRP_UNLOCK(pgrp);
479
480 doenterpgrp(p, pgrp);
481
482 return (0);
483 }
484
485 /*
486 * Move p to an existing process group
487 */
488 int
489 enterthispgrp(p, pgrp)
490 register struct proc *p;
491 struct pgrp *pgrp;
492 {
493
494 sx_assert(&proctree_lock, SX_XLOCKED);
495 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
496 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
497 PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED);
498 SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED);
499 KASSERT(pgrp->pg_session == p->p_session,
500 ("%s: pgrp's session %p, p->p_session %p.\n",
501 __func__,
502 pgrp->pg_session,
503 p->p_session));
504 KASSERT(pgrp != p->p_pgrp,
505 ("%s: p belongs to pgrp.", __func__));
506
507 doenterpgrp(p, pgrp);
508
509 return (0);
510 }
511
512 /*
513 * Move p to a process group
514 */
515 static void
516 doenterpgrp(p, pgrp)
517 struct proc *p;
518 struct pgrp *pgrp;
519 {
520 struct pgrp *savepgrp;
521
522 sx_assert(&proctree_lock, SX_XLOCKED);
523 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
524 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
525 PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED);
526 SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED);
527
528 savepgrp = p->p_pgrp;
529
530 /*
531 * Adjust eligibility of affected pgrps to participate in job control.
532 * Increment eligibility counts before decrementing, otherwise we
533 * could reach 0 spuriously during the first call.
534 */
535 fixjobc(p, pgrp, 1);
536 fixjobc(p, p->p_pgrp, 0);
537
538 PGRP_LOCK(pgrp);
539 PGRP_LOCK(savepgrp);
540 PROC_LOCK(p);
541 LIST_REMOVE(p, p_pglist);
542 p->p_pgrp = pgrp;
543 PROC_UNLOCK(p);
544 LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist);
545 PGRP_UNLOCK(savepgrp);
546 PGRP_UNLOCK(pgrp);
547 if (LIST_EMPTY(&savepgrp->pg_members))
548 pgdelete(savepgrp);
549 }
550
551 /*
552 * remove process from process group
553 */
554 int
555 leavepgrp(p)
556 register struct proc *p;
557 {
558 struct pgrp *savepgrp;
559
560 sx_assert(&proctree_lock, SX_XLOCKED);
561 savepgrp = p->p_pgrp;
562 PGRP_LOCK(savepgrp);
563 PROC_LOCK(p);
564 LIST_REMOVE(p, p_pglist);
565 p->p_pgrp = NULL;
566 PROC_UNLOCK(p);
567 PGRP_UNLOCK(savepgrp);
568 if (LIST_EMPTY(&savepgrp->pg_members))
569 pgdelete(savepgrp);
570 return (0);
571 }
572
573 /*
574 * delete a process group
575 */
576 static void
577 pgdelete(pgrp)
578 register struct pgrp *pgrp;
579 {
580 struct session *savesess;
581 struct tty *tp;
582
583 sx_assert(&proctree_lock, SX_XLOCKED);
584 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
585 SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED);
586
587 /*
588 * Reset any sigio structures pointing to us as a result of
589 * F_SETOWN with our pgid.
590 */
591 funsetownlst(&pgrp->pg_sigiolst);
592
593 PGRP_LOCK(pgrp);
594 tp = pgrp->pg_session->s_ttyp;
595 LIST_REMOVE(pgrp, pg_hash);
596 savesess = pgrp->pg_session;
597 PGRP_UNLOCK(pgrp);
598
599 /* Remove the reference to the pgrp before deallocating it. */
600 if (tp != NULL) {
601 tty_lock(tp);
602 tty_rel_pgrp(tp, pgrp);
603 }
604
605 mtx_destroy(&pgrp->pg_mtx);
606 free(pgrp, M_PGRP);
607 sess_release(savesess);
608 }
609
610 static void
611 pgadjustjobc(pgrp, entering)
612 struct pgrp *pgrp;
613 int entering;
614 {
615
616 PGRP_LOCK(pgrp);
617 if (entering)
618 pgrp->pg_jobc++;
619 else {
620 --pgrp->pg_jobc;
621 if (pgrp->pg_jobc == 0)
622 orphanpg(pgrp);
623 }
624 PGRP_UNLOCK(pgrp);
625 }
626
627 /*
628 * Adjust pgrp jobc counters when specified process changes process group.
629 * We count the number of processes in each process group that "qualify"
630 * the group for terminal job control (those with a parent in a different
631 * process group of the same session). If that count reaches zero, the
632 * process group becomes orphaned. Check both the specified process'
633 * process group and that of its children.
634 * entering == 0 => p is leaving specified group.
635 * entering == 1 => p is entering specified group.
636 */
637 void
638 fixjobc(p, pgrp, entering)
639 register struct proc *p;
640 register struct pgrp *pgrp;
641 int entering;
642 {
643 register struct pgrp *hispgrp;
644 register struct session *mysession;
645
646 sx_assert(&proctree_lock, SX_LOCKED);
647 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
648 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
649 SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED);
650
651 /*
652 * Check p's parent to see whether p qualifies its own process
653 * group; if so, adjust count for p's process group.
654 */
655 mysession = pgrp->pg_session;
656 if ((hispgrp = p->p_pptr->p_pgrp) != pgrp &&
657 hispgrp->pg_session == mysession)
658 pgadjustjobc(pgrp, entering);
659
660 /*
661 * Check this process' children to see whether they qualify
662 * their process groups; if so, adjust counts for children's
663 * process groups.
664 */
665 LIST_FOREACH(p, &p->p_children, p_sibling) {
666 hispgrp = p->p_pgrp;
667 if (hispgrp == pgrp ||
668 hispgrp->pg_session != mysession)
669 continue;
670 PROC_LOCK(p);
671 if (p->p_state == PRS_ZOMBIE) {
672 PROC_UNLOCK(p);
673 continue;
674 }
675 PROC_UNLOCK(p);
676 pgadjustjobc(hispgrp, entering);
677 }
678 }
679
680 /*
681 * A process group has become orphaned;
682 * if there are any stopped processes in the group,
683 * hang-up all process in that group.
684 */
685 static void
686 orphanpg(pg)
687 struct pgrp *pg;
688 {
689 register struct proc *p;
690
691 PGRP_LOCK_ASSERT(pg, MA_OWNED);
692
693 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
694 PROC_LOCK(p);
695 if (P_SHOULDSTOP(p)) {
696 PROC_UNLOCK(p);
697 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
698 PROC_LOCK(p);
699 kern_psignal(p, SIGHUP);
700 kern_psignal(p, SIGCONT);
701 PROC_UNLOCK(p);
702 }
703 return;
704 }
705 PROC_UNLOCK(p);
706 }
707 }
708
709 void
710 sess_hold(struct session *s)
711 {
712
713 refcount_acquire(&s->s_count);
714 }
715
716 void
717 sess_release(struct session *s)
718 {
719
720 if (refcount_release(&s->s_count)) {
721 if (s->s_ttyp != NULL) {
722 tty_lock(s->s_ttyp);
723 tty_rel_sess(s->s_ttyp, s);
724 }
725 mtx_destroy(&s->s_mtx);
726 free(s, M_SESSION);
727 }
728 }
729
730 #ifdef DDB
731
732 DB_SHOW_COMMAND(pgrpdump, pgrpdump)
733 {
734 register struct pgrp *pgrp;
735 register struct proc *p;
736 register int i;
737
738 for (i = 0; i <= pgrphash; i++) {
739 if (!LIST_EMPTY(&pgrphashtbl[i])) {
740 printf("\tindx %d\n", i);
741 LIST_FOREACH(pgrp, &pgrphashtbl[i], pg_hash) {
742 printf(
743 "\tpgrp %p, pgid %ld, sess %p, sesscnt %d, mem %p\n",
744 (void *)pgrp, (long)pgrp->pg_id,
745 (void *)pgrp->pg_session,
746 pgrp->pg_session->s_count,
747 (void *)LIST_FIRST(&pgrp->pg_members));
748 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
749 printf("\t\tpid %ld addr %p pgrp %p\n",
750 (long)p->p_pid, (void *)p,
751 (void *)p->p_pgrp);
752 }
753 }
754 }
755 }
756 }
757 #endif /* DDB */
758
759 /*
760 * Calculate the kinfo_proc members which contain process-wide
761 * informations.
762 * Must be called with the target process locked.
763 */
764 static void
765 fill_kinfo_aggregate(struct proc *p, struct kinfo_proc *kp)
766 {
767 struct thread *td;
768
769 PROC_LOCK_ASSERT(p, MA_OWNED);
770
771 kp->ki_estcpu = 0;
772 kp->ki_pctcpu = 0;
773 FOREACH_THREAD_IN_PROC(p, td) {
774 thread_lock(td);
775 kp->ki_pctcpu += sched_pctcpu(td);
776 kp->ki_estcpu += td->td_estcpu;
777 thread_unlock(td);
778 }
779 }
780
781 /*
782 * Clear kinfo_proc and fill in any information that is common
783 * to all threads in the process.
784 * Must be called with the target process locked.
785 */
786 static void
787 fill_kinfo_proc_only(struct proc *p, struct kinfo_proc *kp)
788 {
789 struct thread *td0;
790 struct tty *tp;
791 struct session *sp;
792 struct ucred *cred;
793 struct sigacts *ps;
794
795 PROC_LOCK_ASSERT(p, MA_OWNED);
796 bzero(kp, sizeof(*kp));
797
798 kp->ki_structsize = sizeof(*kp);
799 kp->ki_paddr = p;
800 kp->ki_addr =/* p->p_addr; */0; /* XXX */
801 kp->ki_args = p->p_args;
802 kp->ki_textvp = p->p_textvp;
803 #ifdef KTRACE
804 kp->ki_tracep = p->p_tracevp;
805 kp->ki_traceflag = p->p_traceflag;
806 #endif
807 kp->ki_fd = p->p_fd;
808 kp->ki_vmspace = p->p_vmspace;
809 kp->ki_flag = p->p_flag;
810 kp->ki_flag2 = p->p_flag2;
811 cred = p->p_ucred;
812 if (cred) {
813 kp->ki_uid = cred->cr_uid;
814 kp->ki_ruid = cred->cr_ruid;
815 kp->ki_svuid = cred->cr_svuid;
816 kp->ki_cr_flags = 0;
817 if (cred->cr_flags & CRED_FLAG_CAPMODE)
818 kp->ki_cr_flags |= KI_CRF_CAPABILITY_MODE;
819 /* XXX bde doesn't like KI_NGROUPS */
820 if (cred->cr_ngroups > KI_NGROUPS) {
821 kp->ki_ngroups = KI_NGROUPS;
822 kp->ki_cr_flags |= KI_CRF_GRP_OVERFLOW;
823 } else
824 kp->ki_ngroups = cred->cr_ngroups;
825 bcopy(cred->cr_groups, kp->ki_groups,
826 kp->ki_ngroups * sizeof(gid_t));
827 kp->ki_rgid = cred->cr_rgid;
828 kp->ki_svgid = cred->cr_svgid;
829 /* If jailed(cred), emulate the old P_JAILED flag. */
830 if (jailed(cred)) {
831 kp->ki_flag |= P_JAILED;
832 /* If inside the jail, use 0 as a jail ID. */
833 if (cred->cr_prison != curthread->td_ucred->cr_prison)
834 kp->ki_jid = cred->cr_prison->pr_id;
835 }
836 strlcpy(kp->ki_loginclass, cred->cr_loginclass->lc_name,
837 sizeof(kp->ki_loginclass));
838 }
839 ps = p->p_sigacts;
840 if (ps) {
841 mtx_lock(&ps->ps_mtx);
842 kp->ki_sigignore = ps->ps_sigignore;
843 kp->ki_sigcatch = ps->ps_sigcatch;
844 mtx_unlock(&ps->ps_mtx);
845 }
846 if (p->p_state != PRS_NEW &&
847 p->p_state != PRS_ZOMBIE &&
848 p->p_vmspace != NULL) {
849 struct vmspace *vm = p->p_vmspace;
850
851 kp->ki_size = vm->vm_map.size;
852 kp->ki_rssize = vmspace_resident_count(vm); /*XXX*/
853 FOREACH_THREAD_IN_PROC(p, td0) {
854 if (!TD_IS_SWAPPED(td0))
855 kp->ki_rssize += td0->td_kstack_pages;
856 }
857 kp->ki_swrss = vm->vm_swrss;
858 kp->ki_tsize = vm->vm_tsize;
859 kp->ki_dsize = vm->vm_dsize;
860 kp->ki_ssize = vm->vm_ssize;
861 } else if (p->p_state == PRS_ZOMBIE)
862 kp->ki_stat = SZOMB;
863 if (kp->ki_flag & P_INMEM)
864 kp->ki_sflag = PS_INMEM;
865 else
866 kp->ki_sflag = 0;
867 /* Calculate legacy swtime as seconds since 'swtick'. */
868 kp->ki_swtime = (ticks - p->p_swtick) / hz;
869 kp->ki_pid = p->p_pid;
870 kp->ki_nice = p->p_nice;
871 kp->ki_fibnum = p->p_fibnum;
872 kp->ki_start = p->p_stats->p_start;
873 timevaladd(&kp->ki_start, &boottime);
874 PROC_SLOCK(p);
875 rufetch(p, &kp->ki_rusage);
876 kp->ki_runtime = cputick2usec(p->p_rux.rux_runtime);
877 calcru(p, &kp->ki_rusage.ru_utime, &kp->ki_rusage.ru_stime);
878 PROC_SUNLOCK(p);
879 calccru(p, &kp->ki_childutime, &kp->ki_childstime);
880 /* Some callers want child times in a single value. */
881 kp->ki_childtime = kp->ki_childstime;
882 timevaladd(&kp->ki_childtime, &kp->ki_childutime);
883
884 FOREACH_THREAD_IN_PROC(p, td0)
885 kp->ki_cow += td0->td_cow;
886
887 tp = NULL;
888 if (p->p_pgrp) {
889 kp->ki_pgid = p->p_pgrp->pg_id;
890 kp->ki_jobc = p->p_pgrp->pg_jobc;
891 sp = p->p_pgrp->pg_session;
892
893 if (sp != NULL) {
894 kp->ki_sid = sp->s_sid;
895 SESS_LOCK(sp);
896 strlcpy(kp->ki_login, sp->s_login,
897 sizeof(kp->ki_login));
898 if (sp->s_ttyvp)
899 kp->ki_kiflag |= KI_CTTY;
900 if (SESS_LEADER(p))
901 kp->ki_kiflag |= KI_SLEADER;
902 /* XXX proctree_lock */
903 tp = sp->s_ttyp;
904 SESS_UNLOCK(sp);
905 }
906 }
907 if ((p->p_flag & P_CONTROLT) && tp != NULL) {
908 kp->ki_tdev = tty_udev(tp);
909 kp->ki_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID;
910 if (tp->t_session)
911 kp->ki_tsid = tp->t_session->s_sid;
912 } else
913 kp->ki_tdev = NODEV;
914 if (p->p_comm[0] != '\0')
915 strlcpy(kp->ki_comm, p->p_comm, sizeof(kp->ki_comm));
916 if (p->p_sysent && p->p_sysent->sv_name != NULL &&
917 p->p_sysent->sv_name[0] != '\0')
918 strlcpy(kp->ki_emul, p->p_sysent->sv_name, sizeof(kp->ki_emul));
919 kp->ki_siglist = p->p_siglist;
920 kp->ki_xstat = p->p_xstat;
921 kp->ki_acflag = p->p_acflag;
922 kp->ki_lock = p->p_lock;
923 if (p->p_pptr)
924 kp->ki_ppid = p->p_pptr->p_pid;
925 }
926
927 /*
928 * Fill in information that is thread specific. Must be called with
929 * target process locked. If 'preferthread' is set, overwrite certain
930 * process-related fields that are maintained for both threads and
931 * processes.
932 */
933 static void
934 fill_kinfo_thread(struct thread *td, struct kinfo_proc *kp, int preferthread)
935 {
936 struct proc *p;
937
938 p = td->td_proc;
939 kp->ki_tdaddr = td;
940 PROC_LOCK_ASSERT(p, MA_OWNED);
941
942 if (preferthread)
943 PROC_SLOCK(p);
944 thread_lock(td);
945 if (td->td_wmesg != NULL)
946 strlcpy(kp->ki_wmesg, td->td_wmesg, sizeof(kp->ki_wmesg));
947 else
948 bzero(kp->ki_wmesg, sizeof(kp->ki_wmesg));
949 strlcpy(kp->ki_tdname, td->td_name, sizeof(kp->ki_tdname));
950 if (TD_ON_LOCK(td)) {
951 kp->ki_kiflag |= KI_LOCKBLOCK;
952 strlcpy(kp->ki_lockname, td->td_lockname,
953 sizeof(kp->ki_lockname));
954 } else {
955 kp->ki_kiflag &= ~KI_LOCKBLOCK;
956 bzero(kp->ki_lockname, sizeof(kp->ki_lockname));
957 }
958
959 if (p->p_state == PRS_NORMAL) { /* approximate. */
960 if (TD_ON_RUNQ(td) ||
961 TD_CAN_RUN(td) ||
962 TD_IS_RUNNING(td)) {
963 kp->ki_stat = SRUN;
964 } else if (P_SHOULDSTOP(p)) {
965 kp->ki_stat = SSTOP;
966 } else if (TD_IS_SLEEPING(td)) {
967 kp->ki_stat = SSLEEP;
968 } else if (TD_ON_LOCK(td)) {
969 kp->ki_stat = SLOCK;
970 } else {
971 kp->ki_stat = SWAIT;
972 }
973 } else if (p->p_state == PRS_ZOMBIE) {
974 kp->ki_stat = SZOMB;
975 } else {
976 kp->ki_stat = SIDL;
977 }
978
979 /* Things in the thread */
980 kp->ki_wchan = td->td_wchan;
981 kp->ki_pri.pri_level = td->td_priority;
982 kp->ki_pri.pri_native = td->td_base_pri;
983 kp->ki_lastcpu = td->td_lastcpu;
984 kp->ki_oncpu = td->td_oncpu;
985 kp->ki_tdflags = td->td_flags;
986 kp->ki_tid = td->td_tid;
987 kp->ki_numthreads = p->p_numthreads;
988 kp->ki_pcb = td->td_pcb;
989 kp->ki_kstack = (void *)td->td_kstack;
990 kp->ki_slptime = (ticks - td->td_slptick) / hz;
991 kp->ki_pri.pri_class = td->td_pri_class;
992 kp->ki_pri.pri_user = td->td_user_pri;
993
994 if (preferthread) {
995 rufetchtd(td, &kp->ki_rusage);
996 kp->ki_runtime = cputick2usec(td->td_rux.rux_runtime);
997 kp->ki_pctcpu = sched_pctcpu(td);
998 kp->ki_estcpu = td->td_estcpu;
999 kp->ki_cow = td->td_cow;
1000 }
1001
1002 /* We can't get this anymore but ps etc never used it anyway. */
1003 kp->ki_rqindex = 0;
1004
1005 if (preferthread)
1006 kp->ki_siglist = td->td_siglist;
1007 kp->ki_sigmask = td->td_sigmask;
1008 thread_unlock(td);
1009 if (preferthread)
1010 PROC_SUNLOCK(p);
1011 }
1012
1013 /*
1014 * Fill in a kinfo_proc structure for the specified process.
1015 * Must be called with the target process locked.
1016 */
1017 void
1018 fill_kinfo_proc(struct proc *p, struct kinfo_proc *kp)
1019 {
1020
1021 MPASS(FIRST_THREAD_IN_PROC(p) != NULL);
1022
1023 fill_kinfo_proc_only(p, kp);
1024 fill_kinfo_thread(FIRST_THREAD_IN_PROC(p), kp, 0);
1025 fill_kinfo_aggregate(p, kp);
1026 }
1027
1028 struct pstats *
1029 pstats_alloc(void)
1030 {
1031
1032 return (malloc(sizeof(struct pstats), M_SUBPROC, M_ZERO|M_WAITOK));
1033 }
1034
1035 /*
1036 * Copy parts of p_stats; zero the rest of p_stats (statistics).
1037 */
1038 void
1039 pstats_fork(struct pstats *src, struct pstats *dst)
1040 {
1041
1042 bzero(&dst->pstat_startzero,
1043 __rangeof(struct pstats, pstat_startzero, pstat_endzero));
1044 bcopy(&src->pstat_startcopy, &dst->pstat_startcopy,
1045 __rangeof(struct pstats, pstat_startcopy, pstat_endcopy));
1046 }
1047
1048 void
1049 pstats_free(struct pstats *ps)
1050 {
1051
1052 free(ps, M_SUBPROC);
1053 }
1054
1055 static struct proc *
1056 zpfind_locked(pid_t pid)
1057 {
1058 struct proc *p;
1059
1060 sx_assert(&allproc_lock, SX_LOCKED);
1061 LIST_FOREACH(p, &zombproc, p_list) {
1062 if (p->p_pid == pid) {
1063 PROC_LOCK(p);
1064 break;
1065 }
1066 }
1067 return (p);
1068 }
1069
1070 /*
1071 * Locate a zombie process by number
1072 */
1073 struct proc *
1074 zpfind(pid_t pid)
1075 {
1076 struct proc *p;
1077
1078 sx_slock(&allproc_lock);
1079 p = zpfind_locked(pid);
1080 sx_sunlock(&allproc_lock);
1081 return (p);
1082 }
1083
1084 #ifdef COMPAT_FREEBSD32
1085
1086 /*
1087 * This function is typically used to copy out the kernel address, so
1088 * it can be replaced by assignment of zero.
1089 */
1090 static inline uint32_t
1091 ptr32_trim(void *ptr)
1092 {
1093 uintptr_t uptr;
1094
1095 uptr = (uintptr_t)ptr;
1096 return ((uptr > UINT_MAX) ? 0 : uptr);
1097 }
1098
1099 #define PTRTRIM_CP(src,dst,fld) \
1100 do { (dst).fld = ptr32_trim((src).fld); } while (0)
1101
1102 static void
1103 freebsd32_kinfo_proc_out(const struct kinfo_proc *ki, struct kinfo_proc32 *ki32)
1104 {
1105 int i;
1106
1107 bzero(ki32, sizeof(struct kinfo_proc32));
1108 ki32->ki_structsize = sizeof(struct kinfo_proc32);
1109 CP(*ki, *ki32, ki_layout);
1110 PTRTRIM_CP(*ki, *ki32, ki_args);
1111 PTRTRIM_CP(*ki, *ki32, ki_paddr);
1112 PTRTRIM_CP(*ki, *ki32, ki_addr);
1113 PTRTRIM_CP(*ki, *ki32, ki_tracep);
1114 PTRTRIM_CP(*ki, *ki32, ki_textvp);
1115 PTRTRIM_CP(*ki, *ki32, ki_fd);
1116 PTRTRIM_CP(*ki, *ki32, ki_vmspace);
1117 PTRTRIM_CP(*ki, *ki32, ki_wchan);
1118 CP(*ki, *ki32, ki_pid);
1119 CP(*ki, *ki32, ki_ppid);
1120 CP(*ki, *ki32, ki_pgid);
1121 CP(*ki, *ki32, ki_tpgid);
1122 CP(*ki, *ki32, ki_sid);
1123 CP(*ki, *ki32, ki_tsid);
1124 CP(*ki, *ki32, ki_jobc);
1125 CP(*ki, *ki32, ki_tdev);
1126 CP(*ki, *ki32, ki_siglist);
1127 CP(*ki, *ki32, ki_sigmask);
1128 CP(*ki, *ki32, ki_sigignore);
1129 CP(*ki, *ki32, ki_sigcatch);
1130 CP(*ki, *ki32, ki_uid);
1131 CP(*ki, *ki32, ki_ruid);
1132 CP(*ki, *ki32, ki_svuid);
1133 CP(*ki, *ki32, ki_rgid);
1134 CP(*ki, *ki32, ki_svgid);
1135 CP(*ki, *ki32, ki_ngroups);
1136 for (i = 0; i < KI_NGROUPS; i++)
1137 CP(*ki, *ki32, ki_groups[i]);
1138 CP(*ki, *ki32, ki_size);
1139 CP(*ki, *ki32, ki_rssize);
1140 CP(*ki, *ki32, ki_swrss);
1141 CP(*ki, *ki32, ki_tsize);
1142 CP(*ki, *ki32, ki_dsize);
1143 CP(*ki, *ki32, ki_ssize);
1144 CP(*ki, *ki32, ki_xstat);
1145 CP(*ki, *ki32, ki_acflag);
1146 CP(*ki, *ki32, ki_pctcpu);
1147 CP(*ki, *ki32, ki_estcpu);
1148 CP(*ki, *ki32, ki_slptime);
1149 CP(*ki, *ki32, ki_swtime);
1150 CP(*ki, *ki32, ki_cow);
1151 CP(*ki, *ki32, ki_runtime);
1152 TV_CP(*ki, *ki32, ki_start);
1153 TV_CP(*ki, *ki32, ki_childtime);
1154 CP(*ki, *ki32, ki_flag);
1155 CP(*ki, *ki32, ki_kiflag);
1156 CP(*ki, *ki32, ki_traceflag);
1157 CP(*ki, *ki32, ki_stat);
1158 CP(*ki, *ki32, ki_nice);
1159 CP(*ki, *ki32, ki_lock);
1160 CP(*ki, *ki32, ki_rqindex);
1161 CP(*ki, *ki32, ki_oncpu);
1162 CP(*ki, *ki32, ki_lastcpu);
1163 bcopy(ki->ki_tdname, ki32->ki_tdname, TDNAMLEN + 1);
1164 bcopy(ki->ki_wmesg, ki32->ki_wmesg, WMESGLEN + 1);
1165 bcopy(ki->ki_login, ki32->ki_login, LOGNAMELEN + 1);
1166 bcopy(ki->ki_lockname, ki32->ki_lockname, LOCKNAMELEN + 1);
1167 bcopy(ki->ki_comm, ki32->ki_comm, COMMLEN + 1);
1168 bcopy(ki->ki_emul, ki32->ki_emul, KI_EMULNAMELEN + 1);
1169 bcopy(ki->ki_loginclass, ki32->ki_loginclass, LOGINCLASSLEN + 1);
1170 CP(*ki, *ki32, ki_flag2);
1171 CP(*ki, *ki32, ki_fibnum);
1172 CP(*ki, *ki32, ki_cr_flags);
1173 CP(*ki, *ki32, ki_jid);
1174 CP(*ki, *ki32, ki_numthreads);
1175 CP(*ki, *ki32, ki_tid);
1176 CP(*ki, *ki32, ki_pri);
1177 freebsd32_rusage_out(&ki->ki_rusage, &ki32->ki_rusage);
1178 freebsd32_rusage_out(&ki->ki_rusage_ch, &ki32->ki_rusage_ch);
1179 PTRTRIM_CP(*ki, *ki32, ki_pcb);
1180 PTRTRIM_CP(*ki, *ki32, ki_kstack);
1181 PTRTRIM_CP(*ki, *ki32, ki_udata);
1182 CP(*ki, *ki32, ki_sflag);
1183 CP(*ki, *ki32, ki_tdflags);
1184 }
1185 #endif
1186
1187 int
1188 kern_proc_out(struct proc *p, struct sbuf *sb, int flags)
1189 {
1190 struct thread *td;
1191 struct kinfo_proc ki;
1192 #ifdef COMPAT_FREEBSD32
1193 struct kinfo_proc32 ki32;
1194 #endif
1195 int error;
1196
1197 PROC_LOCK_ASSERT(p, MA_OWNED);
1198 MPASS(FIRST_THREAD_IN_PROC(p) != NULL);
1199
1200 error = 0;
1201 fill_kinfo_proc(p, &ki);
1202 if ((flags & KERN_PROC_NOTHREADS) != 0) {
1203 #ifdef COMPAT_FREEBSD32
1204 if ((flags & KERN_PROC_MASK32) != 0) {
1205 freebsd32_kinfo_proc_out(&ki, &ki32);
1206 if (sbuf_bcat(sb, &ki32, sizeof(ki32)) != 0)
1207 error = ENOMEM;
1208 } else
1209 #endif
1210 if (sbuf_bcat(sb, &ki, sizeof(ki)) != 0)
1211 error = ENOMEM;
1212 } else {
1213 FOREACH_THREAD_IN_PROC(p, td) {
1214 fill_kinfo_thread(td, &ki, 1);
1215 #ifdef COMPAT_FREEBSD32
1216 if ((flags & KERN_PROC_MASK32) != 0) {
1217 freebsd32_kinfo_proc_out(&ki, &ki32);
1218 if (sbuf_bcat(sb, &ki32, sizeof(ki32)) != 0)
1219 error = ENOMEM;
1220 } else
1221 #endif
1222 if (sbuf_bcat(sb, &ki, sizeof(ki)) != 0)
1223 error = ENOMEM;
1224 if (error != 0)
1225 break;
1226 }
1227 }
1228 PROC_UNLOCK(p);
1229 return (error);
1230 }
1231
1232 static int
1233 sysctl_out_proc(struct proc *p, struct sysctl_req *req, int flags,
1234 int doingzomb)
1235 {
1236 struct sbuf sb;
1237 struct kinfo_proc ki;
1238 struct proc *np;
1239 int error, error2;
1240 pid_t pid;
1241
1242 pid = p->p_pid;
1243 sbuf_new_for_sysctl(&sb, (char *)&ki, sizeof(ki), req);
1244 error = kern_proc_out(p, &sb, flags);
1245 error2 = sbuf_finish(&sb);
1246 sbuf_delete(&sb);
1247 if (error != 0)
1248 return (error);
1249 else if (error2 != 0)
1250 return (error2);
1251 if (doingzomb)
1252 np = zpfind(pid);
1253 else {
1254 if (pid == 0)
1255 return (0);
1256 np = pfind(pid);
1257 }
1258 if (np == NULL)
1259 return (ESRCH);
1260 if (np != p) {
1261 PROC_UNLOCK(np);
1262 return (ESRCH);
1263 }
1264 PROC_UNLOCK(np);
1265 return (0);
1266 }
1267
1268 static int
1269 sysctl_kern_proc(SYSCTL_HANDLER_ARGS)
1270 {
1271 int *name = (int *)arg1;
1272 u_int namelen = arg2;
1273 struct proc *p;
1274 int flags, doingzomb, oid_number;
1275 int error = 0;
1276
1277 oid_number = oidp->oid_number;
1278 if (oid_number != KERN_PROC_ALL &&
1279 (oid_number & KERN_PROC_INC_THREAD) == 0)
1280 flags = KERN_PROC_NOTHREADS;
1281 else {
1282 flags = 0;
1283 oid_number &= ~KERN_PROC_INC_THREAD;
1284 }
1285 #ifdef COMPAT_FREEBSD32
1286 if (req->flags & SCTL_MASK32)
1287 flags |= KERN_PROC_MASK32;
1288 #endif
1289 if (oid_number == KERN_PROC_PID) {
1290 if (namelen != 1)
1291 return (EINVAL);
1292 error = sysctl_wire_old_buffer(req, 0);
1293 if (error)
1294 return (error);
1295 error = pget((pid_t)name[0], PGET_CANSEE, &p);
1296 if (error != 0)
1297 return (error);
1298 error = sysctl_out_proc(p, req, flags, 0);
1299 return (error);
1300 }
1301
1302 switch (oid_number) {
1303 case KERN_PROC_ALL:
1304 if (namelen != 0)
1305 return (EINVAL);
1306 break;
1307 case KERN_PROC_PROC:
1308 if (namelen != 0 && namelen != 1)
1309 return (EINVAL);
1310 break;
1311 default:
1312 if (namelen != 1)
1313 return (EINVAL);
1314 break;
1315 }
1316
1317 if (!req->oldptr) {
1318 /* overestimate by 5 procs */
1319 error = SYSCTL_OUT(req, 0, sizeof (struct kinfo_proc) * 5);
1320 if (error)
1321 return (error);
1322 }
1323 error = sysctl_wire_old_buffer(req, 0);
1324 if (error != 0)
1325 return (error);
1326 sx_slock(&allproc_lock);
1327 for (doingzomb=0 ; doingzomb < 2 ; doingzomb++) {
1328 if (!doingzomb)
1329 p = LIST_FIRST(&allproc);
1330 else
1331 p = LIST_FIRST(&zombproc);
1332 for (; p != 0; p = LIST_NEXT(p, p_list)) {
1333 /*
1334 * Skip embryonic processes.
1335 */
1336 PROC_LOCK(p);
1337 if (p->p_state == PRS_NEW) {
1338 PROC_UNLOCK(p);
1339 continue;
1340 }
1341 KASSERT(p->p_ucred != NULL,
1342 ("process credential is NULL for non-NEW proc"));
1343 /*
1344 * Show a user only appropriate processes.
1345 */
1346 if (p_cansee(curthread, p)) {
1347 PROC_UNLOCK(p);
1348 continue;
1349 }
1350 /*
1351 * TODO - make more efficient (see notes below).
1352 * do by session.
1353 */
1354 switch (oid_number) {
1355
1356 case KERN_PROC_GID:
1357 if (p->p_ucred->cr_gid != (gid_t)name[0]) {
1358 PROC_UNLOCK(p);
1359 continue;
1360 }
1361 break;
1362
1363 case KERN_PROC_PGRP:
1364 /* could do this by traversing pgrp */
1365 if (p->p_pgrp == NULL ||
1366 p->p_pgrp->pg_id != (pid_t)name[0]) {
1367 PROC_UNLOCK(p);
1368 continue;
1369 }
1370 break;
1371
1372 case KERN_PROC_RGID:
1373 if (p->p_ucred->cr_rgid != (gid_t)name[0]) {
1374 PROC_UNLOCK(p);
1375 continue;
1376 }
1377 break;
1378
1379 case KERN_PROC_SESSION:
1380 if (p->p_session == NULL ||
1381 p->p_session->s_sid != (pid_t)name[0]) {
1382 PROC_UNLOCK(p);
1383 continue;
1384 }
1385 break;
1386
1387 case KERN_PROC_TTY:
1388 if ((p->p_flag & P_CONTROLT) == 0 ||
1389 p->p_session == NULL) {
1390 PROC_UNLOCK(p);
1391 continue;
1392 }
1393 /* XXX proctree_lock */
1394 SESS_LOCK(p->p_session);
1395 if (p->p_session->s_ttyp == NULL ||
1396 tty_udev(p->p_session->s_ttyp) !=
1397 (dev_t)name[0]) {
1398 SESS_UNLOCK(p->p_session);
1399 PROC_UNLOCK(p);
1400 continue;
1401 }
1402 SESS_UNLOCK(p->p_session);
1403 break;
1404
1405 case KERN_PROC_UID:
1406 if (p->p_ucred->cr_uid != (uid_t)name[0]) {
1407 PROC_UNLOCK(p);
1408 continue;
1409 }
1410 break;
1411
1412 case KERN_PROC_RUID:
1413 if (p->p_ucred->cr_ruid != (uid_t)name[0]) {
1414 PROC_UNLOCK(p);
1415 continue;
1416 }
1417 break;
1418
1419 case KERN_PROC_PROC:
1420 break;
1421
1422 default:
1423 break;
1424
1425 }
1426
1427 error = sysctl_out_proc(p, req, flags, doingzomb);
1428 if (error) {
1429 sx_sunlock(&allproc_lock);
1430 return (error);
1431 }
1432 }
1433 }
1434 sx_sunlock(&allproc_lock);
1435 return (0);
1436 }
1437
1438 struct pargs *
1439 pargs_alloc(int len)
1440 {
1441 struct pargs *pa;
1442
1443 pa = malloc(sizeof(struct pargs) + len, M_PARGS,
1444 M_WAITOK);
1445 refcount_init(&pa->ar_ref, 1);
1446 pa->ar_length = len;
1447 return (pa);
1448 }
1449
1450 static void
1451 pargs_free(struct pargs *pa)
1452 {
1453
1454 free(pa, M_PARGS);
1455 }
1456
1457 void
1458 pargs_hold(struct pargs *pa)
1459 {
1460
1461 if (pa == NULL)
1462 return;
1463 refcount_acquire(&pa->ar_ref);
1464 }
1465
1466 void
1467 pargs_drop(struct pargs *pa)
1468 {
1469
1470 if (pa == NULL)
1471 return;
1472 if (refcount_release(&pa->ar_ref))
1473 pargs_free(pa);
1474 }
1475
1476 static int
1477 proc_read_mem(struct thread *td, struct proc *p, vm_offset_t offset, void* buf,
1478 size_t len)
1479 {
1480 struct iovec iov;
1481 struct uio uio;
1482
1483 iov.iov_base = (caddr_t)buf;
1484 iov.iov_len = len;
1485 uio.uio_iov = &iov;
1486 uio.uio_iovcnt = 1;
1487 uio.uio_offset = offset;
1488 uio.uio_resid = (ssize_t)len;
1489 uio.uio_segflg = UIO_SYSSPACE;
1490 uio.uio_rw = UIO_READ;
1491 uio.uio_td = td;
1492
1493 return (proc_rwmem(p, &uio));
1494 }
1495
1496 static int
1497 proc_read_string(struct thread *td, struct proc *p, const char *sptr, char *buf,
1498 size_t len)
1499 {
1500 size_t i;
1501 int error;
1502
1503 error = proc_read_mem(td, p, (vm_offset_t)sptr, buf, len);
1504 /*
1505 * Reading the chunk may validly return EFAULT if the string is shorter
1506 * than the chunk and is aligned at the end of the page, assuming the
1507 * next page is not mapped. So if EFAULT is returned do a fallback to
1508 * one byte read loop.
1509 */
1510 if (error == EFAULT) {
1511 for (i = 0; i < len; i++, buf++, sptr++) {
1512 error = proc_read_mem(td, p, (vm_offset_t)sptr, buf, 1);
1513 if (error != 0)
1514 return (error);
1515 if (*buf == '\0')
1516 break;
1517 }
1518 error = 0;
1519 }
1520 return (error);
1521 }
1522
1523 #define PROC_AUXV_MAX 256 /* Safety limit on auxv size. */
1524
1525 enum proc_vector_type {
1526 PROC_ARG,
1527 PROC_ENV,
1528 PROC_AUX,
1529 };
1530
1531 #ifdef COMPAT_FREEBSD32
1532 static int
1533 get_proc_vector32(struct thread *td, struct proc *p, char ***proc_vectorp,
1534 size_t *vsizep, enum proc_vector_type type)
1535 {
1536 struct freebsd32_ps_strings pss;
1537 Elf32_Auxinfo aux;
1538 vm_offset_t vptr, ptr;
1539 uint32_t *proc_vector32;
1540 char **proc_vector;
1541 size_t vsize, size;
1542 int i, error;
1543
1544 error = proc_read_mem(td, p, (vm_offset_t)(p->p_sysent->sv_psstrings),
1545 &pss, sizeof(pss));
1546 if (error != 0)
1547 return (error);
1548 switch (type) {
1549 case PROC_ARG:
1550 vptr = (vm_offset_t)PTRIN(pss.ps_argvstr);
1551 vsize = pss.ps_nargvstr;
1552 if (vsize > ARG_MAX)
1553 return (ENOEXEC);
1554 size = vsize * sizeof(int32_t);
1555 break;
1556 case PROC_ENV:
1557 vptr = (vm_offset_t)PTRIN(pss.ps_envstr);
1558 vsize = pss.ps_nenvstr;
1559 if (vsize > ARG_MAX)
1560 return (ENOEXEC);
1561 size = vsize * sizeof(int32_t);
1562 break;
1563 case PROC_AUX:
1564 vptr = (vm_offset_t)PTRIN(pss.ps_envstr) +
1565 (pss.ps_nenvstr + 1) * sizeof(int32_t);
1566 if (vptr % 4 != 0)
1567 return (ENOEXEC);
1568 for (ptr = vptr, i = 0; i < PROC_AUXV_MAX; i++) {
1569 error = proc_read_mem(td, p, ptr, &aux, sizeof(aux));
1570 if (error != 0)
1571 return (error);
1572 if (aux.a_type == AT_NULL)
1573 break;
1574 ptr += sizeof(aux);
1575 }
1576 if (aux.a_type != AT_NULL)
1577 return (ENOEXEC);
1578 vsize = i + 1;
1579 size = vsize * sizeof(aux);
1580 break;
1581 default:
1582 KASSERT(0, ("Wrong proc vector type: %d", type));
1583 return (EINVAL);
1584 }
1585 proc_vector32 = malloc(size, M_TEMP, M_WAITOK);
1586 error = proc_read_mem(td, p, vptr, proc_vector32, size);
1587 if (error != 0)
1588 goto done;
1589 if (type == PROC_AUX) {
1590 *proc_vectorp = (char **)proc_vector32;
1591 *vsizep = vsize;
1592 return (0);
1593 }
1594 proc_vector = malloc(vsize * sizeof(char *), M_TEMP, M_WAITOK);
1595 for (i = 0; i < (int)vsize; i++)
1596 proc_vector[i] = PTRIN(proc_vector32[i]);
1597 *proc_vectorp = proc_vector;
1598 *vsizep = vsize;
1599 done:
1600 free(proc_vector32, M_TEMP);
1601 return (error);
1602 }
1603 #endif
1604
1605 static int
1606 get_proc_vector(struct thread *td, struct proc *p, char ***proc_vectorp,
1607 size_t *vsizep, enum proc_vector_type type)
1608 {
1609 struct ps_strings pss;
1610 Elf_Auxinfo aux;
1611 vm_offset_t vptr, ptr;
1612 char **proc_vector;
1613 size_t vsize, size;
1614 int error, i;
1615
1616 #ifdef COMPAT_FREEBSD32
1617 if (SV_PROC_FLAG(p, SV_ILP32) != 0)
1618 return (get_proc_vector32(td, p, proc_vectorp, vsizep, type));
1619 #endif
1620 error = proc_read_mem(td, p, (vm_offset_t)(p->p_sysent->sv_psstrings),
1621 &pss, sizeof(pss));
1622 if (error != 0)
1623 return (error);
1624 switch (type) {
1625 case PROC_ARG:
1626 vptr = (vm_offset_t)pss.ps_argvstr;
1627 vsize = pss.ps_nargvstr;
1628 if (vsize > ARG_MAX)
1629 return (ENOEXEC);
1630 size = vsize * sizeof(char *);
1631 break;
1632 case PROC_ENV:
1633 vptr = (vm_offset_t)pss.ps_envstr;
1634 vsize = pss.ps_nenvstr;
1635 if (vsize > ARG_MAX)
1636 return (ENOEXEC);
1637 size = vsize * sizeof(char *);
1638 break;
1639 case PROC_AUX:
1640 /*
1641 * The aux array is just above env array on the stack. Check
1642 * that the address is naturally aligned.
1643 */
1644 vptr = (vm_offset_t)pss.ps_envstr + (pss.ps_nenvstr + 1)
1645 * sizeof(char *);
1646 #if __ELF_WORD_SIZE == 64
1647 if (vptr % sizeof(uint64_t) != 0)
1648 #else
1649 if (vptr % sizeof(uint32_t) != 0)
1650 #endif
1651 return (ENOEXEC);
1652 /*
1653 * We count the array size reading the aux vectors from the
1654 * stack until AT_NULL vector is returned. So (to keep the code
1655 * simple) we read the process stack twice: the first time here
1656 * to find the size and the second time when copying the vectors
1657 * to the allocated proc_vector.
1658 */
1659 for (ptr = vptr, i = 0; i < PROC_AUXV_MAX; i++) {
1660 error = proc_read_mem(td, p, ptr, &aux, sizeof(aux));
1661 if (error != 0)
1662 return (error);
1663 if (aux.a_type == AT_NULL)
1664 break;
1665 ptr += sizeof(aux);
1666 }
1667 /*
1668 * If the PROC_AUXV_MAX entries are iterated over, and we have
1669 * not reached AT_NULL, it is most likely we are reading wrong
1670 * data: either the process doesn't have auxv array or data has
1671 * been modified. Return the error in this case.
1672 */
1673 if (aux.a_type != AT_NULL)
1674 return (ENOEXEC);
1675 vsize = i + 1;
1676 size = vsize * sizeof(aux);
1677 break;
1678 default:
1679 KASSERT(0, ("Wrong proc vector type: %d", type));
1680 return (EINVAL); /* In case we are built without INVARIANTS. */
1681 }
1682 proc_vector = malloc(size, M_TEMP, M_WAITOK);
1683 if (proc_vector == NULL)
1684 return (ENOMEM);
1685 error = proc_read_mem(td, p, vptr, proc_vector, size);
1686 if (error != 0) {
1687 free(proc_vector, M_TEMP);
1688 return (error);
1689 }
1690 *proc_vectorp = proc_vector;
1691 *vsizep = vsize;
1692
1693 return (0);
1694 }
1695
1696 #define GET_PS_STRINGS_CHUNK_SZ 256 /* Chunk size (bytes) for ps_strings operations. */
1697
1698 static int
1699 get_ps_strings(struct thread *td, struct proc *p, struct sbuf *sb,
1700 enum proc_vector_type type)
1701 {
1702 size_t done, len, nchr, vsize;
1703 int error, i;
1704 char **proc_vector, *sptr;
1705 char pss_string[GET_PS_STRINGS_CHUNK_SZ];
1706
1707 PROC_ASSERT_HELD(p);
1708
1709 /*
1710 * We are not going to read more than 2 * (PATH_MAX + ARG_MAX) bytes.
1711 */
1712 nchr = 2 * (PATH_MAX + ARG_MAX);
1713
1714 error = get_proc_vector(td, p, &proc_vector, &vsize, type);
1715 if (error != 0)
1716 return (error);
1717 for (done = 0, i = 0; i < (int)vsize && done < nchr; i++) {
1718 /*
1719 * The program may have scribbled into its argv array, e.g. to
1720 * remove some arguments. If that has happened, break out
1721 * before trying to read from NULL.
1722 */
1723 if (proc_vector[i] == NULL)
1724 break;
1725 for (sptr = proc_vector[i]; ; sptr += GET_PS_STRINGS_CHUNK_SZ) {
1726 error = proc_read_string(td, p, sptr, pss_string,
1727 sizeof(pss_string));
1728 if (error != 0)
1729 goto done;
1730 len = strnlen(pss_string, GET_PS_STRINGS_CHUNK_SZ);
1731 if (done + len >= nchr)
1732 len = nchr - done - 1;
1733 sbuf_bcat(sb, pss_string, len);
1734 if (len != GET_PS_STRINGS_CHUNK_SZ)
1735 break;
1736 done += GET_PS_STRINGS_CHUNK_SZ;
1737 }
1738 sbuf_bcat(sb, "", 1);
1739 done += len + 1;
1740 }
1741 done:
1742 free(proc_vector, M_TEMP);
1743 return (error);
1744 }
1745
1746 int
1747 proc_getargv(struct thread *td, struct proc *p, struct sbuf *sb)
1748 {
1749
1750 return (get_ps_strings(curthread, p, sb, PROC_ARG));
1751 }
1752
1753 int
1754 proc_getenvv(struct thread *td, struct proc *p, struct sbuf *sb)
1755 {
1756
1757 return (get_ps_strings(curthread, p, sb, PROC_ENV));
1758 }
1759
1760 int
1761 proc_getauxv(struct thread *td, struct proc *p, struct sbuf *sb)
1762 {
1763 size_t vsize, size;
1764 char **auxv;
1765 int error;
1766
1767 error = get_proc_vector(td, p, &auxv, &vsize, PROC_AUX);
1768 if (error == 0) {
1769 #ifdef COMPAT_FREEBSD32
1770 if (SV_PROC_FLAG(p, SV_ILP32) != 0)
1771 size = vsize * sizeof(Elf32_Auxinfo);
1772 else
1773 #endif
1774 size = vsize * sizeof(Elf_Auxinfo);
1775 if (sbuf_bcat(sb, auxv, size) != 0)
1776 error = ENOMEM;
1777 free(auxv, M_TEMP);
1778 }
1779 return (error);
1780 }
1781
1782 /*
1783 * This sysctl allows a process to retrieve the argument list or process
1784 * title for another process without groping around in the address space
1785 * of the other process. It also allow a process to set its own "process
1786 * title to a string of its own choice.
1787 */
1788 static int
1789 sysctl_kern_proc_args(SYSCTL_HANDLER_ARGS)
1790 {
1791 int *name = (int *)arg1;
1792 u_int namelen = arg2;
1793 struct pargs *newpa, *pa;
1794 struct proc *p;
1795 struct sbuf sb;
1796 int flags, error = 0, error2;
1797
1798 if (namelen != 1)
1799 return (EINVAL);
1800
1801 flags = PGET_CANSEE;
1802 if (req->newptr != NULL)
1803 flags |= PGET_ISCURRENT;
1804 error = pget((pid_t)name[0], flags, &p);
1805 if (error)
1806 return (error);
1807
1808 pa = p->p_args;
1809 if (pa != NULL) {
1810 pargs_hold(pa);
1811 PROC_UNLOCK(p);
1812 error = SYSCTL_OUT(req, pa->ar_args, pa->ar_length);
1813 pargs_drop(pa);
1814 } else if ((p->p_flag & (P_WEXIT | P_SYSTEM)) == 0) {
1815 _PHOLD(p);
1816 PROC_UNLOCK(p);
1817 sbuf_new_for_sysctl(&sb, NULL, GET_PS_STRINGS_CHUNK_SZ, req);
1818 error = proc_getargv(curthread, p, &sb);
1819 error2 = sbuf_finish(&sb);
1820 PRELE(p);
1821 sbuf_delete(&sb);
1822 if (error == 0 && error2 != 0)
1823 error = error2;
1824 } else {
1825 PROC_UNLOCK(p);
1826 }
1827 if (error != 0 || req->newptr == NULL)
1828 return (error);
1829
1830 if (req->newlen + sizeof(struct pargs) > ps_arg_cache_limit)
1831 return (ENOMEM);
1832 newpa = pargs_alloc(req->newlen);
1833 error = SYSCTL_IN(req, newpa->ar_args, req->newlen);
1834 if (error != 0) {
1835 pargs_free(newpa);
1836 return (error);
1837 }
1838 PROC_LOCK(p);
1839 pa = p->p_args;
1840 p->p_args = newpa;
1841 PROC_UNLOCK(p);
1842 pargs_drop(pa);
1843 return (0);
1844 }
1845
1846 /*
1847 * This sysctl allows a process to retrieve environment of another process.
1848 */
1849 static int
1850 sysctl_kern_proc_env(SYSCTL_HANDLER_ARGS)
1851 {
1852 int *name = (int *)arg1;
1853 u_int namelen = arg2;
1854 struct proc *p;
1855 struct sbuf sb;
1856 int error, error2;
1857
1858 if (namelen != 1)
1859 return (EINVAL);
1860
1861 error = pget((pid_t)name[0], PGET_WANTREAD, &p);
1862 if (error != 0)
1863 return (error);
1864 if ((p->p_flag & P_SYSTEM) != 0) {
1865 PRELE(p);
1866 return (0);
1867 }
1868
1869 sbuf_new_for_sysctl(&sb, NULL, GET_PS_STRINGS_CHUNK_SZ, req);
1870 error = proc_getenvv(curthread, p, &sb);
1871 error2 = sbuf_finish(&sb);
1872 PRELE(p);
1873 sbuf_delete(&sb);
1874 return (error != 0 ? error : error2);
1875 }
1876
1877 /*
1878 * This sysctl allows a process to retrieve ELF auxiliary vector of
1879 * another process.
1880 */
1881 static int
1882 sysctl_kern_proc_auxv(SYSCTL_HANDLER_ARGS)
1883 {
1884 int *name = (int *)arg1;
1885 u_int namelen = arg2;
1886 struct proc *p;
1887 struct sbuf sb;
1888 int error, error2;
1889
1890 if (namelen != 1)
1891 return (EINVAL);
1892
1893 error = pget((pid_t)name[0], PGET_WANTREAD, &p);
1894 if (error != 0)
1895 return (error);
1896 if ((p->p_flag & P_SYSTEM) != 0) {
1897 PRELE(p);
1898 return (0);
1899 }
1900 sbuf_new_for_sysctl(&sb, NULL, GET_PS_STRINGS_CHUNK_SZ, req);
1901 error = proc_getauxv(curthread, p, &sb);
1902 error2 = sbuf_finish(&sb);
1903 PRELE(p);
1904 sbuf_delete(&sb);
1905 return (error != 0 ? error : error2);
1906 }
1907
1908 /*
1909 * This sysctl allows a process to retrieve the path of the executable for
1910 * itself or another process.
1911 */
1912 static int
1913 sysctl_kern_proc_pathname(SYSCTL_HANDLER_ARGS)
1914 {
1915 pid_t *pidp = (pid_t *)arg1;
1916 unsigned int arglen = arg2;
1917 struct proc *p;
1918 struct vnode *vp;
1919 char *retbuf, *freebuf;
1920 int error;
1921
1922 if (arglen != 1)
1923 return (EINVAL);
1924 if (*pidp == -1) { /* -1 means this process */
1925 p = req->td->td_proc;
1926 } else {
1927 error = pget(*pidp, PGET_CANSEE, &p);
1928 if (error != 0)
1929 return (error);
1930 }
1931
1932 vp = p->p_textvp;
1933 if (vp == NULL) {
1934 if (*pidp != -1)
1935 PROC_UNLOCK(p);
1936 return (0);
1937 }
1938 vref(vp);
1939 if (*pidp != -1)
1940 PROC_UNLOCK(p);
1941 error = vn_fullpath(req->td, vp, &retbuf, &freebuf);
1942 vrele(vp);
1943 if (error)
1944 return (error);
1945 error = SYSCTL_OUT(req, retbuf, strlen(retbuf) + 1);
1946 free(freebuf, M_TEMP);
1947 return (error);
1948 }
1949
1950 static int
1951 sysctl_kern_proc_sv_name(SYSCTL_HANDLER_ARGS)
1952 {
1953 struct proc *p;
1954 char *sv_name;
1955 int *name;
1956 int namelen;
1957 int error;
1958
1959 namelen = arg2;
1960 if (namelen != 1)
1961 return (EINVAL);
1962
1963 name = (int *)arg1;
1964 error = pget((pid_t)name[0], PGET_CANSEE, &p);
1965 if (error != 0)
1966 return (error);
1967 sv_name = p->p_sysent->sv_name;
1968 PROC_UNLOCK(p);
1969 return (sysctl_handle_string(oidp, sv_name, 0, req));
1970 }
1971
1972 #ifdef KINFO_OVMENTRY_SIZE
1973 CTASSERT(sizeof(struct kinfo_ovmentry) == KINFO_OVMENTRY_SIZE);
1974 #endif
1975
1976 #ifdef COMPAT_FREEBSD7
1977 static int
1978 sysctl_kern_proc_ovmmap(SYSCTL_HANDLER_ARGS)
1979 {
1980 vm_map_entry_t entry, tmp_entry;
1981 unsigned int last_timestamp;
1982 char *fullpath, *freepath;
1983 struct kinfo_ovmentry *kve;
1984 struct vattr va;
1985 struct ucred *cred;
1986 int error, *name;
1987 struct vnode *vp;
1988 struct proc *p;
1989 vm_map_t map;
1990 struct vmspace *vm;
1991
1992 name = (int *)arg1;
1993 error = pget((pid_t)name[0], PGET_WANTREAD, &p);
1994 if (error != 0)
1995 return (error);
1996 vm = vmspace_acquire_ref(p);
1997 if (vm == NULL) {
1998 PRELE(p);
1999 return (ESRCH);
2000 }
2001 kve = malloc(sizeof(*kve), M_TEMP, M_WAITOK);
2002
2003 map = &vm->vm_map;
2004 vm_map_lock_read(map);
2005 for (entry = map->header.next; entry != &map->header;
2006 entry = entry->next) {
2007 vm_object_t obj, tobj, lobj;
2008 vm_offset_t addr;
2009
2010 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
2011 continue;
2012
2013 bzero(kve, sizeof(*kve));
2014 kve->kve_structsize = sizeof(*kve);
2015
2016 kve->kve_private_resident = 0;
2017 obj = entry->object.vm_object;
2018 if (obj != NULL) {
2019 VM_OBJECT_RLOCK(obj);
2020 if (obj->shadow_count == 1)
2021 kve->kve_private_resident =
2022 obj->resident_page_count;
2023 }
2024 kve->kve_resident = 0;
2025 addr = entry->start;
2026 while (addr < entry->end) {
2027 if (pmap_extract(map->pmap, addr))
2028 kve->kve_resident++;
2029 addr += PAGE_SIZE;
2030 }
2031
2032 for (lobj = tobj = obj; tobj; tobj = tobj->backing_object) {
2033 if (tobj != obj)
2034 VM_OBJECT_RLOCK(tobj);
2035 if (lobj != obj)
2036 VM_OBJECT_RUNLOCK(lobj);
2037 lobj = tobj;
2038 }
2039
2040 kve->kve_start = (void*)entry->start;
2041 kve->kve_end = (void*)entry->end;
2042 kve->kve_offset = (off_t)entry->offset;
2043
2044 if (entry->protection & VM_PROT_READ)
2045 kve->kve_protection |= KVME_PROT_READ;
2046 if (entry->protection & VM_PROT_WRITE)
2047 kve->kve_protection |= KVME_PROT_WRITE;
2048 if (entry->protection & VM_PROT_EXECUTE)
2049 kve->kve_protection |= KVME_PROT_EXEC;
2050
2051 if (entry->eflags & MAP_ENTRY_COW)
2052 kve->kve_flags |= KVME_FLAG_COW;
2053 if (entry->eflags & MAP_ENTRY_NEEDS_COPY)
2054 kve->kve_flags |= KVME_FLAG_NEEDS_COPY;
2055 if (entry->eflags & MAP_ENTRY_NOCOREDUMP)
2056 kve->kve_flags |= KVME_FLAG_NOCOREDUMP;
2057
2058 last_timestamp = map->timestamp;
2059 vm_map_unlock_read(map);
2060
2061 kve->kve_fileid = 0;
2062 kve->kve_fsid = 0;
2063 freepath = NULL;
2064 fullpath = "";
2065 if (lobj) {
2066 vp = NULL;
2067 switch (lobj->type) {
2068 case OBJT_DEFAULT:
2069 kve->kve_type = KVME_TYPE_DEFAULT;
2070 break;
2071 case OBJT_VNODE:
2072 kve->kve_type = KVME_TYPE_VNODE;
2073 vp = lobj->handle;
2074 vref(vp);
2075 break;
2076 case OBJT_SWAP:
2077 kve->kve_type = KVME_TYPE_SWAP;
2078 break;
2079 case OBJT_DEVICE:
2080 kve->kve_type = KVME_TYPE_DEVICE;
2081 break;
2082 case OBJT_PHYS:
2083 kve->kve_type = KVME_TYPE_PHYS;
2084 break;
2085 case OBJT_DEAD:
2086 kve->kve_type = KVME_TYPE_DEAD;
2087 break;
2088 case OBJT_SG:
2089 kve->kve_type = KVME_TYPE_SG;
2090 break;
2091 default:
2092 kve->kve_type = KVME_TYPE_UNKNOWN;
2093 break;
2094 }
2095 if (lobj != obj)
2096 VM_OBJECT_RUNLOCK(lobj);
2097
2098 kve->kve_ref_count = obj->ref_count;
2099 kve->kve_shadow_count = obj->shadow_count;
2100 VM_OBJECT_RUNLOCK(obj);
2101 if (vp != NULL) {
2102 vn_fullpath(curthread, vp, &fullpath,
2103 &freepath);
2104 cred = curthread->td_ucred;
2105 vn_lock(vp, LK_SHARED | LK_RETRY);
2106 if (VOP_GETATTR(vp, &va, cred) == 0) {
2107 kve->kve_fileid = va.va_fileid;
2108 kve->kve_fsid = va.va_fsid;
2109 }
2110 vput(vp);
2111 }
2112 } else {
2113 kve->kve_type = KVME_TYPE_NONE;
2114 kve->kve_ref_count = 0;
2115 kve->kve_shadow_count = 0;
2116 }
2117
2118 strlcpy(kve->kve_path, fullpath, sizeof(kve->kve_path));
2119 if (freepath != NULL)
2120 free(freepath, M_TEMP);
2121
2122 error = SYSCTL_OUT(req, kve, sizeof(*kve));
2123 vm_map_lock_read(map);
2124 if (error)
2125 break;
2126 if (last_timestamp != map->timestamp) {
2127 vm_map_lookup_entry(map, addr - 1, &tmp_entry);
2128 entry = tmp_entry;
2129 }
2130 }
2131 vm_map_unlock_read(map);
2132 vmspace_free(vm);
2133 PRELE(p);
2134 free(kve, M_TEMP);
2135 return (error);
2136 }
2137 #endif /* COMPAT_FREEBSD7 */
2138
2139 #ifdef KINFO_VMENTRY_SIZE
2140 CTASSERT(sizeof(struct kinfo_vmentry) == KINFO_VMENTRY_SIZE);
2141 #endif
2142
2143 static void
2144 kern_proc_vmmap_resident(vm_map_t map, vm_map_entry_t entry,
2145 struct kinfo_vmentry *kve)
2146 {
2147 vm_object_t obj, tobj;
2148 vm_page_t m, m_adv;
2149 vm_offset_t addr;
2150 vm_paddr_t locked_pa;
2151 vm_pindex_t pi, pi_adv, pindex;
2152
2153 locked_pa = 0;
2154 obj = entry->object.vm_object;
2155 addr = entry->start;
2156 m_adv = NULL;
2157 pi = OFF_TO_IDX(entry->offset);
2158 for (; addr < entry->end; addr += IDX_TO_OFF(pi_adv), pi += pi_adv) {
2159 if (m_adv != NULL) {
2160 m = m_adv;
2161 } else {
2162 pi_adv = OFF_TO_IDX(entry->end - addr);
2163 pindex = pi;
2164 for (tobj = obj;; tobj = tobj->backing_object) {
2165 m = vm_page_find_least(tobj, pindex);
2166 if (m != NULL) {
2167 if (m->pindex == pindex)
2168 break;
2169 if (pi_adv > m->pindex - pindex) {
2170 pi_adv = m->pindex - pindex;
2171 m_adv = m;
2172 }
2173 }
2174 if (tobj->backing_object == NULL)
2175 goto next;
2176 pindex += OFF_TO_IDX(tobj->
2177 backing_object_offset);
2178 }
2179 }
2180 m_adv = NULL;
2181 if (m->psind != 0 && addr + pagesizes[1] <= entry->end &&
2182 (addr & (pagesizes[1] - 1)) == 0 &&
2183 (pmap_mincore(map->pmap, addr, &locked_pa) &
2184 MINCORE_SUPER) != 0) {
2185 kve->kve_flags |= KVME_FLAG_SUPER;
2186 pi_adv = OFF_TO_IDX(pagesizes[1]);
2187 } else {
2188 /*
2189 * We do not test the found page on validity.
2190 * Either the page is busy and being paged in,
2191 * or it was invalidated. The first case
2192 * should be counted as resident, the second
2193 * is not so clear; we do account both.
2194 */
2195 pi_adv = 1;
2196 }
2197 kve->kve_resident += pi_adv;
2198 next:;
2199 }
2200 PA_UNLOCK_COND(locked_pa);
2201 }
2202
2203 /*
2204 * Must be called with the process locked and will return unlocked.
2205 */
2206 int
2207 kern_proc_vmmap_out(struct proc *p, struct sbuf *sb)
2208 {
2209 vm_map_entry_t entry, tmp_entry;
2210 struct vattr va;
2211 vm_map_t map;
2212 vm_object_t obj, tobj, lobj;
2213 char *fullpath, *freepath;
2214 struct kinfo_vmentry *kve;
2215 struct ucred *cred;
2216 struct vnode *vp;
2217 struct vmspace *vm;
2218 vm_offset_t addr;
2219 unsigned int last_timestamp;
2220 int error;
2221
2222 PROC_LOCK_ASSERT(p, MA_OWNED);
2223
2224 _PHOLD(p);
2225 PROC_UNLOCK(p);
2226 vm = vmspace_acquire_ref(p);
2227 if (vm == NULL) {
2228 PRELE(p);
2229 return (ESRCH);
2230 }
2231 kve = malloc(sizeof(*kve), M_TEMP, M_WAITOK);
2232
2233 error = 0;
2234 map = &vm->vm_map;
2235 vm_map_lock_read(map);
2236 for (entry = map->header.next; entry != &map->header;
2237 entry = entry->next) {
2238 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
2239 continue;
2240
2241 addr = entry->end;
2242 bzero(kve, sizeof(*kve));
2243 obj = entry->object.vm_object;
2244 if (obj != NULL) {
2245 for (tobj = obj; tobj != NULL;
2246 tobj = tobj->backing_object) {
2247 VM_OBJECT_RLOCK(tobj);
2248 lobj = tobj;
2249 }
2250 if (obj->backing_object == NULL)
2251 kve->kve_private_resident =
2252 obj->resident_page_count;
2253 if (!vmmap_skip_res_cnt)
2254 kern_proc_vmmap_resident(map, entry, kve);
2255 for (tobj = obj; tobj != NULL;
2256 tobj = tobj->backing_object) {
2257 if (tobj != obj && tobj != lobj)
2258 VM_OBJECT_RUNLOCK(tobj);
2259 }
2260 } else {
2261 lobj = NULL;
2262 }
2263
2264 kve->kve_start = entry->start;
2265 kve->kve_end = entry->end;
2266 kve->kve_offset = entry->offset;
2267
2268 if (entry->protection & VM_PROT_READ)
2269 kve->kve_protection |= KVME_PROT_READ;
2270 if (entry->protection & VM_PROT_WRITE)
2271 kve->kve_protection |= KVME_PROT_WRITE;
2272 if (entry->protection & VM_PROT_EXECUTE)
2273 kve->kve_protection |= KVME_PROT_EXEC;
2274
2275 if (entry->eflags & MAP_ENTRY_COW)
2276 kve->kve_flags |= KVME_FLAG_COW;
2277 if (entry->eflags & MAP_ENTRY_NEEDS_COPY)
2278 kve->kve_flags |= KVME_FLAG_NEEDS_COPY;
2279 if (entry->eflags & MAP_ENTRY_NOCOREDUMP)
2280 kve->kve_flags |= KVME_FLAG_NOCOREDUMP;
2281 if (entry->eflags & MAP_ENTRY_GROWS_UP)
2282 kve->kve_flags |= KVME_FLAG_GROWS_UP;
2283 if (entry->eflags & MAP_ENTRY_GROWS_DOWN)
2284 kve->kve_flags |= KVME_FLAG_GROWS_DOWN;
2285
2286 last_timestamp = map->timestamp;
2287 vm_map_unlock_read(map);
2288
2289 freepath = NULL;
2290 fullpath = "";
2291 if (lobj != NULL) {
2292 vp = NULL;
2293 switch (lobj->type) {
2294 case OBJT_DEFAULT:
2295 kve->kve_type = KVME_TYPE_DEFAULT;
2296 break;
2297 case OBJT_VNODE:
2298 kve->kve_type = KVME_TYPE_VNODE;
2299 vp = lobj->handle;
2300 vref(vp);
2301 break;
2302 case OBJT_SWAP:
2303 kve->kve_type = KVME_TYPE_SWAP;
2304 break;
2305 case OBJT_DEVICE:
2306 kve->kve_type = KVME_TYPE_DEVICE;
2307 break;
2308 case OBJT_PHYS:
2309 kve->kve_type = KVME_TYPE_PHYS;
2310 break;
2311 case OBJT_DEAD:
2312 kve->kve_type = KVME_TYPE_DEAD;
2313 break;
2314 case OBJT_SG:
2315 kve->kve_type = KVME_TYPE_SG;
2316 break;
2317 case OBJT_MGTDEVICE:
2318 kve->kve_type = KVME_TYPE_MGTDEVICE;
2319 break;
2320 default:
2321 kve->kve_type = KVME_TYPE_UNKNOWN;
2322 break;
2323 }
2324 if (lobj != obj)
2325 VM_OBJECT_RUNLOCK(lobj);
2326
2327 kve->kve_ref_count = obj->ref_count;
2328 kve->kve_shadow_count = obj->shadow_count;
2329 VM_OBJECT_RUNLOCK(obj);
2330 if (vp != NULL) {
2331 vn_fullpath(curthread, vp, &fullpath,
2332 &freepath);
2333 kve->kve_vn_type = vntype_to_kinfo(vp->v_type);
2334 cred = curthread->td_ucred;
2335 vn_lock(vp, LK_SHARED | LK_RETRY);
2336 if (VOP_GETATTR(vp, &va, cred) == 0) {
2337 kve->kve_vn_fileid = va.va_fileid;
2338 kve->kve_vn_fsid = va.va_fsid;
2339 kve->kve_vn_mode =
2340 MAKEIMODE(va.va_type, va.va_mode);
2341 kve->kve_vn_size = va.va_size;
2342 kve->kve_vn_rdev = va.va_rdev;
2343 kve->kve_status = KF_ATTR_VALID;
2344 }
2345 vput(vp);
2346 }
2347 } else {
2348 kve->kve_type = KVME_TYPE_NONE;
2349 kve->kve_ref_count = 0;
2350 kve->kve_shadow_count = 0;
2351 }
2352
2353 strlcpy(kve->kve_path, fullpath, sizeof(kve->kve_path));
2354 if (freepath != NULL)
2355 free(freepath, M_TEMP);
2356
2357 /* Pack record size down */
2358 kve->kve_structsize = offsetof(struct kinfo_vmentry, kve_path) +
2359 strlen(kve->kve_path) + 1;
2360 kve->kve_structsize = roundup(kve->kve_structsize,
2361 sizeof(uint64_t));
2362 if (sbuf_bcat(sb, kve, kve->kve_structsize) != 0)
2363 error = ENOMEM;
2364 vm_map_lock_read(map);
2365 if (error != 0)
2366 break;
2367 if (last_timestamp != map->timestamp) {
2368 vm_map_lookup_entry(map, addr - 1, &tmp_entry);
2369 entry = tmp_entry;
2370 }
2371 }
2372 vm_map_unlock_read(map);
2373 vmspace_free(vm);
2374 PRELE(p);
2375 free(kve, M_TEMP);
2376 return (error);
2377 }
2378
2379 static int
2380 sysctl_kern_proc_vmmap(SYSCTL_HANDLER_ARGS)
2381 {
2382 struct proc *p;
2383 struct sbuf sb;
2384 int error, error2, *name;
2385
2386 name = (int *)arg1;
2387 sbuf_new_for_sysctl(&sb, NULL, sizeof(struct kinfo_vmentry), req);
2388 error = pget((pid_t)name[0], PGET_CANDEBUG | PGET_NOTWEXIT, &p);
2389 if (error != 0) {
2390 sbuf_delete(&sb);
2391 return (error);
2392 }
2393 error = kern_proc_vmmap_out(p, &sb);
2394 error2 = sbuf_finish(&sb);
2395 sbuf_delete(&sb);
2396 return (error != 0 ? error : error2);
2397 }
2398
2399 #if defined(STACK) || defined(DDB)
2400 static int
2401 sysctl_kern_proc_kstack(SYSCTL_HANDLER_ARGS)
2402 {
2403 struct kinfo_kstack *kkstp;
2404 int error, i, *name, numthreads;
2405 lwpid_t *lwpidarray;
2406 struct thread *td;
2407 struct stack *st;
2408 struct sbuf sb;
2409 struct proc *p;
2410
2411 name = (int *)arg1;
2412 error = pget((pid_t)name[0], PGET_NOTINEXEC | PGET_WANTREAD, &p);
2413 if (error != 0)
2414 return (error);
2415
2416 kkstp = malloc(sizeof(*kkstp), M_TEMP, M_WAITOK);
2417 st = stack_create();
2418
2419 lwpidarray = NULL;
2420 numthreads = 0;
2421 PROC_LOCK(p);
2422 repeat:
2423 if (numthreads < p->p_numthreads) {
2424 if (lwpidarray != NULL) {
2425 free(lwpidarray, M_TEMP);
2426 lwpidarray = NULL;
2427 }
2428 numthreads = p->p_numthreads;
2429 PROC_UNLOCK(p);
2430 lwpidarray = malloc(sizeof(*lwpidarray) * numthreads, M_TEMP,
2431 M_WAITOK | M_ZERO);
2432 PROC_LOCK(p);
2433 goto repeat;
2434 }
2435 i = 0;
2436
2437 /*
2438 * XXXRW: During the below loop, execve(2) and countless other sorts
2439 * of changes could have taken place. Should we check to see if the
2440 * vmspace has been replaced, or the like, in order to prevent
2441 * giving a snapshot that spans, say, execve(2), with some threads
2442 * before and some after? Among other things, the credentials could
2443 * have changed, in which case the right to extract debug info might
2444 * no longer be assured.
2445 */
2446 FOREACH_THREAD_IN_PROC(p, td) {
2447 KASSERT(i < numthreads,
2448 ("sysctl_kern_proc_kstack: numthreads"));
2449 lwpidarray[i] = td->td_tid;
2450 i++;
2451 }
2452 numthreads = i;
2453 for (i = 0; i < numthreads; i++) {
2454 td = thread_find(p, lwpidarray[i]);
2455 if (td == NULL) {
2456 continue;
2457 }
2458 bzero(kkstp, sizeof(*kkstp));
2459 (void)sbuf_new(&sb, kkstp->kkst_trace,
2460 sizeof(kkstp->kkst_trace), SBUF_FIXEDLEN);
2461 thread_lock(td);
2462 kkstp->kkst_tid = td->td_tid;
2463 if (TD_IS_SWAPPED(td))
2464 kkstp->kkst_state = KKST_STATE_SWAPPED;
2465 else if (TD_IS_RUNNING(td))
2466 kkstp->kkst_state = KKST_STATE_RUNNING;
2467 else {
2468 kkstp->kkst_state = KKST_STATE_STACKOK;
2469 stack_save_td(st, td);
2470 }
2471 thread_unlock(td);
2472 PROC_UNLOCK(p);
2473 stack_sbuf_print(&sb, st);
2474 sbuf_finish(&sb);
2475 sbuf_delete(&sb);
2476 error = SYSCTL_OUT(req, kkstp, sizeof(*kkstp));
2477 PROC_LOCK(p);
2478 if (error)
2479 break;
2480 }
2481 _PRELE(p);
2482 PROC_UNLOCK(p);
2483 if (lwpidarray != NULL)
2484 free(lwpidarray, M_TEMP);
2485 stack_destroy(st);
2486 free(kkstp, M_TEMP);
2487 return (error);
2488 }
2489 #endif
2490
2491 /*
2492 * This sysctl allows a process to retrieve the full list of groups from
2493 * itself or another process.
2494 */
2495 static int
2496 sysctl_kern_proc_groups(SYSCTL_HANDLER_ARGS)
2497 {
2498 pid_t *pidp = (pid_t *)arg1;
2499 unsigned int arglen = arg2;
2500 struct proc *p;
2501 struct ucred *cred;
2502 int error;
2503
2504 if (arglen != 1)
2505 return (EINVAL);
2506 if (*pidp == -1) { /* -1 means this process */
2507 p = req->td->td_proc;
2508 } else {
2509 error = pget(*pidp, PGET_CANSEE, &p);
2510 if (error != 0)
2511 return (error);
2512 }
2513
2514 cred = crhold(p->p_ucred);
2515 if (*pidp != -1)
2516 PROC_UNLOCK(p);
2517
2518 error = SYSCTL_OUT(req, cred->cr_groups,
2519 cred->cr_ngroups * sizeof(gid_t));
2520 crfree(cred);
2521 return (error);
2522 }
2523
2524 /*
2525 * This sysctl allows a process to retrieve or/and set the resource limit for
2526 * another process.
2527 */
2528 static int
2529 sysctl_kern_proc_rlimit(SYSCTL_HANDLER_ARGS)
2530 {
2531 int *name = (int *)arg1;
2532 u_int namelen = arg2;
2533 struct rlimit rlim;
2534 struct proc *p;
2535 u_int which;
2536 int flags, error;
2537
2538 if (namelen != 2)
2539 return (EINVAL);
2540
2541 which = (u_int)name[1];
2542 if (which >= RLIM_NLIMITS)
2543 return (EINVAL);
2544
2545 if (req->newptr != NULL && req->newlen != sizeof(rlim))
2546 return (EINVAL);
2547
2548 flags = PGET_HOLD | PGET_NOTWEXIT;
2549 if (req->newptr != NULL)
2550 flags |= PGET_CANDEBUG;
2551 else
2552 flags |= PGET_CANSEE;
2553 error = pget((pid_t)name[0], flags, &p);
2554 if (error != 0)
2555 return (error);
2556
2557 /*
2558 * Retrieve limit.
2559 */
2560 if (req->oldptr != NULL) {
2561 PROC_LOCK(p);
2562 lim_rlimit(p, which, &rlim);
2563 PROC_UNLOCK(p);
2564 }
2565 error = SYSCTL_OUT(req, &rlim, sizeof(rlim));
2566 if (error != 0)
2567 goto errout;
2568
2569 /*
2570 * Set limit.
2571 */
2572 if (req->newptr != NULL) {
2573 error = SYSCTL_IN(req, &rlim, sizeof(rlim));
2574 if (error == 0)
2575 error = kern_proc_setrlimit(curthread, p, which, &rlim);
2576 }
2577
2578 errout:
2579 PRELE(p);
2580 return (error);
2581 }
2582
2583 /*
2584 * This sysctl allows a process to retrieve ps_strings structure location of
2585 * another process.
2586 */
2587 static int
2588 sysctl_kern_proc_ps_strings(SYSCTL_HANDLER_ARGS)
2589 {
2590 int *name = (int *)arg1;
2591 u_int namelen = arg2;
2592 struct proc *p;
2593 vm_offset_t ps_strings;
2594 int error;
2595 #ifdef COMPAT_FREEBSD32
2596 uint32_t ps_strings32;
2597 #endif
2598
2599 if (namelen != 1)
2600 return (EINVAL);
2601
2602 error = pget((pid_t)name[0], PGET_CANDEBUG, &p);
2603 if (error != 0)
2604 return (error);
2605 #ifdef COMPAT_FREEBSD32
2606 if ((req->flags & SCTL_MASK32) != 0) {
2607 /*
2608 * We return 0 if the 32 bit emulation request is for a 64 bit
2609 * process.
2610 */
2611 ps_strings32 = SV_PROC_FLAG(p, SV_ILP32) != 0 ?
2612 PTROUT(p->p_sysent->sv_psstrings) : 0;
2613 PROC_UNLOCK(p);
2614 error = SYSCTL_OUT(req, &ps_strings32, sizeof(ps_strings32));
2615 return (error);
2616 }
2617 #endif
2618 ps_strings = p->p_sysent->sv_psstrings;
2619 PROC_UNLOCK(p);
2620 error = SYSCTL_OUT(req, &ps_strings, sizeof(ps_strings));
2621 return (error);
2622 }
2623
2624 /*
2625 * This sysctl allows a process to retrieve umask of another process.
2626 */
2627 static int
2628 sysctl_kern_proc_umask(SYSCTL_HANDLER_ARGS)
2629 {
2630 int *name = (int *)arg1;
2631 u_int namelen = arg2;
2632 struct proc *p;
2633 int error;
2634 u_short fd_cmask;
2635
2636 if (namelen != 1)
2637 return (EINVAL);
2638
2639 error = pget((pid_t)name[0], PGET_WANTREAD, &p);
2640 if (error != 0)
2641 return (error);
2642
2643 FILEDESC_SLOCK(p->p_fd);
2644 fd_cmask = p->p_fd->fd_cmask;
2645 FILEDESC_SUNLOCK(p->p_fd);
2646 PRELE(p);
2647 error = SYSCTL_OUT(req, &fd_cmask, sizeof(fd_cmask));
2648 return (error);
2649 }
2650
2651 /*
2652 * This sysctl allows a process to set and retrieve binary osreldate of
2653 * another process.
2654 */
2655 static int
2656 sysctl_kern_proc_osrel(SYSCTL_HANDLER_ARGS)
2657 {
2658 int *name = (int *)arg1;
2659 u_int namelen = arg2;
2660 struct proc *p;
2661 int flags, error, osrel;
2662
2663 if (namelen != 1)
2664 return (EINVAL);
2665
2666 if (req->newptr != NULL && req->newlen != sizeof(osrel))
2667 return (EINVAL);
2668
2669 flags = PGET_HOLD | PGET_NOTWEXIT;
2670 if (req->newptr != NULL)
2671 flags |= PGET_CANDEBUG;
2672 else
2673 flags |= PGET_CANSEE;
2674 error = pget((pid_t)name[0], flags, &p);
2675 if (error != 0)
2676 return (error);
2677
2678 error = SYSCTL_OUT(req, &p->p_osrel, sizeof(p->p_osrel));
2679 if (error != 0)
2680 goto errout;
2681
2682 if (req->newptr != NULL) {
2683 error = SYSCTL_IN(req, &osrel, sizeof(osrel));
2684 if (error != 0)
2685 goto errout;
2686 if (osrel < 0) {
2687 error = EINVAL;
2688 goto errout;
2689 }
2690 p->p_osrel = osrel;
2691 }
2692 errout:
2693 PRELE(p);
2694 return (error);
2695 }
2696
2697 static int
2698 sysctl_kern_proc_sigtramp(SYSCTL_HANDLER_ARGS)
2699 {
2700 int *name = (int *)arg1;
2701 u_int namelen = arg2;
2702 struct proc *p;
2703 struct kinfo_sigtramp kst;
2704 const struct sysentvec *sv;
2705 int error;
2706 #ifdef COMPAT_FREEBSD32
2707 struct kinfo_sigtramp32 kst32;
2708 #endif
2709
2710 if (namelen != 1)
2711 return (EINVAL);
2712
2713 error = pget((pid_t)name[0], PGET_CANDEBUG, &p);
2714 if (error != 0)
2715 return (error);
2716 sv = p->p_sysent;
2717 #ifdef COMPAT_FREEBSD32
2718 if ((req->flags & SCTL_MASK32) != 0) {
2719 bzero(&kst32, sizeof(kst32));
2720 if (SV_PROC_FLAG(p, SV_ILP32)) {
2721 if (sv->sv_sigcode_base != 0) {
2722 kst32.ksigtramp_start = sv->sv_sigcode_base;
2723 kst32.ksigtramp_end = sv->sv_sigcode_base +
2724 *sv->sv_szsigcode;
2725 } else {
2726 kst32.ksigtramp_start = sv->sv_psstrings -
2727 *sv->sv_szsigcode;
2728 kst32.ksigtramp_end = sv->sv_psstrings;
2729 }
2730 }
2731 PROC_UNLOCK(p);
2732 error = SYSCTL_OUT(req, &kst32, sizeof(kst32));
2733 return (error);
2734 }
2735 #endif
2736 bzero(&kst, sizeof(kst));
2737 if (sv->sv_sigcode_base != 0) {
2738 kst.ksigtramp_start = (char *)sv->sv_sigcode_base;
2739 kst.ksigtramp_end = (char *)sv->sv_sigcode_base +
2740 *sv->sv_szsigcode;
2741 } else {
2742 kst.ksigtramp_start = (char *)sv->sv_psstrings -
2743 *sv->sv_szsigcode;
2744 kst.ksigtramp_end = (char *)sv->sv_psstrings;
2745 }
2746 PROC_UNLOCK(p);
2747 error = SYSCTL_OUT(req, &kst, sizeof(kst));
2748 return (error);
2749 }
2750
2751 SYSCTL_NODE(_kern, KERN_PROC, proc, CTLFLAG_RD, 0, "Process table");
2752
2753 SYSCTL_PROC(_kern_proc, KERN_PROC_ALL, all, CTLFLAG_RD|CTLTYPE_STRUCT|
2754 CTLFLAG_MPSAFE, 0, 0, sysctl_kern_proc, "S,proc",
2755 "Return entire process table");
2756
2757 static SYSCTL_NODE(_kern_proc, KERN_PROC_GID, gid, CTLFLAG_RD | CTLFLAG_MPSAFE,
2758 sysctl_kern_proc, "Process table");
2759
2760 static SYSCTL_NODE(_kern_proc, KERN_PROC_PGRP, pgrp, CTLFLAG_RD | CTLFLAG_MPSAFE,
2761 sysctl_kern_proc, "Process table");
2762
2763 static SYSCTL_NODE(_kern_proc, KERN_PROC_RGID, rgid, CTLFLAG_RD | CTLFLAG_MPSAFE,
2764 sysctl_kern_proc, "Process table");
2765
2766 static SYSCTL_NODE(_kern_proc, KERN_PROC_SESSION, sid, CTLFLAG_RD |
2767 CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2768
2769 static SYSCTL_NODE(_kern_proc, KERN_PROC_TTY, tty, CTLFLAG_RD | CTLFLAG_MPSAFE,
2770 sysctl_kern_proc, "Process table");
2771
2772 static SYSCTL_NODE(_kern_proc, KERN_PROC_UID, uid, CTLFLAG_RD | CTLFLAG_MPSAFE,
2773 sysctl_kern_proc, "Process table");
2774
2775 static SYSCTL_NODE(_kern_proc, KERN_PROC_RUID, ruid, CTLFLAG_RD | CTLFLAG_MPSAFE,
2776 sysctl_kern_proc, "Process table");
2777
2778 static SYSCTL_NODE(_kern_proc, KERN_PROC_PID, pid, CTLFLAG_RD | CTLFLAG_MPSAFE,
2779 sysctl_kern_proc, "Process table");
2780
2781 static SYSCTL_NODE(_kern_proc, KERN_PROC_PROC, proc, CTLFLAG_RD | CTLFLAG_MPSAFE,
2782 sysctl_kern_proc, "Return process table, no threads");
2783
2784 static SYSCTL_NODE(_kern_proc, KERN_PROC_ARGS, args,
2785 CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_MPSAFE,
2786 sysctl_kern_proc_args, "Process argument list");
2787
2788 static SYSCTL_NODE(_kern_proc, KERN_PROC_ENV, env, CTLFLAG_RD | CTLFLAG_MPSAFE,
2789 sysctl_kern_proc_env, "Process environment");
2790
2791 static SYSCTL_NODE(_kern_proc, KERN_PROC_AUXV, auxv, CTLFLAG_RD |
2792 CTLFLAG_MPSAFE, sysctl_kern_proc_auxv, "Process ELF auxiliary vector");
2793
2794 static SYSCTL_NODE(_kern_proc, KERN_PROC_PATHNAME, pathname, CTLFLAG_RD |
2795 CTLFLAG_MPSAFE, sysctl_kern_proc_pathname, "Process executable path");
2796
2797 static SYSCTL_NODE(_kern_proc, KERN_PROC_SV_NAME, sv_name, CTLFLAG_RD |
2798 CTLFLAG_MPSAFE, sysctl_kern_proc_sv_name,
2799 "Process syscall vector name (ABI type)");
2800
2801 static SYSCTL_NODE(_kern_proc, (KERN_PROC_GID | KERN_PROC_INC_THREAD), gid_td,
2802 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2803
2804 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PGRP | KERN_PROC_INC_THREAD), pgrp_td,
2805 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2806
2807 static SYSCTL_NODE(_kern_proc, (KERN_PROC_RGID | KERN_PROC_INC_THREAD), rgid_td,
2808 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2809
2810 static SYSCTL_NODE(_kern_proc, (KERN_PROC_SESSION | KERN_PROC_INC_THREAD),
2811 sid_td, CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2812
2813 static SYSCTL_NODE(_kern_proc, (KERN_PROC_TTY | KERN_PROC_INC_THREAD), tty_td,
2814 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2815
2816 static SYSCTL_NODE(_kern_proc, (KERN_PROC_UID | KERN_PROC_INC_THREAD), uid_td,
2817 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2818
2819 static SYSCTL_NODE(_kern_proc, (KERN_PROC_RUID | KERN_PROC_INC_THREAD), ruid_td,
2820 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2821
2822 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PID | KERN_PROC_INC_THREAD), pid_td,
2823 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2824
2825 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PROC | KERN_PROC_INC_THREAD), proc_td,
2826 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc,
2827 "Return process table, no threads");
2828
2829 #ifdef COMPAT_FREEBSD7
2830 static SYSCTL_NODE(_kern_proc, KERN_PROC_OVMMAP, ovmmap, CTLFLAG_RD |
2831 CTLFLAG_MPSAFE, sysctl_kern_proc_ovmmap, "Old Process vm map entries");
2832 #endif
2833
2834 static SYSCTL_NODE(_kern_proc, KERN_PROC_VMMAP, vmmap, CTLFLAG_RD |
2835 CTLFLAG_MPSAFE, sysctl_kern_proc_vmmap, "Process vm map entries");
2836
2837 #if defined(STACK) || defined(DDB)
2838 static SYSCTL_NODE(_kern_proc, KERN_PROC_KSTACK, kstack, CTLFLAG_RD |
2839 CTLFLAG_MPSAFE, sysctl_kern_proc_kstack, "Process kernel stacks");
2840 #endif
2841
2842 static SYSCTL_NODE(_kern_proc, KERN_PROC_GROUPS, groups, CTLFLAG_RD |
2843 CTLFLAG_MPSAFE, sysctl_kern_proc_groups, "Process groups");
2844
2845 static SYSCTL_NODE(_kern_proc, KERN_PROC_RLIMIT, rlimit, CTLFLAG_RW |
2846 CTLFLAG_ANYBODY | CTLFLAG_MPSAFE, sysctl_kern_proc_rlimit,
2847 "Process resource limits");
2848
2849 static SYSCTL_NODE(_kern_proc, KERN_PROC_PS_STRINGS, ps_strings, CTLFLAG_RD |
2850 CTLFLAG_MPSAFE, sysctl_kern_proc_ps_strings,
2851 "Process ps_strings location");
2852
2853 static SYSCTL_NODE(_kern_proc, KERN_PROC_UMASK, umask, CTLFLAG_RD |
2854 CTLFLAG_MPSAFE, sysctl_kern_proc_umask, "Process umask");
2855
2856 static SYSCTL_NODE(_kern_proc, KERN_PROC_OSREL, osrel, CTLFLAG_RW |
2857 CTLFLAG_ANYBODY | CTLFLAG_MPSAFE, sysctl_kern_proc_osrel,
2858 "Process binary osreldate");
2859
2860 static SYSCTL_NODE(_kern_proc, KERN_PROC_SIGTRAMP, sigtramp, CTLFLAG_RD |
2861 CTLFLAG_MPSAFE, sysctl_kern_proc_sigtramp,
2862 "Process signal trampoline location");
2863
2864 int allproc_gen;
2865
2866 void
2867 stop_all_proc(void)
2868 {
2869 struct proc *cp, *p;
2870 int r, gen;
2871 bool restart, seen_stopped, seen_exiting, stopped_some;
2872
2873 cp = curproc;
2874 /*
2875 * stop_all_proc() assumes that all process which have
2876 * usermode must be stopped, except current process, for
2877 * obvious reasons. Since other threads in the process
2878 * establishing global stop could unstop something, disable
2879 * calls from multithreaded processes as precaution. The
2880 * service must not be user-callable anyway.
2881 */
2882 KASSERT((cp->p_flag & P_HADTHREADS) == 0 ||
2883 (cp->p_flag & P_KTHREAD) != 0, ("mt stop_all_proc"));
2884
2885 allproc_loop:
2886 sx_xlock(&allproc_lock);
2887 gen = allproc_gen;
2888 seen_exiting = seen_stopped = stopped_some = restart = false;
2889 LIST_REMOVE(cp, p_list);
2890 LIST_INSERT_HEAD(&allproc, cp, p_list);
2891 for (;;) {
2892 p = LIST_NEXT(cp, p_list);
2893 if (p == NULL)
2894 break;
2895 LIST_REMOVE(cp, p_list);
2896 LIST_INSERT_AFTER(p, cp, p_list);
2897 PROC_LOCK(p);
2898 if ((p->p_flag & (P_KTHREAD | P_SYSTEM |
2899 P_TOTAL_STOP)) != 0) {
2900 PROC_UNLOCK(p);
2901 continue;
2902 }
2903 if ((p->p_flag & P_WEXIT) != 0) {
2904 seen_exiting = true;
2905 PROC_UNLOCK(p);
2906 continue;
2907 }
2908 if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) {
2909 /*
2910 * Stopped processes are tolerated when there
2911 * are no other processes which might continue
2912 * them. P_STOPPED_SINGLE but not
2913 * P_TOTAL_STOP process still has at least one
2914 * thread running.
2915 */
2916 seen_stopped = true;
2917 PROC_UNLOCK(p);
2918 continue;
2919 }
2920 _PHOLD(p);
2921 sx_xunlock(&allproc_lock);
2922 r = thread_single(p, SINGLE_ALLPROC);
2923 if (r != 0)
2924 restart = true;
2925 else
2926 stopped_some = true;
2927 _PRELE(p);
2928 PROC_UNLOCK(p);
2929 sx_xlock(&allproc_lock);
2930 }
2931 /* Catch forked children we did not see in iteration. */
2932 if (gen != allproc_gen)
2933 restart = true;
2934 sx_xunlock(&allproc_lock);
2935 if (restart || stopped_some || seen_exiting || seen_stopped) {
2936 kern_yield(PRI_USER);
2937 goto allproc_loop;
2938 }
2939 }
2940
2941 void
2942 resume_all_proc(void)
2943 {
2944 struct proc *cp, *p;
2945
2946 cp = curproc;
2947 sx_xlock(&allproc_lock);
2948 LIST_REMOVE(cp, p_list);
2949 LIST_INSERT_HEAD(&allproc, cp, p_list);
2950 for (;;) {
2951 p = LIST_NEXT(cp, p_list);
2952 if (p == NULL)
2953 break;
2954 LIST_REMOVE(cp, p_list);
2955 LIST_INSERT_AFTER(p, cp, p_list);
2956 PROC_LOCK(p);
2957 if ((p->p_flag & P_TOTAL_STOP) != 0) {
2958 sx_xunlock(&allproc_lock);
2959 _PHOLD(p);
2960 thread_single_end(p, SINGLE_ALLPROC);
2961 _PRELE(p);
2962 PROC_UNLOCK(p);
2963 sx_xlock(&allproc_lock);
2964 } else {
2965 PROC_UNLOCK(p);
2966 }
2967 }
2968 sx_xunlock(&allproc_lock);
2969 }
2970
2971 #define TOTAL_STOP_DEBUG 1
2972 #ifdef TOTAL_STOP_DEBUG
2973 volatile static int ap_resume;
2974 #include <sys/mount.h>
2975
2976 static int
2977 sysctl_debug_stop_all_proc(SYSCTL_HANDLER_ARGS)
2978 {
2979 int error, val;
2980
2981 val = 0;
2982 ap_resume = 0;
2983 error = sysctl_handle_int(oidp, &val, 0, req);
2984 if (error != 0 || req->newptr == NULL)
2985 return (error);
2986 if (val != 0) {
2987 stop_all_proc();
2988 syncer_suspend();
2989 while (ap_resume == 0)
2990 ;
2991 syncer_resume();
2992 resume_all_proc();
2993 }
2994 return (0);
2995 }
2996
2997 SYSCTL_PROC(_debug, OID_AUTO, stop_all_proc, CTLTYPE_INT | CTLFLAG_RW |
2998 CTLFLAG_MPSAFE, __DEVOLATILE(int *, &ap_resume), 0,
2999 sysctl_debug_stop_all_proc, "I",
3000 "");
3001 #endif
Cache object: 8a522cbb374bb0f9ec308312500ec679
|