4 * This contains a simple multithreaded TCP server manager. It sits around
5 * waiting on the specified port for incoming HTTP connections. When a
6 * connection is established, it calls context_loop() from context_loop.c.
21 #include <sys/types.h>
23 #include <sys/socket.h>
24 #ifdef HAVE_SYS_TIME_H
30 #include <netinet/in.h>
40 #include "webserver.h"
43 int vsnprintf(char *buf, size_t max, const char *fmt, va_list argp);
46 int verbosity = 9; /* Logging level */
47 int msock; /* master listening socket */
48 extern void *context_loop(int);
49 extern void *housekeeping_loop(void);
50 extern pthread_mutex_t SessionListMutex;
51 extern pthread_key_t MyConKey;
54 char *server_cookie = NULL;
57 char *ctdlhost = DEFAULT_HOST;
58 char *ctdlport = DEFAULT_PORT;
61 * This is a generic function to set up a master socket for listening on
62 * a TCP port. The server shuts down if the bind fails.
64 int ig_tcp_server(int port_number, int queue_len)
66 struct sockaddr_in sin;
69 memset(&sin, 0, sizeof(sin));
70 sin.sin_family = AF_INET;
71 sin.sin_addr.s_addr = INADDR_ANY;
73 if (port_number == 0) {
74 lprintf(1, "Cannot start: no port number specified.\n");
77 sin.sin_port = htons((u_short) port_number);
79 s = socket(PF_INET, SOCK_STREAM, (getprotobyname("tcp")->p_proto));
81 lprintf(1, "Can't create a socket: %s\n",
85 /* Set some socket options that make sense. */
87 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
89 if (bind(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
90 lprintf(1, "Can't bind: %s\n", strerror(errno));
93 if (listen(s, queue_len) < 0) {
94 lprintf(1, "Can't listen: %s\n", strerror(errno));
102 * Read data from the client socket.
104 * 1 Requested number of bytes has been read.
105 * 0 Request timed out.
106 * -1 Connection is broken, or other error.
108 int client_read_to(int sock, char *buf, int bytes, int timeout)
116 while (len < bytes) {
122 retval = select((sock) + 1,
123 &rfds, NULL, NULL, &tv);
124 if (FD_ISSET(sock, &rfds) == 0) {
127 rlen = read(sock, &buf[len], bytes - len);
129 lprintf(2, "client_read() failed: %s\n",
139 * Read data from the client socket with default timeout.
140 * (This is implemented in terms of client_read_to() and could be
141 * justifiably moved out of sysdep.c)
143 int client_read(int sock, char *buf, int bytes)
145 return (client_read_to(sock, buf, bytes, SLEEPING));
150 * client_gets() ... Get a LF-terminated line of text from the client.
151 * (This is implemented in terms of client_read() and could be
152 * justifiably moved out of sysdep.c)
154 int client_gets(int sock, char *buf)
158 /* Read one character at a time.
161 retval = client_read(sock, &buf[i], 1);
162 if (retval != 1 || buf[i] == '\n' || i == 255)
166 /* If we got a long line, discard characters until the newline.
169 while (buf[i] != '\n' && retval == 1)
170 retval = client_read(sock, &buf[i], 1);
173 * Strip any trailing non-printable characters.
176 while ((strlen(buf) > 0) && (!isprint(buf[strlen(buf) - 1]))) {
177 buf[strlen(buf) - 1] = 0;
184 * Start running as a daemon. Only close stdio if do_close_stdio is set.
186 void start_daemon(int do_close_stdio)
188 if (do_close_stdio) {
193 signal(SIGHUP, SIG_IGN);
194 signal(SIGINT, SIG_IGN);
195 signal(SIGQUIT, SIG_IGN);
200 void spawn_another_worker_thread() {
201 pthread_t SessThread; /* Thread descriptor */
202 pthread_attr_t attr; /* Thread attributes */
204 lprintf(3, "Creating a new thread\n");
206 /* set attributes for the new thread */
207 pthread_attr_init(&attr);
208 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
210 /* now create the thread */
211 if (pthread_create(&SessThread, &attr,
212 (void *(*)(void *)) worker_entry, NULL)
214 lprintf(1, "Can't create thread: %s\n",
220 * Here's where it all begins.
222 int main(int argc, char **argv)
224 pthread_t SessThread; /* Thread descriptor */
225 pthread_attr_t attr; /* Thread attributes */
226 int a, i; /* General-purpose variables */
227 int port = PORT_NUM; /* Port to listen on */
228 char tracefile[PATH_MAX];
230 /* Parse command line */
231 while ((a = getopt(argc, argv, "hp:t:c")) != EOF)
237 strcpy(tracefile, optarg);
238 freopen(tracefile, "w", stdout);
239 freopen(tracefile, "w", stderr);
240 freopen(tracefile, "r", stdin);
243 verbosity = atoi(optarg);
246 server_cookie = malloc(SIZ);
247 if (server_cookie != NULL) {
248 strcpy(server_cookie, "Set-cookie: wcserver=");
250 &server_cookie[strlen(server_cookie)],
252 lprintf(2, "gethostname: %s\n",
259 fprintf(stderr, "usage: webserver [-p localport] "
260 "[-t tracefile] [-c] "
261 "[remotehost [remoteport]]\n");
266 ctdlhost = argv[optind];
268 ctdlport = argv[optind];
270 /* Tell 'em who's in da house */
271 lprintf(1, SERVER "\n"
272 "Copyright (C) 1996-2003 by the Citadel/UX development team.\n"
273 "This software is distributed under the terms of the GNU General Public\n"
274 "License. If you paid for this software, someone is ripping you off.\n\n");
276 if (chdir(WEBCITDIR) != 0)
280 * Set up a place to put thread-specific data.
281 * We only need a single pointer per thread - it points to the
282 * wcsession struct to which the thread is currently bound.
284 if (pthread_key_create(&MyConKey, NULL) != 0) {
285 lprintf(1, "Can't create TSD key: %s\n", strerror(errno));
289 * Bind the server to our favorite port.
290 * There is no need to check for errors, because ig_tcp_server()
291 * exits if it doesn't succeed.
293 lprintf(2, "Attempting to bind to port %d...\n", port);
294 msock = ig_tcp_server(port, LISTEN_QUEUE_LENGTH);
295 lprintf(2, "Listening on socket %d\n", msock);
296 signal(SIGPIPE, SIG_IGN);
298 pthread_mutex_init(&SessionListMutex, NULL);
301 * Start up the housekeeping thread
303 pthread_attr_init(&attr);
304 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
305 pthread_create(&SessThread, &attr,
306 (void *(*)(void *)) housekeeping_loop, NULL);
309 /* Start a few initial worker threads */
310 for (i=0; i<(MIN_WORKER_THREADS); ++i) {
311 spawn_another_worker_thread();
314 /* now the original thread can go away. */
321 * Entry point for worker threads
323 void worker_entry(void) {
329 /* Only one thread can accept at a time */
330 ssock = accept(msock, NULL, 0);
332 lprintf(2, "accept() failed: %s\n", strerror(errno));
334 /* Set the SO_REUSEADDR socket option */
336 setsockopt(ssock, SOL_SOCKET, SO_REUSEADDR,
339 /* Perform an HTTP transaction... */
342 /* ...and close the socket. */
343 lingering_close(ssock);
346 } while (!time_to_die);
352 int lprintf(int loglevel, const char *format, ...)
357 va_start(ap, format);
358 vsprintf(buf, format, ap);
361 if (loglevel <= verbosity) {
365 gettimeofday(&tv, NULL);
366 tim = localtime((time_t *)&(tv.tv_sec));
368 if (WC && WC->wc_session) {
370 "%04d/%02d/%02d %2d:%02d:%02d.%03ld [%ld:%d] %s",
371 tim->tm_year + 1900, tim->tm_mon + 1,
372 tim->tm_mday, tim->tm_hour, tim->tm_min,
373 tim->tm_sec, (long)tv.tv_usec / 1000,
374 (long)pthread_self(),
375 WC->wc_session, buf);
378 "%04d/%02d/%02d %2d:%02d:%02d.%03ld [%ld] %s",
379 tim->tm_year + 1900, tim->tm_mon + 1,
380 tim->tm_mday, tim->tm_hour, tim->tm_min,
381 tim->tm_sec, (long)tv.tv_usec / 1000,
382 (long)pthread_self(),