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