]> arthur.barton.de Git - netdata.git/blob - src/web_server.c
Merge pull request #560 from paulfantom/master
[netdata.git] / src / web_server.c
1 #ifdef HAVE_CONFIG_H
2 #include <config.h>
3 #endif
4 #include <unistd.h>
5 #include <stdlib.h>
6 #include <sys/types.h>
7 #include <sys/socket.h>
8 #include <netinet/in.h>
9 #include <arpa/inet.h>
10 #include <errno.h>
11 #include <pthread.h>
12 #include <sys/stat.h>
13 #include <fcntl.h>
14 #include <netinet/tcp.h>
15 #include <malloc.h>
16 #include <poll.h>
17
18 #include "common.h"
19 #include "log.h"
20 #include "appconfig.h"
21 #include "url.h"
22 #include "web_buffer.h"
23 #include "web_client.h"
24 #include "web_server.h"
25 #include "global_statistics.h"
26 #include "rrd.h"
27 #include "rrd2json.h"
28 #include "../config.h"
29
30 int listen_backlog = LISTEN_BACKLOG;
31 int listen_fd = -1;
32 int listen_port = LISTEN_PORT;
33 int web_server_mode = WEB_SERVER_MODE_MULTI_THREADED;
34
35 #ifdef NETDATA_INTERNAL_CHECKS
36 static void log_allocations(void)
37 {
38         static int mem = 0;
39
40         struct mallinfo mi;
41
42         mi = mallinfo();
43         if(mi.uordblks > mem) {
44                 int clients = 0;
45                 struct web_client *w;
46                 for(w = web_clients; w ; w = w->next) clients++;
47
48                 info("Allocated memory increased from %d to %d (increased by %d bytes). There are %d web clients connected.", mem, mi.uordblks, mi.uordblks - mem, clients);
49                 mem = mi.uordblks;
50         }
51 }
52 #endif
53
54 #ifndef HAVE_ACCEPT4
55 int accept4(int sock, struct sockaddr *addr, socklen_t *addrlen, int flags) {
56         int fd = accept(sock, addr, addrlen);
57         int newflags = 0;
58
59         if (fd < 0) return fd;
60
61         if (flags & SOCK_NONBLOCK) {
62                 newflags |= O_NONBLOCK;
63                 flags &= ~SOCK_NONBLOCK;
64         }
65
66         if (flags & SOCK_CLOEXEC) {
67                 newflags |= O_CLOEXEC;
68                 flags &= ~SOCK_CLOEXEC;
69         }
70
71         if (flags) {
72                 errno = -EINVAL;
73                 return -1;
74         }
75
76         if (fcntl(fd, F_SETFL, newflags) < 0) {
77                 int saved_errno = errno;
78                 close(fd);
79                 errno = saved_errno;
80                 return -1;
81         }
82
83         return fd;
84 }
85 #endif
86
87 static int is_ip_anything(const char *ip)
88 {
89         if(!ip || !*ip
90                         || !strcmp(ip, "any")
91                         || !strcmp(ip, "all")
92                         || !strcmp(ip, "*")
93                         || !strcmp(ip, "::")
94                         || !strcmp(ip, "0.0.0.0")
95                         ) return 1;
96
97         return 0;
98 }
99
100 int create_listen_socket4(const char *ip, int port, int listen_backlog)
101 {
102         int sock;
103         int sockopt = 1;
104
105         debug(D_LISTENER, "IPv4 creating new listening socket on port %d", port);
106
107         sock = socket(AF_INET, SOCK_STREAM, 0);
108         if(sock < 0) {
109                 error("IPv4 socket() failed.");
110                 return -1;
111         }
112
113         /* avoid "address already in use" */
114         if(setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void*)&sockopt, sizeof(sockopt)) != 0)
115                 error("Cannot set SO_REUSEADDR on port's %d socket.", port);
116
117         struct sockaddr_in name;
118         memset(&name, 0, sizeof(struct sockaddr_in));
119         name.sin_family = AF_INET;
120         name.sin_port = htons (port);
121
122         if(is_ip_anything(ip)) {
123                 name.sin_addr.s_addr = htonl(INADDR_ANY);
124                 // info("Listening on any IPs (IPv4).");
125         }
126         else {
127                 int ret = inet_pton(AF_INET, ip, (void *)&name.sin_addr.s_addr);
128                 if(ret != 1) {
129                         error("Failed to convert IP '%s' to a valid IPv4 address.", ip);
130                         close(sock);
131                         return -1;
132                 }
133                 // info("Listening on IP '%s' (IPv4).", ip);
134         }
135
136         if(bind (sock, (struct sockaddr *) &name, sizeof (name)) < 0) {
137                 close(sock);
138                 error("IPv4 bind() failed.");
139                 return -1;
140         }
141
142         if(listen(sock, listen_backlog) < 0) {
143                 close(sock);
144                 fatal("IPv4 listen() failed.");
145                 return -1;
146         }
147
148         debug(D_LISTENER, "IPv4 listening port %d created", port);
149         return sock;
150 }
151
152 int create_listen_socket6(const char *ip, int port, int listen_backlog)
153 {
154         int sock = -1;
155         int sockopt = 1;
156
157         debug(D_LISTENER, "IPv6 creating new listening socket on port %d", port);
158
159         sock = socket(AF_INET6, SOCK_STREAM, 0);
160         if (sock < 0) {
161                 error("IPv6 socket() failed. Disabling IPv6.");
162                 return -1;
163         }
164
165         /* avoid "address already in use" */
166         if(setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void*)&sockopt, sizeof(sockopt)) != 0)
167                 error("Cannot set SO_REUSEADDR on port's %d socket.", port);
168
169         struct sockaddr_in6 name;
170         memset(&name, 0, sizeof(struct sockaddr_in6));
171         name.sin6_family = AF_INET6;
172         name.sin6_port = htons ((uint16_t) port);
173
174         if(is_ip_anything(ip)) {
175                 name.sin6_addr = in6addr_any;
176                 // info("Listening on all IPs (IPv6 and IPv4)");
177         }
178         else {
179                 int ret = inet_pton(AF_INET6, ip, (void *)&name.sin6_addr.s6_addr);
180                 if(ret != 1) {
181                         error("Failed to convert IP '%s' to a valid IPv6 address. Disabling IPv6.", ip);
182                         close(sock);
183                         return -1;
184                 }
185                 // info("Listening on IP '%s' (IPv6)", ip);
186         }
187
188         name.sin6_scope_id = 0;
189
190         if (bind (sock, (struct sockaddr *) &name, sizeof (name)) < 0) {
191                 close(sock);
192                 error("IPv6 bind() failed. Disabling IPv6.");
193                 return -1;
194         }
195
196         if (listen(sock, listen_backlog) < 0) {
197                 close(sock);
198                 error("IPv6 listen() failed. Disabling IPv6.");
199                 return -1;
200         }
201
202         debug(D_LISTENER, "IPv6 listening port %d created", port);
203         return sock;
204 }
205
206
207 int create_listen_socket(void) {
208         listen_backlog = (int) config_get_number("global", "http port listen backlog", LISTEN_BACKLOG);
209
210         listen_port = (int) config_get_number("global", "port", LISTEN_PORT);
211         if(listen_port < 1 || listen_port > 65535) {
212                 error("Invalid listen port %d given. Defaulting to %d.", listen_port, LISTEN_PORT);
213                 listen_port = LISTEN_PORT;
214         }
215         else debug(D_OPTIONS, "Listen port set to %d.", listen_port);
216
217         int ip = 0;
218         char *ipv = config_get("global", "ip version", "any");
219         if(!strcmp(ipv, "any") || !strcmp(ipv, "both") || !strcmp(ipv, "all")) ip = 0;
220         else if(!strcmp(ipv, "ipv4") || !strcmp(ipv, "IPV4") || !strcmp(ipv, "IPv4") || !strcmp(ipv, "4")) ip = 4;
221         else if(!strcmp(ipv, "ipv6") || !strcmp(ipv, "IPV6") || !strcmp(ipv, "IPv6") || !strcmp(ipv, "6")) ip = 6;
222         else error("Cannot understand ip version '%s'. Assuming 'any'.", ipv);
223
224         if(ip == 0 || ip == 6)
225                 listen_fd = create_listen_socket6(config_get("global", "bind socket to IP", "*"), listen_port, listen_backlog);
226
227         if(listen_fd == -1) {
228                 listen_fd = create_listen_socket4(config_get("global", "bind socket to IP", "*"), listen_port, listen_backlog);
229                 //if(listen_fd != -1 && ip != 4)
230                 //      info("Managed to open an IPv4 socket on port %d.", listen_port);
231         }
232
233         return listen_fd;
234 }
235
236 // --------------------------------------------------------------------------------------
237 // the main socket listener
238
239 // 1. it accepts new incoming requests on our port
240 // 2. creates a new web_client for each connection received
241 // 3. spawns a new pthread to serve the client (this is optimal for keep-alive clients)
242 // 4. cleans up old web_clients that their pthreads have been exited
243
244 #define CLEANUP_EVERY_EVENTS 100
245
246 void *socket_listen_main_multi_threaded(void *ptr) {
247         (void)ptr;
248
249         web_server_mode = WEB_SERVER_MODE_MULTI_THREADED;
250         info("Multi-threaded WEB SERVER thread created with task id %d", gettid());
251
252         struct web_client *w;
253         int retval, failures = 0, counter = 0;
254
255         if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
256                 error("Cannot set pthread cancel type to DEFERRED.");
257
258         if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
259                 error("Cannot set pthread cancel state to ENABLE.");
260
261         if(listen_fd < 0)
262                 fatal("LISTENER: Listen socket %d is not ready, or invalid.", listen_fd);
263
264         for(;;) {
265                 struct pollfd fd = { .fd = listen_fd, .events = POLLIN, .revents = 0 };
266                 int timeout = 10 * 1000;
267
268                 // debug(D_WEB_CLIENT, "LISTENER: Waiting...");
269                 retval = poll(&fd, 1, timeout);
270
271                 if(unlikely(retval == -1)) {
272                         debug(D_WEB_CLIENT, "LISTENER: poll() failed.");
273                         failures++;
274
275                         if(failures > 10) {
276                                 error("LISTENER: our listen port %d seems dead. Re-opening it.", listen_fd);
277                                 close(listen_fd);
278                                 listen_fd = -1;
279                                 sleep(5);
280                                 create_listen_socket();
281                                 if(listen_fd < 0)
282                                         fatal("Cannot listen for web clients (connected clients %lu).", global_statistics.connected_clients);
283
284                                 failures = 0;
285                         }
286
287                         continue;
288                 }
289                 else if(likely(retval)) {
290                         // check for new incoming connections
291                         if(fd.revents & POLLIN || fd.revents & POLLPRI) {
292                                 w = web_client_create(listen_fd);
293                                 if(unlikely(!w)) {
294                                         // no need for error log - web_client_create already logged the error
295                                         continue;
296                                 }
297
298                                 if(pthread_create(&w->thread, NULL, web_client_main, w) != 0) {
299                                         error("%llu: failed to create new thread for web client.", w->id);
300                                         w->obsolete = 1;
301                                 }
302                                 else if(pthread_detach(w->thread) != 0) {
303                                         error("%llu: Cannot request detach of newly created web client thread.", w->id);
304                                         w->obsolete = 1;
305                                 }
306                         }
307                         else {
308                                 failures++;
309                                 debug(D_WEB_CLIENT, "LISTENER: select() didn't do anything.");
310                                 continue;
311                         }
312                 }
313                 else {
314                         debug(D_WEB_CLIENT, "LISTENER: select() timeout.");
315                         counter = CLEANUP_EVERY_EVENTS;
316                 }
317
318                 // cleanup unused clients
319                 counter++;
320                 if(counter > CLEANUP_EVERY_EVENTS) {
321                         counter = 0;
322                         for (w = web_clients; w;) {
323                                 if (w->obsolete) {
324                                         debug(D_WEB_CLIENT, "%llu: Removing client.", w->id);
325                                         // pthread_cancel(w->thread);
326                                         // pthread_join(w->thread, NULL);
327                                         w = web_client_free(w);
328 #ifdef NETDATA_INTERNAL_CHECKS
329                                         log_allocations();
330 #endif
331                                 }
332                                 else w = w->next;
333                         }
334                 }
335
336                 failures = 0;
337         }
338
339         debug(D_WEB_CLIENT, "LISTENER: exit!");
340         close(listen_fd);
341         listen_fd = -1;
342         return NULL;
343 }
344
345 struct web_client *single_threaded_clients[FD_SETSIZE];
346
347 static inline int single_threaded_link_client(struct web_client *w, fd_set *ifds, fd_set *ofds, fd_set *efds, int *max) {
348         if(unlikely(w->obsolete || w->dead || (!w->wait_receive && !w->wait_send)))
349                 return 1;
350
351         if(unlikely(w->ifd < 0 || w->ifd >= FD_SETSIZE || w->ofd < 0 || w->ofd >= FD_SETSIZE)) {
352                 error("%llu: invalid file descriptor, ifd = %d, ofd = %d (required 0 <= fd < FD_SETSIZE (%d)", w->id, w->ifd, w->ofd, FD_SETSIZE);
353                 return 1;
354         }
355
356         FD_SET(w->ifd, efds);
357         if(unlikely(*max < w->ifd)) *max = w->ifd;
358
359         if(unlikely(w->ifd != w->ofd)) {
360                 if(*max < w->ofd) *max = w->ofd;
361                 FD_SET(w->ofd, efds);
362         }
363
364         if(w->wait_receive) FD_SET(w->ifd, ifds);
365         if(w->wait_send)    FD_SET(w->ofd, ofds);
366
367         single_threaded_clients[w->ifd] = w;
368         single_threaded_clients[w->ofd] = w;
369
370         return 0;
371 }
372
373 static inline int single_threaded_unlink_client(struct web_client *w, fd_set *ifds, fd_set *ofds, fd_set *efds) {
374         FD_CLR(w->ifd, efds);
375         if(unlikely(w->ifd != w->ofd)) FD_CLR(w->ofd, efds);
376
377         if(w->wait_receive) FD_CLR(w->ifd, ifds);
378         if(w->wait_send)    FD_CLR(w->ofd, ofds);
379
380         single_threaded_clients[w->ifd] = NULL;
381         single_threaded_clients[w->ofd] = NULL;
382
383         if(unlikely(w->obsolete || w->dead || (!w->wait_receive && !w->wait_send)))
384                 return 1;
385
386         return 0;
387 }
388
389 void *socket_listen_main_single_threaded(void *ptr) {
390         (void)ptr;
391
392         web_server_mode = WEB_SERVER_MODE_SINGLE_THREADED;
393
394         info("Single threaded WEB SERVER thread created with task id %d", gettid());
395
396         struct web_client *w;
397         int retval, failures = 0;
398
399         if(ptr) { ; }
400
401         if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
402                 error("Cannot set pthread cancel type to DEFERRED.");
403
404         if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
405                 error("Cannot set pthread cancel state to ENABLE.");
406
407         if(listen_fd < 0 || listen_fd >= FD_SETSIZE)
408                 fatal("LISTENER: Listen socket %d is not ready, or invalid.", listen_fd);
409
410         int i;
411         for(i = 0; i < FD_SETSIZE ; i++)
412                 single_threaded_clients[i] = NULL;
413
414         fd_set ifds, ofds, efds, rifds, rofds, refds;
415         FD_ZERO (&ifds);
416         FD_ZERO (&ofds);
417         FD_ZERO (&efds);
418         FD_SET(listen_fd, &ifds);
419         FD_SET(listen_fd, &efds);
420         int fdmax = listen_fd;
421
422         for(;;) {
423                 debug(D_WEB_CLIENT_ACCESS, "LISTENER: single threaded web server waiting (listen fd = %d, fdmax = %d)...", listen_fd, fdmax);
424
425                 struct timeval tv = { .tv_sec = 1, .tv_usec = 0 };
426                 rifds = ifds;
427                 rofds = ofds;
428                 refds = efds;
429                 retval = select(fdmax+1, &rifds, &rofds, &refds, &tv);
430
431                 if(unlikely(retval == -1)) {
432                         debug(D_WEB_CLIENT, "LISTENER: select() failed.");
433                         failures++;
434                         if(failures > 10) {
435                                 if(global_statistics.connected_clients) {
436                                         error("REMOVING ALL %lu WEB CLIENTS !", global_statistics.connected_clients);
437                                         while (web_clients) {
438                                                 single_threaded_unlink_client(web_clients, &ifds, &ofds, &efds);
439                                                 web_client_free(web_clients);
440                                         }
441                                 }
442
443                                 error("LISTENER: our listen port %d seems dead. Re-opening it.", listen_fd);
444
445                                 close(listen_fd);
446                                 listen_fd = -1;
447                                 sleep(5);
448
449                                 create_listen_socket();
450                                 if(listen_fd < 0 || listen_fd >= FD_SETSIZE)
451                                         fatal("Cannot listen for web clients (connected clients %lu).", global_statistics.connected_clients);
452
453                                 FD_ZERO (&ifds);
454                                 FD_ZERO (&ofds);
455                                 FD_ZERO (&efds);
456                                 FD_SET(listen_fd, &ifds);
457                                 FD_SET(listen_fd, &efds);
458                                 failures = 0;
459                         }
460                 }
461                 else if(likely(retval)) {
462                         failures = 0;
463                         debug(D_WEB_CLIENT_ACCESS, "LISTENER: got something.");
464
465                         if(FD_ISSET(listen_fd, &rifds)) {
466                                 debug(D_WEB_CLIENT_ACCESS, "LISTENER: new connection.");
467                                 w = web_client_create(listen_fd);
468                                 if(single_threaded_link_client(w, &ifds, &ofds, &ifds, &fdmax) != 0) {
469                                         web_client_free(w);
470                                 }
471                         }
472
473                         for(i = 0 ; i <= fdmax ; i++) {
474                                 if(likely(!FD_ISSET(i, &rifds) && !FD_ISSET(i, &rofds) && !FD_ISSET(i, &refds)))
475                                         continue;
476
477                                 w = single_threaded_clients[i];
478                                 if(unlikely(!w))
479                                         continue;
480
481                                 if(unlikely(single_threaded_unlink_client(w, &ifds, &ofds, &efds) != 0)) {
482                                         web_client_free(w);
483                                         continue;
484                                 }
485
486                                 if (unlikely(FD_ISSET(w->ifd, &refds) || FD_ISSET(w->ofd, &refds))) {
487                                         web_client_free(w);
488                                         continue;
489                                 }
490
491                                 if (unlikely(w->wait_receive && FD_ISSET(w->ifd, &rifds))) {
492                                         if (unlikely(web_client_receive(w) < 0)) {
493                                                 web_client_free(w);
494                                                 continue;
495                                         }
496
497                                         if (w->mode != WEB_CLIENT_MODE_FILECOPY) {
498                                                 debug(D_WEB_CLIENT, "%llu: Processing received data.", w->id);
499                                                 web_client_process(w);
500                                         }
501                                 }
502
503                                 if (unlikely(w->wait_send && FD_ISSET(w->ofd, &rofds))) {
504                                         if (unlikely(web_client_send(w) < 0)) {
505                                                 debug(D_WEB_CLIENT, "%llu: Cannot send data to client. Closing client.", w->id);
506                                                 web_client_free(w);
507                                                 continue;
508                                         }
509                                 }
510
511                                 if(unlikely(single_threaded_link_client(w, &ifds, &ofds, &efds, &fdmax) != 0)) {
512                                         web_client_free(w);
513                                 }
514                         }
515                 }
516                 else {
517                         debug(D_WEB_CLIENT_ACCESS, "LISTENER: single threaded web server timeout.");
518 #ifdef NETDATA_INTERNAL_CHECKS
519                         log_allocations();
520 #endif
521                 }
522         }
523
524         debug(D_WEB_CLIENT, "LISTENER: exit!");
525         close(listen_fd);
526         listen_fd = -1;
527         return NULL;
528 }