* Check a file descriptor for read and/or write data, possibly waiting. * If neither forRead nor forWrite are set, immediately return a timeout * condition (without waiting). Return >0 if condition is met, 0 * if a timeout occurred, -1 if an error or interrupt occurred. * * Timeout is infinite if end_time is -1. Timeout is immediate (no blocking) * if end_time is 0 (or indeed, any time bef
| 1203 | * if end_time is 0 (or indeed, any time before now). |
| 1204 | */ |
| 1205 | static int |
| 1206 | pqSocketPoll(int sock, int forRead, int forWrite, time_t end_time) |
| 1207 | { |
| 1208 | /* We use poll(2) if available, otherwise select(2) */ |
| 1209 | #ifdef HAVE_POLL |
| 1210 | struct pollfd input_fd; |
| 1211 | int timeout_ms; |
| 1212 | |
| 1213 | if (!forRead && !forWrite) |
| 1214 | return 0; |
| 1215 | |
| 1216 | input_fd.fd = sock; |
| 1217 | input_fd.events = POLLERR; |
| 1218 | input_fd.revents = 0; |
| 1219 | |
| 1220 | if (forRead) |
| 1221 | input_fd.events |= POLLIN; |
| 1222 | if (forWrite) |
| 1223 | input_fd.events |= POLLOUT; |
| 1224 | |
| 1225 | /* Compute appropriate timeout interval */ |
| 1226 | if (end_time == ((time_t) -1)) |
| 1227 | timeout_ms = -1; |
| 1228 | else |
| 1229 | { |
| 1230 | time_t now = time(NULL); |
| 1231 | |
| 1232 | if (end_time > now) |
| 1233 | timeout_ms = (end_time - now) * 1000; |
| 1234 | else |
| 1235 | timeout_ms = 0; |
| 1236 | } |
| 1237 | |
| 1238 | return poll(&input_fd, 1, timeout_ms); |
| 1239 | #else /* !HAVE_POLL */ |
| 1240 | |
| 1241 | fd_set input_mask; |
| 1242 | fd_set output_mask; |
| 1243 | fd_set except_mask; |
| 1244 | struct timeval timeout; |
| 1245 | struct timeval *ptr_timeout; |
| 1246 | |
| 1247 | if (!forRead && !forWrite) |
| 1248 | return 0; |
| 1249 | |
| 1250 | FD_ZERO(&input_mask); |
| 1251 | FD_ZERO(&output_mask); |
| 1252 | FD_ZERO(&except_mask); |
| 1253 | if (forRead) |
| 1254 | FD_SET(sock, &input_mask); |
| 1255 | |
| 1256 | if (forWrite) |
| 1257 | FD_SET(sock, &output_mask); |
| 1258 | FD_SET(sock, &except_mask); |
| 1259 | |
| 1260 | /* Compute appropriate timeout interval */ |
| 1261 | if (end_time == ((time_t) -1)) |
| 1262 | ptr_timeout = NULL; |