642 */ 643static int 644do_dup(struct thread *td, enum dup_type type, int old, int new, 645 register_t *retval) 646{ 647 struct filedesc *fdp; 648 struct proc *p; 649 struct file *fp; 650 struct file *delfp; 651 int error, holdleaders, maxfd; 652 653 KASSERT((type == DUP_VARIABLE || type == DUP_FIXED), 654 ("invalid dup type %d", type)); 655 656 p = td->td_proc; 657 fdp = p->p_fd; 658 659 /* 660 * Verify we have a valid descriptor to dup from and possibly to 661 * dup to. 662 */ 663 if (old < 0 || new < 0) 664 return (EBADF); 665 PROC_LOCK(p); 666 maxfd = min((int)lim_cur(p, RLIMIT_NOFILE), maxfilesperproc); 667 PROC_UNLOCK(p); 668 if (new >= maxfd) 669 return (EMFILE); 670 671 FILEDESC_XLOCK(fdp); 672 if (old >= fdp->fd_nfiles || fdp->fd_ofiles[old] == NULL) { 673 FILEDESC_XUNLOCK(fdp); 674 return (EBADF); 675 } 676 if (type == DUP_FIXED && old == new) { 677 *retval = new; 678 FILEDESC_XUNLOCK(fdp); 679 return (0); 680 } 681 fp = fdp->fd_ofiles[old]; 682 fhold(fp); 683 684 /* 685 * If the caller specified a file descriptor, make sure the file 686 * table is large enough to hold it, and grab it. Otherwise, just 687 * allocate a new descriptor the usual way. Since the filedesc 688 * lock may be temporarily dropped in the process, we have to look 689 * out for a race. 690 */ 691 if (type == DUP_FIXED) { 692 if (new >= fdp->fd_nfiles) 693 fdgrowtable(fdp, new + 1); 694 if (fdp->fd_ofiles[new] == NULL) 695 fdused(fdp, new); 696 } else { 697 if ((error = fdalloc(td, new, &new)) != 0) { 698 FILEDESC_XUNLOCK(fdp); 699 fdrop(fp, td); 700 return (error); 701 } 702 } 703 704 /* 705 * If the old file changed out from under us then treat it as a 706 * bad file descriptor. Userland should do its own locking to 707 * avoid this case. 708 */ 709 if (fdp->fd_ofiles[old] != fp) { 710 /* we've allocated a descriptor which we won't use */ 711 if (fdp->fd_ofiles[new] == NULL) 712 fdunused(fdp, new); 713 FILEDESC_XUNLOCK(fdp); 714 fdrop(fp, td); 715 return (EBADF); 716 } 717 KASSERT(old != new, 718 ("new fd is same as old")); 719 720 /* 721 * Save info on the descriptor being overwritten. We cannot close 722 * it without introducing an ownership race for the slot, since we 723 * need to drop the filedesc lock to call closef(). 724 * 725 * XXX this duplicates parts of close(). 726 */ 727 delfp = fdp->fd_ofiles[new]; 728 holdleaders = 0; 729 if (delfp != NULL) { 730 if (td->td_proc->p_fdtol != NULL) { 731 /* 732 * Ask fdfree() to sleep to ensure that all relevant 733 * process leaders can be traversed in closef(). 734 */ 735 fdp->fd_holdleaderscount++; 736 holdleaders = 1; 737 } 738 } 739 740 /* 741 * Duplicate the source descriptor 742 */ 743 fdp->fd_ofiles[new] = fp; 744 fdp->fd_ofileflags[new] = fdp->fd_ofileflags[old] &~ UF_EXCLOSE; 745 if (new > fdp->fd_lastfile) 746 fdp->fd_lastfile = new; 747 *retval = new; 748 749 /* 750 * If we dup'd over a valid file, we now own the reference to it 751 * and must dispose of it using closef() semantics (as if a 752 * close() were performed on it). 753 * 754 * XXX this duplicates parts of close(). 755 */ 756 if (delfp != NULL) { 757 knote_fdclose(td, new); 758 if (delfp->f_type == DTYPE_MQUEUE) 759 mq_fdclose(td, new, delfp); 760 FILEDESC_XUNLOCK(fdp); 761 (void) closef(delfp, td); 762 if (holdleaders) { 763 FILEDESC_XLOCK(fdp); 764 fdp->fd_holdleaderscount--; 765 if (fdp->fd_holdleaderscount == 0 && 766 fdp->fd_holdleaderswakeup != 0) { 767 fdp->fd_holdleaderswakeup = 0; 768 wakeup(&fdp->fd_holdleaderscount); 769 } 770 FILEDESC_XUNLOCK(fdp); 771 } 772 } else { 773 FILEDESC_XUNLOCK(fdp); 774 } 775 return (0); 776} 777 778/* 779 * If sigio is on the list associated with a process or process group, 780 * disable signalling from the device, remove sigio from the list and 781 * free sigio. 782 */ 783void 784funsetown(struct sigio **sigiop) 785{ 786 struct sigio *sigio; 787 788 SIGIO_LOCK(); 789 sigio = *sigiop; 790 if (sigio == NULL) { 791 SIGIO_UNLOCK(); 792 return; 793 } 794 *(sigio->sio_myref) = NULL; 795 if ((sigio)->sio_pgid < 0) { 796 struct pgrp *pg = (sigio)->sio_pgrp; 797 PGRP_LOCK(pg); 798 SLIST_REMOVE(&sigio->sio_pgrp->pg_sigiolst, sigio, 799 sigio, sio_pgsigio); 800 PGRP_UNLOCK(pg); 801 } else { 802 struct proc *p = (sigio)->sio_proc; 803 PROC_LOCK(p); 804 SLIST_REMOVE(&sigio->sio_proc->p_sigiolst, sigio, 805 sigio, sio_pgsigio); 806 PROC_UNLOCK(p); 807 } 808 SIGIO_UNLOCK(); 809 crfree(sigio->sio_ucred); 810 FREE(sigio, M_SIGIO); 811} 812 813/* 814 * Free a list of sigio structures. 815 * We only need to lock the SIGIO_LOCK because we have made ourselves 816 * inaccessible to callers of fsetown and therefore do not need to lock 817 * the proc or pgrp struct for the list manipulation. 818 */ 819void 820funsetownlst(struct sigiolst *sigiolst) 821{ 822 struct proc *p; 823 struct pgrp *pg; 824 struct sigio *sigio; 825 826 sigio = SLIST_FIRST(sigiolst); 827 if (sigio == NULL) 828 return; 829 p = NULL; 830 pg = NULL; 831 832 /* 833 * Every entry of the list should belong 834 * to a single proc or pgrp. 835 */ 836 if (sigio->sio_pgid < 0) { 837 pg = sigio->sio_pgrp; 838 PGRP_LOCK_ASSERT(pg, MA_NOTOWNED); 839 } else /* if (sigio->sio_pgid > 0) */ { 840 p = sigio->sio_proc; 841 PROC_LOCK_ASSERT(p, MA_NOTOWNED); 842 } 843 844 SIGIO_LOCK(); 845 while ((sigio = SLIST_FIRST(sigiolst)) != NULL) { 846 *(sigio->sio_myref) = NULL; 847 if (pg != NULL) { 848 KASSERT(sigio->sio_pgid < 0, 849 ("Proc sigio in pgrp sigio list")); 850 KASSERT(sigio->sio_pgrp == pg, 851 ("Bogus pgrp in sigio list")); 852 PGRP_LOCK(pg); 853 SLIST_REMOVE(&pg->pg_sigiolst, sigio, sigio, 854 sio_pgsigio); 855 PGRP_UNLOCK(pg); 856 } else /* if (p != NULL) */ { 857 KASSERT(sigio->sio_pgid > 0, 858 ("Pgrp sigio in proc sigio list")); 859 KASSERT(sigio->sio_proc == p, 860 ("Bogus proc in sigio list")); 861 PROC_LOCK(p); 862 SLIST_REMOVE(&p->p_sigiolst, sigio, sigio, 863 sio_pgsigio); 864 PROC_UNLOCK(p); 865 } 866 SIGIO_UNLOCK(); 867 crfree(sigio->sio_ucred); 868 FREE(sigio, M_SIGIO); 869 SIGIO_LOCK(); 870 } 871 SIGIO_UNLOCK(); 872} 873 874/* 875 * This is common code for FIOSETOWN ioctl called by fcntl(fd, F_SETOWN, arg). 876 * 877 * After permission checking, add a sigio structure to the sigio list for 878 * the process or process group. 879 */ 880int 881fsetown(pid_t pgid, struct sigio **sigiop) 882{ 883 struct proc *proc; 884 struct pgrp *pgrp; 885 struct sigio *sigio; 886 int ret; 887 888 if (pgid == 0) { 889 funsetown(sigiop); 890 return (0); 891 } 892 893 ret = 0; 894 895 /* Allocate and fill in the new sigio out of locks. */ 896 MALLOC(sigio, struct sigio *, sizeof(struct sigio), M_SIGIO, M_WAITOK); 897 sigio->sio_pgid = pgid; 898 sigio->sio_ucred = crhold(curthread->td_ucred); 899 sigio->sio_myref = sigiop; 900 901 sx_slock(&proctree_lock); 902 if (pgid > 0) { 903 proc = pfind(pgid); 904 if (proc == NULL) { 905 ret = ESRCH; 906 goto fail; 907 } 908 909 /* 910 * Policy - Don't allow a process to FSETOWN a process 911 * in another session. 912 * 913 * Remove this test to allow maximum flexibility or 914 * restrict FSETOWN to the current process or process 915 * group for maximum safety. 916 */ 917 PROC_UNLOCK(proc); 918 if (proc->p_session != curthread->td_proc->p_session) { 919 ret = EPERM; 920 goto fail; 921 } 922 923 pgrp = NULL; 924 } else /* if (pgid < 0) */ { 925 pgrp = pgfind(-pgid); 926 if (pgrp == NULL) { 927 ret = ESRCH; 928 goto fail; 929 } 930 PGRP_UNLOCK(pgrp); 931 932 /* 933 * Policy - Don't allow a process to FSETOWN a process 934 * in another session. 935 * 936 * Remove this test to allow maximum flexibility or 937 * restrict FSETOWN to the current process or process 938 * group for maximum safety. 939 */ 940 if (pgrp->pg_session != curthread->td_proc->p_session) { 941 ret = EPERM; 942 goto fail; 943 } 944 945 proc = NULL; 946 } 947 funsetown(sigiop); 948 if (pgid > 0) { 949 PROC_LOCK(proc); 950 /* 951 * Since funsetownlst() is called without the proctree 952 * locked, we need to check for P_WEXIT. 953 * XXX: is ESRCH correct? 954 */ 955 if ((proc->p_flag & P_WEXIT) != 0) { 956 PROC_UNLOCK(proc); 957 ret = ESRCH; 958 goto fail; 959 } 960 SLIST_INSERT_HEAD(&proc->p_sigiolst, sigio, sio_pgsigio); 961 sigio->sio_proc = proc; 962 PROC_UNLOCK(proc); 963 } else { 964 PGRP_LOCK(pgrp); 965 SLIST_INSERT_HEAD(&pgrp->pg_sigiolst, sigio, sio_pgsigio); 966 sigio->sio_pgrp = pgrp; 967 PGRP_UNLOCK(pgrp); 968 } 969 sx_sunlock(&proctree_lock); 970 SIGIO_LOCK(); 971 *sigiop = sigio; 972 SIGIO_UNLOCK(); 973 return (0); 974 975fail: 976 sx_sunlock(&proctree_lock); 977 crfree(sigio->sio_ucred); 978 FREE(sigio, M_SIGIO); 979 return (ret); 980} 981 982/* 983 * This is common code for FIOGETOWN ioctl called by fcntl(fd, F_GETOWN, arg). 984 */ 985pid_t 986fgetown(sigiop) 987 struct sigio **sigiop; 988{ 989 pid_t pgid; 990 991 SIGIO_LOCK(); 992 pgid = (*sigiop != NULL) ? (*sigiop)->sio_pgid : 0; 993 SIGIO_UNLOCK(); 994 return (pgid); 995} 996 997/* 998 * Close a file descriptor. 999 */ 1000#ifndef _SYS_SYSPROTO_H_ 1001struct close_args { 1002 int fd; 1003}; 1004#endif 1005/* ARGSUSED */ 1006int 1007close(td, uap) 1008 struct thread *td; 1009 struct close_args *uap; 1010{ 1011 1012 return (kern_close(td, uap->fd)); 1013} 1014 1015int 1016kern_close(td, fd) 1017 struct thread *td; 1018 int fd; 1019{ 1020 struct filedesc *fdp; 1021 struct file *fp; 1022 int error; 1023 int holdleaders; 1024 1025 error = 0; 1026 holdleaders = 0; 1027 fdp = td->td_proc->p_fd; 1028 1029 AUDIT_SYSCLOSE(td, fd); 1030 1031 FILEDESC_XLOCK(fdp); 1032 if ((unsigned)fd >= fdp->fd_nfiles || 1033 (fp = fdp->fd_ofiles[fd]) == NULL) { 1034 FILEDESC_XUNLOCK(fdp); 1035 return (EBADF); 1036 } 1037 fdp->fd_ofiles[fd] = NULL; 1038 fdp->fd_ofileflags[fd] = 0; 1039 fdunused(fdp, fd); 1040 if (td->td_proc->p_fdtol != NULL) { 1041 /* 1042 * Ask fdfree() to sleep to ensure that all relevant 1043 * process leaders can be traversed in closef(). 1044 */ 1045 fdp->fd_holdleaderscount++; 1046 holdleaders = 1; 1047 } 1048 1049 /* 1050 * We now hold the fp reference that used to be owned by the 1051 * descriptor array. We have to unlock the FILEDESC *AFTER* 1052 * knote_fdclose to prevent a race of the fd getting opened, a knote 1053 * added, and deleteing a knote for the new fd. 1054 */ 1055 knote_fdclose(td, fd); 1056 if (fp->f_type == DTYPE_MQUEUE) 1057 mq_fdclose(td, fd, fp); 1058 FILEDESC_XUNLOCK(fdp); 1059 1060 error = closef(fp, td); 1061 if (holdleaders) { 1062 FILEDESC_XLOCK(fdp); 1063 fdp->fd_holdleaderscount--; 1064 if (fdp->fd_holdleaderscount == 0 && 1065 fdp->fd_holdleaderswakeup != 0) { 1066 fdp->fd_holdleaderswakeup = 0; 1067 wakeup(&fdp->fd_holdleaderscount); 1068 } 1069 FILEDESC_XUNLOCK(fdp); 1070 } 1071 return (error); 1072} 1073 1074#if defined(COMPAT_43) 1075/* 1076 * Return status information about a file descriptor. 1077 */ 1078#ifndef _SYS_SYSPROTO_H_ 1079struct ofstat_args { 1080 int fd; 1081 struct ostat *sb; 1082}; 1083#endif 1084/* ARGSUSED */ 1085int 1086ofstat(struct thread *td, struct ofstat_args *uap) 1087{ 1088 struct ostat oub; 1089 struct stat ub; 1090 int error; 1091 1092 error = kern_fstat(td, uap->fd, &ub); 1093 if (error == 0) { 1094 cvtstat(&ub, &oub); 1095 error = copyout(&oub, uap->sb, sizeof(oub)); 1096 } 1097 return (error); 1098} 1099#endif /* COMPAT_43 */ 1100 1101/* 1102 * Return status information about a file descriptor. 1103 */ 1104#ifndef _SYS_SYSPROTO_H_ 1105struct fstat_args { 1106 int fd; 1107 struct stat *sb; 1108}; 1109#endif 1110/* ARGSUSED */ 1111int 1112fstat(struct thread *td, struct fstat_args *uap) 1113{ 1114 struct stat ub; 1115 int error; 1116 1117 error = kern_fstat(td, uap->fd, &ub); 1118 if (error == 0) 1119 error = copyout(&ub, uap->sb, sizeof(ub)); 1120 return (error); 1121} 1122 1123int 1124kern_fstat(struct thread *td, int fd, struct stat *sbp) 1125{ 1126 struct file *fp; 1127 int error; 1128 1129 AUDIT_ARG(fd, fd); 1130 1131 if ((error = fget(td, fd, &fp)) != 0) 1132 return (error); 1133 1134 AUDIT_ARG(file, td->td_proc, fp); 1135 1136 error = fo_stat(fp, sbp, td->td_ucred, td); 1137 fdrop(fp, td); 1138#ifdef KTRACE 1139 if (error == 0 && KTRPOINT(td, KTR_STRUCT)) 1140 ktrstat(sbp); 1141#endif 1142 return (error); 1143} 1144 1145/* 1146 * Return status information about a file descriptor. 1147 */ 1148#ifndef _SYS_SYSPROTO_H_ 1149struct nfstat_args { 1150 int fd; 1151 struct nstat *sb; 1152}; 1153#endif 1154/* ARGSUSED */ 1155int 1156nfstat(struct thread *td, struct nfstat_args *uap) 1157{ 1158 struct nstat nub; 1159 struct stat ub; 1160 int error; 1161 1162 error = kern_fstat(td, uap->fd, &ub); 1163 if (error == 0) { 1164 cvtnstat(&ub, &nub); 1165 error = copyout(&nub, uap->sb, sizeof(nub)); 1166 } 1167 return (error); 1168} 1169 1170/* 1171 * Return pathconf information about a file descriptor. 1172 */ 1173#ifndef _SYS_SYSPROTO_H_ 1174struct fpathconf_args { 1175 int fd; 1176 int name; 1177}; 1178#endif 1179/* ARGSUSED */ 1180int 1181fpathconf(struct thread *td, struct fpathconf_args *uap) 1182{ 1183 struct file *fp; 1184 struct vnode *vp; 1185 int error; 1186 1187 if ((error = fget(td, uap->fd, &fp)) != 0) 1188 return (error); 1189 1190 /* If asynchronous I/O is available, it works for all descriptors. */ 1191 if (uap->name == _PC_ASYNC_IO) { 1192 td->td_retval[0] = async_io_version; 1193 goto out; 1194 } 1195 vp = fp->f_vnode; 1196 if (vp != NULL) { 1197 int vfslocked; 1198 vfslocked = VFS_LOCK_GIANT(vp->v_mount); 1199 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 1200 error = VOP_PATHCONF(vp, uap->name, td->td_retval); 1201 VOP_UNLOCK(vp, 0); 1202 VFS_UNLOCK_GIANT(vfslocked); 1203 } else if (fp->f_type == DTYPE_PIPE || fp->f_type == DTYPE_SOCKET) { 1204 if (uap->name != _PC_PIPE_BUF) { 1205 error = EINVAL; 1206 } else { 1207 td->td_retval[0] = PIPE_BUF; 1208 error = 0; 1209 } 1210 } else { 1211 error = EOPNOTSUPP; 1212 } 1213out: 1214 fdrop(fp, td); 1215 return (error); 1216} 1217 1218/* 1219 * Grow the file table to accomodate (at least) nfd descriptors. This may 1220 * block and drop the filedesc lock, but it will reacquire it before 1221 * returning. 1222 */ 1223static void 1224fdgrowtable(struct filedesc *fdp, int nfd) 1225{ 1226 struct file **ntable; 1227 char *nfileflags; 1228 int nnfiles, onfiles; 1229 NDSLOTTYPE *nmap; 1230 1231 FILEDESC_XLOCK_ASSERT(fdp); 1232 1233 KASSERT(fdp->fd_nfiles > 0, 1234 ("zero-length file table")); 1235 1236 /* compute the size of the new table */ 1237 onfiles = fdp->fd_nfiles; 1238 nnfiles = NDSLOTS(nfd) * NDENTRIES; /* round up */ 1239 if (nnfiles <= onfiles) 1240 /* the table is already large enough */ 1241 return; 1242 1243 /* allocate a new table and (if required) new bitmaps */ 1244 FILEDESC_XUNLOCK(fdp); 1245 MALLOC(ntable, struct file **, nnfiles * OFILESIZE, 1246 M_FILEDESC, M_ZERO | M_WAITOK); 1247 nfileflags = (char *)&ntable[nnfiles]; 1248 if (NDSLOTS(nnfiles) > NDSLOTS(onfiles)) 1249 MALLOC(nmap, NDSLOTTYPE *, NDSLOTS(nnfiles) * NDSLOTSIZE, 1250 M_FILEDESC, M_ZERO | M_WAITOK); 1251 else 1252 nmap = NULL; 1253 FILEDESC_XLOCK(fdp); 1254 1255 /* 1256 * We now have new tables ready to go. Since we dropped the 1257 * filedesc lock to call malloc(), watch out for a race. 1258 */ 1259 onfiles = fdp->fd_nfiles; 1260 if (onfiles >= nnfiles) { 1261 /* we lost the race, but that's OK */ 1262 free(ntable, M_FILEDESC); 1263 if (nmap != NULL) 1264 free(nmap, M_FILEDESC); 1265 return; 1266 } 1267 bcopy(fdp->fd_ofiles, ntable, onfiles * sizeof(*ntable)); 1268 bcopy(fdp->fd_ofileflags, nfileflags, onfiles); 1269 if (onfiles > NDFILE) 1270 free(fdp->fd_ofiles, M_FILEDESC); 1271 fdp->fd_ofiles = ntable; 1272 fdp->fd_ofileflags = nfileflags; 1273 if (NDSLOTS(nnfiles) > NDSLOTS(onfiles)) { 1274 bcopy(fdp->fd_map, nmap, NDSLOTS(onfiles) * sizeof(*nmap)); 1275 if (NDSLOTS(onfiles) > NDSLOTS(NDFILE)) 1276 free(fdp->fd_map, M_FILEDESC); 1277 fdp->fd_map = nmap; 1278 } 1279 fdp->fd_nfiles = nnfiles; 1280} 1281 1282/* 1283 * Allocate a file descriptor for the process. 1284 */ 1285int 1286fdalloc(struct thread *td, int minfd, int *result) 1287{ 1288 struct proc *p = td->td_proc; 1289 struct filedesc *fdp = p->p_fd; 1290 int fd = -1, maxfd; 1291 1292 FILEDESC_XLOCK_ASSERT(fdp); 1293 1294 if (fdp->fd_freefile > minfd) 1295 minfd = fdp->fd_freefile; 1296 1297 PROC_LOCK(p); 1298 maxfd = min((int)lim_cur(p, RLIMIT_NOFILE), maxfilesperproc); 1299 PROC_UNLOCK(p); 1300 1301 /* 1302 * Search the bitmap for a free descriptor. If none is found, try 1303 * to grow the file table. Keep at it until we either get a file 1304 * descriptor or run into process or system limits; fdgrowtable() 1305 * may drop the filedesc lock, so we're in a race. 1306 */ 1307 for (;;) { 1308 fd = fd_first_free(fdp, minfd, fdp->fd_nfiles); 1309 if (fd >= maxfd) 1310 return (EMFILE); 1311 if (fd < fdp->fd_nfiles) 1312 break; 1313 fdgrowtable(fdp, min(fdp->fd_nfiles * 2, maxfd)); 1314 } 1315 1316 /* 1317 * Perform some sanity checks, then mark the file descriptor as 1318 * used and return it to the caller. 1319 */ 1320 KASSERT(!fdisused(fdp, fd), 1321 ("fd_first_free() returned non-free descriptor")); 1322 KASSERT(fdp->fd_ofiles[fd] == NULL, 1323 ("free descriptor isn't")); 1324 fdp->fd_ofileflags[fd] = 0; /* XXX needed? */ 1325 fdused(fdp, fd); 1326 *result = fd; 1327 return (0); 1328} 1329 1330/* 1331 * Check to see whether n user file descriptors are available to the process 1332 * p. 1333 */ 1334int 1335fdavail(struct thread *td, int n) 1336{ 1337 struct proc *p = td->td_proc; 1338 struct filedesc *fdp = td->td_proc->p_fd; 1339 struct file **fpp; 1340 int i, lim, last; 1341 1342 FILEDESC_LOCK_ASSERT(fdp); 1343 1344 PROC_LOCK(p); 1345 lim = min((int)lim_cur(p, RLIMIT_NOFILE), maxfilesperproc); 1346 PROC_UNLOCK(p); 1347 if ((i = lim - fdp->fd_nfiles) > 0 && (n -= i) <= 0) 1348 return (1); 1349 last = min(fdp->fd_nfiles, lim); 1350 fpp = &fdp->fd_ofiles[fdp->fd_freefile]; 1351 for (i = last - fdp->fd_freefile; --i >= 0; fpp++) { 1352 if (*fpp == NULL && --n <= 0) 1353 return (1); 1354 } 1355 return (0); 1356} 1357 1358/* 1359 * Create a new open file structure and allocate a file decriptor for the 1360 * process that refers to it. We add one reference to the file for the 1361 * descriptor table and one reference for resultfp. This is to prevent us 1362 * being preempted and the entry in the descriptor table closed after we 1363 * release the FILEDESC lock. 1364 */ 1365int 1366falloc(struct thread *td, struct file **resultfp, int *resultfd) 1367{ 1368 struct proc *p = td->td_proc; 1369 struct file *fp; 1370 int error, i; 1371 int maxuserfiles = maxfiles - (maxfiles / 20); 1372 static struct timeval lastfail; 1373 static int curfail; 1374 1375 fp = uma_zalloc(file_zone, M_WAITOK | M_ZERO); 1376 if ((openfiles >= maxuserfiles && 1377 priv_check(td, PRIV_MAXFILES) != 0) || 1378 openfiles >= maxfiles) { 1379 if (ppsratecheck(&lastfail, &curfail, 1)) { 1380 printf("kern.maxfiles limit exceeded by uid %i, please see tuning(7).\n", 1381 td->td_ucred->cr_ruid); 1382 } 1383 uma_zfree(file_zone, fp); 1384 return (ENFILE); 1385 } 1386 atomic_add_int(&openfiles, 1); 1387 1388 /* 1389 * If the process has file descriptor zero open, add the new file 1390 * descriptor to the list of open files at that point, otherwise 1391 * put it at the front of the list of open files. 1392 */ 1393 fp->f_count = 1; 1394 if (resultfp) 1395 fp->f_count++; 1396 fp->f_cred = crhold(td->td_ucred); 1397 fp->f_ops = &badfileops; 1398 fp->f_data = NULL; 1399 fp->f_vnode = NULL; 1400 FILEDESC_XLOCK(p->p_fd); 1401 if ((error = fdalloc(td, 0, &i))) { 1402 FILEDESC_XUNLOCK(p->p_fd); 1403 fdrop(fp, td); 1404 if (resultfp) 1405 fdrop(fp, td); 1406 return (error); 1407 } 1408 p->p_fd->fd_ofiles[i] = fp; 1409 FILEDESC_XUNLOCK(p->p_fd); 1410 if (resultfp) 1411 *resultfp = fp; 1412 if (resultfd) 1413 *resultfd = i; 1414 return (0); 1415} 1416 1417/* 1418 * Build a new filedesc structure from another. 1419 * Copy the current, root, and jail root vnode references. 1420 */ 1421struct filedesc * 1422fdinit(struct filedesc *fdp) 1423{ 1424 struct filedesc0 *newfdp; 1425 1426 newfdp = malloc(sizeof *newfdp, M_FILEDESC, M_WAITOK | M_ZERO); 1427 FILEDESC_LOCK_INIT(&newfdp->fd_fd); 1428 if (fdp != NULL) { 1429 FILEDESC_XLOCK(fdp); 1430 newfdp->fd_fd.fd_cdir = fdp->fd_cdir; 1431 if (newfdp->fd_fd.fd_cdir) 1432 VREF(newfdp->fd_fd.fd_cdir); 1433 newfdp->fd_fd.fd_rdir = fdp->fd_rdir; 1434 if (newfdp->fd_fd.fd_rdir) 1435 VREF(newfdp->fd_fd.fd_rdir); 1436 newfdp->fd_fd.fd_jdir = fdp->fd_jdir; 1437 if (newfdp->fd_fd.fd_jdir) 1438 VREF(newfdp->fd_fd.fd_jdir); 1439 FILEDESC_XUNLOCK(fdp); 1440 } 1441 1442 /* Create the file descriptor table. */ 1443 newfdp->fd_fd.fd_refcnt = 1; 1444 newfdp->fd_fd.fd_holdcnt = 1; 1445 newfdp->fd_fd.fd_cmask = CMASK; 1446 newfdp->fd_fd.fd_ofiles = newfdp->fd_dfiles; 1447 newfdp->fd_fd.fd_ofileflags = newfdp->fd_dfileflags; 1448 newfdp->fd_fd.fd_nfiles = NDFILE; 1449 newfdp->fd_fd.fd_map = newfdp->fd_dmap; 1450 newfdp->fd_fd.fd_lastfile = -1; 1451 return (&newfdp->fd_fd); 1452} 1453 1454static struct filedesc * 1455fdhold(struct proc *p) 1456{ 1457 struct filedesc *fdp; 1458 1459 mtx_lock(&fdesc_mtx); 1460 fdp = p->p_fd; 1461 if (fdp != NULL) 1462 fdp->fd_holdcnt++; 1463 mtx_unlock(&fdesc_mtx); 1464 return (fdp); 1465} 1466 1467static void 1468fddrop(struct filedesc *fdp) 1469{ 1470 int i; 1471 1472 mtx_lock(&fdesc_mtx); 1473 i = --fdp->fd_holdcnt; 1474 mtx_unlock(&fdesc_mtx); 1475 if (i > 0) 1476 return; 1477 1478 FILEDESC_LOCK_DESTROY(fdp); 1479 FREE(fdp, M_FILEDESC); 1480} 1481 1482/* 1483 * Share a filedesc structure. 1484 */ 1485struct filedesc * 1486fdshare(struct filedesc *fdp) 1487{ 1488 1489 FILEDESC_XLOCK(fdp); 1490 fdp->fd_refcnt++; 1491 FILEDESC_XUNLOCK(fdp); 1492 return (fdp); 1493} 1494 1495/* 1496 * Unshare a filedesc structure, if necessary by making a copy 1497 */ 1498void 1499fdunshare(struct proc *p, struct thread *td) 1500{ 1501 1502 FILEDESC_XLOCK(p->p_fd); 1503 if (p->p_fd->fd_refcnt > 1) { 1504 struct filedesc *tmp; 1505 1506 FILEDESC_XUNLOCK(p->p_fd); 1507 tmp = fdcopy(p->p_fd); 1508 fdfree(td); 1509 p->p_fd = tmp; 1510 } else 1511 FILEDESC_XUNLOCK(p->p_fd); 1512} 1513 1514/* 1515 * Copy a filedesc structure. A NULL pointer in returns a NULL reference, 1516 * this is to ease callers, not catch errors. 1517 */ 1518struct filedesc * 1519fdcopy(struct filedesc *fdp) 1520{ 1521 struct filedesc *newfdp; 1522 int i; 1523 1524 /* Certain daemons might not have file descriptors. */ 1525 if (fdp == NULL) 1526 return (NULL); 1527 1528 newfdp = fdinit(fdp); 1529 FILEDESC_SLOCK(fdp); 1530 while (fdp->fd_lastfile >= newfdp->fd_nfiles) { 1531 FILEDESC_SUNLOCK(fdp); 1532 FILEDESC_XLOCK(newfdp); 1533 fdgrowtable(newfdp, fdp->fd_lastfile + 1); 1534 FILEDESC_XUNLOCK(newfdp); 1535 FILEDESC_SLOCK(fdp); 1536 } 1537 /* copy everything except kqueue descriptors */ 1538 newfdp->fd_freefile = -1; 1539 for (i = 0; i <= fdp->fd_lastfile; ++i) { 1540 if (fdisused(fdp, i) && 1541 fdp->fd_ofiles[i]->f_type != DTYPE_KQUEUE) { 1542 newfdp->fd_ofiles[i] = fdp->fd_ofiles[i]; 1543 newfdp->fd_ofileflags[i] = fdp->fd_ofileflags[i]; 1544 fhold(newfdp->fd_ofiles[i]); 1545 newfdp->fd_lastfile = i; 1546 } else { 1547 if (newfdp->fd_freefile == -1) 1548 newfdp->fd_freefile = i; 1549 } 1550 } 1551 FILEDESC_SUNLOCK(fdp); 1552 FILEDESC_XLOCK(newfdp); 1553 for (i = 0; i <= newfdp->fd_lastfile; ++i) 1554 if (newfdp->fd_ofiles[i] != NULL) 1555 fdused(newfdp, i); 1556 FILEDESC_XUNLOCK(newfdp); 1557 FILEDESC_SLOCK(fdp); 1558 if (newfdp->fd_freefile == -1) 1559 newfdp->fd_freefile = i; 1560 newfdp->fd_cmask = fdp->fd_cmask; 1561 FILEDESC_SUNLOCK(fdp); 1562 return (newfdp); 1563} 1564 1565/* 1566 * Release a filedesc structure. 1567 */ 1568void 1569fdfree(struct thread *td) 1570{ 1571 struct filedesc *fdp; 1572 struct file **fpp; 1573 int i, locked; 1574 struct filedesc_to_leader *fdtol; 1575 struct file *fp; 1576 struct vnode *cdir, *jdir, *rdir, *vp; 1577 struct flock lf; 1578 1579 /* Certain daemons might not have file descriptors. */ 1580 fdp = td->td_proc->p_fd; 1581 if (fdp == NULL) 1582 return; 1583 1584 /* Check for special need to clear POSIX style locks */ 1585 fdtol = td->td_proc->p_fdtol; 1586 if (fdtol != NULL) { 1587 FILEDESC_XLOCK(fdp); 1588 KASSERT(fdtol->fdl_refcount > 0, 1589 ("filedesc_to_refcount botch: fdl_refcount=%d", 1590 fdtol->fdl_refcount)); 1591 if (fdtol->fdl_refcount == 1 && 1592 (td->td_proc->p_leader->p_flag & P_ADVLOCK) != 0) { 1593 for (i = 0, fpp = fdp->fd_ofiles; 1594 i <= fdp->fd_lastfile; 1595 i++, fpp++) { 1596 if (*fpp == NULL || 1597 (*fpp)->f_type != DTYPE_VNODE) 1598 continue; 1599 fp = *fpp; 1600 fhold(fp); 1601 FILEDESC_XUNLOCK(fdp); 1602 lf.l_whence = SEEK_SET; 1603 lf.l_start = 0; 1604 lf.l_len = 0; 1605 lf.l_type = F_UNLCK; 1606 vp = fp->f_vnode; 1607 locked = VFS_LOCK_GIANT(vp->v_mount); 1608 (void) VOP_ADVLOCK(vp, 1609 (caddr_t)td->td_proc-> 1610 p_leader, 1611 F_UNLCK, 1612 &lf, 1613 F_POSIX); 1614 VFS_UNLOCK_GIANT(locked); 1615 FILEDESC_XLOCK(fdp); 1616 fdrop(fp, td); 1617 fpp = fdp->fd_ofiles + i; 1618 } 1619 } 1620 retry: 1621 if (fdtol->fdl_refcount == 1) { 1622 if (fdp->fd_holdleaderscount > 0 && 1623 (td->td_proc->p_leader->p_flag & P_ADVLOCK) != 0) { 1624 /* 1625 * close() or do_dup() has cleared a reference 1626 * in a shared file descriptor table. 1627 */ 1628 fdp->fd_holdleaderswakeup = 1; 1629 sx_sleep(&fdp->fd_holdleaderscount, 1630 FILEDESC_LOCK(fdp), PLOCK, "fdlhold", 0); 1631 goto retry; 1632 } 1633 if (fdtol->fdl_holdcount > 0) { 1634 /* 1635 * Ensure that fdtol->fdl_leader remains 1636 * valid in closef(). 1637 */ 1638 fdtol->fdl_wakeup = 1; 1639 sx_sleep(fdtol, FILEDESC_LOCK(fdp), PLOCK, 1640 "fdlhold", 0); 1641 goto retry; 1642 } 1643 } 1644 fdtol->fdl_refcount--; 1645 if (fdtol->fdl_refcount == 0 && 1646 fdtol->fdl_holdcount == 0) { 1647 fdtol->fdl_next->fdl_prev = fdtol->fdl_prev; 1648 fdtol->fdl_prev->fdl_next = fdtol->fdl_next; 1649 } else 1650 fdtol = NULL; 1651 td->td_proc->p_fdtol = NULL; 1652 FILEDESC_XUNLOCK(fdp); 1653 if (fdtol != NULL) 1654 FREE(fdtol, M_FILEDESC_TO_LEADER); 1655 } 1656 FILEDESC_XLOCK(fdp); 1657 i = --fdp->fd_refcnt; 1658 FILEDESC_XUNLOCK(fdp); 1659 if (i > 0) 1660 return; 1661 /* 1662 * We are the last reference to the structure, so we can 1663 * safely assume it will not change out from under us. 1664 */ 1665 fpp = fdp->fd_ofiles; 1666 for (i = fdp->fd_lastfile; i-- >= 0; fpp++) { 1667 if (*fpp) 1668 (void) closef(*fpp, td); 1669 } 1670 FILEDESC_XLOCK(fdp); 1671 1672 /* XXX This should happen earlier. */ 1673 mtx_lock(&fdesc_mtx); 1674 td->td_proc->p_fd = NULL; 1675 mtx_unlock(&fdesc_mtx); 1676 1677 if (fdp->fd_nfiles > NDFILE) 1678 FREE(fdp->fd_ofiles, M_FILEDESC); 1679 if (NDSLOTS(fdp->fd_nfiles) > NDSLOTS(NDFILE)) 1680 FREE(fdp->fd_map, M_FILEDESC); 1681 1682 fdp->fd_nfiles = 0; 1683 1684 cdir = fdp->fd_cdir; 1685 fdp->fd_cdir = NULL; 1686 rdir = fdp->fd_rdir; 1687 fdp->fd_rdir = NULL; 1688 jdir = fdp->fd_jdir; 1689 fdp->fd_jdir = NULL; 1690 FILEDESC_XUNLOCK(fdp); 1691 1692 if (cdir) { 1693 locked = VFS_LOCK_GIANT(cdir->v_mount); 1694 vrele(cdir); 1695 VFS_UNLOCK_GIANT(locked); 1696 } 1697 if (rdir) { 1698 locked = VFS_LOCK_GIANT(rdir->v_mount); 1699 vrele(rdir); 1700 VFS_UNLOCK_GIANT(locked); 1701 } 1702 if (jdir) { 1703 locked = VFS_LOCK_GIANT(jdir->v_mount); 1704 vrele(jdir); 1705 VFS_UNLOCK_GIANT(locked); 1706 } 1707 1708 fddrop(fdp); 1709} 1710 1711/* 1712 * For setugid programs, we don't want to people to use that setugidness 1713 * to generate error messages which write to a file which otherwise would 1714 * otherwise be off-limits to the process. We check for filesystems where 1715 * the vnode can change out from under us after execve (like [lin]procfs). 1716 * 1717 * Since setugidsafety calls this only for fd 0, 1 and 2, this check is 1718 * sufficient. We also don't check for setugidness since we know we are. 1719 */ 1720static int 1721is_unsafe(struct file *fp) 1722{ 1723 if (fp->f_type == DTYPE_VNODE) { 1724 struct vnode *vp = fp->f_vnode; 1725 1726 if ((vp->v_vflag & VV_PROCDEP) != 0) 1727 return (1); 1728 } 1729 return (0); 1730} 1731 1732/* 1733 * Make this setguid thing safe, if at all possible. 1734 */ 1735void 1736setugidsafety(struct thread *td) 1737{ 1738 struct filedesc *fdp; 1739 int i; 1740 1741 /* Certain daemons might not have file descriptors. */ 1742 fdp = td->td_proc->p_fd; 1743 if (fdp == NULL) 1744 return; 1745 1746 /* 1747 * Note: fdp->fd_ofiles may be reallocated out from under us while 1748 * we are blocked in a close. Be careful! 1749 */ 1750 FILEDESC_XLOCK(fdp); 1751 for (i = 0; i <= fdp->fd_lastfile; i++) { 1752 if (i > 2) 1753 break; 1754 if (fdp->fd_ofiles[i] && is_unsafe(fdp->fd_ofiles[i])) { 1755 struct file *fp; 1756 1757 knote_fdclose(td, i); 1758 /* 1759 * NULL-out descriptor prior to close to avoid 1760 * a race while close blocks. 1761 */ 1762 fp = fdp->fd_ofiles[i]; 1763 fdp->fd_ofiles[i] = NULL; 1764 fdp->fd_ofileflags[i] = 0; 1765 fdunused(fdp, i); 1766 FILEDESC_XUNLOCK(fdp); 1767 (void) closef(fp, td); 1768 FILEDESC_XLOCK(fdp); 1769 } 1770 } 1771 FILEDESC_XUNLOCK(fdp); 1772} 1773 1774/* 1775 * If a specific file object occupies a specific file descriptor, close the 1776 * file descriptor entry and drop a reference on the file object. This is a 1777 * convenience function to handle a subsequent error in a function that calls 1778 * falloc() that handles the race that another thread might have closed the 1779 * file descriptor out from under the thread creating the file object. 1780 */ 1781void 1782fdclose(struct filedesc *fdp, struct file *fp, int idx, struct thread *td) 1783{ 1784 1785 FILEDESC_XLOCK(fdp); 1786 if (fdp->fd_ofiles[idx] == fp) { 1787 fdp->fd_ofiles[idx] = NULL; 1788 fdunused(fdp, idx); 1789 FILEDESC_XUNLOCK(fdp); 1790 fdrop(fp, td); 1791 } else 1792 FILEDESC_XUNLOCK(fdp); 1793} 1794 1795/* 1796 * Close any files on exec? 1797 */ 1798void 1799fdcloseexec(struct thread *td) 1800{ 1801 struct filedesc *fdp; 1802 int i; 1803 1804 /* Certain daemons might not have file descriptors. */ 1805 fdp = td->td_proc->p_fd; 1806 if (fdp == NULL) 1807 return; 1808 1809 FILEDESC_XLOCK(fdp); 1810 1811 /* 1812 * We cannot cache fd_ofiles or fd_ofileflags since operations 1813 * may block and rip them out from under us. 1814 */ 1815 for (i = 0; i <= fdp->fd_lastfile; i++) { 1816 if (fdp->fd_ofiles[i] != NULL && 1817 (fdp->fd_ofiles[i]->f_type == DTYPE_MQUEUE || 1818 (fdp->fd_ofileflags[i] & UF_EXCLOSE))) { 1819 struct file *fp; 1820 1821 knote_fdclose(td, i); 1822 /* 1823 * NULL-out descriptor prior to close to avoid 1824 * a race while close blocks. 1825 */ 1826 fp = fdp->fd_ofiles[i]; 1827 fdp->fd_ofiles[i] = NULL; 1828 fdp->fd_ofileflags[i] = 0; 1829 fdunused(fdp, i); 1830 if (fp->f_type == DTYPE_MQUEUE) 1831 mq_fdclose(td, i, fp); 1832 FILEDESC_XUNLOCK(fdp); 1833 (void) closef(fp, td); 1834 FILEDESC_XLOCK(fdp); 1835 } 1836 } 1837 FILEDESC_XUNLOCK(fdp); 1838} 1839 1840/* 1841 * It is unsafe for set[ug]id processes to be started with file 1842 * descriptors 0..2 closed, as these descriptors are given implicit 1843 * significance in the Standard C library. fdcheckstd() will create a 1844 * descriptor referencing /dev/null for each of stdin, stdout, and 1845 * stderr that is not already open. 1846 */ 1847int 1848fdcheckstd(struct thread *td) 1849{ 1850 struct filedesc *fdp; 1851 register_t retval, save; 1852 int i, error, devnull; 1853 1854 fdp = td->td_proc->p_fd; 1855 if (fdp == NULL) 1856 return (0); 1857 KASSERT(fdp->fd_refcnt == 1, ("the fdtable should not be shared")); 1858 devnull = -1; 1859 error = 0; 1860 for (i = 0; i < 3; i++) { 1861 if (fdp->fd_ofiles[i] != NULL) 1862 continue; 1863 if (devnull < 0) { 1864 save = td->td_retval[0]; 1865 error = kern_open(td, "/dev/null", UIO_SYSSPACE, 1866 O_RDWR, 0); 1867 devnull = td->td_retval[0]; 1868 KASSERT(devnull == i, ("oof, we didn't get our fd")); 1869 td->td_retval[0] = save; 1870 if (error) 1871 break; 1872 } else { 1873 error = do_dup(td, DUP_FIXED, devnull, i, &retval); 1874 if (error != 0) 1875 break; 1876 } 1877 } 1878 return (error); 1879} 1880 1881/* 1882 * Internal form of close. Decrement reference count on file structure. 1883 * Note: td may be NULL when closing a file that was being passed in a 1884 * message. 1885 * 1886 * XXXRW: Giant is not required for the caller, but often will be held; this 1887 * makes it moderately likely the Giant will be recursed in the VFS case. 1888 */ 1889int 1890closef(struct file *fp, struct thread *td) 1891{ 1892 struct vnode *vp; 1893 struct flock lf; 1894 struct filedesc_to_leader *fdtol; 1895 struct filedesc *fdp; 1896 1897 /* 1898 * POSIX record locking dictates that any close releases ALL 1899 * locks owned by this process. This is handled by setting 1900 * a flag in the unlock to free ONLY locks obeying POSIX 1901 * semantics, and not to free BSD-style file locks. 1902 * If the descriptor was in a message, POSIX-style locks 1903 * aren't passed with the descriptor, and the thread pointer 1904 * will be NULL. Callers should be careful only to pass a 1905 * NULL thread pointer when there really is no owning 1906 * context that might have locks, or the locks will be 1907 * leaked. 1908 */ 1909 if (fp->f_type == DTYPE_VNODE && td != NULL) { 1910 int vfslocked; 1911 1912 vp = fp->f_vnode; 1913 vfslocked = VFS_LOCK_GIANT(vp->v_mount); 1914 if ((td->td_proc->p_leader->p_flag & P_ADVLOCK) != 0) { 1915 lf.l_whence = SEEK_SET; 1916 lf.l_start = 0; 1917 lf.l_len = 0; 1918 lf.l_type = F_UNLCK; 1919 (void) VOP_ADVLOCK(vp, (caddr_t)td->td_proc->p_leader, 1920 F_UNLCK, &lf, F_POSIX); 1921 } 1922 fdtol = td->td_proc->p_fdtol; 1923 if (fdtol != NULL) { 1924 /* 1925 * Handle special case where file descriptor table is 1926 * shared between multiple process leaders. 1927 */ 1928 fdp = td->td_proc->p_fd; 1929 FILEDESC_XLOCK(fdp); 1930 for (fdtol = fdtol->fdl_next; 1931 fdtol != td->td_proc->p_fdtol; 1932 fdtol = fdtol->fdl_next) { 1933 if ((fdtol->fdl_leader->p_flag & 1934 P_ADVLOCK) == 0) 1935 continue; 1936 fdtol->fdl_holdcount++; 1937 FILEDESC_XUNLOCK(fdp); 1938 lf.l_whence = SEEK_SET; 1939 lf.l_start = 0; 1940 lf.l_len = 0; 1941 lf.l_type = F_UNLCK; 1942 vp = fp->f_vnode; 1943 (void) VOP_ADVLOCK(vp, 1944 (caddr_t)fdtol->fdl_leader, 1945 F_UNLCK, &lf, F_POSIX); 1946 FILEDESC_XLOCK(fdp); 1947 fdtol->fdl_holdcount--; 1948 if (fdtol->fdl_holdcount == 0 && 1949 fdtol->fdl_wakeup != 0) { 1950 fdtol->fdl_wakeup = 0; 1951 wakeup(fdtol); 1952 } 1953 } 1954 FILEDESC_XUNLOCK(fdp); 1955 } 1956 VFS_UNLOCK_GIANT(vfslocked); 1957 } 1958 return (fdrop(fp, td)); 1959} 1960 1961/* 1962 * Initialize the file pointer with the specified properties. 1963 * 1964 * The ops are set with release semantics to be certain that the flags, type, 1965 * and data are visible when ops is. This is to prevent ops methods from being 1966 * called with bad data. 1967 */ 1968void 1969finit(struct file *fp, u_int flag, short type, void *data, struct fileops *ops) 1970{ 1971 fp->f_data = data; 1972 fp->f_flag = flag; 1973 fp->f_type = type; 1974 atomic_store_rel_ptr((volatile uintptr_t *)&fp->f_ops, (uintptr_t)ops); 1975} 1976 1977 1978/* 1979 * Extract the file pointer associated with the specified descriptor for the 1980 * current user process. 1981 * 1982 * If the descriptor doesn't exist, EBADF is returned. 1983 * 1984 * If the descriptor exists but doesn't match 'flags' then return EBADF for 1985 * read attempts and EINVAL for write attempts. 1986 * 1987 * If 'hold' is set (non-zero) the file's refcount will be bumped on return. 1988 * It should be dropped with fdrop(). If it is not set, then the refcount 1989 * will not be bumped however the thread's filedesc struct will be returned 1990 * locked (for fgetsock). 1991 * 1992 * If an error occured the non-zero error is returned and *fpp is set to 1993 * NULL. Otherwise *fpp is set and zero is returned. 1994 */ 1995static __inline int 1996_fget(struct thread *td, int fd, struct file **fpp, int flags, int hold) 1997{ 1998 struct filedesc *fdp; 1999 struct file *fp; 2000 2001 *fpp = NULL; 2002 if (td == NULL || (fdp = td->td_proc->p_fd) == NULL) 2003 return (EBADF); 2004 FILEDESC_SLOCK(fdp); 2005 if ((fp = fget_locked(fdp, fd)) == NULL || fp->f_ops == &badfileops) { 2006 FILEDESC_SUNLOCK(fdp); 2007 return (EBADF); 2008 } 2009 2010 /* 2011 * FREAD and FWRITE failure return EBADF as per POSIX. 2012 * 2013 * Only one flag, or 0, may be specified. 2014 */ 2015 if (flags == FREAD && (fp->f_flag & FREAD) == 0) { 2016 FILEDESC_SUNLOCK(fdp); 2017 return (EBADF); 2018 } 2019 if (flags == FWRITE && (fp->f_flag & FWRITE) == 0) { 2020 FILEDESC_SUNLOCK(fdp); 2021 return (EBADF); 2022 } 2023 if (hold) { 2024 fhold(fp); 2025 FILEDESC_SUNLOCK(fdp); 2026 } 2027 *fpp = fp; 2028 return (0); 2029} 2030 2031int 2032fget(struct thread *td, int fd, struct file **fpp) 2033{ 2034 2035 return(_fget(td, fd, fpp, 0, 1)); 2036} 2037 2038int 2039fget_read(struct thread *td, int fd, struct file **fpp) 2040{ 2041 2042 return(_fget(td, fd, fpp, FREAD, 1)); 2043} 2044 2045int 2046fget_write(struct thread *td, int fd, struct file **fpp) 2047{ 2048 2049 return(_fget(td, fd, fpp, FWRITE, 1)); 2050} 2051 2052/* 2053 * Like fget() but loads the underlying vnode, or returns an error if the 2054 * descriptor does not represent a vnode. Note that pipes use vnodes but 2055 * never have VM objects. The returned vnode will be vref()'d. 2056 * 2057 * XXX: what about the unused flags ? 2058 */ 2059static __inline int 2060_fgetvp(struct thread *td, int fd, struct vnode **vpp, int flags) 2061{ 2062 struct file *fp; 2063 int error; 2064 2065 *vpp = NULL; 2066 if ((error = _fget(td, fd, &fp, flags, 0)) != 0) 2067 return (error); 2068 if (fp->f_vnode == NULL) { 2069 error = EINVAL; 2070 } else { 2071 *vpp = fp->f_vnode; 2072 vref(*vpp); 2073 } 2074 FILEDESC_SUNLOCK(td->td_proc->p_fd); 2075 return (error); 2076} 2077 2078int 2079fgetvp(struct thread *td, int fd, struct vnode **vpp) 2080{ 2081 2082 return (_fgetvp(td, fd, vpp, 0)); 2083} 2084 2085int 2086fgetvp_read(struct thread *td, int fd, struct vnode **vpp) 2087{ 2088 2089 return (_fgetvp(td, fd, vpp, FREAD)); 2090} 2091 2092#ifdef notyet 2093int 2094fgetvp_write(struct thread *td, int fd, struct vnode **vpp) 2095{ 2096 2097 return (_fgetvp(td, fd, vpp, FWRITE)); 2098} 2099#endif 2100 2101/* 2102 * Like fget() but loads the underlying socket, or returns an error if the 2103 * descriptor does not represent a socket. 2104 * 2105 * We bump the ref count on the returned socket. XXX Also obtain the SX lock 2106 * in the future. 2107 * 2108 * XXXRW: fgetsock() and fputsock() are deprecated, as consumers should rely 2109 * on their file descriptor reference to prevent the socket from being free'd 2110 * during use. 2111 */ 2112int 2113fgetsock(struct thread *td, int fd, struct socket **spp, u_int *fflagp) 2114{ 2115 struct file *fp; 2116 int error; 2117 2118 *spp = NULL; 2119 if (fflagp != NULL) 2120 *fflagp = 0; 2121 if ((error = _fget(td, fd, &fp, 0, 0)) != 0) 2122 return (error); 2123 if (fp->f_type != DTYPE_SOCKET) { 2124 error = ENOTSOCK; 2125 } else { 2126 *spp = fp->f_data; 2127 if (fflagp) 2128 *fflagp = fp->f_flag; 2129 SOCK_LOCK(*spp); 2130 soref(*spp); 2131 SOCK_UNLOCK(*spp); 2132 } 2133 FILEDESC_SUNLOCK(td->td_proc->p_fd); 2134 return (error); 2135} 2136 2137/* 2138 * Drop the reference count on the socket and XXX release the SX lock in the 2139 * future. The last reference closes the socket. 2140 * 2141 * XXXRW: fputsock() is deprecated, see comment for fgetsock(). 2142 */ 2143void 2144fputsock(struct socket *so) 2145{ 2146 2147 ACCEPT_LOCK(); 2148 SOCK_LOCK(so); 2149 sorele(so); 2150} 2151 2152/* 2153 * Handle the last reference to a file being closed. 2154 */ 2155int 2156_fdrop(struct file *fp, struct thread *td) 2157{ 2158 int error; 2159 2160 error = 0; 2161 if (fp->f_count != 0) 2162 panic("fdrop: count %d", fp->f_count); 2163 if (fp->f_ops != &badfileops) 2164 error = fo_close(fp, td); 2165 atomic_subtract_int(&openfiles, 1); 2166 crfree(fp->f_cred); 2167 uma_zfree(file_zone, fp); 2168 2169 return (error); 2170} 2171 2172/* 2173 * Apply an advisory lock on a file descriptor. 2174 * 2175 * Just attempt to get a record lock of the requested type on the entire file 2176 * (l_whence = SEEK_SET, l_start = 0, l_len = 0). 2177 */ 2178#ifndef _SYS_SYSPROTO_H_ 2179struct flock_args { 2180 int fd; 2181 int how; 2182}; 2183#endif 2184/* ARGSUSED */ 2185int 2186flock(struct thread *td, struct flock_args *uap) 2187{ 2188 struct file *fp; 2189 struct vnode *vp; 2190 struct flock lf; 2191 int vfslocked; 2192 int error; 2193 2194 if ((error = fget(td, uap->fd, &fp)) != 0) 2195 return (error); 2196 if (fp->f_type != DTYPE_VNODE) { 2197 fdrop(fp, td); 2198 return (EOPNOTSUPP); 2199 } 2200 2201 vp = fp->f_vnode; 2202 vfslocked = VFS_LOCK_GIANT(vp->v_mount); 2203 lf.l_whence = SEEK_SET; 2204 lf.l_start = 0; 2205 lf.l_len = 0; 2206 if (uap->how & LOCK_UN) { 2207 lf.l_type = F_UNLCK; 2208 atomic_clear_int(&fp->f_flag, FHASLOCK); 2209 error = VOP_ADVLOCK(vp, (caddr_t)fp, F_UNLCK, &lf, F_FLOCK); 2210 goto done2; 2211 } 2212 if (uap->how & LOCK_EX) 2213 lf.l_type = F_WRLCK; 2214 else if (uap->how & LOCK_SH) 2215 lf.l_type = F_RDLCK; 2216 else { 2217 error = EBADF; 2218 goto done2; 2219 } 2220 atomic_set_int(&fp->f_flag, FHASLOCK); 2221 error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, 2222 (uap->how & LOCK_NB) ? F_FLOCK : F_FLOCK | F_WAIT); 2223done2: 2224 fdrop(fp, td); 2225 VFS_UNLOCK_GIANT(vfslocked); 2226 return (error); 2227} 2228/* 2229 * Duplicate the specified descriptor to a free descriptor. 2230 */ 2231int 2232dupfdopen(struct thread *td, struct filedesc *fdp, int indx, int dfd, int mode, int error) 2233{ 2234 struct file *wfp; 2235 struct file *fp; 2236 2237 /* 2238 * If the to-be-dup'd fd number is greater than the allowed number 2239 * of file descriptors, or the fd to be dup'd has already been 2240 * closed, then reject. 2241 */ 2242 FILEDESC_XLOCK(fdp); 2243 if (dfd < 0 || dfd >= fdp->fd_nfiles || 2244 (wfp = fdp->fd_ofiles[dfd]) == NULL) { 2245 FILEDESC_XUNLOCK(fdp); 2246 return (EBADF); 2247 } 2248 2249 /* 2250 * There are two cases of interest here. 2251 * 2252 * For ENODEV simply dup (dfd) to file descriptor (indx) and return. 2253 * 2254 * For ENXIO steal away the file structure from (dfd) and store it in 2255 * (indx). (dfd) is effectively closed by this operation. 2256 * 2257 * Any other error code is just returned. 2258 */ 2259 switch (error) { 2260 case ENODEV: 2261 /* 2262 * Check that the mode the file is being opened for is a 2263 * subset of the mode of the existing descriptor. 2264 */ 2265 if (((mode & (FREAD|FWRITE)) | wfp->f_flag) != wfp->f_flag) { 2266 FILEDESC_XUNLOCK(fdp); 2267 return (EACCES); 2268 } 2269 fp = fdp->fd_ofiles[indx]; 2270 fdp->fd_ofiles[indx] = wfp; 2271 fdp->fd_ofileflags[indx] = fdp->fd_ofileflags[dfd]; 2272 if (fp == NULL) 2273 fdused(fdp, indx); 2274 fhold(wfp); 2275 FILEDESC_XUNLOCK(fdp); 2276 if (fp != NULL) 2277 /* 2278 * We now own the reference to fp that the ofiles[] 2279 * array used to own. Release it. 2280 */ 2281 fdrop(fp, td); 2282 return (0); 2283 2284 case ENXIO: 2285 /* 2286 * Steal away the file pointer from dfd and stuff it into indx. 2287 */ 2288 fp = fdp->fd_ofiles[indx]; 2289 fdp->fd_ofiles[indx] = fdp->fd_ofiles[dfd]; 2290 fdp->fd_ofiles[dfd] = NULL; 2291 fdp->fd_ofileflags[indx] = fdp->fd_ofileflags[dfd]; 2292 fdp->fd_ofileflags[dfd] = 0; 2293 fdunused(fdp, dfd); 2294 if (fp == NULL) 2295 fdused(fdp, indx); 2296 FILEDESC_XUNLOCK(fdp); 2297 2298 /* 2299 * We now own the reference to fp that the ofiles[] array 2300 * used to own. Release it. 2301 */ 2302 if (fp != NULL) 2303 fdrop(fp, td); 2304 return (0); 2305 2306 default: 2307 FILEDESC_XUNLOCK(fdp); 2308 return (error); 2309 } 2310 /* NOTREACHED */ 2311} 2312 2313/* 2314 * Scan all active processes to see if any of them have a current or root 2315 * directory of `olddp'. If so, replace them with the new mount point. 2316 */ 2317void 2318mountcheckdirs(struct vnode *olddp, struct vnode *newdp) 2319{ 2320 struct filedesc *fdp; 2321 struct proc *p; 2322 int nrele; 2323 2324 if (vrefcnt(olddp) == 1) 2325 return; 2326 sx_slock(&allproc_lock); 2327 FOREACH_PROC_IN_SYSTEM(p) { 2328 fdp = fdhold(p); 2329 if (fdp == NULL) 2330 continue; 2331 nrele = 0; 2332 FILEDESC_XLOCK(fdp); 2333 if (fdp->fd_cdir == olddp) { 2334 vref(newdp); 2335 fdp->fd_cdir = newdp; 2336 nrele++; 2337 } 2338 if (fdp->fd_rdir == olddp) { 2339 vref(newdp); 2340 fdp->fd_rdir = newdp; 2341 nrele++; 2342 } 2343 FILEDESC_XUNLOCK(fdp); 2344 fddrop(fdp); 2345 while (nrele--) 2346 vrele(olddp); 2347 } 2348 sx_sunlock(&allproc_lock); 2349 if (rootvnode == olddp) { 2350 vrele(rootvnode); 2351 vref(newdp); 2352 rootvnode = newdp; 2353 } 2354} 2355 2356struct filedesc_to_leader * 2357filedesc_to_leader_alloc(struct filedesc_to_leader *old, struct filedesc *fdp, struct proc *leader) 2358{ 2359 struct filedesc_to_leader *fdtol; 2360 2361 MALLOC(fdtol, struct filedesc_to_leader *, 2362 sizeof(struct filedesc_to_leader), 2363 M_FILEDESC_TO_LEADER, 2364 M_WAITOK); 2365 fdtol->fdl_refcount = 1; 2366 fdtol->fdl_holdcount = 0; 2367 fdtol->fdl_wakeup = 0; 2368 fdtol->fdl_leader = leader; 2369 if (old != NULL) { 2370 FILEDESC_XLOCK(fdp); 2371 fdtol->fdl_next = old->fdl_next; 2372 fdtol->fdl_prev = old; 2373 old->fdl_next = fdtol; 2374 fdtol->fdl_next->fdl_prev = fdtol; 2375 FILEDESC_XUNLOCK(fdp); 2376 } else { 2377 fdtol->fdl_next = fdtol; 2378 fdtol->fdl_prev = fdtol; 2379 } 2380 return (fdtol); 2381} 2382 2383/* 2384 * Get file structures globally. 2385 */ 2386static int 2387sysctl_kern_file(SYSCTL_HANDLER_ARGS) 2388{ 2389 struct xfile xf; 2390 struct filedesc *fdp; 2391 struct file *fp; 2392 struct proc *p; 2393 int error, n; 2394 2395 error = sysctl_wire_old_buffer(req, 0); 2396 if (error != 0) 2397 return (error); 2398 if (req->oldptr == NULL) { 2399 n = 0; 2400 sx_slock(&allproc_lock); 2401 FOREACH_PROC_IN_SYSTEM(p) { 2402 if (p->p_state == PRS_NEW) 2403 continue; 2404 fdp = fdhold(p); 2405 if (fdp == NULL) 2406 continue; 2407 /* overestimates sparse tables. */ 2408 if (fdp->fd_lastfile > 0) 2409 n += fdp->fd_lastfile; 2410 fddrop(fdp); 2411 } 2412 sx_sunlock(&allproc_lock); 2413 return (SYSCTL_OUT(req, 0, n * sizeof(xf))); 2414 } 2415 error = 0; 2416 bzero(&xf, sizeof(xf)); 2417 xf.xf_size = sizeof(xf); 2418 sx_slock(&allproc_lock); 2419 FOREACH_PROC_IN_SYSTEM(p) { 2420 if (p->p_state == PRS_NEW) 2421 continue; 2422 PROC_LOCK(p); 2423 if (p_cansee(req->td, p) != 0) { 2424 PROC_UNLOCK(p); 2425 continue; 2426 } 2427 xf.xf_pid = p->p_pid; 2428 xf.xf_uid = p->p_ucred->cr_uid; 2429 PROC_UNLOCK(p); 2430 fdp = fdhold(p); 2431 if (fdp == NULL) 2432 continue; 2433 FILEDESC_SLOCK(fdp); 2434 for (n = 0; fdp->fd_refcnt > 0 && n < fdp->fd_nfiles; ++n) { 2435 if ((fp = fdp->fd_ofiles[n]) == NULL) 2436 continue; 2437 xf.xf_fd = n; 2438 xf.xf_file = fp; 2439 xf.xf_data = fp->f_data; 2440 xf.xf_vnode = fp->f_vnode; 2441 xf.xf_type = fp->f_type; 2442 xf.xf_count = fp->f_count; 2443 xf.xf_msgcount = 0; 2444 xf.xf_offset = fp->f_offset; 2445 xf.xf_flag = fp->f_flag; 2446 error = SYSCTL_OUT(req, &xf, sizeof(xf)); 2447 if (error) 2448 break; 2449 } 2450 FILEDESC_SUNLOCK(fdp); 2451 fddrop(fdp); 2452 if (error) 2453 break; 2454 } 2455 sx_sunlock(&allproc_lock); 2456 return (error); 2457} 2458 2459SYSCTL_PROC(_kern, KERN_FILE, file, CTLTYPE_OPAQUE|CTLFLAG_RD, 2460 0, 0, sysctl_kern_file, "S,xfile", "Entire file table"); 2461 2462static int 2463export_vnode_for_sysctl(struct vnode *vp, int type, 2464 struct kinfo_file *kif, struct filedesc *fdp, struct sysctl_req *req) 2465{ 2466 int error; 2467 char *fullpath, *freepath; 2468 int vfslocked; 2469 2470 bzero(kif, sizeof(*kif)); 2471 kif->kf_structsize = sizeof(*kif); 2472 2473 vref(vp); 2474 kif->kf_fd = type; 2475 kif->kf_type = KF_TYPE_VNODE; 2476 /* This function only handles directories. */ 2477 KASSERT(vp->v_type == VDIR, ("export_vnode_for_sysctl: vnode not directory")); 2478 kif->kf_vnode_type = KF_VTYPE_VDIR; 2479 2480 /* 2481 * This is not a true file descriptor, so we set a bogus refcount 2482 * and offset to indicate these fields should be ignored. 2483 */ 2484 kif->kf_ref_count = -1; 2485 kif->kf_offset = -1; 2486 2487 freepath = NULL; 2488 fullpath = "-"; 2489 FILEDESC_SUNLOCK(fdp); 2490 vfslocked = VFS_LOCK_GIANT(vp->v_mount); 2491 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 2492 vn_fullpath(curthread, vp, &fullpath, &freepath); 2493 vput(vp); 2494 VFS_UNLOCK_GIANT(vfslocked); 2495 strlcpy(kif->kf_path, fullpath, sizeof(kif->kf_path)); 2496 if (freepath != NULL) 2497 free(freepath, M_TEMP); 2498 error = SYSCTL_OUT(req, kif, sizeof(*kif)); 2499 FILEDESC_SLOCK(fdp); 2500 return (error); 2501} 2502 2503/* 2504 * Get per-process file descriptors for use by procstat(1), et al. 2505 */ 2506static int 2507sysctl_kern_proc_filedesc(SYSCTL_HANDLER_ARGS) 2508{ 2509 char *fullpath, *freepath; 2510 struct kinfo_file *kif; 2511 struct filedesc *fdp; 2512 int error, i, *name; 2513 struct socket *so; 2514 struct vnode *vp; 2515 struct file *fp; 2516 struct proc *p; 2517 int vfslocked; 2518 2519 name = (int *)arg1; 2520 if ((p = pfind((pid_t)name[0])) == NULL) 2521 return (ESRCH); 2522 if ((error = p_candebug(curthread, p))) { 2523 PROC_UNLOCK(p); 2524 return (error); 2525 } 2526 fdp = fdhold(p); 2527 PROC_UNLOCK(p); 2528 kif = malloc(sizeof(*kif), M_TEMP, M_WAITOK); 2529 FILEDESC_SLOCK(fdp); 2530 if (fdp->fd_cdir != NULL) 2531 export_vnode_for_sysctl(fdp->fd_cdir, KF_FD_TYPE_CWD, kif, 2532 fdp, req); 2533 if (fdp->fd_rdir != NULL) 2534 export_vnode_for_sysctl(fdp->fd_rdir, KF_FD_TYPE_ROOT, kif, 2535 fdp, req); 2536 if (fdp->fd_jdir != NULL) 2537 export_vnode_for_sysctl(fdp->fd_jdir, KF_FD_TYPE_JAIL, kif, 2538 fdp, req); 2539 for (i = 0; i < fdp->fd_nfiles; i++) { 2540 if ((fp = fdp->fd_ofiles[i]) == NULL) 2541 continue; 2542 bzero(kif, sizeof(*kif)); 2543 kif->kf_structsize = sizeof(*kif); 2544 vp = NULL; 2545 so = NULL; 2546 kif->kf_fd = i; 2547 switch (fp->f_type) { 2548 case DTYPE_VNODE: 2549 kif->kf_type = KF_TYPE_VNODE; 2550 vp = fp->f_vnode; 2551 break; 2552 2553 case DTYPE_SOCKET: 2554 kif->kf_type = KF_TYPE_SOCKET; 2555 so = fp->f_data; 2556 break; 2557 2558 case DTYPE_PIPE: 2559 kif->kf_type = KF_TYPE_PIPE; 2560 break; 2561 2562 case DTYPE_FIFO: 2563 kif->kf_type = KF_TYPE_FIFO; 2564 vp = fp->f_vnode; 2565 vref(vp); 2566 break; 2567 2568 case DTYPE_KQUEUE: 2569 kif->kf_type = KF_TYPE_KQUEUE; 2570 break; 2571 2572 case DTYPE_CRYPTO: 2573 kif->kf_type = KF_TYPE_CRYPTO; 2574 break; 2575 2576 case DTYPE_MQUEUE: 2577 kif->kf_type = KF_TYPE_MQUEUE; 2578 break; 2579 2580 case DTYPE_SHM: 2581 kif->kf_type = KF_TYPE_SHM; 2582 break; 2583 2584 default: 2585 kif->kf_type = KF_TYPE_UNKNOWN; 2586 break; 2587 } 2588 kif->kf_ref_count = fp->f_count; 2589 if (fp->f_flag & FREAD) 2590 kif->kf_flags |= KF_FLAG_READ; 2591 if (fp->f_flag & FWRITE) 2592 kif->kf_flags |= KF_FLAG_WRITE; 2593 if (fp->f_flag & FAPPEND) 2594 kif->kf_flags |= KF_FLAG_APPEND; 2595 if (fp->f_flag & FASYNC) 2596 kif->kf_flags |= KF_FLAG_ASYNC; 2597 if (fp->f_flag & FFSYNC) 2598 kif->kf_flags |= KF_FLAG_FSYNC; 2599 if (fp->f_flag & FNONBLOCK) 2600 kif->kf_flags |= KF_FLAG_NONBLOCK; 2601 if (fp->f_flag & O_DIRECT) 2602 kif->kf_flags |= KF_FLAG_DIRECT; 2603 if (fp->f_flag & FHASLOCK) 2604 kif->kf_flags |= KF_FLAG_HASLOCK; 2605 kif->kf_offset = fp->f_offset; 2606 if (vp != NULL) { 2607 vref(vp); 2608 switch (vp->v_type) { 2609 case VNON: 2610 kif->kf_vnode_type = KF_VTYPE_VNON; 2611 break; 2612 case VREG: 2613 kif->kf_vnode_type = KF_VTYPE_VREG; 2614 break; 2615 case VDIR: 2616 kif->kf_vnode_type = KF_VTYPE_VDIR; 2617 break; 2618 case VBLK: 2619 kif->kf_vnode_type = KF_VTYPE_VBLK; 2620 break; 2621 case VCHR: 2622 kif->kf_vnode_type = KF_VTYPE_VCHR; 2623 break; 2624 case VLNK: 2625 kif->kf_vnode_type = KF_VTYPE_VLNK; 2626 break; 2627 case VSOCK: 2628 kif->kf_vnode_type = KF_VTYPE_VSOCK; 2629 break; 2630 case VFIFO: 2631 kif->kf_vnode_type = KF_VTYPE_VFIFO; 2632 break; 2633 case VBAD: 2634 kif->kf_vnode_type = KF_VTYPE_VBAD; 2635 break; 2636 default: 2637 kif->kf_vnode_type = KF_VTYPE_UNKNOWN; 2638 break; 2639 } 2640 /* 2641 * It is OK to drop the filedesc lock here as we will 2642 * re-validate and re-evaluate its properties when 2643 * the loop continues. 2644 */ 2645 freepath = NULL; 2646 fullpath = "-"; 2647 FILEDESC_SUNLOCK(fdp); 2648 vfslocked = VFS_LOCK_GIANT(vp->v_mount); 2649 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 2650 vn_fullpath(curthread, vp, &fullpath, &freepath); 2651 vput(vp); 2652 VFS_UNLOCK_GIANT(vfslocked); 2653 strlcpy(kif->kf_path, fullpath, 2654 sizeof(kif->kf_path)); 2655 if (freepath != NULL) 2656 free(freepath, M_TEMP); 2657 FILEDESC_SLOCK(fdp); 2658 } 2659 if (so != NULL) { 2660 struct sockaddr *sa; 2661 2662 if (so->so_proto->pr_usrreqs->pru_sockaddr(so, &sa) 2663 == 0 && sa->sa_len <= sizeof(kif->kf_sa_local)) { 2664 bcopy(sa, &kif->kf_sa_local, sa->sa_len); 2665 free(sa, M_SONAME); 2666 } 2667 if (so->so_proto->pr_usrreqs->pru_peeraddr(so, &sa) 2668 == 00 && sa->sa_len <= sizeof(kif->kf_sa_peer)) { 2669 bcopy(sa, &kif->kf_sa_peer, sa->sa_len); 2670 free(sa, M_SONAME); 2671 } 2672 kif->kf_sock_domain = 2673 so->so_proto->pr_domain->dom_family; 2674 kif->kf_sock_type = so->so_type; 2675 kif->kf_sock_protocol = so->so_proto->pr_protocol; 2676 } 2677 error = SYSCTL_OUT(req, kif, sizeof(*kif)); 2678 if (error) 2679 break; 2680 } 2681 FILEDESC_SUNLOCK(fdp); 2682 fddrop(fdp); 2683 free(kif, M_TEMP); 2684 return (0); 2685} 2686 2687static SYSCTL_NODE(_kern_proc, KERN_PROC_FILEDESC, filedesc, CTLFLAG_RD, 2688 sysctl_kern_proc_filedesc, "Process filedesc entries"); 2689 2690#ifdef DDB 2691/* 2692 * For the purposes of debugging, generate a human-readable string for the 2693 * file type. 2694 */ 2695static const char * 2696file_type_to_name(short type) 2697{ 2698 2699 switch (type) { 2700 case 0: 2701 return ("zero"); 2702 case DTYPE_VNODE: 2703 return ("vnod"); 2704 case DTYPE_SOCKET: 2705 return ("sock"); 2706 case DTYPE_PIPE: 2707 return ("pipe"); 2708 case DTYPE_FIFO: 2709 return ("fifo"); 2710 case DTYPE_KQUEUE: 2711 return ("kque"); 2712 case DTYPE_CRYPTO: 2713 return ("crpt"); 2714 case DTYPE_MQUEUE: 2715 return ("mque"); 2716 case DTYPE_SHM: 2717 return ("shm"); 2718 default: 2719 return ("unkn"); 2720 } 2721} 2722 2723/* 2724 * For the purposes of debugging, identify a process (if any, perhaps one of 2725 * many) that references the passed file in its file descriptor array. Return 2726 * NULL if none. 2727 */ 2728static struct proc * 2729file_to_first_proc(struct file *fp) 2730{ 2731 struct filedesc *fdp; 2732 struct proc *p; 2733 int n; 2734 2735 FOREACH_PROC_IN_SYSTEM(p) { 2736 if (p->p_state == PRS_NEW) 2737 continue; 2738 fdp = p->p_fd; 2739 if (fdp == NULL) 2740 continue; 2741 for (n = 0; n < fdp->fd_nfiles; n++) { 2742 if (fp == fdp->fd_ofiles[n]) 2743 return (p); 2744 } 2745 } 2746 return (NULL); 2747} 2748 2749static void 2750db_print_file(struct file *fp, int header) 2751{ 2752 struct proc *p; 2753 2754 if (header) 2755 db_printf("%8s %4s %8s %8s %4s %5s %6s %8s %5s %12s\n", 2756 "File", "Type", "Data", "Flag", "GCFl", "Count", 2757 "MCount", "Vnode", "FPID", "FCmd"); 2758 p = file_to_first_proc(fp); 2759 db_printf("%8p %4s %8p %08x %04x %5d %6d %8p %5d %12s\n", fp, 2760 file_type_to_name(fp->f_type), fp->f_data, fp->f_flag, 2761 0, fp->f_count, 0, fp->f_vnode, 2762 p != NULL ? p->p_pid : -1, p != NULL ? p->p_comm : "-"); 2763} 2764 2765DB_SHOW_COMMAND(file, db_show_file) 2766{ 2767 struct file *fp; 2768 2769 if (!have_addr) { 2770 db_printf("usage: show file <addr>\n"); 2771 return; 2772 } 2773 fp = (struct file *)addr; 2774 db_print_file(fp, 1); 2775} 2776 2777DB_SHOW_COMMAND(files, db_show_files) 2778{ 2779 struct filedesc *fdp; 2780 struct file *fp; 2781 struct proc *p; 2782 int header; 2783 int n; 2784 2785 header = 1; 2786 FOREACH_PROC_IN_SYSTEM(p) { 2787 if (p->p_state == PRS_NEW) 2788 continue; 2789 if ((fdp = p->p_fd) == NULL) 2790 continue; 2791 for (n = 0; n < fdp->fd_nfiles; ++n) { 2792 if ((fp = fdp->fd_ofiles[n]) == NULL) 2793 continue; 2794 db_print_file(fp, header); 2795 header = 0; 2796 } 2797 } 2798} 2799#endif 2800 2801SYSCTL_INT(_kern, KERN_MAXFILESPERPROC, maxfilesperproc, CTLFLAG_RW, 2802 &maxfilesperproc, 0, "Maximum files allowed open per process"); 2803 2804SYSCTL_INT(_kern, KERN_MAXFILES, maxfiles, CTLFLAG_RW, 2805 &maxfiles, 0, "Maximum number of files"); 2806 2807SYSCTL_INT(_kern, OID_AUTO, openfiles, CTLFLAG_RD, 2808 __DEVOLATILE(int *, &openfiles), 0, "System-wide number of open files"); 2809 2810/* ARGSUSED*/ 2811static void 2812filelistinit(void *dummy) 2813{ 2814 2815 file_zone = uma_zcreate("Files", sizeof(struct file), NULL, NULL, 2816 NULL, NULL, UMA_ALIGN_PTR, 0); 2817 mtx_init(&sigio_lock, "sigio lock", NULL, MTX_DEF); 2818 mtx_init(&fdesc_mtx, "fdesc", NULL, MTX_DEF); 2819} 2820SYSINIT(select, SI_SUB_LOCK, SI_ORDER_FIRST, filelistinit, NULL) 2821 2822/*-------------------------------------------------------------------*/ 2823 2824static int 2825badfo_readwrite(struct file *fp, struct uio *uio, struct ucred *active_cred, int flags, struct thread *td) 2826{ 2827 2828 return (EBADF); 2829} 2830 2831static int 2832badfo_truncate(struct file *fp, off_t length, struct ucred *active_cred, struct thread *td) 2833{ 2834 2835 return (EINVAL); 2836} 2837 2838static int 2839badfo_ioctl(struct file *fp, u_long com, void *data, struct ucred *active_cred, struct thread *td) 2840{ 2841 2842 return (EBADF); 2843} 2844 2845static int 2846badfo_poll(struct file *fp, int events, struct ucred *active_cred, struct thread *td) 2847{ 2848 2849 return (0); 2850} 2851 2852static int 2853badfo_kqfilter(struct file *fp, struct knote *kn) 2854{ 2855 2856 return (EBADF); 2857} 2858 2859static int 2860badfo_stat(struct file *fp, struct stat *sb, struct ucred *active_cred, struct thread *td) 2861{ 2862 2863 return (EBADF); 2864} 2865 2866static int 2867badfo_close(struct file *fp, struct thread *td) 2868{ 2869 2870 return (EBADF); 2871} 2872 2873struct fileops badfileops = { 2874 .fo_read = badfo_readwrite, 2875 .fo_write = badfo_readwrite, 2876 .fo_truncate = badfo_truncate, 2877 .fo_ioctl = badfo_ioctl, 2878 .fo_poll = badfo_poll, 2879 .fo_kqfilter = badfo_kqfilter, 2880 .fo_stat = badfo_stat, 2881 .fo_close = badfo_close, 2882}; 2883 2884 2885/*-------------------------------------------------------------------*/ 2886 2887/* 2888 * File Descriptor pseudo-device driver (/dev/fd/). 2889 * 2890 * Opening minor device N dup()s the file (if any) connected to file 2891 * descriptor N belonging to the calling process. Note that this driver 2892 * consists of only the ``open()'' routine, because all subsequent 2893 * references to this file will be direct to the other driver. 2894 * 2895 * XXX: we could give this one a cloning event handler if necessary. 2896 */ 2897 2898/* ARGSUSED */ 2899static int 2900fdopen(struct cdev *dev, int mode, int type, struct thread *td) 2901{ 2902 2903 /* 2904 * XXX Kludge: set curthread->td_dupfd to contain the value of the 2905 * the file descriptor being sought for duplication. The error 2906 * return ensures that the vnode for this device will be released 2907 * by vn_open. Open will detect this special error and take the 2908 * actions in dupfdopen below. Other callers of vn_open or VOP_OPEN 2909 * will simply report the error. 2910 */ 2911 td->td_dupfd = dev2unit(dev); 2912 return (ENODEV); 2913} 2914 2915static struct cdevsw fildesc_cdevsw = { 2916 .d_version = D_VERSION, 2917 .d_flags = D_NEEDGIANT, 2918 .d_open = fdopen, 2919 .d_name = "FD", 2920}; 2921 2922static void 2923fildesc_drvinit(void *unused) 2924{ 2925 struct cdev *dev; 2926 2927 dev = make_dev(&fildesc_cdevsw, 0, UID_ROOT, GID_WHEEL, 0666, "fd/0"); 2928 make_dev_alias(dev, "stdin"); 2929 dev = make_dev(&fildesc_cdevsw, 1, UID_ROOT, GID_WHEEL, 0666, "fd/1"); 2930 make_dev_alias(dev, "stdout"); 2931 dev = make_dev(&fildesc_cdevsw, 2, UID_ROOT, GID_WHEEL, 0666, "fd/2"); 2932 make_dev_alias(dev, "stderr"); 2933} 2934 2935SYSINIT(fildescdev, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, fildesc_drvinit, NULL)
|