blob: a86abeb3196d3a47f416d05e4d69d030c4ab1470 [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
81#include <proto/backend.h>
82#include <proto/buffers.h>
83#include <proto/client.h>
84#include <proto/fd.h>
85#include <proto/log.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>
Willy Tarreauc6ca1a02007-05-13 19:43:47 +020090#include <proto/session.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020091#include <proto/stream_sock.h>
92#include <proto/task.h>
93
Willy Tarreau6d1a9882007-01-07 02:03:04 +010094#ifdef CONFIG_HAP_TCPSPLICE
95#include <libtcpsplice.h>
96#endif
97
Willy Tarreaub38651a2007-03-24 17:24:39 +010098#ifdef CONFIG_HAP_CTTPROXY
99#include <proto/cttproxy.h>
100#endif
101
Willy Tarreaubaaee002006-06-26 02:48:02 +0200102/*********************************************************************/
103
104/*********************************************************************/
105
106char *cfg_cfgfile = NULL; /* configuration file */
107char *progname = NULL; /* program name */
108int pid; /* current process id */
109
110/* global options */
111struct global global = {
112 logfac1 : -1,
113 logfac2 : -1,
114 loglev1 : 7, /* max syslog level : debug */
115 loglev2 : 7,
116 /* others NULL OK */
117};
118
119/*********************************************************************/
120
121int stopping; /* non zero means stopping in progress */
122
123/* Here we store informations about the pids of the processes we may pause
124 * or kill. We will send them a signal every 10 ms until we can bind to all
125 * our ports. With 200 retries, that's about 2 seconds.
126 */
127#define MAX_START_RETRIES 200
128static int nb_oldpids = 0;
129static int *oldpids = NULL;
130static int oldpids_sig; /* use USR1 or TERM */
131
132/* this is used to drain data, and as a temporary buffer for sprintf()... */
133char trash[BUFSIZE];
134
135const int zero = 0;
136const int one = 1;
137
138/*
139 * Syslog facilities and levels. Conforming to RFC3164.
140 */
141
142#define MAX_HOSTNAME_LEN 32
143static char hostname[MAX_HOSTNAME_LEN] = "";
144
145
146/*********************************************************************/
147/* general purpose functions ***************************************/
148/*********************************************************************/
149
150void display_version()
151{
152 printf("HA-Proxy version " HAPROXY_VERSION " " HAPROXY_DATE"\n");
Willy Tarreau49e1ee82007-01-22 00:56:46 +0100153 printf("Copyright 2000-2007 Willy Tarreau <w@1wt.eu>\n\n");
Willy Tarreaubaaee002006-06-26 02:48:02 +0200154}
155
156/*
157 * This function prints the command line usage and exits
158 */
159void usage(char *name)
160{
161 display_version();
162 fprintf(stderr,
163 "Usage : %s -f <cfgfile> [ -vdV"
164 "D ] [ -n <maxconn> ] [ -N <maxpconn> ]\n"
165 " [ -p <pidfile> ] [ -m <max megs> ]\n"
166 " -v displays version\n"
167 " -d enters debug mode ; -db only disables background mode.\n"
168 " -V enters verbose mode (disables quiet mode)\n"
169 " -D goes daemon ; implies -q\n"
170 " -q quiet mode : don't display messages\n"
171 " -c check mode : only check config file and exit\n"
172 " -n sets the maximum total # of connections (%d)\n"
173 " -m limits the usable amount of memory (in MB)\n"
174 " -N sets the default, per-proxy maximum # of connections (%d)\n"
175 " -p writes pids of all children to this file\n"
176#if defined(ENABLE_EPOLL)
177 " -de disables epoll() usage even when available\n"
178#endif
Willy Tarreaude99e992007-04-16 00:53:59 +0200179#if defined(ENABLE_SEPOLL)
180 " -ds disables speculative epoll() usage even when available\n"
181#endif
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200182#if defined(ENABLE_KQUEUE)
183 " -dk disables kqueue() usage even when available\n"
184#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200185#if defined(ENABLE_POLL)
186 " -dp disables poll() usage even when available\n"
187#endif
188 " -sf/-st [pid ]* finishes/terminates old pids. Must be last arguments.\n"
189 "\n",
190 name, DEFAULT_MAXCONN, cfg_maxpconn);
191 exit(1);
192}
193
194
195
196/*********************************************************************/
197/* more specific functions ***************************************/
198/*********************************************************************/
199
200/*
201 * upon SIGUSR1, let's have a soft stop.
202 */
203void sig_soft_stop(int sig)
204{
205 soft_stop();
Willy Tarreau4d2d0982007-05-14 00:39:29 +0200206 pool_gc2();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200207 signal(sig, SIG_IGN);
208}
209
210/*
211 * upon SIGTTOU, we pause everything
212 */
213void sig_pause(int sig)
214{
215 pause_proxies();
Willy Tarreau4d2d0982007-05-14 00:39:29 +0200216 pool_gc2();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200217 signal(sig, sig_pause);
218}
219
220/*
221 * upon SIGTTIN, let's have a soft stop.
222 */
223void sig_listen(int sig)
224{
225 listen_proxies();
226 signal(sig, sig_listen);
227}
228
229/*
230 * this function dumps every server's state when the process receives SIGHUP.
231 */
232void sig_dump_state(int sig)
233{
234 struct proxy *p = proxy;
235
236 Warning("SIGHUP received, dumping servers states.\n");
237 while (p) {
238 struct server *s = p->srv;
239
240 send_log(p, LOG_NOTICE, "SIGHUP received, dumping servers states for proxy %s.\n", p->id);
241 while (s) {
242 snprintf(trash, sizeof(trash),
243 "SIGHUP: Server %s/%s is %s. Conn: %d act, %d pend, %d tot.",
244 p->id, s->id,
245 (s->state & SRV_RUNNING) ? "UP" : "DOWN",
246 s->cur_sess, s->nbpend, s->cum_sess);
247 Warning("%s\n", trash);
248 send_log(p, LOG_NOTICE, "%s\n", trash);
249 s = s->next;
250 }
251
252 if (p->srv_act == 0) {
253 snprintf(trash, sizeof(trash),
Willy Tarreauf1221aa2006-12-17 22:14:12 +0100254 "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 +0200255 p->id,
256 (p->srv_bck) ? "is running on backup servers" : "has no server available",
Willy Tarreauf1221aa2006-12-17 22:14:12 +0100257 p->feconn, p->beconn, p->totpend, p->nbpend, p->cum_feconn, p->cum_beconn);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200258 } else {
259 snprintf(trash, sizeof(trash),
260 "SIGHUP: Proxy %s has %d active servers and %d backup servers available."
Willy Tarreauf1221aa2006-12-17 22:14:12 +0100261 " 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, p->srv_act, p->srv_bck,
Willy Tarreauf1221aa2006-12-17 22:14:12 +0100263 p->feconn, p->beconn, p->totpend, p->nbpend, p->cum_feconn, p->cum_beconn);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200264 }
265 Warning("%s\n", trash);
266 send_log(p, LOG_NOTICE, "%s\n", trash);
267
268 p = p->next;
269 }
270 signal(sig, sig_dump_state);
271}
272
273void dump(int sig)
274{
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200275#if 0
Willy Tarreau964c9362007-01-07 00:38:00 +0100276 struct task *t;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200277 struct session *s;
Willy Tarreau964c9362007-01-07 00:38:00 +0100278 struct rb_node *node;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200279
Willy Tarreau964c9362007-01-07 00:38:00 +0100280 for(node = rb_first(&wait_queue[0]);
281 node != NULL; node = rb_next(node)) {
282 t = rb_entry(node, struct task, rb_node);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200283 s = t->context;
284 qfprintf(stderr,"[dump] wq: task %p, still %ld ms, "
285 "cli=%d, srv=%d, cr=%d, cw=%d, sr=%d, sw=%d, "
286 "req=%d, rep=%d, clifd=%d\n",
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200287 s, tv_ms_remain(&now, &t->expire),
Willy Tarreaubaaee002006-06-26 02:48:02 +0200288 s->cli_state,
289 s->srv_state,
Willy Tarreauf161a342007-04-08 16:59:42 +0200290 EV_FD_ISSET(s->cli_fd, DIR_RD),
291 EV_FD_ISSET(s->cli_fd, DIR_WR),
292 EV_FD_ISSET(s->srv_fd, DIR_RD),
293 EV_FD_ISSET(s->srv_fd, DIR_WR),
Willy Tarreaubaaee002006-06-26 02:48:02 +0200294 s->req->l, s->rep?s->rep->l:0, s->cli_fd
295 );
296 }
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200297#endif
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200298 /* dump memory usage then free everything possible */
299 dump_pools();
300 pool_gc2();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200301}
302
303#ifdef DEBUG_MEMORY
304static void fast_stop(void)
305{
306 struct proxy *p;
307 p = proxy;
308 while (p) {
309 p->grace = 0;
310 p = p->next;
311 }
312 soft_stop();
313}
314
315void sig_int(int sig)
316{
317 /* This would normally be a hard stop,
318 but we want to be sure about deallocation,
319 and so on, so we do a soft stop with
320 0 GRACE time
321 */
322 fast_stop();
Willy Tarreau4d2d0982007-05-14 00:39:29 +0200323 pool_gc2();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200324 /* If we are killed twice, we decide to die*/
325 signal(sig, SIG_DFL);
326}
327
328void sig_term(int sig)
329{
330 /* This would normally be a hard stop,
331 but we want to be sure about deallocation,
332 and so on, so we do a soft stop with
333 0 GRACE time
334 */
335 fast_stop();
Willy Tarreau4d2d0982007-05-14 00:39:29 +0200336 pool_gc2();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200337 /* If we are killed twice, we decide to die*/
338 signal(sig, SIG_DFL);
339}
340#endif
341
342
343/*
344 * This function initializes all the necessary variables. It only returns
345 * if everything is OK. If something fails, it exits.
346 */
347void init(int argc, char **argv)
348{
349 int i;
350 int arg_mode = 0; /* MODE_DEBUG, ... */
351 char *old_argv = *argv;
352 char *tmp;
353 char *cfg_pidfile = NULL;
354
355 if (1<<INTBITS != sizeof(int)*8) {
356 fprintf(stderr,
357 "Error: wrong architecture. Recompile so that sizeof(int)=%d\n",
358 (int)(sizeof(int)*8));
359 exit(1);
360 }
361
362 /*
363 * Initialize the previously static variables.
364 */
365
366 totalconn = actconn = maxfd = listeners = stopping = 0;
367
368
369#ifdef HAPROXY_MEMMAX
370 global.rlimit_memmax = HAPROXY_MEMMAX;
371#endif
372
373 /* initialize the libc's localtime structures once for all so that we
374 * won't be missing memory if we want to send alerts under OOM conditions.
Willy Tarreau2b35c952006-10-15 15:25:48 +0200375 * Also, the Alert() and Warning() functions need <now> to be initialized.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200376 */
377 tv_now(&now);
Willy Tarreaubf736132006-10-15 22:54:47 +0200378 localtime((time_t *)&now.tv_sec);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200379 start_date = now;
380
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200381 init_task();
382 init_session();
Willy Tarreaue4d7e552007-05-13 20:19:55 +0200383 init_buffer();
384 init_pendconn();
Willy Tarreau80587432006-12-24 17:47:20 +0100385 init_proto_http();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200386
387 cfg_polling_mechanism = POLL_USE_SELECT; /* select() is always available */
388#if defined(ENABLE_POLL)
389 cfg_polling_mechanism |= POLL_USE_POLL;
390#endif
391#if defined(ENABLE_EPOLL)
392 cfg_polling_mechanism |= POLL_USE_EPOLL;
393#endif
Willy Tarreaude99e992007-04-16 00:53:59 +0200394#if defined(ENABLE_SEPOLL)
395 cfg_polling_mechanism |= POLL_USE_SEPOLL;
396#endif
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200397#if defined(ENABLE_KQUEUE)
398 cfg_polling_mechanism |= POLL_USE_KQUEUE;
399#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200400
401 pid = getpid();
402 progname = *argv;
403 while ((tmp = strchr(progname, '/')) != NULL)
404 progname = tmp + 1;
405
406 argc--; argv++;
407 while (argc > 0) {
408 char *flag;
409
410 if (**argv == '-') {
411 flag = *argv+1;
412
413 /* 1 arg */
414 if (*flag == 'v') {
415 display_version();
416 exit(0);
417 }
418#if defined(ENABLE_EPOLL)
419 else if (*flag == 'd' && flag[1] == 'e')
420 cfg_polling_mechanism &= ~POLL_USE_EPOLL;
421#endif
Willy Tarreaude99e992007-04-16 00:53:59 +0200422#if defined(ENABLE_SEPOLL)
423 else if (*flag == 'd' && flag[1] == 's')
424 cfg_polling_mechanism &= ~POLL_USE_SEPOLL;
425#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200426#if defined(ENABLE_POLL)
427 else if (*flag == 'd' && flag[1] == 'p')
428 cfg_polling_mechanism &= ~POLL_USE_POLL;
429#endif
Willy Tarreau69cad1a2007-04-10 22:45:11 +0200430#if defined(ENABLE_KQUEUE)
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200431 else if (*flag == 'd' && flag[1] == 'k')
432 cfg_polling_mechanism &= ~POLL_USE_KQUEUE;
433#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200434 else if (*flag == 'V')
435 arg_mode |= MODE_VERBOSE;
436 else if (*flag == 'd' && flag[1] == 'b')
437 arg_mode |= MODE_FOREGROUND;
438 else if (*flag == 'd')
439 arg_mode |= MODE_DEBUG;
440 else if (*flag == 'c')
441 arg_mode |= MODE_CHECK;
442 else if (*flag == 'D')
443 arg_mode |= MODE_DAEMON | MODE_QUIET;
444 else if (*flag == 'q')
445 arg_mode |= MODE_QUIET;
446 else if (*flag == 's' && (flag[1] == 'f' || flag[1] == 't')) {
447 /* list of pids to finish ('f') or terminate ('t') */
448
449 if (flag[1] == 'f')
450 oldpids_sig = SIGUSR1; /* finish then exit */
451 else
452 oldpids_sig = SIGTERM; /* terminate immediately */
453 argv++; argc--;
454
455 if (argc > 0) {
456 oldpids = calloc(argc, sizeof(int));
457 while (argc > 0) {
458 oldpids[nb_oldpids] = atol(*argv);
459 if (oldpids[nb_oldpids] <= 0)
460 usage(old_argv);
461 argc--; argv++;
462 nb_oldpids++;
463 }
464 }
465 }
466 else { /* >=2 args */
467 argv++; argc--;
468 if (argc == 0)
469 usage(old_argv);
470
471 switch (*flag) {
472 case 'n' : cfg_maxconn = atol(*argv); break;
473 case 'm' : global.rlimit_memmax = atol(*argv); break;
474 case 'N' : cfg_maxpconn = atol(*argv); break;
475 case 'f' : cfg_cfgfile = *argv; break;
476 case 'p' : cfg_pidfile = *argv; break;
477 default: usage(old_argv);
478 }
479 }
480 }
481 else
482 usage(old_argv);
483 argv++; argc--;
484 }
485
486 global.mode = MODE_STARTING | /* during startup, we want most of the alerts */
487 (arg_mode & (MODE_DAEMON | MODE_FOREGROUND | MODE_VERBOSE
488 | MODE_QUIET | MODE_CHECK | MODE_DEBUG));
489
490 if (!cfg_cfgfile)
491 usage(old_argv);
492
493 gethostname(hostname, MAX_HOSTNAME_LEN);
494
495 have_appsession = 0;
496 global.maxsock = 10; /* reserve 10 fds ; will be incremented by socket eaters */
497 if (readcfgfile(cfg_cfgfile) < 0) {
498 Alert("Error reading configuration file : %s\n", cfg_cfgfile);
499 exit(1);
500 }
501 if (have_appsession)
502 appsession_init();
503
504 if (global.mode & MODE_CHECK) {
505 qfprintf(stdout, "Configuration file is valid : %s\n", cfg_cfgfile);
506 exit(0);
507 }
508
509 if (cfg_maxconn > 0)
510 global.maxconn = cfg_maxconn;
511
512 if (cfg_pidfile) {
513 if (global.pidfile)
514 free(global.pidfile);
515 global.pidfile = strdup(cfg_pidfile);
516 }
517
518 if (global.maxconn == 0)
519 global.maxconn = DEFAULT_MAXCONN;
520
521 global.maxsock += global.maxconn * 2; /* each connection needs two sockets */
522
523 if (arg_mode & (MODE_DEBUG | MODE_FOREGROUND)) {
524 /* command line debug mode inhibits configuration mode */
525 global.mode &= ~(MODE_DAEMON | MODE_QUIET);
526 }
527 global.mode |= (arg_mode & (MODE_DAEMON | MODE_FOREGROUND | MODE_QUIET |
528 MODE_VERBOSE | MODE_DEBUG | MODE_STATS | MODE_LOG));
529
530 if ((global.mode & MODE_DEBUG) && (global.mode & (MODE_DAEMON | MODE_QUIET))) {
531 Warning("<debug> mode incompatible with <quiet> and <daemon>. Keeping <debug> only.\n");
532 global.mode &= ~(MODE_DAEMON | MODE_QUIET);
533 }
534
535 if ((global.nbproc > 1) && !(global.mode & MODE_DAEMON)) {
536 if (!(global.mode & (MODE_FOREGROUND | MODE_DEBUG)))
537 Warning("<nbproc> is only meaningful in daemon mode. Setting limit to 1 process.\n");
538 global.nbproc = 1;
539 }
540
541 if (global.nbproc < 1)
542 global.nbproc = 1;
543
Willy Tarreaubaaee002006-06-26 02:48:02 +0200544 fdtab = (struct fdtab *)calloc(1,
545 sizeof(struct fdtab) * (global.maxsock));
546 for (i = 0; i < global.maxsock; i++) {
547 fdtab[i].state = FD_STCLOSE;
548 }
Willy Tarreau4f60f162007-04-08 16:39:58 +0200549
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200550 /*
551 * Note: we could register external pollers here.
552 * Built-in pollers have been registered before main().
553 */
Willy Tarreau4f60f162007-04-08 16:39:58 +0200554
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200555 if (!(cfg_polling_mechanism & POLL_USE_KQUEUE))
556 disable_poller("kqueue");
557
Willy Tarreau4f60f162007-04-08 16:39:58 +0200558 if (!(cfg_polling_mechanism & POLL_USE_EPOLL))
559 disable_poller("epoll");
560
Willy Tarreaude99e992007-04-16 00:53:59 +0200561 if (!(cfg_polling_mechanism & POLL_USE_SEPOLL))
562 disable_poller("sepoll");
563
Willy Tarreau4f60f162007-04-08 16:39:58 +0200564 if (!(cfg_polling_mechanism & POLL_USE_POLL))
565 disable_poller("poll");
566
567 if (!(cfg_polling_mechanism & POLL_USE_SELECT))
568 disable_poller("select");
569
570 /* Note: we could disable any poller by name here */
571
Willy Tarreau2ff76222007-04-09 19:29:56 +0200572 if (global.mode & (MODE_VERBOSE|MODE_DEBUG))
573 list_pollers(stderr);
574
Willy Tarreau4f60f162007-04-08 16:39:58 +0200575 if (!init_pollers()) {
Willy Tarreau2ff76222007-04-09 19:29:56 +0200576 Alert("No polling mechanism available.\n");
Willy Tarreau4f60f162007-04-08 16:39:58 +0200577 exit(1);
578 }
Willy Tarreau2ff76222007-04-09 19:29:56 +0200579 if (global.mode & (MODE_VERBOSE|MODE_DEBUG)) {
580 printf("Using %s() as the polling mechanism.\n", cur_poller.name);
Willy Tarreau4f60f162007-04-08 16:39:58 +0200581 }
582
Willy Tarreaubaaee002006-06-26 02:48:02 +0200583}
584
585void deinit(void)
586{
Willy Tarreau4d2d0982007-05-14 00:39:29 +0200587 struct proxy *p = proxy, *p0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200588 struct cap_hdr *h,*h_next;
589 struct server *s,*s_next;
590 struct listener *l,*l_next;
591
592 while (p) {
593 if (p->id)
594 free(p->id);
595
596 if (p->check_req)
597 free(p->check_req);
598
599 if (p->cookie_name)
600 free(p->cookie_name);
601
602 if (p->capture_name)
603 free(p->capture_name);
604
605 /* only strup if the user have set in config.
606 When should we free it?!
607 if (p->errmsg.msg400) free(p->errmsg.msg400);
608 if (p->errmsg.msg403) free(p->errmsg.msg403);
609 if (p->errmsg.msg408) free(p->errmsg.msg408);
610 if (p->errmsg.msg500) free(p->errmsg.msg500);
611 if (p->errmsg.msg502) free(p->errmsg.msg502);
612 if (p->errmsg.msg503) free(p->errmsg.msg503);
613 if (p->errmsg.msg504) free(p->errmsg.msg504);
614 */
615 if (p->appsession_name)
616 free(p->appsession_name);
617
618 h = p->req_cap;
619 while (h) {
620 h_next = h->next;
621 if (h->name)
622 free(h->name);
Willy Tarreaucf7f3202007-05-13 22:46:04 +0200623 pool_destroy2(h->pool);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200624 free(h);
625 h = h_next;
626 }/* end while(h) */
627
628 h = p->rsp_cap;
629 while (h) {
630 h_next = h->next;
631 if (h->name)
632 free(h->name);
633
Willy Tarreaucf7f3202007-05-13 22:46:04 +0200634 pool_destroy2(h->pool);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200635 free(h);
636 h = h_next;
637 }/* end while(h) */
638
639 s = p->srv;
640 while (s) {
641 s_next = s->next;
642 if (s->id)
643 free(s->id);
644
645 if (s->cookie)
646 free(s->cookie);
647
648 free(s);
649 s = s_next;
650 }/* end while(s) */
651
652 l = p->listen;
653 while (l) {
654 l_next = l->next;
655 free(l);
656 l = l_next;
657 }/* end while(l) */
658
Willy Tarreaucf7f3202007-05-13 22:46:04 +0200659 pool_destroy2(p->req_cap_pool);
660 pool_destroy2(p->rsp_cap_pool);
Willy Tarreau4d2d0982007-05-14 00:39:29 +0200661 p0 = p;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200662 p = p->next;
Willy Tarreau4d2d0982007-05-14 00:39:29 +0200663 free(p0);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200664 }/* end while(p) */
665
666 if (global.chroot) free(global.chroot);
667 if (global.pidfile) free(global.pidfile);
668
Willy Tarreaubaaee002006-06-26 02:48:02 +0200669 if (fdtab) free(fdtab);
670
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200671 pool_destroy2(pool2_session);
Willy Tarreau7341d942007-05-13 19:56:02 +0200672 pool_destroy2(pool2_buffer);
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200673 pool_destroy2(pool2_requri);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200674 pool_destroy2(pool2_task);
Willy Tarreau086b3b42007-05-13 21:45:51 +0200675 pool_destroy2(pool2_capture);
Willy Tarreau63963c62007-05-13 21:29:55 +0200676 pool_destroy2(pool2_appsess);
Willy Tarreaue4d7e552007-05-13 20:19:55 +0200677 pool_destroy2(pool2_pendconn);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200678
679 if (have_appsession) {
Willy Tarreau63963c62007-05-13 21:29:55 +0200680 pool_destroy2(apools.serverid);
681 pool_destroy2(apools.sessid);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200682 }
683} /* end deinit() */
684
685/* sends the signal <sig> to all pids found in <oldpids> */
686static void tell_old_pids(int sig)
687{
688 int p;
689 for (p = 0; p < nb_oldpids; p++)
690 kill(oldpids[p], sig);
691}
692
Willy Tarreau4f60f162007-04-08 16:39:58 +0200693/*
694 * Runs the polling loop
695 *
696 * FIXME:
697 * - we still use 'listeners' to check whether we want to stop or not.
698 *
699 */
700void run_poll_loop()
701{
Willy Tarreaud825eef2007-05-12 22:35:00 +0200702 struct timeval next;
Willy Tarreau4f60f162007-04-08 16:39:58 +0200703
Willy Tarreaud825eef2007-05-12 22:35:00 +0200704 tv_now(&now);
Willy Tarreau4f60f162007-04-08 16:39:58 +0200705 while (1) {
Willy Tarreaud825eef2007-05-12 22:35:00 +0200706 process_runnable_tasks(&next);
Willy Tarreau4f60f162007-04-08 16:39:58 +0200707
708 /* stop when there's no connection left and we don't allow them anymore */
709 if (!actconn && listeners == 0)
710 break;
711
Willy Tarreaud825eef2007-05-12 22:35:00 +0200712 cur_poller.poll(&cur_poller, &next);
Willy Tarreau4f60f162007-04-08 16:39:58 +0200713 }
714}
715
716
Willy Tarreaubaaee002006-06-26 02:48:02 +0200717int main(int argc, char **argv)
718{
719 int err, retry;
720 struct rlimit limit;
721 FILE *pidfile = NULL;
722 init(argc, argv);
723
724 signal(SIGQUIT, dump);
725 signal(SIGUSR1, sig_soft_stop);
726 signal(SIGHUP, sig_dump_state);
727#ifdef DEBUG_MEMORY
728 signal(SIGINT, sig_int);
729 signal(SIGTERM, sig_term);
730#endif
731
732 /* on very high loads, a sigpipe sometimes happen just between the
733 * getsockopt() which tells "it's OK to write", and the following write :-(
734 */
735#ifndef MSG_NOSIGNAL
736 signal(SIGPIPE, SIG_IGN);
737#endif
738
739 /* We will loop at most 100 times with 10 ms delay each time.
740 * That's at most 1 second. We only send a signal to old pids
741 * if we cannot grab at least one port.
742 */
743 retry = MAX_START_RETRIES;
744 err = ERR_NONE;
745 while (retry >= 0) {
746 struct timeval w;
747 err = start_proxies(retry == 0 || nb_oldpids == 0);
748 if (err != ERR_RETRYABLE)
749 break;
750 if (nb_oldpids == 0)
751 break;
752
753 /* FIXME-20060514: Solaris and OpenBSD do not support shutdown() on
754 * listening sockets. So on those platforms, it would be wiser to
755 * simply send SIGUSR1, which will not be undoable.
756 */
757 tell_old_pids(SIGTTOU);
758 /* give some time to old processes to stop listening */
759 w.tv_sec = 0;
760 w.tv_usec = 10*1000;
761 select(0, NULL, NULL, NULL, &w);
762 retry--;
763 }
764
765 /* Note: start_proxies() sends an alert when it fails. */
766 if (err != ERR_NONE) {
767 if (retry != MAX_START_RETRIES && nb_oldpids)
768 tell_old_pids(SIGTTIN);
769 exit(1);
770 }
771
772 if (listeners == 0) {
773 Alert("[%s.main()] No enabled listener found (check the <listen> keywords) ! Exiting.\n", argv[0]);
774 /* Note: we don't have to send anything to the old pids because we
775 * never stopped them. */
776 exit(1);
777 }
778
779 /* prepare pause/play signals */
780 signal(SIGTTOU, sig_pause);
781 signal(SIGTTIN, sig_listen);
782
783 if (global.mode & MODE_DAEMON) {
784 global.mode &= ~MODE_VERBOSE;
785 global.mode |= MODE_QUIET;
786 }
787
788 /* MODE_QUIET can inhibit alerts and warnings below this line */
789
790 global.mode &= ~MODE_STARTING;
791 if ((global.mode & MODE_QUIET) && !(global.mode & MODE_VERBOSE)) {
792 /* detach from the tty */
793 fclose(stdin); fclose(stdout); fclose(stderr);
794 close(0); close(1); close(2);
795 }
796
797 /* open log & pid files before the chroot */
798 if (global.mode & MODE_DAEMON && global.pidfile != NULL) {
799 int pidfd;
800 unlink(global.pidfile);
801 pidfd = open(global.pidfile, O_CREAT | O_WRONLY | O_TRUNC, 0644);
802 if (pidfd < 0) {
803 Alert("[%s.main()] Cannot create pidfile %s\n", argv[0], global.pidfile);
804 if (nb_oldpids)
805 tell_old_pids(SIGTTIN);
806 exit(1);
807 }
808 pidfile = fdopen(pidfd, "w");
809 }
810
811 /* chroot if needed */
812 if (global.chroot != NULL) {
813 if (chroot(global.chroot) == -1) {
814 Alert("[%s.main()] Cannot chroot(%s).\n", argv[0], global.chroot);
815 if (nb_oldpids)
816 tell_old_pids(SIGTTIN);
817 }
818 chdir("/");
819 }
820
821 /* ulimits */
822 if (!global.rlimit_nofile)
823 global.rlimit_nofile = global.maxsock;
824
825 if (global.rlimit_nofile) {
826 limit.rlim_cur = limit.rlim_max = global.rlimit_nofile;
827 if (setrlimit(RLIMIT_NOFILE, &limit) == -1) {
828 Warning("[%s.main()] Cannot raise FD limit to %d.\n", argv[0], global.rlimit_nofile);
829 }
830 }
831
832 if (global.rlimit_memmax) {
833 limit.rlim_cur = limit.rlim_max =
834 global.rlimit_memmax * 1048576 / global.nbproc;
835#ifdef RLIMIT_AS
836 if (setrlimit(RLIMIT_AS, &limit) == -1) {
837 Warning("[%s.main()] Cannot fix MEM limit to %d megs.\n",
838 argv[0], global.rlimit_memmax);
839 }
840#else
841 if (setrlimit(RLIMIT_DATA, &limit) == -1) {
842 Warning("[%s.main()] Cannot fix MEM limit to %d megs.\n",
843 argv[0], global.rlimit_memmax);
844 }
845#endif
846 }
847
Willy Tarreau6d1a9882007-01-07 02:03:04 +0100848#ifdef CONFIG_HAP_TCPSPLICE
849 if (global.last_checks & LSTCHK_TCPSPLICE) {
850 if (tcp_splice_start() < 0) {
851 Alert("[%s.main()] Cannot enable tcp_splice.\n"
852 " Make sure you have enough permissions and that the module is loadable.\n"
853 " Alternatively, you may disable the 'tcpsplice' options in the configuration.\n"
854 "", argv[0], global.gid);
855 exit(1);
856 }
857 }
858#endif
859
Willy Tarreaub38651a2007-03-24 17:24:39 +0100860#ifdef CONFIG_HAP_CTTPROXY
861 if (global.last_checks & LSTCHK_CTTPROXY) {
862 int ret;
863
864 ret = check_cttproxy_version();
865 if (ret < 0) {
866 Alert("[%s.main()] Cannot enable cttproxy.\n%s",
867 argv[0],
868 (ret == -1) ? " Incorrect module version.\n"
869 : " Make sure you have enough permissions and that the module is loaded.\n");
870 exit(1);
871 }
872 }
873#endif
874
875 if ((global.last_checks & LSTCHK_NETADM) && global.uid) {
876 Alert("[%s.main()] Some configuration options require full privileges, so global.uid cannot be changed.\n"
877 "", argv[0], global.gid);
878 exit(1);
879 }
880
Willy Tarreaubaaee002006-06-26 02:48:02 +0200881 if (nb_oldpids)
882 tell_old_pids(oldpids_sig);
883
884 /* Note that any error at this stage will be fatal because we will not
885 * be able to restart the old pids.
886 */
887
888 /* setgid / setuid */
889 if (global.gid && setgid(global.gid) == -1) {
890 Alert("[%s.main()] Cannot set gid %d.\n", argv[0], global.gid);
891 exit(1);
892 }
893
894 if (global.uid && setuid(global.uid) == -1) {
895 Alert("[%s.main()] Cannot set uid %d.\n", argv[0], global.uid);
896 exit(1);
897 }
898
899 /* check ulimits */
900 limit.rlim_cur = limit.rlim_max = 0;
901 getrlimit(RLIMIT_NOFILE, &limit);
902 if (limit.rlim_cur < global.maxsock) {
903 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",
904 argv[0], limit.rlim_cur, global.maxconn, global.maxsock, global.maxsock);
905 }
906
907 if (global.mode & MODE_DAEMON) {
908 int ret = 0;
909 int proc;
910
911 /* the father launches the required number of processes */
912 for (proc = 0; proc < global.nbproc; proc++) {
913 ret = fork();
914 if (ret < 0) {
915 Alert("[%s.main()] Cannot fork.\n", argv[0]);
916 if (nb_oldpids)
917 exit(1); /* there has been an error */
918 }
919 else if (ret == 0) /* child breaks here */
920 break;
921 if (pidfile != NULL) {
922 fprintf(pidfile, "%d\n", ret);
923 fflush(pidfile);
924 }
925 }
926 /* close the pidfile both in children and father */
927 if (pidfile != NULL)
928 fclose(pidfile);
929 free(global.pidfile);
930
931 if (proc == global.nbproc)
932 exit(0); /* parent must leave */
933
934 /* if we're NOT in QUIET mode, we should now close the 3 first FDs to ensure
935 * that we can detach from the TTY. We MUST NOT do it in other cases since
936 * it would have already be done, and 0-2 would have been affected to listening
937 * sockets
938 */
939 if (!(global.mode & MODE_QUIET)) {
940 /* detach from the tty */
941 fclose(stdin); fclose(stdout); fclose(stderr);
942 close(0); close(1); close(2); /* close all fd's */
943 global.mode |= MODE_QUIET; /* ensure that we won't say anything from now */
944 }
945 pid = getpid(); /* update child's pid */
946 setsid();
Willy Tarreau2ff76222007-04-09 19:29:56 +0200947 fork_poller();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200948 }
949
Willy Tarreau4f60f162007-04-08 16:39:58 +0200950 /*
951 * That's it : the central polling loop. Run until we stop.
952 */
953 run_poll_loop();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200954
955 /* Free all Hash Keys and all Hash elements */
956 appsession_cleanup();
957 /* Do some cleanup */
958 deinit();
959
960 exit(0);
961}
962
963
964/*
965 * Local variables:
966 * c-indent-level: 8
967 * c-basic-offset: 8
968 * End:
969 */