2 * - by Richard W.M. Jones <rich@annexia.org>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the Free
16 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 * $Id: main.c,v 1.16 2002/11/27 18:45:23 rich Exp $
38 #ifdef HAVE_SYS_TYPES_H
39 #include <sys/types.h>
42 #ifdef HAVE_SYS_WAIT_H
54 #include <pthr_http.h>
55 #include <pthr_server.h>
60 #include "mime_types.h"
61 #include "process_rq.h"
65 static void startup (int argc, char *argv[]);
66 static void start_thread (int sock, void *data);
67 static void catch_reload_signal (int sig);
68 static void catch_quit_signal (int sig);
69 static void catch_child_signal (int sig);
70 static void reload_config (void);
72 const char *config_path = "/etc/rws";
75 const pcre *re_alias_start,
86 main (int argc, char *argv[])
88 const char *user, *name, *stderr_file;
94 /* Initialise various shared regular expressions. */
95 re_alias_start = precomp (global_pool, "^alias[[:space:]]+(.*)$", 0);
96 re_alias_end = precomp (global_pool, "^end[[:space:]]+alias$", 0);
97 re_begin = precomp (global_pool, "^begin[[:space:]]+(.*):?[[:space:]]*$", 0);
98 re_conf_line = precomp (global_pool, "^(.*):[[:space:]]*(.*)?$", 0);
99 re_ext = precomp (global_pool, "\\.([^.]+)$", 0);
100 re_icon = precomp (global_pool,
101 "([^[:space:]]+)[[:space:]]+([0-9]+)x([0-9]+)[[:space:]]+\"(.*)\"", 0);
102 re_so = precomp (global_pool, "\\.so$", 0);
103 re_ws = precomp (global_pool, "[ \t]+", 0);
104 re_comma = precomp (global_pool, "[,;]+", 0);
106 while ((c = getopt (argc, argv, "C:p:a:fd")) != -1)
119 config_path = optarg;
131 fprintf (stderr, "usage: rws [-d] [-f] [-a address] [-p port] [-C configpath]\n");
136 /* Read configuration file. Do this early so we have configuration
137 * data available for other initializations.
141 /* Change the thread stack size? */
142 stack_size = cfg_get_int (0, 0, "stack size", 0);
144 pseudothread_set_stack_size (stack_size * 1024);
146 /* Initialize the file cache. */
149 /* Initialize the shared object script cache. */
152 /* Intercept signals. */
153 memset (&sa, 0, sizeof sa);
154 sa.sa_handler = catch_reload_signal;
155 sa.sa_flags = SA_RESTART;
156 sigaction (SIGHUP, &sa, 0);
158 sa.sa_handler = catch_quit_signal;
159 sigaction (SIGINT, &sa, 0);
160 sigaction (SIGQUIT, &sa, 0);
161 sigaction (SIGTERM, &sa, 0);
163 sa.sa_handler = catch_child_signal;
164 sa.sa_flags = SA_RESTART | SA_NOCLDSTOP;
165 sigaction (SIGCHLD, &sa, 0);
167 /* ... but ignore SIGPIPE errors. */
168 sa.sa_handler = SIG_IGN;
169 sa.sa_flags = SA_RESTART;
170 sigaction (SIGPIPE, &sa, 0);
172 /* Change user on startup. */
173 user = cfg_get_string (0, 0, "user", "nobody");
174 pthr_server_username (user);
178 pthr_server_disable_chdir ();
179 pthr_server_disable_fork ();
183 pthr_server_disable_close ();
186 /* Errors to error log file. */
187 stderr_file = cfg_get_string (0, 0, "error log", "/tmp/error_log");
188 pthr_server_stderr_file (stderr_file);
190 /* Enable stack trace on SIGSEGV. */
191 pthr_server_enable_stack_trace_on_segv ();
194 /* Set server name. */
195 name = psprintf (global_pool,
196 PACKAGE "/" VERSION " %s",
197 http_get_servername ());
198 http_set_servername (name);
201 pthr_server_startup_fn (startup);
203 /* Start up the server. */
204 pthr_server_main_loop (argc, argv, start_thread);
210 startup (int argc, char *argv[])
214 /* Open the access log. */
216 = fopen (cfg_get_string (0, 0, "access log", "/tmp/access_log"), "a");
219 perror ("open: access log");
222 if (fcntl (fileno (access_log), F_SETFD, FD_CLOEXEC) < 0)
223 { perror ("fcntl"); exit (1); }
225 http_set_log_file (access_log);
229 start_thread (int sock, void *data)
231 (void) new_process_rq (sock);
235 catch_reload_signal (int sig)
241 catch_quit_signal (int sig)
243 /* Exit gracefully (how!?!) XXX */
248 catch_child_signal (int sig)
250 /* Clean up the child process. */
257 /* Reread configuration file. */
258 cfg_reread_config (config_path);
260 /* Read /etc/mime.types file. */
261 mime_types_reread_config (cfg_get_string (0, 0, "mime types file",
264 /* Reset rewrite rules. */
265 rewrite_reset_rules ();