From f7399ef2ece50c1164ed6136ba87b254154cbd53 Mon Sep 17 00:00:00 2001 From: Roberto De Ioris Date: Sun, 21 Oct 2012 11:56:04 +0200 Subject: [PATCH] use linux SOCK_NONBLOCK on uwsgi_connect() --- core/protocol.c | 8 ++++++++ core/socket.c | 25 ++++++++++++++++++++----- plugins/corerouter/corerouter.c | 2 -- 3 files changed, 28 insertions(+), 7 deletions(-) diff --git a/core/protocol.c b/core/protocol.c index a38e528c..03f17fc1 100644 --- a/core/protocol.c +++ b/core/protocol.c @@ -368,7 +368,11 @@ int uwsgi_enqueue_message(char *host, int port, uint8_t modifier1, uint8_t modif return -1; } +#if defined(__linux__) && defined(SOCK_NONBLOCK) && !defined(OBSOLETE_LINUX_KERNEL) + uwsgi_poll.fd = socket(AF_INET, SOCK_STREAM|SOCK_NONBLOCK, 0); +#else uwsgi_poll.fd = socket(AF_INET, SOCK_STREAM, 0); +#endif if (uwsgi_poll.fd < 0) { uwsgi_error("socket()"); return -1; @@ -1161,7 +1165,11 @@ int uwsgi_ping_node(int node, struct wsgi_request *wsgi_req) { return 0; } +#if defined(__linux__) && defined(SOCK_NONBLOCK) && !defined(OBSOLETE_LINUX_KERNEL) + uwsgi_poll.fd = socket(AF_INET, SOCK_STREAM|SOCK_NONBLOCK, 0); +#else uwsgi_poll.fd = socket(AF_INET, SOCK_STREAM, 0); +#endif if (uwsgi_poll.fd < 0) { uwsgi_error("socket()"); return -1; diff --git a/core/socket.c b/core/socket.c index 0cf67164..55e61a62 100644 --- a/core/socket.c +++ b/core/socket.c @@ -334,7 +334,11 @@ int connect_to_unix(char *socket_name, int timeout, int async) { memcpy(uws_addr.sun_path, socket_name, UMIN(strlen(socket_name), 102)); } +#if defined(__linux__) && defined(SOCK_NONBLOCK) && !defined(OBSOLETE_LINUX_KERNEL) + uwsgi_poll.fd = socket(AF_UNIX, SOCK_STREAM|SOCK_NONBLOCK, 0); +#else uwsgi_poll.fd = socket(AF_UNIX, SOCK_STREAM, 0); +#endif if (uwsgi_poll.fd < 0) { uwsgi_error("socket()"); return -1; @@ -372,7 +376,11 @@ int connect_to_tcp(char *socket_name, int port, int timeout, int async) { socket_name[strlen(socket_name)] = ':'; +#if defined(__linux__) && defined(SOCK_NONBLOCK) && !defined(OBSOLETE_LINUX_KERNEL) + uwsgi_poll.fd = socket(AF_INET, SOCK_STREAM|SOCK_NONBLOCK, 0); +#else uwsgi_poll.fd = socket(AF_INET, SOCK_STREAM, 0); +#endif if (uwsgi_poll.fd < 0) { uwsgi_error("socket()"); return -1; @@ -677,13 +685,16 @@ void uwsgi_socket_b(int fd) { int timed_connect(struct pollfd *fdpoll, const struct sockaddr *addr, int addr_size, int timeout, int async) { - int arg, ret; + int ret; int soopt = 0; socklen_t solen = sizeof(int); int cnt; /* set non-blocking socket */ - arg = fcntl(fdpoll->fd, F_GETFL, NULL); +#if defined(__linux__) && defined(SOCK_NONBLOCK) && !defined(OBSOLETE_LINUX_KERNEL) + // hmm, nothing to do, as we are already non-blocking +#else + int arg = fcntl(fdpoll->fd, F_GETFL, NULL); if (arg < 0) { uwsgi_error("fcntl()"); return -1; @@ -693,6 +704,7 @@ int timed_connect(struct pollfd *fdpoll, const struct sockaddr *addr, int addr_s uwsgi_error("fcntl()"); return -1; } +#endif ret = connect(fdpoll->fd, addr, addr_size); @@ -700,17 +712,20 @@ int timed_connect(struct pollfd *fdpoll, const struct sockaddr *addr, int addr_s if (ret < 0 && errno != EINPROGRESS) { return -1; } + return 0; } + +#if defined(__linux__) && defined(SOCK_NONBLOCK) && !defined(OBSOLETE_LINUX_KERNEL) + // hmm, nothing to do, as we are already non-blocking +#else /* re-set blocking socket */ arg &= (~O_NONBLOCK); if (fcntl(fdpoll->fd, F_SETFL, arg) < 0) { uwsgi_error("fcntl()"); return -1; } - - if (async) - return 0; +#endif if (ret < 0) { /* check what happened */ diff --git a/plugins/corerouter/corerouter.c b/plugins/corerouter/corerouter.c index 08a9838a..98fbc5b5 100644 --- a/plugins/corerouter/corerouter.c +++ b/plugins/corerouter/corerouter.c @@ -754,9 +754,7 @@ void uwsgi_corerouter_loop(int id, void *data) { if (ugs->gateway == &ushared->gateways[id] && interesting_fd == ugs->fd) { if (!ugs->subscription) { #if defined(__linux__) && defined(SOCK_NONBLOCK) && !defined(OBSOLETE_LINUX_KERNEL) -#define _GNU_SOURCE new_connection = accept4(interesting_fd, (struct sockaddr *) &cr_addr, &cr_addr_len, SOCK_NONBLOCK); -#undef _GNU_SOURCE if (new_connection < 0) { taken = 1; break;