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