sys/vfs/procfs/procfs_vnops.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 | /* * Copyright (c) 1993, 1995 Jan-Simon Pendry * Copyright (c) 1993, 1995 * The Regents of the University of California. All rights reserved. * * This code is derived from software contributed to Berkeley by * Jan-Simon Pendry. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * @(#)procfs_vnops.c 8.18 (Berkeley) 5/21/95 * * $FreeBSD: src/sys/miscfs/procfs/procfs_vnops.c,v 1.76.2.7 2002/01/22 17:22:59 nectar Exp $ */ /* * procfs vnode interface */ #include <sys/param.h> #include <sys/systm.h> #include <sys/time.h> #include <sys/kernel.h> #include <sys/lock.h> #include <sys/fcntl.h> #include <sys/proc.h> #include <sys/caps.h> #include <sys/signalvar.h> #include <sys/vnode.h> #include <sys/uio.h> #include <sys/mount.h> #include <sys/namei.h> #include <sys/dirent.h> #include <sys/malloc.h> #include <sys/reg.h> #include <vm/vm_zone.h> #include <vfs/procfs/procfs.h> #include <sys/pioctl.h> #include <sys/spinlock2.h> #include <machine/limits.h> static int procfs_access (struct vop_access_args *); static int procfs_badop (struct vop_generic_args *); static int procfs_bmap (struct vop_bmap_args *); static int procfs_close (struct vop_close_args *); static int procfs_getattr (struct vop_getattr_args *); static int procfs_inactive (struct vop_inactive_args *); static int procfs_ioctl (struct vop_ioctl_args *); static int procfs_kqfilter (struct vop_kqfilter_args *); static int procfs_lookup (struct vop_old_lookup_args *); static int procfs_open (struct vop_open_args *); static int procfs_print (struct vop_print_args *); static int procfs_readdir (struct vop_readdir_args *); static int procfs_readlink (struct vop_readlink_args *); static int procfs_reclaim (struct vop_reclaim_args *); static int procfs_setattr (struct vop_setattr_args *); static int procfs_readdir_proc(struct vop_readdir_args *); static int procfs_readdir_root(struct vop_readdir_args *); /* * procfs vnode operations. */ struct vop_ops procfs_vnode_vops = { .vop_default = vop_defaultop, .vop_access = procfs_access, .vop_advlock = (void *)procfs_badop, .vop_bmap = procfs_bmap, .vop_close = procfs_close, .vop_old_create = (void *)procfs_badop, .vop_getattr = procfs_getattr, .vop_inactive = procfs_inactive, .vop_old_link = (void *)procfs_badop, .vop_old_lookup = procfs_lookup, .vop_old_mkdir = (void *)procfs_badop, .vop_old_mknod = (void *)procfs_badop, .vop_open = procfs_open, .vop_pathconf = vop_stdpathconf, .vop_print = procfs_print, .vop_read = procfs_rw, .vop_readdir = procfs_readdir, .vop_readlink = procfs_readlink, .vop_reclaim = procfs_reclaim, .vop_old_remove = (void *)procfs_badop, .vop_old_rename = (void *)procfs_badop, .vop_old_rmdir = (void *)procfs_badop, .vop_setattr = procfs_setattr, .vop_old_symlink = (void *)procfs_badop, .vop_write = (void *)procfs_rw, .vop_ioctl = procfs_ioctl, .vop_kqfilter = procfs_kqfilter, }; /* * This is a list of the valid names in the * process-specific sub-directories. It is * used in procfs_lookup and procfs_readdir */ static struct proc_target { u_char pt_type; u_char pt_namlen; char *pt_name; pfstype pt_pfstype; int (*pt_valid) (struct lwp *p); } proc_targets[] = { #define N(s) sizeof(s)-1, s /* name type validp */ { DT_DIR, N("."), Pproc, NULL }, { DT_DIR, N(".."), Proot, NULL }, { DT_REG, N("mem"), Pmem, NULL }, { DT_REG, N("regs"), Pregs, procfs_validregs }, { DT_REG, N("fpregs"), Pfpregs, procfs_validfpregs }, { DT_REG, N("dbregs"), Pdbregs, procfs_validdbregs }, { DT_REG, N("ctl"), Pctl, NULL }, { DT_REG, N("status"), Pstatus, NULL }, { DT_REG, N("note"), Pnote, NULL }, { DT_REG, N("notepg"), Pnotepg, NULL }, { DT_REG, N("map"), Pmap, procfs_validmap }, { DT_REG, N("etype"), Ptype, procfs_validtype }, { DT_REG, N("cmdline"), Pcmdline, NULL }, { DT_REG, N("rlimit"), Prlimit, NULL }, { DT_LNK, N("file"), Pfile, NULL }, { DT_LNK, N("exe"), Pfile, NULL }, #undef N }; static const int nproc_targets = NELEM(proc_targets); static pid_t atopid (const char *, u_int); /* * set things up for doing i/o on * the pfsnode (vp). (vp) is locked * on entry, and should be left locked * on exit. * * for procfs we don't need to do anything * in particular for i/o. all that is done * is to support exclusive open on process * memory images. * * procfs_open(struct vnode *a_vp, int a_mode, struct ucred *a_cred, * struct file *a_fp) */ static int procfs_open(struct vop_open_args *ap) { struct pfsnode *pfs = VTOPFS(ap->a_vp); struct proc *p1, *p2; int error; p2 = pfs_pfind(pfs->pfs_pid); if (p2 == NULL) return (ENOENT); if (pfs->pfs_pid && !PRISON_CHECK(ap->a_cred, p2->p_ucred)) { error = ENOENT; goto done; } switch (pfs->pfs_type) { case Pmem: if (((pfs->pfs_flags & FWRITE) && (ap->a_mode & O_EXCL)) || ((pfs->pfs_flags & O_EXCL) && (ap->a_mode & FWRITE))) { error = EBUSY; goto done; } p1 = curproc; KKASSERT(p1); /* Can't trace a process that's currently exec'ing. */ if ((p2->p_flags & P_INEXEC) != 0) { error = EAGAIN; goto done; } if (!CHECKIO(p1, p2) || p_trespass(ap->a_cred, p2->p_ucred)) { error = EPERM; goto done; } if (ap->a_mode & FWRITE) pfs->pfs_flags = ap->a_mode & (FWRITE|O_EXCL); break; default: break; } error = vop_stdopen(ap); done: pfs_pdone(p2); return error; } /* * close the pfsnode (vp) after doing i/o. * (vp) is not locked on entry or exit. * * nothing to do for procfs other than undo * any exclusive open flag (see _open above). * * procfs_close(struct vnode *a_vp, int a_fflag, struct ucred *a_cred) */ static int procfs_close(struct vop_close_args *ap) { struct pfsnode *pfs = VTOPFS(ap->a_vp); struct proc *p; /* * Make sure the lock is exclusive for opencount tests */ vn_lock(ap->a_vp, LK_UPGRADE | LK_RETRY); switch (pfs->pfs_type) { case Pmem: if ((ap->a_fflag & FWRITE) && (pfs->pfs_flags & O_EXCL)) pfs->pfs_flags &= ~(FWRITE|O_EXCL); /* * v_opencount determines the last real close on the vnode. * * If this is the last close, then it checks to see if * the target process has PF_LINGER set in p_pfsflags, * if this is *not* the case, then the process' stop flags * are cleared, and the process is woken up. This is * to help prevent the case where a process has been * told to stop on an event, but then the requesting process * has gone away or forgotten about it. */ p = NULL; if ((ap->a_vp->v_opencount < 2) && ((p = pfs_pfind(pfs->pfs_pid)) != NULL || (p = pfs_zpfind(pfs->pfs_pid)) != NULL) && !(p->p_pfsflags & PF_LINGER)) { spin_lock(&p->p_spin); p->p_stops = 0; p->p_step = 0; spin_unlock(&p->p_spin); wakeup(&p->p_stype); wakeup(&p->p_step); } pfs_pdone(p); break; default: break; } return (vop_stdclose(ap)); } /* * do an ioctl operation on a pfsnode (vp). * (vp) is not locked on entry or exit. */ static int procfs_ioctl(struct vop_ioctl_args *ap) { struct pfsnode *pfs = VTOPFS(ap->a_vp); struct proc *procp; struct proc *p; int error; int signo; struct procfs_status *psp; unsigned char flags; procp = pfs_pfind(pfs->pfs_pid); if (procp == NULL) return ENOTTY; p = curproc; if (p == NULL) { error = EINVAL; goto done; } /* Can't trace a process that's currently exec'ing. */ if ((procp->p_flags & P_INEXEC) != 0) { error = EAGAIN; goto done; } if (!CHECKIO(p, procp) || p_trespass(ap->a_cred, procp->p_ucred)) { error = EPERM; goto done; } switch (ap->a_command) { case PIOCBIS: spin_lock(&procp->p_spin); procp->p_stops |= *(unsigned int*)ap->a_data; spin_unlock(&procp->p_spin); break; case PIOCBIC: spin_lock(&procp->p_spin); procp->p_stops &= ~*(unsigned int*)ap->a_data; spin_unlock(&procp->p_spin); break; case PIOCSFL: /* * NFLAGS is "non-suser_xxx flags" -- currently, only * PFS_ISUGID ("ignore set u/g id"); */ #define NFLAGS (PF_ISUGID) flags = (unsigned char)*(unsigned int*)ap->a_data; if (flags & NFLAGS && (error = caps_priv_check(ap->a_cred, SYSCAP_RESTRICTEDROOT))) { goto done; } procp->p_pfsflags = flags; break; case PIOCGFL: *(unsigned int*)ap->a_data = (unsigned int)procp->p_pfsflags; break; case PIOCSTATUS: /* * NOTE: syscall entry deals with stopevents and may run * without the MP lock. */ psp = (struct procfs_status *)ap->a_data; psp->flags = procp->p_pfsflags; psp->events = procp->p_stops; spin_lock(&procp->p_spin); if (procp->p_step) { psp->state = 0; psp->why = procp->p_stype; psp->val = procp->p_xstat; spin_unlock(&procp->p_spin); } else { psp->state = 1; spin_unlock(&procp->p_spin); psp->why = 0; /* Not defined values */ psp->val = 0; /* Not defined values */ } break; case PIOCWAIT: /* * NOTE: syscall entry deals with stopevents and may run * without the MP lock. */ psp = (struct procfs_status *)ap->a_data; spin_lock(&procp->p_spin); while (procp->p_step == 0) { tsleep_interlock(&procp->p_stype, PCATCH); spin_unlock(&procp->p_spin); if (procp->p_stops == 0) { error = 0; goto done; } if (procp->p_flags & P_POSTEXIT) { error = EINVAL; goto done; } if (procp->p_flags & P_INEXEC) { error = EAGAIN; goto done; } error = tsleep(&procp->p_stype, PCATCH | PINTERLOCKED, "piocwait", 0); if (error) goto done; spin_lock(&procp->p_spin); } spin_unlock(&procp->p_spin); psp->state = 1; /* It stopped */ psp->flags = procp->p_pfsflags; psp->events = procp->p_stops; psp->why = procp->p_stype; /* why it stopped */ psp->val = procp->p_xstat; /* any extra info */ break; case PIOCCONT: /* Restart a proc */ /* * NOTE: syscall entry deals with stopevents and may run * without the MP lock. However, the caller is * presumably interlocked by having waited. */ if (procp->p_step == 0) { error = EINVAL; /* Can only start a stopped process */ goto done; } if ((signo = *(int*)ap->a_data) != 0) { if (signo >= NSIG || signo <= 0) { error = EINVAL; goto done; } ksignal(procp, signo); } procp->p_step = 0; wakeup(&procp->p_step); break; default: error = ENOTTY; goto done; } error = 0; done: pfs_pdone(procp); return error; } /* * do block mapping for pfsnode (vp). * since we don't use the buffer cache * for procfs this function should never * be called. in any case, it's not clear * what part of the kernel ever makes use * of this function. for sanity, this is the * usual no-op bmap, although returning * (EIO) would be a reasonable alternative. * * XXX mmap assumes buffer cache operation * * procfs_bmap(struct vnode *a_vp, off_t a_loffset, * off_t *a_doffsetp, int *a_runp, int *a_runb) */ static int procfs_bmap(struct vop_bmap_args *ap) { if (ap->a_doffsetp != NULL) *ap->a_doffsetp = ap->a_loffset; if (ap->a_runp != NULL) *ap->a_runp = 0; if (ap->a_runb != NULL) *ap->a_runb = 0; return (0); } /* * procfs_inactive is called when the pfsnode * is vrele'd and the reference count goes * to zero. (vp) will be on the vnode free * list, so to get it back vget() must be * used. * * (vp) is locked on entry, but must be unlocked on exit. * * procfs_inactive(struct vnode *a_vp) */ static int procfs_inactive(struct vop_inactive_args *ap) { struct pfsnode *pfs = VTOPFS(ap->a_vp); if (pfs->pfs_pid & PFS_DEAD) vrecycle(ap->a_vp); return (0); } /* * _reclaim is called when getnewvnode() * wants to make use of an entry on the vnode * free list. at this time the filesystem needs * to free any private data and remove the node * from any private lists. * * procfs_reclaim(struct vnode *a_vp) */ static int procfs_reclaim(struct vop_reclaim_args *ap) { return (procfs_freevp(ap->a_vp)); } /* * _print is used for debugging. * just print a readable description * of (vp). * * procfs_print(struct vnode *a_vp) */ static int procfs_print(struct vop_print_args *ap) { struct pfsnode *pfs = VTOPFS(ap->a_vp); kprintf("tag VT_PROCFS, type %d, pid %ld, mode %x, flags %lx\n", pfs->pfs_type, (long)pfs->pfs_pid, pfs->pfs_mode, pfs->pfs_flags); return (0); } /* * generic entry point for unsupported operations */ static int procfs_badop(struct vop_generic_args *ap) { return (EIO); } /* * Invent attributes for pfsnode (vp) and store * them in (vap). * Directories lengths are returned as zero since * any real length would require the genuine size * to be computed, and nothing cares anyway. * * this is relatively minimal for procfs. * * procfs_getattr(struct vnode *a_vp, struct vattr *a_vap) */ static int procfs_getattr(struct vop_getattr_args *ap) { struct pfsnode *pfs = VTOPFS(ap->a_vp); struct vattr *vap = ap->a_vap; struct proc *procp; int error; /* * First make sure that the process and its credentials * still exist. */ switch (pfs->pfs_type) { case Proot: case Pcurproc: procp = NULL; break; default: procp = pfs_pfind(pfs->pfs_pid); if (procp == NULL || procp->p_ucred == NULL) { error = ENOENT; goto done; } break; } error = 0; /* start by zeroing out the attributes */ VATTR_NULL(vap); /* next do all the common fields */ vap->va_type = ap->a_vp->v_type; vap->va_mode = pfs->pfs_mode; vap->va_fileid = pfs->pfs_fileno; vap->va_flags = 0; vap->va_blocksize = PAGE_SIZE; vap->va_bytes = vap->va_size = 0; vap->va_fsid = ap->a_vp->v_mount->mnt_stat.f_fsid.val[0]; /* * Make all times be current TOD. * It would be possible to get the process start * time from the p_stat structure, but there's * no "file creation" time stamp anyway, and the * p_stat structure is not addressible if u. gets * swapped out for that process. */ vfs_timestamp(&vap->va_ctime); vap->va_atime = vap->va_mtime = vap->va_ctime; /* * If the process has exercised some setuid or setgid * privilege, then rip away read/write permission so * that only root can gain access. */ switch (pfs->pfs_type) { case Pctl: case Pregs: case Pfpregs: case Pdbregs: case Pmem: if (procp->p_flags & P_SUGID) { vap->va_mode &= ~((VREAD|VWRITE)| ((VREAD|VWRITE)>>3)| ((VREAD|VWRITE)>>6)); } break; default: break; } /* * now do the object specific fields * * The size could be set from struct reg, but it's hardly * worth the trouble, and it puts some (potentially) machine * dependent data into this machine-independent code. If it * becomes important then this function should break out into * a per-file stat function in the corresponding .c file. */ vap->va_nlink = 1; if (procp) { if (procp->p_ucred) { vap->va_uid = procp->p_ucred->cr_uid; vap->va_gid = procp->p_ucred->cr_gid; } else { vap->va_uid = -1; vap->va_gid = -1; } } switch (pfs->pfs_type) { case Proot: /* * Set nlink to 1 to tell fts(3) we don't actually know. */ vap->va_nlink = 1; vap->va_uid = 0; vap->va_gid = 0; vap->va_size = vap->va_bytes = DEV_BSIZE; break; case Pcurproc: { char buf[16]; /* should be enough */ vap->va_uid = 0; vap->va_gid = 0; vap->va_size = ksnprintf(buf, sizeof(buf), "%ld", (long)curproc->p_pid); vap->va_bytes = vap->va_size; break; } case Pproc: vap->va_nlink = nproc_targets; vap->va_size = vap->va_bytes = DEV_BSIZE; break; case Pfile: { char *fullpath, *freepath; if (procp->p_textnch.ncp) { struct nchandle nch; cache_copy(&procp->p_textnch, &nch); error = cache_fullpath(procp, &nch, NULL, &fullpath, &freepath, 0); cache_drop(&nch); } else { error = EINVAL; } if (error == 0) { vap->va_size = strlen(fullpath); kfree(freepath, M_TEMP); } else { vap->va_size = sizeof("unknown") - 1; error = 0; } vap->va_bytes = vap->va_size; break; } case Pmem: /* * If we denied owner access earlier, then we have to * change the owner to root - otherwise 'ps' and friends * will break even though they are setgid kmem. *SIGH* */ if (procp->p_flags & P_SUGID) vap->va_uid = 0; else if (procp->p_ucred) vap->va_uid = procp->p_ucred->cr_uid; else vap->va_uid = -1; break; case Pregs: vap->va_bytes = vap->va_size = sizeof(struct reg); break; case Pfpregs: vap->va_bytes = vap->va_size = sizeof(struct fpreg); break; case Pdbregs: vap->va_bytes = vap->va_size = sizeof(struct dbreg); break; case Ptype: case Pmap: case Pctl: case Pstatus: case Pnote: case Pnotepg: case Pcmdline: case Prlimit: break; default: panic("procfs_getattr"); } done: pfs_pdone(procp); return (error); } /* * procfs_setattr(struct vnode *a_vp, struct vattr *a_vap, * struct ucred *a_cred) */ static int procfs_setattr(struct vop_setattr_args *ap) { if (ap->a_vap->va_flags != VNOVAL) return (EOPNOTSUPP); /* * just fake out attribute setting * it's not good to generate an error * return, otherwise things like creat() * will fail when they try to set the * file length to 0. worse, this means * that echo $note > /proc/$pid/note will fail. */ return (0); } /* * implement access checking. * * procfs_access(struct vnode *a_vp, int a_mode, struct ucred *a_cred) */ static int procfs_access(struct vop_access_args *ap) { struct vattr vattr; int error; error = VOP_GETATTR(ap->a_vp, &vattr); if (!error) error = vop_helper_access(ap, vattr.va_uid, vattr.va_gid, vattr.va_mode, 0); return (error); } /* * lookup. this is incredibly complicated in the general case, however * for most pseudo-filesystems very little needs to be done. * * procfs_lookup(struct vnode *a_dvp, struct vnode **a_vpp, * struct componentname *a_cnp) */ static int procfs_lookup(struct vop_old_lookup_args *ap) { struct componentname *cnp = ap->a_cnp; struct vnode **vpp = ap->a_vpp; struct vnode *dvp = ap->a_dvp; char *pname = cnp->cn_nameptr; /* struct proc *curp = cnp->cn_proc; */ struct proc_target *pt; pid_t pid; struct pfsnode *pfs; struct proc *p; struct lwp *lp; int i; int error; *vpp = NULL; if (cnp->cn_nameiop == NAMEI_DELETE || cnp->cn_nameiop == NAMEI_RENAME) return (EROFS); p = NULL; error = 0; if (cnp->cn_namelen == 1 && *pname == '.') { *vpp = dvp; vref(*vpp); goto out; } pfs = VTOPFS(dvp); switch (pfs->pfs_type) { case Proot: if (cnp->cn_flags & CNP_ISDOTDOT) return (EIO); if (CNEQ(cnp, "curproc", 7) || CNEQ(cnp, "self", 4)) { error = procfs_allocvp(dvp->v_mount, vpp, 0, Pcurproc); goto out; } pid = atopid(pname, cnp->cn_namelen); if (pid == NO_PID) break; p = pfs_pfind(pid); if (p == NULL) break; if (!PRISON_CHECK(ap->a_cnp->cn_cred, p->p_ucred)) break; if (ps_showallprocs == 0 && ap->a_cnp->cn_cred->cr_uid != 0 && ap->a_cnp->cn_cred->cr_uid != p->p_ucred->cr_uid) break; error = procfs_allocvp(dvp->v_mount, vpp, pid, Pproc); goto out; case Pproc: if (cnp->cn_flags & CNP_ISDOTDOT) { error = procfs_root(dvp->v_mount, vpp); goto out; } p = pfs_pfind(pfs->pfs_pid); if (p == NULL) break; /* XXX lwp */ lp = FIRST_LWP_IN_PROC(p); if (lp == NULL) break; if (!PRISON_CHECK(ap->a_cnp->cn_cred, p->p_ucred)) break; if (ps_showallprocs == 0 && ap->a_cnp->cn_cred->cr_uid != 0 && ap->a_cnp->cn_cred->cr_uid != p->p_ucred->cr_uid) break; for (pt = proc_targets, i = 0; i < nproc_targets; pt++, i++) { if (cnp->cn_namelen == pt->pt_namlen && bcmp(pt->pt_name, pname, cnp->cn_namelen) == 0 && (pt->pt_valid == NULL || (*pt->pt_valid)(lp))) goto found; } break; found: error = procfs_allocvp(dvp->v_mount, vpp, pfs->pfs_pid, pt->pt_pfstype); goto out; default: error = ENOTDIR; goto out; } if (cnp->cn_nameiop == NAMEI_LOOKUP) error = ENOENT; else error = EROFS; /* * If no error occured *vpp will hold a referenced locked vnode. * dvp was passed to us locked and *vpp must be returned locked. * If *vpp != dvp then we should unlock dvp if (1) this is not the * last component or (2) CNP_LOCKPARENT is not set. */ out: if (error == 0 && *vpp != dvp) { if ((cnp->cn_flags & CNP_LOCKPARENT) == 0) { cnp->cn_flags |= CNP_PDIRUNLOCK; vn_unlock(dvp); } } pfs_pdone(p); return (error); } /* * Does this process have a text file? */ int procfs_validfile(struct lwp *lp) { return (procfs_findtextvp(lp->lwp_proc) != NULLVP); } /* * readdir() returns directory entries from pfsnode (vp). * * We generate just one directory entry at a time, as it would probably * not pay off to buffer several entries locally to save uiomove calls. * * procfs_readdir(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred, * int *a_eofflag, int *a_ncookies, off_t **a_cookies) */ static int procfs_readdir(struct vop_readdir_args *ap) { struct pfsnode *pfs; int error; if (ap->a_uio->uio_offset < 0 || ap->a_uio->uio_offset > INT_MAX) return (EINVAL); error = vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY | LK_FAILRECLAIM); if (error) return (error); pfs = VTOPFS(ap->a_vp); switch (pfs->pfs_type) { case Pproc: /* * this is for the process-specific sub-directories. * all that is needed to is copy out all the entries * from the procent[] table (top of this file). */ error = procfs_readdir_proc(ap); break; case Proot: /* * this is for the root of the procfs filesystem * what is needed is a special entry for "curproc" * followed by an entry for each process on allproc */ error = procfs_readdir_root(ap); break; default: error = ENOTDIR; break; } vn_unlock(ap->a_vp); return (error); } static int procfs_readdir_proc(struct vop_readdir_args *ap) { struct pfsnode *pfs; int error, i, retval; struct proc *p; struct lwp *lp; struct proc_target *pt; struct uio *uio = ap->a_uio; pfs = VTOPFS(ap->a_vp); p = pfs_pfind(pfs->pfs_pid); if (p == NULL) return(0); if (!PRISON_CHECK(ap->a_cred, p->p_ucred)) { error = 0; goto done; } /* XXX lwp, not MPSAFE */ lp = FIRST_LWP_IN_PROC(p); if (lp == NULL) { error = EINVAL; goto done; } error = 0; i = (int)uio->uio_offset; if (i < 0) { error = EINVAL; goto done; } for (pt = &proc_targets[i]; !error && uio->uio_resid > 0 && i < nproc_targets; pt++, i++) { if (pt->pt_valid && (*pt->pt_valid)(lp) == 0) continue; retval = vop_write_dirent(&error, uio, PROCFS_FILENO(pfs->pfs_pid, pt->pt_pfstype), pt->pt_type, pt->pt_namlen, pt->pt_name); if (retval) break; } uio->uio_offset = (off_t)i; error = 0; done: pfs_pdone(p); return error; } struct procfs_readdir_root_info { int error; int i; int pcnt; struct uio *uio; struct ucred *cred; }; static int procfs_readdir_root_callback(struct proc *p, void *data); static int procfs_readdir_root(struct vop_readdir_args *ap) { struct procfs_readdir_root_info info; struct uio *uio = ap->a_uio; int res; res = 0; info.error = 0; info.i = (int)uio->uio_offset; if (info.i < 0) return (EINVAL); info.pcnt = 0; info.uio = uio; info.cred = ap->a_cred; while (info.pcnt < 4) { res = procfs_readdir_root_callback(NULL, &info); if (res < 0) break; } if (res >= 0) allproc_scan(procfs_readdir_root_callback, &info, 0); uio->uio_offset = (off_t)info.i; return (info.error); } static int procfs_readdir_root_callback(struct proc *p, void *data) { struct procfs_readdir_root_info *info = data; struct uio *uio; int retval; ino_t d_ino; const char *d_name; char d_name_pid[20]; size_t d_namlen; uint8_t d_type; uio = info->uio; if (uio->uio_resid <= 0 || info->error) return(-1); switch (info->pcnt) { case 0: /* `.' */ d_ino = PROCFS_FILENO(0, Proot); d_name = "."; d_namlen = 1; d_type = DT_DIR; break; case 1: /* `..' */ d_ino = PROCFS_FILENO(0, Proot); d_name = ".."; d_namlen = 2; d_type = DT_DIR; break; case 2: d_ino = PROCFS_FILENO(0, Pcurproc); d_namlen = 7; d_name = "curproc"; d_type = DT_LNK; break; case 3: d_ino = PROCFS_FILENO(0, Pcurproc); d_namlen = 4; d_name = "self"; d_type = DT_LNK; break; default: if (!PRISON_CHECK(info->cred, p->p_ucred)) return(0); if (ps_showallprocs == 0 && info->cred->cr_uid != 0 && info->cred->cr_uid != p->p_ucred->cr_uid) { return(0); } /* * Skip entries we have already returned (optimization) */ if (info->pcnt < info->i) { ++info->pcnt; return(0); } d_ino = PROCFS_FILENO(p->p_pid, Pproc); d_namlen = ksnprintf(d_name_pid, sizeof(d_name_pid), "%ld", (long)p->p_pid); d_name = d_name_pid; d_type = DT_DIR; break; } /* * Skip entries we have already returned (optimization) */ if (info->pcnt < info->i) { ++info->pcnt; return(0); } retval = vop_write_dirent(&info->error, uio, d_ino, d_type, d_namlen, d_name); if (retval) return(-1); ++info->pcnt; ++info->i; return(0); } /* * readlink reads the link of `curproc' or `file' */ static int procfs_readlink(struct vop_readlink_args *ap) { char buf[16]; /* should be enough */ struct proc *procp; struct vnode *vp = ap->a_vp; struct pfsnode *pfs = VTOPFS(vp); char *fullpath, *freepath; int error, len; switch (pfs->pfs_type) { case Pcurproc: if (pfs->pfs_fileno != PROCFS_FILENO(0, Pcurproc)) return (EINVAL); len = ksnprintf(buf, sizeof(buf), "%ld", (long)curproc->p_pid); return (uiomove(buf, len, ap->a_uio)); case Pfile: /* * procfs's directory topology is somewhat asynchronous from * reality so it is possible for pid requests to race exiting * processes. In this situation, bit 31 is set in * pfs->pfs_pid which guarantees that pfs_pfind() will return * NULL. * * It is also possible to catch a process in the middle of * an exit sequence so various fields might wind up being * NULL that are not normally NULL. */ procp = pfs_pfind(pfs->pfs_pid); if (procp == NULL || procp->p_ucred == NULL) { pfs_pdone(procp); return (uiomove("unknown", sizeof("unknown") - 1, ap->a_uio)); } if (procp->p_textnch.ncp) { struct nchandle nch; cache_copy(&procp->p_textnch, &nch); error = cache_fullpath(procp, &nch, NULL, &fullpath, &freepath, 0); cache_drop(&nch); } else { error = EINVAL; } if (error != 0) { pfs_pdone(procp); return (uiomove("unknown", sizeof("unknown") - 1, ap->a_uio)); } error = uiomove(fullpath, strlen(fullpath), ap->a_uio); kfree(freepath, M_TEMP); pfs_pdone(procp); return (error); default: return (EINVAL); } } /* * convert decimal ascii to pid_t */ static pid_t atopid(const char *b, u_int len) { pid_t p = 0; while (len--) { char c = *b++; if (c < '0' || c > '9') return (NO_PID); p = 10 * p + (c - '0'); if (p > PID_MAX) return (NO_PID); } return (p); } /* * kqfilter operations */ static void procfs_filt_detach(struct knote *kn) { struct vnode *vp = (void *)kn->kn_hook; knote_remove(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn); } static int procfs_filt_read(struct knote *kn, long hint) { if (hint == NOTE_REVOKE) { kn->kn_flags |= (EV_EOF | EV_NODATA | EV_ONESHOT); return (1); } /* Files on procfs have a size of 0. */ kn->kn_data = 0; if (kn->kn_sfflags & NOTE_OLDAPI) return (1); return (kn->kn_data != 0); } static int procfs_filt_write(struct knote *kn, long hint) { if (hint == NOTE_REVOKE) kn->kn_flags |= (EV_EOF | EV_NODATA | EV_ONESHOT); kn->kn_data = 0; return (1); } static int procfs_filt_vnode(struct knote *kn, long hint) { if (kn->kn_sfflags & hint) kn->kn_fflags |= hint; if (hint == NOTE_REVOKE) { kn->kn_flags |= (EV_EOF | EV_NODATA); return (1); } return (kn->kn_fflags != 0); } static struct filterops procfs_read_filtops = { FILTEROP_ISFD | FILTEROP_MPSAFE, NULL, procfs_filt_detach, procfs_filt_read, }; static struct filterops procfs_write_filtops = { FILTEROP_ISFD | FILTEROP_MPSAFE, NULL, procfs_filt_detach, procfs_filt_write, }; static struct filterops procfs_vnode_filtops = { FILTEROP_ISFD | FILTEROP_MPSAFE, NULL, procfs_filt_detach, procfs_filt_vnode, }; static int procfs_kqfilter(struct vop_kqfilter_args *ap) { struct vnode *vp = ap->a_vp; struct knote *kn = ap->a_kn; switch (kn->kn_filter) { case EVFILT_READ: kn->kn_fop = &procfs_read_filtops; break; case EVFILT_WRITE: kn->kn_fop = &procfs_write_filtops; break; case EVFILT_VNODE: kn->kn_fop = &procfs_vnode_filtops; break; default: return (EOPNOTSUPP); } kn->kn_hook = (caddr_t)vp; knote_insert(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn); return (0); } |