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