blob: 84ee209115e8cb14465e4ca571458ff5e4453524 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * HA-Proxy : High Availability-enabled HTTP/TCP proxy
3 * Copyright 2000-2006 Willy Tarreau <w@1wt.eu>.
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version
8 * 2 of the License, or (at your option) any later version.
9 *
10 * Please refer to RFC2068 or RFC2616 for informations about HTTP protocol, and
11 * RFC2965 for informations about cookies usage. More generally, the IETF HTTP
12 * Working Group's web site should be consulted for protocol related changes :
13 *
14 * http://ftp.ics.uci.edu/pub/ietf/http/
15 *
16 * Pending bugs (may be not fixed because never reproduced) :
17 * - solaris only : sometimes, an HTTP proxy with only a dispatch address causes
18 * the proxy to terminate (no core) if the client breaks the connection during
19 * the response. Seen on 1.1.8pre4, but never reproduced. May not be related to
20 * the snprintf() bug since requests were simple (GET / HTTP/1.0), but may be
21 * related to missing setsid() (fixed in 1.1.15)
22 * - a proxy with an invalid config will prevent the startup even if disabled.
23 *
24 * ChangeLog has moved to the CHANGELOG file.
25 *
26 * TODO:
27 * - handle properly intermediate incomplete server headers. Done ?
28 * - handle hot-reconfiguration
29 * - fix client/server state transition when server is in connect or headers state
30 * and client suddenly disconnects. The server *should* switch to SHUT_WR, but
31 * still handle HTTP headers.
32 * - remove MAX_NEWHDR
33 * - cut this huge file into several ones
34 *
35 */
36
37#include <stdio.h>
38#include <stdlib.h>
39#include <unistd.h>
40#include <string.h>
41#include <ctype.h>
42#include <sys/time.h>
43#include <sys/types.h>
44#include <sys/socket.h>
45#include <netinet/tcp.h>
46#include <netinet/in.h>
47#include <arpa/inet.h>
48#include <netdb.h>
49#include <fcntl.h>
50#include <errno.h>
51#include <signal.h>
52#include <stdarg.h>
53#include <sys/resource.h>
54#include <time.h>
55#include <syslog.h>
56
57#ifdef DEBUG_FULL
58#include <assert.h>
59#endif
60
Willy Tarreau2dd0d472006-06-29 17:53:05 +020061#include <common/appsession.h>
62#include <common/base64.h>
63#include <common/cfgparse.h>
64#include <common/compat.h>
65#include <common/config.h>
66#include <common/defaults.h>
67#include <common/memory.h>
68#include <common/mini-clist.h>
69#include <common/regex.h>
70#include <common/standard.h>
71#include <common/time.h>
72#include <common/uri_auth.h>
73#include <common/version.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020074
75#include <types/capture.h>
76#include <types/global.h>
77#include <types/httperr.h>
78#include <types/proto_http.h>
79
80#include <proto/backend.h>
81#include <proto/buffers.h>
82#include <proto/client.h>
83#include <proto/fd.h>
84#include <proto/log.h>
85#include <proto/polling.h>
Willy Tarreau80587432006-12-24 17:47:20 +010086#include <proto/proto_http.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020087#include <proto/proxy.h>
88#include <proto/queue.h>
89#include <proto/server.h>
90#include <proto/stream_sock.h>
91#include <proto/task.h>
92
Willy Tarreau6d1a9882007-01-07 02:03:04 +010093#ifdef CONFIG_HAP_TCPSPLICE
94#include <libtcpsplice.h>
95#endif
96
Willy Tarreaubaaee002006-06-26 02:48:02 +020097/*********************************************************************/
98
99/*********************************************************************/
100
101char *cfg_cfgfile = NULL; /* configuration file */
102char *progname = NULL; /* program name */
103int pid; /* current process id */
104
105/* global options */
106struct global global = {
107 logfac1 : -1,
108 logfac2 : -1,
109 loglev1 : 7, /* max syslog level : debug */
110 loglev2 : 7,
111 /* others NULL OK */
112};
113
114/*********************************************************************/
115
116int stopping; /* non zero means stopping in progress */
117
118/* Here we store informations about the pids of the processes we may pause
119 * or kill. We will send them a signal every 10 ms until we can bind to all
120 * our ports. With 200 retries, that's about 2 seconds.
121 */
122#define MAX_START_RETRIES 200
123static int nb_oldpids = 0;
124static int *oldpids = NULL;
125static int oldpids_sig; /* use USR1 or TERM */
126
127/* this is used to drain data, and as a temporary buffer for sprintf()... */
128char trash[BUFSIZE];
129
130const int zero = 0;
131const int one = 1;
132
133/*
134 * Syslog facilities and levels. Conforming to RFC3164.
135 */
136
137#define MAX_HOSTNAME_LEN 32
138static char hostname[MAX_HOSTNAME_LEN] = "";
139
140
141/*********************************************************************/
142/* general purpose functions ***************************************/
143/*********************************************************************/
144
145void display_version()
146{
147 printf("HA-Proxy version " HAPROXY_VERSION " " HAPROXY_DATE"\n");
148 printf("Copyright 2000-2006 Willy Tarreau <w@1wt.eu>\n\n");
149}
150
151/*
152 * This function prints the command line usage and exits
153 */
154void usage(char *name)
155{
156 display_version();
157 fprintf(stderr,
158 "Usage : %s -f <cfgfile> [ -vdV"
159 "D ] [ -n <maxconn> ] [ -N <maxpconn> ]\n"
160 " [ -p <pidfile> ] [ -m <max megs> ]\n"
161 " -v displays version\n"
162 " -d enters debug mode ; -db only disables background mode.\n"
163 " -V enters verbose mode (disables quiet mode)\n"
164 " -D goes daemon ; implies -q\n"
165 " -q quiet mode : don't display messages\n"
166 " -c check mode : only check config file and exit\n"
167 " -n sets the maximum total # of connections (%d)\n"
168 " -m limits the usable amount of memory (in MB)\n"
169 " -N sets the default, per-proxy maximum # of connections (%d)\n"
170 " -p writes pids of all children to this file\n"
171#if defined(ENABLE_EPOLL)
172 " -de disables epoll() usage even when available\n"
173#endif
174#if defined(ENABLE_POLL)
175 " -dp disables poll() usage even when available\n"
176#endif
177 " -sf/-st [pid ]* finishes/terminates old pids. Must be last arguments.\n"
178 "\n",
179 name, DEFAULT_MAXCONN, cfg_maxpconn);
180 exit(1);
181}
182
183
184
185/*********************************************************************/
186/* more specific functions ***************************************/
187/*********************************************************************/
188
189/*
190 * upon SIGUSR1, let's have a soft stop.
191 */
192void sig_soft_stop(int sig)
193{
194 soft_stop();
195 signal(sig, SIG_IGN);
196}
197
198/*
199 * upon SIGTTOU, we pause everything
200 */
201void sig_pause(int sig)
202{
203 pause_proxies();
204 signal(sig, sig_pause);
205}
206
207/*
208 * upon SIGTTIN, let's have a soft stop.
209 */
210void sig_listen(int sig)
211{
212 listen_proxies();
213 signal(sig, sig_listen);
214}
215
216/*
217 * this function dumps every server's state when the process receives SIGHUP.
218 */
219void sig_dump_state(int sig)
220{
221 struct proxy *p = proxy;
222
223 Warning("SIGHUP received, dumping servers states.\n");
224 while (p) {
225 struct server *s = p->srv;
226
227 send_log(p, LOG_NOTICE, "SIGHUP received, dumping servers states for proxy %s.\n", p->id);
228 while (s) {
229 snprintf(trash, sizeof(trash),
230 "SIGHUP: Server %s/%s is %s. Conn: %d act, %d pend, %d tot.",
231 p->id, s->id,
232 (s->state & SRV_RUNNING) ? "UP" : "DOWN",
233 s->cur_sess, s->nbpend, s->cum_sess);
234 Warning("%s\n", trash);
235 send_log(p, LOG_NOTICE, "%s\n", trash);
236 s = s->next;
237 }
238
239 if (p->srv_act == 0) {
240 snprintf(trash, sizeof(trash),
Willy Tarreauf1221aa2006-12-17 22:14:12 +0100241 "SIGHUP: Proxy %s %s ! Conn: act(FE+BE): %d+%d, %d pend (%d unass), tot(FE+BE): %d+%d.",
Willy Tarreaubaaee002006-06-26 02:48:02 +0200242 p->id,
243 (p->srv_bck) ? "is running on backup servers" : "has no server available",
Willy Tarreauf1221aa2006-12-17 22:14:12 +0100244 p->feconn, p->beconn, p->totpend, p->nbpend, p->cum_feconn, p->cum_beconn);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200245 } else {
246 snprintf(trash, sizeof(trash),
247 "SIGHUP: Proxy %s has %d active servers and %d backup servers available."
Willy Tarreauf1221aa2006-12-17 22:14:12 +0100248 " Conn: act(FE+BE): %d+%d, %d pend (%d unass), tot(FE+BE): %d+%d.",
Willy Tarreaubaaee002006-06-26 02:48:02 +0200249 p->id, p->srv_act, p->srv_bck,
Willy Tarreauf1221aa2006-12-17 22:14:12 +0100250 p->feconn, p->beconn, p->totpend, p->nbpend, p->cum_feconn, p->cum_beconn);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200251 }
252 Warning("%s\n", trash);
253 send_log(p, LOG_NOTICE, "%s\n", trash);
254
255 p = p->next;
256 }
257 signal(sig, sig_dump_state);
258}
259
260void dump(int sig)
261{
Willy Tarreau964c9362007-01-07 00:38:00 +0100262 struct task *t;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200263 struct session *s;
Willy Tarreau964c9362007-01-07 00:38:00 +0100264 struct rb_node *node;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200265
Willy Tarreau964c9362007-01-07 00:38:00 +0100266 for(node = rb_first(&wait_queue[0]);
267 node != NULL; node = rb_next(node)) {
268 t = rb_entry(node, struct task, rb_node);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200269 s = t->context;
270 qfprintf(stderr,"[dump] wq: task %p, still %ld ms, "
271 "cli=%d, srv=%d, cr=%d, cw=%d, sr=%d, sw=%d, "
272 "req=%d, rep=%d, clifd=%d\n",
273 s, tv_remain(&now, &t->expire),
274 s->cli_state,
275 s->srv_state,
Willy Tarreau2a429502006-10-15 14:52:29 +0200276 MY_FD_ISSET(s->cli_fd, StaticReadEvent),
277 MY_FD_ISSET(s->cli_fd, StaticWriteEvent),
278 MY_FD_ISSET(s->srv_fd, StaticReadEvent),
279 MY_FD_ISSET(s->srv_fd, StaticWriteEvent),
Willy Tarreaubaaee002006-06-26 02:48:02 +0200280 s->req->l, s->rep?s->rep->l:0, s->cli_fd
281 );
282 }
283}
284
285#ifdef DEBUG_MEMORY
286static void fast_stop(void)
287{
288 struct proxy *p;
289 p = proxy;
290 while (p) {
291 p->grace = 0;
292 p = p->next;
293 }
294 soft_stop();
295}
296
297void sig_int(int sig)
298{
299 /* This would normally be a hard stop,
300 but we want to be sure about deallocation,
301 and so on, so we do a soft stop with
302 0 GRACE time
303 */
304 fast_stop();
305 /* If we are killed twice, we decide to die*/
306 signal(sig, SIG_DFL);
307}
308
309void sig_term(int sig)
310{
311 /* This would normally be a hard stop,
312 but we want to be sure about deallocation,
313 and so on, so we do a soft stop with
314 0 GRACE time
315 */
316 fast_stop();
317 /* If we are killed twice, we decide to die*/
318 signal(sig, SIG_DFL);
319}
320#endif
321
322
323/*
324 * This function initializes all the necessary variables. It only returns
325 * if everything is OK. If something fails, it exits.
326 */
327void init(int argc, char **argv)
328{
329 int i;
330 int arg_mode = 0; /* MODE_DEBUG, ... */
331 char *old_argv = *argv;
332 char *tmp;
333 char *cfg_pidfile = NULL;
334
335 if (1<<INTBITS != sizeof(int)*8) {
336 fprintf(stderr,
337 "Error: wrong architecture. Recompile so that sizeof(int)=%d\n",
338 (int)(sizeof(int)*8));
339 exit(1);
340 }
341
342 /*
343 * Initialize the previously static variables.
344 */
345
346 totalconn = actconn = maxfd = listeners = stopping = 0;
347
348
349#ifdef HAPROXY_MEMMAX
350 global.rlimit_memmax = HAPROXY_MEMMAX;
351#endif
352
353 /* initialize the libc's localtime structures once for all so that we
354 * won't be missing memory if we want to send alerts under OOM conditions.
Willy Tarreau2b35c952006-10-15 15:25:48 +0200355 * Also, the Alert() and Warning() functions need <now> to be initialized.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200356 */
357 tv_now(&now);
Willy Tarreaubf736132006-10-15 22:54:47 +0200358 localtime((time_t *)&now.tv_sec);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200359 start_date = now;
360
361 init_log();
Willy Tarreau80587432006-12-24 17:47:20 +0100362 init_proto_http();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200363
364 cfg_polling_mechanism = POLL_USE_SELECT; /* select() is always available */
365#if defined(ENABLE_POLL)
366 cfg_polling_mechanism |= POLL_USE_POLL;
367#endif
368#if defined(ENABLE_EPOLL)
369 cfg_polling_mechanism |= POLL_USE_EPOLL;
370#endif
371
372 pid = getpid();
373 progname = *argv;
374 while ((tmp = strchr(progname, '/')) != NULL)
375 progname = tmp + 1;
376
377 argc--; argv++;
378 while (argc > 0) {
379 char *flag;
380
381 if (**argv == '-') {
382 flag = *argv+1;
383
384 /* 1 arg */
385 if (*flag == 'v') {
386 display_version();
387 exit(0);
388 }
389#if defined(ENABLE_EPOLL)
390 else if (*flag == 'd' && flag[1] == 'e')
391 cfg_polling_mechanism &= ~POLL_USE_EPOLL;
392#endif
393#if defined(ENABLE_POLL)
394 else if (*flag == 'd' && flag[1] == 'p')
395 cfg_polling_mechanism &= ~POLL_USE_POLL;
396#endif
397 else if (*flag == 'V')
398 arg_mode |= MODE_VERBOSE;
399 else if (*flag == 'd' && flag[1] == 'b')
400 arg_mode |= MODE_FOREGROUND;
401 else if (*flag == 'd')
402 arg_mode |= MODE_DEBUG;
403 else if (*flag == 'c')
404 arg_mode |= MODE_CHECK;
405 else if (*flag == 'D')
406 arg_mode |= MODE_DAEMON | MODE_QUIET;
407 else if (*flag == 'q')
408 arg_mode |= MODE_QUIET;
409 else if (*flag == 's' && (flag[1] == 'f' || flag[1] == 't')) {
410 /* list of pids to finish ('f') or terminate ('t') */
411
412 if (flag[1] == 'f')
413 oldpids_sig = SIGUSR1; /* finish then exit */
414 else
415 oldpids_sig = SIGTERM; /* terminate immediately */
416 argv++; argc--;
417
418 if (argc > 0) {
419 oldpids = calloc(argc, sizeof(int));
420 while (argc > 0) {
421 oldpids[nb_oldpids] = atol(*argv);
422 if (oldpids[nb_oldpids] <= 0)
423 usage(old_argv);
424 argc--; argv++;
425 nb_oldpids++;
426 }
427 }
428 }
429 else { /* >=2 args */
430 argv++; argc--;
431 if (argc == 0)
432 usage(old_argv);
433
434 switch (*flag) {
435 case 'n' : cfg_maxconn = atol(*argv); break;
436 case 'm' : global.rlimit_memmax = atol(*argv); break;
437 case 'N' : cfg_maxpconn = atol(*argv); break;
438 case 'f' : cfg_cfgfile = *argv; break;
439 case 'p' : cfg_pidfile = *argv; break;
440 default: usage(old_argv);
441 }
442 }
443 }
444 else
445 usage(old_argv);
446 argv++; argc--;
447 }
448
449 global.mode = MODE_STARTING | /* during startup, we want most of the alerts */
450 (arg_mode & (MODE_DAEMON | MODE_FOREGROUND | MODE_VERBOSE
451 | MODE_QUIET | MODE_CHECK | MODE_DEBUG));
452
453 if (!cfg_cfgfile)
454 usage(old_argv);
455
456 gethostname(hostname, MAX_HOSTNAME_LEN);
457
458 have_appsession = 0;
459 global.maxsock = 10; /* reserve 10 fds ; will be incremented by socket eaters */
460 if (readcfgfile(cfg_cfgfile) < 0) {
461 Alert("Error reading configuration file : %s\n", cfg_cfgfile);
462 exit(1);
463 }
464 if (have_appsession)
465 appsession_init();
466
467 if (global.mode & MODE_CHECK) {
468 qfprintf(stdout, "Configuration file is valid : %s\n", cfg_cfgfile);
469 exit(0);
470 }
471
472 if (cfg_maxconn > 0)
473 global.maxconn = cfg_maxconn;
474
475 if (cfg_pidfile) {
476 if (global.pidfile)
477 free(global.pidfile);
478 global.pidfile = strdup(cfg_pidfile);
479 }
480
481 if (global.maxconn == 0)
482 global.maxconn = DEFAULT_MAXCONN;
483
484 global.maxsock += global.maxconn * 2; /* each connection needs two sockets */
485
486 if (arg_mode & (MODE_DEBUG | MODE_FOREGROUND)) {
487 /* command line debug mode inhibits configuration mode */
488 global.mode &= ~(MODE_DAEMON | MODE_QUIET);
489 }
490 global.mode |= (arg_mode & (MODE_DAEMON | MODE_FOREGROUND | MODE_QUIET |
491 MODE_VERBOSE | MODE_DEBUG | MODE_STATS | MODE_LOG));
492
493 if ((global.mode & MODE_DEBUG) && (global.mode & (MODE_DAEMON | MODE_QUIET))) {
494 Warning("<debug> mode incompatible with <quiet> and <daemon>. Keeping <debug> only.\n");
495 global.mode &= ~(MODE_DAEMON | MODE_QUIET);
496 }
497
498 if ((global.nbproc > 1) && !(global.mode & MODE_DAEMON)) {
499 if (!(global.mode & (MODE_FOREGROUND | MODE_DEBUG)))
500 Warning("<nbproc> is only meaningful in daemon mode. Setting limit to 1 process.\n");
501 global.nbproc = 1;
502 }
503
504 if (global.nbproc < 1)
505 global.nbproc = 1;
506
507 StaticReadEvent = (fd_set *)calloc(1,
508 sizeof(fd_set) *
509 (global.maxsock + FD_SETSIZE - 1) / FD_SETSIZE);
510 StaticWriteEvent = (fd_set *)calloc(1,
511 sizeof(fd_set) *
512 (global.maxsock + FD_SETSIZE - 1) / FD_SETSIZE);
513
514 fdtab = (struct fdtab *)calloc(1,
515 sizeof(struct fdtab) * (global.maxsock));
516 for (i = 0; i < global.maxsock; i++) {
517 fdtab[i].state = FD_STCLOSE;
518 }
519}
520
521void deinit(void)
522{
523 struct proxy *p = proxy;
524 struct cap_hdr *h,*h_next;
525 struct server *s,*s_next;
526 struct listener *l,*l_next;
527
528 while (p) {
529 if (p->id)
530 free(p->id);
531
532 if (p->check_req)
533 free(p->check_req);
534
535 if (p->cookie_name)
536 free(p->cookie_name);
537
538 if (p->capture_name)
539 free(p->capture_name);
540
541 /* only strup if the user have set in config.
542 When should we free it?!
543 if (p->errmsg.msg400) free(p->errmsg.msg400);
544 if (p->errmsg.msg403) free(p->errmsg.msg403);
545 if (p->errmsg.msg408) free(p->errmsg.msg408);
546 if (p->errmsg.msg500) free(p->errmsg.msg500);
547 if (p->errmsg.msg502) free(p->errmsg.msg502);
548 if (p->errmsg.msg503) free(p->errmsg.msg503);
549 if (p->errmsg.msg504) free(p->errmsg.msg504);
550 */
551 if (p->appsession_name)
552 free(p->appsession_name);
553
554 h = p->req_cap;
555 while (h) {
556 h_next = h->next;
557 if (h->name)
558 free(h->name);
559 pool_destroy(h->pool);
560 free(h);
561 h = h_next;
562 }/* end while(h) */
563
564 h = p->rsp_cap;
565 while (h) {
566 h_next = h->next;
567 if (h->name)
568 free(h->name);
569
570 pool_destroy(h->pool);
571 free(h);
572 h = h_next;
573 }/* end while(h) */
574
575 s = p->srv;
576 while (s) {
577 s_next = s->next;
578 if (s->id)
579 free(s->id);
580
581 if (s->cookie)
582 free(s->cookie);
583
584 free(s);
585 s = s_next;
586 }/* end while(s) */
587
588 l = p->listen;
589 while (l) {
590 l_next = l->next;
591 free(l);
592 l = l_next;
593 }/* end while(l) */
594
595 pool_destroy((void **) p->req_cap_pool);
596 pool_destroy((void **) p->rsp_cap_pool);
597 p = p->next;
598 }/* end while(p) */
599
600 if (global.chroot) free(global.chroot);
601 if (global.pidfile) free(global.pidfile);
602
603 if (StaticReadEvent) free(StaticReadEvent);
604 if (StaticWriteEvent) free(StaticWriteEvent);
605 if (fdtab) free(fdtab);
606
607 pool_destroy(pool_session);
608 pool_destroy(pool_buffer);
609 pool_destroy(pool_requri);
610 pool_destroy(pool_task);
611 pool_destroy(pool_capture);
612 pool_destroy(pool_appsess);
613
614 if (have_appsession) {
615 pool_destroy(apools.serverid);
616 pool_destroy(apools.sessid);
617 }
618} /* end deinit() */
619
620/* sends the signal <sig> to all pids found in <oldpids> */
621static void tell_old_pids(int sig)
622{
623 int p;
624 for (p = 0; p < nb_oldpids; p++)
625 kill(oldpids[p], sig);
626}
627
628int main(int argc, char **argv)
629{
630 int err, retry;
631 struct rlimit limit;
632 FILE *pidfile = NULL;
633 init(argc, argv);
634
635 signal(SIGQUIT, dump);
636 signal(SIGUSR1, sig_soft_stop);
637 signal(SIGHUP, sig_dump_state);
638#ifdef DEBUG_MEMORY
639 signal(SIGINT, sig_int);
640 signal(SIGTERM, sig_term);
641#endif
642
643 /* on very high loads, a sigpipe sometimes happen just between the
644 * getsockopt() which tells "it's OK to write", and the following write :-(
645 */
646#ifndef MSG_NOSIGNAL
647 signal(SIGPIPE, SIG_IGN);
648#endif
649
650 /* We will loop at most 100 times with 10 ms delay each time.
651 * That's at most 1 second. We only send a signal to old pids
652 * if we cannot grab at least one port.
653 */
654 retry = MAX_START_RETRIES;
655 err = ERR_NONE;
656 while (retry >= 0) {
657 struct timeval w;
658 err = start_proxies(retry == 0 || nb_oldpids == 0);
659 if (err != ERR_RETRYABLE)
660 break;
661 if (nb_oldpids == 0)
662 break;
663
664 /* FIXME-20060514: Solaris and OpenBSD do not support shutdown() on
665 * listening sockets. So on those platforms, it would be wiser to
666 * simply send SIGUSR1, which will not be undoable.
667 */
668 tell_old_pids(SIGTTOU);
669 /* give some time to old processes to stop listening */
670 w.tv_sec = 0;
671 w.tv_usec = 10*1000;
672 select(0, NULL, NULL, NULL, &w);
673 retry--;
674 }
675
676 /* Note: start_proxies() sends an alert when it fails. */
677 if (err != ERR_NONE) {
678 if (retry != MAX_START_RETRIES && nb_oldpids)
679 tell_old_pids(SIGTTIN);
680 exit(1);
681 }
682
683 if (listeners == 0) {
684 Alert("[%s.main()] No enabled listener found (check the <listen> keywords) ! Exiting.\n", argv[0]);
685 /* Note: we don't have to send anything to the old pids because we
686 * never stopped them. */
687 exit(1);
688 }
689
690 /* prepare pause/play signals */
691 signal(SIGTTOU, sig_pause);
692 signal(SIGTTIN, sig_listen);
693
694 if (global.mode & MODE_DAEMON) {
695 global.mode &= ~MODE_VERBOSE;
696 global.mode |= MODE_QUIET;
697 }
698
699 /* MODE_QUIET can inhibit alerts and warnings below this line */
700
701 global.mode &= ~MODE_STARTING;
702 if ((global.mode & MODE_QUIET) && !(global.mode & MODE_VERBOSE)) {
703 /* detach from the tty */
704 fclose(stdin); fclose(stdout); fclose(stderr);
705 close(0); close(1); close(2);
706 }
707
708 /* open log & pid files before the chroot */
709 if (global.mode & MODE_DAEMON && global.pidfile != NULL) {
710 int pidfd;
711 unlink(global.pidfile);
712 pidfd = open(global.pidfile, O_CREAT | O_WRONLY | O_TRUNC, 0644);
713 if (pidfd < 0) {
714 Alert("[%s.main()] Cannot create pidfile %s\n", argv[0], global.pidfile);
715 if (nb_oldpids)
716 tell_old_pids(SIGTTIN);
717 exit(1);
718 }
719 pidfile = fdopen(pidfd, "w");
720 }
721
722 /* chroot if needed */
723 if (global.chroot != NULL) {
724 if (chroot(global.chroot) == -1) {
725 Alert("[%s.main()] Cannot chroot(%s).\n", argv[0], global.chroot);
726 if (nb_oldpids)
727 tell_old_pids(SIGTTIN);
728 }
729 chdir("/");
730 }
731
732 /* ulimits */
733 if (!global.rlimit_nofile)
734 global.rlimit_nofile = global.maxsock;
735
736 if (global.rlimit_nofile) {
737 limit.rlim_cur = limit.rlim_max = global.rlimit_nofile;
738 if (setrlimit(RLIMIT_NOFILE, &limit) == -1) {
739 Warning("[%s.main()] Cannot raise FD limit to %d.\n", argv[0], global.rlimit_nofile);
740 }
741 }
742
743 if (global.rlimit_memmax) {
744 limit.rlim_cur = limit.rlim_max =
745 global.rlimit_memmax * 1048576 / global.nbproc;
746#ifdef RLIMIT_AS
747 if (setrlimit(RLIMIT_AS, &limit) == -1) {
748 Warning("[%s.main()] Cannot fix MEM limit to %d megs.\n",
749 argv[0], global.rlimit_memmax);
750 }
751#else
752 if (setrlimit(RLIMIT_DATA, &limit) == -1) {
753 Warning("[%s.main()] Cannot fix MEM limit to %d megs.\n",
754 argv[0], global.rlimit_memmax);
755 }
756#endif
757 }
758
Willy Tarreau6d1a9882007-01-07 02:03:04 +0100759#ifdef CONFIG_HAP_TCPSPLICE
760 if (global.last_checks & LSTCHK_TCPSPLICE) {
761 if (tcp_splice_start() < 0) {
762 Alert("[%s.main()] Cannot enable tcp_splice.\n"
763 " Make sure you have enough permissions and that the module is loadable.\n"
764 " Alternatively, you may disable the 'tcpsplice' options in the configuration.\n"
765 "", argv[0], global.gid);
766 exit(1);
767 }
768 }
769#endif
770
Willy Tarreaubaaee002006-06-26 02:48:02 +0200771 if (nb_oldpids)
772 tell_old_pids(oldpids_sig);
773
774 /* Note that any error at this stage will be fatal because we will not
775 * be able to restart the old pids.
776 */
777
778 /* setgid / setuid */
779 if (global.gid && setgid(global.gid) == -1) {
780 Alert("[%s.main()] Cannot set gid %d.\n", argv[0], global.gid);
781 exit(1);
782 }
783
784 if (global.uid && setuid(global.uid) == -1) {
785 Alert("[%s.main()] Cannot set uid %d.\n", argv[0], global.uid);
786 exit(1);
787 }
788
789 /* check ulimits */
790 limit.rlim_cur = limit.rlim_max = 0;
791 getrlimit(RLIMIT_NOFILE, &limit);
792 if (limit.rlim_cur < global.maxsock) {
793 Warning("[%s.main()] FD limit (%d) too low for maxconn=%d/maxsock=%d. Please raise 'ulimit-n' to %d or more to avoid any trouble.\n",
794 argv[0], limit.rlim_cur, global.maxconn, global.maxsock, global.maxsock);
795 }
796
797 if (global.mode & MODE_DAEMON) {
798 int ret = 0;
799 int proc;
800
801 /* the father launches the required number of processes */
802 for (proc = 0; proc < global.nbproc; proc++) {
803 ret = fork();
804 if (ret < 0) {
805 Alert("[%s.main()] Cannot fork.\n", argv[0]);
806 if (nb_oldpids)
807 exit(1); /* there has been an error */
808 }
809 else if (ret == 0) /* child breaks here */
810 break;
811 if (pidfile != NULL) {
812 fprintf(pidfile, "%d\n", ret);
813 fflush(pidfile);
814 }
815 }
816 /* close the pidfile both in children and father */
817 if (pidfile != NULL)
818 fclose(pidfile);
819 free(global.pidfile);
820
821 if (proc == global.nbproc)
822 exit(0); /* parent must leave */
823
824 /* if we're NOT in QUIET mode, we should now close the 3 first FDs to ensure
825 * that we can detach from the TTY. We MUST NOT do it in other cases since
826 * it would have already be done, and 0-2 would have been affected to listening
827 * sockets
828 */
829 if (!(global.mode & MODE_QUIET)) {
830 /* detach from the tty */
831 fclose(stdin); fclose(stdout); fclose(stderr);
832 close(0); close(1); close(2); /* close all fd's */
833 global.mode |= MODE_QUIET; /* ensure that we won't say anything from now */
834 }
835 pid = getpid(); /* update child's pid */
836 setsid();
837 }
838
839#if defined(ENABLE_EPOLL)
840 if (cfg_polling_mechanism & POLL_USE_EPOLL) {
841 if (epoll_loop(POLL_LOOP_ACTION_INIT)) {
842 epoll_loop(POLL_LOOP_ACTION_RUN);
843 epoll_loop(POLL_LOOP_ACTION_CLEAN);
844 cfg_polling_mechanism &= POLL_USE_EPOLL;
845 }
846 else {
847 Warning("epoll() is not available. Using poll()/select() instead.\n");
848 cfg_polling_mechanism &= ~POLL_USE_EPOLL;
849 }
850 }
851#endif
852
853#if defined(ENABLE_POLL)
854 if (cfg_polling_mechanism & POLL_USE_POLL) {
855 if (poll_loop(POLL_LOOP_ACTION_INIT)) {
856 poll_loop(POLL_LOOP_ACTION_RUN);
857 poll_loop(POLL_LOOP_ACTION_CLEAN);
858 cfg_polling_mechanism &= POLL_USE_POLL;
859 }
860 else {
861 Warning("poll() is not available. Using select() instead.\n");
862 cfg_polling_mechanism &= ~POLL_USE_POLL;
863 }
864 }
865#endif
866 if (cfg_polling_mechanism & POLL_USE_SELECT) {
867 if (select_loop(POLL_LOOP_ACTION_INIT)) {
868 select_loop(POLL_LOOP_ACTION_RUN);
869 select_loop(POLL_LOOP_ACTION_CLEAN);
870 cfg_polling_mechanism &= POLL_USE_SELECT;
871 }
872 }
873
874
875 /* Free all Hash Keys and all Hash elements */
876 appsession_cleanup();
877 /* Do some cleanup */
878 deinit();
879
880 exit(0);
881}
882
883
884/*
885 * Local variables:
886 * c-indent-level: 8
887 * c-basic-offset: 8
888 * End:
889 */