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>
41 int vsnprintf(char *buf, size_t max, const char *fmt, va_list argp);
44 int msock; /* master listening socket */
45 extern void *context_loop(int);
46 extern void *housekeeping_loop(void);
47 extern pthread_mutex_t SessionListMutex;
48 extern pthread_key_t MyConKey;
54 const char *defaulthost = DEFAULT_HOST;
55 const char *defaultport = DEFAULT_PORT;
57 pthread_mutex_t AcceptQueue;
60 * This is a generic function to set up a master socket for listening on
61 * a TCP port. The server shuts down if the bind fails.
63 int ig_tcp_server(int port_number, int queue_len)
65 struct sockaddr_in sin;
68 memset(&sin, 0, sizeof(sin));
69 sin.sin_family = AF_INET;
70 sin.sin_addr.s_addr = INADDR_ANY;
72 if (port_number == 0) {
74 "webcit: 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 fprintf(stderr, "webcit: 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 fprintf(stderr, "webcit: Can't bind: %s\n", strerror(errno));
93 if (listen(s, queue_len) < 0) {
94 fprintf(stderr, "webcit: Can't listen: %s\n", strerror(errno));
102 * client_write() ... Send binary data to the client.
104 void client_write(int sock, char *buf, int nbytes)
106 int bytes_written = 0;
108 while (bytes_written < nbytes) {
109 retval = write(sock, &buf[bytes_written],
110 nbytes - bytes_written);
112 fprintf(stderr, "client_write() failed: %s\n",
115 bytes_written = bytes_written + retval;
121 * cprintf() ... Send formatted printable data to the client.
123 void cprintf(int sock, const char *format,...)
128 va_start(arg_ptr, format);
129 if (vsnprintf(buf, sizeof buf, format, arg_ptr) == -1)
130 buf[sizeof buf - 2] = '\n';
131 client_write(sock, buf, strlen(buf));
137 * Read data from the client socket.
139 * 1 Requested number of bytes has been read.
140 * 0 Request timed out.
141 * If the socket breaks, the session is immediately terminated.
143 int client_read_to(int sock, char *buf, int bytes, int timeout)
151 while (len < bytes) {
157 retval = select((sock) + 1,
158 &rfds, NULL, NULL, &tv);
159 if (FD_ISSET(sock, &rfds) == 0) {
162 rlen = read(sock, &buf[len], bytes - len);
164 fprintf(stderr, "client_read() failed: %s\n",
173 * Read data from the client socket with default timeout.
174 * (This is implemented in terms of client_read_to() and could be
175 * justifiably moved out of sysdep.c)
177 int client_read(int sock, char *buf, int bytes)
179 return (client_read_to(sock, buf, bytes, SLEEPING));
184 * client_gets() ... Get a LF-terminated line of text from the client.
185 * (This is implemented in terms of client_read() and could be
186 * justifiably moved out of sysdep.c)
188 int client_gets(int sock, char *buf)
192 /* Read one character at a time.
195 retval = client_read(sock, &buf[i], 1);
196 if (retval != 1 || buf[i] == '\n' || i == 255)
200 /* If we got a long line, discard characters until the newline.
203 while (buf[i] != '\n' && retval == 1)
204 retval = client_read(sock, &buf[i], 1);
207 * Strip any trailing not-printable characters.
210 while ((strlen(buf) > 0) && (!isprint(buf[strlen(buf) - 1]))) {
211 buf[strlen(buf) - 1] = 0;
218 * Start running as a daemon. Only close stdio if do_close_stdio is set.
220 void start_daemon(int do_close_stdio)
222 if (do_close_stdio) {
227 signal(SIGHUP, SIG_IGN);
228 signal(SIGINT, SIG_IGN);
229 signal(SIGQUIT, SIG_IGN);
234 void spawn_another_worker_thread() {
235 pthread_t SessThread; /* Thread descriptor */
236 pthread_attr_t attr; /* Thread attributes */
238 fprintf(stderr, "Creating a new thread\n");
240 /* set attributes for the new thread */
241 pthread_attr_init(&attr);
242 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
244 /* now create the thread */
245 if (pthread_create(&SessThread, &attr,
246 (void *(*)(void *)) worker_entry, NULL)
248 fprintf(stderr, "webcit: can't create thread: %s\n",
254 * Here's where it all begins.
256 int main(int argc, char **argv)
258 pthread_t SessThread; /* Thread descriptor */
259 pthread_attr_t attr; /* Thread attributes */
260 int a, i; /* General-purpose variables */
261 int port = PORT_NUM; /* Port to listen on */
262 char tracefile[PATH_MAX];
264 /* Parse command line */
265 while ((a = getopt(argc, argv, "hp:t:")) != EOF)
271 strcpy(tracefile, optarg);
272 freopen(tracefile, "w", stdout);
273 freopen(tracefile, "w", stderr);
274 freopen(tracefile, "r", stdin);
277 fprintf(stderr, "usage: webserver [-p localport] "
279 "[remotehost [remoteport]]\n");
284 defaulthost = argv[optind];
286 defaultport = argv[optind];
288 /* Tell 'em who's in da house */
289 fprintf(stderr, SERVER "\n"
290 "Copyright (C) 1996-1999. All rights reserved.\n\n");
292 if (chdir(WEBCITDIR) != 0)
296 * Set up a place to put thread-specific data.
297 * We only need a single pointer per thread - it points to the
298 * wcsession struct to which the thread is currently bound.
300 if (pthread_key_create(&MyConKey, NULL) != 0) {
301 fprintf(stderr, "Can't create TSD key: %s\n", strerror(errno));
305 * Bind the server to our favorite port.
306 * There is no need to check for errors, because ig_tcp_server()
307 * exits if it doesn't succeed.
309 fprintf(stderr, "Attempting to bind to port %d...\n", port);
310 msock = ig_tcp_server(port, 5);
311 fprintf(stderr, "Listening on socket %d\n", msock);
312 signal(SIGPIPE, SIG_IGN);
314 pthread_mutex_init(&SessionListMutex, NULL);
315 pthread_mutex_init(&AcceptQueue, NULL);
318 * Start up the housekeeping thread
320 pthread_attr_init(&attr);
321 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
322 pthread_create(&SessThread, &attr,
323 (void *(*)(void *)) housekeeping_loop, NULL);
326 /* Start a few initial worker threads */
327 for (i=0; i<(INITIAL_WORKER_THREADS-1); ++i) {
328 spawn_another_worker_thread();
332 /* now the original thread becomes an ordinary worker thread */
339 * Entry point for worker threads
341 void worker_entry(void) {
343 struct sockaddr_in fsin;
347 time_t start_time, stop_time;
350 /* Only one thread can accept at a time */
351 start_time = time(NULL);
352 pthread_mutex_lock(&AcceptQueue);
353 ssock = accept(msock, (struct sockaddr *) &fsin, &alen);
354 pthread_mutex_unlock(&AcceptQueue);
355 stop_time = time(NULL);
357 /* Augment the thread pool if we're not blocking at all */
358 if ( (stop_time - start_time) == 0L) {
359 spawn_another_worker_thread();
363 fprintf(stderr, "webcit: accept() failed: %s\n",
366 /* Set the SO_REUSEADDR socket option */
368 setsockopt(ssock, SOL_SOCKET, SO_REUSEADDR,
373 } while (!time_to_die);