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