]> arthur.barton.de Git - netdata.git/blob - src/web_server.c
Merge remote-tracking branch 'fredericopissarra/changes' into registry
[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
17 #include "common.h"
18 #include "log.h"
19 #include "appconfig.h"
20 #include "url.h"
21 #include "web_buffer.h"
22 #include "web_client.h"
23 #include "web_server.h"
24 #include "global_statistics.h"
25 #include "rrd.h"
26 #include "rrd2json.h"
27 #include "../config.h"
28
29 int listen_backlog = LISTEN_BACKLOG;
30
31 int listen_fd = -1;
32 int listen_port = LISTEN_PORT;
33
34 #ifdef NETDATA_INTERNAL_CHECKS
35 static void log_allocations(void)
36 {
37         static int mem = 0;
38
39         struct mallinfo mi;
40
41         mi = mallinfo();
42         if(mi.uordblks > mem) {
43                 int clients = 0;
44                 struct web_client *w;
45                 for(w = web_clients; w ; w = w->next) clients++;
46
47                 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);
48                 mem = mi.uordblks;
49         }
50 }
51 #endif
52
53 static int is_ip_anything(const char *ip)
54 {
55         if(!ip || !*ip
56                         || !strcmp(ip, "any")
57                         || !strcmp(ip, "all")
58                         || !strcmp(ip, "*")
59                         || !strcmp(ip, "::")
60                         || !strcmp(ip, "0.0.0.0")
61                         ) return 1;
62
63         return 0;
64 }
65
66 int create_listen_socket4(const char *ip, int port, int listen_backlog)
67 {
68         int sock;
69         int sockopt = 1;
70
71         debug(D_LISTENER, "IPv4 creating new listening socket on port %d", port);
72
73         sock = socket(AF_INET, SOCK_STREAM, 0);
74         if(sock < 0) {
75                 error("IPv4 socket() failed.");
76                 return -1;
77         }
78
79         /* avoid "address already in use" */
80         setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void*)&sockopt, sizeof(sockopt));
81
82         struct sockaddr_in name = { .sin_family = AF_INET, .sin_port = htons(port) };
83
84         if(is_ip_anything(ip)) {
85                 name.sin_addr.s_addr = htonl(INADDR_ANY);
86                 info("Listening on any IPs (IPv4).");
87         }
88         else {
89                 int ret = inet_pton(AF_INET, ip, (void *)&name.sin_addr.s_addr);
90                 if(ret != 1) {
91                         error("Failed to convert IP '%s' to a valid IPv4 address.", ip);
92                         close(sock);
93                         return -1;
94                 }
95                 info("Listening on IP '%s' (IPv4).", ip);
96         }
97
98         if(bind (sock, (struct sockaddr *) &name, sizeof (name)) < 0) {
99                 close(sock);
100                 error("IPv4 bind() failed.");
101                 return -1;
102         }
103
104         if(listen(sock, listen_backlog) < 0) {
105                 close(sock);
106                 fatal("IPv4 listen() failed.");
107                 return -1;
108         }
109
110         debug(D_LISTENER, "IPv4 listening port %d created", port);
111         return sock;
112 }
113
114 int create_listen_socket6(const char *ip, int port, int listen_backlog)
115 {
116         int sock = -1;
117         int sockopt = 1;
118
119         debug(D_LISTENER, "IPv6 creating new listening socket on port %d", port);
120
121         sock = socket(AF_INET6, SOCK_STREAM, 0);
122         if (sock < 0) {
123                 error("IPv6 socket() failed. Disabling IPv6.");
124                 return -1;
125         }
126
127         /* avoid "address already in use" */
128         setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void*)&sockopt, sizeof(sockopt));
129
130         struct sockaddr_in6 name = { .sin6_family = AF_INET6, .sin6_port = htons((uint16_t)port) };
131
132         if(is_ip_anything(ip)) {
133                 name.sin6_addr = in6addr_any;
134                 info("Listening on all IPs (IPv6 and IPv4)");
135         }
136         else {
137                 int ret = inet_pton(AF_INET6, ip, (void *)&name.sin6_addr.s6_addr);
138                 if(ret != 1) {
139                         error("Failed to convert IP '%s' to a valid IPv6 address. Disabling IPv6.", ip);
140                         close(sock);
141                         return -1;
142                 }
143                 info("Listening on IP '%s' (IPv6)", ip);
144         }
145
146         name.sin6_scope_id = 0;
147
148         if (bind (sock, (struct sockaddr *) &name, sizeof (name)) < 0) {
149                 close(sock);
150                 error("IPv6 bind() failed. Disabling IPv6.");
151                 return -1;
152         }
153
154         if (listen(sock, listen_backlog) < 0) {
155                 close(sock);
156                 error("IPv6 listen() failed. Disabling IPv6.");
157                 return -1;
158         }
159
160         debug(D_LISTENER, "IPv6 listening port %d created", port);
161         return sock;
162 }
163
164
165 // --------------------------------------------------------------------------------------
166 // the main socket listener
167
168 // 1. it accepts new incoming requests on our port
169 // 2. creates a new web_client for each connection received
170 // 3. spawns a new pthread to serve the client (this is optimal for keep-alive clients)
171 // 4. cleans up old web_clients that their pthreads have been exited
172
173 void *socket_listen_main(void *ptr)
174 {
175         if(ptr) { ; }
176
177         info("WEB SERVER thread created with task id %d", gettid());
178
179         struct web_client *w;
180         struct timeval tv;
181         int retval;
182
183         if(ptr) { ; }
184
185         if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
186                 error("Cannot set pthread cancel type to DEFERRED.");
187
188         if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
189                 error("Cannot set pthread cancel state to ENABLE.");
190
191         web_client_timeout = (int) config_get_number("global", "disconnect idle web clients after seconds", DEFAULT_DISCONNECT_IDLE_WEB_CLIENTS_AFTER_SECONDS);
192         web_enable_gzip = config_get_boolean("global", "enable web responses gzip compression", web_enable_gzip);
193
194         if(listen_fd < 0) fatal("LISTENER: Listen socket is not ready.");
195
196         fd_set ifds, ofds, efds;
197         int fdmax = listen_fd;
198
199         FD_ZERO (&ifds);
200         FD_ZERO (&ofds);
201         FD_ZERO (&efds);
202
203         for(;;) {
204                 tv.tv_sec = 0;
205                 tv.tv_usec = 200000;
206
207                 if(listen_fd >= 0) {
208                         FD_SET(listen_fd, &ifds);
209                         FD_SET(listen_fd, &efds);
210                 }
211
212                 // debug(D_WEB_CLIENT, "LISTENER: Waiting...");
213                 retval = select(fdmax+1, &ifds, &ofds, &efds, &tv);
214
215                 if(retval == -1) {
216                         error("LISTENER: select() failed.");
217                         continue;
218                 }
219                 else if(retval) {
220                         // check for new incoming connections
221                         if(FD_ISSET(listen_fd, &ifds)) {
222                                 w = web_client_create(listen_fd);
223                                 if(unlikely(!w)) {
224                                         // no need for error log - web_client_create already logged the error
225                                         continue;
226                                 }
227
228                                 if(pthread_create(&w->thread, NULL, web_client_main, w) != 0) {
229                                         error("%llu: failed to create new thread for web client.");
230                                         w->obsolete = 1;
231                                 }
232                                 else if(pthread_detach(w->thread) != 0) {
233                                         error("%llu: Cannot request detach of newly created web client thread.", w->id);
234                                         w->obsolete = 1;
235                                 }
236                         }
237                         else debug(D_WEB_CLIENT, "LISTENER: select() didn't do anything.");
238
239                 }
240                 //else {
241                 //      debug(D_WEB_CLIENT, "LISTENER: select() timeout.");
242                 //}
243
244                 // cleanup unused clients
245                 for(w = web_clients; w ; w = w?w->next:NULL) {
246                         if(w->obsolete) {
247                                 debug(D_WEB_CLIENT, "%llu: Removing client.", w->id);
248                                 // pthread_join(w->thread,  NULL);
249                                 w = web_client_free(w);
250 #ifdef NETDATA_INTERNAL_CHECKS
251                                 log_allocations();
252 #endif
253                         }
254                 }
255         }
256
257         error("LISTENER: exit!");
258
259         if(listen_fd >= 0) close(listen_fd);
260         exit(2);
261
262         return NULL;
263 }
264