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