7 #include <sys/socket.h>
8 #include <netinet/in.h>
14 #include <netinet/tcp.h>
19 #include "appconfig.h"
21 #include "web_buffer.h"
22 #include "web_client.h"
23 #include "web_server.h"
24 #include "global_statistics.h"
28 int listen_backlog = LISTEN_BACKLOG;
31 int listen_port = LISTEN_PORT;
33 #ifdef NETDATA_INTERNAL_CHECKS
34 static void log_allocations(void)
41 if(mi.uordblks > mem) {
44 for(w = web_clients; w ; w = w->next) clients++;
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);
52 int create_listen_socket4(int port, int listen_backlog)
56 struct sockaddr_in name;
58 debug(D_LISTENER, "IPv4 creating new listening socket on port %d", port);
60 sock = socket(AF_INET, SOCK_STREAM, 0);
62 error("IPv4 socket() failed.");
66 /* avoid "address already in use" */
67 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void*)&sockopt, sizeof(sockopt));
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);
74 if(bind (sock, (struct sockaddr *) &name, sizeof (name)) < 0) {
76 error("IPv4 bind() failed.");
80 if(listen(sock, listen_backlog) < 0) {
82 fatal("IPv4 listen() failed.");
86 debug(D_LISTENER, "IPv4 listening port %d created", port);
90 int create_listen_socket6(int port, int listen_backlog)
94 struct sockaddr_in6 name;
96 debug(D_LISTENER, "IPv6 creating new listening socket on port %d", port);
98 sock = socket(AF_INET6, SOCK_STREAM, 0);
100 error("IPv6 socket() failed.");
104 /* avoid "address already in use" */
105 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void*)&sockopt, sizeof(sockopt));
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;
113 if (bind (sock, (struct sockaddr *) &name, sizeof (name)) < 0) {
115 error("IPv6 bind() failed.");
119 if (listen(sock, listen_backlog) < 0) {
121 fatal("IPv6 listen() failed.");
125 debug(D_LISTENER, "IPv6 listening port %d created", port);
130 // --------------------------------------------------------------------------------------
131 // the main socket listener
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
138 void *socket_listen_main(void *ptr)
142 info("WEB SERVER thread created with task id %d", gettid());
144 struct web_client *w;
150 if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
151 error("Cannot set pthread cancel type to DEFERRED.");
153 if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
154 error("Cannot set pthread cancel state to ENABLE.");
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);
159 if(listen_fd < 0) fatal("LISTENER: Listen socket is not ready.");
161 fd_set ifds, ofds, efds;
162 int fdmax = listen_fd;
173 FD_SET(listen_fd, &ifds);
174 FD_SET(listen_fd, &efds);
177 // debug(D_WEB_CLIENT, "LISTENER: Waiting...");
178 retval = select(fdmax+1, &ifds, &ofds, &efds, &tv);
181 error("LISTENER: select() failed.");
185 // check for new incoming connections
186 if(FD_ISSET(listen_fd, &ifds)) {
187 w = web_client_create(listen_fd);
189 // no need for error log - web_client_create already logged the error
193 if(pthread_create(&w->thread, NULL, web_client_main, w) != 0) {
194 error("%llu: failed to create new thread for web client.");
197 else if(pthread_detach(w->thread) != 0) {
198 error("%llu: Cannot request detach of newly created web client thread.", w->id);
202 else debug(D_WEB_CLIENT, "LISTENER: select() didn't do anything.");
206 // debug(D_WEB_CLIENT, "LISTENER: select() timeout.");
209 // cleanup unused clients
210 for(w = web_clients; w ; w = w?w->next:NULL) {
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
222 error("LISTENER: exit!");
224 if(listen_fd >= 0) close(listen_fd);