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