| 1065 | } |
| 1066 | |
| 1067 | int |
| 1068 | kern_select(struct thread *td, int nd, fd_set *fd_in, fd_set *fd_ou, |
| 1069 | fd_set *fd_ex, struct timeval *tvp, int abi_nfdbits) |
| 1070 | { |
| 1071 | struct filedesc *fdp; |
| 1072 | /* |
| 1073 | * The magic 2048 here is chosen to be just enough for FD_SETSIZE |
| 1074 | * infds with the new FD_SETSIZE of 1024, and more than enough for |
| 1075 | * FD_SETSIZE infds, outfds and exceptfds with the old FD_SETSIZE |
| 1076 | * of 256. |
| 1077 | */ |
| 1078 | fd_mask s_selbits[howmany(2048, NFDBITS)]; |
| 1079 | fd_mask *ibits[3], *obits[3], *selbits, *sbp; |
| 1080 | struct timeval rtv; |
| 1081 | sbintime_t asbt, precision, rsbt; |
| 1082 | u_int nbufbytes, ncpbytes, ncpubytes, nfdbits; |
| 1083 | int error, lf, ndu; |
| 1084 | |
| 1085 | if (nd < 0) |
| 1086 | return (EINVAL); |
| 1087 | fdp = td->td_proc->p_fd; |
| 1088 | ndu = nd; |
| 1089 | lf = fdp->fd_nfiles; |
| 1090 | if (nd > lf) |
| 1091 | nd = lf; |
| 1092 | |
| 1093 | error = select_check_badfd(fd_in, nd, ndu, abi_nfdbits); |
| 1094 | if (error != 0) |
| 1095 | return (error); |
| 1096 | error = select_check_badfd(fd_ou, nd, ndu, abi_nfdbits); |
| 1097 | if (error != 0) |
| 1098 | return (error); |
| 1099 | error = select_check_badfd(fd_ex, nd, ndu, abi_nfdbits); |
| 1100 | if (error != 0) |
| 1101 | return (error); |
| 1102 | |
| 1103 | /* |
| 1104 | * Allocate just enough bits for the non-null fd_sets. Use the |
| 1105 | * preallocated auto buffer if possible. |
| 1106 | */ |
| 1107 | nfdbits = roundup(nd, NFDBITS); |
| 1108 | ncpbytes = nfdbits / NBBY; |
| 1109 | ncpubytes = roundup(nd, abi_nfdbits) / NBBY; |
| 1110 | nbufbytes = 0; |
| 1111 | if (fd_in != NULL) |
| 1112 | nbufbytes += 2 * ncpbytes; |
| 1113 | if (fd_ou != NULL) |
| 1114 | nbufbytes += 2 * ncpbytes; |
| 1115 | if (fd_ex != NULL) |
| 1116 | nbufbytes += 2 * ncpbytes; |
| 1117 | if (nbufbytes <= sizeof s_selbits) |
| 1118 | selbits = &s_selbits[0]; |
| 1119 | else |
| 1120 | selbits = malloc(nbufbytes, M_SELECT, M_WAITOK); |
| 1121 | |
| 1122 | /* |
| 1123 | * Assign pointers into the bit buffers and fetch the input bits. |
| 1124 | * Put the output buffers together so that they can be bzeroed |