1 /* $NetBSD: procfs_vnops.c,v 1.61 1999/03/12 18:45:40 christos Exp $ */ 2 3 /* 4 * Copyright (c) 1993 Jan-Simon Pendry 5 * Copyright (c) 1993, 1995 6 * The Regents of the University of California. All rights reserved. 7 * 8 * This code is derived from software contributed to Berkeley by 9 * Jan-Simon Pendry. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. All advertising materials mentioning features or use of this software 20 * must display the following acknowledgement: 21 * This product includes software developed by the University of 22 * California, Berkeley and its contributors. 23 * 4. Neither the name of the University nor the names of its contributors 24 * may be used to endorse or promote products derived from this software 25 * without specific prior written permission. 26 * 27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 37 * SUCH DAMAGE. 38 * 39 * @(#)procfs_vnops.c 8.18 (Berkeley) 5/21/95 40 */ 41 42 /* 43 * procfs vnode interface 44 */ 45 46 #include <sys/param.h> 47 #include <sys/systm.h> 48 #include <sys/time.h> 49 #include <sys/kernel.h> 50 #include <sys/file.h> 51 #include <sys/proc.h> 52 #include <sys/vnode.h> 53 #include <sys/namei.h> 54 #include <sys/malloc.h> 55 #include <sys/dirent.h> 56 #include <sys/resourcevar.h> 57 #include <sys/ptrace.h> 58 #include <sys/stat.h> 59 60 #include <vm/vm.h> /* for PAGE_SIZE */ 61 62 #include <machine/reg.h> 63 64 #include <miscfs/genfs/genfs.h> 65 #include <miscfs/procfs/procfs.h> 66 67 /* 68 * Vnode Operations. 69 * 70 */ 71 72 /* 73 * This is a list of the valid names in the 74 * process-specific sub-directories. It is 75 * used in procfs_lookup and procfs_readdir 76 */ 77 struct proc_target { 78 u_char pt_type; 79 u_char pt_namlen; 80 char *pt_name; 81 pfstype pt_pfstype; 82 int (*pt_valid) __P((struct proc *p)); 83 } proc_targets[] = { 84 #define N(s) sizeof(s)-1, s 85 /* name type validp */ 86 { DT_DIR, N("."), Pproc, NULL }, 87 { DT_DIR, N(".."), Proot, NULL }, 88 { DT_REG, N("file"), Pfile, procfs_validfile }, 89 { DT_REG, N("mem"), Pmem, NULL }, 90 { DT_REG, N("regs"), Pregs, procfs_validregs }, 91 { DT_REG, N("fpregs"), Pfpregs, procfs_validfpregs }, 92 { DT_REG, N("ctl"), Pctl, NULL }, 93 { DT_REG, N("status"), Pstatus, NULL }, 94 { DT_REG, N("note"), Pnote, NULL }, 95 { DT_REG, N("notepg"), Pnotepg, NULL }, 96 { DT_REG, N("map"), Pmap, procfs_validmap }, 97 { DT_REG, N("cmdline"), Pcmdline, NULL }, 98 #undef N 99 }; 100 static int nproc_targets = sizeof(proc_targets) / sizeof(proc_targets[0]); 101 102 static pid_t atopid __P((const char *, u_int)); 103 104 int procfs_lookup __P((void *)); 105 #define procfs_create genfs_eopnotsupp 106 #define procfs_mknod genfs_eopnotsupp 107 int procfs_open __P((void *)); 108 int procfs_close __P((void *)); 109 int procfs_access __P((void *)); 110 int procfs_getattr __P((void *)); 111 int procfs_setattr __P((void *)); 112 #define procfs_read procfs_rw 113 #define procfs_write procfs_rw 114 #define procfs_ioctl genfs_enoioctl 115 #define procfs_poll genfs_poll 116 #define procfs_revoke genfs_revoke 117 #define procfs_mmap genfs_eopnotsupp 118 #define procfs_fsync genfs_nullop 119 #define procfs_seek genfs_nullop 120 #define procfs_remove genfs_eopnotsupp 121 int procfs_link __P((void *)); 122 #define procfs_rename genfs_eopnotsupp 123 #define procfs_mkdir genfs_eopnotsupp 124 #define procfs_rmdir genfs_eopnotsupp 125 int procfs_symlink __P((void *)); 126 int procfs_readdir __P((void *)); 127 int procfs_readlink __P((void *)); 128 #define procfs_abortop genfs_abortop 129 int procfs_inactive __P((void *)); 130 int procfs_reclaim __P((void *)); 131 #define procfs_lock genfs_nolock 132 #define procfs_unlock genfs_nounlock 133 int procfs_bmap __P((void *)); 134 #define procfs_strategy genfs_badop 135 int procfs_print __P((void *)); 136 int procfs_pathconf __P((void *)); 137 #define procfs_islocked genfs_noislocked 138 #define procfs_advlock genfs_einval 139 #define procfs_blkatoff genfs_eopnotsupp 140 #define procfs_valloc genfs_eopnotsupp 141 #define procfs_vfree genfs_nullop 142 #define procfs_truncate genfs_eopnotsupp 143 #define procfs_update genfs_nullop 144 #define procfs_bwrite genfs_eopnotsupp 145 146 static pid_t atopid __P((const char *, u_int)); 147 148 /* 149 * procfs vnode operations. 150 */ 151 int (**procfs_vnodeop_p) __P((void *)); 152 struct vnodeopv_entry_desc procfs_vnodeop_entries[] = { 153 { &vop_default_desc, vn_default_error }, 154 { &vop_lookup_desc, procfs_lookup }, /* lookup */ 155 { &vop_create_desc, procfs_create }, /* create */ 156 { &vop_mknod_desc, procfs_mknod }, /* mknod */ 157 { &vop_open_desc, procfs_open }, /* open */ 158 { &vop_close_desc, procfs_close }, /* close */ 159 { &vop_access_desc, procfs_access }, /* access */ 160 { &vop_getattr_desc, procfs_getattr }, /* getattr */ 161 { &vop_setattr_desc, procfs_setattr }, /* setattr */ 162 { &vop_read_desc, procfs_read }, /* read */ 163 { &vop_write_desc, procfs_write }, /* write */ 164 { &vop_ioctl_desc, procfs_ioctl }, /* ioctl */ 165 { &vop_poll_desc, procfs_poll }, /* poll */ 166 { &vop_revoke_desc, procfs_revoke }, /* revoke */ 167 { &vop_mmap_desc, procfs_mmap }, /* mmap */ 168 { &vop_fsync_desc, procfs_fsync }, /* fsync */ 169 { &vop_seek_desc, procfs_seek }, /* seek */ 170 { &vop_remove_desc, procfs_remove }, /* remove */ 171 { &vop_link_desc, procfs_link }, /* link */ 172 { &vop_rename_desc, procfs_rename }, /* rename */ 173 { &vop_mkdir_desc, procfs_mkdir }, /* mkdir */ 174 { &vop_rmdir_desc, procfs_rmdir }, /* rmdir */ 175 { &vop_symlink_desc, procfs_symlink }, /* symlink */ 176 { &vop_readdir_desc, procfs_readdir }, /* readdir */ 177 { &vop_readlink_desc, procfs_readlink }, /* readlink */ 178 { &vop_abortop_desc, procfs_abortop }, /* abortop */ 179 { &vop_inactive_desc, procfs_inactive }, /* inactive */ 180 { &vop_reclaim_desc, procfs_reclaim }, /* reclaim */ 181 { &vop_lock_desc, procfs_lock }, /* lock */ 182 { &vop_unlock_desc, procfs_unlock }, /* unlock */ 183 { &vop_bmap_desc, procfs_bmap }, /* bmap */ 184 { &vop_strategy_desc, procfs_strategy }, /* strategy */ 185 { &vop_print_desc, procfs_print }, /* print */ 186 { &vop_islocked_desc, procfs_islocked }, /* islocked */ 187 { &vop_pathconf_desc, procfs_pathconf }, /* pathconf */ 188 { &vop_advlock_desc, procfs_advlock }, /* advlock */ 189 { &vop_blkatoff_desc, procfs_blkatoff }, /* blkatoff */ 190 { &vop_valloc_desc, procfs_valloc }, /* valloc */ 191 { &vop_vfree_desc, procfs_vfree }, /* vfree */ 192 { &vop_truncate_desc, procfs_truncate }, /* truncate */ 193 { &vop_update_desc, procfs_update }, /* update */ 194 { (struct vnodeop_desc*)NULL, (int(*) __P((void *)))NULL } 195 }; 196 struct vnodeopv_desc procfs_vnodeop_opv_desc = 197 { &procfs_vnodeop_p, procfs_vnodeop_entries }; 198 /* 199 * set things up for doing i/o on 200 * the pfsnode (vp). (vp) is locked 201 * on entry, and should be left locked 202 * on exit. 203 * 204 * for procfs we don't need to do anything 205 * in particular for i/o. all that is done 206 * is to support exclusive open on process 207 * memory images. 208 */ 209 int 210 procfs_open(v) 211 void *v; 212 { 213 struct vop_open_args /* { 214 struct vnode *a_vp; 215 int a_mode; 216 struct ucred *a_cred; 217 struct proc *a_p; 218 } */ *ap = v; 219 struct pfsnode *pfs = VTOPFS(ap->a_vp); 220 struct proc *p1, *p2; 221 int error; 222 223 p1 = ap->a_p; /* tracer */ 224 p2 = PFIND(pfs->pfs_pid); /* traced */ 225 226 if (p2 == NULL) 227 return (ENOENT); /* was ESRCH, jsp */ 228 229 switch (pfs->pfs_type) { 230 case Pmem: 231 if (((pfs->pfs_flags & FWRITE) && (ap->a_mode & O_EXCL)) || 232 ((pfs->pfs_flags & O_EXCL) && (ap->a_mode & FWRITE))) 233 return (EBUSY); 234 235 if ((error = procfs_checkioperm(p1, p2)) != 0) 236 return (EPERM); 237 238 if (ap->a_mode & FWRITE) 239 pfs->pfs_flags = ap->a_mode & (FWRITE|O_EXCL); 240 241 return (0); 242 243 default: 244 break; 245 } 246 247 return (0); 248 } 249 250 /* 251 * close the pfsnode (vp) after doing i/o. 252 * (vp) is not locked on entry or exit. 253 * 254 * nothing to do for procfs other than undo 255 * any exclusive open flag (see _open above). 256 */ 257 int 258 procfs_close(v) 259 void *v; 260 { 261 struct vop_close_args /* { 262 struct vnode *a_vp; 263 int a_fflag; 264 struct ucred *a_cred; 265 struct proc *a_p; 266 } */ *ap = v; 267 struct pfsnode *pfs = VTOPFS(ap->a_vp); 268 269 switch (pfs->pfs_type) { 270 case Pmem: 271 if ((ap->a_fflag & FWRITE) && (pfs->pfs_flags & O_EXCL)) 272 pfs->pfs_flags &= ~(FWRITE|O_EXCL); 273 break; 274 275 default: 276 break; 277 } 278 279 return (0); 280 } 281 282 /* 283 * do block mapping for pfsnode (vp). 284 * since we don't use the buffer cache 285 * for procfs this function should never 286 * be called. in any case, it's not clear 287 * what part of the kernel ever makes use 288 * of this function. for sanity, this is the 289 * usual no-op bmap, although returning 290 * (EIO) would be a reasonable alternative. 291 */ 292 int 293 procfs_bmap(v) 294 void *v; 295 { 296 struct vop_bmap_args /* { 297 struct vnode *a_vp; 298 daddr_t a_bn; 299 struct vnode **a_vpp; 300 daddr_t *a_bnp; 301 int * a_runp; 302 } */ *ap = v; 303 304 if (ap->a_vpp != NULL) 305 *ap->a_vpp = ap->a_vp; 306 if (ap->a_bnp != NULL) 307 *ap->a_bnp = ap->a_bn; 308 if (ap->a_runp != NULL) 309 *ap->a_runp = 0; 310 return (0); 311 } 312 313 /* 314 * _inactive is called when the pfsnode 315 * is vrele'd and the reference count goes 316 * to zero. (vp) will be on the vnode free 317 * list, so to get it back vget() must be 318 * used. 319 * 320 * for procfs, check if the process is still 321 * alive and if it isn't then just throw away 322 * the vnode by calling vgone(). this may 323 * be overkill and a waste of time since the 324 * chances are that the process will still be 325 * there and PFIND is not free. 326 * 327 * (vp) is ocked on entry, but must be unlocked on exit. 328 */ 329 int 330 procfs_inactive(v) 331 void *v; 332 { 333 struct vop_inactive_args /* { 334 struct vnode *a_vp; 335 struct proc *a_p; 336 } */ *ap = v; 337 struct pfsnode *pfs = VTOPFS(ap->a_vp); 338 339 VOP_UNLOCK(ap->a_vp, 0); 340 if (PFIND(pfs->pfs_pid) == 0) 341 vgone(ap->a_vp); 342 343 return (0); 344 } 345 346 /* 347 * _reclaim is called when getnewvnode() 348 * wants to make use of an entry on the vnode 349 * free list. at this time the filesystem needs 350 * to free any private data and remove the node 351 * from any private lists. 352 */ 353 int 354 procfs_reclaim(v) 355 void *v; 356 { 357 struct vop_reclaim_args /* { 358 struct vnode *a_vp; 359 } */ *ap = v; 360 361 return (procfs_freevp(ap->a_vp)); 362 } 363 364 /* 365 * Return POSIX pathconf information applicable to special devices. 366 */ 367 int 368 procfs_pathconf(v) 369 void *v; 370 { 371 struct vop_pathconf_args /* { 372 struct vnode *a_vp; 373 int a_name; 374 register_t *a_retval; 375 } */ *ap = v; 376 377 switch (ap->a_name) { 378 case _PC_LINK_MAX: 379 *ap->a_retval = LINK_MAX; 380 return (0); 381 case _PC_MAX_CANON: 382 *ap->a_retval = MAX_CANON; 383 return (0); 384 case _PC_MAX_INPUT: 385 *ap->a_retval = MAX_INPUT; 386 return (0); 387 case _PC_PIPE_BUF: 388 *ap->a_retval = PIPE_BUF; 389 return (0); 390 case _PC_CHOWN_RESTRICTED: 391 *ap->a_retval = 1; 392 return (0); 393 case _PC_VDISABLE: 394 *ap->a_retval = _POSIX_VDISABLE; 395 return (0); 396 case _PC_SYNC_IO: 397 *ap->a_retval = 1; 398 return (0); 399 default: 400 return (EINVAL); 401 } 402 /* NOTREACHED */ 403 } 404 405 /* 406 * _print is used for debugging. 407 * just print a readable description 408 * of (vp). 409 */ 410 int 411 procfs_print(v) 412 void *v; 413 { 414 struct vop_print_args /* { 415 struct vnode *a_vp; 416 } */ *ap = v; 417 struct pfsnode *pfs = VTOPFS(ap->a_vp); 418 419 printf("tag VT_PROCFS, type %d, pid %d, mode %x, flags %lx\n", 420 pfs->pfs_type, pfs->pfs_pid, pfs->pfs_mode, pfs->pfs_flags); 421 return 0; 422 } 423 424 int 425 procfs_link(v) 426 void *v; 427 { 428 struct vop_link_args /* { 429 struct vnode *a_dvp; 430 struct vnode *a_vp; 431 struct componentname *a_cnp; 432 } */ *ap = v; 433 434 VOP_ABORTOP(ap->a_dvp, ap->a_cnp); 435 vput(ap->a_dvp); 436 return (EROFS); 437 } 438 439 int 440 procfs_symlink(v) 441 void *v; 442 { 443 struct vop_symlink_args /* { 444 struct vnode *a_dvp; 445 struct vnode **a_vpp; 446 struct componentname *a_cnp; 447 struct vattr *a_vap; 448 char *a_target; 449 } */ *ap = v; 450 451 VOP_ABORTOP(ap->a_dvp, ap->a_cnp); 452 vput(ap->a_dvp); 453 return (EROFS); 454 } 455 456 /* 457 * Invent attributes for pfsnode (vp) and store 458 * them in (vap). 459 * Directories lengths are returned as zero since 460 * any real length would require the genuine size 461 * to be computed, and nothing cares anyway. 462 * 463 * this is relatively minimal for procfs. 464 */ 465 int 466 procfs_getattr(v) 467 void *v; 468 { 469 struct vop_getattr_args /* { 470 struct vnode *a_vp; 471 struct vattr *a_vap; 472 struct ucred *a_cred; 473 struct proc *a_p; 474 } */ *ap = v; 475 struct pfsnode *pfs = VTOPFS(ap->a_vp); 476 struct vattr *vap = ap->a_vap; 477 struct proc *procp; 478 struct timeval tv; 479 int error; 480 481 /* first check the process still exists */ 482 switch (pfs->pfs_type) { 483 case Proot: 484 case Pcurproc: 485 procp = 0; 486 break; 487 488 default: 489 procp = PFIND(pfs->pfs_pid); 490 if (procp == 0) 491 return (ENOENT); 492 break; 493 } 494 495 error = 0; 496 497 /* start by zeroing out the attributes */ 498 VATTR_NULL(vap); 499 500 /* next do all the common fields */ 501 vap->va_type = ap->a_vp->v_type; 502 vap->va_mode = pfs->pfs_mode; 503 vap->va_fileid = pfs->pfs_fileno; 504 vap->va_flags = 0; 505 vap->va_blocksize = PAGE_SIZE; 506 507 /* 508 * Make all times be current TOD. 509 * It would be possible to get the process start 510 * time from the p_stat structure, but there's 511 * no "file creation" time stamp anyway, and the 512 * p_stat structure is not addressible if u. gets 513 * swapped out for that process. 514 */ 515 microtime(&tv); 516 TIMEVAL_TO_TIMESPEC(&tv, &vap->va_ctime); 517 vap->va_atime = vap->va_mtime = vap->va_ctime; 518 519 switch (pfs->pfs_type) { 520 case Pmem: 521 case Pregs: 522 case Pfpregs: 523 /* 524 * If the process has exercised some setuid or setgid 525 * privilege, then rip away read/write permission so 526 * that only root can gain access. 527 */ 528 if (procp->p_flag & P_SUGID) 529 vap->va_mode &= ~(S_IRUSR|S_IWUSR); 530 /* FALLTHROUGH */ 531 case Pctl: 532 case Pstatus: 533 case Pnote: 534 case Pnotepg: 535 case Pmap: 536 case Pcmdline: 537 vap->va_nlink = 1; 538 vap->va_uid = procp->p_ucred->cr_uid; 539 vap->va_gid = procp->p_ucred->cr_gid; 540 break; 541 542 default: 543 break; 544 } 545 546 /* 547 * now do the object specific fields 548 * 549 * The size could be set from struct reg, but it's hardly 550 * worth the trouble, and it puts some (potentially) machine 551 * dependent data into this machine-independent code. If it 552 * becomes important then this function should break out into 553 * a per-file stat function in the corresponding .c file. 554 */ 555 556 switch (pfs->pfs_type) { 557 case Proot: 558 /* 559 * Set nlink to 1 to tell fts(3) we don't actually know. 560 */ 561 vap->va_nlink = 1; 562 vap->va_uid = 0; 563 vap->va_gid = 0; 564 vap->va_bytes = vap->va_size = DEV_BSIZE; 565 break; 566 567 case Pcurproc: { 568 char buf[16]; /* should be enough */ 569 vap->va_nlink = 1; 570 vap->va_uid = 0; 571 vap->va_gid = 0; 572 vap->va_bytes = vap->va_size = 573 sprintf(buf, "%ld", (long)curproc->p_pid); 574 break; 575 } 576 577 case Pproc: 578 vap->va_nlink = 2; 579 vap->va_uid = procp->p_ucred->cr_uid; 580 vap->va_gid = procp->p_ucred->cr_gid; 581 vap->va_bytes = vap->va_size = DEV_BSIZE; 582 break; 583 584 case Pfile: 585 error = EOPNOTSUPP; 586 break; 587 588 case Pmem: 589 vap->va_bytes = vap->va_size = 590 ctob(procp->p_vmspace->vm_tsize + 591 procp->p_vmspace->vm_dsize + 592 procp->p_vmspace->vm_ssize); 593 break; 594 595 #if defined(PT_GETREGS) || defined(PT_SETREGS) 596 case Pregs: 597 vap->va_bytes = vap->va_size = sizeof(struct reg); 598 break; 599 #endif 600 601 #if defined(PT_GETFPREGS) || defined(PT_SETFPREGS) 602 case Pfpregs: 603 vap->va_bytes = vap->va_size = sizeof(struct fpreg); 604 break; 605 #endif 606 607 case Pctl: 608 case Pstatus: 609 case Pnote: 610 case Pnotepg: 611 case Pmap: 612 case Pcmdline: 613 vap->va_bytes = vap->va_size = 0; 614 break; 615 616 default: 617 panic("procfs_getattr"); 618 } 619 620 return (error); 621 } 622 623 /*ARGSUSED*/ 624 int 625 procfs_setattr(v) 626 void *v; 627 { 628 /* 629 * just fake out attribute setting 630 * it's not good to generate an error 631 * return, otherwise things like creat() 632 * will fail when they try to set the 633 * file length to 0. worse, this means 634 * that echo $note > /proc/$pid/note will fail. 635 */ 636 637 return (0); 638 } 639 640 /* 641 * implement access checking. 642 * 643 * actually, the check for super-user is slightly 644 * broken since it will allow read access to write-only 645 * objects. this doesn't cause any particular trouble 646 * but does mean that the i/o entry points need to check 647 * that the operation really does make sense. 648 */ 649 int 650 procfs_access(v) 651 void *v; 652 { 653 struct vop_access_args /* { 654 struct vnode *a_vp; 655 int a_mode; 656 struct ucred *a_cred; 657 struct proc *a_p; 658 } */ *ap = v; 659 struct vattr va; 660 int error; 661 662 if ((error = VOP_GETATTR(ap->a_vp, &va, ap->a_cred, ap->a_p)) != 0) 663 return (error); 664 665 return (vaccess(va.va_type, va.va_mode, 666 va.va_uid, va.va_gid, ap->a_mode, ap->a_cred)); 667 } 668 669 /* 670 * lookup. this is incredibly complicated in the 671 * general case, however for most pseudo-filesystems 672 * very little needs to be done. 673 * 674 * unless you want to get a migraine, just make sure your 675 * filesystem doesn't do any locking of its own. otherwise 676 * read and inwardly digest ufs_lookup(). 677 */ 678 int 679 procfs_lookup(v) 680 void *v; 681 { 682 struct vop_lookup_args /* { 683 struct vnode * a_dvp; 684 struct vnode ** a_vpp; 685 struct componentname * a_cnp; 686 } */ *ap = v; 687 struct componentname *cnp = ap->a_cnp; 688 struct vnode **vpp = ap->a_vpp; 689 struct vnode *dvp = ap->a_dvp; 690 const char *pname = cnp->cn_nameptr; 691 struct proc_target *pt; 692 struct vnode *fvp; 693 pid_t pid; 694 struct pfsnode *pfs; 695 struct proc *p; 696 int i; 697 698 *vpp = NULL; 699 700 if (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME) 701 return (EROFS); 702 703 if (cnp->cn_namelen == 1 && *pname == '.') { 704 *vpp = dvp; 705 VREF(dvp); 706 /* vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY, curp); */ 707 return (0); 708 } 709 710 pfs = VTOPFS(dvp); 711 switch (pfs->pfs_type) { 712 case Proot: 713 if (cnp->cn_flags & ISDOTDOT) 714 return (EIO); 715 716 if (CNEQ(cnp, "curproc", 7)) 717 return (procfs_allocvp(dvp->v_mount, vpp, 0, Pcurproc)); 718 719 pid = atopid(pname, cnp->cn_namelen); 720 if (pid == NO_PID) 721 break; 722 723 p = PFIND(pid); 724 if (p == 0) 725 break; 726 727 return (procfs_allocvp(dvp->v_mount, vpp, pid, Pproc)); 728 729 case Pproc: 730 if (cnp->cn_flags & ISDOTDOT) 731 return (procfs_root(dvp->v_mount, vpp)); 732 733 p = PFIND(pfs->pfs_pid); 734 if (p == 0) 735 break; 736 737 for (pt = proc_targets, i = 0; i < nproc_targets; pt++, i++) { 738 if (cnp->cn_namelen == pt->pt_namlen && 739 memcmp(pt->pt_name, pname, cnp->cn_namelen) == 0 && 740 (pt->pt_valid == NULL || (*pt->pt_valid)(p))) 741 goto found; 742 } 743 break; 744 745 found: 746 if (pt->pt_pfstype == Pfile) { 747 fvp = procfs_findtextvp(p); 748 /* We already checked that it exists. */ 749 VREF(fvp); 750 vn_lock(fvp, LK_EXCLUSIVE | LK_RETRY); 751 *vpp = fvp; 752 return (0); 753 } 754 755 return (procfs_allocvp(dvp->v_mount, vpp, pfs->pfs_pid, 756 pt->pt_pfstype)); 757 758 default: 759 return (ENOTDIR); 760 } 761 762 return (cnp->cn_nameiop == LOOKUP ? ENOENT : EROFS); 763 } 764 765 int 766 procfs_validfile(p) 767 struct proc *p; 768 { 769 770 return (procfs_findtextvp(p) != NULLVP); 771 } 772 773 /* 774 * readdir returns directory entries from pfsnode (vp). 775 * 776 * the strategy here with procfs is to generate a single 777 * directory entry at a time (struct dirent) and then 778 * copy that out to userland using uiomove. a more efficent 779 * though more complex implementation, would try to minimize 780 * the number of calls to uiomove(). for procfs, this is 781 * hardly worth the added code complexity. 782 * 783 * this should just be done through read() 784 */ 785 int 786 procfs_readdir(v) 787 void *v; 788 { 789 struct vop_readdir_args /* { 790 struct vnode *a_vp; 791 struct uio *a_uio; 792 struct ucred *a_cred; 793 int *a_eofflag; 794 off_t **a_cookies; 795 int *a_ncookies; 796 } */ *ap = v; 797 struct uio *uio = ap->a_uio; 798 struct dirent d; 799 struct pfsnode *pfs; 800 int i; 801 int error; 802 off_t *cookies = NULL; 803 int ncookies; 804 805 pfs = VTOPFS(ap->a_vp); 806 807 if (uio->uio_resid < UIO_MX) 808 return (EINVAL); 809 if (uio->uio_offset < 0) 810 return (EINVAL); 811 812 error = 0; 813 i = uio->uio_offset; 814 memset((caddr_t)&d, 0, UIO_MX); 815 d.d_reclen = UIO_MX; 816 ncookies = uio->uio_resid / UIO_MX; 817 818 switch (pfs->pfs_type) { 819 /* 820 * this is for the process-specific sub-directories. 821 * all that is needed to is copy out all the entries 822 * from the procent[] table (top of this file). 823 */ 824 case Pproc: { 825 struct proc *p; 826 struct proc_target *pt; 827 828 p = PFIND(pfs->pfs_pid); 829 if (p == NULL) 830 break; 831 832 if (ap->a_ncookies) { 833 ncookies = min(ncookies, (nproc_targets - i)); 834 MALLOC(cookies, off_t *, ncookies * sizeof (off_t), 835 M_TEMP, M_WAITOK); 836 *ap->a_cookies = cookies; 837 } 838 839 for (pt = &proc_targets[i]; 840 uio->uio_resid >= UIO_MX && i < nproc_targets; pt++, i++) { 841 if (pt->pt_valid && (*pt->pt_valid)(p) == 0) 842 continue; 843 844 d.d_fileno = PROCFS_FILENO(pfs->pfs_pid, pt->pt_pfstype); 845 d.d_namlen = pt->pt_namlen; 846 memcpy(d.d_name, pt->pt_name, pt->pt_namlen + 1); 847 d.d_type = pt->pt_type; 848 849 if ((error = uiomove((caddr_t)&d, UIO_MX, uio)) != 0) 850 break; 851 if (cookies) 852 *cookies++ = i + 1; 853 } 854 855 break; 856 } 857 858 /* 859 * this is for the root of the procfs filesystem 860 * what is needed is a special entry for "curproc" 861 * followed by an entry for each process on allproc 862 #ifdef PROCFS_ZOMBIE 863 * and deadproc and zombproc. 864 #endif 865 */ 866 867 case Proot: { 868 int pcnt = i, nc = 0; 869 const struct proclist_desc *pd; 870 volatile struct proc *p; 871 872 if (pcnt > 3) 873 pcnt = 3; 874 if (ap->a_ncookies) { 875 /* 876 * XXX Potentially allocating too much space here, 877 * but I'm lazy. This loop needs some work. 878 */ 879 MALLOC(cookies, off_t *, ncookies * sizeof (off_t), 880 M_TEMP, M_WAITOK); 881 *ap->a_cookies = cookies; 882 } 883 /* 884 * XXX: THIS LOOP ASSUMES THAT allproc IS THE FIRST 885 * PROCLIST IN THE proclists! 886 */ 887 pd = proclists; 888 #ifdef PROCFS_ZOMBIE 889 again: 890 #endif 891 for (p = LIST_FIRST(pd->pd_list); 892 p != NULL && uio->uio_resid >= UIO_MX; i++, pcnt++) { 893 switch (i) { 894 case 0: /* `.' */ 895 case 1: /* `..' */ 896 d.d_fileno = PROCFS_FILENO(0, Proot); 897 d.d_namlen = i + 1; 898 memcpy(d.d_name, "..", d.d_namlen); 899 d.d_name[i + 1] = '\0'; 900 d.d_type = DT_DIR; 901 break; 902 903 case 2: 904 d.d_fileno = PROCFS_FILENO(0, Pcurproc); 905 d.d_namlen = 7; 906 memcpy(d.d_name, "curproc", 8); 907 d.d_type = DT_LNK; 908 break; 909 910 default: 911 while (pcnt < i) { 912 pcnt++; 913 p = LIST_NEXT(p, p_list); 914 if (!p) 915 goto done; 916 } 917 d.d_fileno = PROCFS_FILENO(p->p_pid, Pproc); 918 d.d_namlen = sprintf(d.d_name, "%ld", 919 (long)p->p_pid); 920 d.d_type = DT_REG; 921 p = p->p_list.le_next; 922 break; 923 } 924 925 if ((error = uiomove((caddr_t)&d, UIO_MX, uio)) != 0) 926 break; 927 nc++; 928 if (cookies) 929 *cookies++ = i + 1; 930 } 931 done: 932 933 #ifdef PROCFS_ZOMBIE 934 pd++; 935 if (p == NULL && pd->pd_list != NULL) 936 goto again; 937 #endif 938 ncookies = nc; 939 940 break; 941 942 } 943 944 default: 945 error = ENOTDIR; 946 break; 947 } 948 949 if (ap->a_ncookies) { 950 if (error) { 951 if (cookies) 952 FREE(*ap->a_cookies, M_TEMP); 953 *ap->a_ncookies = 0; 954 *ap->a_cookies = NULL; 955 } else 956 *ap->a_ncookies = ncookies; 957 } 958 uio->uio_offset = i; 959 return (error); 960 } 961 962 /* 963 * readlink reads the link of `curproc' 964 */ 965 int 966 procfs_readlink(v) 967 void *v; 968 { 969 struct vop_readlink_args *ap = v; 970 char buf[16]; /* should be enough */ 971 int len; 972 973 if (VTOPFS(ap->a_vp)->pfs_fileno != PROCFS_FILENO(0, Pcurproc)) 974 return (EINVAL); 975 976 len = sprintf(buf, "%ld", (long)curproc->p_pid); 977 978 return (uiomove((caddr_t)buf, len, ap->a_uio)); 979 } 980 981 /* 982 * convert decimal ascii to pid_t 983 */ 984 static pid_t 985 atopid(b, len) 986 const char *b; 987 u_int len; 988 { 989 pid_t p = 0; 990 991 while (len--) { 992 char c = *b++; 993 if (c < '0' || c > '9') 994 return (NO_PID); 995 p = 10 * p + (c - '0'); 996 if (p > PID_MAX) 997 return (NO_PID); 998 } 999 1000 return (p); 1001 } 1002