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