Do a safe read, handling any needed looping and error handling. * Returns the count of the bytes read, which will only be different * from "len" if we encountered an EOF. This routine is not used on * the socket except very early in the transfer. */
| 237 | * from "len" if we encountered an EOF. This routine is not used on |
| 238 | * the socket except very early in the transfer. */ |
| 239 | static size_t safe_read(int fd, char *buf, size_t len) |
| 240 | { |
| 241 | size_t got = 0; |
| 242 | |
| 243 | assert(fd != iobuf.in_fd); |
| 244 | |
| 245 | while (1) { |
| 246 | struct timeval tv; |
| 247 | fd_set r_fds, e_fds; |
| 248 | int cnt; |
| 249 | |
| 250 | FD_ZERO(&r_fds); |
| 251 | FD_SET(fd, &r_fds); |
| 252 | FD_ZERO(&e_fds); |
| 253 | FD_SET(fd, &e_fds); |
| 254 | tv.tv_sec = select_timeout; |
| 255 | tv.tv_usec = 0; |
| 256 | |
| 257 | cnt = select(fd+1, &r_fds, NULL, &e_fds, &tv); |
| 258 | if (cnt <= 0) { |
| 259 | if (cnt < 0 && errno == EBADF) { |
| 260 | rsyserr(FERROR, errno, "safe_read select failed"); |
| 261 | exit_cleanup(RERR_FILEIO); |
| 262 | } |
| 263 | check_timeout(1, MSK_ALLOW_FLUSH); |
| 264 | continue; |
| 265 | } |
| 266 | |
| 267 | /*if (FD_ISSET(fd, &e_fds)) |
| 268 | rprintf(FINFO, "select exception on fd %d\n", fd); */ |
| 269 | |
| 270 | if (FD_ISSET(fd, &r_fds)) { |
| 271 | ssize_t n = read(fd, buf + got, len - got); |
| 272 | if (DEBUG_GTE(IO, 2)) { |
| 273 | rprintf(FINFO, "[%s] safe_read(%d)=%" SIZE_T_FMT_MOD "d\n", |
| 274 | who_am_i(), fd, (SIZE_T_FMT_CAST)n); |
| 275 | } |
| 276 | if (n == 0) |
| 277 | break; |
| 278 | if (n < 0) { |
| 279 | if (errno == EINTR) |
| 280 | continue; |
| 281 | rsyserr(FERROR, errno, "safe_read failed to read %" SIZE_T_FMT_MOD "d bytes", |
| 282 | (SIZE_T_FMT_CAST)len); |
| 283 | exit_cleanup(RERR_STREAMIO); |
| 284 | } |
| 285 | if ((got += (size_t)n) == len) |
| 286 | break; |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | return got; |
| 291 | } |
| 292 | |
| 293 | static const char *what_fd_is(int fd) |
| 294 | { |
no test coverage detected