1 /* 2 * Copyright (c) 1982, 1986, 1989, 1991, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * @(#)kern_proc.c 8.7 (Berkeley) 2/14/95 34 * $FreeBSD$ 35 */ 36 37 #include <sys/param.h> 38 #include <sys/systm.h> 39 #include <sys/kernel.h> 40 #include <sys/sysctl.h> 41 #include <sys/malloc.h> 42 #include <sys/proc.h> 43 #include <sys/filedesc.h> 44 #include <sys/tty.h> 45 #include <sys/signalvar.h> 46 #include <sys/sx.h> 47 #include <vm/vm.h> 48 #include <sys/lock.h> 49 #include <vm/pmap.h> 50 #include <vm/vm_map.h> 51 #include <sys/user.h> 52 #include <vm/vm_zone.h> 53 #include <sys/jail.h> 54 55 static MALLOC_DEFINE(M_PGRP, "pgrp", "process group header"); 56 MALLOC_DEFINE(M_SESSION, "session", "session header"); 57 static MALLOC_DEFINE(M_PROC, "proc", "Proc structures"); 58 MALLOC_DEFINE(M_SUBPROC, "subproc", "Proc sub-structures"); 59 60 int ps_showallprocs = 1; 61 SYSCTL_INT(_kern, OID_AUTO, ps_showallprocs, CTLFLAG_RW, 62 &ps_showallprocs, 0, ""); 63 64 static void pgdelete __P((struct pgrp *)); 65 66 static void orphanpg __P((struct pgrp *pg)); 67 68 /* 69 * Other process lists 70 */ 71 struct pidhashhead *pidhashtbl; 72 u_long pidhash; 73 struct pgrphashhead *pgrphashtbl; 74 u_long pgrphash; 75 struct proclist allproc; 76 struct proclist zombproc; 77 struct sx allproc_lock; 78 struct sx proctree_lock; 79 vm_zone_t proc_zone; 80 vm_zone_t ithread_zone; 81 82 /* 83 * Initialize global process hashing structures. 84 */ 85 void 86 procinit() 87 { 88 int i, j; 89 90 sx_init(&allproc_lock, "allproc"); 91 sx_init(&proctree_lock, "proctree"); 92 LIST_INIT(&allproc); 93 LIST_INIT(&zombproc); 94 pidhashtbl = hashinit(maxproc / 4, M_PROC, &pidhash); 95 pgrphashtbl = hashinit(maxproc / 4, M_PROC, &pgrphash); 96 proc_zone = zinit("PROC", sizeof (struct proc), 0, 0, 5); 97 uihashinit(); 98 /* 99 * This should really be a compile time warning, but I do 100 * not know of any way to do that... 101 */ 102 if (sizeof(struct kinfo_proc) != KINFO_PROC_SIZE) { 103 printf("This message will repeat for the next 20 seconds\n"); 104 for (i = 0; i < 20; i++) { 105 printf("WARNING: size of kinfo_proc (%ld) should be %d!!!\n", 106 (long)sizeof(struct kinfo_proc), KINFO_PROC_SIZE); 107 printf("The kinfo_proc structure was changed "); 108 printf("incorrectly in <sys/user.h>\n"); 109 for (j = 0; j < 0x7ffffff; j++); 110 } 111 112 } 113 } 114 115 /* 116 * Is p an inferior of the current process? 117 */ 118 int 119 inferior(p) 120 register struct proc *p; 121 { 122 int rval = 1; 123 124 sx_slock(&proctree_lock); 125 for (; p != curproc; p = p->p_pptr) 126 if (p->p_pid == 0) { 127 rval = 0; 128 break; 129 } 130 sx_sunlock(&proctree_lock); 131 return (rval); 132 } 133 134 /* 135 * Locate a process by number 136 */ 137 struct proc * 138 pfind(pid) 139 register pid_t pid; 140 { 141 register struct proc *p; 142 143 sx_slock(&allproc_lock); 144 LIST_FOREACH(p, PIDHASH(pid), p_hash) 145 if (p->p_pid == pid) 146 break; 147 sx_sunlock(&allproc_lock); 148 return (p); 149 } 150 151 /* 152 * Locate a process group by number 153 */ 154 struct pgrp * 155 pgfind(pgid) 156 register pid_t pgid; 157 { 158 register struct pgrp *pgrp; 159 160 LIST_FOREACH(pgrp, PGRPHASH(pgid), pg_hash) 161 if (pgrp->pg_id == pgid) 162 return (pgrp); 163 return (NULL); 164 } 165 166 /* 167 * Move p to a new or existing process group (and session) 168 */ 169 int 170 enterpgrp(p, pgid, mksess) 171 register struct proc *p; 172 pid_t pgid; 173 int mksess; 174 { 175 register struct pgrp *pgrp = pgfind(pgid); 176 struct pgrp *savegrp; 177 178 KASSERT(pgrp == NULL || !mksess, 179 ("enterpgrp: setsid into non-empty pgrp")); 180 KASSERT(!SESS_LEADER(p), 181 ("enterpgrp: session leader attempted setpgrp")); 182 183 if (pgrp == NULL) { 184 pid_t savepid = p->p_pid; 185 struct proc *np; 186 /* 187 * new process group 188 */ 189 KASSERT(p->p_pid == pgid, 190 ("enterpgrp: new pgrp and pid != pgid")); 191 if ((np = pfind(savepid)) == NULL || np != p) 192 return (ESRCH); 193 MALLOC(pgrp, struct pgrp *, sizeof(struct pgrp), M_PGRP, 194 M_WAITOK); 195 if (mksess) { 196 register struct session *sess; 197 198 /* 199 * new session 200 */ 201 MALLOC(sess, struct session *, sizeof(struct session), 202 M_SESSION, M_WAITOK); 203 sess->s_leader = p; 204 sess->s_sid = p->p_pid; 205 sess->s_count = 1; 206 sess->s_ttyvp = NULL; 207 sess->s_ttyp = NULL; 208 bcopy(p->p_session->s_login, sess->s_login, 209 sizeof(sess->s_login)); 210 PROC_LOCK(p); 211 p->p_flag &= ~P_CONTROLT; 212 PROC_UNLOCK(p); 213 pgrp->pg_session = sess; 214 KASSERT(p == curproc, 215 ("enterpgrp: mksession and p != curproc")); 216 } else { 217 pgrp->pg_session = p->p_session; 218 pgrp->pg_session->s_count++; 219 } 220 pgrp->pg_id = pgid; 221 LIST_INIT(&pgrp->pg_members); 222 LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash); 223 pgrp->pg_jobc = 0; 224 SLIST_INIT(&pgrp->pg_sigiolst); 225 } else if (pgrp == p->p_pgrp) 226 return (0); 227 228 /* 229 * Adjust eligibility of affected pgrps to participate in job control. 230 * Increment eligibility counts before decrementing, otherwise we 231 * could reach 0 spuriously during the first call. 232 */ 233 fixjobc(p, pgrp, 1); 234 fixjobc(p, p->p_pgrp, 0); 235 236 PROC_LOCK(p); 237 LIST_REMOVE(p, p_pglist); 238 savegrp = p->p_pgrp; 239 p->p_pgrp = pgrp; 240 LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist); 241 PROC_UNLOCK(p); 242 if (LIST_EMPTY(&savegrp->pg_members)) 243 pgdelete(savegrp); 244 return (0); 245 } 246 247 /* 248 * remove process from process group 249 */ 250 int 251 leavepgrp(p) 252 register struct proc *p; 253 { 254 struct pgrp *savegrp; 255 256 PROC_LOCK(p); 257 LIST_REMOVE(p, p_pglist); 258 savegrp = p->p_pgrp; 259 p->p_pgrp = NULL; 260 PROC_UNLOCK(p); 261 if (LIST_EMPTY(&savegrp->pg_members)) 262 pgdelete(savegrp); 263 return (0); 264 } 265 266 /* 267 * delete a process group 268 */ 269 static void 270 pgdelete(pgrp) 271 register struct pgrp *pgrp; 272 { 273 274 /* 275 * Reset any sigio structures pointing to us as a result of 276 * F_SETOWN with our pgid. 277 */ 278 funsetownlst(&pgrp->pg_sigiolst); 279 280 if (pgrp->pg_session->s_ttyp != NULL && 281 pgrp->pg_session->s_ttyp->t_pgrp == pgrp) 282 pgrp->pg_session->s_ttyp->t_pgrp = NULL; 283 LIST_REMOVE(pgrp, pg_hash); 284 if (--pgrp->pg_session->s_count == 0) 285 FREE(pgrp->pg_session, M_SESSION); 286 FREE(pgrp, M_PGRP); 287 } 288 289 /* 290 * Adjust pgrp jobc counters when specified process changes process group. 291 * We count the number of processes in each process group that "qualify" 292 * the group for terminal job control (those with a parent in a different 293 * process group of the same session). If that count reaches zero, the 294 * process group becomes orphaned. Check both the specified process' 295 * process group and that of its children. 296 * entering == 0 => p is leaving specified group. 297 * entering == 1 => p is entering specified group. 298 */ 299 void 300 fixjobc(p, pgrp, entering) 301 register struct proc *p; 302 register struct pgrp *pgrp; 303 int entering; 304 { 305 register struct pgrp *hispgrp; 306 register struct session *mysession = pgrp->pg_session; 307 308 /* 309 * Check p's parent to see whether p qualifies its own process 310 * group; if so, adjust count for p's process group. 311 */ 312 sx_slock(&proctree_lock); 313 if ((hispgrp = p->p_pptr->p_pgrp) != pgrp && 314 hispgrp->pg_session == mysession) { 315 if (entering) 316 pgrp->pg_jobc++; 317 else if (--pgrp->pg_jobc == 0) 318 orphanpg(pgrp); 319 } 320 321 /* 322 * Check this process' children to see whether they qualify 323 * their process groups; if so, adjust counts for children's 324 * process groups. 325 */ 326 LIST_FOREACH(p, &p->p_children, p_sibling) 327 if ((hispgrp = p->p_pgrp) != pgrp && 328 hispgrp->pg_session == mysession && 329 p->p_stat != SZOMB) { 330 if (entering) 331 hispgrp->pg_jobc++; 332 else if (--hispgrp->pg_jobc == 0) 333 orphanpg(hispgrp); 334 } 335 sx_sunlock(&proctree_lock); 336 } 337 338 /* 339 * A process group has become orphaned; 340 * if there are any stopped processes in the group, 341 * hang-up all process in that group. 342 */ 343 static void 344 orphanpg(pg) 345 struct pgrp *pg; 346 { 347 register struct proc *p; 348 349 mtx_lock_spin(&sched_lock); 350 LIST_FOREACH(p, &pg->pg_members, p_pglist) { 351 if (p->p_stat == SSTOP) { 352 mtx_unlock_spin(&sched_lock); 353 LIST_FOREACH(p, &pg->pg_members, p_pglist) { 354 PROC_LOCK(p); 355 psignal(p, SIGHUP); 356 psignal(p, SIGCONT); 357 PROC_UNLOCK(p); 358 } 359 return; 360 } 361 } 362 mtx_unlock_spin(&sched_lock); 363 } 364 365 #include "opt_ddb.h" 366 #ifdef DDB 367 #include <ddb/ddb.h> 368 369 DB_SHOW_COMMAND(pgrpdump, pgrpdump) 370 { 371 register struct pgrp *pgrp; 372 register struct proc *p; 373 register int i; 374 375 for (i = 0; i <= pgrphash; i++) { 376 if (!LIST_EMPTY(&pgrphashtbl[i])) { 377 printf("\tindx %d\n", i); 378 LIST_FOREACH(pgrp, &pgrphashtbl[i], pg_hash) { 379 printf( 380 "\tpgrp %p, pgid %ld, sess %p, sesscnt %d, mem %p\n", 381 (void *)pgrp, (long)pgrp->pg_id, 382 (void *)pgrp->pg_session, 383 pgrp->pg_session->s_count, 384 (void *)LIST_FIRST(&pgrp->pg_members)); 385 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) { 386 printf("\t\tpid %ld addr %p pgrp %p\n", 387 (long)p->p_pid, (void *)p, 388 (void *)p->p_pgrp); 389 } 390 } 391 } 392 } 393 } 394 #endif /* DDB */ 395 396 /* 397 * Fill in an kinfo_proc structure for the specified process. 398 */ 399 void 400 fill_kinfo_proc(p, kp) 401 struct proc *p; 402 struct kinfo_proc *kp; 403 { 404 struct tty *tp; 405 struct session *sp; 406 407 bzero(kp, sizeof(*kp)); 408 409 kp->ki_structsize = sizeof(*kp); 410 kp->ki_paddr = p; 411 PROC_LOCK(p); 412 kp->ki_addr = p->p_addr; 413 kp->ki_args = p->p_args; 414 kp->ki_tracep = p->p_tracep; 415 kp->ki_textvp = p->p_textvp; 416 kp->ki_fd = p->p_fd; 417 kp->ki_vmspace = p->p_vmspace; 418 if (p->p_cred) { 419 kp->ki_uid = p->p_cred->pc_ucred->cr_uid; 420 kp->ki_ruid = p->p_cred->p_ruid; 421 kp->ki_svuid = p->p_cred->p_svuid; 422 kp->ki_ngroups = p->p_cred->pc_ucred->cr_ngroups; 423 bcopy(p->p_cred->pc_ucred->cr_groups, kp->ki_groups, 424 NGROUPS * sizeof(gid_t)); 425 kp->ki_rgid = p->p_cred->p_rgid; 426 kp->ki_svgid = p->p_cred->p_svgid; 427 } 428 if (p->p_procsig) { 429 kp->ki_sigignore = p->p_procsig->ps_sigignore; 430 kp->ki_sigcatch = p->p_procsig->ps_sigcatch; 431 } 432 mtx_lock_spin(&sched_lock); 433 if (p->p_stat != SIDL && p->p_stat != SZOMB && p->p_vmspace != NULL) { 434 struct vmspace *vm = p->p_vmspace; 435 436 kp->ki_size = vm->vm_map.size; 437 kp->ki_rssize = vmspace_resident_count(vm); /*XXX*/ 438 kp->ki_swrss = vm->vm_swrss; 439 kp->ki_tsize = vm->vm_tsize; 440 kp->ki_dsize = vm->vm_dsize; 441 kp->ki_ssize = vm->vm_ssize; 442 } 443 if ((p->p_sflag & PS_INMEM) && p->p_stats) { 444 kp->ki_start = p->p_stats->p_start; 445 kp->ki_rusage = p->p_stats->p_ru; 446 kp->ki_childtime.tv_sec = p->p_stats->p_cru.ru_utime.tv_sec + 447 p->p_stats->p_cru.ru_stime.tv_sec; 448 kp->ki_childtime.tv_usec = p->p_stats->p_cru.ru_utime.tv_usec + 449 p->p_stats->p_cru.ru_stime.tv_usec; 450 } 451 if (p->p_wmesg) { 452 strncpy(kp->ki_wmesg, p->p_wmesg, WMESGLEN); 453 kp->ki_wmesg[WMESGLEN] = 0; 454 } 455 if (p->p_stat == SMTX) { 456 kp->ki_kiflag |= KI_MTXBLOCK; 457 strncpy(kp->ki_mtxname, p->p_mtxname, MTXNAMELEN); 458 kp->ki_mtxname[MTXNAMELEN] = 0; 459 } 460 kp->ki_stat = p->p_stat; 461 kp->ki_sflag = p->p_sflag; 462 kp->ki_pctcpu = p->p_pctcpu; 463 kp->ki_estcpu = p->p_estcpu; 464 kp->ki_slptime = p->p_slptime; 465 kp->ki_swtime = p->p_swtime; 466 kp->ki_wchan = p->p_wchan; 467 kp->ki_traceflag = p->p_traceflag; 468 kp->ki_pri = p->p_pri; 469 kp->ki_nice = p->p_nice; 470 kp->ki_runtime = p->p_runtime; 471 kp->ki_pid = p->p_pid; 472 kp->ki_rqindex = p->p_rqindex; 473 kp->ki_oncpu = p->p_oncpu; 474 kp->ki_lastcpu = p->p_lastcpu; 475 mtx_unlock_spin(&sched_lock); 476 sp = NULL; 477 if (p->p_pgrp) { 478 kp->ki_pgid = p->p_pgrp->pg_id; 479 kp->ki_jobc = p->p_pgrp->pg_jobc; 480 sp = p->p_pgrp->pg_session; 481 482 if (sp != NULL) { 483 kp->ki_sid = sp->s_sid; 484 bcopy(sp->s_login, kp->ki_login, sizeof(kp->ki_login)); 485 if (sp->s_ttyvp) 486 kp->ki_kiflag = KI_CTTY; 487 if (SESS_LEADER(p)) 488 kp->ki_kiflag |= KI_SLEADER; 489 } 490 } 491 if ((p->p_flag & P_CONTROLT) && sp && ((tp = sp->s_ttyp) != NULL)) { 492 kp->ki_tdev = dev2udev(tp->t_dev); 493 kp->ki_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID; 494 if (tp->t_session) 495 kp->ki_tsid = tp->t_session->s_sid; 496 } else 497 kp->ki_tdev = NOUDEV; 498 if (p->p_comm[0] != 0) { 499 strncpy(kp->ki_comm, p->p_comm, MAXCOMLEN); 500 kp->ki_comm[MAXCOMLEN] = 0; 501 } 502 kp->ki_siglist = p->p_siglist; 503 kp->ki_sigmask = p->p_sigmask; 504 kp->ki_xstat = p->p_xstat; 505 kp->ki_acflag = p->p_acflag; 506 kp->ki_flag = p->p_flag; 507 /* If jailed(p->p_ucred), emulate the old P_JAILED flag. */ 508 if (jailed(p->p_ucred)) 509 kp->ki_flag |= P_JAILED; 510 kp->ki_lock = p->p_lock; 511 if (p->p_pptr) 512 kp->ki_ppid = p->p_pptr->p_pid; 513 PROC_UNLOCK(p); 514 } 515 516 /* 517 * Locate a zombie process by number 518 */ 519 struct proc * 520 zpfind(pid_t pid) 521 { 522 struct proc *p; 523 524 sx_slock(&allproc_lock); 525 LIST_FOREACH(p, &zombproc, p_list) 526 if (p->p_pid == pid) 527 break; 528 sx_sunlock(&allproc_lock); 529 return (p); 530 } 531 532 533 static int 534 sysctl_out_proc(struct proc *p, struct sysctl_req *req, int doingzomb) 535 { 536 struct kinfo_proc kinfo_proc; 537 int error; 538 pid_t pid = p->p_pid; 539 540 fill_kinfo_proc(p, &kinfo_proc); 541 error = SYSCTL_OUT(req, (caddr_t)&kinfo_proc, sizeof(kinfo_proc)); 542 if (error) 543 return (error); 544 if (!doingzomb && pid && (pfind(pid) != p)) 545 return EAGAIN; 546 if (doingzomb && zpfind(pid) != p) 547 return EAGAIN; 548 return (0); 549 } 550 551 static int 552 sysctl_kern_proc(SYSCTL_HANDLER_ARGS) 553 { 554 int *name = (int*) arg1; 555 u_int namelen = arg2; 556 struct proc *p; 557 int doingzomb; 558 int error = 0; 559 560 if (oidp->oid_number == KERN_PROC_PID) { 561 if (namelen != 1) 562 return (EINVAL); 563 p = pfind((pid_t)name[0]); 564 if (!p) 565 return (0); 566 if (p_can(curproc, p, P_CAN_SEE, NULL)) 567 return (0); 568 error = sysctl_out_proc(p, req, 0); 569 return (error); 570 } 571 if (oidp->oid_number == KERN_PROC_ALL && !namelen) 572 ; 573 else if (oidp->oid_number != KERN_PROC_ALL && namelen == 1) 574 ; 575 else 576 return (EINVAL); 577 578 if (!req->oldptr) { 579 /* overestimate by 5 procs */ 580 error = SYSCTL_OUT(req, 0, sizeof (struct kinfo_proc) * 5); 581 if (error) 582 return (error); 583 } 584 sx_slock(&allproc_lock); 585 for (doingzomb=0 ; doingzomb < 2 ; doingzomb++) { 586 if (!doingzomb) 587 p = LIST_FIRST(&allproc); 588 else 589 p = LIST_FIRST(&zombproc); 590 for (; p != 0; p = LIST_NEXT(p, p_list)) { 591 /* 592 * Show a user only appropriate processes. 593 */ 594 if (p_can(curproc, p, P_CAN_SEE, NULL)) 595 continue; 596 /* 597 * Skip embryonic processes. 598 */ 599 if (p->p_stat == SIDL) 600 continue; 601 /* 602 * TODO - make more efficient (see notes below). 603 * do by session. 604 */ 605 switch (oidp->oid_number) { 606 607 case KERN_PROC_PGRP: 608 /* could do this by traversing pgrp */ 609 if (p->p_pgrp == NULL || 610 p->p_pgrp->pg_id != (pid_t)name[0]) 611 continue; 612 break; 613 614 case KERN_PROC_TTY: 615 if ((p->p_flag & P_CONTROLT) == 0 || 616 p->p_session == NULL || 617 p->p_session->s_ttyp == NULL || 618 dev2udev(p->p_session->s_ttyp->t_dev) != 619 (udev_t)name[0]) 620 continue; 621 break; 622 623 case KERN_PROC_UID: 624 if (p->p_ucred == NULL || 625 p->p_ucred->cr_uid != (uid_t)name[0]) 626 continue; 627 break; 628 629 case KERN_PROC_RUID: 630 if (p->p_ucred == NULL || 631 p->p_cred->p_ruid != (uid_t)name[0]) 632 continue; 633 break; 634 } 635 636 if (p_can(curproc, p, P_CAN_SEE, NULL)) 637 continue; 638 639 error = sysctl_out_proc(p, req, doingzomb); 640 if (error) { 641 sx_sunlock(&allproc_lock); 642 return (error); 643 } 644 } 645 } 646 sx_sunlock(&allproc_lock); 647 return (0); 648 } 649 650 /* 651 * This sysctl allows a process to retrieve the argument list or process 652 * title for another process without groping around in the address space 653 * of the other process. It also allow a process to set its own "process 654 * title to a string of its own choice. 655 */ 656 static int 657 sysctl_kern_proc_args(SYSCTL_HANDLER_ARGS) 658 { 659 int *name = (int*) arg1; 660 u_int namelen = arg2; 661 struct proc *p; 662 struct pargs *pa; 663 int error = 0; 664 665 if (namelen != 1) 666 return (EINVAL); 667 668 p = pfind((pid_t)name[0]); 669 if (!p) 670 return (0); 671 672 if ((!ps_argsopen) && p_can(curproc, p, P_CAN_SEE, NULL)) 673 return (0); 674 675 if (req->newptr && curproc != p) 676 return (EPERM); 677 678 if (req->oldptr && p->p_args != NULL) 679 error = SYSCTL_OUT(req, p->p_args->ar_args, p->p_args->ar_length); 680 if (req->newptr == NULL) 681 return (error); 682 683 if (p->p_args && --p->p_args->ar_ref == 0) 684 FREE(p->p_args, M_PARGS); 685 PROC_LOCK(p); 686 p->p_args = NULL; 687 PROC_UNLOCK(p); 688 689 if (req->newlen + sizeof(struct pargs) > ps_arg_cache_limit) 690 return (error); 691 692 MALLOC(pa, struct pargs *, sizeof(struct pargs) + req->newlen, 693 M_PARGS, M_WAITOK); 694 pa->ar_ref = 1; 695 pa->ar_length = req->newlen; 696 error = SYSCTL_IN(req, pa->ar_args, req->newlen); 697 if (!error) { 698 PROC_LOCK(p); 699 p->p_args = pa; 700 PROC_UNLOCK(p); 701 } else 702 FREE(pa, M_PARGS); 703 return (error); 704 } 705 706 SYSCTL_NODE(_kern, KERN_PROC, proc, CTLFLAG_RD, 0, "Process table"); 707 708 SYSCTL_PROC(_kern_proc, KERN_PROC_ALL, all, CTLFLAG_RD|CTLTYPE_STRUCT, 709 0, 0, sysctl_kern_proc, "S,proc", "Return entire process table"); 710 711 SYSCTL_NODE(_kern_proc, KERN_PROC_PGRP, pgrp, CTLFLAG_RD, 712 sysctl_kern_proc, "Process table"); 713 714 SYSCTL_NODE(_kern_proc, KERN_PROC_TTY, tty, CTLFLAG_RD, 715 sysctl_kern_proc, "Process table"); 716 717 SYSCTL_NODE(_kern_proc, KERN_PROC_UID, uid, CTLFLAG_RD, 718 sysctl_kern_proc, "Process table"); 719 720 SYSCTL_NODE(_kern_proc, KERN_PROC_RUID, ruid, CTLFLAG_RD, 721 sysctl_kern_proc, "Process table"); 722 723 SYSCTL_NODE(_kern_proc, KERN_PROC_PID, pid, CTLFLAG_RD, 724 sysctl_kern_proc, "Process table"); 725 726 SYSCTL_NODE(_kern_proc, KERN_PROC_ARGS, args, CTLFLAG_RW | CTLFLAG_ANYBODY, 727 sysctl_kern_proc_args, "Process argument list"); 728