/[hydra]/hydra/src/boa.c
ViewVC logotype

Contents of /hydra/src/boa.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.10 - (show annotations)
Sat Sep 28 10:05:00 2002 UTC (21 years, 6 months ago) by nmav
Branch: MAIN
Changes since 1.9: +8 -20 lines
File MIME type: text/plain
Improved HIC support. Now queues are used, instead of pipes for IPC. This avoids hanging the server, if the pipe was full.

1 /*
2 * Boa, an http server
3 * Copyright (C) 1995 Paul Phillips <paulp@go2net.com>
4 * Some changes Copyright (C) 1996 Charles F. Randall <crandall@goldsys.com>
5 * Some changes Copyright (C) 1996 Larry Doolittle <ldoolitt@boa.org>
6 * Some changes Copyright (C) 1996-2002 Jon Nelson <jnelson@boa.org>
7 * Portions Copyright (C) 2002 Nikos Mavroyanopoulos <nmav@gnutls.org>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 1, or (at your option)
12 * any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 *
23 */
24
25 /* $Id: boa.c,v 1.9 2002/09/27 23:40:29 nmav Exp $*/
26
27 #include "boa.h"
28 #include "ssl.h"
29 #include <sys/resource.h>
30 #ifdef ENABLE_SMP
31 pthread_t father_id;
32 #endif
33
34 extern int ssl_params_refresh;
35
36 /* globals */
37 int backlog = SO_MAXCONN;
38 time_t start_time;
39
40 time_t current_time;
41
42
43 /* static to boa.c */
44 static void fixup_server_root(void);
45 static socket_type create_server_socket( int port, int);
46 static void hic_init(void);
47 static void drop_privs(void);
48 static server_params *smp_init(socket_type server_s[2]);
49 static int sock_opt = 1;
50 static int do_fork = 1;
51 int devnullfd = -1;
52
53 int main(int argc, char **argv)
54 {
55 int c; /* command line arg */
56 socket_type server_s[2] = {{ -1, 0, 0, 0}, { -1, -1, 0, 0 }}; /* boa socket */
57 server_params *params;
58
59 /* set umask to u+rw, u-x, go-rwx */
60 c = umask(~0600);
61 if (c == -1) {
62 perror("umask");
63 exit(1);
64 }
65
66 devnullfd = open("/dev/null", 0);
67
68 /* make STDIN and STDOUT point to /dev/null */
69 if (devnullfd == -1) {
70 DIE("can't open /dev/null");
71 }
72
73 if (dup2(devnullfd, STDIN_FILENO) == -1) {
74 DIE("can't dup2 /dev/null to STDIN_FILENO");
75 }
76
77 if (dup2(devnullfd, STDOUT_FILENO) == -1) {
78 DIE("can't dup2 /dev/null to STDOUT_FILENO");
79 }
80
81 /* but first, update timestamp, because log_error_time uses it */
82 (void) time(&current_time);
83
84 while ((c = getopt(argc, argv, "c:r:d")) != -1) {
85 switch (c) {
86 case 'c':
87 if (server_root)
88 free(server_root);
89 server_root = strdup(optarg);
90 if (!server_root) {
91 perror("strdup (for server_root)");
92 exit(1);
93 }
94 break;
95 case 'r':
96 if (chdir(optarg) == -1) {
97 log_error_time();
98 perror("chdir (to chroot)");
99 exit(1);
100 }
101 if (chroot(optarg) == -1) {
102 log_error_time();
103 perror("chroot");
104 exit(1);
105 }
106 if (chdir("/") == -1) {
107 log_error_time();
108 perror("chdir (after chroot)");
109 exit(1);
110 }
111 break;
112 case 'd':
113 do_fork = 0;
114 break;
115 default:
116 fprintf(stderr, "Usage: %s [-c serverroot] [-r chroot] [-d]\n", argv[0]);
117 exit(1);
118 }
119 }
120
121 fixup_server_root();
122 read_config_files();
123 open_logs();
124
125 if ((boa_ssl >= 2 || boa_ssl == 0) && server_port > 0) {
126 server_s[0] = create_server_socket( server_port, 0);
127 }
128
129 if (boa_ssl != 0 && ssl_port > 0) {
130 server_s[1] = create_server_socket( ssl_port, 1);
131 }
132
133 if (server_s[1].socket == -1 && server_s[0].socket == -1) {
134 log_error_time();
135 fprintf(stderr, "Could not initialize sockets\n");
136 exit(1);
137 }
138
139 init_signals();
140 drop_privs();
141 create_common_env();
142 build_needs_escape();
143
144 if (boa_ssl) {
145 initialize_ssl();
146 }
147
148 initialize_mmap();
149
150 if (max_connections < 1) {
151 struct rlimit rl;
152
153 /* has not been set explicitly */
154 c = getrlimit(RLIMIT_NOFILE, &rl);
155 if (c < 0) {
156 perror("getrlimit");
157 exit(1);
158 }
159 max_connections = rl.rlim_cur;
160 }
161
162 /* background ourself */
163 if (do_fork) {
164 switch(fork()) {
165 case -1:
166 /* error */
167 perror("fork");
168 exit(1);
169 break;
170 case 0:
171 /* child, success */
172 break;
173 default:
174 /* parent, success */
175 exit(0);
176 break;
177 }
178 }
179
180 /* main loop */
181 timestamp();
182
183 start_time = current_time;
184
185 /* Blocks signals that are not supposed to be catched
186 * by the children.
187 */
188 block_main_signals();
189
190 hic_init();
191
192 /* spawn the children pool
193 */
194 params = smp_init( server_s);
195
196 /* unblock signals for daddy
197 */
198 unblock_main_signals();
199
200 /* regenerate parameters in that time interval
201 */
202 if (boa_ssl) {
203 if (ssl_params_refresh < DEFAULT_SSL_PARAMS_REFRESH) {
204 log_error_time();
205 fprintf(stderr, "SSL parameters will be refreshed every %d minutes\n",
206 DEFAULT_SSL_PARAMS_REFRESH/60);
207 ssl_params_refresh = DEFAULT_SSL_PARAMS_REFRESH;
208 }
209 alarm( ssl_params_refresh);
210 }
211
212 select_loop( params);
213
214 return 0;
215 }
216
217 server_params *global_server_params;
218 int global_server_params_size;
219
220 extern int server_max_threads;
221
222 /* This function will return a server_params pointer. This
223 * pointer is to be used as a pointer to the select loop.
224 */
225 static server_params* smp_init( socket_type server_s[2])
226 {
227 int i;
228 server_params *params;
229
230 #ifdef ENABLE_SMP
231 pthread_t tid;
232 int max_threads = server_max_threads;
233
234 father_id = pthread_self();
235 #else
236 const int max_threads = 1;
237 #endif
238
239 params = malloc( sizeof(server_params) * max_threads);
240 if (params == NULL) {
241 log_error_time();
242 fprintf(stderr,
243 "Could not allocate memory.\n");
244 exit(1);
245 }
246
247 for (i=0;i<max_threads;i++) {
248 params[i].server_s[0] = server_s[0];
249 params[i].server_s[1] = server_s[1];
250 params[i].request_ready = NULL;
251 params[i].request_block = NULL;
252 params[i].request_free = NULL;
253
254 /* for signal handling */
255 params[i].sighup_flag = 0;
256 params[i].sigchld_flag = 0;
257 params[i].sigalrm_flag = 0;
258 params[i].sigusr1_flag = 0;
259 params[i].sigterm_flag = 0;
260
261 params[i].sockbufsize = SOCKETBUF_SIZE;
262
263 params[i].status.requests = 0;
264 params[i].status.errors = 0;
265
266 params[i].total_connections = 0;
267 params[i].max_fd = 0;
268
269 params[i].handle_sigbus = 0;
270 }
271
272 #ifdef ENABLE_SMP
273 params[0].tid = father_id;
274
275 for( i=1;i<max_threads;i++) {
276 if (pthread_create( &tid, NULL, &select_loop, &params[i]) != 0)
277 {
278 log_error_time();
279 fprintf(stderr,
280 "Could not dispatch threads.\n");
281 exit(1);
282 }
283 params[i].tid = tid;
284 }
285 #endif
286
287 if (max_threads > 1) {
288 log_error_time();
289 fprintf(stderr,
290 "%s: Generated pool of %d threads.\n", SERVER_NAME, max_threads);
291 }
292
293 global_server_params_size = max_threads;
294 global_server_params = params;
295
296 return &params[0];
297 }
298
299 void smp_reinit()
300 {
301 #ifdef ENABLE_SMP
302 int i;
303 server_params *params = global_server_params;
304 int max_threads = server_max_threads;
305 #else
306 int max_threads = 1;
307 #endif
308
309 if (global_server_params_size < max_threads) {
310 log_error_time();
311 fprintf(stderr,
312 "Cannot increase threads on runtime.\n");
313 max_threads = global_server_params_size;
314 }
315
316 #ifdef ENABLE_SMP
317 for( i=1;i<max_threads;i++) {
318 pthread_t tid;
319 if (pthread_create( &tid, NULL, &select_loop, &params[i]) != 0)
320 {
321 log_error_time();
322 fprintf(stderr,
323 "Could not dispatch threads.\n");
324 exit(1);
325 }
326 params[i].tid = tid;
327 }
328 #endif
329
330 if (max_threads > 0) {
331 log_error_time();
332 fprintf(stderr,
333 "Regenerated a pool of %d threads.\n", max_threads);
334 }
335
336 return;
337 }
338
339 #ifdef ENABLE_HIC
340
341 pthread_t hic_tid;
342
343 /* This function will return a server_params pointer. This
344 * pointer is to be used as a pointer to the select loop.
345 */
346 static void hic_init()
347 {
348
349 #ifdef ENABLE_SMP
350
351 if (pthread_create( &hic_tid, NULL, &hic_main_loop, NULL) != 0)
352 {
353 log_error_time();
354 fprintf(stderr,
355 "Could not dispatch hic thread.\n");
356 exit(1);
357 }
358
359 log_error_time();
360 fprintf(stderr,
361 "%s: Dispatched HIC main thread.\n", SERVER_NAME);
362
363 #endif
364
365 return;
366 }
367
368 #endif /* ENABLE_HIC */
369
370 static socket_type create_server_socket( int port, int secure)
371 {
372 socket_type server_s;
373
374 server_s.secure = secure;
375 server_s.port = port;
376
377 server_s.socket = socket(SERVER_AF, SOCK_STREAM, IPPROTO_TCP);
378 if (server_s.socket == -1) {
379 DIE("unable to create socket");
380 }
381
382 /* server socket is nonblocking */
383 if (set_nonblock_fd(server_s.socket) == -1) {
384 DIE("fcntl: unable to set server socket to nonblocking");
385 }
386
387 /* close server socket on exec so cgi's can't write to it */
388 if (fcntl(server_s.socket, F_SETFD, 1) == -1) {
389 DIE("can't set close-on-exec on server socket!");
390 }
391
392 /* reuse socket addr */
393 if ((setsockopt(server_s.socket, SOL_SOCKET, SO_REUSEADDR, (void *) &sock_opt,
394 sizeof (sock_opt))) == -1) {
395 DIE("setsockopt");
396 }
397
398 /* internet family-specific code encapsulated in bind_server() */
399 if (bind_server(server_s.socket, server_ip, port) == -1) {
400 DIE("unable to bind");
401 }
402
403 /* listen: large number just in case your kernel is nicely tweaked */
404 if (listen(server_s.socket, backlog) == -1) {
405 DIE("unable to listen");
406 }
407 return server_s;
408 }
409
410 static void drop_privs(void)
411 {
412 /* give away our privs if we can */
413 if (getuid() == 0) {
414 struct passwd *passwdbuf;
415 passwdbuf = getpwuid(server_uid);
416 if (passwdbuf == NULL) {
417 DIE("getpwuid");
418 }
419 if (initgroups(passwdbuf->pw_name, passwdbuf->pw_gid) == -1) {
420 DIE("initgroups");
421 }
422 if (setgid(server_gid) == -1) {
423 DIE("setgid");
424 }
425 if (setuid(server_uid) == -1) {
426 DIE("setuid");
427 }
428 /* test for failed-but-return-was-successful setuid
429 * http://www.securityportal.com/list-archive/bugtraq/2000/Jun/0101.html
430 */
431 if (setuid(0) != -1) {
432 DIE("icky Linux kernel bug!");
433 }
434 } else {
435 if (server_gid || server_uid) {
436 log_error_time();
437 fprintf(stderr, "Warning: "
438 "Not running as root: no attempt to change"
439 " to uid %d gid %d\n", server_uid, server_gid);
440 }
441 server_gid = getgid();
442 server_uid = getuid();
443 }
444 }
445
446 /*
447 * Name: fixup_server_root
448 *
449 * Description: Makes sure the server root is valid.
450 *
451 */
452
453 static void fixup_server_root()
454 {
455 char *dirbuf;
456
457 if (!server_root) {
458 #ifdef SERVER_ROOT
459 server_root = strdup(SERVER_ROOT);
460 if (!server_root) {
461 perror("strdup (SERVER_ROOT)");
462 exit(1);
463 }
464 #else
465 fputs(SERVER_NAME": don't know where server root is. Please #define "
466 "SERVER_ROOT in defines.h\n"
467 "and recompile, or use the -c command line option to "
468 "specify it.\n", stderr);
469 exit(1);
470 #endif
471 }
472
473 if (chdir(server_root) == -1) {
474 fprintf(stderr, "Could not chdir to \"%s\": aborting\n",
475 server_root);
476 exit(1);
477 }
478
479 dirbuf = normalize_path(server_root);
480 free(server_root);
481 server_root = dirbuf;
482 }
483

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26