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