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