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) {
73 printf("webcit: Cannot start: no port number specified.\n");
76 sin.sin_port = htons((u_short) port_number);
78 s = socket(PF_INET, SOCK_STREAM, (getprotobyname("tcp")->p_proto));
80 printf("webcit: Can't create a socket: %s\n",
84 /* Set some socket options that make sense. */
86 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
88 if (bind(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
89 printf("webcit: Can't bind: %s\n", strerror(errno));
92 if (listen(s, queue_len) < 0) {
93 printf("webcit: Can't listen: %s\n", strerror(errno));
101 * client_write() ... Send binary data to the client.
103 void client_write(int sock, char *buf, int nbytes)
105 int bytes_written = 0;
107 while (bytes_written < nbytes) {
108 retval = write(sock, &buf[bytes_written],
109 nbytes - bytes_written);
111 printf("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 printf("client_read() failed: %s\n",
174 * Read data from the client socket with default timeout.
175 * (This is implemented in terms of client_read_to() and could be
176 * justifiably moved out of sysdep.c)
178 int client_read(int sock, char *buf, int bytes)
180 return (client_read_to(sock, buf, bytes, SLEEPING));
185 * client_gets() ... Get a LF-terminated line of text from the client.
186 * (This is implemented in terms of client_read() and could be
187 * justifiably moved out of sysdep.c)
189 int client_gets(int sock, char *buf)
193 /* Read one character at a time.
196 retval = client_read(sock, &buf[i], 1);
197 if (retval != 1 || buf[i] == '\n' || i == 255)
201 /* If we got a long line, discard characters until the newline.
204 while (buf[i] != '\n' && retval == 1)
205 retval = client_read(sock, &buf[i], 1);
208 * Strip any trailing not-printable characters.
211 while ((strlen(buf) > 0) && (!isprint(buf[strlen(buf) - 1]))) {
212 buf[strlen(buf) - 1] = 0;
219 * Start running as a daemon. Only close stdio if do_close_stdio is set.
221 void start_daemon(int do_close_stdio)
223 if (do_close_stdio) {
228 signal(SIGHUP, SIG_IGN);
229 signal(SIGINT, SIG_IGN);
230 signal(SIGQUIT, SIG_IGN);
236 * Here's where it all begins.
238 int main(int argc, char **argv)
240 pthread_t SessThread; /* Thread descriptor */
241 pthread_attr_t attr; /* Thread attributes */
242 int a, i; /* General-purpose variables */
243 int port = PORT_NUM; /* Port to listen on */
244 char tracefile[PATH_MAX];
246 /* Parse command line */
247 while ((a = getopt(argc, argv, "hp:t:")) != EOF)
253 strcpy(tracefile, optarg);
254 freopen(tracefile, "w", stdout);
255 freopen(tracefile, "w", stderr);
256 freopen(tracefile, "r", stdin);
259 fprintf(stderr, "usage: webserver [-p localport] "
261 "[remotehost [remoteport]]\n");
266 defaulthost = argv[optind];
268 defaultport = argv[optind];
270 /* Tell 'em who's in da house */
271 fprintf(stderr, SERVER "\n"
272 "Copyright (C) 1996-1999. All rights reserved.\n\n");
274 if (chdir(WEBCITDIR) != 0)
278 * Set up a place to put thread-specific data.
279 * We only need a single pointer per thread - it points to the
280 * wcsession struct to which the thread is currently bound.
282 if (pthread_key_create(&MyConKey, NULL) != 0) {
283 fprintf(stderr, "Can't create TSD key: %s\n", strerror(errno));
287 * Bind the server to our favorite port.
288 * There is no need to check for errors, because ig_tcp_server()
289 * exits if it doesn't succeed.
291 printf("Attempting to bind to port %d...\n", port);
292 msock = ig_tcp_server(port, 5);
293 printf("Listening on socket %d\n", msock);
294 signal(SIGPIPE, SIG_IGN);
296 pthread_mutex_init(&SessionListMutex, NULL);
297 pthread_mutex_init(&AcceptQueue, NULL);
300 * Start up the housekeeping thread
302 pthread_attr_init(&attr);
303 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
304 pthread_create(&SessThread, &attr,
305 (void *(*)(void *)) housekeeping_loop, NULL);
309 /* FIX make this variable */
310 for (i=0; i<10; ++i) {
312 /* set attributes for the new thread */
313 pthread_attr_init(&attr);
314 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
316 /* now create the thread */
317 if (pthread_create(&SessThread, &attr,
318 (void *(*)(void *)) worker_entry, NULL)
320 printf("webcit: can't create thread: %s\n",
325 /* now become a worker thread too */
332 * Entry point for worker threads
334 void worker_entry(void) {
336 struct sockaddr_in fsin;
342 /* Only one thread can accept at a time */
343 pthread_mutex_lock(&AcceptQueue);
344 ssock = accept(msock, (struct sockaddr *) &fsin, &alen);
345 pthread_mutex_unlock(&AcceptQueue);
347 printf("New connection on socket %d\n", ssock);
349 printf("webcit: accept() failed: %s\n",
352 /* Set the SO_REUSEADDR socket option */
354 setsockopt(ssock, SOL_SOCKET, SO_REUSEADDR,
359 } while (!time_to_die);