int attr;
int err;
struct addrinfo hints, *servinfo, *p;
+ int optval;
+ socklen_t optlen = sizeof(optval);
/* Prepare hint for getaddrinfo */
memset(&hints, 0, sizeof hints);
if ((sock = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
LOG(log_info, logtype_default, "tsock_getfd: socket CNID server %s:: %s",
host, strerror(errno));
- continue;
+ continue;
}
attr = 1;
sock = -1;
return -1;
}
-
+
if (connect(sock, p->ai_addr, p->ai_addrlen) == -1) {
if (errno == EINPROGRESS) {
struct timeval tv;
fd_set wfds;
FD_ZERO(&wfds);
FD_SET(sock, &wfds);
- if (select(sock + 1, NULL, &wfds, NULL, &tv) < 1) {
+
+ if ((err = select(sock + 1, NULL, &wfds, NULL, &tv)) == 0) {
+ /* timeout */
+ LOG(log_error, logtype_cnid, "getfd: select timed out for CNID server %s",
+ host);
+ close(sock);
+ sock = -1;
+ continue;
+ }
+ if (err == -1) {
+ /* select failed */
+ LOG(log_error, logtype_cnid, "getfd: select failed for CNID server %s",
+ host);
+ close(sock);
+ sock = -1;
+ continue;
+ }
+
+ if ( ! FD_ISSET(sock, &wfds)) {
/* give up */
- LOG(log_error, logtype_cnid, "getfd: select timed out for CNID server %s: %s",
- host, strerror(errno));
+ LOG(log_error, logtype_cnid, "getfd: socket not ready connecting to %s",
+ host);
close(sock);
sock = -1;
continue;
}
- int optval;
- socklen_t optlen = sizeof(optval);
- if (getsockopt(sock, SOL_SOCKET, SO_ERROR, &optval, &optlen) != 0 || optval != 0) {
- /* somethings still wrong */
- LOG(log_error, logtype_cnid, "getfd: getsockopt error with CNID server %s: %s",
- host, strerror(errno));
+
+ if ((err = getsockopt(sock, SOL_SOCKET, SO_ERROR, &optval, &optlen)) != 0 || optval != 0) {
+ if (err != 0) {
+ /* somethings very wrong */
+ LOG(log_error, logtype_cnid, "getfd: getsockopt error with CNID server %s: %s",
+ host, strerror(errno));
+ } else {
+ errno = optval;
+ LOG(log_error, logtype_cnid, "getfd: getsockopt says: %s",
+ strerror(errno));
+ }
close(sock);
sock = -1;
continue;
continue;
}
}
-
+
/* We've got a socket */
break;
}
freeaddrinfo(servinfo);
if (p == NULL) {
- LOG(log_error, logtype_cnid, "tsock_getfd: no suitable network config from CNID server %s:%s",
- host, port);
+ errno = optval;
+ LOG(log_error, logtype_cnid, "tsock_getfd: no suitable network config from CNID server (%s:%s): %s",
+ host, port, strerror(errno));
return -1;
}
static int write_vec(int fd, struct iovec *iov, ssize_t towrite, int vecs)
{
ssize_t len;
+ int slept = 0;
+ int sleepsecs;
while (1) {
if (((len = writev(fd, iov, vecs)) == -1 && errno == EINTR))
continue;
+ if ((! slept) && len == -1 && errno == EAGAIN) {
+ sleepsecs = 5;
+ while ((sleepsecs = sleep(sleepsecs)));
+ slept = 1;
+ continue;
+ }
+
if (len == towrite) /* wrote everything out */
break;
int len;
struct iovec iov[2];
- LOG(log_debug, logtype_cnid, "init_tsock: BEGIN. Opening volume '%s', CNID Server: %s/%s",
+ LOG(log_debug, logtype_cnid, "init_tsock: BEGIN. Opening volume '%s', CNID Server: %s/%s",
db->db_dir, db->cnidserver, db->cnidport);
if ((fd = tsock_getfd(db->cnidserver, db->cnidport)) < 0)
db->db_dir, strerror(errno));
return -1;
}
-
+
LOG(log_maxdebug, logtype_cnid, "send_packet: {done}");
return 0;
}
stored = 0;
while (stored < length) {
- len = read(socket, (u_int8_t *) data + stored, length - stored);
+ len = readt(socket, (u_int8_t *) data + stored, length - stored, 0, 5);
if (len == -1) {
switch (errno) {
case EINTR:
memcpy(db->client_stamp, stamp, ADEDLEN_PRIVSYN);
memcpy(db->stamp, stamp, ADEDLEN_PRIVSYN);
}
- LOG(log_debug, logtype_cnid, "transmit: attached to '%s', stamp: '%08lx'.",
+ LOG(log_debug, logtype_cnid, "transmit: attached to '%s', stamp: '%08lx'.",
db->db_dir, *(uint64_t *)stamp);
}
if (!dbd_rpc(db, rqst, rply)) {
db->fd = -1; /* FD not valid... will need to reconnect */
}
+ if (errno == ECONNREFUSED) { /* errno carefully injected in tsock_getfd */
+ /* give up */
+ LOG(log_error, logtype_cnid, "transmit: connection refused (db_dir %s)", db->db_dir);
+ return -1;
+ }
+
if (!clean) { /* don't sleep if just got disconnected by cnid server */
time(&t);
if (t - orig > MAX_DELAY) {