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