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