1 /* 2 * Copyright (c) 1993 Jan-Simon Pendry 3 * Copyright (c) 1993 4 * The Regents of the University of California. All rights reserved. 5 * 6 * This code is derived from software contributed to Berkeley by 7 * Jan-Simon Pendry. 8 * 9 * %sccs.include.redist.c% 10 * 11 * @(#)procfs_vnops.c 8.6 (Berkeley) 02/07/94 12 * 13 * From: 14 * $Id: procfs_vnops.c,v 3.2 1993/12/15 09:40:17 jsp Exp $ 15 */ 16 17 /* 18 * procfs vnode interface 19 */ 20 21 #include <sys/param.h> 22 #include <sys/systm.h> 23 #include <sys/time.h> 24 #include <sys/kernel.h> 25 #include <sys/file.h> 26 #include <sys/proc.h> 27 #include <sys/vnode.h> 28 #include <sys/namei.h> 29 #include <sys/malloc.h> 30 #include <sys/dirent.h> 31 #include <sys/resourcevar.h> 32 #include <miscfs/procfs/procfs.h> 33 #include <vm/vm.h> /* for PAGE_SIZE */ 34 35 /* 36 * Vnode Operations. 37 * 38 */ 39 40 /* 41 * This is a list of the valid names in the 42 * process-specific sub-directories. It is 43 * used in procfs_lookup and procfs_readdir 44 */ 45 static struct pfsnames { 46 u_short d_namlen; 47 char d_name[PROCFS_NAMELEN]; 48 pfstype d_pfstype; 49 } procent[] = { 50 #define N(s) sizeof(s)-1, s 51 /* namlen, nam, type */ 52 { N("file"), Pfile }, 53 { N("mem"), Pmem }, 54 { N("regs"), Pregs }, 55 { N("fpregs"), Pfpregs }, 56 { N("ctl"), Pctl }, 57 { N("status"), Pstatus }, 58 { N("note"), Pnote }, 59 { N("notepg"), Pnotepg }, 60 #undef N 61 }; 62 #define Nprocent (sizeof(procent)/sizeof(procent[0])) 63 64 static pid_t atopid __P((const char *, u_int)); 65 66 /* 67 * set things up for doing i/o on 68 * the pfsnode (vp). (vp) is locked 69 * on entry, and should be left locked 70 * on exit. 71 * 72 * for procfs we don't need to do anything 73 * in particular for i/o. all that is done 74 * is to support exclusive open on process 75 * memory images. 76 */ 77 procfs_open(ap) 78 struct vop_open_args *ap; 79 { 80 struct pfsnode *pfs = VTOPFS(ap->a_vp); 81 82 switch (pfs->pfs_type) { 83 case Pmem: 84 if (PFIND(pfs->pfs_pid) == 0) 85 return (ENOENT); /* was ESRCH, jsp */ 86 87 if ((pfs->pfs_flags & FWRITE) && (ap->a_mode & O_EXCL) || 88 (pfs->pfs_flags & O_EXCL) && (ap->a_mode & FWRITE)) 89 return (EBUSY); 90 91 92 if (ap->a_mode & FWRITE) 93 pfs->pfs_flags = ap->a_mode & (FWRITE|O_EXCL); 94 95 return (0); 96 97 default: 98 break; 99 } 100 101 return (0); 102 } 103 104 /* 105 * close the pfsnode (vp) after doing i/o. 106 * (vp) is not locked on entry or exit. 107 * 108 * nothing to do for procfs other than undo 109 * any exclusive open flag (see _open above). 110 */ 111 procfs_close(ap) 112 struct vop_close_args *ap; 113 { 114 struct pfsnode *pfs = VTOPFS(ap->a_vp); 115 116 switch (pfs->pfs_type) { 117 case Pmem: 118 if ((ap->a_fflag & FWRITE) && (pfs->pfs_flags & O_EXCL)) 119 pfs->pfs_flags &= ~(FWRITE|O_EXCL); 120 break; 121 } 122 123 return (0); 124 } 125 126 /* 127 * do an ioctl operation on pfsnode (vp). 128 * (vp) is not locked on entry or exit. 129 */ 130 procfs_ioctl(ap) 131 struct vop_ioctl_args *ap; 132 { 133 134 return (ENOTTY); 135 } 136 137 /* 138 * do block mapping for pfsnode (vp). 139 * since we don't use the buffer cache 140 * for procfs this function should never 141 * be called. in any case, it's not clear 142 * what part of the kernel ever makes use 143 * of this function. for sanity, this is the 144 * usual no-op bmap, although returning 145 * (EIO) would be a reasonable alternative. 146 */ 147 procfs_bmap(ap) 148 struct vop_bmap_args *ap; 149 { 150 151 if (ap->a_vpp != NULL) 152 *ap->a_vpp = ap->a_vp; 153 if (ap->a_bnp != NULL) 154 *ap->a_bnp = ap->a_bn; 155 return (0); 156 } 157 158 /* 159 * _inactive is called when the pfsnode 160 * is vrele'd and the reference count goes 161 * to zero. (vp) will be on the vnode free 162 * list, so to get it back vget() must be 163 * used. 164 * 165 * for procfs, check if the process is still 166 * alive and if it isn't then just throw away 167 * the vnode by calling vgone(). this may 168 * be overkill and a waste of time since the 169 * chances are that the process will still be 170 * there and PFIND is not free. 171 * 172 * (vp) is not locked on entry or exit. 173 */ 174 procfs_inactive(ap) 175 struct vop_inactive_args *ap; 176 { 177 struct pfsnode *pfs = VTOPFS(ap->a_vp); 178 179 if (PFIND(pfs->pfs_pid) == 0) 180 vgone(ap->a_vp); 181 182 return (0); 183 } 184 185 /* 186 * _reclaim is called when getnewvnode() 187 * wants to make use of an entry on the vnode 188 * free list. at this time the filesystem needs 189 * to free any private data and remove the node 190 * from any private lists. 191 */ 192 procfs_reclaim(ap) 193 struct vop_reclaim_args *ap; 194 { 195 int error; 196 197 error = procfs_freevp(ap->a_vp); 198 return (error); 199 } 200 201 /* 202 * Return POSIX pathconf information applicable to special devices. 203 */ 204 procfs_pathconf(ap) 205 struct vop_pathconf_args /* { 206 struct vnode *a_vp; 207 int a_name; 208 int *a_retval; 209 } */ *ap; 210 { 211 212 switch (ap->a_name) { 213 case _PC_LINK_MAX: 214 *ap->a_retval = LINK_MAX; 215 return (0); 216 case _PC_MAX_CANON: 217 *ap->a_retval = MAX_CANON; 218 return (0); 219 case _PC_MAX_INPUT: 220 *ap->a_retval = MAX_INPUT; 221 return (0); 222 case _PC_PIPE_BUF: 223 *ap->a_retval = PIPE_BUF; 224 return (0); 225 case _PC_CHOWN_RESTRICTED: 226 *ap->a_retval = 1; 227 return (0); 228 case _PC_VDISABLE: 229 *ap->a_retval = _POSIX_VDISABLE; 230 return (0); 231 default: 232 return (EINVAL); 233 } 234 /* NOTREACHED */ 235 } 236 237 /* 238 * _print is used for debugging. 239 * just print a readable description 240 * of (vp). 241 */ 242 procfs_print(ap) 243 struct vop_print_args *ap; 244 { 245 struct pfsnode *pfs = VTOPFS(ap->a_vp); 246 247 printf("tag VT_PROCFS, pid %d, mode %x, flags %x\n", 248 pfs->pfs_pid, 249 pfs->pfs_mode, pfs->pfs_flags); 250 } 251 252 /* 253 * _abortop is called when operations such as 254 * rename and create fail. this entry is responsible 255 * for undoing any side-effects caused by the lookup. 256 * this will always include freeing the pathname buffer. 257 */ 258 procfs_abortop(ap) 259 struct vop_abortop_args *ap; 260 { 261 262 if ((ap->a_cnp->cn_flags & (HASBUF | SAVESTART)) == HASBUF) 263 FREE(ap->a_cnp->cn_pnbuf, M_NAMEI); 264 return (0); 265 } 266 267 /* 268 * generic entry point for unsupported operations 269 */ 270 procfs_badop() 271 { 272 273 return (EIO); 274 } 275 276 /* 277 * Invent attributes for pfsnode (vp) and store 278 * them in (vap). 279 * Directories lengths are returned as zero since 280 * any real length would require the genuine size 281 * to be computed, and nothing cares anyway. 282 * 283 * this is relatively minimal for procfs. 284 */ 285 procfs_getattr(ap) 286 struct vop_getattr_args *ap; 287 { 288 struct pfsnode *pfs = VTOPFS(ap->a_vp); 289 struct vattr *vap = ap->a_vap; 290 struct proc *procp; 291 int error; 292 293 /* first check the process still exists */ 294 switch (pfs->pfs_type) { 295 case Proot: 296 procp = 0; 297 break; 298 299 default: 300 procp = PFIND(pfs->pfs_pid); 301 if (procp == 0) 302 return (ENOENT); 303 } 304 305 error = 0; 306 307 /* start by zeroing out the attributes */ 308 VATTR_NULL(vap); 309 310 /* next do all the common fields */ 311 vap->va_type = ap->a_vp->v_type; 312 vap->va_mode = pfs->pfs_mode; 313 vap->va_fileid = pfs->pfs_fileno; 314 vap->va_flags = 0; 315 vap->va_blocksize = PAGE_SIZE; 316 vap->va_bytes = vap->va_size = 0; 317 318 /* 319 * If the process has exercised some setuid or setgid 320 * privilege, then rip away read/write permission so 321 * that only root can gain access. 322 */ 323 switch (pfs->pfs_type) { 324 case Pregs: 325 case Pfpregs: 326 case Pmem: 327 if (procp->p_flag & P_SUGID) 328 vap->va_mode &= ~((VREAD|VWRITE)| 329 ((VREAD|VWRITE)>>3)| 330 ((VREAD|VWRITE)>>6)); 331 break; 332 } 333 334 /* 335 * Make all times be current TOD. 336 * It would be possible to get the process start 337 * time from the p_stat structure, but there's 338 * no "file creation" time stamp anyway, and the 339 * p_stat structure is not addressible if u. gets 340 * swapped out for that process. 341 */ 342 microtime(&vap->va_ctime); 343 vap->va_atime = vap->va_mtime = vap->va_ctime; 344 345 /* 346 * now do the object specific fields 347 * 348 * The size could be set from struct reg, but it's hardly 349 * worth the trouble, and it puts some (potentially) machine 350 * dependent data into this machine-independent code. If it 351 * becomes important then this function should break out into 352 * a per-file stat function in the corresponding .c file. 353 */ 354 355 switch (pfs->pfs_type) { 356 case Proot: 357 vap->va_nlink = 2; 358 vap->va_uid = 0; 359 vap->va_gid = 0; 360 break; 361 362 case Pproc: 363 vap->va_nlink = 2; 364 vap->va_uid = procp->p_ucred->cr_uid; 365 vap->va_gid = procp->p_ucred->cr_gid; 366 break; 367 368 case Pfile: 369 error = EOPNOTSUPP; 370 break; 371 372 case Pmem: 373 vap->va_nlink = 1; 374 vap->va_bytes = vap->va_size = 375 ctob(procp->p_vmspace->vm_tsize + 376 procp->p_vmspace->vm_dsize + 377 procp->p_vmspace->vm_ssize); 378 vap->va_uid = procp->p_ucred->cr_uid; 379 vap->va_gid = procp->p_ucred->cr_gid; 380 break; 381 382 case Pregs: 383 case Pfpregs: 384 case Pctl: 385 case Pstatus: 386 case Pnote: 387 case Pnotepg: 388 vap->va_nlink = 1; 389 vap->va_uid = procp->p_ucred->cr_uid; 390 vap->va_gid = procp->p_ucred->cr_gid; 391 break; 392 393 default: 394 panic("procfs_getattr"); 395 } 396 397 return (error); 398 } 399 400 procfs_setattr(ap) 401 struct vop_setattr_args *ap; 402 { 403 /* 404 * just fake out attribute setting 405 * it's not good to generate an error 406 * return, otherwise things like creat() 407 * will fail when they try to set the 408 * file length to 0. worse, this means 409 * that echo $note > /proc/$pid/note will fail. 410 */ 411 412 return (0); 413 } 414 415 /* 416 * implement access checking. 417 * 418 * something very similar to this code is duplicated 419 * throughout the 4bsd kernel and should be moved 420 * into kern/vfs_subr.c sometime. 421 * 422 * actually, the check for super-user is slightly 423 * broken since it will allow read access to write-only 424 * objects. this doesn't cause any particular trouble 425 * but does mean that the i/o entry points need to check 426 * that the operation really does make sense. 427 */ 428 procfs_access(ap) 429 struct vop_access_args *ap; 430 { 431 struct vattr *vap; 432 struct vattr vattr; 433 int error; 434 435 /* 436 * If you're the super-user, 437 * you always get access. 438 */ 439 if (ap->a_cred->cr_uid == (uid_t) 0) 440 return (0); 441 vap = &vattr; 442 if (error = VOP_GETATTR(ap->a_vp, vap, ap->a_cred, ap->a_p)) 443 return (error); 444 445 /* 446 * Access check is based on only one of owner, group, public. 447 * If not owner, then check group. If not a member of the 448 * group, then check public access. 449 */ 450 if (ap->a_cred->cr_uid != vap->va_uid) { 451 gid_t *gp; 452 int i; 453 454 (ap->a_mode) >>= 3; 455 gp = ap->a_cred->cr_groups; 456 for (i = 0; i < ap->a_cred->cr_ngroups; i++, gp++) 457 if (vap->va_gid == *gp) 458 goto found; 459 ap->a_mode >>= 3; 460 found: 461 ; 462 } 463 464 if ((vap->va_mode & ap->a_mode) == ap->a_mode) 465 return (0); 466 467 return (EACCES); 468 } 469 470 /* 471 * lookup. this is incredibly complicated in the 472 * general case, however for most pseudo-filesystems 473 * very little needs to be done. 474 * 475 * unless you want to get a migraine, just make sure your 476 * filesystem doesn't do any locking of its own. otherwise 477 * read and inwardly digest ufs_lookup(). 478 */ 479 procfs_lookup(ap) 480 struct vop_lookup_args *ap; 481 { 482 struct componentname *cnp = ap->a_cnp; 483 struct vnode **vpp = ap->a_vpp; 484 struct vnode *dvp = ap->a_dvp; 485 char *pname = cnp->cn_nameptr; 486 int error = 0; 487 pid_t pid; 488 struct vnode *nvp; 489 struct pfsnode *pfs; 490 struct proc *procp; 491 pfstype pfs_type; 492 int i; 493 494 if (cnp->cn_namelen == 1 && *pname == '.') { 495 *vpp = dvp; 496 VREF(dvp); 497 /*VOP_LOCK(dvp);*/ 498 return (0); 499 } 500 501 *vpp = NULL; 502 503 pfs = VTOPFS(dvp); 504 switch (pfs->pfs_type) { 505 case Proot: 506 if (cnp->cn_flags & ISDOTDOT) 507 return (EIO); 508 509 if (CNEQ(cnp, "curproc", 7)) 510 pid = cnp->cn_proc->p_pid; 511 else 512 pid = atopid(pname, cnp->cn_namelen); 513 if (pid == NO_PID) 514 return (ENOENT); 515 516 procp = PFIND(pid); 517 if (procp == 0) 518 return (ENOENT); 519 520 error = procfs_allocvp(dvp->v_mount, &nvp, pid, Pproc); 521 if (error) 522 return (error); 523 524 nvp->v_type = VDIR; 525 pfs = VTOPFS(nvp); 526 527 *vpp = nvp; 528 return (0); 529 530 case Pproc: 531 if (cnp->cn_flags & ISDOTDOT) { 532 error = procfs_root(dvp->v_mount, vpp); 533 return (error); 534 } 535 536 procp = PFIND(pfs->pfs_pid); 537 if (procp == 0) 538 return (ENOENT); 539 540 for (i = 0; i < Nprocent; i++) { 541 struct pfsnames *dp = &procent[i]; 542 543 if (cnp->cn_namelen == dp->d_namlen && 544 bcmp(pname, dp->d_name, dp->d_namlen) == 0) { 545 pfs_type = dp->d_pfstype; 546 goto found; 547 } 548 } 549 return (ENOENT); 550 551 found: 552 if (pfs_type == Pfile) { 553 nvp = procfs_findtextvp(procp); 554 if (nvp) { 555 VREF(nvp); 556 VOP_LOCK(nvp); 557 } else { 558 error = ENXIO; 559 } 560 } else { 561 error = procfs_allocvp(dvp->v_mount, &nvp, 562 pfs->pfs_pid, pfs_type); 563 if (error) 564 return (error); 565 566 nvp->v_type = VREG; 567 pfs = VTOPFS(nvp); 568 } 569 *vpp = nvp; 570 return (error); 571 572 default: 573 return (ENOTDIR); 574 } 575 } 576 577 /* 578 * readdir returns directory entries from pfsnode (vp). 579 * 580 * the strategy here with procfs is to generate a single 581 * directory entry at a time (struct pfsdent) and then 582 * copy that out to userland using uiomove. a more efficent 583 * though more complex implementation, would try to minimize 584 * the number of calls to uiomove(). for procfs, this is 585 * hardly worth the added code complexity. 586 * 587 * this should just be done through read() 588 */ 589 procfs_readdir(ap) 590 struct vop_readdir_args *ap; 591 { 592 struct uio *uio = ap->a_uio; 593 struct pfsdent d; 594 struct pfsdent *dp = &d; 595 struct pfsnode *pfs; 596 int error; 597 int count; 598 int i; 599 600 pfs = VTOPFS(ap->a_vp); 601 602 if (uio->uio_resid < UIO_MX) 603 return (EINVAL); 604 if (uio->uio_offset & (UIO_MX-1)) 605 return (EINVAL); 606 if (uio->uio_offset < 0) 607 return (EINVAL); 608 609 error = 0; 610 count = 0; 611 i = uio->uio_offset / UIO_MX; 612 613 switch (pfs->pfs_type) { 614 /* 615 * this is for the process-specific sub-directories. 616 * all that is needed to is copy out all the entries 617 * from the procent[] table (top of this file). 618 */ 619 case Pproc: { 620 while (uio->uio_resid >= UIO_MX) { 621 struct pfsnames *dt; 622 623 if (i >= Nprocent) 624 break; 625 626 dt = &procent[i]; 627 628 dp->d_reclen = UIO_MX; 629 dp->d_fileno = PROCFS_FILENO(pfs->pfs_pid, dt->d_pfstype); 630 dp->d_type = DT_REG; 631 dp->d_namlen = dt->d_namlen; 632 bcopy(dt->d_name, dp->d_name, sizeof(dt->d_name)-1); 633 error = uiomove((caddr_t) dp, UIO_MX, uio); 634 if (error) 635 break; 636 count += UIO_MX; 637 i++; 638 } 639 640 break; 641 642 } 643 644 /* 645 * this is for the root of the procfs filesystem 646 * what is needed is a special entry for "curproc" 647 * followed by an entry for each process on allproc 648 #ifdef PROCFS_ZOMBIE 649 * and zombproc. 650 #endif 651 */ 652 653 case Proot: { 654 int pcnt; 655 #ifdef PROCFS_ZOMBIE 656 int doingzomb = 0; 657 #endif 658 volatile struct proc *p; 659 660 p = allproc; 661 662 #define PROCFS_XFILES 1 /* number of other entries, like "curproc" */ 663 pcnt = PROCFS_XFILES; 664 665 while (p && uio->uio_resid >= UIO_MX) { 666 bzero((char *) dp, UIO_MX); 667 dp->d_type = DT_DIR; 668 dp->d_reclen = UIO_MX; 669 670 switch (i) { 671 case 0: 672 /* ship out entry for "curproc" */ 673 dp->d_fileno = PROCFS_FILENO(PID_MAX+1, Pproc); 674 dp->d_namlen = sprintf(dp->d_name, "curproc"); 675 break; 676 677 default: 678 if (pcnt >= i) { 679 dp->d_fileno = PROCFS_FILENO(p->p_pid, Pproc); 680 dp->d_namlen = sprintf(dp->d_name, "%ld", (long) p->p_pid); 681 } 682 683 p = p->p_next; 684 685 #ifdef PROCFS_ZOMBIE 686 if (p == 0 && doingzomb == 0) { 687 doingzomb = 1; 688 p = zombproc; 689 } 690 #endif 691 692 if (pcnt++ < i) 693 continue; 694 695 break; 696 } 697 error = uiomove((caddr_t) dp, UIO_MX, uio); 698 if (error) 699 break; 700 count += UIO_MX; 701 i++; 702 } 703 704 break; 705 706 } 707 708 default: 709 error = ENOTDIR; 710 break; 711 } 712 713 uio->uio_offset = i * UIO_MX; 714 715 return (error); 716 } 717 718 /* 719 * convert decimal ascii to pid_t 720 */ 721 static pid_t 722 atopid(b, len) 723 const char *b; 724 u_int len; 725 { 726 pid_t p = 0; 727 728 while (len--) { 729 char c = *b++; 730 if (c < '0' || c > '9') 731 return (NO_PID); 732 p = 10 * p + (c - '0'); 733 if (p > PID_MAX) 734 return (NO_PID); 735 } 736 737 return (p); 738 } 739 740 /* 741 * procfs vnode operations. 742 */ 743 int (**procfs_vnodeop_p)(); 744 struct vnodeopv_entry_desc procfs_vnodeop_entries[] = { 745 { &vop_default_desc, vn_default_error }, 746 { &vop_lookup_desc, procfs_lookup }, /* lookup */ 747 { &vop_create_desc, procfs_create }, /* create */ 748 { &vop_mknod_desc, procfs_mknod }, /* mknod */ 749 { &vop_open_desc, procfs_open }, /* open */ 750 { &vop_close_desc, procfs_close }, /* close */ 751 { &vop_access_desc, procfs_access }, /* access */ 752 { &vop_getattr_desc, procfs_getattr }, /* getattr */ 753 { &vop_setattr_desc, procfs_setattr }, /* setattr */ 754 { &vop_read_desc, procfs_read }, /* read */ 755 { &vop_write_desc, procfs_write }, /* write */ 756 { &vop_ioctl_desc, procfs_ioctl }, /* ioctl */ 757 { &vop_select_desc, procfs_select }, /* select */ 758 { &vop_mmap_desc, procfs_mmap }, /* mmap */ 759 { &vop_fsync_desc, procfs_fsync }, /* fsync */ 760 { &vop_seek_desc, procfs_seek }, /* seek */ 761 { &vop_remove_desc, procfs_remove }, /* remove */ 762 { &vop_link_desc, procfs_link }, /* link */ 763 { &vop_rename_desc, procfs_rename }, /* rename */ 764 { &vop_mkdir_desc, procfs_mkdir }, /* mkdir */ 765 { &vop_rmdir_desc, procfs_rmdir }, /* rmdir */ 766 { &vop_symlink_desc, procfs_symlink }, /* symlink */ 767 { &vop_readdir_desc, procfs_readdir }, /* readdir */ 768 { &vop_readlink_desc, procfs_readlink }, /* readlink */ 769 { &vop_abortop_desc, procfs_abortop }, /* abortop */ 770 { &vop_inactive_desc, procfs_inactive }, /* inactive */ 771 { &vop_reclaim_desc, procfs_reclaim }, /* reclaim */ 772 { &vop_lock_desc, procfs_lock }, /* lock */ 773 { &vop_unlock_desc, procfs_unlock }, /* unlock */ 774 { &vop_bmap_desc, procfs_bmap }, /* bmap */ 775 { &vop_strategy_desc, procfs_strategy }, /* strategy */ 776 { &vop_print_desc, procfs_print }, /* print */ 777 { &vop_islocked_desc, procfs_islocked }, /* islocked */ 778 { &vop_pathconf_desc, procfs_pathconf }, /* pathconf */ 779 { &vop_advlock_desc, procfs_advlock }, /* advlock */ 780 { &vop_blkatoff_desc, procfs_blkatoff }, /* blkatoff */ 781 { &vop_valloc_desc, procfs_valloc }, /* valloc */ 782 { &vop_vfree_desc, procfs_vfree }, /* vfree */ 783 { &vop_truncate_desc, procfs_truncate }, /* truncate */ 784 { &vop_update_desc, procfs_update }, /* update */ 785 { (struct vnodeop_desc*)NULL, (int(*)())NULL } 786 }; 787 struct vnodeopv_desc procfs_vnodeop_opv_desc = 788 { &procfs_vnodeop_p, procfs_vnodeop_entries }; 789