4 // This module handles the task of setting up a listening socket, accepting
5 // connections, and dispatching active connections onto a pool of worker
8 // Copyright (c) 1996-2018 by the citadel.org team
10 // This program is open source software. It runs great on the
11 // Linux operating system (and probably elsewhere). You can use,
12 // copy, and run it under the terms of the GNU General Public
15 // This program is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
20 #include "webcit.h" // All other headers are included from this header.
22 int num_threads_executing = 1; // Number of worker threads currently bound to a connected client
23 int num_threads_existing = 1; // Total number of worker threads that exist right now
24 int is_https = 0; // Set to nonzero if we are running as an HTTPS server today.
25 static void *original_brk = NULL; // Remember the original program break so we can test for leaks
29 * Spawn an additional worker thread into the pool.
31 void spawn_another_worker_thread(int *pointer_to_master_socket)
33 pthread_t th; // Thread descriptor
34 pthread_attr_t attr; // Thread attributes
36 /* set attributes for the new thread */
37 pthread_attr_init(&attr);
38 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
39 pthread_attr_setstacksize(&attr, 1048576); // Large stacks to prevent MIME parser crash on FreeBSD
41 /* now create the thread */
42 if (pthread_create(&th, &attr, (void *(*)(void *)) worker_entry, (void *) pointer_to_master_socket) != 0) {
43 syslog(LOG_WARNING, "Can't create thread: %s", strerror(errno));
45 ++num_threads_existing;
46 ++num_threads_executing;
49 /* free up the attributes */
50 pthread_attr_destroy(&attr);
55 * Entry point for worker threads
57 void worker_entry(int *pointer_to_master_socket)
59 int master_socket = *pointer_to_master_socket;
61 int fail_this_transaction = 0;
62 struct client_handle ch;
65 /* Each worker thread blocks on accept() while waiting for something to do. */
66 memset(&ch, 0, sizeof ch);
70 --num_threads_executing;
71 syslog(LOG_DEBUG, "Additional memory allocated since startup: %d bytes", (int) (sbrk(0) - original_brk));
72 syslog(LOG_DEBUG, "Thread 0x%x calling accept() on master socket %d", (unsigned int) pthread_self(),
74 ch.sock = accept(master_socket, NULL, 0);
76 syslog(LOG_DEBUG, "accept() : %s", strerror(errno));
78 ++num_threads_executing;
79 syslog(LOG_DEBUG, "socket %d is awake , threads executing: %d , threads total: %d", ch.sock,
80 num_threads_executing, num_threads_existing);
81 } while ((master_socket > 0) && (ch.sock < 0));
83 /* If all threads are executing, spawn more, up to the maximum */
84 if ((num_threads_executing >= num_threads_existing) && (num_threads_existing <= MAX_WORKER_THREADS)) {
85 spawn_another_worker_thread(pointer_to_master_socket);
88 /* We have a client. Do some work. */
90 /* Set the SO_REUSEADDR socket option */
92 setsockopt(ch.sock, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
94 /* If we are an HTTPS server, go crypto now. */
97 if (ch.ssl_handle == NULL) {
98 fail_this_transaction = 1;
102 fdflags = fcntl(ch.sock, F_GETFL);
104 syslog(LOG_WARNING, "unable to get server socket flags! %s", strerror(errno));
108 /* Perform an HTTP transaction... */
109 if (fail_this_transaction == 0) {
110 perform_one_http_transaction(&ch);
113 /* Shut down SSL/TLS if required... */
117 /* ...and close the socket. */
118 //syslog(LOG_DEBUG, "Closing socket %d...", ch.sock);
119 //lingering_close(ch.sock);
121 syslog(LOG_DEBUG, "Closed socket %d.", ch.sock);
127 * Start up a TCP HTTP[S] server on the requested port
129 int webserver(char *webserver_interface, int webserver_port, int webserver_protocol)
131 int master_socket = (-1);
132 original_brk = sbrk(0);
134 switch (webserver_protocol) {
136 syslog(LOG_DEBUG, "Starting HTTP server on %s:%d", webserver_interface, webserver_port);
137 master_socket = webcit_tcp_server(webserver_interface, webserver_port, 10);
139 case WEBSERVER_HTTPS:
140 syslog(LOG_DEBUG, "Starting HTTPS server on %s:%d", webserver_interface, webserver_port);
141 master_socket = webcit_tcp_server(webserver_interface, webserver_port, 10);
146 syslog(LOG_ERR, "unknown protocol");
150 if (master_socket < 1) {
151 syslog(LOG_ERR, "Unable to bind the web server listening socket");
155 syslog(LOG_INFO, "Listening on socket %d", master_socket);
156 signal(SIGPIPE, SIG_IGN);
158 worker_entry(&master_socket); // this thread becomes a worker