From 8222166257be49ad9b9d08cfd7c9647912f641e2 Mon Sep 17 00:00:00 2001 From: Con Kolivas Date: Fri, 11 Apr 2014 13:42:47 +1000 Subject: [PATCH] Provide a write socket function --- src/libckpool.c | 26 ++++++++++++++++++++++++++ src/libckpool.h | 1 + 2 files changed, 27 insertions(+) diff --git a/src/libckpool.c b/src/libckpool.c index c42876ba..730bf575 100644 --- a/src/libckpool.c +++ b/src/libckpool.c @@ -388,6 +388,32 @@ out: return sockd; } +int write_socket(int fd, const void *buf, size_t nbyte) +{ + struct timeval tv_timeout = {1, 0}; + fd_set writefds; + int ret; + +retry: + FD_ZERO(&writefds); + FD_SET(fd, &writefds); + ret = select(fd + 1, NULL, &writefds, NULL, &tv_timeout); + if (ret < 0 && interrupted()) + goto retry; + if (ret < 1) { + if (!ret) + LOGNOTICE("Select timed out in write_socket"); + else + LOGNOTICE("Select failed in write_socket with errno %d", errno); + goto out; + } + ret = write(fd, buf, nbyte); + if (ret < 0) + LOGWARNING("Failed to write in write_socket"); +out: + return ret; +} + /* Align a size_t to 4 byte boundaries for fussy arches */ void align_len(size_t *len) { diff --git a/src/libckpool.h b/src/libckpool.h index 5eec0913..4cf54dde 100644 --- a/src/libckpool.h +++ b/src/libckpool.h @@ -138,6 +138,7 @@ void keep_sockalive(int fd); void noblock_socket(int fd); void block_socket(int fd); int connect_socket(char *url, char *port); +int write_socket(int fd, const void *buf, size_t nbyte); void align_len(size_t *len); void realloc_strcat(char **ptr, const char *s);