1 /* $NetBSD: procfs_vnops.c,v 1.192 2014/09/05 09:26:16 matt Exp $ */ 2 3 /*- 4 * Copyright (c) 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 Andrew Doran. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 /* 33 * Copyright (c) 1993, 1995 34 * The Regents of the University of California. All rights reserved. 35 * 36 * This code is derived from software contributed to Berkeley by 37 * Jan-Simon Pendry. 38 * 39 * Redistribution and use in source and binary forms, with or without 40 * modification, are permitted provided that the following conditions 41 * are met: 42 * 1. Redistributions of source code must retain the above copyright 43 * notice, this list of conditions and the following disclaimer. 44 * 2. Redistributions in binary form must reproduce the above copyright 45 * notice, this list of conditions and the following disclaimer in the 46 * documentation and/or other materials provided with the distribution. 47 * 3. Neither the name of the University nor the names of its contributors 48 * may be used to endorse or promote products derived from this software 49 * without specific prior written permission. 50 * 51 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 52 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 53 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 54 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 55 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 56 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 57 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 58 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 59 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 60 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 61 * SUCH DAMAGE. 62 * 63 * @(#)procfs_vnops.c 8.18 (Berkeley) 5/21/95 64 */ 65 66 /* 67 * Copyright (c) 1993 Jan-Simon Pendry 68 * 69 * This code is derived from software contributed to Berkeley by 70 * Jan-Simon Pendry. 71 * 72 * Redistribution and use in source and binary forms, with or without 73 * modification, are permitted provided that the following conditions 74 * are met: 75 * 1. Redistributions of source code must retain the above copyright 76 * notice, this list of conditions and the following disclaimer. 77 * 2. Redistributions in binary form must reproduce the above copyright 78 * notice, this list of conditions and the following disclaimer in the 79 * documentation and/or other materials provided with the distribution. 80 * 3. All advertising materials mentioning features or use of this software 81 * must display the following acknowledgement: 82 * This product includes software developed by the University of 83 * California, Berkeley and its contributors. 84 * 4. Neither the name of the University nor the names of its contributors 85 * may be used to endorse or promote products derived from this software 86 * without specific prior written permission. 87 * 88 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 89 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 90 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 91 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 92 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 93 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 94 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 95 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 96 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 97 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 98 * SUCH DAMAGE. 99 * 100 * @(#)procfs_vnops.c 8.18 (Berkeley) 5/21/95 101 */ 102 103 /* 104 * procfs vnode interface 105 */ 106 107 #include <sys/cdefs.h> 108 __KERNEL_RCSID(0, "$NetBSD: procfs_vnops.c,v 1.192 2014/09/05 09:26:16 matt Exp $"); 109 110 #include <sys/param.h> 111 #include <sys/systm.h> 112 #include <sys/time.h> 113 #include <sys/kernel.h> 114 #include <sys/file.h> 115 #include <sys/filedesc.h> 116 #include <sys/proc.h> 117 #include <sys/vnode.h> 118 #include <sys/namei.h> 119 #include <sys/malloc.h> 120 #include <sys/mount.h> 121 #include <sys/dirent.h> 122 #include <sys/resourcevar.h> 123 #include <sys/stat.h> 124 #include <sys/ptrace.h> 125 #include <sys/kauth.h> 126 127 #include <uvm/uvm_extern.h> /* for PAGE_SIZE */ 128 129 #include <machine/reg.h> 130 131 #include <miscfs/genfs/genfs.h> 132 #include <miscfs/procfs/procfs.h> 133 134 /* 135 * Vnode Operations. 136 * 137 */ 138 139 static int procfs_validfile_linux(struct lwp *, struct mount *); 140 static int procfs_root_readdir_callback(struct proc *, void *); 141 static void procfs_dir(pfstype, struct lwp *, struct proc *, char **, char *, 142 size_t); 143 144 /* 145 * This is a list of the valid names in the 146 * process-specific sub-directories. It is 147 * used in procfs_lookup and procfs_readdir 148 */ 149 static const struct proc_target { 150 u_char pt_type; 151 u_char pt_namlen; 152 const char *pt_name; 153 pfstype pt_pfstype; 154 int (*pt_valid)(struct lwp *, struct mount *); 155 } proc_targets[] = { 156 #define N(s) sizeof(s)-1, s 157 /* name type validp */ 158 { DT_DIR, N("."), PFSproc, NULL }, 159 { DT_DIR, N(".."), PFSroot, NULL }, 160 { DT_DIR, N("fd"), PFSfd, NULL }, 161 { DT_REG, N("file"), PFSfile, procfs_validfile }, 162 { DT_REG, N("mem"), PFSmem, NULL }, 163 { DT_REG, N("regs"), PFSregs, procfs_validregs }, 164 { DT_REG, N("fpregs"), PFSfpregs, procfs_validfpregs }, 165 { DT_REG, N("ctl"), PFSctl, NULL }, 166 { DT_REG, N("stat"), PFSstat, procfs_validfile_linux }, 167 { DT_REG, N("status"), PFSstatus, NULL }, 168 { DT_REG, N("note"), PFSnote, NULL }, 169 { DT_REG, N("notepg"), PFSnotepg, NULL }, 170 { DT_REG, N("map"), PFSmap, procfs_validmap }, 171 { DT_REG, N("maps"), PFSmaps, procfs_validmap }, 172 { DT_REG, N("cmdline"), PFScmdline, NULL }, 173 { DT_REG, N("exe"), PFSexe, procfs_validfile }, 174 { DT_LNK, N("cwd"), PFScwd, NULL }, 175 { DT_LNK, N("root"), PFSchroot, NULL }, 176 { DT_LNK, N("emul"), PFSemul, NULL }, 177 { DT_REG, N("statm"), PFSstatm, procfs_validfile_linux }, 178 { DT_DIR, N("task"), PFStask, procfs_validfile_linux }, 179 #ifdef __HAVE_PROCFS_MACHDEP 180 PROCFS_MACHDEP_NODETYPE_DEFNS 181 #endif 182 #undef N 183 }; 184 static const int nproc_targets = sizeof(proc_targets) / sizeof(proc_targets[0]); 185 186 /* 187 * List of files in the root directory. Note: the validate function will 188 * be called with p == NULL for these ones. 189 */ 190 static const struct proc_target proc_root_targets[] = { 191 #define N(s) sizeof(s)-1, s 192 /* name type validp */ 193 { DT_REG, N("meminfo"), PFSmeminfo, procfs_validfile_linux }, 194 { DT_REG, N("cpuinfo"), PFScpuinfo, procfs_validfile_linux }, 195 { DT_REG, N("uptime"), PFSuptime, procfs_validfile_linux }, 196 { DT_REG, N("mounts"), PFSmounts, procfs_validfile_linux }, 197 { DT_REG, N("devices"), PFSdevices, procfs_validfile_linux }, 198 { DT_REG, N("stat"), PFScpustat, procfs_validfile_linux }, 199 { DT_REG, N("loadavg"), PFSloadavg, procfs_validfile_linux }, 200 { DT_REG, N("version"), PFSversion, procfs_validfile_linux }, 201 #undef N 202 }; 203 static const int nproc_root_targets = 204 sizeof(proc_root_targets) / sizeof(proc_root_targets[0]); 205 206 int procfs_lookup(void *); 207 #define procfs_create genfs_eopnotsupp 208 #define procfs_mknod genfs_eopnotsupp 209 int procfs_open(void *); 210 int procfs_close(void *); 211 int procfs_access(void *); 212 int procfs_getattr(void *); 213 int procfs_setattr(void *); 214 #define procfs_read procfs_rw 215 #define procfs_write procfs_rw 216 #define procfs_fcntl genfs_fcntl 217 #define procfs_ioctl genfs_enoioctl 218 #define procfs_poll genfs_poll 219 #define procfs_revoke genfs_revoke 220 #define procfs_fsync genfs_nullop 221 #define procfs_seek genfs_nullop 222 #define procfs_remove genfs_eopnotsupp 223 int procfs_link(void *); 224 #define procfs_rename genfs_eopnotsupp 225 #define procfs_mkdir genfs_eopnotsupp 226 #define procfs_rmdir genfs_eopnotsupp 227 int procfs_symlink(void *); 228 int procfs_readdir(void *); 229 int procfs_readlink(void *); 230 #define procfs_abortop genfs_abortop 231 int procfs_inactive(void *); 232 int procfs_reclaim(void *); 233 #define procfs_lock genfs_lock 234 #define procfs_unlock genfs_unlock 235 #define procfs_bmap genfs_badop 236 #define procfs_strategy genfs_badop 237 int procfs_print(void *); 238 int procfs_pathconf(void *); 239 #define procfs_islocked genfs_islocked 240 #define procfs_advlock genfs_einval 241 #define procfs_bwrite genfs_eopnotsupp 242 #define procfs_putpages genfs_null_putpages 243 244 static int atoi(const char *, size_t); 245 246 /* 247 * procfs vnode operations. 248 */ 249 int (**procfs_vnodeop_p)(void *); 250 const struct vnodeopv_entry_desc procfs_vnodeop_entries[] = { 251 { &vop_default_desc, vn_default_error }, 252 { &vop_lookup_desc, procfs_lookup }, /* lookup */ 253 { &vop_create_desc, procfs_create }, /* create */ 254 { &vop_mknod_desc, procfs_mknod }, /* mknod */ 255 { &vop_open_desc, procfs_open }, /* open */ 256 { &vop_close_desc, procfs_close }, /* close */ 257 { &vop_access_desc, procfs_access }, /* access */ 258 { &vop_getattr_desc, procfs_getattr }, /* getattr */ 259 { &vop_setattr_desc, procfs_setattr }, /* setattr */ 260 { &vop_read_desc, procfs_read }, /* read */ 261 { &vop_write_desc, procfs_write }, /* write */ 262 { &vop_fallocate_desc, genfs_eopnotsupp }, /* fallocate */ 263 { &vop_fdiscard_desc, genfs_eopnotsupp }, /* fdiscard */ 264 { &vop_fcntl_desc, procfs_fcntl }, /* fcntl */ 265 { &vop_ioctl_desc, procfs_ioctl }, /* ioctl */ 266 { &vop_poll_desc, procfs_poll }, /* poll */ 267 { &vop_revoke_desc, procfs_revoke }, /* revoke */ 268 { &vop_fsync_desc, procfs_fsync }, /* fsync */ 269 { &vop_seek_desc, procfs_seek }, /* seek */ 270 { &vop_remove_desc, procfs_remove }, /* remove */ 271 { &vop_link_desc, procfs_link }, /* link */ 272 { &vop_rename_desc, procfs_rename }, /* rename */ 273 { &vop_mkdir_desc, procfs_mkdir }, /* mkdir */ 274 { &vop_rmdir_desc, procfs_rmdir }, /* rmdir */ 275 { &vop_symlink_desc, procfs_symlink }, /* symlink */ 276 { &vop_readdir_desc, procfs_readdir }, /* readdir */ 277 { &vop_readlink_desc, procfs_readlink }, /* readlink */ 278 { &vop_abortop_desc, procfs_abortop }, /* abortop */ 279 { &vop_inactive_desc, procfs_inactive }, /* inactive */ 280 { &vop_reclaim_desc, procfs_reclaim }, /* reclaim */ 281 { &vop_lock_desc, procfs_lock }, /* lock */ 282 { &vop_unlock_desc, procfs_unlock }, /* unlock */ 283 { &vop_bmap_desc, procfs_bmap }, /* bmap */ 284 { &vop_strategy_desc, procfs_strategy }, /* strategy */ 285 { &vop_print_desc, procfs_print }, /* print */ 286 { &vop_islocked_desc, procfs_islocked }, /* islocked */ 287 { &vop_pathconf_desc, procfs_pathconf }, /* pathconf */ 288 { &vop_advlock_desc, procfs_advlock }, /* advlock */ 289 { &vop_putpages_desc, procfs_putpages }, /* putpages */ 290 { NULL, NULL } 291 }; 292 const struct vnodeopv_desc procfs_vnodeop_opv_desc = 293 { &procfs_vnodeop_p, procfs_vnodeop_entries }; 294 /* 295 * set things up for doing i/o on 296 * the pfsnode (vp). (vp) is locked 297 * on entry, and should be left locked 298 * on exit. 299 * 300 * for procfs we don't need to do anything 301 * in particular for i/o. all that is done 302 * is to support exclusive open on process 303 * memory images. 304 */ 305 int 306 procfs_open(void *v) 307 { 308 struct vop_open_args /* { 309 struct vnode *a_vp; 310 int a_mode; 311 kauth_cred_t a_cred; 312 } */ *ap = v; 313 struct pfsnode *pfs = VTOPFS(ap->a_vp); 314 struct lwp *l1; 315 struct proc *p2; 316 int error; 317 318 if ((error = procfs_proc_lock(pfs->pfs_pid, &p2, ENOENT)) != 0) 319 return error; 320 321 l1 = curlwp; /* tracer */ 322 323 #define M2K(m) (((m) & FREAD) && ((m) & FWRITE) ? \ 324 KAUTH_REQ_PROCESS_PROCFS_RW : \ 325 (m) & FWRITE ? KAUTH_REQ_PROCESS_PROCFS_WRITE : \ 326 KAUTH_REQ_PROCESS_PROCFS_READ) 327 328 mutex_enter(p2->p_lock); 329 error = kauth_authorize_process(l1->l_cred, KAUTH_PROCESS_PROCFS, 330 p2, pfs, KAUTH_ARG(M2K(ap->a_mode)), NULL); 331 mutex_exit(p2->p_lock); 332 if (error) { 333 procfs_proc_unlock(p2); 334 return (error); 335 } 336 337 #undef M2K 338 339 switch (pfs->pfs_type) { 340 case PFSmem: 341 if (((pfs->pfs_flags & FWRITE) && (ap->a_mode & O_EXCL)) || 342 ((pfs->pfs_flags & O_EXCL) && (ap->a_mode & FWRITE))) { 343 error = EBUSY; 344 break; 345 } 346 347 if (!proc_isunder(p2, l1)) { 348 error = EPERM; 349 break; 350 } 351 352 if (ap->a_mode & FWRITE) 353 pfs->pfs_flags = ap->a_mode & (FWRITE|O_EXCL); 354 355 break; 356 357 case PFSregs: 358 case PFSfpregs: 359 if (!proc_isunder(p2, l1)) { 360 error = EPERM; 361 break; 362 } 363 break; 364 365 default: 366 break; 367 } 368 369 procfs_proc_unlock(p2); 370 return (error); 371 } 372 373 /* 374 * close the pfsnode (vp) after doing i/o. 375 * (vp) is not locked on entry or exit. 376 * 377 * nothing to do for procfs other than undo 378 * any exclusive open flag (see _open above). 379 */ 380 int 381 procfs_close(void *v) 382 { 383 struct vop_close_args /* { 384 struct vnode *a_vp; 385 int a_fflag; 386 kauth_cred_t a_cred; 387 } */ *ap = v; 388 struct pfsnode *pfs = VTOPFS(ap->a_vp); 389 390 switch (pfs->pfs_type) { 391 case PFSmem: 392 if ((ap->a_fflag & FWRITE) && (pfs->pfs_flags & O_EXCL)) 393 pfs->pfs_flags &= ~(FWRITE|O_EXCL); 394 break; 395 396 default: 397 break; 398 } 399 400 return (0); 401 } 402 403 /* 404 * _inactive is called when the pfsnode 405 * is vrele'd and the reference count goes 406 * to zero. (vp) will be on the vnode free 407 * list, so to get it back vget() must be 408 * used. 409 * 410 * (vp) is locked on entry, but must be unlocked on exit. 411 */ 412 int 413 procfs_inactive(void *v) 414 { 415 struct vop_inactive_args /* { 416 struct vnode *a_vp; 417 bool *a_recycle; 418 } */ *ap = v; 419 struct vnode *vp = ap->a_vp; 420 struct pfsnode *pfs = VTOPFS(vp); 421 422 mutex_enter(proc_lock); 423 *ap->a_recycle = (proc_find(pfs->pfs_pid) == NULL); 424 mutex_exit(proc_lock); 425 426 VOP_UNLOCK(vp); 427 428 return (0); 429 } 430 431 /* 432 * _reclaim is called when getnewvnode() 433 * wants to make use of an entry on the vnode 434 * free list. at this time the filesystem needs 435 * to free any private data and remove the node 436 * from any private lists. 437 */ 438 int 439 procfs_reclaim(void *v) 440 { 441 struct vop_reclaim_args /* { 442 struct vnode *a_vp; 443 } */ *ap = v; 444 struct vnode *vp = ap->a_vp; 445 struct pfsnode *pfs = VTOPFS(vp); 446 447 /* 448 * To interlock with procfs_revoke_vnodes(). 449 */ 450 mutex_enter(vp->v_interlock); 451 vp->v_data = NULL; 452 mutex_exit(vp->v_interlock); 453 vcache_remove(vp->v_mount, &pfs->pfs_key, sizeof(pfs->pfs_key)); 454 kmem_free(pfs, sizeof(*pfs)); 455 return 0; 456 } 457 458 /* 459 * Return POSIX pathconf information applicable to special devices. 460 */ 461 int 462 procfs_pathconf(void *v) 463 { 464 struct vop_pathconf_args /* { 465 struct vnode *a_vp; 466 int a_name; 467 register_t *a_retval; 468 } */ *ap = v; 469 470 switch (ap->a_name) { 471 case _PC_LINK_MAX: 472 *ap->a_retval = LINK_MAX; 473 return (0); 474 case _PC_MAX_CANON: 475 *ap->a_retval = MAX_CANON; 476 return (0); 477 case _PC_MAX_INPUT: 478 *ap->a_retval = MAX_INPUT; 479 return (0); 480 case _PC_PIPE_BUF: 481 *ap->a_retval = PIPE_BUF; 482 return (0); 483 case _PC_CHOWN_RESTRICTED: 484 *ap->a_retval = 1; 485 return (0); 486 case _PC_VDISABLE: 487 *ap->a_retval = _POSIX_VDISABLE; 488 return (0); 489 case _PC_SYNC_IO: 490 *ap->a_retval = 1; 491 return (0); 492 default: 493 return (EINVAL); 494 } 495 /* NOTREACHED */ 496 } 497 498 /* 499 * _print is used for debugging. 500 * just print a readable description 501 * of (vp). 502 */ 503 int 504 procfs_print(void *v) 505 { 506 struct vop_print_args /* { 507 struct vnode *a_vp; 508 } */ *ap = v; 509 struct pfsnode *pfs = VTOPFS(ap->a_vp); 510 511 printf("tag VT_PROCFS, type %d, pid %d, mode %x, flags %lx\n", 512 pfs->pfs_type, pfs->pfs_pid, pfs->pfs_mode, pfs->pfs_flags); 513 return 0; 514 } 515 516 int 517 procfs_link(void *v) 518 { 519 struct vop_link_args /* { 520 struct vnode *a_dvp; 521 struct vnode *a_vp; 522 struct componentname *a_cnp; 523 } */ *ap = v; 524 525 VOP_ABORTOP(ap->a_dvp, ap->a_cnp); 526 vput(ap->a_dvp); 527 return (EROFS); 528 } 529 530 int 531 procfs_symlink(void *v) 532 { 533 struct vop_symlink_v3_args /* { 534 struct vnode *a_dvp; 535 struct vnode **a_vpp; 536 struct componentname *a_cnp; 537 struct vattr *a_vap; 538 char *a_target; 539 } */ *ap = v; 540 541 VOP_ABORTOP(ap->a_dvp, ap->a_cnp); 542 return (EROFS); 543 } 544 545 /* 546 * Works out the path to (and vnode of) the target process's current 547 * working directory or chroot. If the caller is in a chroot and 548 * can't "reach" the target's cwd or root (or some other error 549 * occurs), a "/" is returned for the path and a NULL pointer is 550 * returned for the vnode. 551 */ 552 static void 553 procfs_dir(pfstype t, struct lwp *caller, struct proc *target, char **bpp, 554 char *path, size_t len) 555 { 556 struct cwdinfo *cwdi; 557 struct vnode *vp, *rvp; 558 char *bp; 559 560 cwdi = caller->l_proc->p_cwdi; 561 rw_enter(&cwdi->cwdi_lock, RW_READER); 562 563 rvp = cwdi->cwdi_rdir; 564 bp = bpp ? *bpp : NULL; 565 566 switch (t) { 567 case PFScwd: 568 vp = target->p_cwdi->cwdi_cdir; 569 break; 570 case PFSchroot: 571 vp = target->p_cwdi->cwdi_rdir; 572 break; 573 case PFSexe: 574 vp = target->p_textvp; 575 break; 576 default: 577 rw_exit(&cwdi->cwdi_lock); 578 return; 579 } 580 581 /* 582 * XXX: this horrible kludge avoids locking panics when 583 * attempting to lookup links that point to within procfs 584 */ 585 if (vp != NULL && vp->v_tag == VT_PROCFS) { 586 if (bpp) { 587 *--bp = '/'; 588 *bpp = bp; 589 } 590 rw_exit(&cwdi->cwdi_lock); 591 return; 592 } 593 594 if (rvp == NULL) 595 rvp = rootvnode; 596 if (vp == NULL || getcwd_common(vp, rvp, bp ? &bp : NULL, path, 597 len / 2, 0, caller) != 0) { 598 vp = NULL; 599 if (bpp) { 600 /* 601 if (t == PFSexe) { 602 snprintf(path, len, "%s/%d/file" 603 mp->mnt_stat.f_mntonname, pfs->pfs_pid); 604 } else */ { 605 bp = *bpp; 606 *--bp = '/'; 607 } 608 } 609 } 610 611 if (bpp) 612 *bpp = bp; 613 614 rw_exit(&cwdi->cwdi_lock); 615 } 616 617 /* 618 * Invent attributes for pfsnode (vp) and store 619 * them in (vap). 620 * Directories lengths are returned as zero since 621 * any real length would require the genuine size 622 * to be computed, and nothing cares anyway. 623 * 624 * this is relatively minimal for procfs. 625 */ 626 int 627 procfs_getattr(void *v) 628 { 629 struct vop_getattr_args /* { 630 struct vnode *a_vp; 631 struct vattr *a_vap; 632 kauth_cred_t a_cred; 633 } */ *ap = v; 634 struct pfsnode *pfs = VTOPFS(ap->a_vp); 635 struct vattr *vap = ap->a_vap; 636 struct proc *procp; 637 char *path; 638 int error; 639 640 /* first check the process still exists */ 641 switch (pfs->pfs_type) { 642 case PFSroot: 643 case PFScurproc: 644 case PFSself: 645 procp = NULL; 646 break; 647 648 default: 649 error = procfs_proc_lock(pfs->pfs_pid, &procp, ENOENT); 650 if (error != 0) 651 return (error); 652 break; 653 } 654 655 switch (pfs->pfs_type) { 656 case PFStask: 657 if (pfs->pfs_fd == -1) { 658 path = NULL; 659 break; 660 } 661 /*FALLTHROUGH*/ 662 case PFScwd: 663 case PFSchroot: 664 case PFSexe: 665 path = malloc(MAXPATHLEN + 4, M_TEMP, M_WAITOK|M_CANFAIL); 666 if (path == NULL && procp != NULL) { 667 procfs_proc_unlock(procp); 668 return (ENOMEM); 669 } 670 break; 671 672 default: 673 path = NULL; 674 break; 675 } 676 677 if (procp != NULL) { 678 mutex_enter(procp->p_lock); 679 error = kauth_authorize_process(kauth_cred_get(), 680 KAUTH_PROCESS_CANSEE, procp, 681 KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_ENTRY), NULL, NULL); 682 mutex_exit(procp->p_lock); 683 if (error != 0) { 684 procfs_proc_unlock(procp); 685 if (path != NULL) 686 free(path, M_TEMP); 687 return (ENOENT); 688 } 689 } 690 691 error = 0; 692 693 /* start by zeroing out the attributes */ 694 vattr_null(vap); 695 696 /* next do all the common fields */ 697 vap->va_type = ap->a_vp->v_type; 698 vap->va_mode = pfs->pfs_mode; 699 vap->va_fileid = pfs->pfs_fileno; 700 vap->va_flags = 0; 701 vap->va_blocksize = PAGE_SIZE; 702 703 /* 704 * Make all times be current TOD. 705 * 706 * It would be possible to get the process start 707 * time from the p_stats structure, but there's 708 * no "file creation" time stamp anyway, and the 709 * p_stats structure is not addressable if u. gets 710 * swapped out for that process. 711 */ 712 getnanotime(&vap->va_ctime); 713 vap->va_atime = vap->va_mtime = vap->va_ctime; 714 if (procp) 715 TIMEVAL_TO_TIMESPEC(&procp->p_stats->p_start, 716 &vap->va_birthtime); 717 else 718 getnanotime(&vap->va_birthtime); 719 720 switch (pfs->pfs_type) { 721 case PFSmem: 722 case PFSregs: 723 case PFSfpregs: 724 #if defined(__HAVE_PROCFS_MACHDEP) && defined(PROCFS_MACHDEP_PROTECT_CASES) 725 PROCFS_MACHDEP_PROTECT_CASES 726 #endif 727 /* 728 * If the process has exercised some setuid or setgid 729 * privilege, then rip away read/write permission so 730 * that only root can gain access. 731 */ 732 if (procp->p_flag & PK_SUGID) 733 vap->va_mode &= ~(S_IRUSR|S_IWUSR); 734 /* FALLTHROUGH */ 735 case PFSctl: 736 case PFSstatus: 737 case PFSstat: 738 case PFSnote: 739 case PFSnotepg: 740 case PFSmap: 741 case PFSmaps: 742 case PFScmdline: 743 case PFSemul: 744 case PFSstatm: 745 if (pfs->pfs_type == PFSmap || pfs->pfs_type == PFSmaps) 746 vap->va_mode = S_IRUSR; 747 vap->va_nlink = 1; 748 vap->va_uid = kauth_cred_geteuid(procp->p_cred); 749 vap->va_gid = kauth_cred_getegid(procp->p_cred); 750 break; 751 case PFSmeminfo: 752 case PFSdevices: 753 case PFScpuinfo: 754 case PFSuptime: 755 case PFSmounts: 756 case PFScpustat: 757 case PFSloadavg: 758 case PFSversion: 759 vap->va_nlink = 1; 760 vap->va_uid = vap->va_gid = 0; 761 break; 762 763 default: 764 break; 765 } 766 767 /* 768 * now do the object specific fields 769 * 770 * The size could be set from struct reg, but it's hardly 771 * worth the trouble, and it puts some (potentially) machine 772 * dependent data into this machine-independent code. If it 773 * becomes important then this function should break out into 774 * a per-file stat function in the corresponding .c file. 775 */ 776 777 switch (pfs->pfs_type) { 778 case PFSroot: 779 /* 780 * Set nlink to 1 to tell fts(3) we don't actually know. 781 */ 782 vap->va_nlink = 1; 783 vap->va_uid = 0; 784 vap->va_gid = 0; 785 vap->va_bytes = vap->va_size = DEV_BSIZE; 786 break; 787 788 case PFSself: 789 case PFScurproc: { 790 char bf[16]; /* should be enough */ 791 vap->va_nlink = 1; 792 vap->va_uid = 0; 793 vap->va_gid = 0; 794 vap->va_bytes = vap->va_size = 795 snprintf(bf, sizeof(bf), "%ld", (long)curproc->p_pid); 796 break; 797 } 798 case PFStask: 799 if (pfs->pfs_fd != -1) { 800 char bf[4]; /* should be enough */ 801 vap->va_nlink = 1; 802 vap->va_uid = 0; 803 vap->va_gid = 0; 804 vap->va_bytes = vap->va_size = 805 snprintf(bf, sizeof(bf), ".."); 806 break; 807 } 808 /*FALLTHROUGH*/ 809 case PFSfd: 810 if (pfs->pfs_fd != -1) { 811 file_t *fp; 812 813 fp = fd_getfile2(procp, pfs->pfs_fd); 814 if (fp == NULL) { 815 error = EBADF; 816 break; 817 } 818 vap->va_nlink = 1; 819 vap->va_uid = kauth_cred_geteuid(fp->f_cred); 820 vap->va_gid = kauth_cred_getegid(fp->f_cred); 821 switch (fp->f_type) { 822 case DTYPE_VNODE: 823 vap->va_bytes = vap->va_size = 824 fp->f_vnode->v_size; 825 break; 826 default: 827 vap->va_bytes = vap->va_size = 0; 828 break; 829 } 830 closef(fp); 831 break; 832 } 833 /*FALLTHROUGH*/ 834 case PFSproc: 835 vap->va_nlink = 2; 836 vap->va_uid = kauth_cred_geteuid(procp->p_cred); 837 vap->va_gid = kauth_cred_getegid(procp->p_cred); 838 vap->va_bytes = vap->va_size = DEV_BSIZE; 839 break; 840 841 case PFSfile: 842 error = EOPNOTSUPP; 843 break; 844 845 case PFSmem: 846 vap->va_bytes = vap->va_size = 847 ctob(procp->p_vmspace->vm_tsize + 848 procp->p_vmspace->vm_dsize + 849 procp->p_vmspace->vm_ssize); 850 break; 851 852 #if defined(PT_GETREGS) || defined(PT_SETREGS) 853 case PFSregs: 854 vap->va_bytes = vap->va_size = sizeof(struct reg); 855 break; 856 #endif 857 858 #if defined(PT_GETFPREGS) || defined(PT_SETFPREGS) 859 case PFSfpregs: 860 vap->va_bytes = vap->va_size = sizeof(struct fpreg); 861 break; 862 #endif 863 864 case PFSctl: 865 case PFSstatus: 866 case PFSstat: 867 case PFSnote: 868 case PFSnotepg: 869 case PFScmdline: 870 case PFSmeminfo: 871 case PFSdevices: 872 case PFScpuinfo: 873 case PFSuptime: 874 case PFSmounts: 875 case PFScpustat: 876 case PFSloadavg: 877 case PFSstatm: 878 case PFSversion: 879 vap->va_bytes = vap->va_size = 0; 880 break; 881 case PFSmap: 882 case PFSmaps: 883 /* 884 * Advise a larger blocksize for the map files, so that 885 * they may be read in one pass. 886 */ 887 vap->va_blocksize = 4 * PAGE_SIZE; 888 vap->va_bytes = vap->va_size = 0; 889 break; 890 891 case PFScwd: 892 case PFSchroot: 893 case PFSexe: { 894 char *bp; 895 896 vap->va_nlink = 1; 897 vap->va_uid = 0; 898 vap->va_gid = 0; 899 bp = path + MAXPATHLEN; 900 *--bp = '\0'; 901 procfs_dir(pfs->pfs_type, curlwp, procp, &bp, path, 902 MAXPATHLEN); 903 vap->va_bytes = vap->va_size = strlen(bp); 904 break; 905 } 906 907 case PFSemul: 908 vap->va_bytes = vap->va_size = strlen(procp->p_emul->e_name); 909 break; 910 911 #ifdef __HAVE_PROCFS_MACHDEP 912 PROCFS_MACHDEP_NODETYPE_CASES 913 error = procfs_machdep_getattr(ap->a_vp, vap, procp); 914 break; 915 #endif 916 917 default: 918 panic("procfs_getattr"); 919 } 920 921 if (procp != NULL) 922 procfs_proc_unlock(procp); 923 if (path != NULL) 924 free(path, M_TEMP); 925 926 return (error); 927 } 928 929 /*ARGSUSED*/ 930 int 931 procfs_setattr(void *v) 932 { 933 /* 934 * just fake out attribute setting 935 * it's not good to generate an error 936 * return, otherwise things like creat() 937 * will fail when they try to set the 938 * file length to 0. worse, this means 939 * that echo $note > /proc/$pid/note will fail. 940 */ 941 942 return (0); 943 } 944 945 /* 946 * implement access checking. 947 * 948 * actually, the check for super-user is slightly 949 * broken since it will allow read access to write-only 950 * objects. this doesn't cause any particular trouble 951 * but does mean that the i/o entry points need to check 952 * that the operation really does make sense. 953 */ 954 int 955 procfs_access(void *v) 956 { 957 struct vop_access_args /* { 958 struct vnode *a_vp; 959 int a_mode; 960 kauth_cred_t a_cred; 961 } */ *ap = v; 962 struct vattr va; 963 int error; 964 965 if ((error = VOP_GETATTR(ap->a_vp, &va, ap->a_cred)) != 0) 966 return (error); 967 968 return kauth_authorize_vnode(ap->a_cred, 969 KAUTH_ACCESS_ACTION(ap->a_mode, ap->a_vp->v_type, va.va_mode), 970 ap->a_vp, NULL, genfs_can_access(va.va_type, va.va_mode, 971 va.va_uid, va.va_gid, ap->a_mode, ap->a_cred)); 972 } 973 974 /* 975 * lookup. this is incredibly complicated in the 976 * general case, however for most pseudo-filesystems 977 * very little needs to be done. 978 * 979 * Locking isn't hard here, just poorly documented. 980 * 981 * If we're looking up ".", just vref the parent & return it. 982 * 983 * If we're looking up "..", unlock the parent, and lock "..". If everything 984 * went ok, and we're on the last component and the caller requested the 985 * parent locked, try to re-lock the parent. We do this to prevent lock 986 * races. 987 * 988 * For anything else, get the needed node. Then unlock the parent if not 989 * the last component or not LOCKPARENT (i.e. if we wouldn't re-lock the 990 * parent in the .. case). 991 * 992 * We try to exit with the parent locked in error cases. 993 */ 994 int 995 procfs_lookup(void *v) 996 { 997 struct vop_lookup_v2_args /* { 998 struct vnode * a_dvp; 999 struct vnode ** a_vpp; 1000 struct componentname * a_cnp; 1001 } */ *ap = v; 1002 struct componentname *cnp = ap->a_cnp; 1003 struct vnode **vpp = ap->a_vpp; 1004 struct vnode *dvp = ap->a_dvp; 1005 const char *pname = cnp->cn_nameptr; 1006 const struct proc_target *pt = NULL; 1007 struct vnode *fvp; 1008 pid_t pid, vnpid; 1009 struct pfsnode *pfs; 1010 struct proc *p = NULL; 1011 struct lwp *plwp; 1012 int i, error; 1013 pfstype type; 1014 1015 *vpp = NULL; 1016 1017 if (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME) 1018 return (EROFS); 1019 1020 if (cnp->cn_namelen == 1 && *pname == '.') { 1021 *vpp = dvp; 1022 vref(dvp); 1023 return (0); 1024 } 1025 1026 pfs = VTOPFS(dvp); 1027 switch (pfs->pfs_type) { 1028 case PFSroot: 1029 /* 1030 * Shouldn't get here with .. in the root node. 1031 */ 1032 if (cnp->cn_flags & ISDOTDOT) 1033 return (EIO); 1034 1035 for (i = 0; i < nproc_root_targets; i++) { 1036 pt = &proc_root_targets[i]; 1037 /* 1038 * check for node match. proc is always NULL here, 1039 * so call pt_valid with constant NULL lwp. 1040 */ 1041 if (cnp->cn_namelen == pt->pt_namlen && 1042 memcmp(pt->pt_name, pname, cnp->cn_namelen) == 0 && 1043 (pt->pt_valid == NULL || 1044 (*pt->pt_valid)(NULL, dvp->v_mount))) 1045 break; 1046 } 1047 1048 if (i != nproc_root_targets) { 1049 error = procfs_allocvp(dvp->v_mount, vpp, 0, 1050 pt->pt_pfstype, -1); 1051 return (error); 1052 } 1053 1054 if (CNEQ(cnp, "curproc", 7)) { 1055 pid = curproc->p_pid; 1056 vnpid = 0; 1057 type = PFScurproc; 1058 } else if (CNEQ(cnp, "self", 4)) { 1059 pid = curproc->p_pid; 1060 vnpid = 0; 1061 type = PFSself; 1062 } else { 1063 pid = (pid_t)atoi(pname, cnp->cn_namelen); 1064 vnpid = pid; 1065 type = PFSproc; 1066 } 1067 1068 if (procfs_proc_lock(pid, &p, ESRCH) != 0) 1069 break; 1070 error = procfs_allocvp(dvp->v_mount, vpp, vnpid, type, -1); 1071 procfs_proc_unlock(p); 1072 return (error); 1073 1074 case PFSproc: 1075 if (cnp->cn_flags & ISDOTDOT) { 1076 error = procfs_allocvp(dvp->v_mount, vpp, 0, PFSroot, 1077 -1); 1078 return (error); 1079 } 1080 1081 if (procfs_proc_lock(pfs->pfs_pid, &p, ESRCH) != 0) 1082 break; 1083 1084 mutex_enter(p->p_lock); 1085 LIST_FOREACH(plwp, &p->p_lwps, l_sibling) { 1086 if (plwp->l_stat != LSZOMB) 1087 break; 1088 } 1089 /* Process is exiting if no-LWPS or all LWPs are LSZOMB */ 1090 if (plwp == NULL) { 1091 mutex_exit(p->p_lock); 1092 procfs_proc_unlock(p); 1093 return ESRCH; 1094 } 1095 1096 lwp_addref(plwp); 1097 mutex_exit(p->p_lock); 1098 1099 for (pt = proc_targets, i = 0; i < nproc_targets; pt++, i++) { 1100 int found; 1101 1102 found = cnp->cn_namelen == pt->pt_namlen && 1103 memcmp(pt->pt_name, pname, cnp->cn_namelen) == 0 && 1104 (pt->pt_valid == NULL 1105 || (*pt->pt_valid)(plwp, dvp->v_mount)); 1106 if (found) 1107 break; 1108 } 1109 lwp_delref(plwp); 1110 1111 if (i == nproc_targets) { 1112 procfs_proc_unlock(p); 1113 break; 1114 } 1115 if (pt->pt_pfstype == PFSfile) { 1116 fvp = p->p_textvp; 1117 /* We already checked that it exists. */ 1118 vref(fvp); 1119 procfs_proc_unlock(p); 1120 *vpp = fvp; 1121 return (0); 1122 } 1123 1124 error = procfs_allocvp(dvp->v_mount, vpp, pfs->pfs_pid, 1125 pt->pt_pfstype, -1); 1126 procfs_proc_unlock(p); 1127 return (error); 1128 1129 case PFSfd: { 1130 int fd; 1131 file_t *fp; 1132 1133 if ((error = procfs_proc_lock(pfs->pfs_pid, &p, ENOENT)) != 0) 1134 return error; 1135 1136 if (cnp->cn_flags & ISDOTDOT) { 1137 error = procfs_allocvp(dvp->v_mount, vpp, pfs->pfs_pid, 1138 PFSproc, -1); 1139 procfs_proc_unlock(p); 1140 return (error); 1141 } 1142 fd = atoi(pname, cnp->cn_namelen); 1143 1144 fp = fd_getfile2(p, fd); 1145 if (fp == NULL) { 1146 procfs_proc_unlock(p); 1147 return ENOENT; 1148 } 1149 fvp = fp->f_vnode; 1150 1151 /* Don't show directories */ 1152 if (fp->f_type == DTYPE_VNODE && fvp->v_type != VDIR) { 1153 vref(fvp); 1154 closef(fp); 1155 procfs_proc_unlock(p); 1156 *vpp = fvp; 1157 return 0; 1158 } 1159 1160 closef(fp); 1161 error = procfs_allocvp(dvp->v_mount, vpp, pfs->pfs_pid, 1162 PFSfd, fd); 1163 procfs_proc_unlock(p); 1164 return error; 1165 } 1166 case PFStask: { 1167 int xpid; 1168 1169 if ((error = procfs_proc_lock(pfs->pfs_pid, &p, ENOENT)) != 0) 1170 return error; 1171 1172 if (cnp->cn_flags & ISDOTDOT) { 1173 error = procfs_allocvp(dvp->v_mount, vpp, pfs->pfs_pid, 1174 PFSproc, -1); 1175 procfs_proc_unlock(p); 1176 return (error); 1177 } 1178 xpid = atoi(pname, cnp->cn_namelen); 1179 1180 if (xpid != pfs->pfs_pid) { 1181 procfs_proc_unlock(p); 1182 return ENOENT; 1183 } 1184 error = procfs_allocvp(dvp->v_mount, vpp, pfs->pfs_pid, 1185 PFStask, 0); 1186 procfs_proc_unlock(p); 1187 return error; 1188 } 1189 default: 1190 return (ENOTDIR); 1191 } 1192 1193 return (cnp->cn_nameiop == LOOKUP ? ENOENT : EROFS); 1194 } 1195 1196 int 1197 procfs_validfile(struct lwp *l, struct mount *mp) 1198 { 1199 return l != NULL && l->l_proc != NULL && l->l_proc->p_textvp != NULL; 1200 } 1201 1202 static int 1203 procfs_validfile_linux(struct lwp *l, struct mount *mp) 1204 { 1205 int flags; 1206 1207 flags = VFSTOPROC(mp)->pmnt_flags; 1208 return (flags & PROCFSMNT_LINUXCOMPAT) && 1209 (l == NULL || l->l_proc == NULL || procfs_validfile(l, mp)); 1210 } 1211 1212 struct procfs_root_readdir_ctx { 1213 struct uio *uiop; 1214 off_t *cookies; 1215 int ncookies; 1216 off_t off; 1217 off_t startoff; 1218 int error; 1219 }; 1220 1221 static int 1222 procfs_root_readdir_callback(struct proc *p, void *arg) 1223 { 1224 struct procfs_root_readdir_ctx *ctxp = arg; 1225 struct dirent d; 1226 struct uio *uiop; 1227 int error; 1228 1229 uiop = ctxp->uiop; 1230 if (uiop->uio_resid < UIO_MX) 1231 return -1; /* no space */ 1232 1233 if (ctxp->off < ctxp->startoff) { 1234 ctxp->off++; 1235 return 0; 1236 } 1237 1238 if (kauth_authorize_process(kauth_cred_get(), 1239 KAUTH_PROCESS_CANSEE, p, 1240 KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_ENTRY), NULL, NULL) != 0) 1241 return 0; 1242 1243 memset(&d, 0, UIO_MX); 1244 d.d_reclen = UIO_MX; 1245 d.d_fileno = PROCFS_FILENO(p->p_pid, PFSproc, -1); 1246 d.d_namlen = snprintf(d.d_name, 1247 UIO_MX - offsetof(struct dirent, d_name), "%ld", (long)p->p_pid); 1248 d.d_type = DT_DIR; 1249 1250 mutex_exit(proc_lock); 1251 error = uiomove(&d, UIO_MX, uiop); 1252 mutex_enter(proc_lock); 1253 if (error) { 1254 ctxp->error = error; 1255 return -1; 1256 } 1257 1258 ctxp->ncookies++; 1259 if (ctxp->cookies) 1260 *(ctxp->cookies)++ = ctxp->off + 1; 1261 ctxp->off++; 1262 1263 return 0; 1264 } 1265 1266 /* 1267 * readdir returns directory entries from pfsnode (vp). 1268 * 1269 * the strategy here with procfs is to generate a single 1270 * directory entry at a time (struct dirent) and then 1271 * copy that out to userland using uiomove. a more efficent 1272 * though more complex implementation, would try to minimize 1273 * the number of calls to uiomove(). for procfs, this is 1274 * hardly worth the added code complexity. 1275 * 1276 * this should just be done through read() 1277 */ 1278 int 1279 procfs_readdir(void *v) 1280 { 1281 struct vop_readdir_args /* { 1282 struct vnode *a_vp; 1283 struct uio *a_uio; 1284 kauth_cred_t a_cred; 1285 int *a_eofflag; 1286 off_t **a_cookies; 1287 int *a_ncookies; 1288 } */ *ap = v; 1289 struct uio *uio = ap->a_uio; 1290 struct dirent d; 1291 struct pfsnode *pfs; 1292 off_t i; 1293 int error; 1294 off_t *cookies = NULL; 1295 int ncookies; 1296 struct vnode *vp; 1297 const struct proc_target *pt; 1298 struct procfs_root_readdir_ctx ctx; 1299 struct lwp *l; 1300 int nfd; 1301 1302 vp = ap->a_vp; 1303 pfs = VTOPFS(vp); 1304 1305 if (uio->uio_resid < UIO_MX) 1306 return (EINVAL); 1307 if (uio->uio_offset < 0) 1308 return (EINVAL); 1309 1310 error = 0; 1311 i = uio->uio_offset; 1312 memset(&d, 0, UIO_MX); 1313 d.d_reclen = UIO_MX; 1314 ncookies = uio->uio_resid / UIO_MX; 1315 1316 switch (pfs->pfs_type) { 1317 /* 1318 * this is for the process-specific sub-directories. 1319 * all that is needed to is copy out all the entries 1320 * from the procent[] table (top of this file). 1321 */ 1322 case PFSproc: { 1323 struct proc *p; 1324 1325 if (i >= nproc_targets) 1326 return 0; 1327 1328 if (procfs_proc_lock(pfs->pfs_pid, &p, ESRCH) != 0) 1329 break; 1330 1331 if (ap->a_ncookies) { 1332 ncookies = min(ncookies, (nproc_targets - i)); 1333 cookies = malloc(ncookies * sizeof (off_t), 1334 M_TEMP, M_WAITOK); 1335 *ap->a_cookies = cookies; 1336 } 1337 1338 for (pt = &proc_targets[i]; 1339 uio->uio_resid >= UIO_MX && i < nproc_targets; pt++, i++) { 1340 if (pt->pt_valid) { 1341 /* XXXSMP LWP can disappear */ 1342 mutex_enter(p->p_lock); 1343 l = LIST_FIRST(&p->p_lwps); 1344 KASSERT(l != NULL); 1345 mutex_exit(p->p_lock); 1346 if ((*pt->pt_valid)(l, vp->v_mount) == 0) 1347 continue; 1348 } 1349 1350 d.d_fileno = PROCFS_FILENO(pfs->pfs_pid, 1351 pt->pt_pfstype, -1); 1352 d.d_namlen = pt->pt_namlen; 1353 memcpy(d.d_name, pt->pt_name, pt->pt_namlen + 1); 1354 d.d_type = pt->pt_type; 1355 1356 if ((error = uiomove(&d, UIO_MX, uio)) != 0) 1357 break; 1358 if (cookies) 1359 *cookies++ = i + 1; 1360 } 1361 1362 procfs_proc_unlock(p); 1363 break; 1364 } 1365 case PFSfd: { 1366 struct proc *p; 1367 file_t *fp; 1368 int lim, nc = 0; 1369 1370 if ((error = procfs_proc_lock(pfs->pfs_pid, &p, ESRCH)) != 0) 1371 return error; 1372 1373 /* XXX Should this be by file as well? */ 1374 if (kauth_authorize_process(kauth_cred_get(), 1375 KAUTH_PROCESS_CANSEE, p, 1376 KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_OPENFILES), NULL, 1377 NULL) != 0) { 1378 procfs_proc_unlock(p); 1379 return ESRCH; 1380 } 1381 1382 nfd = p->p_fd->fd_dt->dt_nfiles; 1383 1384 lim = min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfiles); 1385 if (i >= lim) { 1386 procfs_proc_unlock(p); 1387 return 0; 1388 } 1389 1390 if (ap->a_ncookies) { 1391 ncookies = min(ncookies, (nfd + 2 - i)); 1392 cookies = malloc(ncookies * sizeof (off_t), 1393 M_TEMP, M_WAITOK); 1394 *ap->a_cookies = cookies; 1395 } 1396 1397 for (; i < 2 && uio->uio_resid >= UIO_MX; i++) { 1398 pt = &proc_targets[i]; 1399 d.d_namlen = pt->pt_namlen; 1400 d.d_fileno = PROCFS_FILENO(pfs->pfs_pid, 1401 pt->pt_pfstype, -1); 1402 (void)memcpy(d.d_name, pt->pt_name, pt->pt_namlen + 1); 1403 d.d_type = pt->pt_type; 1404 if ((error = uiomove(&d, UIO_MX, uio)) != 0) 1405 break; 1406 if (cookies) 1407 *cookies++ = i + 1; 1408 nc++; 1409 } 1410 if (error) { 1411 ncookies = nc; 1412 break; 1413 } 1414 for (; uio->uio_resid >= UIO_MX && i < nfd; i++) { 1415 /* check the descriptor exists */ 1416 if ((fp = fd_getfile2(p, i - 2)) == NULL) 1417 continue; 1418 closef(fp); 1419 1420 d.d_fileno = PROCFS_FILENO(pfs->pfs_pid, PFSfd, i - 2); 1421 d.d_namlen = snprintf(d.d_name, sizeof(d.d_name), 1422 "%lld", (long long)(i - 2)); 1423 d.d_type = VREG; 1424 if ((error = uiomove(&d, UIO_MX, uio)) != 0) 1425 break; 1426 if (cookies) 1427 *cookies++ = i + 1; 1428 nc++; 1429 } 1430 ncookies = nc; 1431 procfs_proc_unlock(p); 1432 break; 1433 } 1434 case PFStask: { 1435 struct proc *p; 1436 int nc = 0; 1437 1438 if ((error = procfs_proc_lock(pfs->pfs_pid, &p, ESRCH)) != 0) 1439 return error; 1440 1441 nfd = 3; /* ., .., pid */ 1442 1443 if (ap->a_ncookies) { 1444 ncookies = min(ncookies, (nfd + 2 - i)); 1445 cookies = malloc(ncookies * sizeof (off_t), 1446 M_TEMP, M_WAITOK); 1447 *ap->a_cookies = cookies; 1448 } 1449 1450 for (; i < 2 && uio->uio_resid >= UIO_MX; i++) { 1451 pt = &proc_targets[i]; 1452 d.d_namlen = pt->pt_namlen; 1453 d.d_fileno = PROCFS_FILENO(pfs->pfs_pid, 1454 pt->pt_pfstype, -1); 1455 (void)memcpy(d.d_name, pt->pt_name, pt->pt_namlen + 1); 1456 d.d_type = pt->pt_type; 1457 if ((error = uiomove(&d, UIO_MX, uio)) != 0) 1458 break; 1459 if (cookies) 1460 *cookies++ = i + 1; 1461 nc++; 1462 } 1463 if (error) { 1464 ncookies = nc; 1465 break; 1466 } 1467 for (; uio->uio_resid >= UIO_MX && i < nfd; i++) { 1468 /* check the descriptor exists */ 1469 d.d_fileno = PROCFS_FILENO(pfs->pfs_pid, PFStask, 1470 i - 2); 1471 d.d_namlen = snprintf(d.d_name, sizeof(d.d_name), 1472 "%ld", (long)pfs->pfs_pid); 1473 d.d_type = DT_LNK; 1474 if ((error = uiomove(&d, UIO_MX, uio)) != 0) 1475 break; 1476 if (cookies) 1477 *cookies++ = i + 1; 1478 nc++; 1479 } 1480 ncookies = nc; 1481 procfs_proc_unlock(p); 1482 break; 1483 } 1484 1485 /* 1486 * this is for the root of the procfs filesystem 1487 * what is needed are special entries for "curproc" 1488 * and "self" followed by an entry for each process 1489 * on allproc. 1490 */ 1491 1492 case PFSroot: { 1493 int nc = 0; 1494 1495 if (ap->a_ncookies) { 1496 /* 1497 * XXX Potentially allocating too much space here, 1498 * but I'm lazy. This loop needs some work. 1499 */ 1500 cookies = malloc(ncookies * sizeof (off_t), 1501 M_TEMP, M_WAITOK); 1502 *ap->a_cookies = cookies; 1503 } 1504 error = 0; 1505 /* 0 ... 3 are static entries. */ 1506 for (; i <= 3 && uio->uio_resid >= UIO_MX; i++) { 1507 switch (i) { 1508 case 0: /* `.' */ 1509 case 1: /* `..' */ 1510 d.d_fileno = PROCFS_FILENO(0, PFSroot, -1); 1511 d.d_namlen = i + 1; 1512 memcpy(d.d_name, "..", d.d_namlen); 1513 d.d_name[i + 1] = '\0'; 1514 d.d_type = DT_DIR; 1515 break; 1516 1517 case 2: 1518 d.d_fileno = PROCFS_FILENO(0, PFScurproc, -1); 1519 d.d_namlen = sizeof("curproc") - 1; 1520 memcpy(d.d_name, "curproc", sizeof("curproc")); 1521 d.d_type = DT_LNK; 1522 break; 1523 1524 case 3: 1525 d.d_fileno = PROCFS_FILENO(0, PFSself, -1); 1526 d.d_namlen = sizeof("self") - 1; 1527 memcpy(d.d_name, "self", sizeof("self")); 1528 d.d_type = DT_LNK; 1529 break; 1530 } 1531 1532 if ((error = uiomove(&d, UIO_MX, uio)) != 0) 1533 break; 1534 nc++; 1535 if (cookies) 1536 *cookies++ = i + 1; 1537 } 1538 /* 4 ... are process entries. */ 1539 ctx.uiop = uio; 1540 ctx.error = 0; 1541 ctx.off = 4; 1542 ctx.startoff = i; 1543 ctx.cookies = cookies; 1544 ctx.ncookies = nc; 1545 proclist_foreach_call(&allproc, 1546 procfs_root_readdir_callback, &ctx); 1547 cookies = ctx.cookies; 1548 nc = ctx.ncookies; 1549 error = ctx.error; 1550 if (error) 1551 break; 1552 1553 /* misc entries. */ 1554 if (i < ctx.off) 1555 i = ctx.off; 1556 if (i >= ctx.off + nproc_root_targets) 1557 break; 1558 for (pt = &proc_root_targets[i - ctx.off]; 1559 uio->uio_resid >= UIO_MX && 1560 pt < &proc_root_targets[nproc_root_targets]; 1561 pt++, i++) { 1562 if (pt->pt_valid && 1563 (*pt->pt_valid)(NULL, vp->v_mount) == 0) 1564 continue; 1565 d.d_fileno = PROCFS_FILENO(0, pt->pt_pfstype, -1); 1566 d.d_namlen = pt->pt_namlen; 1567 memcpy(d.d_name, pt->pt_name, pt->pt_namlen + 1); 1568 d.d_type = pt->pt_type; 1569 1570 if ((error = uiomove(&d, UIO_MX, uio)) != 0) 1571 break; 1572 nc++; 1573 if (cookies) 1574 *cookies++ = i + 1; 1575 } 1576 1577 ncookies = nc; 1578 break; 1579 } 1580 1581 default: 1582 error = ENOTDIR; 1583 break; 1584 } 1585 1586 if (ap->a_ncookies) { 1587 if (error) { 1588 if (cookies) 1589 free(*ap->a_cookies, M_TEMP); 1590 *ap->a_ncookies = 0; 1591 *ap->a_cookies = NULL; 1592 } else 1593 *ap->a_ncookies = ncookies; 1594 } 1595 uio->uio_offset = i; 1596 return (error); 1597 } 1598 1599 /* 1600 * readlink reads the link of `curproc' and others 1601 */ 1602 int 1603 procfs_readlink(void *v) 1604 { 1605 struct vop_readlink_args *ap = v; 1606 char bf[16]; /* should be enough */ 1607 char *bp = bf; 1608 char *path = NULL; 1609 int len = 0; 1610 int error = 0; 1611 struct pfsnode *pfs = VTOPFS(ap->a_vp); 1612 struct proc *pown; 1613 1614 if (pfs->pfs_fileno == PROCFS_FILENO(0, PFScurproc, -1)) 1615 len = snprintf(bf, sizeof(bf), "%ld", (long)curproc->p_pid); 1616 else if (pfs->pfs_fileno == PROCFS_FILENO(0, PFSself, -1)) 1617 len = snprintf(bf, sizeof(bf), "%s", "curproc"); 1618 else if (pfs->pfs_fileno == PROCFS_FILENO(pfs->pfs_pid, PFStask, 0)) 1619 len = snprintf(bf, sizeof(bf), ".."); 1620 else if (pfs->pfs_fileno == PROCFS_FILENO(pfs->pfs_pid, PFScwd, -1) || 1621 pfs->pfs_fileno == PROCFS_FILENO(pfs->pfs_pid, PFSchroot, -1) || 1622 pfs->pfs_fileno == PROCFS_FILENO(pfs->pfs_pid, PFSexe, -1)) { 1623 if ((error = procfs_proc_lock(pfs->pfs_pid, &pown, ESRCH)) != 0) 1624 return error; 1625 path = malloc(MAXPATHLEN + 4, M_TEMP, M_WAITOK|M_CANFAIL); 1626 if (path == NULL) { 1627 procfs_proc_unlock(pown); 1628 return (ENOMEM); 1629 } 1630 bp = path + MAXPATHLEN; 1631 *--bp = '\0'; 1632 procfs_dir(PROCFS_TYPE(pfs->pfs_fileno), curlwp, pown, 1633 &bp, path, MAXPATHLEN); 1634 procfs_proc_unlock(pown); 1635 len = strlen(bp); 1636 } else { 1637 file_t *fp; 1638 struct vnode *vxp, *vp; 1639 1640 if ((error = procfs_proc_lock(pfs->pfs_pid, &pown, ESRCH)) != 0) 1641 return error; 1642 1643 fp = fd_getfile2(pown, pfs->pfs_fd); 1644 if (fp == NULL) { 1645 procfs_proc_unlock(pown); 1646 return EBADF; 1647 } 1648 1649 switch (fp->f_type) { 1650 case DTYPE_VNODE: 1651 vxp = fp->f_vnode; 1652 if (vxp->v_type != VDIR) { 1653 error = EINVAL; 1654 break; 1655 } 1656 if ((path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK)) 1657 == NULL) { 1658 error = ENOMEM; 1659 break; 1660 } 1661 bp = path + MAXPATHLEN; 1662 *--bp = '\0'; 1663 1664 /* 1665 * XXX: kludge to avoid locking against ourselves 1666 * in getcwd() 1667 */ 1668 if (vxp->v_tag == VT_PROCFS) { 1669 *--bp = '/'; 1670 } else { 1671 rw_enter(&curproc->p_cwdi->cwdi_lock, 1672 RW_READER); 1673 vp = curproc->p_cwdi->cwdi_rdir; 1674 if (vp == NULL) 1675 vp = rootvnode; 1676 error = getcwd_common(vxp, vp, &bp, path, 1677 MAXPATHLEN / 2, 0, curlwp); 1678 rw_exit(&curproc->p_cwdi->cwdi_lock); 1679 } 1680 if (error) 1681 break; 1682 len = strlen(bp); 1683 break; 1684 1685 case DTYPE_MISC: 1686 len = snprintf(bf, sizeof(bf), "%s", "[misc]"); 1687 break; 1688 1689 case DTYPE_KQUEUE: 1690 len = snprintf(bf, sizeof(bf), "%s", "[kqueue]"); 1691 break; 1692 1693 case DTYPE_SEM: 1694 len = snprintf(bf, sizeof(bf), "%s", "[ksem]"); 1695 break; 1696 1697 default: 1698 error = EINVAL; 1699 break; 1700 } 1701 closef(fp); 1702 procfs_proc_unlock(pown); 1703 } 1704 1705 if (error == 0) 1706 error = uiomove(bp, len, ap->a_uio); 1707 if (path) 1708 free(path, M_TEMP); 1709 return error; 1710 } 1711 1712 /* 1713 * convert decimal ascii to int 1714 */ 1715 static int 1716 atoi(const char *b, size_t len) 1717 { 1718 int p = 0; 1719 1720 while (len--) { 1721 char c = *b++; 1722 if (c < '0' || c > '9') 1723 return -1; 1724 p = 10 * p + (c - '0'); 1725 } 1726 1727 return p; 1728 } 1729