kern_proc.c revision 1.221 1 /* $NetBSD: kern_proc.c,v 1.221 2018/11/24 19:22:17 christos Exp $ */
2
3 /*-
4 * Copyright (c) 1999, 2006, 2007, 2008 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 * NASA Ames Research Center, and by Andrew Doran.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 /*
34 * Copyright (c) 1982, 1986, 1989, 1991, 1993
35 * The Regents of the University of California. All rights reserved.
36 *
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
39 * are met:
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * 3. Neither the name of the University nor the names of its contributors
46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission.
48 *
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 * SUCH DAMAGE.
60 *
61 * @(#)kern_proc.c 8.7 (Berkeley) 2/14/95
62 */
63
64 #include <sys/cdefs.h>
65 __KERNEL_RCSID(0, "$NetBSD: kern_proc.c,v 1.221 2018/11/24 19:22:17 christos Exp $");
66
67 #ifdef _KERNEL_OPT
68 #include "opt_kstack.h"
69 #include "opt_maxuprc.h"
70 #include "opt_dtrace.h"
71 #include "opt_compat_netbsd32.h"
72 #endif
73
74 #if defined(__HAVE_COMPAT_NETBSD32) && !defined(COMPAT_NETBSD32) \
75 && !defined(_RUMPKERNEL)
76 #define COMPAT_NETBSD32
77 #endif
78
79 #include <sys/param.h>
80 #include <sys/systm.h>
81 #include <sys/kernel.h>
82 #include <sys/proc.h>
83 #include <sys/resourcevar.h>
84 #include <sys/buf.h>
85 #include <sys/acct.h>
86 #include <sys/wait.h>
87 #include <sys/file.h>
88 #include <ufs/ufs/quota.h>
89 #include <sys/uio.h>
90 #include <sys/pool.h>
91 #include <sys/pset.h>
92 #include <sys/mbuf.h>
93 #include <sys/ioctl.h>
94 #include <sys/tty.h>
95 #include <sys/signalvar.h>
96 #include <sys/ras.h>
97 #include <sys/filedesc.h>
98 #include <sys/syscall_stats.h>
99 #include <sys/kauth.h>
100 #include <sys/sleepq.h>
101 #include <sys/atomic.h>
102 #include <sys/kmem.h>
103 #include <sys/namei.h>
104 #include <sys/dtrace_bsd.h>
105 #include <sys/sysctl.h>
106 #include <sys/exec.h>
107 #include <sys/cpu.h>
108
109 #include <uvm/uvm_extern.h>
110 #include <uvm/uvm.h>
111
112 #ifdef COMPAT_NETBSD32
113 #include <compat/netbsd32/netbsd32.h>
114 #endif
115
116 /*
117 * Process lists.
118 */
119
120 struct proclist allproc __cacheline_aligned;
121 struct proclist zombproc __cacheline_aligned;
122
123 kmutex_t * proc_lock __cacheline_aligned;
124
125 /*
126 * pid to proc lookup is done by indexing the pid_table array.
127 * Since pid numbers are only allocated when an empty slot
128 * has been found, there is no need to search any lists ever.
129 * (an orphaned pgrp will lock the slot, a session will lock
130 * the pgrp with the same number.)
131 * If the table is too small it is reallocated with twice the
132 * previous size and the entries 'unzipped' into the two halves.
133 * A linked list of free entries is passed through the pt_proc
134 * field of 'free' items - set odd to be an invalid ptr.
135 */
136
137 struct pid_table {
138 struct proc *pt_proc;
139 struct pgrp *pt_pgrp;
140 pid_t pt_pid;
141 };
142 #if 1 /* strongly typed cast - should be a noop */
143 static inline uint p2u(struct proc *p) { return (uint)(uintptr_t)p; }
144 #else
145 #define p2u(p) ((uint)p)
146 #endif
147 #define P_VALID(p) (!(p2u(p) & 1))
148 #define P_NEXT(p) (p2u(p) >> 1)
149 #define P_FREE(pid) ((struct proc *)(uintptr_t)((pid) << 1 | 1))
150
151 /*
152 * Table of process IDs (PIDs).
153 */
154 static struct pid_table *pid_table __read_mostly;
155
156 #define INITIAL_PID_TABLE_SIZE (1 << 5)
157
158 /* Table mask, threshold for growing and number of allocated PIDs. */
159 static u_int pid_tbl_mask __read_mostly;
160 static u_int pid_alloc_lim __read_mostly;
161 static u_int pid_alloc_cnt __cacheline_aligned;
162
163 /* Next free, last free and maximum PIDs. */
164 static u_int next_free_pt __cacheline_aligned;
165 static u_int last_free_pt __cacheline_aligned;
166 static pid_t pid_max __read_mostly;
167
168 /* Components of the first process -- never freed. */
169
170 extern struct emul emul_netbsd; /* defined in kern_exec.c */
171
172 struct session session0 = {
173 .s_count = 1,
174 .s_sid = 0,
175 };
176 struct pgrp pgrp0 = {
177 .pg_members = LIST_HEAD_INITIALIZER(&pgrp0.pg_members),
178 .pg_session = &session0,
179 };
180 filedesc_t filedesc0;
181 struct cwdinfo cwdi0 = {
182 .cwdi_cmask = CMASK,
183 .cwdi_refcnt = 1,
184 };
185 struct plimit limit0;
186 struct pstats pstat0;
187 struct vmspace vmspace0;
188 struct sigacts sigacts0;
189 struct proc proc0 = {
190 .p_lwps = LIST_HEAD_INITIALIZER(&proc0.p_lwps),
191 .p_sigwaiters = LIST_HEAD_INITIALIZER(&proc0.p_sigwaiters),
192 .p_nlwps = 1,
193 .p_nrlwps = 1,
194 .p_nlwpid = 1, /* must match lwp0.l_lid */
195 .p_pgrp = &pgrp0,
196 .p_comm = "system",
197 /*
198 * Set P_NOCLDWAIT so that kernel threads are reparented to init(8)
199 * when they exit. init(8) can easily wait them out for us.
200 */
201 .p_flag = PK_SYSTEM | PK_NOCLDWAIT,
202 .p_stat = SACTIVE,
203 .p_nice = NZERO,
204 .p_emul = &emul_netbsd,
205 .p_cwdi = &cwdi0,
206 .p_limit = &limit0,
207 .p_fd = &filedesc0,
208 .p_vmspace = &vmspace0,
209 .p_stats = &pstat0,
210 .p_sigacts = &sigacts0,
211 #ifdef PROC0_MD_INITIALIZERS
212 PROC0_MD_INITIALIZERS
213 #endif
214 };
215 kauth_cred_t cred0;
216
217 static const int nofile = NOFILE;
218 static const int maxuprc = MAXUPRC;
219
220 static int sysctl_doeproc(SYSCTLFN_PROTO);
221 static int sysctl_kern_proc_args(SYSCTLFN_PROTO);
222
223 /*
224 * The process list descriptors, used during pid allocation and
225 * by sysctl. No locking on this data structure is needed since
226 * it is completely static.
227 */
228 const struct proclist_desc proclists[] = {
229 { &allproc },
230 { &zombproc },
231 { NULL },
232 };
233
234 static struct pgrp * pg_remove(pid_t);
235 static void pg_delete(pid_t);
236 static void orphanpg(struct pgrp *);
237
238 static specificdata_domain_t proc_specificdata_domain;
239
240 static pool_cache_t proc_cache;
241
242 static kauth_listener_t proc_listener;
243
244 static void fill_proc(const struct proc *, struct proc *);
245 static int fill_pathname(struct lwp *, pid_t, void *, size_t *);
246
247 static int
248 proc_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie,
249 void *arg0, void *arg1, void *arg2, void *arg3)
250 {
251 struct proc *p;
252 int result;
253
254 result = KAUTH_RESULT_DEFER;
255 p = arg0;
256
257 switch (action) {
258 case KAUTH_PROCESS_CANSEE: {
259 enum kauth_process_req req;
260
261 req = (enum kauth_process_req)arg1;
262
263 switch (req) {
264 case KAUTH_REQ_PROCESS_CANSEE_ARGS:
265 case KAUTH_REQ_PROCESS_CANSEE_ENTRY:
266 case KAUTH_REQ_PROCESS_CANSEE_OPENFILES:
267 case KAUTH_REQ_PROCESS_CANSEE_EPROC:
268 result = KAUTH_RESULT_ALLOW;
269 break;
270
271 case KAUTH_REQ_PROCESS_CANSEE_ENV:
272 if (kauth_cred_getuid(cred) !=
273 kauth_cred_getuid(p->p_cred) ||
274 kauth_cred_getuid(cred) !=
275 kauth_cred_getsvuid(p->p_cred))
276 break;
277
278 result = KAUTH_RESULT_ALLOW;
279
280 break;
281
282 case KAUTH_REQ_PROCESS_CANSEE_KPTR:
283 default:
284 break;
285 }
286
287 break;
288 }
289
290 case KAUTH_PROCESS_FORK: {
291 int lnprocs = (int)(unsigned long)arg2;
292
293 /*
294 * Don't allow a nonprivileged user to use the last few
295 * processes. The variable lnprocs is the current number of
296 * processes, maxproc is the limit.
297 */
298 if (__predict_false((lnprocs >= maxproc - 5)))
299 break;
300
301 result = KAUTH_RESULT_ALLOW;
302
303 break;
304 }
305
306 case KAUTH_PROCESS_CORENAME:
307 case KAUTH_PROCESS_STOPFLAG:
308 if (proc_uidmatch(cred, p->p_cred) == 0)
309 result = KAUTH_RESULT_ALLOW;
310
311 break;
312
313 default:
314 break;
315 }
316
317 return result;
318 }
319
320 static int
321 proc_ctor(void *arg __unused, void *obj, int flags __unused)
322 {
323 memset(obj, 0, sizeof(struct proc));
324 return 0;
325 }
326
327 /*
328 * Initialize global process hashing structures.
329 */
330 void
331 procinit(void)
332 {
333 const struct proclist_desc *pd;
334 u_int i;
335 #define LINK_EMPTY ((PID_MAX + INITIAL_PID_TABLE_SIZE) & ~(INITIAL_PID_TABLE_SIZE - 1))
336
337 for (pd = proclists; pd->pd_list != NULL; pd++)
338 LIST_INIT(pd->pd_list);
339
340 proc_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
341 pid_table = kmem_alloc(INITIAL_PID_TABLE_SIZE
342 * sizeof(struct pid_table), KM_SLEEP);
343 pid_tbl_mask = INITIAL_PID_TABLE_SIZE - 1;
344 pid_max = PID_MAX;
345
346 /* Set free list running through table...
347 Preset 'use count' above PID_MAX so we allocate pid 1 next. */
348 for (i = 0; i <= pid_tbl_mask; i++) {
349 pid_table[i].pt_proc = P_FREE(LINK_EMPTY + i + 1);
350 pid_table[i].pt_pgrp = 0;
351 pid_table[i].pt_pid = 0;
352 }
353 /* slot 0 is just grabbed */
354 next_free_pt = 1;
355 /* Need to fix last entry. */
356 last_free_pt = pid_tbl_mask;
357 pid_table[last_free_pt].pt_proc = P_FREE(LINK_EMPTY);
358 /* point at which we grow table - to avoid reusing pids too often */
359 pid_alloc_lim = pid_tbl_mask - 1;
360 #undef LINK_EMPTY
361
362 proc_specificdata_domain = specificdata_domain_create();
363 KASSERT(proc_specificdata_domain != NULL);
364
365 proc_cache = pool_cache_init(sizeof(struct proc), 0, 0, 0,
366 "procpl", NULL, IPL_NONE, proc_ctor, NULL, NULL);
367
368 proc_listener = kauth_listen_scope(KAUTH_SCOPE_PROCESS,
369 proc_listener_cb, NULL);
370 }
371
372 void
373 procinit_sysctl(void)
374 {
375 static struct sysctllog *clog;
376
377 sysctl_createv(&clog, 0, NULL, NULL,
378 CTLFLAG_PERMANENT,
379 CTLTYPE_NODE, "proc",
380 SYSCTL_DESCR("System-wide process information"),
381 sysctl_doeproc, 0, NULL, 0,
382 CTL_KERN, KERN_PROC, CTL_EOL);
383 sysctl_createv(&clog, 0, NULL, NULL,
384 CTLFLAG_PERMANENT,
385 CTLTYPE_NODE, "proc2",
386 SYSCTL_DESCR("Machine-independent process information"),
387 sysctl_doeproc, 0, NULL, 0,
388 CTL_KERN, KERN_PROC2, CTL_EOL);
389 sysctl_createv(&clog, 0, NULL, NULL,
390 CTLFLAG_PERMANENT,
391 CTLTYPE_NODE, "proc_args",
392 SYSCTL_DESCR("Process argument information"),
393 sysctl_kern_proc_args, 0, NULL, 0,
394 CTL_KERN, KERN_PROC_ARGS, CTL_EOL);
395
396 /*
397 "nodes" under these:
398
399 KERN_PROC_ALL
400 KERN_PROC_PID pid
401 KERN_PROC_PGRP pgrp
402 KERN_PROC_SESSION sess
403 KERN_PROC_TTY tty
404 KERN_PROC_UID uid
405 KERN_PROC_RUID uid
406 KERN_PROC_GID gid
407 KERN_PROC_RGID gid
408
409 all in all, probably not worth the effort...
410 */
411 }
412
413 /*
414 * Initialize process 0.
415 */
416 void
417 proc0_init(void)
418 {
419 struct proc *p;
420 struct pgrp *pg;
421 struct rlimit *rlim;
422 rlim_t lim;
423 int i;
424
425 p = &proc0;
426 pg = &pgrp0;
427
428 mutex_init(&p->p_stmutex, MUTEX_DEFAULT, IPL_HIGH);
429 mutex_init(&p->p_auxlock, MUTEX_DEFAULT, IPL_NONE);
430 p->p_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
431
432 rw_init(&p->p_reflock);
433 cv_init(&p->p_waitcv, "wait");
434 cv_init(&p->p_lwpcv, "lwpwait");
435
436 LIST_INSERT_HEAD(&p->p_lwps, &lwp0, l_sibling);
437
438 pid_table[0].pt_proc = p;
439 LIST_INSERT_HEAD(&allproc, p, p_list);
440
441 pid_table[0].pt_pgrp = pg;
442 LIST_INSERT_HEAD(&pg->pg_members, p, p_pglist);
443
444 #ifdef __HAVE_SYSCALL_INTERN
445 (*p->p_emul->e_syscall_intern)(p);
446 #endif
447
448 /* Create credentials. */
449 cred0 = kauth_cred_alloc();
450 p->p_cred = cred0;
451
452 /* Create the CWD info. */
453 rw_init(&cwdi0.cwdi_lock);
454
455 /* Create the limits structures. */
456 mutex_init(&limit0.pl_lock, MUTEX_DEFAULT, IPL_NONE);
457
458 rlim = limit0.pl_rlimit;
459 for (i = 0; i < __arraycount(limit0.pl_rlimit); i++) {
460 rlim[i].rlim_cur = RLIM_INFINITY;
461 rlim[i].rlim_max = RLIM_INFINITY;
462 }
463
464 rlim[RLIMIT_NOFILE].rlim_max = maxfiles;
465 rlim[RLIMIT_NOFILE].rlim_cur = maxfiles < nofile ? maxfiles : nofile;
466
467 rlim[RLIMIT_NPROC].rlim_max = maxproc;
468 rlim[RLIMIT_NPROC].rlim_cur = maxproc < maxuprc ? maxproc : maxuprc;
469
470 lim = MIN(VM_MAXUSER_ADDRESS, ctob((rlim_t)uvmexp.free));
471 rlim[RLIMIT_RSS].rlim_max = lim;
472 rlim[RLIMIT_MEMLOCK].rlim_max = lim;
473 rlim[RLIMIT_MEMLOCK].rlim_cur = lim / 3;
474
475 rlim[RLIMIT_NTHR].rlim_max = maxlwp;
476 rlim[RLIMIT_NTHR].rlim_cur = maxlwp < maxuprc ? maxlwp : maxuprc;
477
478 /* Note that default core name has zero length. */
479 limit0.pl_corename = defcorename;
480 limit0.pl_cnlen = 0;
481 limit0.pl_refcnt = 1;
482 limit0.pl_writeable = false;
483 limit0.pl_sv_limit = NULL;
484
485 /* Configure virtual memory system, set vm rlimits. */
486 uvm_init_limits(p);
487
488 /* Initialize file descriptor table for proc0. */
489 fd_init(&filedesc0);
490
491 /*
492 * Initialize proc0's vmspace, which uses the kernel pmap.
493 * All kernel processes (which never have user space mappings)
494 * share proc0's vmspace, and thus, the kernel pmap.
495 */
496 uvmspace_init(&vmspace0, pmap_kernel(), round_page(VM_MIN_ADDRESS),
497 trunc_page(VM_MAXUSER_ADDRESS),
498 #ifdef __USE_TOPDOWN_VM
499 true
500 #else
501 false
502 #endif
503 );
504
505 /* Initialize signal state for proc0. XXX IPL_SCHED */
506 mutex_init(&p->p_sigacts->sa_mutex, MUTEX_DEFAULT, IPL_SCHED);
507 siginit(p);
508
509 proc_initspecific(p);
510 kdtrace_proc_ctor(NULL, p);
511 }
512
513 /*
514 * Session reference counting.
515 */
516
517 void
518 proc_sesshold(struct session *ss)
519 {
520
521 KASSERT(mutex_owned(proc_lock));
522 ss->s_count++;
523 }
524
525 void
526 proc_sessrele(struct session *ss)
527 {
528
529 KASSERT(mutex_owned(proc_lock));
530 /*
531 * We keep the pgrp with the same id as the session in order to
532 * stop a process being given the same pid. Since the pgrp holds
533 * a reference to the session, it must be a 'zombie' pgrp by now.
534 */
535 if (--ss->s_count == 0) {
536 struct pgrp *pg;
537
538 pg = pg_remove(ss->s_sid);
539 mutex_exit(proc_lock);
540
541 kmem_free(pg, sizeof(struct pgrp));
542 kmem_free(ss, sizeof(struct session));
543 } else {
544 mutex_exit(proc_lock);
545 }
546 }
547
548 /*
549 * Check that the specified process group is in the session of the
550 * specified process.
551 * Treats -ve ids as process ids.
552 * Used to validate TIOCSPGRP requests.
553 */
554 int
555 pgid_in_session(struct proc *p, pid_t pg_id)
556 {
557 struct pgrp *pgrp;
558 struct session *session;
559 int error;
560
561 mutex_enter(proc_lock);
562 if (pg_id < 0) {
563 struct proc *p1 = proc_find(-pg_id);
564 if (p1 == NULL) {
565 error = EINVAL;
566 goto fail;
567 }
568 pgrp = p1->p_pgrp;
569 } else {
570 pgrp = pgrp_find(pg_id);
571 if (pgrp == NULL) {
572 error = EINVAL;
573 goto fail;
574 }
575 }
576 session = pgrp->pg_session;
577 error = (session != p->p_pgrp->pg_session) ? EPERM : 0;
578 fail:
579 mutex_exit(proc_lock);
580 return error;
581 }
582
583 /*
584 * p_inferior: is p an inferior of q?
585 */
586 static inline bool
587 p_inferior(struct proc *p, struct proc *q)
588 {
589
590 KASSERT(mutex_owned(proc_lock));
591
592 for (; p != q; p = p->p_pptr)
593 if (p->p_pid == 0)
594 return false;
595 return true;
596 }
597
598 /*
599 * proc_find: locate a process by the ID.
600 *
601 * => Must be called with proc_lock held.
602 */
603 proc_t *
604 proc_find_raw(pid_t pid)
605 {
606 struct pid_table *pt;
607 proc_t *p;
608
609 KASSERT(mutex_owned(proc_lock));
610 pt = &pid_table[pid & pid_tbl_mask];
611 p = pt->pt_proc;
612 if (__predict_false(!P_VALID(p) || pt->pt_pid != pid)) {
613 return NULL;
614 }
615 return p;
616 }
617
618 proc_t *
619 proc_find(pid_t pid)
620 {
621 proc_t *p;
622
623 p = proc_find_raw(pid);
624 if (__predict_false(p == NULL)) {
625 return NULL;
626 }
627
628 /*
629 * Only allow live processes to be found by PID.
630 * XXX: p_stat might change, since unlocked.
631 */
632 if (__predict_true(p->p_stat == SACTIVE || p->p_stat == SSTOP)) {
633 return p;
634 }
635 return NULL;
636 }
637
638 /*
639 * pgrp_find: locate a process group by the ID.
640 *
641 * => Must be called with proc_lock held.
642 */
643 struct pgrp *
644 pgrp_find(pid_t pgid)
645 {
646 struct pgrp *pg;
647
648 KASSERT(mutex_owned(proc_lock));
649
650 pg = pid_table[pgid & pid_tbl_mask].pt_pgrp;
651
652 /*
653 * Cannot look up a process group that only exists because the
654 * session has not died yet (traditional).
655 */
656 if (pg == NULL || pg->pg_id != pgid || LIST_EMPTY(&pg->pg_members)) {
657 return NULL;
658 }
659 return pg;
660 }
661
662 static void
663 expand_pid_table(void)
664 {
665 size_t pt_size, tsz;
666 struct pid_table *n_pt, *new_pt;
667 struct proc *proc;
668 struct pgrp *pgrp;
669 pid_t pid, rpid;
670 u_int i;
671 uint new_pt_mask;
672
673 pt_size = pid_tbl_mask + 1;
674 tsz = pt_size * 2 * sizeof(struct pid_table);
675 new_pt = kmem_alloc(tsz, KM_SLEEP);
676 new_pt_mask = pt_size * 2 - 1;
677
678 mutex_enter(proc_lock);
679 if (pt_size != pid_tbl_mask + 1) {
680 /* Another process beat us to it... */
681 mutex_exit(proc_lock);
682 kmem_free(new_pt, tsz);
683 return;
684 }
685
686 /*
687 * Copy entries from old table into new one.
688 * If 'pid' is 'odd' we need to place in the upper half,
689 * even pid's to the lower half.
690 * Free items stay in the low half so we don't have to
691 * fixup the reference to them.
692 * We stuff free items on the front of the freelist
693 * because we can't write to unmodified entries.
694 * Processing the table backwards maintains a semblance
695 * of issuing pid numbers that increase with time.
696 */
697 i = pt_size - 1;
698 n_pt = new_pt + i;
699 for (; ; i--, n_pt--) {
700 proc = pid_table[i].pt_proc;
701 pgrp = pid_table[i].pt_pgrp;
702 if (!P_VALID(proc)) {
703 /* Up 'use count' so that link is valid */
704 pid = (P_NEXT(proc) + pt_size) & ~pt_size;
705 rpid = 0;
706 proc = P_FREE(pid);
707 if (pgrp)
708 pid = pgrp->pg_id;
709 } else {
710 pid = pid_table[i].pt_pid;
711 rpid = pid;
712 }
713
714 /* Save entry in appropriate half of table */
715 n_pt[pid & pt_size].pt_proc = proc;
716 n_pt[pid & pt_size].pt_pgrp = pgrp;
717 n_pt[pid & pt_size].pt_pid = rpid;
718
719 /* Put other piece on start of free list */
720 pid = (pid ^ pt_size) & ~pid_tbl_mask;
721 n_pt[pid & pt_size].pt_proc =
722 P_FREE((pid & ~pt_size) | next_free_pt);
723 n_pt[pid & pt_size].pt_pgrp = 0;
724 n_pt[pid & pt_size].pt_pid = 0;
725
726 next_free_pt = i | (pid & pt_size);
727 if (i == 0)
728 break;
729 }
730
731 /* Save old table size and switch tables */
732 tsz = pt_size * sizeof(struct pid_table);
733 n_pt = pid_table;
734 pid_table = new_pt;
735 pid_tbl_mask = new_pt_mask;
736
737 /*
738 * pid_max starts as PID_MAX (= 30000), once we have 16384
739 * allocated pids we need it to be larger!
740 */
741 if (pid_tbl_mask > PID_MAX) {
742 pid_max = pid_tbl_mask * 2 + 1;
743 pid_alloc_lim |= pid_alloc_lim << 1;
744 } else
745 pid_alloc_lim <<= 1; /* doubles number of free slots... */
746
747 mutex_exit(proc_lock);
748 kmem_free(n_pt, tsz);
749 }
750
751 struct proc *
752 proc_alloc(void)
753 {
754 struct proc *p;
755
756 p = pool_cache_get(proc_cache, PR_WAITOK);
757 p->p_stat = SIDL; /* protect against others */
758 proc_initspecific(p);
759 kdtrace_proc_ctor(NULL, p);
760 p->p_pid = -1;
761 proc_alloc_pid(p);
762 return p;
763 }
764
765 /*
766 * proc_alloc_pid: allocate PID and record the given proc 'p' so that
767 * proc_find_raw() can find it by the PID.
768 */
769
770 pid_t
771 proc_alloc_pid(struct proc *p)
772 {
773 struct pid_table *pt;
774 pid_t pid;
775 int nxt;
776
777 for (;;expand_pid_table()) {
778 if (__predict_false(pid_alloc_cnt >= pid_alloc_lim))
779 /* ensure pids cycle through 2000+ values */
780 continue;
781 mutex_enter(proc_lock);
782 pt = &pid_table[next_free_pt];
783 #ifdef DIAGNOSTIC
784 if (__predict_false(P_VALID(pt->pt_proc) || pt->pt_pgrp))
785 panic("proc_alloc: slot busy");
786 #endif
787 nxt = P_NEXT(pt->pt_proc);
788 if (nxt & pid_tbl_mask)
789 break;
790 /* Table full - expand (NB last entry not used....) */
791 mutex_exit(proc_lock);
792 }
793
794 /* pid is 'saved use count' + 'size' + entry */
795 pid = (nxt & ~pid_tbl_mask) + pid_tbl_mask + 1 + next_free_pt;
796 if ((uint)pid > (uint)pid_max)
797 pid &= pid_tbl_mask;
798 next_free_pt = nxt & pid_tbl_mask;
799
800 /* Grab table slot */
801 pt->pt_proc = p;
802
803 KASSERT(pt->pt_pid == 0);
804 pt->pt_pid = pid;
805 if (p->p_pid == -1) {
806 p->p_pid = pid;
807 }
808 pid_alloc_cnt++;
809 mutex_exit(proc_lock);
810
811 return pid;
812 }
813
814 /*
815 * Free a process id - called from proc_free (in kern_exit.c)
816 *
817 * Called with the proc_lock held.
818 */
819 void
820 proc_free_pid(pid_t pid)
821 {
822 struct pid_table *pt;
823
824 KASSERT(mutex_owned(proc_lock));
825
826 pt = &pid_table[pid & pid_tbl_mask];
827
828 /* save pid use count in slot */
829 pt->pt_proc = P_FREE(pid & ~pid_tbl_mask);
830 KASSERT(pt->pt_pid == pid);
831 pt->pt_pid = 0;
832
833 if (pt->pt_pgrp == NULL) {
834 /* link last freed entry onto ours */
835 pid &= pid_tbl_mask;
836 pt = &pid_table[last_free_pt];
837 pt->pt_proc = P_FREE(P_NEXT(pt->pt_proc) | pid);
838 pt->pt_pid = 0;
839 last_free_pt = pid;
840 pid_alloc_cnt--;
841 }
842
843 atomic_dec_uint(&nprocs);
844 }
845
846 void
847 proc_free_mem(struct proc *p)
848 {
849
850 kdtrace_proc_dtor(NULL, p);
851 pool_cache_put(proc_cache, p);
852 }
853
854 /*
855 * proc_enterpgrp: move p to a new or existing process group (and session).
856 *
857 * If we are creating a new pgrp, the pgid should equal
858 * the calling process' pid.
859 * If is only valid to enter a process group that is in the session
860 * of the process.
861 * Also mksess should only be set if we are creating a process group
862 *
863 * Only called from sys_setsid, sys_setpgid and posix_spawn/spawn_return.
864 */
865 int
866 proc_enterpgrp(struct proc *curp, pid_t pid, pid_t pgid, bool mksess)
867 {
868 struct pgrp *new_pgrp, *pgrp;
869 struct session *sess;
870 struct proc *p;
871 int rval;
872 pid_t pg_id = NO_PGID;
873
874 sess = mksess ? kmem_alloc(sizeof(*sess), KM_SLEEP) : NULL;
875
876 /* Allocate data areas we might need before doing any validity checks */
877 mutex_enter(proc_lock); /* Because pid_table might change */
878 if (pid_table[pgid & pid_tbl_mask].pt_pgrp == 0) {
879 mutex_exit(proc_lock);
880 new_pgrp = kmem_alloc(sizeof(*new_pgrp), KM_SLEEP);
881 mutex_enter(proc_lock);
882 } else
883 new_pgrp = NULL;
884 rval = EPERM; /* most common error (to save typing) */
885
886 /* Check pgrp exists or can be created */
887 pgrp = pid_table[pgid & pid_tbl_mask].pt_pgrp;
888 if (pgrp != NULL && pgrp->pg_id != pgid)
889 goto done;
890
891 /* Can only set another process under restricted circumstances. */
892 if (pid != curp->p_pid) {
893 /* Must exist and be one of our children... */
894 p = proc_find(pid);
895 if (p == NULL || !p_inferior(p, curp)) {
896 rval = ESRCH;
897 goto done;
898 }
899 /* ... in the same session... */
900 if (sess != NULL || p->p_session != curp->p_session)
901 goto done;
902 /* ... existing pgid must be in same session ... */
903 if (pgrp != NULL && pgrp->pg_session != p->p_session)
904 goto done;
905 /* ... and not done an exec. */
906 if (p->p_flag & PK_EXEC) {
907 rval = EACCES;
908 goto done;
909 }
910 } else {
911 /* ... setsid() cannot re-enter a pgrp */
912 if (mksess && (curp->p_pgid == curp->p_pid ||
913 pgrp_find(curp->p_pid)))
914 goto done;
915 p = curp;
916 }
917
918 /* Changing the process group/session of a session
919 leader is definitely off limits. */
920 if (SESS_LEADER(p)) {
921 if (sess == NULL && p->p_pgrp == pgrp)
922 /* unless it's a definite noop */
923 rval = 0;
924 goto done;
925 }
926
927 /* Can only create a process group with id of process */
928 if (pgrp == NULL && pgid != pid)
929 goto done;
930
931 /* Can only create a session if creating pgrp */
932 if (sess != NULL && pgrp != NULL)
933 goto done;
934
935 /* Check we allocated memory for a pgrp... */
936 if (pgrp == NULL && new_pgrp == NULL)
937 goto done;
938
939 /* Don't attach to 'zombie' pgrp */
940 if (pgrp != NULL && LIST_EMPTY(&pgrp->pg_members))
941 goto done;
942
943 /* Expect to succeed now */
944 rval = 0;
945
946 if (pgrp == p->p_pgrp)
947 /* nothing to do */
948 goto done;
949
950 /* Ok all setup, link up required structures */
951
952 if (pgrp == NULL) {
953 pgrp = new_pgrp;
954 new_pgrp = NULL;
955 if (sess != NULL) {
956 sess->s_sid = p->p_pid;
957 sess->s_leader = p;
958 sess->s_count = 1;
959 sess->s_ttyvp = NULL;
960 sess->s_ttyp = NULL;
961 sess->s_flags = p->p_session->s_flags & ~S_LOGIN_SET;
962 memcpy(sess->s_login, p->p_session->s_login,
963 sizeof(sess->s_login));
964 p->p_lflag &= ~PL_CONTROLT;
965 } else {
966 sess = p->p_pgrp->pg_session;
967 proc_sesshold(sess);
968 }
969 pgrp->pg_session = sess;
970 sess = NULL;
971
972 pgrp->pg_id = pgid;
973 LIST_INIT(&pgrp->pg_members);
974 #ifdef DIAGNOSTIC
975 if (__predict_false(pid_table[pgid & pid_tbl_mask].pt_pgrp))
976 panic("enterpgrp: pgrp table slot in use");
977 if (__predict_false(mksess && p != curp))
978 panic("enterpgrp: mksession and p != curproc");
979 #endif
980 pid_table[pgid & pid_tbl_mask].pt_pgrp = pgrp;
981 pgrp->pg_jobc = 0;
982 }
983
984 /*
985 * Adjust eligibility of affected pgrps to participate in job control.
986 * Increment eligibility counts before decrementing, otherwise we
987 * could reach 0 spuriously during the first call.
988 */
989 fixjobc(p, pgrp, 1);
990 fixjobc(p, p->p_pgrp, 0);
991
992 /* Interlock with ttread(). */
993 mutex_spin_enter(&tty_lock);
994
995 /* Move process to requested group. */
996 LIST_REMOVE(p, p_pglist);
997 if (LIST_EMPTY(&p->p_pgrp->pg_members))
998 /* defer delete until we've dumped the lock */
999 pg_id = p->p_pgrp->pg_id;
1000 p->p_pgrp = pgrp;
1001 LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist);
1002
1003 /* Done with the swap; we can release the tty mutex. */
1004 mutex_spin_exit(&tty_lock);
1005
1006 done:
1007 if (pg_id != NO_PGID) {
1008 /* Releases proc_lock. */
1009 pg_delete(pg_id);
1010 } else {
1011 mutex_exit(proc_lock);
1012 }
1013 if (sess != NULL)
1014 kmem_free(sess, sizeof(*sess));
1015 if (new_pgrp != NULL)
1016 kmem_free(new_pgrp, sizeof(*new_pgrp));
1017 #ifdef DEBUG_PGRP
1018 if (__predict_false(rval))
1019 printf("enterpgrp(%d,%d,%d), curproc %d, rval %d\n",
1020 pid, pgid, mksess, curp->p_pid, rval);
1021 #endif
1022 return rval;
1023 }
1024
1025 /*
1026 * proc_leavepgrp: remove a process from its process group.
1027 * => must be called with the proc_lock held, which will be released;
1028 */
1029 void
1030 proc_leavepgrp(struct proc *p)
1031 {
1032 struct pgrp *pgrp;
1033
1034 KASSERT(mutex_owned(proc_lock));
1035
1036 /* Interlock with ttread() */
1037 mutex_spin_enter(&tty_lock);
1038 pgrp = p->p_pgrp;
1039 LIST_REMOVE(p, p_pglist);
1040 p->p_pgrp = NULL;
1041 mutex_spin_exit(&tty_lock);
1042
1043 if (LIST_EMPTY(&pgrp->pg_members)) {
1044 /* Releases proc_lock. */
1045 pg_delete(pgrp->pg_id);
1046 } else {
1047 mutex_exit(proc_lock);
1048 }
1049 }
1050
1051 /*
1052 * pg_remove: remove a process group from the table.
1053 * => must be called with the proc_lock held;
1054 * => returns process group to free;
1055 */
1056 static struct pgrp *
1057 pg_remove(pid_t pg_id)
1058 {
1059 struct pgrp *pgrp;
1060 struct pid_table *pt;
1061
1062 KASSERT(mutex_owned(proc_lock));
1063
1064 pt = &pid_table[pg_id & pid_tbl_mask];
1065 pgrp = pt->pt_pgrp;
1066
1067 KASSERT(pgrp != NULL);
1068 KASSERT(pgrp->pg_id == pg_id);
1069 KASSERT(LIST_EMPTY(&pgrp->pg_members));
1070
1071 pt->pt_pgrp = NULL;
1072
1073 if (!P_VALID(pt->pt_proc)) {
1074 /* Orphaned pgrp, put slot onto free list. */
1075 KASSERT((P_NEXT(pt->pt_proc) & pid_tbl_mask) == 0);
1076 pg_id &= pid_tbl_mask;
1077 pt = &pid_table[last_free_pt];
1078 pt->pt_proc = P_FREE(P_NEXT(pt->pt_proc) | pg_id);
1079 KASSERT(pt->pt_pid == 0);
1080 last_free_pt = pg_id;
1081 pid_alloc_cnt--;
1082 }
1083 return pgrp;
1084 }
1085
1086 /*
1087 * pg_delete: delete and free a process group.
1088 * => must be called with the proc_lock held, which will be released.
1089 */
1090 static void
1091 pg_delete(pid_t pg_id)
1092 {
1093 struct pgrp *pg;
1094 struct tty *ttyp;
1095 struct session *ss;
1096
1097 KASSERT(mutex_owned(proc_lock));
1098
1099 pg = pid_table[pg_id & pid_tbl_mask].pt_pgrp;
1100 if (pg == NULL || pg->pg_id != pg_id || !LIST_EMPTY(&pg->pg_members)) {
1101 mutex_exit(proc_lock);
1102 return;
1103 }
1104
1105 ss = pg->pg_session;
1106
1107 /* Remove reference (if any) from tty to this process group */
1108 mutex_spin_enter(&tty_lock);
1109 ttyp = ss->s_ttyp;
1110 if (ttyp != NULL && ttyp->t_pgrp == pg) {
1111 ttyp->t_pgrp = NULL;
1112 KASSERT(ttyp->t_session == ss);
1113 }
1114 mutex_spin_exit(&tty_lock);
1115
1116 /*
1117 * The leading process group in a session is freed by proc_sessrele(),
1118 * if last reference. Note: proc_sessrele() releases proc_lock.
1119 */
1120 pg = (ss->s_sid != pg->pg_id) ? pg_remove(pg_id) : NULL;
1121 proc_sessrele(ss);
1122
1123 if (pg != NULL) {
1124 /* Free it, if was not done by proc_sessrele(). */
1125 kmem_free(pg, sizeof(struct pgrp));
1126 }
1127 }
1128
1129 /*
1130 * Adjust pgrp jobc counters when specified process changes process group.
1131 * We count the number of processes in each process group that "qualify"
1132 * the group for terminal job control (those with a parent in a different
1133 * process group of the same session). If that count reaches zero, the
1134 * process group becomes orphaned. Check both the specified process'
1135 * process group and that of its children.
1136 * entering == 0 => p is leaving specified group.
1137 * entering == 1 => p is entering specified group.
1138 *
1139 * Call with proc_lock held.
1140 */
1141 void
1142 fixjobc(struct proc *p, struct pgrp *pgrp, int entering)
1143 {
1144 struct pgrp *hispgrp;
1145 struct session *mysession = pgrp->pg_session;
1146 struct proc *child;
1147
1148 KASSERT(mutex_owned(proc_lock));
1149
1150 /*
1151 * Check p's parent to see whether p qualifies its own process
1152 * group; if so, adjust count for p's process group.
1153 */
1154 hispgrp = p->p_pptr->p_pgrp;
1155 if (hispgrp != pgrp && hispgrp->pg_session == mysession) {
1156 if (entering) {
1157 pgrp->pg_jobc++;
1158 p->p_lflag &= ~PL_ORPHANPG;
1159 } else if (--pgrp->pg_jobc == 0)
1160 orphanpg(pgrp);
1161 }
1162
1163 /*
1164 * Check this process' children to see whether they qualify
1165 * their process groups; if so, adjust counts for children's
1166 * process groups.
1167 */
1168 LIST_FOREACH(child, &p->p_children, p_sibling) {
1169 hispgrp = child->p_pgrp;
1170 if (hispgrp != pgrp && hispgrp->pg_session == mysession &&
1171 !P_ZOMBIE(child)) {
1172 if (entering) {
1173 child->p_lflag &= ~PL_ORPHANPG;
1174 hispgrp->pg_jobc++;
1175 } else if (--hispgrp->pg_jobc == 0)
1176 orphanpg(hispgrp);
1177 }
1178 }
1179 }
1180
1181 /*
1182 * A process group has become orphaned;
1183 * if there are any stopped processes in the group,
1184 * hang-up all process in that group.
1185 *
1186 * Call with proc_lock held.
1187 */
1188 static void
1189 orphanpg(struct pgrp *pg)
1190 {
1191 struct proc *p;
1192
1193 KASSERT(mutex_owned(proc_lock));
1194
1195 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
1196 if (p->p_stat == SSTOP) {
1197 p->p_lflag |= PL_ORPHANPG;
1198 psignal(p, SIGHUP);
1199 psignal(p, SIGCONT);
1200 }
1201 }
1202 }
1203
1204 #ifdef DDB
1205 #include <ddb/db_output.h>
1206 void pidtbl_dump(void);
1207 void
1208 pidtbl_dump(void)
1209 {
1210 struct pid_table *pt;
1211 struct proc *p;
1212 struct pgrp *pgrp;
1213 int id;
1214
1215 db_printf("pid table %p size %x, next %x, last %x\n",
1216 pid_table, pid_tbl_mask+1,
1217 next_free_pt, last_free_pt);
1218 for (pt = pid_table, id = 0; id <= pid_tbl_mask; id++, pt++) {
1219 p = pt->pt_proc;
1220 if (!P_VALID(p) && !pt->pt_pgrp)
1221 continue;
1222 db_printf(" id %x: ", id);
1223 if (P_VALID(p))
1224 db_printf("slotpid %d proc %p id %d (0x%x) %s\n",
1225 pt->pt_pid, p, p->p_pid, p->p_pid, p->p_comm);
1226 else
1227 db_printf("next %x use %x\n",
1228 P_NEXT(p) & pid_tbl_mask,
1229 P_NEXT(p) & ~pid_tbl_mask);
1230 if ((pgrp = pt->pt_pgrp)) {
1231 db_printf("\tsession %p, sid %d, count %d, login %s\n",
1232 pgrp->pg_session, pgrp->pg_session->s_sid,
1233 pgrp->pg_session->s_count,
1234 pgrp->pg_session->s_login);
1235 db_printf("\tpgrp %p, pg_id %d, pg_jobc %d, members %p\n",
1236 pgrp, pgrp->pg_id, pgrp->pg_jobc,
1237 LIST_FIRST(&pgrp->pg_members));
1238 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1239 db_printf("\t\tpid %d addr %p pgrp %p %s\n",
1240 p->p_pid, p, p->p_pgrp, p->p_comm);
1241 }
1242 }
1243 }
1244 }
1245 #endif /* DDB */
1246
1247 #ifdef KSTACK_CHECK_MAGIC
1248
1249 #define KSTACK_MAGIC 0xdeadbeaf
1250
1251 /* XXX should be per process basis? */
1252 static int kstackleftmin = KSTACK_SIZE;
1253 static int kstackleftthres = KSTACK_SIZE / 8;
1254
1255 void
1256 kstack_setup_magic(const struct lwp *l)
1257 {
1258 uint32_t *ip;
1259 uint32_t const *end;
1260
1261 KASSERT(l != NULL);
1262 KASSERT(l != &lwp0);
1263
1264 /*
1265 * fill all the stack with magic number
1266 * so that later modification on it can be detected.
1267 */
1268 ip = (uint32_t *)KSTACK_LOWEST_ADDR(l);
1269 end = (uint32_t *)((char *)KSTACK_LOWEST_ADDR(l) + KSTACK_SIZE);
1270 for (; ip < end; ip++) {
1271 *ip = KSTACK_MAGIC;
1272 }
1273 }
1274
1275 void
1276 kstack_check_magic(const struct lwp *l)
1277 {
1278 uint32_t const *ip, *end;
1279 int stackleft;
1280
1281 KASSERT(l != NULL);
1282
1283 /* don't check proc0 */ /*XXX*/
1284 if (l == &lwp0)
1285 return;
1286
1287 #ifdef __MACHINE_STACK_GROWS_UP
1288 /* stack grows upwards (eg. hppa) */
1289 ip = (uint32_t *)((void *)KSTACK_LOWEST_ADDR(l) + KSTACK_SIZE);
1290 end = (uint32_t *)KSTACK_LOWEST_ADDR(l);
1291 for (ip--; ip >= end; ip--)
1292 if (*ip != KSTACK_MAGIC)
1293 break;
1294
1295 stackleft = (void *)KSTACK_LOWEST_ADDR(l) + KSTACK_SIZE - (void *)ip;
1296 #else /* __MACHINE_STACK_GROWS_UP */
1297 /* stack grows downwards (eg. i386) */
1298 ip = (uint32_t *)KSTACK_LOWEST_ADDR(l);
1299 end = (uint32_t *)((char *)KSTACK_LOWEST_ADDR(l) + KSTACK_SIZE);
1300 for (; ip < end; ip++)
1301 if (*ip != KSTACK_MAGIC)
1302 break;
1303
1304 stackleft = ((const char *)ip) - (const char *)KSTACK_LOWEST_ADDR(l);
1305 #endif /* __MACHINE_STACK_GROWS_UP */
1306
1307 if (kstackleftmin > stackleft) {
1308 kstackleftmin = stackleft;
1309 if (stackleft < kstackleftthres)
1310 printf("warning: kernel stack left %d bytes"
1311 "(pid %u:lid %u)\n", stackleft,
1312 (u_int)l->l_proc->p_pid, (u_int)l->l_lid);
1313 }
1314
1315 if (stackleft <= 0) {
1316 panic("magic on the top of kernel stack changed for "
1317 "pid %u, lid %u: maybe kernel stack overflow",
1318 (u_int)l->l_proc->p_pid, (u_int)l->l_lid);
1319 }
1320 }
1321 #endif /* KSTACK_CHECK_MAGIC */
1322
1323 int
1324 proclist_foreach_call(struct proclist *list,
1325 int (*callback)(struct proc *, void *arg), void *arg)
1326 {
1327 struct proc marker;
1328 struct proc *p;
1329 int ret = 0;
1330
1331 marker.p_flag = PK_MARKER;
1332 mutex_enter(proc_lock);
1333 for (p = LIST_FIRST(list); ret == 0 && p != NULL;) {
1334 if (p->p_flag & PK_MARKER) {
1335 p = LIST_NEXT(p, p_list);
1336 continue;
1337 }
1338 LIST_INSERT_AFTER(p, &marker, p_list);
1339 ret = (*callback)(p, arg);
1340 KASSERT(mutex_owned(proc_lock));
1341 p = LIST_NEXT(&marker, p_list);
1342 LIST_REMOVE(&marker, p_list);
1343 }
1344 mutex_exit(proc_lock);
1345
1346 return ret;
1347 }
1348
1349 int
1350 proc_vmspace_getref(struct proc *p, struct vmspace **vm)
1351 {
1352
1353 /* XXXCDC: how should locking work here? */
1354
1355 /* curproc exception is for coredump. */
1356
1357 if ((p != curproc && (p->p_sflag & PS_WEXIT) != 0) ||
1358 (p->p_vmspace->vm_refcnt < 1)) { /* XXX */
1359 return EFAULT;
1360 }
1361
1362 uvmspace_addref(p->p_vmspace);
1363 *vm = p->p_vmspace;
1364
1365 return 0;
1366 }
1367
1368 /*
1369 * Acquire a write lock on the process credential.
1370 */
1371 void
1372 proc_crmod_enter(void)
1373 {
1374 struct lwp *l = curlwp;
1375 struct proc *p = l->l_proc;
1376 kauth_cred_t oc;
1377
1378 /* Reset what needs to be reset in plimit. */
1379 if (p->p_limit->pl_corename != defcorename) {
1380 lim_setcorename(p, defcorename, 0);
1381 }
1382
1383 mutex_enter(p->p_lock);
1384
1385 /* Ensure the LWP cached credentials are up to date. */
1386 if ((oc = l->l_cred) != p->p_cred) {
1387 kauth_cred_hold(p->p_cred);
1388 l->l_cred = p->p_cred;
1389 kauth_cred_free(oc);
1390 }
1391 }
1392
1393 /*
1394 * Set in a new process credential, and drop the write lock. The credential
1395 * must have a reference already. Optionally, free a no-longer required
1396 * credential. The scheduler also needs to inspect p_cred, so we also
1397 * briefly acquire the sched state mutex.
1398 */
1399 void
1400 proc_crmod_leave(kauth_cred_t scred, kauth_cred_t fcred, bool sugid)
1401 {
1402 struct lwp *l = curlwp, *l2;
1403 struct proc *p = l->l_proc;
1404 kauth_cred_t oc;
1405
1406 KASSERT(mutex_owned(p->p_lock));
1407
1408 /* Is there a new credential to set in? */
1409 if (scred != NULL) {
1410 p->p_cred = scred;
1411 LIST_FOREACH(l2, &p->p_lwps, l_sibling) {
1412 if (l2 != l)
1413 l2->l_prflag |= LPR_CRMOD;
1414 }
1415
1416 /* Ensure the LWP cached credentials are up to date. */
1417 if ((oc = l->l_cred) != scred) {
1418 kauth_cred_hold(scred);
1419 l->l_cred = scred;
1420 }
1421 } else
1422 oc = NULL; /* XXXgcc */
1423
1424 if (sugid) {
1425 /*
1426 * Mark process as having changed credentials, stops
1427 * tracing etc.
1428 */
1429 p->p_flag |= PK_SUGID;
1430 }
1431
1432 mutex_exit(p->p_lock);
1433
1434 /* If there is a credential to be released, free it now. */
1435 if (fcred != NULL) {
1436 KASSERT(scred != NULL);
1437 kauth_cred_free(fcred);
1438 if (oc != scred)
1439 kauth_cred_free(oc);
1440 }
1441 }
1442
1443 /*
1444 * proc_specific_key_create --
1445 * Create a key for subsystem proc-specific data.
1446 */
1447 int
1448 proc_specific_key_create(specificdata_key_t *keyp, specificdata_dtor_t dtor)
1449 {
1450
1451 return (specificdata_key_create(proc_specificdata_domain, keyp, dtor));
1452 }
1453
1454 /*
1455 * proc_specific_key_delete --
1456 * Delete a key for subsystem proc-specific data.
1457 */
1458 void
1459 proc_specific_key_delete(specificdata_key_t key)
1460 {
1461
1462 specificdata_key_delete(proc_specificdata_domain, key);
1463 }
1464
1465 /*
1466 * proc_initspecific --
1467 * Initialize a proc's specificdata container.
1468 */
1469 void
1470 proc_initspecific(struct proc *p)
1471 {
1472 int error __diagused;
1473
1474 error = specificdata_init(proc_specificdata_domain, &p->p_specdataref);
1475 KASSERT(error == 0);
1476 }
1477
1478 /*
1479 * proc_finispecific --
1480 * Finalize a proc's specificdata container.
1481 */
1482 void
1483 proc_finispecific(struct proc *p)
1484 {
1485
1486 specificdata_fini(proc_specificdata_domain, &p->p_specdataref);
1487 }
1488
1489 /*
1490 * proc_getspecific --
1491 * Return proc-specific data corresponding to the specified key.
1492 */
1493 void *
1494 proc_getspecific(struct proc *p, specificdata_key_t key)
1495 {
1496
1497 return (specificdata_getspecific(proc_specificdata_domain,
1498 &p->p_specdataref, key));
1499 }
1500
1501 /*
1502 * proc_setspecific --
1503 * Set proc-specific data corresponding to the specified key.
1504 */
1505 void
1506 proc_setspecific(struct proc *p, specificdata_key_t key, void *data)
1507 {
1508
1509 specificdata_setspecific(proc_specificdata_domain,
1510 &p->p_specdataref, key, data);
1511 }
1512
1513 int
1514 proc_uidmatch(kauth_cred_t cred, kauth_cred_t target)
1515 {
1516 int r = 0;
1517
1518 if (kauth_cred_getuid(cred) != kauth_cred_getuid(target) ||
1519 kauth_cred_getuid(cred) != kauth_cred_getsvuid(target)) {
1520 /*
1521 * suid proc of ours or proc not ours
1522 */
1523 r = EPERM;
1524 } else if (kauth_cred_getgid(target) != kauth_cred_getsvgid(target)) {
1525 /*
1526 * sgid proc has sgid back to us temporarily
1527 */
1528 r = EPERM;
1529 } else {
1530 /*
1531 * our rgid must be in target's group list (ie,
1532 * sub-processes started by a sgid process)
1533 */
1534 int ismember = 0;
1535
1536 if (kauth_cred_ismember_gid(cred,
1537 kauth_cred_getgid(target), &ismember) != 0 ||
1538 !ismember)
1539 r = EPERM;
1540 }
1541
1542 return (r);
1543 }
1544
1545 /*
1546 * sysctl stuff
1547 */
1548
1549 #define KERN_PROCSLOP (5 * sizeof(struct kinfo_proc))
1550
1551 static const u_int sysctl_flagmap[] = {
1552 PK_ADVLOCK, P_ADVLOCK,
1553 PK_EXEC, P_EXEC,
1554 PK_NOCLDWAIT, P_NOCLDWAIT,
1555 PK_32, P_32,
1556 PK_CLDSIGIGN, P_CLDSIGIGN,
1557 PK_SUGID, P_SUGID,
1558 0
1559 };
1560
1561 static const u_int sysctl_sflagmap[] = {
1562 PS_NOCLDSTOP, P_NOCLDSTOP,
1563 PS_WEXIT, P_WEXIT,
1564 PS_STOPFORK, P_STOPFORK,
1565 PS_STOPEXEC, P_STOPEXEC,
1566 PS_STOPEXIT, P_STOPEXIT,
1567 0
1568 };
1569
1570 static const u_int sysctl_slflagmap[] = {
1571 PSL_TRACED, P_TRACED,
1572 PSL_CHTRACED, P_CHTRACED,
1573 PSL_SYSCALL, P_SYSCALL,
1574 0
1575 };
1576
1577 static const u_int sysctl_lflagmap[] = {
1578 PL_CONTROLT, P_CONTROLT,
1579 PL_PPWAIT, P_PPWAIT,
1580 0
1581 };
1582
1583 static const u_int sysctl_stflagmap[] = {
1584 PST_PROFIL, P_PROFIL,
1585 0
1586
1587 };
1588
1589 /* used by kern_lwp also */
1590 const u_int sysctl_lwpflagmap[] = {
1591 LW_SINTR, L_SINTR,
1592 LW_SYSTEM, L_SYSTEM,
1593 0
1594 };
1595
1596 /*
1597 * Find the most ``active'' lwp of a process and return it for ps display
1598 * purposes
1599 */
1600 static struct lwp *
1601 proc_active_lwp(struct proc *p)
1602 {
1603 static const int ostat[] = {
1604 0,
1605 2, /* LSIDL */
1606 6, /* LSRUN */
1607 5, /* LSSLEEP */
1608 4, /* LSSTOP */
1609 0, /* LSZOMB */
1610 1, /* LSDEAD */
1611 7, /* LSONPROC */
1612 3 /* LSSUSPENDED */
1613 };
1614
1615 struct lwp *l, *lp = NULL;
1616 LIST_FOREACH(l, &p->p_lwps, l_sibling) {
1617 KASSERT(l->l_stat >= 0 && l->l_stat < __arraycount(ostat));
1618 if (lp == NULL ||
1619 ostat[l->l_stat] > ostat[lp->l_stat] ||
1620 (ostat[l->l_stat] == ostat[lp->l_stat] &&
1621 l->l_cpticks > lp->l_cpticks)) {
1622 lp = l;
1623 continue;
1624 }
1625 }
1626 return lp;
1627 }
1628
1629 static int
1630 sysctl_doeproc(SYSCTLFN_ARGS)
1631 {
1632 union {
1633 struct kinfo_proc kproc;
1634 struct kinfo_proc2 kproc2;
1635 } *kbuf;
1636 struct proc *p, *next, *marker;
1637 char *where, *dp;
1638 int type, op, arg, error;
1639 u_int elem_size, kelem_size, elem_count;
1640 size_t buflen, needed;
1641 bool match, zombie, mmmbrains;
1642
1643 if (namelen == 1 && name[0] == CTL_QUERY)
1644 return (sysctl_query(SYSCTLFN_CALL(rnode)));
1645
1646 dp = where = oldp;
1647 buflen = where != NULL ? *oldlenp : 0;
1648 error = 0;
1649 needed = 0;
1650 type = rnode->sysctl_num;
1651
1652 if (type == KERN_PROC) {
1653 if (namelen == 0)
1654 return EINVAL;
1655 switch (op = name[0]) {
1656 case KERN_PROC_ALL:
1657 if (namelen != 1)
1658 return EINVAL;
1659 arg = 0;
1660 break;
1661 default:
1662 if (namelen != 2)
1663 return EINVAL;
1664 arg = name[1];
1665 break;
1666 }
1667 elem_count = 0; /* Hush little compiler, don't you cry */
1668 kelem_size = elem_size = sizeof(kbuf->kproc);
1669 } else {
1670 if (namelen != 4)
1671 return EINVAL;
1672 op = name[0];
1673 arg = name[1];
1674 elem_size = name[2];
1675 elem_count = name[3];
1676 kelem_size = sizeof(kbuf->kproc2);
1677 }
1678
1679 sysctl_unlock();
1680
1681 kbuf = kmem_zalloc(sizeof(*kbuf), KM_SLEEP);
1682 marker = kmem_alloc(sizeof(*marker), KM_SLEEP);
1683 marker->p_flag = PK_MARKER;
1684
1685 mutex_enter(proc_lock);
1686 /*
1687 * Start with zombies to prevent reporting processes twice, in case they
1688 * are dying and being moved from the list of alive processes to zombies.
1689 */
1690 mmmbrains = true;
1691 for (p = LIST_FIRST(&zombproc);; p = next) {
1692 if (p == NULL) {
1693 if (mmmbrains) {
1694 p = LIST_FIRST(&allproc);
1695 mmmbrains = false;
1696 }
1697 if (p == NULL)
1698 break;
1699 }
1700 next = LIST_NEXT(p, p_list);
1701 if ((p->p_flag & PK_MARKER) != 0)
1702 continue;
1703
1704 /*
1705 * Skip embryonic processes.
1706 */
1707 if (p->p_stat == SIDL)
1708 continue;
1709
1710 mutex_enter(p->p_lock);
1711 error = kauth_authorize_process(l->l_cred,
1712 KAUTH_PROCESS_CANSEE, p,
1713 KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_EPROC), NULL, NULL);
1714 if (error != 0) {
1715 mutex_exit(p->p_lock);
1716 continue;
1717 }
1718
1719 /*
1720 * Hande all the operations in one switch on the cost of
1721 * algorithm complexity is on purpose. The win splitting this
1722 * function into several similar copies makes maintenance burden
1723 * burden, code grow and boost is neglible in practical systems.
1724 */
1725 switch (op) {
1726 case KERN_PROC_PID:
1727 match = (p->p_pid == (pid_t)arg);
1728 break;
1729
1730 case KERN_PROC_PGRP:
1731 match = (p->p_pgrp->pg_id == (pid_t)arg);
1732 break;
1733
1734 case KERN_PROC_SESSION:
1735 match = (p->p_session->s_sid == (pid_t)arg);
1736 break;
1737
1738 case KERN_PROC_TTY:
1739 match = true;
1740 if (arg == (int) KERN_PROC_TTY_REVOKE) {
1741 if ((p->p_lflag & PL_CONTROLT) == 0 ||
1742 p->p_session->s_ttyp == NULL ||
1743 p->p_session->s_ttyvp != NULL) {
1744 match = false;
1745 }
1746 } else if ((p->p_lflag & PL_CONTROLT) == 0 ||
1747 p->p_session->s_ttyp == NULL) {
1748 if ((dev_t)arg != KERN_PROC_TTY_NODEV) {
1749 match = false;
1750 }
1751 } else if (p->p_session->s_ttyp->t_dev != (dev_t)arg) {
1752 match = false;
1753 }
1754 break;
1755
1756 case KERN_PROC_UID:
1757 match = (kauth_cred_geteuid(p->p_cred) == (uid_t)arg);
1758 break;
1759
1760 case KERN_PROC_RUID:
1761 match = (kauth_cred_getuid(p->p_cred) == (uid_t)arg);
1762 break;
1763
1764 case KERN_PROC_GID:
1765 match = (kauth_cred_getegid(p->p_cred) == (uid_t)arg);
1766 break;
1767
1768 case KERN_PROC_RGID:
1769 match = (kauth_cred_getgid(p->p_cred) == (uid_t)arg);
1770 break;
1771
1772 case KERN_PROC_ALL:
1773 match = true;
1774 /* allow everything */
1775 break;
1776
1777 default:
1778 error = EINVAL;
1779 mutex_exit(p->p_lock);
1780 goto cleanup;
1781 }
1782 if (!match) {
1783 mutex_exit(p->p_lock);
1784 continue;
1785 }
1786
1787 /*
1788 * Grab a hold on the process.
1789 */
1790 if (mmmbrains) {
1791 zombie = true;
1792 } else {
1793 zombie = !rw_tryenter(&p->p_reflock, RW_READER);
1794 }
1795 if (zombie) {
1796 LIST_INSERT_AFTER(p, marker, p_list);
1797 }
1798
1799 if (buflen >= elem_size &&
1800 (type == KERN_PROC || elem_count > 0)) {
1801 if (type == KERN_PROC) {
1802 fill_proc(p, &kbuf->kproc.kp_proc);
1803 fill_eproc(p, &kbuf->kproc.kp_eproc, zombie);
1804 } else {
1805 fill_kproc2(p, &kbuf->kproc2, zombie);
1806 elem_count--;
1807 }
1808 mutex_exit(p->p_lock);
1809 mutex_exit(proc_lock);
1810 /*
1811 * Copy out elem_size, but not larger than kelem_size
1812 */
1813 error = sysctl_copyout(l, kbuf, dp,
1814 uimin(kelem_size, elem_size));
1815 mutex_enter(proc_lock);
1816 if (error) {
1817 goto bah;
1818 }
1819 dp += elem_size;
1820 buflen -= elem_size;
1821 } else {
1822 mutex_exit(p->p_lock);
1823 }
1824 needed += elem_size;
1825
1826 /*
1827 * Release reference to process.
1828 */
1829 if (zombie) {
1830 next = LIST_NEXT(marker, p_list);
1831 LIST_REMOVE(marker, p_list);
1832 } else {
1833 rw_exit(&p->p_reflock);
1834 next = LIST_NEXT(p, p_list);
1835 }
1836
1837 /*
1838 * Short-circuit break quickly!
1839 */
1840 if (op == KERN_PROC_PID)
1841 break;
1842 }
1843 mutex_exit(proc_lock);
1844
1845 if (where != NULL) {
1846 *oldlenp = dp - where;
1847 if (needed > *oldlenp) {
1848 error = ENOMEM;
1849 goto out;
1850 }
1851 } else {
1852 needed += KERN_PROCSLOP;
1853 *oldlenp = needed;
1854 }
1855 kmem_free(kbuf, sizeof(*kbuf));
1856 kmem_free(marker, sizeof(*marker));
1857 sysctl_relock();
1858 return 0;
1859 bah:
1860 if (zombie)
1861 LIST_REMOVE(marker, p_list);
1862 else
1863 rw_exit(&p->p_reflock);
1864 cleanup:
1865 mutex_exit(proc_lock);
1866 out:
1867 kmem_free(kbuf, sizeof(*kbuf));
1868 kmem_free(marker, sizeof(*marker));
1869 sysctl_relock();
1870 return error;
1871 }
1872
1873 int
1874 copyin_psstrings(struct proc *p, struct ps_strings *arginfo)
1875 {
1876
1877 #ifdef COMPAT_NETBSD32
1878 if (p->p_flag & PK_32) {
1879 struct ps_strings32 arginfo32;
1880
1881 int error = copyin_proc(p, (void *)p->p_psstrp, &arginfo32,
1882 sizeof(arginfo32));
1883 if (error)
1884 return error;
1885 arginfo->ps_argvstr = (void *)(uintptr_t)arginfo32.ps_argvstr;
1886 arginfo->ps_nargvstr = arginfo32.ps_nargvstr;
1887 arginfo->ps_envstr = (void *)(uintptr_t)arginfo32.ps_envstr;
1888 arginfo->ps_nenvstr = arginfo32.ps_nenvstr;
1889 return 0;
1890 }
1891 #endif
1892 return copyin_proc(p, (void *)p->p_psstrp, arginfo, sizeof(*arginfo));
1893 }
1894
1895 static int
1896 copy_procargs_sysctl_cb(void *cookie_, const void *src, size_t off, size_t len)
1897 {
1898 void **cookie = cookie_;
1899 struct lwp *l = cookie[0];
1900 char *dst = cookie[1];
1901
1902 return sysctl_copyout(l, src, dst + off, len);
1903 }
1904
1905 /*
1906 * sysctl helper routine for kern.proc_args pseudo-subtree.
1907 */
1908 static int
1909 sysctl_kern_proc_args(SYSCTLFN_ARGS)
1910 {
1911 struct ps_strings pss;
1912 struct proc *p;
1913 pid_t pid;
1914 int type, error;
1915 void *cookie[2];
1916
1917 if (namelen == 1 && name[0] == CTL_QUERY)
1918 return (sysctl_query(SYSCTLFN_CALL(rnode)));
1919
1920 if (newp != NULL || namelen != 2)
1921 return (EINVAL);
1922 pid = name[0];
1923 type = name[1];
1924
1925 switch (type) {
1926 case KERN_PROC_PATHNAME:
1927 sysctl_unlock();
1928 error = fill_pathname(l, pid, oldp, oldlenp);
1929 sysctl_relock();
1930 return error;
1931
1932 case KERN_PROC_ARGV:
1933 case KERN_PROC_NARGV:
1934 case KERN_PROC_ENV:
1935 case KERN_PROC_NENV:
1936 /* ok */
1937 break;
1938 default:
1939 return (EINVAL);
1940 }
1941
1942 sysctl_unlock();
1943
1944 /* check pid */
1945 mutex_enter(proc_lock);
1946 if ((p = proc_find(pid)) == NULL) {
1947 error = EINVAL;
1948 goto out_locked;
1949 }
1950 mutex_enter(p->p_lock);
1951
1952 /* Check permission. */
1953 if (type == KERN_PROC_ARGV || type == KERN_PROC_NARGV)
1954 error = kauth_authorize_process(l->l_cred, KAUTH_PROCESS_CANSEE,
1955 p, KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_ARGS), NULL, NULL);
1956 else if (type == KERN_PROC_ENV || type == KERN_PROC_NENV)
1957 error = kauth_authorize_process(l->l_cred, KAUTH_PROCESS_CANSEE,
1958 p, KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_ENV), NULL, NULL);
1959 else
1960 error = EINVAL; /* XXXGCC */
1961 if (error) {
1962 mutex_exit(p->p_lock);
1963 goto out_locked;
1964 }
1965
1966 if (oldp == NULL) {
1967 if (type == KERN_PROC_NARGV || type == KERN_PROC_NENV)
1968 *oldlenp = sizeof (int);
1969 else
1970 *oldlenp = ARG_MAX; /* XXX XXX XXX */
1971 error = 0;
1972 mutex_exit(p->p_lock);
1973 goto out_locked;
1974 }
1975
1976 /*
1977 * Zombies don't have a stack, so we can't read their psstrings.
1978 * System processes also don't have a user stack.
1979 */
1980 if (P_ZOMBIE(p) || (p->p_flag & PK_SYSTEM) != 0) {
1981 error = EINVAL;
1982 mutex_exit(p->p_lock);
1983 goto out_locked;
1984 }
1985
1986 error = rw_tryenter(&p->p_reflock, RW_READER) ? 0 : EBUSY;
1987 mutex_exit(p->p_lock);
1988 if (error) {
1989 goto out_locked;
1990 }
1991 mutex_exit(proc_lock);
1992
1993 if (type == KERN_PROC_NARGV || type == KERN_PROC_NENV) {
1994 int value;
1995 if ((error = copyin_psstrings(p, &pss)) == 0) {
1996 if (type == KERN_PROC_NARGV)
1997 value = pss.ps_nargvstr;
1998 else
1999 value = pss.ps_nenvstr;
2000 error = sysctl_copyout(l, &value, oldp, sizeof(value));
2001 *oldlenp = sizeof(value);
2002 }
2003 } else {
2004 cookie[0] = l;
2005 cookie[1] = oldp;
2006 error = copy_procargs(p, type, oldlenp,
2007 copy_procargs_sysctl_cb, cookie);
2008 }
2009 rw_exit(&p->p_reflock);
2010 sysctl_relock();
2011 return error;
2012
2013 out_locked:
2014 mutex_exit(proc_lock);
2015 sysctl_relock();
2016 return error;
2017 }
2018
2019 int
2020 copy_procargs(struct proc *p, int oid, size_t *limit,
2021 int (*cb)(void *, const void *, size_t, size_t), void *cookie)
2022 {
2023 struct ps_strings pss;
2024 size_t len, i, loaded, entry_len;
2025 struct uio auio;
2026 struct iovec aiov;
2027 int error, argvlen;
2028 char *arg;
2029 char **argv;
2030 vaddr_t user_argv;
2031 struct vmspace *vmspace;
2032
2033 /*
2034 * Allocate a temporary buffer to hold the argument vector and
2035 * the arguments themselve.
2036 */
2037 arg = kmem_alloc(PAGE_SIZE, KM_SLEEP);
2038 argv = kmem_alloc(PAGE_SIZE, KM_SLEEP);
2039
2040 /*
2041 * Lock the process down in memory.
2042 */
2043 vmspace = p->p_vmspace;
2044 uvmspace_addref(vmspace);
2045
2046 /*
2047 * Read in the ps_strings structure.
2048 */
2049 if ((error = copyin_psstrings(p, &pss)) != 0)
2050 goto done;
2051
2052 /*
2053 * Now read the address of the argument vector.
2054 */
2055 switch (oid) {
2056 case KERN_PROC_ARGV:
2057 user_argv = (uintptr_t)pss.ps_argvstr;
2058 argvlen = pss.ps_nargvstr;
2059 break;
2060 case KERN_PROC_ENV:
2061 user_argv = (uintptr_t)pss.ps_envstr;
2062 argvlen = pss.ps_nenvstr;
2063 break;
2064 default:
2065 error = EINVAL;
2066 goto done;
2067 }
2068
2069 if (argvlen < 0) {
2070 error = EIO;
2071 goto done;
2072 }
2073
2074
2075 /*
2076 * Now copy each string.
2077 */
2078 len = 0; /* bytes written to user buffer */
2079 loaded = 0; /* bytes from argv already processed */
2080 i = 0; /* To make compiler happy */
2081 entry_len = PROC_PTRSZ(p);
2082
2083 for (; argvlen; --argvlen) {
2084 int finished = 0;
2085 vaddr_t base;
2086 size_t xlen;
2087 int j;
2088
2089 if (loaded == 0) {
2090 size_t rem = entry_len * argvlen;
2091 loaded = MIN(rem, PAGE_SIZE);
2092 error = copyin_vmspace(vmspace,
2093 (const void *)user_argv, argv, loaded);
2094 if (error)
2095 break;
2096 user_argv += loaded;
2097 i = 0;
2098 }
2099
2100 #ifdef COMPAT_NETBSD32
2101 if (p->p_flag & PK_32) {
2102 netbsd32_charp *argv32;
2103
2104 argv32 = (netbsd32_charp *)argv;
2105 base = (vaddr_t)NETBSD32PTR64(argv32[i++]);
2106 } else
2107 #endif
2108 base = (vaddr_t)argv[i++];
2109 loaded -= entry_len;
2110
2111 /*
2112 * The program has messed around with its arguments,
2113 * possibly deleting some, and replacing them with
2114 * NULL's. Treat this as the last argument and not
2115 * a failure.
2116 */
2117 if (base == 0)
2118 break;
2119
2120 while (!finished) {
2121 xlen = PAGE_SIZE - (base & PAGE_MASK);
2122
2123 aiov.iov_base = arg;
2124 aiov.iov_len = PAGE_SIZE;
2125 auio.uio_iov = &aiov;
2126 auio.uio_iovcnt = 1;
2127 auio.uio_offset = base;
2128 auio.uio_resid = xlen;
2129 auio.uio_rw = UIO_READ;
2130 UIO_SETUP_SYSSPACE(&auio);
2131 error = uvm_io(&vmspace->vm_map, &auio, 0);
2132 if (error)
2133 goto done;
2134
2135 /* Look for the end of the string */
2136 for (j = 0; j < xlen; j++) {
2137 if (arg[j] == '\0') {
2138 xlen = j + 1;
2139 finished = 1;
2140 break;
2141 }
2142 }
2143
2144 /* Check for user buffer overflow */
2145 if (len + xlen > *limit) {
2146 finished = 1;
2147 if (len > *limit)
2148 xlen = 0;
2149 else
2150 xlen = *limit - len;
2151 }
2152
2153 /* Copyout the page */
2154 error = (*cb)(cookie, arg, len, xlen);
2155 if (error)
2156 goto done;
2157
2158 len += xlen;
2159 base += xlen;
2160 }
2161 }
2162 *limit = len;
2163
2164 done:
2165 kmem_free(argv, PAGE_SIZE);
2166 kmem_free(arg, PAGE_SIZE);
2167 uvmspace_free(vmspace);
2168 return error;
2169 }
2170
2171 /*
2172 * Fill in a proc structure for the specified process.
2173 */
2174 static void
2175 fill_proc(const struct proc *psrc, struct proc *p)
2176 {
2177 const bool allowaddr = get_expose_address(curproc);
2178
2179 COND_SET_VALUE(p->p_list, psrc->p_list, allowaddr);
2180 COND_SET_VALUE(p->p_auxlock, psrc->p_auxlock, allowaddr);
2181 COND_SET_VALUE(p->p_lock, psrc->p_lock, allowaddr);
2182 COND_SET_VALUE(p->p_stmutex, psrc->p_stmutex, allowaddr);
2183 COND_SET_VALUE(p->p_reflock, psrc->p_reflock, allowaddr);
2184 COND_SET_VALUE(p->p_waitcv, psrc->p_waitcv, allowaddr);
2185 COND_SET_VALUE(p->p_lwpcv, psrc->p_lwpcv, allowaddr);
2186 COND_SET_VALUE(p->p_cred, psrc->p_cred, allowaddr);
2187 COND_SET_VALUE(p->p_fd, psrc->p_fd, allowaddr);
2188 COND_SET_VALUE(p->p_cwdi, psrc->p_cwdi, allowaddr);
2189 COND_SET_VALUE(p->p_stats, psrc->p_stats, allowaddr);
2190 COND_SET_VALUE(p->p_limit, psrc->p_limit, allowaddr);
2191 COND_SET_VALUE(p->p_vmspace, psrc->p_vmspace, allowaddr);
2192 COND_SET_VALUE(p->p_sigacts, psrc->p_sigacts, allowaddr);
2193 COND_SET_VALUE(p->p_aio, psrc->p_aio, allowaddr);
2194 p->p_mqueue_cnt = psrc->p_mqueue_cnt;
2195 COND_SET_VALUE(p->p_specdataref, psrc->p_specdataref, allowaddr);
2196 p->p_exitsig = psrc->p_exitsig;
2197 p->p_flag = psrc->p_flag;
2198 p->p_sflag = psrc->p_sflag;
2199 p->p_slflag = psrc->p_slflag;
2200 p->p_lflag = psrc->p_lflag;
2201 p->p_stflag = psrc->p_stflag;
2202 p->p_stat = psrc->p_stat;
2203 p->p_trace_enabled = psrc->p_trace_enabled;
2204 p->p_pid = psrc->p_pid;
2205 COND_SET_VALUE(p->p_pglist, psrc->p_pglist, allowaddr);
2206 COND_SET_VALUE(p->p_pptr, psrc->p_pptr, allowaddr);
2207 COND_SET_VALUE(p->p_sibling, psrc->p_sibling, allowaddr);
2208 COND_SET_VALUE(p->p_children, psrc->p_children, allowaddr);
2209 COND_SET_VALUE(p->p_lwps, psrc->p_lwps, allowaddr);
2210 COND_SET_VALUE(p->p_raslist, psrc->p_raslist, allowaddr);
2211 p->p_nlwps = psrc->p_nlwps;
2212 p->p_nzlwps = psrc->p_nzlwps;
2213 p->p_nrlwps = psrc->p_nrlwps;
2214 p->p_nlwpwait = psrc->p_nlwpwait;
2215 p->p_ndlwps = psrc->p_ndlwps;
2216 p->p_nlwpid = psrc->p_nlwpid;
2217 p->p_nstopchild = psrc->p_nstopchild;
2218 p->p_waited = psrc->p_waited;
2219 COND_SET_VALUE(p->p_zomblwp, psrc->p_zomblwp, allowaddr);
2220 COND_SET_VALUE(p->p_vforklwp, psrc->p_vforklwp, allowaddr);
2221 COND_SET_VALUE(p->p_sched_info, psrc->p_sched_info, allowaddr);
2222 p->p_estcpu = psrc->p_estcpu;
2223 p->p_estcpu_inherited = psrc->p_estcpu_inherited;
2224 p->p_forktime = psrc->p_forktime;
2225 p->p_pctcpu = psrc->p_pctcpu;
2226 COND_SET_VALUE(p->p_opptr, psrc->p_opptr, allowaddr);
2227 COND_SET_VALUE(p->p_timers, psrc->p_timers, allowaddr);
2228 p->p_rtime = psrc->p_rtime;
2229 p->p_uticks = psrc->p_uticks;
2230 p->p_sticks = psrc->p_sticks;
2231 p->p_iticks = psrc->p_iticks;
2232 p->p_xutime = psrc->p_xutime;
2233 p->p_xstime = psrc->p_xstime;
2234 p->p_traceflag = psrc->p_traceflag;
2235 COND_SET_VALUE(p->p_tracep, psrc->p_tracep, allowaddr);
2236 COND_SET_VALUE(p->p_textvp, psrc->p_textvp, allowaddr);
2237 COND_SET_VALUE(p->p_emul, psrc->p_emul, allowaddr);
2238 COND_SET_VALUE(p->p_emuldata, psrc->p_emuldata, allowaddr);
2239 COND_SET_VALUE(p->p_execsw, psrc->p_execsw, allowaddr);
2240 COND_SET_VALUE(p->p_klist, psrc->p_klist, allowaddr);
2241 COND_SET_VALUE(p->p_sigwaiters, psrc->p_sigwaiters, allowaddr);
2242 COND_SET_VALUE(p->p_sigpend, psrc->p_sigpend, allowaddr);
2243 COND_SET_VALUE(p->p_lwpctl, psrc->p_lwpctl, allowaddr);
2244 p->p_ppid = psrc->p_ppid;
2245 p->p_fpid = psrc->p_fpid;
2246 p->p_vfpid = psrc->p_vfpid;
2247 p->p_vfpid_done = psrc->p_vfpid_done;
2248 p->p_lwp_created = psrc->p_lwp_created;
2249 p->p_lwp_exited = psrc->p_lwp_exited;
2250 p->p_nsems = psrc->p_nsems;
2251 COND_SET_VALUE(p->p_path, psrc->p_path, allowaddr);
2252 COND_SET_VALUE(p->p_sigctx, psrc->p_sigctx, allowaddr);
2253 p->p_nice = psrc->p_nice;
2254 memcpy(p->p_comm, psrc->p_comm, sizeof(p->p_comm));
2255 COND_SET_VALUE(p->p_pgrp, psrc->p_pgrp, allowaddr);
2256 COND_SET_VALUE(p->p_psstrp, psrc->p_psstrp, allowaddr);
2257 p->p_pax = psrc->p_pax;
2258 p->p_xexit = psrc->p_xexit;
2259 p->p_xsig = psrc->p_xsig;
2260 p->p_acflag = psrc->p_acflag;
2261 COND_SET_VALUE(p->p_md, psrc->p_md, allowaddr);
2262 p->p_stackbase = psrc->p_stackbase;
2263 COND_SET_VALUE(p->p_dtrace, psrc->p_dtrace, allowaddr);
2264 }
2265
2266 /*
2267 * Fill in an eproc structure for the specified process.
2268 */
2269 void
2270 fill_eproc(struct proc *p, struct eproc *ep, bool zombie)
2271 {
2272 struct tty *tp;
2273 struct lwp *l;
2274
2275 KASSERT(mutex_owned(proc_lock));
2276 KASSERT(mutex_owned(p->p_lock));
2277
2278 const bool allowaddr = get_expose_address(curproc);
2279
2280 COND_SET_VALUE(ep->e_paddr, p, allowaddr);
2281 COND_SET_VALUE(ep->e_sess, p->p_session, allowaddr);
2282 if (p->p_cred) {
2283 kauth_cred_topcred(p->p_cred, &ep->e_pcred);
2284 kauth_cred_toucred(p->p_cred, &ep->e_ucred);
2285 }
2286 if (p->p_stat != SIDL && !P_ZOMBIE(p) && !zombie) {
2287 struct vmspace *vm = p->p_vmspace;
2288
2289 ep->e_vm.vm_rssize = vm_resident_count(vm);
2290 ep->e_vm.vm_tsize = vm->vm_tsize;
2291 ep->e_vm.vm_dsize = vm->vm_dsize;
2292 ep->e_vm.vm_ssize = vm->vm_ssize;
2293 ep->e_vm.vm_map.size = vm->vm_map.size;
2294
2295 /* Pick the primary (first) LWP */
2296 l = proc_active_lwp(p);
2297 KASSERT(l != NULL);
2298 lwp_lock(l);
2299 if (l->l_wchan)
2300 strncpy(ep->e_wmesg, l->l_wmesg, WMESGLEN);
2301 lwp_unlock(l);
2302 }
2303 ep->e_ppid = p->p_ppid;
2304 if (p->p_pgrp && p->p_session) {
2305 ep->e_pgid = p->p_pgrp->pg_id;
2306 ep->e_jobc = p->p_pgrp->pg_jobc;
2307 ep->e_sid = p->p_session->s_sid;
2308 if ((p->p_lflag & PL_CONTROLT) &&
2309 (tp = p->p_session->s_ttyp)) {
2310 ep->e_tdev = tp->t_dev;
2311 ep->e_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PGID;
2312 COND_SET_VALUE(ep->e_tsess, tp->t_session, allowaddr);
2313 } else
2314 ep->e_tdev = (uint32_t)NODEV;
2315 ep->e_flag = p->p_session->s_ttyvp ? EPROC_CTTY : 0;
2316 if (SESS_LEADER(p))
2317 ep->e_flag |= EPROC_SLEADER;
2318 strncpy(ep->e_login, p->p_session->s_login, MAXLOGNAME);
2319 }
2320 ep->e_xsize = ep->e_xrssize = 0;
2321 ep->e_xccount = ep->e_xswrss = 0;
2322 }
2323
2324 /*
2325 * Fill in a kinfo_proc2 structure for the specified process.
2326 */
2327 void
2328 fill_kproc2(struct proc *p, struct kinfo_proc2 *ki, bool zombie)
2329 {
2330 struct tty *tp;
2331 struct lwp *l, *l2;
2332 struct timeval ut, st, rt;
2333 sigset_t ss1, ss2;
2334 struct rusage ru;
2335 struct vmspace *vm;
2336
2337 KASSERT(mutex_owned(proc_lock));
2338 KASSERT(mutex_owned(p->p_lock));
2339
2340 const bool allowaddr = get_expose_address(curproc);
2341
2342 sigemptyset(&ss1);
2343 sigemptyset(&ss2);
2344
2345 COND_SET_VALUE(ki->p_paddr, PTRTOUINT64(p), allowaddr);
2346 COND_SET_VALUE(ki->p_fd, PTRTOUINT64(p->p_fd), allowaddr);
2347 COND_SET_VALUE(ki->p_cwdi, PTRTOUINT64(p->p_cwdi), allowaddr);
2348 COND_SET_VALUE(ki->p_stats, PTRTOUINT64(p->p_stats), allowaddr);
2349 COND_SET_VALUE(ki->p_limit, PTRTOUINT64(p->p_limit), allowaddr);
2350 COND_SET_VALUE(ki->p_vmspace, PTRTOUINT64(p->p_vmspace), allowaddr);
2351 COND_SET_VALUE(ki->p_sigacts, PTRTOUINT64(p->p_sigacts), allowaddr);
2352 COND_SET_VALUE(ki->p_sess, PTRTOUINT64(p->p_session), allowaddr);
2353 ki->p_tsess = 0; /* may be changed if controlling tty below */
2354 COND_SET_VALUE(ki->p_ru, PTRTOUINT64(&p->p_stats->p_ru), allowaddr);
2355 ki->p_eflag = 0;
2356 ki->p_exitsig = p->p_exitsig;
2357 ki->p_flag = L_INMEM; /* Process never swapped out */
2358 ki->p_flag |= sysctl_map_flags(sysctl_flagmap, p->p_flag);
2359 ki->p_flag |= sysctl_map_flags(sysctl_sflagmap, p->p_sflag);
2360 ki->p_flag |= sysctl_map_flags(sysctl_slflagmap, p->p_slflag);
2361 ki->p_flag |= sysctl_map_flags(sysctl_lflagmap, p->p_lflag);
2362 ki->p_flag |= sysctl_map_flags(sysctl_stflagmap, p->p_stflag);
2363 ki->p_pid = p->p_pid;
2364 ki->p_ppid = p->p_ppid;
2365 ki->p_uid = kauth_cred_geteuid(p->p_cred);
2366 ki->p_ruid = kauth_cred_getuid(p->p_cred);
2367 ki->p_gid = kauth_cred_getegid(p->p_cred);
2368 ki->p_rgid = kauth_cred_getgid(p->p_cred);
2369 ki->p_svuid = kauth_cred_getsvuid(p->p_cred);
2370 ki->p_svgid = kauth_cred_getsvgid(p->p_cred);
2371 ki->p_ngroups = kauth_cred_ngroups(p->p_cred);
2372 kauth_cred_getgroups(p->p_cred, ki->p_groups,
2373 uimin(ki->p_ngroups, sizeof(ki->p_groups) / sizeof(ki->p_groups[0])),
2374 UIO_SYSSPACE);
2375
2376 ki->p_uticks = p->p_uticks;
2377 ki->p_sticks = p->p_sticks;
2378 ki->p_iticks = p->p_iticks;
2379 ki->p_tpgid = NO_PGID; /* may be changed if controlling tty below */
2380 COND_SET_VALUE(ki->p_tracep, PTRTOUINT64(p->p_tracep), allowaddr);
2381 ki->p_traceflag = p->p_traceflag;
2382
2383 memcpy(&ki->p_sigignore, &p->p_sigctx.ps_sigignore,sizeof(ki_sigset_t));
2384 memcpy(&ki->p_sigcatch, &p->p_sigctx.ps_sigcatch, sizeof(ki_sigset_t));
2385
2386 ki->p_cpticks = 0;
2387 ki->p_pctcpu = p->p_pctcpu;
2388 ki->p_estcpu = 0;
2389 ki->p_stat = p->p_stat; /* Will likely be overridden by LWP status */
2390 ki->p_realstat = p->p_stat;
2391 ki->p_nice = p->p_nice;
2392 ki->p_xstat = P_WAITSTATUS(p);
2393 ki->p_acflag = p->p_acflag;
2394
2395 strncpy(ki->p_comm, p->p_comm,
2396 uimin(sizeof(ki->p_comm), sizeof(p->p_comm)));
2397 strncpy(ki->p_ename, p->p_emul->e_name, sizeof(ki->p_ename));
2398
2399 ki->p_nlwps = p->p_nlwps;
2400 ki->p_realflag = ki->p_flag;
2401
2402 if (p->p_stat != SIDL && !P_ZOMBIE(p) && !zombie) {
2403 vm = p->p_vmspace;
2404 ki->p_vm_rssize = vm_resident_count(vm);
2405 ki->p_vm_tsize = vm->vm_tsize;
2406 ki->p_vm_dsize = vm->vm_dsize;
2407 ki->p_vm_ssize = vm->vm_ssize;
2408 ki->p_vm_vsize = atop(vm->vm_map.size);
2409 /*
2410 * Since the stack is initially mapped mostly with
2411 * PROT_NONE and grown as needed, adjust the "mapped size"
2412 * to skip the unused stack portion.
2413 */
2414 ki->p_vm_msize =
2415 atop(vm->vm_map.size) - vm->vm_issize + vm->vm_ssize;
2416
2417 /* Pick the primary (first) LWP */
2418 l = proc_active_lwp(p);
2419 KASSERT(l != NULL);
2420 lwp_lock(l);
2421 ki->p_nrlwps = p->p_nrlwps;
2422 ki->p_forw = 0;
2423 ki->p_back = 0;
2424 COND_SET_VALUE(ki->p_addr, PTRTOUINT64(l->l_addr), allowaddr);
2425 ki->p_stat = l->l_stat;
2426 ki->p_flag |= sysctl_map_flags(sysctl_lwpflagmap, l->l_flag);
2427 ki->p_swtime = l->l_swtime;
2428 ki->p_slptime = l->l_slptime;
2429 if (l->l_stat == LSONPROC)
2430 ki->p_schedflags = l->l_cpu->ci_schedstate.spc_flags;
2431 else
2432 ki->p_schedflags = 0;
2433 ki->p_priority = lwp_eprio(l);
2434 ki->p_usrpri = l->l_priority;
2435 if (l->l_wchan)
2436 strncpy(ki->p_wmesg, l->l_wmesg, sizeof(ki->p_wmesg));
2437 COND_SET_VALUE(ki->p_wchan, PTRTOUINT64(l->l_wchan), allowaddr);
2438 ki->p_cpuid = cpu_index(l->l_cpu);
2439 lwp_unlock(l);
2440 LIST_FOREACH(l, &p->p_lwps, l_sibling) {
2441 /* This is hardly correct, but... */
2442 sigplusset(&l->l_sigpend.sp_set, &ss1);
2443 sigplusset(&l->l_sigmask, &ss2);
2444 ki->p_cpticks += l->l_cpticks;
2445 ki->p_pctcpu += l->l_pctcpu;
2446 ki->p_estcpu += l->l_estcpu;
2447 }
2448 }
2449 sigplusset(&p->p_sigpend.sp_set, &ss2);
2450 memcpy(&ki->p_siglist, &ss1, sizeof(ki_sigset_t));
2451 memcpy(&ki->p_sigmask, &ss2, sizeof(ki_sigset_t));
2452
2453 if (p->p_session != NULL) {
2454 ki->p_sid = p->p_session->s_sid;
2455 ki->p__pgid = p->p_pgrp->pg_id;
2456 if (p->p_session->s_ttyvp)
2457 ki->p_eflag |= EPROC_CTTY;
2458 if (SESS_LEADER(p))
2459 ki->p_eflag |= EPROC_SLEADER;
2460 strncpy(ki->p_login, p->p_session->s_login,
2461 uimin(sizeof ki->p_login - 1, sizeof p->p_session->s_login));
2462 ki->p_jobc = p->p_pgrp->pg_jobc;
2463 if ((p->p_lflag & PL_CONTROLT) && (tp = p->p_session->s_ttyp)) {
2464 ki->p_tdev = tp->t_dev;
2465 ki->p_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PGID;
2466 COND_SET_VALUE(ki->p_tsess, PTRTOUINT64(tp->t_session),
2467 allowaddr);
2468 } else {
2469 ki->p_tdev = (int32_t)NODEV;
2470 }
2471 }
2472
2473 if (!P_ZOMBIE(p) && !zombie) {
2474 ki->p_uvalid = 1;
2475 ki->p_ustart_sec = p->p_stats->p_start.tv_sec;
2476 ki->p_ustart_usec = p->p_stats->p_start.tv_usec;
2477
2478 calcru(p, &ut, &st, NULL, &rt);
2479 ki->p_rtime_sec = rt.tv_sec;
2480 ki->p_rtime_usec = rt.tv_usec;
2481 ki->p_uutime_sec = ut.tv_sec;
2482 ki->p_uutime_usec = ut.tv_usec;
2483 ki->p_ustime_sec = st.tv_sec;
2484 ki->p_ustime_usec = st.tv_usec;
2485
2486 memcpy(&ru, &p->p_stats->p_ru, sizeof(ru));
2487 ki->p_uru_nvcsw = 0;
2488 ki->p_uru_nivcsw = 0;
2489 LIST_FOREACH(l2, &p->p_lwps, l_sibling) {
2490 ki->p_uru_nvcsw += (l2->l_ncsw - l2->l_nivcsw);
2491 ki->p_uru_nivcsw += l2->l_nivcsw;
2492 ruadd(&ru, &l2->l_ru);
2493 }
2494 ki->p_uru_maxrss = ru.ru_maxrss;
2495 ki->p_uru_ixrss = ru.ru_ixrss;
2496 ki->p_uru_idrss = ru.ru_idrss;
2497 ki->p_uru_isrss = ru.ru_isrss;
2498 ki->p_uru_minflt = ru.ru_minflt;
2499 ki->p_uru_majflt = ru.ru_majflt;
2500 ki->p_uru_nswap = ru.ru_nswap;
2501 ki->p_uru_inblock = ru.ru_inblock;
2502 ki->p_uru_oublock = ru.ru_oublock;
2503 ki->p_uru_msgsnd = ru.ru_msgsnd;
2504 ki->p_uru_msgrcv = ru.ru_msgrcv;
2505 ki->p_uru_nsignals = ru.ru_nsignals;
2506
2507 timeradd(&p->p_stats->p_cru.ru_utime,
2508 &p->p_stats->p_cru.ru_stime, &ut);
2509 ki->p_uctime_sec = ut.tv_sec;
2510 ki->p_uctime_usec = ut.tv_usec;
2511 }
2512 }
2513
2514
2515 int
2516 proc_find_locked(struct lwp *l, struct proc **p, pid_t pid)
2517 {
2518 int error;
2519
2520 mutex_enter(proc_lock);
2521 if (pid == -1)
2522 *p = l->l_proc;
2523 else
2524 *p = proc_find(pid);
2525
2526 if (*p == NULL) {
2527 if (pid != -1)
2528 mutex_exit(proc_lock);
2529 return ESRCH;
2530 }
2531 if (pid != -1)
2532 mutex_enter((*p)->p_lock);
2533 mutex_exit(proc_lock);
2534
2535 error = kauth_authorize_process(l->l_cred,
2536 KAUTH_PROCESS_CANSEE, *p,
2537 KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_ENTRY), NULL, NULL);
2538 if (error) {
2539 if (pid != -1)
2540 mutex_exit((*p)->p_lock);
2541 }
2542 return error;
2543 }
2544
2545 static int
2546 fill_pathname(struct lwp *l, pid_t pid, void *oldp, size_t *oldlenp)
2547 {
2548 int error;
2549 struct proc *p;
2550
2551 if ((error = proc_find_locked(l, &p, pid)) != 0)
2552 return error;
2553
2554 if (p->p_path == NULL) {
2555 if (pid != -1)
2556 mutex_exit(p->p_lock);
2557 return ENOENT;
2558 }
2559
2560 size_t len = strlen(p->p_path) + 1;
2561 if (oldp != NULL) {
2562 size_t copylen = uimin(len, *oldlenp);
2563 error = sysctl_copyout(l, p->p_path, oldp, copylen);
2564 if (error == 0 && *oldlenp < len)
2565 error = ENOSPC;
2566 }
2567 *oldlenp = len;
2568 if (pid != -1)
2569 mutex_exit(p->p_lock);
2570 return error;
2571 }
2572
2573 int
2574 proc_getauxv(struct proc *p, void **buf, size_t *len)
2575 {
2576 struct ps_strings pss;
2577 int error;
2578 void *uauxv, *kauxv;
2579 size_t size;
2580
2581 if ((error = copyin_psstrings(p, &pss)) != 0)
2582 return error;
2583 if (pss.ps_envstr == NULL)
2584 return EIO;
2585
2586 size = p->p_execsw->es_arglen;
2587 if (size == 0)
2588 return EIO;
2589
2590 size_t ptrsz = PROC_PTRSZ(p);
2591 uauxv = (void *)((char *)pss.ps_envstr + (pss.ps_nenvstr + 1) * ptrsz);
2592
2593 kauxv = kmem_alloc(size, KM_SLEEP);
2594
2595 error = copyin_proc(p, uauxv, kauxv, size);
2596 if (error) {
2597 kmem_free(kauxv, size);
2598 return error;
2599 }
2600
2601 *buf = kauxv;
2602 *len = size;
2603
2604 return 0;
2605 }
2606