| 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: src/sys/kern/kern_proc.c,v 1.63.2.9 2003/05/08 07:47:16 kbyanc Exp $ |
| 35 | * $DragonFly: src/sys/kern/kern_proc.c,v 1.18 2005/02/01 02:25:45 joerg Exp $ |
| 36 | */ |
| 37 | |
| 38 | #include <sys/param.h> |
| 39 | #include <sys/systm.h> |
| 40 | #include <sys/kernel.h> |
| 41 | #include <sys/sysctl.h> |
| 42 | #include <sys/malloc.h> |
| 43 | #include <sys/proc.h> |
| 44 | #include <sys/jail.h> |
| 45 | #include <sys/filedesc.h> |
| 46 | #include <sys/tty.h> |
| 47 | #include <sys/signalvar.h> |
| 48 | #include <vm/vm.h> |
| 49 | #include <sys/lock.h> |
| 50 | #include <vm/pmap.h> |
| 51 | #include <vm/vm_map.h> |
| 52 | #include <sys/user.h> |
| 53 | #include <vm/vm_zone.h> |
| 54 | #include <machine/smp.h> |
| 55 | |
| 56 | static MALLOC_DEFINE(M_PGRP, "pgrp", "process group header"); |
| 57 | MALLOC_DEFINE(M_SESSION, "session", "session header"); |
| 58 | static MALLOC_DEFINE(M_PROC, "proc", "Proc structures"); |
| 59 | MALLOC_DEFINE(M_SUBPROC, "subproc", "Proc sub-structures"); |
| 60 | |
| 61 | int ps_showallprocs = 1; |
| 62 | static int ps_showallthreads = 1; |
| 63 | SYSCTL_INT(_kern, OID_AUTO, ps_showallprocs, CTLFLAG_RW, |
| 64 | &ps_showallprocs, 0, ""); |
| 65 | SYSCTL_INT(_kern, OID_AUTO, ps_showallthreads, CTLFLAG_RW, |
| 66 | &ps_showallthreads, 0, ""); |
| 67 | |
| 68 | static void pgdelete (struct pgrp *); |
| 69 | |
| 70 | static void orphanpg (struct pgrp *pg); |
| 71 | |
| 72 | /* |
| 73 | * Other process lists |
| 74 | */ |
| 75 | struct pidhashhead *pidhashtbl; |
| 76 | u_long pidhash; |
| 77 | struct pgrphashhead *pgrphashtbl; |
| 78 | u_long pgrphash; |
| 79 | struct proclist allproc; |
| 80 | struct proclist zombproc; |
| 81 | vm_zone_t proc_zone; |
| 82 | vm_zone_t thread_zone; |
| 83 | |
| 84 | /* |
| 85 | * Initialize global process hashing structures. |
| 86 | */ |
| 87 | void |
| 88 | procinit() |
| 89 | { |
| 90 | |
| 91 | LIST_INIT(&allproc); |
| 92 | LIST_INIT(&zombproc); |
| 93 | pidhashtbl = hashinit(maxproc / 4, M_PROC, &pidhash); |
| 94 | pgrphashtbl = hashinit(maxproc / 4, M_PROC, &pgrphash); |
| 95 | proc_zone = zinit("PROC", sizeof (struct proc), 0, 0, 5); |
| 96 | thread_zone = zinit("THREAD", sizeof (struct thread), 0, 0, 5); |
| 97 | uihashinit(); |
| 98 | } |
| 99 | |
| 100 | /* |
| 101 | * Is p an inferior of the current process? |
| 102 | */ |
| 103 | int |
| 104 | inferior(p) |
| 105 | struct proc *p; |
| 106 | { |
| 107 | |
| 108 | for (; p != curproc; p = p->p_pptr) |
| 109 | if (p->p_pid == 0) |
| 110 | return (0); |
| 111 | return (1); |
| 112 | } |
| 113 | |
| 114 | /* |
| 115 | * Locate a process by number |
| 116 | */ |
| 117 | struct proc * |
| 118 | pfind(pid) |
| 119 | pid_t pid; |
| 120 | { |
| 121 | struct proc *p; |
| 122 | |
| 123 | LIST_FOREACH(p, PIDHASH(pid), p_hash) |
| 124 | if (p->p_pid == pid) |
| 125 | return (p); |
| 126 | return (NULL); |
| 127 | } |
| 128 | |
| 129 | /* |
| 130 | * Locate a process group by number |
| 131 | */ |
| 132 | struct pgrp * |
| 133 | pgfind(pgid) |
| 134 | pid_t pgid; |
| 135 | { |
| 136 | struct pgrp *pgrp; |
| 137 | |
| 138 | LIST_FOREACH(pgrp, PGRPHASH(pgid), pg_hash) |
| 139 | if (pgrp->pg_id == pgid) |
| 140 | return (pgrp); |
| 141 | return (NULL); |
| 142 | } |
| 143 | |
| 144 | /* |
| 145 | * Move p to a new or existing process group (and session) |
| 146 | */ |
| 147 | int |
| 148 | enterpgrp(p, pgid, mksess) |
| 149 | struct proc *p; |
| 150 | pid_t pgid; |
| 151 | int mksess; |
| 152 | { |
| 153 | struct pgrp *pgrp = pgfind(pgid); |
| 154 | |
| 155 | KASSERT(pgrp == NULL || !mksess, |
| 156 | ("enterpgrp: setsid into non-empty pgrp")); |
| 157 | KASSERT(!SESS_LEADER(p), |
| 158 | ("enterpgrp: session leader attempted setpgrp")); |
| 159 | |
| 160 | if (pgrp == NULL) { |
| 161 | pid_t savepid = p->p_pid; |
| 162 | struct proc *np; |
| 163 | /* |
| 164 | * new process group |
| 165 | */ |
| 166 | KASSERT(p->p_pid == pgid, |
| 167 | ("enterpgrp: new pgrp and pid != pgid")); |
| 168 | if ((np = pfind(savepid)) == NULL || np != p) |
| 169 | return (ESRCH); |
| 170 | MALLOC(pgrp, struct pgrp *, sizeof(struct pgrp), M_PGRP, |
| 171 | M_WAITOK); |
| 172 | if (mksess) { |
| 173 | struct session *sess; |
| 174 | |
| 175 | /* |
| 176 | * new session |
| 177 | */ |
| 178 | MALLOC(sess, struct session *, sizeof(struct session), |
| 179 | M_SESSION, M_WAITOK); |
| 180 | sess->s_leader = p; |
| 181 | sess->s_sid = p->p_pid; |
| 182 | sess->s_count = 1; |
| 183 | sess->s_ttyvp = NULL; |
| 184 | sess->s_ttyp = NULL; |
| 185 | bcopy(p->p_session->s_login, sess->s_login, |
| 186 | sizeof(sess->s_login)); |
| 187 | p->p_flag &= ~P_CONTROLT; |
| 188 | pgrp->pg_session = sess; |
| 189 | KASSERT(p == curproc, |
| 190 | ("enterpgrp: mksession and p != curproc")); |
| 191 | } else { |
| 192 | pgrp->pg_session = p->p_session; |
| 193 | sess_hold(pgrp->pg_session); |
| 194 | } |
| 195 | pgrp->pg_id = pgid; |
| 196 | LIST_INIT(&pgrp->pg_members); |
| 197 | LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash); |
| 198 | pgrp->pg_jobc = 0; |
| 199 | SLIST_INIT(&pgrp->pg_sigiolst); |
| 200 | } else if (pgrp == p->p_pgrp) |
| 201 | return (0); |
| 202 | |
| 203 | /* |
| 204 | * Adjust eligibility of affected pgrps to participate in job control. |
| 205 | * Increment eligibility counts before decrementing, otherwise we |
| 206 | * could reach 0 spuriously during the first call. |
| 207 | */ |
| 208 | fixjobc(p, pgrp, 1); |
| 209 | fixjobc(p, p->p_pgrp, 0); |
| 210 | |
| 211 | LIST_REMOVE(p, p_pglist); |
| 212 | if (LIST_EMPTY(&p->p_pgrp->pg_members)) |
| 213 | pgdelete(p->p_pgrp); |
| 214 | p->p_pgrp = pgrp; |
| 215 | LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist); |
| 216 | return (0); |
| 217 | } |
| 218 | |
| 219 | /* |
| 220 | * remove process from process group |
| 221 | */ |
| 222 | int |
| 223 | leavepgrp(p) |
| 224 | struct proc *p; |
| 225 | { |
| 226 | |
| 227 | LIST_REMOVE(p, p_pglist); |
| 228 | if (LIST_EMPTY(&p->p_pgrp->pg_members)) |
| 229 | pgdelete(p->p_pgrp); |
| 230 | p->p_pgrp = 0; |
| 231 | return (0); |
| 232 | } |
| 233 | |
| 234 | /* |
| 235 | * delete a process group |
| 236 | */ |
| 237 | static void |
| 238 | pgdelete(pgrp) |
| 239 | struct pgrp *pgrp; |
| 240 | { |
| 241 | |
| 242 | /* |
| 243 | * Reset any sigio structures pointing to us as a result of |
| 244 | * F_SETOWN with our pgid. |
| 245 | */ |
| 246 | funsetownlst(&pgrp->pg_sigiolst); |
| 247 | |
| 248 | if (pgrp->pg_session->s_ttyp != NULL && |
| 249 | pgrp->pg_session->s_ttyp->t_pgrp == pgrp) |
| 250 | pgrp->pg_session->s_ttyp->t_pgrp = NULL; |
| 251 | LIST_REMOVE(pgrp, pg_hash); |
| 252 | sess_rele(pgrp->pg_session); |
| 253 | free(pgrp, M_PGRP); |
| 254 | } |
| 255 | |
| 256 | /* |
| 257 | * Adjust the ref count on a session structure. When the ref count falls to |
| 258 | * zero the tty is disassociated from the session and the session structure |
| 259 | * is freed. Note that tty assocation is not itself ref-counted. |
| 260 | */ |
| 261 | void |
| 262 | sess_hold(struct session *sp) |
| 263 | { |
| 264 | ++sp->s_count; |
| 265 | } |
| 266 | |
| 267 | void |
| 268 | sess_rele(struct session *sp) |
| 269 | { |
| 270 | KKASSERT(sp->s_count > 0); |
| 271 | if (--sp->s_count == 0) { |
| 272 | if (sp->s_ttyp && sp->s_ttyp->t_session) { |
| 273 | #ifdef TTY_DO_FULL_CLOSE |
| 274 | /* FULL CLOSE, see ttyclearsession() */ |
| 275 | KKASSERT(sp->s_ttyp->t_session == sp); |
| 276 | sp->s_ttyp->t_session = NULL; |
| 277 | #else |
| 278 | /* HALF CLOSE, see ttyclearsession() */ |
| 279 | if (sp->s_ttyp->t_session == sp) |
| 280 | sp->s_ttyp->t_session = NULL; |
| 281 | #endif |
| 282 | } |
| 283 | free(sp, M_SESSION); |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | /* |
| 288 | * Adjust pgrp jobc counters when specified process changes process group. |
| 289 | * We count the number of processes in each process group that "qualify" |
| 290 | * the group for terminal job control (those with a parent in a different |
| 291 | * process group of the same session). If that count reaches zero, the |
| 292 | * process group becomes orphaned. Check both the specified process' |
| 293 | * process group and that of its children. |
| 294 | * entering == 0 => p is leaving specified group. |
| 295 | * entering == 1 => p is entering specified group. |
| 296 | */ |
| 297 | void |
| 298 | fixjobc(p, pgrp, entering) |
| 299 | struct proc *p; |
| 300 | struct pgrp *pgrp; |
| 301 | int entering; |
| 302 | { |
| 303 | struct pgrp *hispgrp; |
| 304 | struct session *mysession = pgrp->pg_session; |
| 305 | |
| 306 | /* |
| 307 | * Check p's parent to see whether p qualifies its own process |
| 308 | * group; if so, adjust count for p's process group. |
| 309 | */ |
| 310 | if ((hispgrp = p->p_pptr->p_pgrp) != pgrp && |
| 311 | hispgrp->pg_session == mysession) { |
| 312 | if (entering) |
| 313 | pgrp->pg_jobc++; |
| 314 | else if (--pgrp->pg_jobc == 0) |
| 315 | orphanpg(pgrp); |
| 316 | } |
| 317 | |
| 318 | /* |
| 319 | * Check this process' children to see whether they qualify |
| 320 | * their process groups; if so, adjust counts for children's |
| 321 | * process groups. |
| 322 | */ |
| 323 | LIST_FOREACH(p, &p->p_children, p_sibling) |
| 324 | if ((hispgrp = p->p_pgrp) != pgrp && |
| 325 | hispgrp->pg_session == mysession && |
| 326 | p->p_stat != SZOMB) { |
| 327 | if (entering) |
| 328 | hispgrp->pg_jobc++; |
| 329 | else if (--hispgrp->pg_jobc == 0) |
| 330 | orphanpg(hispgrp); |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | /* |
| 335 | * A process group has become orphaned; |
| 336 | * if there are any stopped processes in the group, |
| 337 | * hang-up all process in that group. |
| 338 | */ |
| 339 | static void |
| 340 | orphanpg(pg) |
| 341 | struct pgrp *pg; |
| 342 | { |
| 343 | struct proc *p; |
| 344 | |
| 345 | LIST_FOREACH(p, &pg->pg_members, p_pglist) { |
| 346 | if (p->p_stat == SSTOP) { |
| 347 | LIST_FOREACH(p, &pg->pg_members, p_pglist) { |
| 348 | psignal(p, SIGHUP); |
| 349 | psignal(p, SIGCONT); |
| 350 | } |
| 351 | return; |
| 352 | } |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | #include "opt_ddb.h" |
| 357 | #ifdef DDB |
| 358 | #include <ddb/ddb.h> |
| 359 | |
| 360 | DB_SHOW_COMMAND(pgrpdump, pgrpdump) |
| 361 | { |
| 362 | struct pgrp *pgrp; |
| 363 | struct proc *p; |
| 364 | int i; |
| 365 | |
| 366 | for (i = 0; i <= pgrphash; i++) { |
| 367 | if (!LIST_EMPTY(&pgrphashtbl[i])) { |
| 368 | printf("\tindx %d\n", i); |
| 369 | LIST_FOREACH(pgrp, &pgrphashtbl[i], pg_hash) { |
| 370 | printf( |
| 371 | "\tpgrp %p, pgid %ld, sess %p, sesscnt %d, mem %p\n", |
| 372 | (void *)pgrp, (long)pgrp->pg_id, |
| 373 | (void *)pgrp->pg_session, |
| 374 | pgrp->pg_session->s_count, |
| 375 | (void *)LIST_FIRST(&pgrp->pg_members)); |
| 376 | LIST_FOREACH(p, &pgrp->pg_members, p_pglist) { |
| 377 | printf("\t\tpid %ld addr %p pgrp %p\n", |
| 378 | (long)p->p_pid, (void *)p, |
| 379 | (void *)p->p_pgrp); |
| 380 | } |
| 381 | } |
| 382 | } |
| 383 | } |
| 384 | } |
| 385 | #endif /* DDB */ |
| 386 | |
| 387 | /* |
| 388 | * Fill in an eproc structure for the specified thread. |
| 389 | */ |
| 390 | void |
| 391 | fill_eproc_td(thread_t td, struct eproc *ep, struct proc *xp) |
| 392 | { |
| 393 | bzero(ep, sizeof(*ep)); |
| 394 | |
| 395 | ep->e_uticks = td->td_uticks; |
| 396 | ep->e_sticks = td->td_sticks; |
| 397 | ep->e_iticks = td->td_iticks; |
| 398 | ep->e_tdev = NOUDEV; |
| 399 | ep->e_cpuid = td->td_gd->gd_cpuid; |
| 400 | if (td->td_wmesg) { |
| 401 | strncpy(ep->e_wmesg, td->td_wmesg, WMESGLEN); |
| 402 | ep->e_wmesg[WMESGLEN] = 0; |
| 403 | } |
| 404 | |
| 405 | /* |
| 406 | * Fake up portions of the proc structure copied out by the sysctl |
| 407 | * to return useful information. Note that using td_pri directly |
| 408 | * is messy because it includes critial section data so we fake |
| 409 | * up an rtprio.prio for threads. |
| 410 | */ |
| 411 | if (xp) { |
| 412 | *xp = *initproc; |
| 413 | xp->p_rtprio.type = RTP_PRIO_THREAD; |
| 414 | xp->p_rtprio.prio = td->td_pri & TDPRI_MASK; |
| 415 | xp->p_pid = -1; |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | /* |
| 420 | * Fill in an eproc structure for the specified process. |
| 421 | */ |
| 422 | void |
| 423 | fill_eproc(struct proc *p, struct eproc *ep) |
| 424 | { |
| 425 | struct tty *tp; |
| 426 | |
| 427 | fill_eproc_td(p->p_thread, ep, NULL); |
| 428 | |
| 429 | ep->e_paddr = p; |
| 430 | if (p->p_ucred) { |
| 431 | ep->e_ucred = *p->p_ucred; |
| 432 | } |
| 433 | if (p->p_procsig) { |
| 434 | ep->e_procsig = *p->p_procsig; |
| 435 | } |
| 436 | if (p->p_stat != SIDL && p->p_stat != SZOMB && p->p_vmspace != NULL) { |
| 437 | struct vmspace *vm = p->p_vmspace; |
| 438 | ep->e_vm = *vm; |
| 439 | ep->e_vm.vm_rssize = vmspace_resident_count(vm); /*XXX*/ |
| 440 | } |
| 441 | if ((p->p_flag & P_INMEM) && p->p_stats) |
| 442 | ep->e_stats = *p->p_stats; |
| 443 | if (p->p_pptr) |
| 444 | ep->e_ppid = p->p_pptr->p_pid; |
| 445 | if (p->p_pgrp) { |
| 446 | ep->e_pgid = p->p_pgrp->pg_id; |
| 447 | ep->e_jobc = p->p_pgrp->pg_jobc; |
| 448 | ep->e_sess = p->p_pgrp->pg_session; |
| 449 | |
| 450 | if (ep->e_sess) { |
| 451 | bcopy(ep->e_sess->s_login, ep->e_login, sizeof(ep->e_login)); |
| 452 | if (ep->e_sess->s_ttyvp) |
| 453 | ep->e_flag = EPROC_CTTY; |
| 454 | if (p->p_session && SESS_LEADER(p)) |
| 455 | ep->e_flag |= EPROC_SLEADER; |
| 456 | } |
| 457 | } |
| 458 | if ((p->p_flag & P_CONTROLT) && |
| 459 | (ep->e_sess != NULL) && |
| 460 | ((tp = ep->e_sess->s_ttyp) != NULL)) { |
| 461 | ep->e_tdev = dev2udev(tp->t_dev); |
| 462 | ep->e_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID; |
| 463 | ep->e_tsess = tp->t_session; |
| 464 | } else { |
| 465 | ep->e_tdev = NOUDEV; |
| 466 | } |
| 467 | } |
| 468 | |
| 469 | struct proc * |
| 470 | zpfind(pid_t pid) |
| 471 | { |
| 472 | struct proc *p; |
| 473 | |
| 474 | LIST_FOREACH(p, &zombproc, p_list) |
| 475 | if (p->p_pid == pid) |
| 476 | return (p); |
| 477 | return (NULL); |
| 478 | } |
| 479 | |
| 480 | static int |
| 481 | sysctl_out_proc(struct proc *p, struct thread *td, struct sysctl_req *req, int doingzomb) |
| 482 | { |
| 483 | struct eproc eproc; |
| 484 | struct proc xproc; |
| 485 | int error; |
| 486 | #if 0 |
| 487 | pid_t pid = p->p_pid; |
| 488 | #endif |
| 489 | |
| 490 | if (p) { |
| 491 | td = p->p_thread; |
| 492 | fill_eproc(p, &eproc); |
| 493 | xproc = *p; |
| 494 | |
| 495 | /* |
| 496 | * Fixup p_stat from SRUN to SSLEEP if the LWKT thread is |
| 497 | * in a thread-blocked state. |
| 498 | * |
| 499 | * XXX temporary fix which might become permanent (I'd rather |
| 500 | * not pollute the thread scheduler with knowlege about |
| 501 | * processes). |
| 502 | */ |
| 503 | if (p->p_stat == SRUN && td && (td->td_flags & TDF_BLOCKED)) { |
| 504 | xproc.p_stat = SSLEEP; |
| 505 | } |
| 506 | } else if (td) { |
| 507 | fill_eproc_td(td, &eproc, &xproc); |
| 508 | } |
| 509 | error = SYSCTL_OUT(req,(caddr_t)&xproc, sizeof(struct proc)); |
| 510 | if (error) |
| 511 | return (error); |
| 512 | error = SYSCTL_OUT(req,(caddr_t)&eproc, sizeof(eproc)); |
| 513 | if (error) |
| 514 | return (error); |
| 515 | error = SYSCTL_OUT(req,(caddr_t)td, sizeof(struct thread)); |
| 516 | if (error) |
| 517 | return (error); |
| 518 | #if 0 |
| 519 | if (!doingzomb && pid && (pfind(pid) != p)) |
| 520 | return EAGAIN; |
| 521 | if (doingzomb && zpfind(pid) != p) |
| 522 | return EAGAIN; |
| 523 | #endif |
| 524 | return (0); |
| 525 | } |
| 526 | |
| 527 | static int |
| 528 | sysctl_kern_proc(SYSCTL_HANDLER_ARGS) |
| 529 | { |
| 530 | int *name = (int*) arg1; |
| 531 | u_int namelen = arg2; |
| 532 | struct proc *p; |
| 533 | struct thread *td; |
| 534 | int doingzomb; |
| 535 | int error = 0; |
| 536 | int n; |
| 537 | int origcpu; |
| 538 | struct ucred *cr1 = curproc->p_ucred; |
| 539 | |
| 540 | if (oidp->oid_number == KERN_PROC_PID) { |
| 541 | if (namelen != 1) |
| 542 | return (EINVAL); |
| 543 | p = pfind((pid_t)name[0]); |
| 544 | if (!p) |
| 545 | return (0); |
| 546 | if (!PRISON_CHECK(cr1, p->p_ucred)) |
| 547 | return (0); |
| 548 | error = sysctl_out_proc(p, NULL, req, 0); |
| 549 | return (error); |
| 550 | } |
| 551 | if (oidp->oid_number == KERN_PROC_ALL && !namelen) |
| 552 | ; |
| 553 | else if (oidp->oid_number != KERN_PROC_ALL && namelen == 1) |
| 554 | ; |
| 555 | else |
| 556 | return (EINVAL); |
| 557 | |
| 558 | if (!req->oldptr) { |
| 559 | /* overestimate by 5 procs */ |
| 560 | error = SYSCTL_OUT(req, 0, sizeof (struct kinfo_proc) * 5); |
| 561 | if (error) |
| 562 | return (error); |
| 563 | } |
| 564 | for (doingzomb=0 ; doingzomb < 2 ; doingzomb++) { |
| 565 | if (!doingzomb) |
| 566 | p = LIST_FIRST(&allproc); |
| 567 | else |
| 568 | p = LIST_FIRST(&zombproc); |
| 569 | for (; p != 0; p = LIST_NEXT(p, p_list)) { |
| 570 | /* |
| 571 | * Show a user only their processes. |
| 572 | */ |
| 573 | if ((!ps_showallprocs) && p_trespass(cr1, p->p_ucred)) |
| 574 | continue; |
| 575 | /* |
| 576 | * Skip embryonic processes. |
| 577 | */ |
| 578 | if (p->p_stat == SIDL) |
| 579 | continue; |
| 580 | /* |
| 581 | * TODO - make more efficient (see notes below). |
| 582 | * do by session. |
| 583 | */ |
| 584 | switch (oidp->oid_number) { |
| 585 | case KERN_PROC_PGRP: |
| 586 | /* could do this by traversing pgrp */ |
| 587 | if (p->p_pgrp == NULL || |
| 588 | p->p_pgrp->pg_id != (pid_t)name[0]) |
| 589 | continue; |
| 590 | break; |
| 591 | |
| 592 | case KERN_PROC_TTY: |
| 593 | if ((p->p_flag & P_CONTROLT) == 0 || |
| 594 | p->p_session == NULL || |
| 595 | p->p_session->s_ttyp == NULL || |
| 596 | dev2udev(p->p_session->s_ttyp->t_dev) != |
| 597 | (udev_t)name[0]) |
| 598 | continue; |
| 599 | break; |
| 600 | |
| 601 | case KERN_PROC_UID: |
| 602 | if (p->p_ucred == NULL || |
| 603 | p->p_ucred->cr_uid != (uid_t)name[0]) |
| 604 | continue; |
| 605 | break; |
| 606 | |
| 607 | case KERN_PROC_RUID: |
| 608 | if (p->p_ucred == NULL || |
| 609 | p->p_ucred->cr_ruid != (uid_t)name[0]) |
| 610 | continue; |
| 611 | break; |
| 612 | } |
| 613 | |
| 614 | if (!PRISON_CHECK(cr1, p->p_ucred)) |
| 615 | continue; |
| 616 | PHOLD(p); |
| 617 | error = sysctl_out_proc(p, NULL, req, doingzomb); |
| 618 | PRELE(p); |
| 619 | if (error) |
| 620 | return (error); |
| 621 | } |
| 622 | } |
| 623 | |
| 624 | /* |
| 625 | * Iterate over all active cpus and scan their thread list. Start |
| 626 | * with the next logical cpu and end with our original cpu. We |
| 627 | * migrate our own thread to each target cpu in order to safely scan |
| 628 | * its thread list. In the last loop we migrate back to our original |
| 629 | * cpu. |
| 630 | */ |
| 631 | origcpu = mycpu->gd_cpuid; |
| 632 | if (!ps_showallthreads || jailed(cr1)) |
| 633 | goto post_threads; |
| 634 | for (n = 1; n <= ncpus; ++n) { |
| 635 | globaldata_t rgd; |
| 636 | int nid; |
| 637 | |
| 638 | nid = (origcpu + n) % ncpus; |
| 639 | if ((smp_active_mask & (1 << nid)) == 0) |
| 640 | continue; |
| 641 | rgd = globaldata_find(nid); |
| 642 | lwkt_setcpu_self(rgd); |
| 643 | cpu_mb1(); /* CURRENT CPU HAS CHANGED */ |
| 644 | |
| 645 | TAILQ_FOREACH(td, &mycpu->gd_tdallq, td_allq) { |
| 646 | if (td->td_proc) |
| 647 | continue; |
| 648 | switch (oidp->oid_number) { |
| 649 | case KERN_PROC_PGRP: |
| 650 | case KERN_PROC_TTY: |
| 651 | case KERN_PROC_UID: |
| 652 | case KERN_PROC_RUID: |
| 653 | continue; |
| 654 | default: |
| 655 | break; |
| 656 | } |
| 657 | lwkt_hold(td); |
| 658 | error = sysctl_out_proc(NULL, td, req, doingzomb); |
| 659 | lwkt_rele(td); |
| 660 | if (error) |
| 661 | return (error); |
| 662 | } |
| 663 | } |
| 664 | post_threads: |
| 665 | return (0); |
| 666 | } |
| 667 | |
| 668 | /* |
| 669 | * This sysctl allows a process to retrieve the argument list or process |
| 670 | * title for another process without groping around in the address space |
| 671 | * of the other process. It also allow a process to set its own "process |
| 672 | * title to a string of its own choice. |
| 673 | */ |
| 674 | static int |
| 675 | sysctl_kern_proc_args(SYSCTL_HANDLER_ARGS) |
| 676 | { |
| 677 | int *name = (int*) arg1; |
| 678 | u_int namelen = arg2; |
| 679 | struct proc *p; |
| 680 | struct pargs *pa; |
| 681 | int error = 0; |
| 682 | struct ucred *cr1 = curproc->p_ucred; |
| 683 | |
| 684 | if (namelen != 1) |
| 685 | return (EINVAL); |
| 686 | |
| 687 | p = pfind((pid_t)name[0]); |
| 688 | if (!p) |
| 689 | return (0); |
| 690 | |
| 691 | if ((!ps_argsopen) && p_trespass(cr1, p->p_ucred)) |
| 692 | return (0); |
| 693 | |
| 694 | if (req->newptr && curproc != p) |
| 695 | return (EPERM); |
| 696 | |
| 697 | if (req->oldptr && p->p_args != NULL) |
| 698 | error = SYSCTL_OUT(req, p->p_args->ar_args, p->p_args->ar_length); |
| 699 | if (req->newptr == NULL) |
| 700 | return (error); |
| 701 | |
| 702 | if (p->p_args && --p->p_args->ar_ref == 0) |
| 703 | FREE(p->p_args, M_PARGS); |
| 704 | p->p_args = NULL; |
| 705 | |
| 706 | if (req->newlen + sizeof(struct pargs) > ps_arg_cache_limit) |
| 707 | return (error); |
| 708 | |
| 709 | MALLOC(pa, struct pargs *, sizeof(struct pargs) + req->newlen, |
| 710 | M_PARGS, M_WAITOK); |
| 711 | pa->ar_ref = 1; |
| 712 | pa->ar_length = req->newlen; |
| 713 | error = SYSCTL_IN(req, pa->ar_args, req->newlen); |
| 714 | if (!error) |
| 715 | p->p_args = pa; |
| 716 | else |
| 717 | FREE(pa, M_PARGS); |
| 718 | return (error); |
| 719 | } |
| 720 | |
| 721 | SYSCTL_NODE(_kern, KERN_PROC, proc, CTLFLAG_RD, 0, "Process table"); |
| 722 | |
| 723 | SYSCTL_PROC(_kern_proc, KERN_PROC_ALL, all, CTLFLAG_RD|CTLTYPE_STRUCT, |
| 724 | 0, 0, sysctl_kern_proc, "S,proc", "Return entire process table"); |
| 725 | |
| 726 | SYSCTL_NODE(_kern_proc, KERN_PROC_PGRP, pgrp, CTLFLAG_RD, |
| 727 | sysctl_kern_proc, "Process table"); |
| 728 | |
| 729 | SYSCTL_NODE(_kern_proc, KERN_PROC_TTY, tty, CTLFLAG_RD, |
| 730 | sysctl_kern_proc, "Process table"); |
| 731 | |
| 732 | SYSCTL_NODE(_kern_proc, KERN_PROC_UID, uid, CTLFLAG_RD, |
| 733 | sysctl_kern_proc, "Process table"); |
| 734 | |
| 735 | SYSCTL_NODE(_kern_proc, KERN_PROC_RUID, ruid, CTLFLAG_RD, |
| 736 | sysctl_kern_proc, "Process table"); |
| 737 | |
| 738 | SYSCTL_NODE(_kern_proc, KERN_PROC_PID, pid, CTLFLAG_RD, |
| 739 | sysctl_kern_proc, "Process table"); |
| 740 | |
| 741 | SYSCTL_NODE(_kern_proc, KERN_PROC_ARGS, args, CTLFLAG_RW | CTLFLAG_ANYBODY, |
| 742 | sysctl_kern_proc_args, "Process argument list"); |