]> arthur.barton.de Git - netdata.git/commitdiff
fixed dead clients logging; added protection against invalid file descriptors in...
authorCosta Tsaousis <costa@tsaousis.gr>
Sun, 29 May 2016 10:51:57 +0000 (13:51 +0300)
committerCosta Tsaousis <costa@tsaousis.gr>
Sun, 29 May 2016 10:51:57 +0000 (13:51 +0300)
src/web_client.c
src/web_server.c
src/web_server.h

index 78f6269776d93ff379ed1dd6cf171f7c818b0b30..6969caea8c8ee8da7f2900018dcae00d7feb4d27 100644 (file)
@@ -149,12 +149,16 @@ void web_client_reset(struct web_client *w)
        debug(D_WEB_CLIENT, "%llu: Reseting client.", w->id);
 
        if(w->stats_received_bytes || w->stats_sent_bytes) {
-               global_statistics_lock();
+               if(web_server_mode == WEB_SERVER_MODE_MULTI_THREADED)
+                       global_statistics_lock();
+
                global_statistics.web_requests++;
                global_statistics.web_usec += usecdiff(&tv, &w->tv_in);
                global_statistics.bytes_received += w->stats_received_bytes;
                global_statistics.bytes_sent += w->stats_sent_bytes;
-               global_statistics_unlock();
+
+               if(web_server_mode == WEB_SERVER_MODE_MULTI_THREADED)
+                       global_statistics_unlock();
        }
        w->stats_received_bytes = 0;
        w->stats_sent_bytes = 0;
@@ -1882,10 +1886,10 @@ ssize_t web_client_send_deflate(struct web_client *w)
                        return t;
                }
 
-               if(unlikely(w->keepalive == 0)) {
+               if(unlikely(!w->keepalive)) {
                        debug(D_WEB_CLIENT, "%llu: Closing (keep-alive is not enabled). %ld bytes sent.", w->id, w->response.sent);
-                       errno = 0;
-                       return(-1);
+                       WEB_CLIENT_IS_DEAD(w);
+                       return t;
                }
 
                // reset the client
@@ -1989,18 +1993,18 @@ ssize_t web_client_send(struct web_client *w) {
                        // we have to wait, more data will come
                        debug(D_WEB_CLIENT, "%llu: Waiting for more data to become available.", w->id);
                        w->wait_send = 0;
-                       return(0);
+                       return 0;
                }
 
-               if(unlikely(w->keepalive == 0)) {
+               if(unlikely(!w->keepalive)) {
                        debug(D_WEB_CLIENT, "%llu: Closing (keep-alive is not enabled). %ld bytes sent.", w->id, w->response.sent);
-                       errno = 0;
-                       return(-1);
+                       WEB_CLIENT_IS_DEAD(w);
+                       return 0;
                }
 
                web_client_reset(w);
                debug(D_WEB_CLIENT, "%llu: Done sending all data on socket. Waiting for next request on the same socket.", w->id);
-               return(0);
+               return 0;
        }
 
        bytes = send(w->ofd, &w->response.data->buffer[w->response.sent], w->response.data->len - w->response.sent, MSG_DONTWAIT);
@@ -2107,7 +2111,7 @@ void *web_client_main(void *ptr)
 
        for(;;) {
                if(unlikely(w->dead)) {
-                       error("%llu: client is dead.");
+                       error("%llu: client is dead.", w->id);
                        break;
                }
                else if(unlikely(!w->wait_receive && !w->wait_send)) {
@@ -2119,6 +2123,11 @@ void *web_client_main(void *ptr)
                FD_ZERO (&ofds);
                FD_ZERO (&efds);
 
+               if(w->ifd < 0 || w->ifd >= FD_SETSIZE || w->ofd < 0 || w->ofd >= FD_SETSIZE) {
+                       error("%llu: invalid file descriptor, ifd = %d, ofd = %d (required 0 <= fd < FD_SETSIZE (%d)", w->id, w->ifd, w->ofd, FD_SETSIZE);
+                       break;
+               }
+
                FD_SET(w->ifd, &efds);
 
                if(w->ifd != w->ofd)
@@ -2159,28 +2168,22 @@ void *web_client_main(void *ptr)
                }
 
                if(w->wait_send && FD_ISSET(w->ofd, &ofds)) {
-                       ssize_t bytes;
-                       if((bytes = web_client_send(w)) < 0) {
+                       if(web_client_send(w) < 0) {
                                debug(D_WEB_CLIENT, "%llu: Cannot send data to client. Closing client.", w->id);
-                               errno = 0;
                                break;
                        }
                }
 
                if(w->wait_receive && FD_ISSET(w->ifd, &ifds)) {
-                       ssize_t bytes;
-                       if((bytes = web_client_receive(w)) < 0) {
+                       if(web_client_receive(w) < 0) {
                                debug(D_WEB_CLIENT, "%llu: Cannot receive data from client. Closing client.", w->id);
-                               errno = 0;
                                break;
                        }
 
                        if(w->mode == WEB_CLIENT_MODE_NORMAL) {
-                               debug(D_WEB_CLIENT, "%llu: Attempting to process received data (%ld bytes).", w->id, bytes);
-                               // info("%llu: Attempting to process received data (%ld bytes).", w->id, bytes);
+                               debug(D_WEB_CLIENT, "%llu: Attempting to process received data.", w->id);
                                web_client_process(w);
                        }
-
                }
        }
 
index b736dae5eeb732b16087b067dfd6b6a07e46d93f..d49923a075f12075dc97156a577a0c813129c46a 100644 (file)
@@ -27,9 +27,9 @@
 #include "../config.h"
 
 int listen_backlog = LISTEN_BACKLOG;
-
 int listen_fd = -1;
 int listen_port = LISTEN_PORT;
+int web_server_mode = WEB_SERVER_MODE_MULTI_THREADED;
 
 #ifdef NETDATA_INTERNAL_CHECKS
 static void log_allocations(void)
@@ -205,6 +205,7 @@ int create_listen_socket(void) {
 void *socket_listen_main_multi_threaded(void *ptr) {
        if(ptr) { ; }
 
+       web_server_mode = WEB_SERVER_MODE_MULTI_THREADED;
        info("Multi-threaded WEB SERVER thread created with task id %d", gettid());
 
        struct web_client *w;
@@ -308,6 +309,8 @@ void *socket_listen_main_multi_threaded(void *ptr) {
 void *socket_listen_main_single_threaded(void *ptr) {
        if(ptr) { ; }
 
+       web_server_mode = WEB_SERVER_MODE_SINGLE_THREADED;
+
        info("Single threaded WEB SERVER thread created with task id %d", gettid());
 
        struct web_client *w;
@@ -343,7 +346,7 @@ void *socket_listen_main_single_threaded(void *ptr) {
 
                for(w = web_clients; w ; w = w->next) {
                        if(unlikely(w->dead)) {
-                               error("%llu: client is dead.");
+                               error("%llu: client is dead.", w->id);
                                w->obsolete = 1;
                        }
                        else if(unlikely(!w->wait_receive && !w->wait_send)) {
@@ -351,6 +354,11 @@ void *socket_listen_main_single_threaded(void *ptr) {
                                w->obsolete = 1;
                        }
 
+                       if(w->ifd < 0 || w->ifd >= FD_SETSIZE || w->ofd < 0 || w->ofd >= FD_SETSIZE) {
+                               error("%llu: invalid file descriptor, ifd = %d, ofd = %d (required 0 <= fd < FD_SETSIZE (%d)", w->id, w->ifd, w->ofd, FD_SETSIZE);
+                               w->obsolete = 1;
+                       }
+
                        if(unlikely(w->obsolete)) {
                                has_obsolete++;
                                continue;
index 68d16748e2e16c8d35fa2a912bba436d666e02ab..5df6bfa71c8456fbf67f254ce116c4f1a06b2a04 100644 (file)
@@ -13,6 +13,10 @@ extern int listen_backlog;
 extern int listen_fd;
 extern int listen_port;
 
+#define WEB_SERVER_MODE_MULTI_THREADED 0
+#define WEB_SERVER_MODE_SINGLE_THREADED 1
+extern int web_server_mode;
+
 extern int create_listen_socket4(const char *ip, int port, int listen_backlog);
 extern int create_listen_socket6(const char *ip, int port, int listen_backlog);
 extern void *socket_listen_main_multi_threaded(void *ptr);