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