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