From 9933435506b1c96e8bfe7a0fc5f305398fd0f62e Mon Sep 17 00:00:00 2001 From: Max Lv Date: Tue, 25 Jun 2013 21:48:28 +0800 Subject: [PATCH] fix asyncns with blocked send --- libasyncns/asyncns.c | 18 +++++++++++++++--- src/server.c | 13 ++----------- 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/libasyncns/asyncns.c b/libasyncns/asyncns.c index 3a2fef3a..90cc370f 100644 --- a/libasyncns/asyncns.c +++ b/libasyncns/asyncns.c @@ -388,6 +388,18 @@ static int fd_cloexec(int fd) { return fcntl(fd, F_SETFD, v | FD_CLOEXEC); } +static ssize_t block_and_send(int socket, const void *buffer, + size_t length, int flags) { + for(;;) { + int len = send(socket, buffer, length, flags); + if (len < 0 && + (errno == EAGAIN || errno == EINTR)) { + continue; + } + return length; + } +} + static int send_died(int out_fd) { rheader_t rh; assert(out_fd > 0); @@ -397,7 +409,7 @@ static int send_died(int out_fd) { rh.id = 0; rh.length = sizeof(rh); - return send(out_fd, &rh, rh.length, MSG_NOSIGNAL); + return block_and_send(out_fd, &rh, rh.length, MSG_NOSIGNAL); } static void *serialize_addrinfo(void *p, const struct addrinfo *ai, size_t *length, size_t maxlength) { @@ -460,7 +472,7 @@ static int send_addrinfo_reply(int out_fd, unsigned id, int ret, struct addrinfo if (ai) freeaddrinfo(ai); - return send(out_fd, resp, resp->header.length, MSG_NOSIGNAL); + return block_and_send(out_fd, resp, resp->header.length, MSG_NOSIGNAL); } static int send_nameinfo_reply(int out_fd, unsigned id, int ret, const char *host, const char *serv, int _errno, int _h_errno) { @@ -491,7 +503,7 @@ static int send_nameinfo_reply(int out_fd, unsigned id, int ret, const char *hos if (serv) memcpy((uint8_t *)data + sizeof(nameinfo_response_t) + hl, serv, sl); - return send(out_fd, resp, resp->header.length, MSG_NOSIGNAL); + return block_and_send(out_fd, resp, resp->header.length, MSG_NOSIGNAL); } static int handle_request(int out_fd, const packet_t *packet, size_t length) { diff --git a/src/server.c b/src/server.c index 65544794..b6cc7808 100644 --- a/src/server.c +++ b/src/server.c @@ -278,7 +278,7 @@ static void server_recv_cb (EV_P_ ev_io *w, int revents) { sprintf(port, "%d", p); if (verbose) { - LOGD("connect to: %s:%s in atyp %d", host, port, atyp); + LOGD("connect to: %s:%s", host, port); } struct addrinfo hints; @@ -398,16 +398,7 @@ static void server_resolve_cb(EV_P_ ev_timer *watcher, int revents) { if (asyncns_wait(asyncns, 0) == -1) { // asyncns error - LOGE("asnyncns session died."); - close_and_free_server(EV_A_ server); - - // reset asyncns sesstion - asyncns_free(asyncns); - if (!(server->listen_ctx->asyncns = asyncns_new(DNS_THREAD_NUM))) { - FATAL("asyncns failed"); - } - - return; + FATAL("asyncns exit unexpectedly."); } if (!asyncns_isdone(asyncns, query)) {