blob: a4afca39fd8eef401ca5041059a408bf9331b252 [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
365 init_log();
Willy Tarreau80587432006-12-24 17:47:20 +0100366 init_proto_http();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200367
368 cfg_polling_mechanism = POLL_USE_SELECT; /* select() is always available */
369#if defined(ENABLE_POLL)
370 cfg_polling_mechanism |= POLL_USE_POLL;
371#endif
372#if defined(ENABLE_EPOLL)
373 cfg_polling_mechanism |= POLL_USE_EPOLL;
374#endif
375
376 pid = getpid();
377 progname = *argv;
378 while ((tmp = strchr(progname, '/')) != NULL)
379 progname = tmp + 1;
380
381 argc--; argv++;
382 while (argc > 0) {
383 char *flag;
384
385 if (**argv == '-') {
386 flag = *argv+1;
387
388 /* 1 arg */
389 if (*flag == 'v') {
390 display_version();
391 exit(0);
392 }
393#if defined(ENABLE_EPOLL)
394 else if (*flag == 'd' && flag[1] == 'e')
395 cfg_polling_mechanism &= ~POLL_USE_EPOLL;
396#endif
397#if defined(ENABLE_POLL)
398 else if (*flag == 'd' && flag[1] == 'p')
399 cfg_polling_mechanism &= ~POLL_USE_POLL;
400#endif
401 else if (*flag == 'V')
402 arg_mode |= MODE_VERBOSE;
403 else if (*flag == 'd' && flag[1] == 'b')
404 arg_mode |= MODE_FOREGROUND;
405 else if (*flag == 'd')
406 arg_mode |= MODE_DEBUG;
407 else if (*flag == 'c')
408 arg_mode |= MODE_CHECK;
409 else if (*flag == 'D')
410 arg_mode |= MODE_DAEMON | MODE_QUIET;
411 else if (*flag == 'q')
412 arg_mode |= MODE_QUIET;
413 else if (*flag == 's' && (flag[1] == 'f' || flag[1] == 't')) {
414 /* list of pids to finish ('f') or terminate ('t') */
415
416 if (flag[1] == 'f')
417 oldpids_sig = SIGUSR1; /* finish then exit */
418 else
419 oldpids_sig = SIGTERM; /* terminate immediately */
420 argv++; argc--;
421
422 if (argc > 0) {
423 oldpids = calloc(argc, sizeof(int));
424 while (argc > 0) {
425 oldpids[nb_oldpids] = atol(*argv);
426 if (oldpids[nb_oldpids] <= 0)
427 usage(old_argv);
428 argc--; argv++;
429 nb_oldpids++;
430 }
431 }
432 }
433 else { /* >=2 args */
434 argv++; argc--;
435 if (argc == 0)
436 usage(old_argv);
437
438 switch (*flag) {
439 case 'n' : cfg_maxconn = atol(*argv); break;
440 case 'm' : global.rlimit_memmax = atol(*argv); break;
441 case 'N' : cfg_maxpconn = atol(*argv); break;
442 case 'f' : cfg_cfgfile = *argv; break;
443 case 'p' : cfg_pidfile = *argv; break;
444 default: usage(old_argv);
445 }
446 }
447 }
448 else
449 usage(old_argv);
450 argv++; argc--;
451 }
452
453 global.mode = MODE_STARTING | /* during startup, we want most of the alerts */
454 (arg_mode & (MODE_DAEMON | MODE_FOREGROUND | MODE_VERBOSE
455 | MODE_QUIET | MODE_CHECK | MODE_DEBUG));
456
457 if (!cfg_cfgfile)
458 usage(old_argv);
459
460 gethostname(hostname, MAX_HOSTNAME_LEN);
461
462 have_appsession = 0;
463 global.maxsock = 10; /* reserve 10 fds ; will be incremented by socket eaters */
464 if (readcfgfile(cfg_cfgfile) < 0) {
465 Alert("Error reading configuration file : %s\n", cfg_cfgfile);
466 exit(1);
467 }
468 if (have_appsession)
469 appsession_init();
470
471 if (global.mode & MODE_CHECK) {
472 qfprintf(stdout, "Configuration file is valid : %s\n", cfg_cfgfile);
473 exit(0);
474 }
475
476 if (cfg_maxconn > 0)
477 global.maxconn = cfg_maxconn;
478
479 if (cfg_pidfile) {
480 if (global.pidfile)
481 free(global.pidfile);
482 global.pidfile = strdup(cfg_pidfile);
483 }
484
485 if (global.maxconn == 0)
486 global.maxconn = DEFAULT_MAXCONN;
487
488 global.maxsock += global.maxconn * 2; /* each connection needs two sockets */
489
490 if (arg_mode & (MODE_DEBUG | MODE_FOREGROUND)) {
491 /* command line debug mode inhibits configuration mode */
492 global.mode &= ~(MODE_DAEMON | MODE_QUIET);
493 }
494 global.mode |= (arg_mode & (MODE_DAEMON | MODE_FOREGROUND | MODE_QUIET |
495 MODE_VERBOSE | MODE_DEBUG | MODE_STATS | MODE_LOG));
496
497 if ((global.mode & MODE_DEBUG) && (global.mode & (MODE_DAEMON | MODE_QUIET))) {
498 Warning("<debug> mode incompatible with <quiet> and <daemon>. Keeping <debug> only.\n");
499 global.mode &= ~(MODE_DAEMON | MODE_QUIET);
500 }
501
502 if ((global.nbproc > 1) && !(global.mode & MODE_DAEMON)) {
503 if (!(global.mode & (MODE_FOREGROUND | MODE_DEBUG)))
504 Warning("<nbproc> is only meaningful in daemon mode. Setting limit to 1 process.\n");
505 global.nbproc = 1;
506 }
507
508 if (global.nbproc < 1)
509 global.nbproc = 1;
510
511 StaticReadEvent = (fd_set *)calloc(1,
512 sizeof(fd_set) *
513 (global.maxsock + FD_SETSIZE - 1) / FD_SETSIZE);
514 StaticWriteEvent = (fd_set *)calloc(1,
515 sizeof(fd_set) *
516 (global.maxsock + FD_SETSIZE - 1) / FD_SETSIZE);
517
518 fdtab = (struct fdtab *)calloc(1,
519 sizeof(struct fdtab) * (global.maxsock));
520 for (i = 0; i < global.maxsock; i++) {
521 fdtab[i].state = FD_STCLOSE;
522 }
523}
524
525void deinit(void)
526{
527 struct proxy *p = proxy;
528 struct cap_hdr *h,*h_next;
529 struct server *s,*s_next;
530 struct listener *l,*l_next;
531
532 while (p) {
533 if (p->id)
534 free(p->id);
535
536 if (p->check_req)
537 free(p->check_req);
538
539 if (p->cookie_name)
540 free(p->cookie_name);
541
542 if (p->capture_name)
543 free(p->capture_name);
544
545 /* only strup if the user have set in config.
546 When should we free it?!
547 if (p->errmsg.msg400) free(p->errmsg.msg400);
548 if (p->errmsg.msg403) free(p->errmsg.msg403);
549 if (p->errmsg.msg408) free(p->errmsg.msg408);
550 if (p->errmsg.msg500) free(p->errmsg.msg500);
551 if (p->errmsg.msg502) free(p->errmsg.msg502);
552 if (p->errmsg.msg503) free(p->errmsg.msg503);
553 if (p->errmsg.msg504) free(p->errmsg.msg504);
554 */
555 if (p->appsession_name)
556 free(p->appsession_name);
557
558 h = p->req_cap;
559 while (h) {
560 h_next = h->next;
561 if (h->name)
562 free(h->name);
563 pool_destroy(h->pool);
564 free(h);
565 h = h_next;
566 }/* end while(h) */
567
568 h = p->rsp_cap;
569 while (h) {
570 h_next = h->next;
571 if (h->name)
572 free(h->name);
573
574 pool_destroy(h->pool);
575 free(h);
576 h = h_next;
577 }/* end while(h) */
578
579 s = p->srv;
580 while (s) {
581 s_next = s->next;
582 if (s->id)
583 free(s->id);
584
585 if (s->cookie)
586 free(s->cookie);
587
588 free(s);
589 s = s_next;
590 }/* end while(s) */
591
592 l = p->listen;
593 while (l) {
594 l_next = l->next;
595 free(l);
596 l = l_next;
597 }/* end while(l) */
598
599 pool_destroy((void **) p->req_cap_pool);
600 pool_destroy((void **) p->rsp_cap_pool);
601 p = p->next;
602 }/* end while(p) */
603
604 if (global.chroot) free(global.chroot);
605 if (global.pidfile) free(global.pidfile);
606
607 if (StaticReadEvent) free(StaticReadEvent);
608 if (StaticWriteEvent) free(StaticWriteEvent);
609 if (fdtab) free(fdtab);
610
611 pool_destroy(pool_session);
612 pool_destroy(pool_buffer);
613 pool_destroy(pool_requri);
614 pool_destroy(pool_task);
615 pool_destroy(pool_capture);
616 pool_destroy(pool_appsess);
617
618 if (have_appsession) {
619 pool_destroy(apools.serverid);
620 pool_destroy(apools.sessid);
621 }
622} /* end deinit() */
623
624/* sends the signal <sig> to all pids found in <oldpids> */
625static void tell_old_pids(int sig)
626{
627 int p;
628 for (p = 0; p < nb_oldpids; p++)
629 kill(oldpids[p], sig);
630}
631
632int main(int argc, char **argv)
633{
634 int err, retry;
635 struct rlimit limit;
636 FILE *pidfile = NULL;
637 init(argc, argv);
638
639 signal(SIGQUIT, dump);
640 signal(SIGUSR1, sig_soft_stop);
641 signal(SIGHUP, sig_dump_state);
642#ifdef DEBUG_MEMORY
643 signal(SIGINT, sig_int);
644 signal(SIGTERM, sig_term);
645#endif
646
647 /* on very high loads, a sigpipe sometimes happen just between the
648 * getsockopt() which tells "it's OK to write", and the following write :-(
649 */
650#ifndef MSG_NOSIGNAL
651 signal(SIGPIPE, SIG_IGN);
652#endif
653
654 /* We will loop at most 100 times with 10 ms delay each time.
655 * That's at most 1 second. We only send a signal to old pids
656 * if we cannot grab at least one port.
657 */
658 retry = MAX_START_RETRIES;
659 err = ERR_NONE;
660 while (retry >= 0) {
661 struct timeval w;
662 err = start_proxies(retry == 0 || nb_oldpids == 0);
663 if (err != ERR_RETRYABLE)
664 break;
665 if (nb_oldpids == 0)
666 break;
667
668 /* FIXME-20060514: Solaris and OpenBSD do not support shutdown() on
669 * listening sockets. So on those platforms, it would be wiser to
670 * simply send SIGUSR1, which will not be undoable.
671 */
672 tell_old_pids(SIGTTOU);
673 /* give some time to old processes to stop listening */
674 w.tv_sec = 0;
675 w.tv_usec = 10*1000;
676 select(0, NULL, NULL, NULL, &w);
677 retry--;
678 }
679
680 /* Note: start_proxies() sends an alert when it fails. */
681 if (err != ERR_NONE) {
682 if (retry != MAX_START_RETRIES && nb_oldpids)
683 tell_old_pids(SIGTTIN);
684 exit(1);
685 }
686
687 if (listeners == 0) {
688 Alert("[%s.main()] No enabled listener found (check the <listen> keywords) ! Exiting.\n", argv[0]);
689 /* Note: we don't have to send anything to the old pids because we
690 * never stopped them. */
691 exit(1);
692 }
693
694 /* prepare pause/play signals */
695 signal(SIGTTOU, sig_pause);
696 signal(SIGTTIN, sig_listen);
697
698 if (global.mode & MODE_DAEMON) {
699 global.mode &= ~MODE_VERBOSE;
700 global.mode |= MODE_QUIET;
701 }
702
703 /* MODE_QUIET can inhibit alerts and warnings below this line */
704
705 global.mode &= ~MODE_STARTING;
706 if ((global.mode & MODE_QUIET) && !(global.mode & MODE_VERBOSE)) {
707 /* detach from the tty */
708 fclose(stdin); fclose(stdout); fclose(stderr);
709 close(0); close(1); close(2);
710 }
711
712 /* open log & pid files before the chroot */
713 if (global.mode & MODE_DAEMON && global.pidfile != NULL) {
714 int pidfd;
715 unlink(global.pidfile);
716 pidfd = open(global.pidfile, O_CREAT | O_WRONLY | O_TRUNC, 0644);
717 if (pidfd < 0) {
718 Alert("[%s.main()] Cannot create pidfile %s\n", argv[0], global.pidfile);
719 if (nb_oldpids)
720 tell_old_pids(SIGTTIN);
721 exit(1);
722 }
723 pidfile = fdopen(pidfd, "w");
724 }
725
726 /* chroot if needed */
727 if (global.chroot != NULL) {
728 if (chroot(global.chroot) == -1) {
729 Alert("[%s.main()] Cannot chroot(%s).\n", argv[0], global.chroot);
730 if (nb_oldpids)
731 tell_old_pids(SIGTTIN);
732 }
733 chdir("/");
734 }
735
736 /* ulimits */
737 if (!global.rlimit_nofile)
738 global.rlimit_nofile = global.maxsock;
739
740 if (global.rlimit_nofile) {
741 limit.rlim_cur = limit.rlim_max = global.rlimit_nofile;
742 if (setrlimit(RLIMIT_NOFILE, &limit) == -1) {
743 Warning("[%s.main()] Cannot raise FD limit to %d.\n", argv[0], global.rlimit_nofile);
744 }
745 }
746
747 if (global.rlimit_memmax) {
748 limit.rlim_cur = limit.rlim_max =
749 global.rlimit_memmax * 1048576 / global.nbproc;
750#ifdef RLIMIT_AS
751 if (setrlimit(RLIMIT_AS, &limit) == -1) {
752 Warning("[%s.main()] Cannot fix MEM limit to %d megs.\n",
753 argv[0], global.rlimit_memmax);
754 }
755#else
756 if (setrlimit(RLIMIT_DATA, &limit) == -1) {
757 Warning("[%s.main()] Cannot fix MEM limit to %d megs.\n",
758 argv[0], global.rlimit_memmax);
759 }
760#endif
761 }
762
Willy Tarreau6d1a9882007-01-07 02:03:04 +0100763#ifdef CONFIG_HAP_TCPSPLICE
764 if (global.last_checks & LSTCHK_TCPSPLICE) {
765 if (tcp_splice_start() < 0) {
766 Alert("[%s.main()] Cannot enable tcp_splice.\n"
767 " Make sure you have enough permissions and that the module is loadable.\n"
768 " Alternatively, you may disable the 'tcpsplice' options in the configuration.\n"
769 "", argv[0], global.gid);
770 exit(1);
771 }
772 }
773#endif
774
Willy Tarreaub38651a2007-03-24 17:24:39 +0100775#ifdef CONFIG_HAP_CTTPROXY
776 if (global.last_checks & LSTCHK_CTTPROXY) {
777 int ret;
778
779 ret = check_cttproxy_version();
780 if (ret < 0) {
781 Alert("[%s.main()] Cannot enable cttproxy.\n%s",
782 argv[0],
783 (ret == -1) ? " Incorrect module version.\n"
784 : " Make sure you have enough permissions and that the module is loaded.\n");
785 exit(1);
786 }
787 }
788#endif
789
790 if ((global.last_checks & LSTCHK_NETADM) && global.uid) {
791 Alert("[%s.main()] Some configuration options require full privileges, so global.uid cannot be changed.\n"
792 "", argv[0], global.gid);
793 exit(1);
794 }
795
Willy Tarreaubaaee002006-06-26 02:48:02 +0200796 if (nb_oldpids)
797 tell_old_pids(oldpids_sig);
798
799 /* Note that any error at this stage will be fatal because we will not
800 * be able to restart the old pids.
801 */
802
803 /* setgid / setuid */
804 if (global.gid && setgid(global.gid) == -1) {
805 Alert("[%s.main()] Cannot set gid %d.\n", argv[0], global.gid);
806 exit(1);
807 }
808
809 if (global.uid && setuid(global.uid) == -1) {
810 Alert("[%s.main()] Cannot set uid %d.\n", argv[0], global.uid);
811 exit(1);
812 }
813
814 /* check ulimits */
815 limit.rlim_cur = limit.rlim_max = 0;
816 getrlimit(RLIMIT_NOFILE, &limit);
817 if (limit.rlim_cur < global.maxsock) {
818 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",
819 argv[0], limit.rlim_cur, global.maxconn, global.maxsock, global.maxsock);
820 }
821
822 if (global.mode & MODE_DAEMON) {
823 int ret = 0;
824 int proc;
825
826 /* the father launches the required number of processes */
827 for (proc = 0; proc < global.nbproc; proc++) {
828 ret = fork();
829 if (ret < 0) {
830 Alert("[%s.main()] Cannot fork.\n", argv[0]);
831 if (nb_oldpids)
832 exit(1); /* there has been an error */
833 }
834 else if (ret == 0) /* child breaks here */
835 break;
836 if (pidfile != NULL) {
837 fprintf(pidfile, "%d\n", ret);
838 fflush(pidfile);
839 }
840 }
841 /* close the pidfile both in children and father */
842 if (pidfile != NULL)
843 fclose(pidfile);
844 free(global.pidfile);
845
846 if (proc == global.nbproc)
847 exit(0); /* parent must leave */
848
849 /* if we're NOT in QUIET mode, we should now close the 3 first FDs to ensure
850 * that we can detach from the TTY. We MUST NOT do it in other cases since
851 * it would have already be done, and 0-2 would have been affected to listening
852 * sockets
853 */
854 if (!(global.mode & MODE_QUIET)) {
855 /* detach from the tty */
856 fclose(stdin); fclose(stdout); fclose(stderr);
857 close(0); close(1); close(2); /* close all fd's */
858 global.mode |= MODE_QUIET; /* ensure that we won't say anything from now */
859 }
860 pid = getpid(); /* update child's pid */
861 setsid();
862 }
863
864#if defined(ENABLE_EPOLL)
865 if (cfg_polling_mechanism & POLL_USE_EPOLL) {
866 if (epoll_loop(POLL_LOOP_ACTION_INIT)) {
867 epoll_loop(POLL_LOOP_ACTION_RUN);
868 epoll_loop(POLL_LOOP_ACTION_CLEAN);
869 cfg_polling_mechanism &= POLL_USE_EPOLL;
870 }
871 else {
872 Warning("epoll() is not available. Using poll()/select() instead.\n");
873 cfg_polling_mechanism &= ~POLL_USE_EPOLL;
874 }
875 }
876#endif
877
878#if defined(ENABLE_POLL)
879 if (cfg_polling_mechanism & POLL_USE_POLL) {
880 if (poll_loop(POLL_LOOP_ACTION_INIT)) {
881 poll_loop(POLL_LOOP_ACTION_RUN);
882 poll_loop(POLL_LOOP_ACTION_CLEAN);
883 cfg_polling_mechanism &= POLL_USE_POLL;
884 }
885 else {
886 Warning("poll() is not available. Using select() instead.\n");
887 cfg_polling_mechanism &= ~POLL_USE_POLL;
888 }
889 }
890#endif
891 if (cfg_polling_mechanism & POLL_USE_SELECT) {
892 if (select_loop(POLL_LOOP_ACTION_INIT)) {
893 select_loop(POLL_LOOP_ACTION_RUN);
894 select_loop(POLL_LOOP_ACTION_CLEAN);
895 cfg_polling_mechanism &= POLL_USE_SELECT;
896 }
897 }
898
899
900 /* Free all Hash Keys and all Hash elements */
901 appsession_cleanup();
902 /* Do some cleanup */
903 deinit();
904
905 exit(0);
906}
907
908
909/*
910 * Local variables:
911 * c-indent-level: 8
912 * c-basic-offset: 8
913 * End:
914 */