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