[Sheepdog] [PATCH 3/7] move sd_nonblocking and sd_nodelay to common functions
MORITA Kazutaka
morita.kazutaka at lab.ntt.co.jp
Thu Apr 7 02:56:19 CEST 2011
Signed-off-by: MORITA Kazutaka <morita.kazutaka at lab.ntt.co.jp>
---
include/net.h | 2 ++
lib/net.c | 36 +++++++++++++++++++++++++++---------
sheep/sdnet.c | 31 ++-----------------------------
3 files changed, 31 insertions(+), 38 deletions(-)
diff --git a/include/net.h b/include/net.h
index 539701e..6a9a787 100644
--- a/include/net.h
+++ b/include/net.h
@@ -38,5 +38,7 @@ int exec_req(int sockfd, struct sd_req *hdr, void *data,
int create_listen_ports(int port, int (*callback)(int fd, void *), void *data);
char *addr_to_str(char *str, int size, uint8_t *addr, uint16_t port);
+int set_nonblocking(int fd);
+int set_nodelay(int fd);
#endif
diff --git a/lib/net.c b/lib/net.c
index 56994e3..10fbbd8 100644
--- a/lib/net.c
+++ b/lib/net.c
@@ -146,18 +146,10 @@ int create_listen_ports(int port, int (*callback)(int fd, void *), void *data)
continue;
}
- ret = fcntl(fd, F_GETFL);
+ ret = set_nonblocking(fd);
if (ret < 0) {
- eprintf("can't fcntl (F_GETFL), %m\n");
close(fd);
continue;
- } else {
- ret = fcntl(fd, F_SETFL, ret | O_NONBLOCK);
- if (ret < 0) {
- eprintf("can't fcntl (O_NONBLOCK), %m\n");
- close(fd);
- continue;
- }
}
ret = callback(fd, data);
@@ -368,3 +360,29 @@ char *addr_to_str(char *str, int size, uint8_t *addr, uint16_t port)
return str;
}
+
+int set_nonblocking(int fd)
+{
+ int ret;
+
+ ret = fcntl(fd, F_GETFL);
+ if (ret < 0) {
+ eprintf("can't fcntl (F_GETFL), %m\n");
+ close(fd);
+ } else {
+ ret = fcntl(fd, F_SETFL, ret | O_NONBLOCK);
+ if (ret < 0)
+ eprintf("can't fcntl (O_NONBLOCK), %m\n");
+ }
+
+ return ret;
+}
+
+int set_nodelay(int fd)
+{
+ int ret, opt;
+
+ opt = 1;
+ ret = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof(opt));
+ return ret;
+}
diff --git a/sheep/sdnet.c b/sheep/sdnet.c
index f1ef27d..7e21072 100644
--- a/sheep/sdnet.c
+++ b/sheep/sdnet.c
@@ -526,7 +526,7 @@ static void listen_handler(int listen_fd, int events, void *data)
{
struct sockaddr_storage from;
socklen_t namesize;
- int fd, ret, opt;
+ int fd, ret;
struct client_info *ci;
if (sys->status == SD_STATUS_SHUTDOWN) {
@@ -542,8 +542,7 @@ static void listen_handler(int listen_fd, int events, void *data)
return;
}
- opt = 1;
- ret = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof(opt));
+ ret = set_nodelay(fd);
if (ret) {
close(fd);
return;
@@ -755,32 +754,6 @@ int remove_object(struct sheepdog_vnode_list_entry *e,
return 0;
}
-static int set_nonblocking(int fd)
-{
- int ret;
-
- ret = fcntl(fd, F_GETFL);
- if (ret < 0) {
- eprintf("can't fcntl (F_GETFL), %m\n");
- close(fd);
- } else {
- ret = fcntl(fd, F_SETFL, ret | O_NONBLOCK);
- if (ret < 0)
- eprintf("can't fcntl (O_NONBLOCK), %m\n");
- }
-
- return ret;
-}
-
-static int set_nodelay(int fd)
-{
- int ret, opt;
-
- opt = 1;
- ret = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof(opt));
- return ret;
-}
-
int get_sheep_fd(uint8_t *addr, uint16_t port, int node_idx,
uint32_t epoch, int worker_idx)
{
--
1.5.6.5
More information about the sheepdog
mailing list