1 /* $NetBSD: procfs_vnops.c,v 1.172 2008/09/05 14:01:11 skrll 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.172 2008/09/05 14:01:11 skrll 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 = (p_find(pfs->pfs_pid, PFIND_LOCKED) == NULL); 420 mutex_exit(proc_lock); 421 422 VOP_UNLOCK(vp, 0); 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 MALLOC(path, char *, MAXPATHLEN + 4, M_TEMP, 647 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 vap->va_nlink = 1; 728 vap->va_uid = kauth_cred_geteuid(procp->p_cred); 729 vap->va_gid = kauth_cred_getegid(procp->p_cred); 730 break; 731 case PFSmeminfo: 732 case PFSdevices: 733 case PFScpuinfo: 734 case PFSuptime: 735 case PFSmounts: 736 case PFScpustat: 737 case PFSloadavg: 738 vap->va_nlink = 1; 739 vap->va_uid = vap->va_gid = 0; 740 break; 741 742 default: 743 break; 744 } 745 746 /* 747 * now do the object specific fields 748 * 749 * The size could be set from struct reg, but it's hardly 750 * worth the trouble, and it puts some (potentially) machine 751 * dependent data into this machine-independent code. If it 752 * becomes important then this function should break out into 753 * a per-file stat function in the corresponding .c file. 754 */ 755 756 switch (pfs->pfs_type) { 757 case PFSroot: 758 /* 759 * Set nlink to 1 to tell fts(3) we don't actually know. 760 */ 761 vap->va_nlink = 1; 762 vap->va_uid = 0; 763 vap->va_gid = 0; 764 vap->va_bytes = vap->va_size = DEV_BSIZE; 765 break; 766 767 case PFSself: 768 case PFScurproc: { 769 char bf[16]; /* should be enough */ 770 vap->va_nlink = 1; 771 vap->va_uid = 0; 772 vap->va_gid = 0; 773 vap->va_bytes = vap->va_size = 774 snprintf(bf, sizeof(bf), "%ld", (long)curproc->p_pid); 775 break; 776 } 777 778 case PFSfd: 779 if (pfs->pfs_fd != -1) { 780 file_t *fp; 781 782 fp = fd_getfile2(procp, pfs->pfs_fd); 783 if (fp == NULL) { 784 error = EBADF; 785 break; 786 } 787 vap->va_nlink = 1; 788 vap->va_uid = kauth_cred_geteuid(fp->f_cred); 789 vap->va_gid = kauth_cred_getegid(fp->f_cred); 790 switch (fp->f_type) { 791 case DTYPE_VNODE: 792 vap->va_bytes = vap->va_size = 793 ((struct vnode *)fp->f_data)->v_size; 794 break; 795 default: 796 vap->va_bytes = vap->va_size = 0; 797 break; 798 } 799 closef(fp); 800 break; 801 } 802 /*FALLTHROUGH*/ 803 case PFSproc: 804 vap->va_nlink = 2; 805 vap->va_uid = kauth_cred_geteuid(procp->p_cred); 806 vap->va_gid = kauth_cred_getegid(procp->p_cred); 807 vap->va_bytes = vap->va_size = DEV_BSIZE; 808 break; 809 810 case PFSfile: 811 error = EOPNOTSUPP; 812 break; 813 814 case PFSmem: 815 vap->va_bytes = vap->va_size = 816 ctob(procp->p_vmspace->vm_tsize + 817 procp->p_vmspace->vm_dsize + 818 procp->p_vmspace->vm_ssize); 819 break; 820 821 #if defined(PT_GETREGS) || defined(PT_SETREGS) 822 case PFSregs: 823 vap->va_bytes = vap->va_size = sizeof(struct reg); 824 break; 825 #endif 826 827 #if defined(PT_GETFPREGS) || defined(PT_SETFPREGS) 828 case PFSfpregs: 829 vap->va_bytes = vap->va_size = sizeof(struct fpreg); 830 break; 831 #endif 832 833 case PFSctl: 834 case PFSstatus: 835 case PFSstat: 836 case PFSnote: 837 case PFSnotepg: 838 case PFScmdline: 839 case PFSmeminfo: 840 case PFSdevices: 841 case PFScpuinfo: 842 case PFSuptime: 843 case PFSmounts: 844 case PFScpustat: 845 case PFSloadavg: 846 case PFSstatm: 847 vap->va_bytes = vap->va_size = 0; 848 break; 849 case PFSmap: 850 case PFSmaps: 851 /* 852 * Advise a larger blocksize for the map files, so that 853 * they may be read in one pass. 854 */ 855 vap->va_blocksize = 4 * PAGE_SIZE; 856 vap->va_bytes = vap->va_size = 0; 857 break; 858 859 case PFScwd: 860 case PFSchroot: 861 case PFSexe: { 862 char *bp; 863 864 vap->va_nlink = 1; 865 vap->va_uid = 0; 866 vap->va_gid = 0; 867 bp = path + MAXPATHLEN; 868 *--bp = '\0'; 869 procfs_dir(pfs->pfs_type, curlwp, procp, &bp, path, 870 MAXPATHLEN); 871 vap->va_bytes = vap->va_size = strlen(bp); 872 break; 873 } 874 875 case PFSemul: 876 vap->va_bytes = vap->va_size = strlen(procp->p_emul->e_name); 877 break; 878 879 #ifdef __HAVE_PROCFS_MACHDEP 880 PROCFS_MACHDEP_NODETYPE_CASES 881 error = procfs_machdep_getattr(ap->a_vp, vap, procp); 882 break; 883 #endif 884 885 default: 886 panic("procfs_getattr"); 887 } 888 889 if (procp != NULL) 890 procfs_proc_unlock(procp); 891 if (path != NULL) 892 free(path, M_TEMP); 893 894 return (error); 895 } 896 897 /*ARGSUSED*/ 898 int 899 procfs_setattr(void *v) 900 { 901 /* 902 * just fake out attribute setting 903 * it's not good to generate an error 904 * return, otherwise things like creat() 905 * will fail when they try to set the 906 * file length to 0. worse, this means 907 * that echo $note > /proc/$pid/note will fail. 908 */ 909 910 return (0); 911 } 912 913 /* 914 * implement access checking. 915 * 916 * actually, the check for super-user is slightly 917 * broken since it will allow read access to write-only 918 * objects. this doesn't cause any particular trouble 919 * but does mean that the i/o entry points need to check 920 * that the operation really does make sense. 921 */ 922 int 923 procfs_access(void *v) 924 { 925 struct vop_access_args /* { 926 struct vnode *a_vp; 927 int a_mode; 928 kauth_cred_t a_cred; 929 } */ *ap = v; 930 struct vattr va; 931 int error; 932 933 if ((error = VOP_GETATTR(ap->a_vp, &va, ap->a_cred)) != 0) 934 return (error); 935 936 return (vaccess(va.va_type, va.va_mode, 937 va.va_uid, va.va_gid, ap->a_mode, ap->a_cred)); 938 } 939 940 /* 941 * lookup. this is incredibly complicated in the 942 * general case, however for most pseudo-filesystems 943 * very little needs to be done. 944 * 945 * Locking isn't hard here, just poorly documented. 946 * 947 * If we're looking up ".", just vref the parent & return it. 948 * 949 * If we're looking up "..", unlock the parent, and lock "..". If everything 950 * went ok, and we're on the last component and the caller requested the 951 * parent locked, try to re-lock the parent. We do this to prevent lock 952 * races. 953 * 954 * For anything else, get the needed node. Then unlock the parent if not 955 * the last component or not LOCKPARENT (i.e. if we wouldn't re-lock the 956 * parent in the .. case). 957 * 958 * We try to exit with the parent locked in error cases. 959 */ 960 int 961 procfs_lookup(void *v) 962 { 963 struct vop_lookup_args /* { 964 struct vnode * a_dvp; 965 struct vnode ** a_vpp; 966 struct componentname * a_cnp; 967 } */ *ap = v; 968 struct componentname *cnp = ap->a_cnp; 969 struct vnode **vpp = ap->a_vpp; 970 struct vnode *dvp = ap->a_dvp; 971 const char *pname = cnp->cn_nameptr; 972 const struct proc_target *pt = NULL; 973 struct vnode *fvp; 974 pid_t pid, vnpid; 975 struct pfsnode *pfs; 976 struct proc *p = NULL; 977 struct lwp *plwp; 978 int i, error; 979 pfstype type; 980 981 *vpp = NULL; 982 983 if (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME) 984 return (EROFS); 985 986 if (cnp->cn_namelen == 1 && *pname == '.') { 987 *vpp = dvp; 988 VREF(dvp); 989 return (0); 990 } 991 992 pfs = VTOPFS(dvp); 993 switch (pfs->pfs_type) { 994 case PFSroot: 995 /* 996 * Shouldn't get here with .. in the root node. 997 */ 998 if (cnp->cn_flags & ISDOTDOT) 999 return (EIO); 1000 1001 for (i = 0; i < nproc_root_targets; i++) { 1002 pt = &proc_root_targets[i]; 1003 /* 1004 * check for node match. proc is always NULL here, 1005 * so call pt_valid with constant NULL lwp. 1006 */ 1007 if (cnp->cn_namelen == pt->pt_namlen && 1008 memcmp(pt->pt_name, pname, cnp->cn_namelen) == 0 && 1009 (pt->pt_valid == NULL || 1010 (*pt->pt_valid)(NULL, dvp->v_mount))) 1011 break; 1012 } 1013 1014 if (i != nproc_root_targets) { 1015 error = procfs_allocvp(dvp->v_mount, vpp, 0, 1016 pt->pt_pfstype, -1, NULL); 1017 return (error); 1018 } 1019 1020 if (CNEQ(cnp, "curproc", 7)) { 1021 pid = curproc->p_pid; 1022 vnpid = 0; 1023 type = PFScurproc; 1024 } else if (CNEQ(cnp, "self", 4)) { 1025 pid = curproc->p_pid; 1026 vnpid = 0; 1027 type = PFSself; 1028 } else { 1029 pid = (pid_t)atoi(pname, cnp->cn_namelen); 1030 vnpid = pid; 1031 type = PFSproc; 1032 } 1033 1034 if (procfs_proc_lock(pid, &p, ESRCH) != 0) 1035 break; 1036 error = procfs_allocvp(dvp->v_mount, vpp, vnpid, type, -1, p); 1037 procfs_proc_unlock(p); 1038 return (error); 1039 1040 case PFSproc: 1041 /* 1042 * do the .. dance. We unlock the directory, and then 1043 * get the root dir. That will automatically return .. 1044 * locked. Then if the caller wanted dvp locked, we 1045 * re-lock. 1046 */ 1047 if (cnp->cn_flags & ISDOTDOT) { 1048 VOP_UNLOCK(dvp, 0); 1049 error = procfs_root(dvp->v_mount, vpp); 1050 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY); 1051 return (error); 1052 } 1053 1054 if (procfs_proc_lock(pfs->pfs_pid, &p, ESRCH) != 0) 1055 break; 1056 1057 mutex_enter(p->p_lock); 1058 LIST_FOREACH(plwp, &p->p_lwps, l_sibling) { 1059 if (plwp->l_stat != LSZOMB) 1060 break; 1061 } 1062 /* Process is exiting if no-LWPS or all LWPs are LSZOMB */ 1063 if (plwp == NULL) { 1064 mutex_exit(p->p_lock); 1065 procfs_proc_unlock(p); 1066 return ESRCH; 1067 } 1068 1069 lwp_addref(plwp); 1070 mutex_exit(p->p_lock); 1071 1072 for (pt = proc_targets, i = 0; i < nproc_targets; pt++, i++) { 1073 int found; 1074 1075 found = cnp->cn_namelen == pt->pt_namlen && 1076 memcmp(pt->pt_name, pname, cnp->cn_namelen) == 0 && 1077 (pt->pt_valid == NULL 1078 || (*pt->pt_valid)(plwp, dvp->v_mount)); 1079 if (found) 1080 break; 1081 } 1082 lwp_delref(plwp); 1083 1084 if (i == nproc_targets) { 1085 procfs_proc_unlock(p); 1086 break; 1087 } 1088 if (pt->pt_pfstype == PFSfile) { 1089 fvp = p->p_textvp; 1090 /* We already checked that it exists. */ 1091 VREF(fvp); 1092 procfs_proc_unlock(p); 1093 vn_lock(fvp, LK_EXCLUSIVE | LK_RETRY); 1094 *vpp = fvp; 1095 return (0); 1096 } 1097 1098 error = procfs_allocvp(dvp->v_mount, vpp, pfs->pfs_pid, 1099 pt->pt_pfstype, -1, p); 1100 procfs_proc_unlock(p); 1101 return (error); 1102 1103 case PFSfd: { 1104 int fd; 1105 file_t *fp; 1106 1107 if ((error = procfs_proc_lock(pfs->pfs_pid, &p, ENOENT)) != 0) 1108 return error; 1109 1110 /* 1111 * do the .. dance. We unlock the directory, and then 1112 * get the proc dir. That will automatically return .. 1113 * locked. Then re-lock the directory. 1114 */ 1115 if (cnp->cn_flags & ISDOTDOT) { 1116 VOP_UNLOCK(dvp, 0); 1117 error = procfs_allocvp(dvp->v_mount, vpp, pfs->pfs_pid, 1118 PFSproc, -1, p); 1119 procfs_proc_unlock(p); 1120 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY); 1121 return (error); 1122 } 1123 fd = atoi(pname, cnp->cn_namelen); 1124 1125 fp = fd_getfile2(p, fd); 1126 if (fp == NULL) { 1127 procfs_proc_unlock(p); 1128 return ENOENT; 1129 } 1130 fvp = fp->f_data; 1131 1132 /* Don't show directories */ 1133 if (fp->f_type == DTYPE_VNODE && fvp->v_type != VDIR) { 1134 VREF(fvp); 1135 closef(fp); 1136 procfs_proc_unlock(p); 1137 vn_lock(fvp, LK_EXCLUSIVE | LK_RETRY | 1138 (p == curproc ? LK_CANRECURSE : 0)); 1139 *vpp = fvp; 1140 return 0; 1141 } 1142 1143 closef(fp); 1144 error = procfs_allocvp(dvp->v_mount, vpp, pfs->pfs_pid, 1145 PFSfd, fd, p); 1146 procfs_proc_unlock(p); 1147 return error; 1148 } 1149 default: 1150 return (ENOTDIR); 1151 } 1152 1153 return (cnp->cn_nameiop == LOOKUP ? ENOENT : EROFS); 1154 } 1155 1156 int 1157 procfs_validfile(struct lwp *l, struct mount *mp) 1158 { 1159 return l != NULL && l->l_proc != NULL && l->l_proc->p_textvp != NULL; 1160 } 1161 1162 static int 1163 procfs_validfile_linux(struct lwp *l, struct mount *mp) 1164 { 1165 int flags; 1166 1167 flags = VFSTOPROC(mp)->pmnt_flags; 1168 return (flags & PROCFSMNT_LINUXCOMPAT) && 1169 (l == NULL || l->l_proc == NULL || procfs_validfile(l, mp)); 1170 } 1171 1172 struct procfs_root_readdir_ctx { 1173 struct uio *uiop; 1174 off_t *cookies; 1175 int ncookies; 1176 off_t off; 1177 off_t startoff; 1178 int error; 1179 }; 1180 1181 static int 1182 procfs_root_readdir_callback(struct proc *p, void *arg) 1183 { 1184 struct procfs_root_readdir_ctx *ctxp = arg; 1185 struct dirent d; 1186 struct uio *uiop; 1187 int error; 1188 1189 uiop = ctxp->uiop; 1190 if (uiop->uio_resid < UIO_MX) 1191 return -1; /* no space */ 1192 1193 if (ctxp->off < ctxp->startoff) { 1194 ctxp->off++; 1195 return 0; 1196 } 1197 1198 if (kauth_authorize_process(kauth_cred_get(), 1199 KAUTH_PROCESS_CANSEE, p, 1200 KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_ENTRY), NULL, NULL) != 0) 1201 return 0; 1202 1203 memset(&d, 0, UIO_MX); 1204 d.d_reclen = UIO_MX; 1205 d.d_fileno = PROCFS_FILENO(p->p_pid, PFSproc, -1); 1206 d.d_namlen = snprintf(d.d_name, 1207 UIO_MX - offsetof(struct dirent, d_name), "%ld", (long)p->p_pid); 1208 d.d_type = DT_DIR; 1209 1210 mutex_exit(proc_lock); 1211 error = uiomove(&d, UIO_MX, uiop); 1212 mutex_enter(proc_lock); 1213 if (error) { 1214 ctxp->error = error; 1215 return -1; 1216 } 1217 1218 ctxp->ncookies++; 1219 if (ctxp->cookies) 1220 *(ctxp->cookies)++ = ctxp->off + 1; 1221 ctxp->off++; 1222 1223 return 0; 1224 } 1225 1226 /* 1227 * readdir returns directory entries from pfsnode (vp). 1228 * 1229 * the strategy here with procfs is to generate a single 1230 * directory entry at a time (struct dirent) and then 1231 * copy that out to userland using uiomove. a more efficent 1232 * though more complex implementation, would try to minimize 1233 * the number of calls to uiomove(). for procfs, this is 1234 * hardly worth the added code complexity. 1235 * 1236 * this should just be done through read() 1237 */ 1238 int 1239 procfs_readdir(void *v) 1240 { 1241 struct vop_readdir_args /* { 1242 struct vnode *a_vp; 1243 struct uio *a_uio; 1244 kauth_cred_t a_cred; 1245 int *a_eofflag; 1246 off_t **a_cookies; 1247 int *a_ncookies; 1248 } */ *ap = v; 1249 struct uio *uio = ap->a_uio; 1250 struct dirent d; 1251 struct pfsnode *pfs; 1252 off_t i; 1253 int error; 1254 off_t *cookies = NULL; 1255 int ncookies; 1256 struct vnode *vp; 1257 const struct proc_target *pt; 1258 struct procfs_root_readdir_ctx ctx; 1259 struct lwp *l; 1260 int nfd; 1261 1262 vp = ap->a_vp; 1263 pfs = VTOPFS(vp); 1264 1265 if (uio->uio_resid < UIO_MX) 1266 return (EINVAL); 1267 if (uio->uio_offset < 0) 1268 return (EINVAL); 1269 1270 error = 0; 1271 i = uio->uio_offset; 1272 memset(&d, 0, UIO_MX); 1273 d.d_reclen = UIO_MX; 1274 ncookies = uio->uio_resid / UIO_MX; 1275 1276 switch (pfs->pfs_type) { 1277 /* 1278 * this is for the process-specific sub-directories. 1279 * all that is needed to is copy out all the entries 1280 * from the procent[] table (top of this file). 1281 */ 1282 case PFSproc: { 1283 struct proc *p; 1284 1285 if (i >= nproc_targets) 1286 return 0; 1287 1288 if (procfs_proc_lock(pfs->pfs_pid, &p, ESRCH) != 0) 1289 break; 1290 1291 if (ap->a_ncookies) { 1292 ncookies = min(ncookies, (nproc_targets - i)); 1293 cookies = malloc(ncookies * sizeof (off_t), 1294 M_TEMP, M_WAITOK); 1295 *ap->a_cookies = cookies; 1296 } 1297 1298 for (pt = &proc_targets[i]; 1299 uio->uio_resid >= UIO_MX && i < nproc_targets; pt++, i++) { 1300 if (pt->pt_valid) { 1301 /* XXXSMP LWP can disappear */ 1302 mutex_enter(p->p_lock); 1303 l = LIST_FIRST(&p->p_lwps); 1304 KASSERT(l != NULL); 1305 mutex_exit(p->p_lock); 1306 if ((*pt->pt_valid)(l, vp->v_mount) == 0) 1307 continue; 1308 } 1309 1310 d.d_fileno = PROCFS_FILENO(pfs->pfs_pid, 1311 pt->pt_pfstype, -1); 1312 d.d_namlen = pt->pt_namlen; 1313 memcpy(d.d_name, pt->pt_name, pt->pt_namlen + 1); 1314 d.d_type = pt->pt_type; 1315 1316 if ((error = uiomove(&d, UIO_MX, uio)) != 0) 1317 break; 1318 if (cookies) 1319 *cookies++ = i + 1; 1320 } 1321 1322 procfs_proc_unlock(p); 1323 break; 1324 } 1325 case PFSfd: { 1326 struct proc *p; 1327 file_t *fp; 1328 int lim, nc = 0; 1329 1330 if ((error = procfs_proc_lock(pfs->pfs_pid, &p, ESRCH)) != 0) 1331 return error; 1332 1333 /* XXX Should this be by file as well? */ 1334 if (kauth_authorize_process(kauth_cred_get(), 1335 KAUTH_PROCESS_CANSEE, p, 1336 KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_OPENFILES), NULL, 1337 NULL) != 0) { 1338 procfs_proc_unlock(p); 1339 return ESRCH; 1340 } 1341 1342 nfd = p->p_fd->fd_nfiles; 1343 1344 lim = min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfiles); 1345 if (i >= lim) { 1346 procfs_proc_unlock(p); 1347 return 0; 1348 } 1349 1350 if (ap->a_ncookies) { 1351 ncookies = min(ncookies, (nfd + 2 - i)); 1352 cookies = malloc(ncookies * sizeof (off_t), 1353 M_TEMP, M_WAITOK); 1354 *ap->a_cookies = cookies; 1355 } 1356 1357 for (; i < 2 && uio->uio_resid >= UIO_MX; i++) { 1358 pt = &proc_targets[i]; 1359 d.d_namlen = pt->pt_namlen; 1360 d.d_fileno = PROCFS_FILENO(pfs->pfs_pid, 1361 pt->pt_pfstype, -1); 1362 (void)memcpy(d.d_name, pt->pt_name, pt->pt_namlen + 1); 1363 d.d_type = pt->pt_type; 1364 if ((error = uiomove(&d, UIO_MX, uio)) != 0) 1365 break; 1366 if (cookies) 1367 *cookies++ = i + 1; 1368 nc++; 1369 } 1370 if (error) { 1371 ncookies = nc; 1372 break; 1373 } 1374 for (; uio->uio_resid >= UIO_MX && i < nfd; i++) { 1375 /* check the descriptor exists */ 1376 if ((fp = fd_getfile2(p, i - 2)) == NULL) 1377 continue; 1378 closef(fp); 1379 1380 d.d_fileno = PROCFS_FILENO(pfs->pfs_pid, PFSfd, i - 2); 1381 d.d_namlen = snprintf(d.d_name, sizeof(d.d_name), 1382 "%lld", (long long)(i - 2)); 1383 d.d_type = VREG; 1384 if ((error = uiomove(&d, UIO_MX, uio)) != 0) 1385 break; 1386 if (cookies) 1387 *cookies++ = i + 1; 1388 nc++; 1389 } 1390 ncookies = nc; 1391 procfs_proc_unlock(p); 1392 break; 1393 } 1394 1395 /* 1396 * this is for the root of the procfs filesystem 1397 * what is needed are special entries for "curproc" 1398 * and "self" followed by an entry for each process 1399 * on allproc. 1400 */ 1401 1402 case PFSroot: { 1403 int nc = 0; 1404 1405 if (ap->a_ncookies) { 1406 /* 1407 * XXX Potentially allocating too much space here, 1408 * but I'm lazy. This loop needs some work. 1409 */ 1410 cookies = malloc(ncookies * sizeof (off_t), 1411 M_TEMP, M_WAITOK); 1412 *ap->a_cookies = cookies; 1413 } 1414 error = 0; 1415 /* 0 ... 3 are static entries. */ 1416 for (; i <= 3 && uio->uio_resid >= UIO_MX; i++) { 1417 switch (i) { 1418 case 0: /* `.' */ 1419 case 1: /* `..' */ 1420 d.d_fileno = PROCFS_FILENO(0, PFSroot, -1); 1421 d.d_namlen = i + 1; 1422 memcpy(d.d_name, "..", d.d_namlen); 1423 d.d_name[i + 1] = '\0'; 1424 d.d_type = DT_DIR; 1425 break; 1426 1427 case 2: 1428 d.d_fileno = PROCFS_FILENO(0, PFScurproc, -1); 1429 d.d_namlen = sizeof("curproc") - 1; 1430 memcpy(d.d_name, "curproc", sizeof("curproc")); 1431 d.d_type = DT_LNK; 1432 break; 1433 1434 case 3: 1435 d.d_fileno = PROCFS_FILENO(0, PFSself, -1); 1436 d.d_namlen = sizeof("self") - 1; 1437 memcpy(d.d_name, "self", sizeof("self")); 1438 d.d_type = DT_LNK; 1439 break; 1440 } 1441 1442 if ((error = uiomove(&d, UIO_MX, uio)) != 0) 1443 break; 1444 nc++; 1445 if (cookies) 1446 *cookies++ = i + 1; 1447 } 1448 /* 4 ... are process entries. */ 1449 ctx.uiop = uio; 1450 ctx.error = 0; 1451 ctx.off = 4; 1452 ctx.startoff = i; 1453 ctx.cookies = cookies; 1454 ctx.ncookies = nc; 1455 proclist_foreach_call(&allproc, 1456 procfs_root_readdir_callback, &ctx); 1457 cookies = ctx.cookies; 1458 nc = ctx.ncookies; 1459 error = ctx.error; 1460 if (error) 1461 break; 1462 1463 /* misc entries. */ 1464 if (i < ctx.off) 1465 i = ctx.off; 1466 if (i >= ctx.off + nproc_root_targets) 1467 break; 1468 for (pt = &proc_root_targets[i - ctx.off]; 1469 uio->uio_resid >= UIO_MX && 1470 pt < &proc_root_targets[nproc_root_targets]; 1471 pt++, i++) { 1472 if (pt->pt_valid && 1473 (*pt->pt_valid)(NULL, vp->v_mount) == 0) 1474 continue; 1475 d.d_fileno = PROCFS_FILENO(0, pt->pt_pfstype, -1); 1476 d.d_namlen = pt->pt_namlen; 1477 memcpy(d.d_name, pt->pt_name, pt->pt_namlen + 1); 1478 d.d_type = pt->pt_type; 1479 1480 if ((error = uiomove(&d, UIO_MX, uio)) != 0) 1481 break; 1482 nc++; 1483 if (cookies) 1484 *cookies++ = i + 1; 1485 } 1486 1487 ncookies = nc; 1488 break; 1489 } 1490 1491 default: 1492 error = ENOTDIR; 1493 break; 1494 } 1495 1496 if (ap->a_ncookies) { 1497 if (error) { 1498 if (cookies) 1499 free(*ap->a_cookies, M_TEMP); 1500 *ap->a_ncookies = 0; 1501 *ap->a_cookies = NULL; 1502 } else 1503 *ap->a_ncookies = ncookies; 1504 } 1505 uio->uio_offset = i; 1506 return (error); 1507 } 1508 1509 /* 1510 * readlink reads the link of `curproc' and others 1511 */ 1512 int 1513 procfs_readlink(void *v) 1514 { 1515 struct vop_readlink_args *ap = v; 1516 char bf[16]; /* should be enough */ 1517 char *bp = bf; 1518 char *path = NULL; 1519 int len = 0; 1520 int error = 0; 1521 struct pfsnode *pfs = VTOPFS(ap->a_vp); 1522 struct proc *pown; 1523 1524 if (pfs->pfs_fileno == PROCFS_FILENO(0, PFScurproc, -1)) 1525 len = snprintf(bf, sizeof(bf), "%ld", (long)curproc->p_pid); 1526 else if (pfs->pfs_fileno == PROCFS_FILENO(0, PFSself, -1)) 1527 len = snprintf(bf, sizeof(bf), "%s", "curproc"); 1528 else if (pfs->pfs_fileno == PROCFS_FILENO(pfs->pfs_pid, PFScwd, -1) || 1529 pfs->pfs_fileno == PROCFS_FILENO(pfs->pfs_pid, PFSchroot, -1) || 1530 pfs->pfs_fileno == PROCFS_FILENO(pfs->pfs_pid, PFSexe, -1)) { 1531 if ((error = procfs_proc_lock(pfs->pfs_pid, &pown, ESRCH)) != 0) 1532 return error; 1533 MALLOC(path, char *, MAXPATHLEN + 4, M_TEMP, 1534 M_WAITOK|M_CANFAIL); 1535 if (path == NULL) { 1536 procfs_proc_unlock(pown); 1537 return (ENOMEM); 1538 } 1539 bp = path + MAXPATHLEN; 1540 *--bp = '\0'; 1541 procfs_dir(PROCFS_TYPE(pfs->pfs_fileno), curlwp, pown, 1542 &bp, path, MAXPATHLEN); 1543 procfs_proc_unlock(pown); 1544 len = strlen(bp); 1545 } else { 1546 file_t *fp; 1547 struct vnode *vxp, *vp; 1548 1549 if ((error = procfs_proc_lock(pfs->pfs_pid, &pown, ESRCH)) != 0) 1550 return error; 1551 1552 fp = fd_getfile2(pown, pfs->pfs_fd); 1553 if (fp == NULL) { 1554 procfs_proc_unlock(pown); 1555 return EBADF; 1556 } 1557 1558 switch (fp->f_type) { 1559 case DTYPE_VNODE: 1560 vxp = (struct vnode *)fp->f_data; 1561 if (vxp->v_type != VDIR) { 1562 error = EINVAL; 1563 break; 1564 } 1565 if ((path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK)) 1566 == NULL) { 1567 error = ENOMEM; 1568 break; 1569 } 1570 bp = path + MAXPATHLEN; 1571 *--bp = '\0'; 1572 1573 /* 1574 * XXX: kludge to avoid locking against ourselves 1575 * in getcwd() 1576 */ 1577 if (vxp->v_tag == VT_PROCFS) { 1578 *--bp = '/'; 1579 } else { 1580 rw_enter(&curproc->p_cwdi->cwdi_lock, RW_READER); 1581 vp = curproc->p_cwdi->cwdi_rdir; 1582 if (vp == NULL) 1583 vp = rootvnode; 1584 error = getcwd_common(vxp, vp, &bp, path, 1585 MAXPATHLEN / 2, 0, curlwp); 1586 rw_exit(&curproc->p_cwdi->cwdi_lock); 1587 } 1588 if (error) 1589 break; 1590 len = strlen(bp); 1591 break; 1592 1593 case DTYPE_MISC: 1594 len = snprintf(bf, sizeof(bf), "%s", "[misc]"); 1595 break; 1596 1597 case DTYPE_KQUEUE: 1598 len = snprintf(bf, sizeof(bf), "%s", "[kqueue]"); 1599 break; 1600 1601 default: 1602 error = EINVAL; 1603 break; 1604 } 1605 closef(fp); 1606 procfs_proc_unlock(pown); 1607 } 1608 1609 if (error == 0) 1610 error = uiomove(bp, len, ap->a_uio); 1611 if (path) 1612 free(path, M_TEMP); 1613 return error; 1614 } 1615 1616 /* 1617 * convert decimal ascii to int 1618 */ 1619 static int 1620 atoi(const char *b, size_t len) 1621 { 1622 int p = 0; 1623 1624 while (len--) { 1625 char c = *b++; 1626 if (c < '0' || c > '9') 1627 return -1; 1628 p = 10 * p + (c - '0'); 1629 } 1630 1631 return p; 1632 } 1633