blob: fe836308ede4fc67a2a126c76068549769b0bd53 [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>
Willy Tarreau69801b82007-04-09 15:28:51 +020078#include <types/polling.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020079#include <types/proto_http.h>
80
Willy Tarreau0fc45a72007-06-17 00:36:03 +020081#include <proto/acl.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020082#include <proto/backend.h>
83#include <proto/buffers.h>
84#include <proto/client.h>
85#include <proto/fd.h>
86#include <proto/log.h>
Willy Tarreau80587432006-12-24 17:47:20 +010087#include <proto/proto_http.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020088#include <proto/proxy.h>
89#include <proto/queue.h>
90#include <proto/server.h>
Willy Tarreauc6ca1a02007-05-13 19:43:47 +020091#include <proto/session.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020092#include <proto/stream_sock.h>
93#include <proto/task.h>
94
Willy Tarreau6d1a9882007-01-07 02:03:04 +010095#ifdef CONFIG_HAP_TCPSPLICE
96#include <libtcpsplice.h>
97#endif
98
Willy Tarreaub38651a2007-03-24 17:24:39 +010099#ifdef CONFIG_HAP_CTTPROXY
100#include <proto/cttproxy.h>
101#endif
102
Willy Tarreaubaaee002006-06-26 02:48:02 +0200103/*********************************************************************/
104
105/*********************************************************************/
106
107char *cfg_cfgfile = NULL; /* configuration file */
108char *progname = NULL; /* program name */
109int pid; /* current process id */
110
111/* global options */
112struct global global = {
113 logfac1 : -1,
114 logfac2 : -1,
115 loglev1 : 7, /* max syslog level : debug */
116 loglev2 : 7,
117 /* others NULL OK */
118};
119
120/*********************************************************************/
121
122int stopping; /* non zero means stopping in progress */
123
124/* Here we store informations about the pids of the processes we may pause
125 * or kill. We will send them a signal every 10 ms until we can bind to all
126 * our ports. With 200 retries, that's about 2 seconds.
127 */
128#define MAX_START_RETRIES 200
129static int nb_oldpids = 0;
130static int *oldpids = NULL;
131static int oldpids_sig; /* use USR1 or TERM */
132
133/* this is used to drain data, and as a temporary buffer for sprintf()... */
134char trash[BUFSIZE];
135
136const int zero = 0;
137const int one = 1;
138
139/*
140 * Syslog facilities and levels. Conforming to RFC3164.
141 */
142
143#define MAX_HOSTNAME_LEN 32
144static char hostname[MAX_HOSTNAME_LEN] = "";
145
146
147/*********************************************************************/
148/* general purpose functions ***************************************/
149/*********************************************************************/
150
151void display_version()
152{
153 printf("HA-Proxy version " HAPROXY_VERSION " " HAPROXY_DATE"\n");
Willy Tarreau49e1ee82007-01-22 00:56:46 +0100154 printf("Copyright 2000-2007 Willy Tarreau <w@1wt.eu>\n\n");
Willy Tarreaubaaee002006-06-26 02:48:02 +0200155}
156
157/*
158 * This function prints the command line usage and exits
159 */
160void usage(char *name)
161{
162 display_version();
163 fprintf(stderr,
164 "Usage : %s -f <cfgfile> [ -vdV"
165 "D ] [ -n <maxconn> ] [ -N <maxpconn> ]\n"
166 " [ -p <pidfile> ] [ -m <max megs> ]\n"
167 " -v displays version\n"
168 " -d enters debug mode ; -db only disables background mode.\n"
169 " -V enters verbose mode (disables quiet mode)\n"
170 " -D goes daemon ; implies -q\n"
171 " -q quiet mode : don't display messages\n"
172 " -c check mode : only check config file and exit\n"
173 " -n sets the maximum total # of connections (%d)\n"
174 " -m limits the usable amount of memory (in MB)\n"
175 " -N sets the default, per-proxy maximum # of connections (%d)\n"
176 " -p writes pids of all children to this file\n"
177#if defined(ENABLE_EPOLL)
178 " -de disables epoll() usage even when available\n"
179#endif
Willy Tarreaude99e992007-04-16 00:53:59 +0200180#if defined(ENABLE_SEPOLL)
181 " -ds disables speculative epoll() usage even when available\n"
182#endif
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200183#if defined(ENABLE_KQUEUE)
184 " -dk disables kqueue() usage even when available\n"
185#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200186#if defined(ENABLE_POLL)
187 " -dp disables poll() usage even when available\n"
188#endif
189 " -sf/-st [pid ]* finishes/terminates old pids. Must be last arguments.\n"
190 "\n",
191 name, DEFAULT_MAXCONN, cfg_maxpconn);
192 exit(1);
193}
194
195
196
197/*********************************************************************/
198/* more specific functions ***************************************/
199/*********************************************************************/
200
201/*
202 * upon SIGUSR1, let's have a soft stop.
203 */
204void sig_soft_stop(int sig)
205{
206 soft_stop();
Willy Tarreau4d2d0982007-05-14 00:39:29 +0200207 pool_gc2();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200208 signal(sig, SIG_IGN);
209}
210
211/*
212 * upon SIGTTOU, we pause everything
213 */
214void sig_pause(int sig)
215{
216 pause_proxies();
Willy Tarreau4d2d0982007-05-14 00:39:29 +0200217 pool_gc2();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200218 signal(sig, sig_pause);
219}
220
221/*
222 * upon SIGTTIN, let's have a soft stop.
223 */
224void sig_listen(int sig)
225{
226 listen_proxies();
227 signal(sig, sig_listen);
228}
229
230/*
231 * this function dumps every server's state when the process receives SIGHUP.
232 */
233void sig_dump_state(int sig)
234{
235 struct proxy *p = proxy;
236
237 Warning("SIGHUP received, dumping servers states.\n");
238 while (p) {
239 struct server *s = p->srv;
240
241 send_log(p, LOG_NOTICE, "SIGHUP received, dumping servers states for proxy %s.\n", p->id);
242 while (s) {
243 snprintf(trash, sizeof(trash),
244 "SIGHUP: Server %s/%s is %s. Conn: %d act, %d pend, %d tot.",
245 p->id, s->id,
246 (s->state & SRV_RUNNING) ? "UP" : "DOWN",
247 s->cur_sess, s->nbpend, s->cum_sess);
248 Warning("%s\n", trash);
249 send_log(p, LOG_NOTICE, "%s\n", trash);
250 s = s->next;
251 }
252
Willy Tarreau5fcc8f12007-09-17 11:27:09 +0200253 /* FIXME: those info are a bit outdated. We should be able to distinguish between FE and BE. */
254 if (!p->srv) {
255 snprintf(trash, sizeof(trash),
256 "SIGHUP: Proxy %s has no servers. Conn: act(FE+BE): %d+%d, %d pend (%d unass), tot(FE+BE): %d+%d.",
257 p->id,
258 p->feconn, p->beconn, p->totpend, p->nbpend, p->cum_feconn, p->cum_beconn);
259 } else if (p->srv_act == 0) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200260 snprintf(trash, sizeof(trash),
Willy Tarreauf1221aa2006-12-17 22:14:12 +0100261 "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 +0200262 p->id,
263 (p->srv_bck) ? "is running on backup servers" : "has no server available",
Willy Tarreauf1221aa2006-12-17 22:14:12 +0100264 p->feconn, p->beconn, p->totpend, p->nbpend, p->cum_feconn, p->cum_beconn);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200265 } else {
266 snprintf(trash, sizeof(trash),
267 "SIGHUP: Proxy %s has %d active servers and %d backup servers available."
Willy Tarreauf1221aa2006-12-17 22:14:12 +0100268 " Conn: act(FE+BE): %d+%d, %d pend (%d unass), tot(FE+BE): %d+%d.",
Willy Tarreaubaaee002006-06-26 02:48:02 +0200269 p->id, p->srv_act, p->srv_bck,
Willy Tarreauf1221aa2006-12-17 22:14:12 +0100270 p->feconn, p->beconn, p->totpend, p->nbpend, p->cum_feconn, p->cum_beconn);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200271 }
272 Warning("%s\n", trash);
273 send_log(p, LOG_NOTICE, "%s\n", trash);
274
275 p = p->next;
276 }
277 signal(sig, sig_dump_state);
278}
279
280void dump(int sig)
281{
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200282#if 0
Willy Tarreau964c9362007-01-07 00:38:00 +0100283 struct task *t;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200284 struct session *s;
Willy Tarreau964c9362007-01-07 00:38:00 +0100285 struct rb_node *node;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200286
Willy Tarreau964c9362007-01-07 00:38:00 +0100287 for(node = rb_first(&wait_queue[0]);
288 node != NULL; node = rb_next(node)) {
289 t = rb_entry(node, struct task, rb_node);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200290 s = t->context;
291 qfprintf(stderr,"[dump] wq: task %p, still %ld ms, "
292 "cli=%d, srv=%d, cr=%d, cw=%d, sr=%d, sw=%d, "
293 "req=%d, rep=%d, clifd=%d\n",
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200294 s, tv_ms_remain(&now, &t->expire),
Willy Tarreaubaaee002006-06-26 02:48:02 +0200295 s->cli_state,
296 s->srv_state,
Willy Tarreauf161a342007-04-08 16:59:42 +0200297 EV_FD_ISSET(s->cli_fd, DIR_RD),
298 EV_FD_ISSET(s->cli_fd, DIR_WR),
299 EV_FD_ISSET(s->srv_fd, DIR_RD),
300 EV_FD_ISSET(s->srv_fd, DIR_WR),
Willy Tarreaubaaee002006-06-26 02:48:02 +0200301 s->req->l, s->rep?s->rep->l:0, s->cli_fd
302 );
303 }
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200304#endif
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200305 /* dump memory usage then free everything possible */
306 dump_pools();
307 pool_gc2();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200308}
309
310#ifdef DEBUG_MEMORY
311static void fast_stop(void)
312{
313 struct proxy *p;
314 p = proxy;
315 while (p) {
316 p->grace = 0;
317 p = p->next;
318 }
319 soft_stop();
320}
321
322void sig_int(int sig)
323{
324 /* This would normally be a hard stop,
325 but we want to be sure about deallocation,
326 and so on, so we do a soft stop with
327 0 GRACE time
328 */
329 fast_stop();
Willy Tarreau4d2d0982007-05-14 00:39:29 +0200330 pool_gc2();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200331 /* If we are killed twice, we decide to die*/
332 signal(sig, SIG_DFL);
333}
334
335void sig_term(int sig)
336{
337 /* This would normally be a hard stop,
338 but we want to be sure about deallocation,
339 and so on, so we do a soft stop with
340 0 GRACE time
341 */
342 fast_stop();
Willy Tarreau4d2d0982007-05-14 00:39:29 +0200343 pool_gc2();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200344 /* If we are killed twice, we decide to die*/
345 signal(sig, SIG_DFL);
346}
347#endif
348
349
350/*
351 * This function initializes all the necessary variables. It only returns
352 * if everything is OK. If something fails, it exits.
353 */
354void init(int argc, char **argv)
355{
356 int i;
357 int arg_mode = 0; /* MODE_DEBUG, ... */
358 char *old_argv = *argv;
359 char *tmp;
360 char *cfg_pidfile = NULL;
361
362 if (1<<INTBITS != sizeof(int)*8) {
363 fprintf(stderr,
364 "Error: wrong architecture. Recompile so that sizeof(int)=%d\n",
365 (int)(sizeof(int)*8));
366 exit(1);
367 }
368
369 /*
370 * Initialize the previously static variables.
371 */
372
373 totalconn = actconn = maxfd = listeners = stopping = 0;
374
375
376#ifdef HAPROXY_MEMMAX
377 global.rlimit_memmax = HAPROXY_MEMMAX;
378#endif
379
380 /* initialize the libc's localtime structures once for all so that we
381 * won't be missing memory if we want to send alerts under OOM conditions.
Willy Tarreau2b35c952006-10-15 15:25:48 +0200382 * Also, the Alert() and Warning() functions need <now> to be initialized.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200383 */
384 tv_now(&now);
Willy Tarreaubf736132006-10-15 22:54:47 +0200385 localtime((time_t *)&now.tv_sec);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200386 start_date = now;
387
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200388 init_task();
389 init_session();
Willy Tarreaue4d7e552007-05-13 20:19:55 +0200390 init_buffer();
391 init_pendconn();
Willy Tarreau80587432006-12-24 17:47:20 +0100392 init_proto_http();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200393
394 cfg_polling_mechanism = POLL_USE_SELECT; /* select() is always available */
395#if defined(ENABLE_POLL)
396 cfg_polling_mechanism |= POLL_USE_POLL;
397#endif
398#if defined(ENABLE_EPOLL)
399 cfg_polling_mechanism |= POLL_USE_EPOLL;
400#endif
Willy Tarreaude99e992007-04-16 00:53:59 +0200401#if defined(ENABLE_SEPOLL)
402 cfg_polling_mechanism |= POLL_USE_SEPOLL;
403#endif
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200404#if defined(ENABLE_KQUEUE)
405 cfg_polling_mechanism |= POLL_USE_KQUEUE;
406#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200407
408 pid = getpid();
409 progname = *argv;
410 while ((tmp = strchr(progname, '/')) != NULL)
411 progname = tmp + 1;
412
413 argc--; argv++;
414 while (argc > 0) {
415 char *flag;
416
417 if (**argv == '-') {
418 flag = *argv+1;
419
420 /* 1 arg */
421 if (*flag == 'v') {
422 display_version();
423 exit(0);
424 }
425#if defined(ENABLE_EPOLL)
426 else if (*flag == 'd' && flag[1] == 'e')
427 cfg_polling_mechanism &= ~POLL_USE_EPOLL;
428#endif
Willy Tarreaude99e992007-04-16 00:53:59 +0200429#if defined(ENABLE_SEPOLL)
430 else if (*flag == 'd' && flag[1] == 's')
431 cfg_polling_mechanism &= ~POLL_USE_SEPOLL;
432#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200433#if defined(ENABLE_POLL)
434 else if (*flag == 'd' && flag[1] == 'p')
435 cfg_polling_mechanism &= ~POLL_USE_POLL;
436#endif
Willy Tarreau69cad1a2007-04-10 22:45:11 +0200437#if defined(ENABLE_KQUEUE)
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200438 else if (*flag == 'd' && flag[1] == 'k')
439 cfg_polling_mechanism &= ~POLL_USE_KQUEUE;
440#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200441 else if (*flag == 'V')
442 arg_mode |= MODE_VERBOSE;
443 else if (*flag == 'd' && flag[1] == 'b')
444 arg_mode |= MODE_FOREGROUND;
445 else if (*flag == 'd')
446 arg_mode |= MODE_DEBUG;
447 else if (*flag == 'c')
448 arg_mode |= MODE_CHECK;
449 else if (*flag == 'D')
450 arg_mode |= MODE_DAEMON | MODE_QUIET;
451 else if (*flag == 'q')
452 arg_mode |= MODE_QUIET;
453 else if (*flag == 's' && (flag[1] == 'f' || flag[1] == 't')) {
454 /* list of pids to finish ('f') or terminate ('t') */
455
456 if (flag[1] == 'f')
457 oldpids_sig = SIGUSR1; /* finish then exit */
458 else
459 oldpids_sig = SIGTERM; /* terminate immediately */
460 argv++; argc--;
461
462 if (argc > 0) {
463 oldpids = calloc(argc, sizeof(int));
464 while (argc > 0) {
465 oldpids[nb_oldpids] = atol(*argv);
466 if (oldpids[nb_oldpids] <= 0)
467 usage(old_argv);
468 argc--; argv++;
469 nb_oldpids++;
470 }
471 }
472 }
473 else { /* >=2 args */
474 argv++; argc--;
475 if (argc == 0)
476 usage(old_argv);
477
478 switch (*flag) {
479 case 'n' : cfg_maxconn = atol(*argv); break;
480 case 'm' : global.rlimit_memmax = atol(*argv); break;
481 case 'N' : cfg_maxpconn = atol(*argv); break;
482 case 'f' : cfg_cfgfile = *argv; break;
483 case 'p' : cfg_pidfile = *argv; break;
484 default: usage(old_argv);
485 }
486 }
487 }
488 else
489 usage(old_argv);
490 argv++; argc--;
491 }
492
493 global.mode = MODE_STARTING | /* during startup, we want most of the alerts */
494 (arg_mode & (MODE_DAEMON | MODE_FOREGROUND | MODE_VERBOSE
495 | MODE_QUIET | MODE_CHECK | MODE_DEBUG));
496
497 if (!cfg_cfgfile)
498 usage(old_argv);
499
500 gethostname(hostname, MAX_HOSTNAME_LEN);
501
502 have_appsession = 0;
503 global.maxsock = 10; /* reserve 10 fds ; will be incremented by socket eaters */
504 if (readcfgfile(cfg_cfgfile) < 0) {
505 Alert("Error reading configuration file : %s\n", cfg_cfgfile);
506 exit(1);
507 }
508 if (have_appsession)
509 appsession_init();
510
511 if (global.mode & MODE_CHECK) {
512 qfprintf(stdout, "Configuration file is valid : %s\n", cfg_cfgfile);
513 exit(0);
514 }
515
516 if (cfg_maxconn > 0)
517 global.maxconn = cfg_maxconn;
518
519 if (cfg_pidfile) {
520 if (global.pidfile)
521 free(global.pidfile);
522 global.pidfile = strdup(cfg_pidfile);
523 }
524
525 if (global.maxconn == 0)
526 global.maxconn = DEFAULT_MAXCONN;
527
528 global.maxsock += global.maxconn * 2; /* each connection needs two sockets */
529
Willy Tarreau1db37712007-06-03 17:16:49 +0200530 if (global.tune.maxpollevents <= 0)
531 global.tune.maxpollevents = MAX_POLL_EVENTS;
532
Willy Tarreaubaaee002006-06-26 02:48:02 +0200533 if (arg_mode & (MODE_DEBUG | MODE_FOREGROUND)) {
534 /* command line debug mode inhibits configuration mode */
535 global.mode &= ~(MODE_DAEMON | MODE_QUIET);
536 }
537 global.mode |= (arg_mode & (MODE_DAEMON | MODE_FOREGROUND | MODE_QUIET |
538 MODE_VERBOSE | MODE_DEBUG | MODE_STATS | MODE_LOG));
539
540 if ((global.mode & MODE_DEBUG) && (global.mode & (MODE_DAEMON | MODE_QUIET))) {
541 Warning("<debug> mode incompatible with <quiet> and <daemon>. Keeping <debug> only.\n");
542 global.mode &= ~(MODE_DAEMON | MODE_QUIET);
543 }
544
545 if ((global.nbproc > 1) && !(global.mode & MODE_DAEMON)) {
546 if (!(global.mode & (MODE_FOREGROUND | MODE_DEBUG)))
547 Warning("<nbproc> is only meaningful in daemon mode. Setting limit to 1 process.\n");
548 global.nbproc = 1;
549 }
550
551 if (global.nbproc < 1)
552 global.nbproc = 1;
553
Willy Tarreaubaaee002006-06-26 02:48:02 +0200554 fdtab = (struct fdtab *)calloc(1,
555 sizeof(struct fdtab) * (global.maxsock));
556 for (i = 0; i < global.maxsock; i++) {
557 fdtab[i].state = FD_STCLOSE;
558 }
Willy Tarreau4f60f162007-04-08 16:39:58 +0200559
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200560 /*
561 * Note: we could register external pollers here.
562 * Built-in pollers have been registered before main().
563 */
Willy Tarreau4f60f162007-04-08 16:39:58 +0200564
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200565 if (!(cfg_polling_mechanism & POLL_USE_KQUEUE))
566 disable_poller("kqueue");
567
Willy Tarreau4f60f162007-04-08 16:39:58 +0200568 if (!(cfg_polling_mechanism & POLL_USE_EPOLL))
569 disable_poller("epoll");
570
Willy Tarreaude99e992007-04-16 00:53:59 +0200571 if (!(cfg_polling_mechanism & POLL_USE_SEPOLL))
572 disable_poller("sepoll");
573
Willy Tarreau4f60f162007-04-08 16:39:58 +0200574 if (!(cfg_polling_mechanism & POLL_USE_POLL))
575 disable_poller("poll");
576
577 if (!(cfg_polling_mechanism & POLL_USE_SELECT))
578 disable_poller("select");
579
580 /* Note: we could disable any poller by name here */
581
Willy Tarreau2ff76222007-04-09 19:29:56 +0200582 if (global.mode & (MODE_VERBOSE|MODE_DEBUG))
583 list_pollers(stderr);
584
Willy Tarreau4f60f162007-04-08 16:39:58 +0200585 if (!init_pollers()) {
Willy Tarreau2ff76222007-04-09 19:29:56 +0200586 Alert("No polling mechanism available.\n");
Willy Tarreau4f60f162007-04-08 16:39:58 +0200587 exit(1);
588 }
Willy Tarreau2ff76222007-04-09 19:29:56 +0200589 if (global.mode & (MODE_VERBOSE|MODE_DEBUG)) {
590 printf("Using %s() as the polling mechanism.\n", cur_poller.name);
Willy Tarreau4f60f162007-04-08 16:39:58 +0200591 }
592
Willy Tarreaubaaee002006-06-26 02:48:02 +0200593}
594
595void deinit(void)
596{
Willy Tarreau4d2d0982007-05-14 00:39:29 +0200597 struct proxy *p = proxy, *p0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200598 struct cap_hdr *h,*h_next;
599 struct server *s,*s_next;
600 struct listener *l,*l_next;
Willy Tarreau0fc45a72007-06-17 00:36:03 +0200601 struct acl_cond *cond, *condb;
602 struct hdr_exp *exp, *expb;
603 int i;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200604
605 while (p) {
606 if (p->id)
607 free(p->id);
608
609 if (p->check_req)
610 free(p->check_req);
611
612 if (p->cookie_name)
613 free(p->cookie_name);
614
615 if (p->capture_name)
616 free(p->capture_name);
617
Willy Tarreau0fc45a72007-06-17 00:36:03 +0200618 if (p->monitor_uri)
619 free(p->monitor_uri);
620
621 for (i = 0; i < HTTP_ERR_SIZE; i++) {
622 if (p->errmsg[i].len)
623 free(p->errmsg[i].str);
624 }
625
626 for (i = 0; i < p->nb_reqadd; i++) {
627 if (p->req_add[i])
628 free(p->req_add[i]);
629 }
630
631 for (i = 0; i < p->nb_rspadd; i++) {
632 if (p->rsp_add[i])
633 free(p->rsp_add[i]);
634 }
635
636 list_for_each_entry_safe(cond, condb, &p->block_cond, list) {
637 LIST_DEL(&cond->list);
638 prune_acl_cond(cond);
639 free(cond);
640 }
641
642 for (exp = p->req_exp; exp != NULL; ) {
643 if (exp->preg)
644 regfree((regex_t *)exp->preg);
645 if (exp->replace && exp->action != ACT_SETBE)
646 free((char *)exp->replace);
647 expb = exp;
648 exp = exp->next;
649 free(expb);
650 }
651
652 for (exp = p->rsp_exp; exp != NULL; ) {
653 if (exp->preg)
654 regfree((regex_t *)exp->preg);
655 if (exp->replace && exp->action != ACT_SETBE)
656 free((char *)exp->replace);
657 expb = exp;
658 exp = exp->next;
659 free(expb);
660 }
661
662 /* FIXME: this must also be freed :
663 * - ACLs
664 * - uri_auth (but it's shared)
665 */
666
Willy Tarreaubaaee002006-06-26 02:48:02 +0200667 if (p->appsession_name)
668 free(p->appsession_name);
669
670 h = p->req_cap;
671 while (h) {
672 h_next = h->next;
673 if (h->name)
674 free(h->name);
Willy Tarreaucf7f3202007-05-13 22:46:04 +0200675 pool_destroy2(h->pool);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200676 free(h);
677 h = h_next;
678 }/* end while(h) */
679
680 h = p->rsp_cap;
681 while (h) {
682 h_next = h->next;
683 if (h->name)
684 free(h->name);
685
Willy Tarreaucf7f3202007-05-13 22:46:04 +0200686 pool_destroy2(h->pool);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200687 free(h);
688 h = h_next;
689 }/* end while(h) */
690
691 s = p->srv;
692 while (s) {
693 s_next = s->next;
694 if (s->id)
695 free(s->id);
696
697 if (s->cookie)
698 free(s->cookie);
699
700 free(s);
701 s = s_next;
702 }/* end while(s) */
703
704 l = p->listen;
705 while (l) {
706 l_next = l->next;
707 free(l);
708 l = l_next;
709 }/* end while(l) */
710
Willy Tarreaucf7f3202007-05-13 22:46:04 +0200711 pool_destroy2(p->req_cap_pool);
712 pool_destroy2(p->rsp_cap_pool);
Willy Tarreau4d2d0982007-05-14 00:39:29 +0200713 p0 = p;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200714 p = p->next;
Willy Tarreau4d2d0982007-05-14 00:39:29 +0200715 free(p0);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200716 }/* end while(p) */
717
718 if (global.chroot) free(global.chroot);
Willy Tarreau0fc45a72007-06-17 00:36:03 +0200719 global.chroot = NULL;
720
Willy Tarreaubaaee002006-06-26 02:48:02 +0200721 if (global.pidfile) free(global.pidfile);
Willy Tarreau0fc45a72007-06-17 00:36:03 +0200722 global.pidfile = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200723
Willy Tarreaubaaee002006-06-26 02:48:02 +0200724 if (fdtab) free(fdtab);
Willy Tarreau0fc45a72007-06-17 00:36:03 +0200725 fdtab = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200726
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200727 pool_destroy2(pool2_session);
Willy Tarreau7341d942007-05-13 19:56:02 +0200728 pool_destroy2(pool2_buffer);
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200729 pool_destroy2(pool2_requri);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200730 pool_destroy2(pool2_task);
Willy Tarreau086b3b42007-05-13 21:45:51 +0200731 pool_destroy2(pool2_capture);
Willy Tarreau63963c62007-05-13 21:29:55 +0200732 pool_destroy2(pool2_appsess);
Willy Tarreaue4d7e552007-05-13 20:19:55 +0200733 pool_destroy2(pool2_pendconn);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200734
735 if (have_appsession) {
Willy Tarreau63963c62007-05-13 21:29:55 +0200736 pool_destroy2(apools.serverid);
737 pool_destroy2(apools.sessid);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200738 }
739} /* end deinit() */
740
741/* sends the signal <sig> to all pids found in <oldpids> */
742static void tell_old_pids(int sig)
743{
744 int p;
745 for (p = 0; p < nb_oldpids; p++)
746 kill(oldpids[p], sig);
747}
748
Willy Tarreau4f60f162007-04-08 16:39:58 +0200749/*
750 * Runs the polling loop
751 *
752 * FIXME:
753 * - we still use 'listeners' to check whether we want to stop or not.
754 *
755 */
756void run_poll_loop()
757{
Willy Tarreaud825eef2007-05-12 22:35:00 +0200758 struct timeval next;
Willy Tarreau4f60f162007-04-08 16:39:58 +0200759
Willy Tarreaud825eef2007-05-12 22:35:00 +0200760 tv_now(&now);
Willy Tarreau4f60f162007-04-08 16:39:58 +0200761 while (1) {
Willy Tarreaud825eef2007-05-12 22:35:00 +0200762 process_runnable_tasks(&next);
Willy Tarreau4f60f162007-04-08 16:39:58 +0200763
764 /* stop when there's no connection left and we don't allow them anymore */
765 if (!actconn && listeners == 0)
766 break;
767
Willy Tarreaud825eef2007-05-12 22:35:00 +0200768 cur_poller.poll(&cur_poller, &next);
Willy Tarreau4f60f162007-04-08 16:39:58 +0200769 }
770}
771
772
Willy Tarreaubaaee002006-06-26 02:48:02 +0200773int main(int argc, char **argv)
774{
775 int err, retry;
776 struct rlimit limit;
777 FILE *pidfile = NULL;
778 init(argc, argv);
779
780 signal(SIGQUIT, dump);
781 signal(SIGUSR1, sig_soft_stop);
782 signal(SIGHUP, sig_dump_state);
783#ifdef DEBUG_MEMORY
784 signal(SIGINT, sig_int);
785 signal(SIGTERM, sig_term);
786#endif
787
788 /* on very high loads, a sigpipe sometimes happen just between the
789 * getsockopt() which tells "it's OK to write", and the following write :-(
790 */
791#ifndef MSG_NOSIGNAL
792 signal(SIGPIPE, SIG_IGN);
793#endif
794
795 /* We will loop at most 100 times with 10 ms delay each time.
796 * That's at most 1 second. We only send a signal to old pids
797 * if we cannot grab at least one port.
798 */
799 retry = MAX_START_RETRIES;
800 err = ERR_NONE;
801 while (retry >= 0) {
802 struct timeval w;
803 err = start_proxies(retry == 0 || nb_oldpids == 0);
804 if (err != ERR_RETRYABLE)
805 break;
806 if (nb_oldpids == 0)
807 break;
808
809 /* FIXME-20060514: Solaris and OpenBSD do not support shutdown() on
810 * listening sockets. So on those platforms, it would be wiser to
811 * simply send SIGUSR1, which will not be undoable.
812 */
813 tell_old_pids(SIGTTOU);
814 /* give some time to old processes to stop listening */
815 w.tv_sec = 0;
816 w.tv_usec = 10*1000;
817 select(0, NULL, NULL, NULL, &w);
818 retry--;
819 }
820
821 /* Note: start_proxies() sends an alert when it fails. */
822 if (err != ERR_NONE) {
823 if (retry != MAX_START_RETRIES && nb_oldpids)
824 tell_old_pids(SIGTTIN);
825 exit(1);
826 }
827
828 if (listeners == 0) {
829 Alert("[%s.main()] No enabled listener found (check the <listen> keywords) ! Exiting.\n", argv[0]);
830 /* Note: we don't have to send anything to the old pids because we
831 * never stopped them. */
832 exit(1);
833 }
834
835 /* prepare pause/play signals */
836 signal(SIGTTOU, sig_pause);
837 signal(SIGTTIN, sig_listen);
838
839 if (global.mode & MODE_DAEMON) {
840 global.mode &= ~MODE_VERBOSE;
841 global.mode |= MODE_QUIET;
842 }
843
844 /* MODE_QUIET can inhibit alerts and warnings below this line */
845
846 global.mode &= ~MODE_STARTING;
847 if ((global.mode & MODE_QUIET) && !(global.mode & MODE_VERBOSE)) {
848 /* detach from the tty */
849 fclose(stdin); fclose(stdout); fclose(stderr);
850 close(0); close(1); close(2);
851 }
852
853 /* open log & pid files before the chroot */
854 if (global.mode & MODE_DAEMON && global.pidfile != NULL) {
855 int pidfd;
856 unlink(global.pidfile);
857 pidfd = open(global.pidfile, O_CREAT | O_WRONLY | O_TRUNC, 0644);
858 if (pidfd < 0) {
859 Alert("[%s.main()] Cannot create pidfile %s\n", argv[0], global.pidfile);
860 if (nb_oldpids)
861 tell_old_pids(SIGTTIN);
862 exit(1);
863 }
864 pidfile = fdopen(pidfd, "w");
865 }
866
867 /* chroot if needed */
868 if (global.chroot != NULL) {
869 if (chroot(global.chroot) == -1) {
870 Alert("[%s.main()] Cannot chroot(%s).\n", argv[0], global.chroot);
871 if (nb_oldpids)
872 tell_old_pids(SIGTTIN);
873 }
874 chdir("/");
875 }
876
877 /* ulimits */
878 if (!global.rlimit_nofile)
879 global.rlimit_nofile = global.maxsock;
880
881 if (global.rlimit_nofile) {
882 limit.rlim_cur = limit.rlim_max = global.rlimit_nofile;
883 if (setrlimit(RLIMIT_NOFILE, &limit) == -1) {
884 Warning("[%s.main()] Cannot raise FD limit to %d.\n", argv[0], global.rlimit_nofile);
885 }
886 }
887
888 if (global.rlimit_memmax) {
889 limit.rlim_cur = limit.rlim_max =
890 global.rlimit_memmax * 1048576 / global.nbproc;
891#ifdef RLIMIT_AS
892 if (setrlimit(RLIMIT_AS, &limit) == -1) {
893 Warning("[%s.main()] Cannot fix MEM limit to %d megs.\n",
894 argv[0], global.rlimit_memmax);
895 }
896#else
897 if (setrlimit(RLIMIT_DATA, &limit) == -1) {
898 Warning("[%s.main()] Cannot fix MEM limit to %d megs.\n",
899 argv[0], global.rlimit_memmax);
900 }
901#endif
902 }
903
Willy Tarreau6d1a9882007-01-07 02:03:04 +0100904#ifdef CONFIG_HAP_TCPSPLICE
905 if (global.last_checks & LSTCHK_TCPSPLICE) {
906 if (tcp_splice_start() < 0) {
907 Alert("[%s.main()] Cannot enable tcp_splice.\n"
908 " Make sure you have enough permissions and that the module is loadable.\n"
909 " Alternatively, you may disable the 'tcpsplice' options in the configuration.\n"
910 "", argv[0], global.gid);
911 exit(1);
912 }
913 }
914#endif
915
Willy Tarreaub38651a2007-03-24 17:24:39 +0100916#ifdef CONFIG_HAP_CTTPROXY
917 if (global.last_checks & LSTCHK_CTTPROXY) {
918 int ret;
919
920 ret = check_cttproxy_version();
921 if (ret < 0) {
922 Alert("[%s.main()] Cannot enable cttproxy.\n%s",
923 argv[0],
924 (ret == -1) ? " Incorrect module version.\n"
925 : " Make sure you have enough permissions and that the module is loaded.\n");
926 exit(1);
927 }
928 }
929#endif
930
931 if ((global.last_checks & LSTCHK_NETADM) && global.uid) {
932 Alert("[%s.main()] Some configuration options require full privileges, so global.uid cannot be changed.\n"
933 "", argv[0], global.gid);
934 exit(1);
935 }
936
Willy Tarreaubaaee002006-06-26 02:48:02 +0200937 if (nb_oldpids)
938 tell_old_pids(oldpids_sig);
939
940 /* Note that any error at this stage will be fatal because we will not
941 * be able to restart the old pids.
942 */
943
944 /* setgid / setuid */
945 if (global.gid && setgid(global.gid) == -1) {
946 Alert("[%s.main()] Cannot set gid %d.\n", argv[0], global.gid);
947 exit(1);
948 }
949
950 if (global.uid && setuid(global.uid) == -1) {
951 Alert("[%s.main()] Cannot set uid %d.\n", argv[0], global.uid);
952 exit(1);
953 }
954
955 /* check ulimits */
956 limit.rlim_cur = limit.rlim_max = 0;
957 getrlimit(RLIMIT_NOFILE, &limit);
958 if (limit.rlim_cur < global.maxsock) {
959 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",
960 argv[0], limit.rlim_cur, global.maxconn, global.maxsock, global.maxsock);
961 }
962
963 if (global.mode & MODE_DAEMON) {
964 int ret = 0;
965 int proc;
966
967 /* the father launches the required number of processes */
968 for (proc = 0; proc < global.nbproc; proc++) {
969 ret = fork();
970 if (ret < 0) {
971 Alert("[%s.main()] Cannot fork.\n", argv[0]);
972 if (nb_oldpids)
973 exit(1); /* there has been an error */
974 }
975 else if (ret == 0) /* child breaks here */
976 break;
977 if (pidfile != NULL) {
978 fprintf(pidfile, "%d\n", ret);
979 fflush(pidfile);
980 }
981 }
982 /* close the pidfile both in children and father */
983 if (pidfile != NULL)
984 fclose(pidfile);
985 free(global.pidfile);
986
987 if (proc == global.nbproc)
988 exit(0); /* parent must leave */
989
990 /* if we're NOT in QUIET mode, we should now close the 3 first FDs to ensure
991 * that we can detach from the TTY. We MUST NOT do it in other cases since
992 * it would have already be done, and 0-2 would have been affected to listening
993 * sockets
994 */
995 if (!(global.mode & MODE_QUIET)) {
996 /* detach from the tty */
997 fclose(stdin); fclose(stdout); fclose(stderr);
998 close(0); close(1); close(2); /* close all fd's */
999 global.mode |= MODE_QUIET; /* ensure that we won't say anything from now */
1000 }
1001 pid = getpid(); /* update child's pid */
1002 setsid();
Willy Tarreau2ff76222007-04-09 19:29:56 +02001003 fork_poller();
Willy Tarreaubaaee002006-06-26 02:48:02 +02001004 }
1005
Willy Tarreau4f60f162007-04-08 16:39:58 +02001006 /*
1007 * That's it : the central polling loop. Run until we stop.
1008 */
1009 run_poll_loop();
Willy Tarreaubaaee002006-06-26 02:48:02 +02001010
1011 /* Free all Hash Keys and all Hash elements */
1012 appsession_cleanup();
1013 /* Do some cleanup */
1014 deinit();
1015
1016 exit(0);
1017}
1018
1019
1020/*
1021 * Local variables:
1022 * c-indent-level: 8
1023 * c-basic-offset: 8
1024 * End:
1025 */