* io mode for the socket
* @param timeout (r) number of seconds to try reading
*
- * @returns number of bytes actually read or -1 on fatal error
+ * @returns number of bytes actually read or -1 on timeout or error
*/
ssize_t readt(int socket, void *data, const size_t length, int setnonblocking, int timeout)
{
fd_set rfds;
int ret;
+ FD_ZERO(&rfds);
+
if (setnonblocking) {
if (setnonblock(socket, 1) != 0)
return -1;
case EINTR:
continue;
case EAGAIN:
- FD_ZERO(&rfds);
FD_SET(socket, &rfds);
tv.tv_usec = 0;
tv.tv_sec = timeout;
while ((ret = select(socket + 1, &rfds, NULL, NULL, &tv)) < 1) {
switch (ret) {
case 0:
- LOG(log_warning, logtype_afpd, "select timeout %d s", timeout);
+ LOG(log_debug, logtype_afpd, "select timeout %d s", timeout);
+ errno = EAGAIN;
goto exit;
default: /* -1 */
tv.tv_usec = end.tv_usec - now.tv_usec;
tv.tv_sec = end.tv_sec - now.tv_sec;
}
- FD_ZERO(&rfds);
FD_SET(socket, &rfds);
continue;
}