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