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