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