#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/socket.h>
#ifdef HAVE_SYS_SELECT_H
# include <sys/select.h>
#endif
#include <sys/time.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include "distcc.h"
#include "trace.h"
#include "util.h"
#include "exitcode.h"
const int dcc_io_timeout = 300;
int dcc_select_for_read(int fd,
int timeout)
{
fd_set fds;
int rs;
struct timeval tv;
tv.tv_sec = timeout;
tv.tv_usec = 0;
while (1) {
FD_ZERO(&fds);
FD_SET(fd, &fds);
rs_trace("select for read on fd%d for %ds", fd, (int) tv.tv_sec);
rs = select(fd+1, &fds, NULL, NULL, &tv);
if (rs == -1 && errno == EINTR) {
rs_trace("select was interrupted");
continue;
} else if (rs == -1) {
rs_log_error("select() failed: %s", strerror(errno));
return EXIT_IO_ERROR;
} else if (rs == 0) {
rs_log_error("IO timeout");
return EXIT_IO_ERROR;
} else if (!FD_ISSET(fd, &fds)) {
rs_log_error("how did fd not get set?");
continue;
} else {
break;
}
}
return 0;
}
int dcc_select_for_write(int fd, int timeout)
{
fd_set write_fds;
fd_set except_fds;
int rs;
struct timeval tv;
tv.tv_sec = timeout;
tv.tv_usec = 0;
while (1) {
FD_ZERO(&write_fds);
FD_ZERO(&except_fds);
FD_SET(fd, &write_fds);
FD_SET(fd, &except_fds);
rs_trace("select for write on fd%d", fd);
rs = select(fd + 1, NULL, &write_fds, &except_fds, &tv);
if (rs == -1 && errno == EINTR) {
rs_trace("select was interrupted");
continue;
} else if (rs == -1) {
rs_log_error("select failed: %s", strerror(errno));
return EXIT_IO_ERROR;
} else if (rs == 0) {
rs_log_error("IO timeout");
return EXIT_IO_ERROR;
} else {
if (FD_ISSET(fd, &except_fds)) {
rs_trace("error condition on fd%d", fd);
}
return 0;
}
}
}
int dcc_readx(int fd, void *buf, size_t len)
{
ssize_t r;
int ret;
while (len > 0) {
r = read(fd, buf, len);
if (r == -1 && errno == EAGAIN) {
if ((ret = dcc_select_for_read(fd, dcc_io_timeout)))
return ret;
else
continue;
} else if (r == -1 && errno == EINTR) {
continue;
} else if (r == -1) {
rs_log_error("failed to read: %s", strerror(errno));
return EXIT_IO_ERROR;
} else if (r == 0) {
rs_log_error("unexpected eof on fd%d", fd);
return EXIT_TRUNCATED;
} else {
buf = &((char *) buf)[r];
len -= r;
}
}
return 0;
}
int dcc_writex(int fd, const void *buf, size_t len)
{
ssize_t r;
int ret;
while (len > 0) {
r = write(fd, buf, len);
if (r == -1 && errno == EAGAIN) {
if ((ret = dcc_select_for_write(fd, dcc_io_timeout)))
return ret;
else
continue;
} else if (r == -1 && errno == EINTR) {
continue;
} else if (r == -1) {
rs_log_error("failed to write: %s", strerror(errno));
return EXIT_IO_ERROR;
} else {
buf = &((char *) buf)[r];
len -= r;
}
}
return 0;
}
int tcp_cork_sock(int POSSIBLY_UNUSED(fd), int POSSIBLY_UNUSED(corked))
{
#ifdef TCP_CORK
if (!dcc_getenv_bool("DISTCC_TCP_CORK", 1))
return 0;
if (setsockopt(fd, SOL_TCP, TCP_CORK, &corked, sizeof corked) == -1) {
if (errno == ENOSYS || errno == ENOTSUP) {
if (corked)
rs_trace("no corks allowed on fd%d", fd);
} else {
rs_log_warning("setsockopt(corked=%d) failed: %s",
corked, strerror(errno));
}
}
#endif
return 0;
}
int dcc_close(int fd)
{
if (close(fd) != 0) {
rs_log_error("failed to close fd%d: %s", fd, strerror(errno));
return EXIT_IO_ERROR;
}
return 0;
}