new license declaration text
[citadel.git] / webcit-ng / webserver.c
1 //
2 // webserver.c
3 //
4 // This module handles the task of setting up a listening socket, accepting
5 // connections, and dispatching active connections onto a pool of worker
6 // threads.
7 //
8 // Copyright (c) 1996-2022 by the citadel.org team
9 //
10 // This program is open source software.  Use, duplication, or
11 // disclosure are subject to the GNU General Public License v3.
12
13 #include "webcit.h"                     // All other headers are included from this header.
14
15 int num_threads_executing = 1;          // Number of worker threads currently bound to a connected client
16 int num_threads_existing = 1;           // Total number of worker threads that exist right now
17 int is_https = 0;                       // Set to nonzero if we are running as an HTTPS server today.
18 static void *original_brk = NULL;       // Remember the original program break so we can test for leaks
19
20
21 // Spawn an additional worker thread into the pool.
22 void spawn_another_worker_thread(int *pointer_to_master_socket) {
23         pthread_t th;           // Thread descriptor
24         pthread_attr_t attr;    // Thread attributes
25
26         // set attributes for the new thread
27         pthread_attr_init(&attr);
28         pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
29         pthread_attr_setstacksize(&attr, 1048576);      // Large stacks to prevent MIME parser crash on FreeBSD
30
31         // now create the thread
32         if (pthread_create(&th, &attr, (void *(*)(void *)) worker_entry, (void *) pointer_to_master_socket) != 0) {
33                 syslog(LOG_WARNING, "Can't create thread: %s", strerror(errno));
34         }
35         else {
36                 ++num_threads_existing;
37                 ++num_threads_executing;
38         }
39
40         // free up the attributes
41         pthread_attr_destroy(&attr);
42 }
43
44
45 // Entry point for worker threads
46 void worker_entry(int *pointer_to_master_socket) {
47         int master_socket = *pointer_to_master_socket;
48         int i = 0;
49         int fail_this_transaction = 0;
50         struct client_handle ch;
51
52         while (1) {
53                 // Each worker thread blocks on accept() while waiting for something to do.
54                 // We don't have to worry about the "thundering herd" problem on modern kernels; for an explanation see
55                 // https://stackoverflow.com/questions/2213779/does-the-thundering-herd-problem-exist-on-linux-anymore
56                 memset(&ch, 0, sizeof ch);
57                 ch.sock = -1;
58                 errno = EAGAIN;
59                 do {
60                         --num_threads_executing;
61                         syslog(LOG_DEBUG, "Additional memory allocated since startup: %d bytes", (int) (sbrk(0) - original_brk));
62                         syslog(LOG_DEBUG, "Thread 0x%x calling accept() on master socket %d", (unsigned int) pthread_self(),
63                                master_socket);
64                         ch.sock = accept(master_socket, NULL, 0);
65                         if (ch.sock < 0) {
66                                 syslog(LOG_DEBUG, "accept() : %s", strerror(errno));
67                         }
68                         ++num_threads_executing;
69                         syslog(LOG_DEBUG, "socket %d is awake , threads executing: %d , threads total: %d", ch.sock,
70                                num_threads_executing, num_threads_existing);
71                 } while ((master_socket > 0) && (ch.sock < 0));
72
73                 // If all threads are executing, spawn more, up to the maximum
74                 if ((num_threads_executing >= num_threads_existing) && (num_threads_existing <= MAX_WORKER_THREADS)) {
75                         spawn_another_worker_thread(pointer_to_master_socket);
76                 }
77
78                 // We have a client.  Do some work.
79
80                 // Set the SO_REUSEADDR socket option
81                 i = 1;
82                 setsockopt(ch.sock, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
83
84                 // If we are an HTTPS server, go crypto now.
85                 if (is_https) {
86                         starttls(&ch);
87                         if (ch.ssl_handle == NULL) {
88                                 fail_this_transaction = 1;
89                         }
90                 }
91                 else {
92                         int fdflags;
93                         fdflags = fcntl(ch.sock, F_GETFL);
94                         if (fdflags < 0) {
95                                 syslog(LOG_WARNING, "unable to get server socket flags! %s", strerror(errno));
96                         }
97                 }
98
99                 // Perform an HTTP transaction...
100                 if (fail_this_transaction == 0) {
101                         perform_one_http_transaction(&ch);
102                 }
103
104                 // Shut down SSL/TLS if required...
105                 if (is_https) {
106                         endtls(&ch);
107                 }
108                 // ...and close the socket.
109                 //syslog(LOG_DEBUG, "Closing socket %d...", ch.sock);
110                 //lingering_close(ch.sock);
111                 close(ch.sock);
112                 syslog(LOG_DEBUG, "Closed socket %d.", ch.sock);
113         }
114 }
115
116
117 // Start up a TCP HTTP[S] server on the requested port
118 int webserver(char *webserver_interface, int webserver_port, int webserver_protocol) {
119         int master_socket = (-1);
120         original_brk = sbrk(0);
121
122         switch (webserver_protocol) {
123         case WEBSERVER_HTTP:
124                 syslog(LOG_DEBUG, "Starting HTTP server on %s:%d", webserver_interface, webserver_port);
125                 master_socket = webcit_tcp_server(webserver_interface, webserver_port, 10);
126                 break;
127         case WEBSERVER_HTTPS:
128                 syslog(LOG_DEBUG, "Starting HTTPS server on %s:%d", webserver_interface, webserver_port);
129                 master_socket = webcit_tcp_server(webserver_interface, webserver_port, 10);
130                 init_ssl();
131                 is_https = 1;
132                 break;
133         default:
134                 syslog(LOG_ERR, "unknown protocol");
135                 ;;
136         }
137
138         if (master_socket < 1) {
139                 syslog(LOG_ERR, "Unable to bind the web server listening socket");
140                 return (1);
141         }
142
143         syslog(LOG_INFO, "Listening on socket %d", master_socket);
144         signal(SIGPIPE, SIG_IGN);
145
146         worker_entry(&master_socket);   // this thread becomes a worker
147         return (0);
148 }