blob: e0e42b23fccf5400428d226603217482a70a36ad [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
Willy Tarreau1db37712007-06-03 17:16:49 +0200523 if (global.tune.maxpollevents <= 0)
524 global.tune.maxpollevents = MAX_POLL_EVENTS;
525
Willy Tarreaubaaee002006-06-26 02:48:02 +0200526 if (arg_mode & (MODE_DEBUG | MODE_FOREGROUND)) {
527 /* command line debug mode inhibits configuration mode */
528 global.mode &= ~(MODE_DAEMON | MODE_QUIET);
529 }
530 global.mode |= (arg_mode & (MODE_DAEMON | MODE_FOREGROUND | MODE_QUIET |
531 MODE_VERBOSE | MODE_DEBUG | MODE_STATS | MODE_LOG));
532
533 if ((global.mode & MODE_DEBUG) && (global.mode & (MODE_DAEMON | MODE_QUIET))) {
534 Warning("<debug> mode incompatible with <quiet> and <daemon>. Keeping <debug> only.\n");
535 global.mode &= ~(MODE_DAEMON | MODE_QUIET);
536 }
537
538 if ((global.nbproc > 1) && !(global.mode & MODE_DAEMON)) {
539 if (!(global.mode & (MODE_FOREGROUND | MODE_DEBUG)))
540 Warning("<nbproc> is only meaningful in daemon mode. Setting limit to 1 process.\n");
541 global.nbproc = 1;
542 }
543
544 if (global.nbproc < 1)
545 global.nbproc = 1;
546
Willy Tarreaubaaee002006-06-26 02:48:02 +0200547 fdtab = (struct fdtab *)calloc(1,
548 sizeof(struct fdtab) * (global.maxsock));
549 for (i = 0; i < global.maxsock; i++) {
550 fdtab[i].state = FD_STCLOSE;
551 }
Willy Tarreau4f60f162007-04-08 16:39:58 +0200552
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200553 /*
554 * Note: we could register external pollers here.
555 * Built-in pollers have been registered before main().
556 */
Willy Tarreau4f60f162007-04-08 16:39:58 +0200557
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200558 if (!(cfg_polling_mechanism & POLL_USE_KQUEUE))
559 disable_poller("kqueue");
560
Willy Tarreau4f60f162007-04-08 16:39:58 +0200561 if (!(cfg_polling_mechanism & POLL_USE_EPOLL))
562 disable_poller("epoll");
563
Willy Tarreaude99e992007-04-16 00:53:59 +0200564 if (!(cfg_polling_mechanism & POLL_USE_SEPOLL))
565 disable_poller("sepoll");
566
Willy Tarreau4f60f162007-04-08 16:39:58 +0200567 if (!(cfg_polling_mechanism & POLL_USE_POLL))
568 disable_poller("poll");
569
570 if (!(cfg_polling_mechanism & POLL_USE_SELECT))
571 disable_poller("select");
572
573 /* Note: we could disable any poller by name here */
574
Willy Tarreau2ff76222007-04-09 19:29:56 +0200575 if (global.mode & (MODE_VERBOSE|MODE_DEBUG))
576 list_pollers(stderr);
577
Willy Tarreau4f60f162007-04-08 16:39:58 +0200578 if (!init_pollers()) {
Willy Tarreau2ff76222007-04-09 19:29:56 +0200579 Alert("No polling mechanism available.\n");
Willy Tarreau4f60f162007-04-08 16:39:58 +0200580 exit(1);
581 }
Willy Tarreau2ff76222007-04-09 19:29:56 +0200582 if (global.mode & (MODE_VERBOSE|MODE_DEBUG)) {
583 printf("Using %s() as the polling mechanism.\n", cur_poller.name);
Willy Tarreau4f60f162007-04-08 16:39:58 +0200584 }
585
Willy Tarreaubaaee002006-06-26 02:48:02 +0200586}
587
588void deinit(void)
589{
Willy Tarreau4d2d0982007-05-14 00:39:29 +0200590 struct proxy *p = proxy, *p0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200591 struct cap_hdr *h,*h_next;
592 struct server *s,*s_next;
593 struct listener *l,*l_next;
594
595 while (p) {
596 if (p->id)
597 free(p->id);
598
599 if (p->check_req)
600 free(p->check_req);
601
602 if (p->cookie_name)
603 free(p->cookie_name);
604
605 if (p->capture_name)
606 free(p->capture_name);
607
608 /* only strup if the user have set in config.
609 When should we free it?!
610 if (p->errmsg.msg400) free(p->errmsg.msg400);
611 if (p->errmsg.msg403) free(p->errmsg.msg403);
612 if (p->errmsg.msg408) free(p->errmsg.msg408);
613 if (p->errmsg.msg500) free(p->errmsg.msg500);
614 if (p->errmsg.msg502) free(p->errmsg.msg502);
615 if (p->errmsg.msg503) free(p->errmsg.msg503);
616 if (p->errmsg.msg504) free(p->errmsg.msg504);
617 */
618 if (p->appsession_name)
619 free(p->appsession_name);
620
621 h = p->req_cap;
622 while (h) {
623 h_next = h->next;
624 if (h->name)
625 free(h->name);
Willy Tarreaucf7f3202007-05-13 22:46:04 +0200626 pool_destroy2(h->pool);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200627 free(h);
628 h = h_next;
629 }/* end while(h) */
630
631 h = p->rsp_cap;
632 while (h) {
633 h_next = h->next;
634 if (h->name)
635 free(h->name);
636
Willy Tarreaucf7f3202007-05-13 22:46:04 +0200637 pool_destroy2(h->pool);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200638 free(h);
639 h = h_next;
640 }/* end while(h) */
641
642 s = p->srv;
643 while (s) {
644 s_next = s->next;
645 if (s->id)
646 free(s->id);
647
648 if (s->cookie)
649 free(s->cookie);
650
651 free(s);
652 s = s_next;
653 }/* end while(s) */
654
655 l = p->listen;
656 while (l) {
657 l_next = l->next;
658 free(l);
659 l = l_next;
660 }/* end while(l) */
661
Willy Tarreaucf7f3202007-05-13 22:46:04 +0200662 pool_destroy2(p->req_cap_pool);
663 pool_destroy2(p->rsp_cap_pool);
Willy Tarreau4d2d0982007-05-14 00:39:29 +0200664 p0 = p;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200665 p = p->next;
Willy Tarreau4d2d0982007-05-14 00:39:29 +0200666 free(p0);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200667 }/* end while(p) */
668
669 if (global.chroot) free(global.chroot);
670 if (global.pidfile) free(global.pidfile);
671
Willy Tarreaubaaee002006-06-26 02:48:02 +0200672 if (fdtab) free(fdtab);
673
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200674 pool_destroy2(pool2_session);
Willy Tarreau7341d942007-05-13 19:56:02 +0200675 pool_destroy2(pool2_buffer);
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200676 pool_destroy2(pool2_requri);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200677 pool_destroy2(pool2_task);
Willy Tarreau086b3b42007-05-13 21:45:51 +0200678 pool_destroy2(pool2_capture);
Willy Tarreau63963c62007-05-13 21:29:55 +0200679 pool_destroy2(pool2_appsess);
Willy Tarreaue4d7e552007-05-13 20:19:55 +0200680 pool_destroy2(pool2_pendconn);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200681
682 if (have_appsession) {
Willy Tarreau63963c62007-05-13 21:29:55 +0200683 pool_destroy2(apools.serverid);
684 pool_destroy2(apools.sessid);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200685 }
686} /* end deinit() */
687
688/* sends the signal <sig> to all pids found in <oldpids> */
689static void tell_old_pids(int sig)
690{
691 int p;
692 for (p = 0; p < nb_oldpids; p++)
693 kill(oldpids[p], sig);
694}
695
Willy Tarreau4f60f162007-04-08 16:39:58 +0200696/*
697 * Runs the polling loop
698 *
699 * FIXME:
700 * - we still use 'listeners' to check whether we want to stop or not.
701 *
702 */
703void run_poll_loop()
704{
Willy Tarreaud825eef2007-05-12 22:35:00 +0200705 struct timeval next;
Willy Tarreau4f60f162007-04-08 16:39:58 +0200706
Willy Tarreaud825eef2007-05-12 22:35:00 +0200707 tv_now(&now);
Willy Tarreau4f60f162007-04-08 16:39:58 +0200708 while (1) {
Willy Tarreaud825eef2007-05-12 22:35:00 +0200709 process_runnable_tasks(&next);
Willy Tarreau4f60f162007-04-08 16:39:58 +0200710
711 /* stop when there's no connection left and we don't allow them anymore */
712 if (!actconn && listeners == 0)
713 break;
714
Willy Tarreaud825eef2007-05-12 22:35:00 +0200715 cur_poller.poll(&cur_poller, &next);
Willy Tarreau4f60f162007-04-08 16:39:58 +0200716 }
717}
718
719
Willy Tarreaubaaee002006-06-26 02:48:02 +0200720int main(int argc, char **argv)
721{
722 int err, retry;
723 struct rlimit limit;
724 FILE *pidfile = NULL;
725 init(argc, argv);
726
727 signal(SIGQUIT, dump);
728 signal(SIGUSR1, sig_soft_stop);
729 signal(SIGHUP, sig_dump_state);
730#ifdef DEBUG_MEMORY
731 signal(SIGINT, sig_int);
732 signal(SIGTERM, sig_term);
733#endif
734
735 /* on very high loads, a sigpipe sometimes happen just between the
736 * getsockopt() which tells "it's OK to write", and the following write :-(
737 */
738#ifndef MSG_NOSIGNAL
739 signal(SIGPIPE, SIG_IGN);
740#endif
741
742 /* We will loop at most 100 times with 10 ms delay each time.
743 * That's at most 1 second. We only send a signal to old pids
744 * if we cannot grab at least one port.
745 */
746 retry = MAX_START_RETRIES;
747 err = ERR_NONE;
748 while (retry >= 0) {
749 struct timeval w;
750 err = start_proxies(retry == 0 || nb_oldpids == 0);
751 if (err != ERR_RETRYABLE)
752 break;
753 if (nb_oldpids == 0)
754 break;
755
756 /* FIXME-20060514: Solaris and OpenBSD do not support shutdown() on
757 * listening sockets. So on those platforms, it would be wiser to
758 * simply send SIGUSR1, which will not be undoable.
759 */
760 tell_old_pids(SIGTTOU);
761 /* give some time to old processes to stop listening */
762 w.tv_sec = 0;
763 w.tv_usec = 10*1000;
764 select(0, NULL, NULL, NULL, &w);
765 retry--;
766 }
767
768 /* Note: start_proxies() sends an alert when it fails. */
769 if (err != ERR_NONE) {
770 if (retry != MAX_START_RETRIES && nb_oldpids)
771 tell_old_pids(SIGTTIN);
772 exit(1);
773 }
774
775 if (listeners == 0) {
776 Alert("[%s.main()] No enabled listener found (check the <listen> keywords) ! Exiting.\n", argv[0]);
777 /* Note: we don't have to send anything to the old pids because we
778 * never stopped them. */
779 exit(1);
780 }
781
782 /* prepare pause/play signals */
783 signal(SIGTTOU, sig_pause);
784 signal(SIGTTIN, sig_listen);
785
786 if (global.mode & MODE_DAEMON) {
787 global.mode &= ~MODE_VERBOSE;
788 global.mode |= MODE_QUIET;
789 }
790
791 /* MODE_QUIET can inhibit alerts and warnings below this line */
792
793 global.mode &= ~MODE_STARTING;
794 if ((global.mode & MODE_QUIET) && !(global.mode & MODE_VERBOSE)) {
795 /* detach from the tty */
796 fclose(stdin); fclose(stdout); fclose(stderr);
797 close(0); close(1); close(2);
798 }
799
800 /* open log & pid files before the chroot */
801 if (global.mode & MODE_DAEMON && global.pidfile != NULL) {
802 int pidfd;
803 unlink(global.pidfile);
804 pidfd = open(global.pidfile, O_CREAT | O_WRONLY | O_TRUNC, 0644);
805 if (pidfd < 0) {
806 Alert("[%s.main()] Cannot create pidfile %s\n", argv[0], global.pidfile);
807 if (nb_oldpids)
808 tell_old_pids(SIGTTIN);
809 exit(1);
810 }
811 pidfile = fdopen(pidfd, "w");
812 }
813
814 /* chroot if needed */
815 if (global.chroot != NULL) {
816 if (chroot(global.chroot) == -1) {
817 Alert("[%s.main()] Cannot chroot(%s).\n", argv[0], global.chroot);
818 if (nb_oldpids)
819 tell_old_pids(SIGTTIN);
820 }
821 chdir("/");
822 }
823
824 /* ulimits */
825 if (!global.rlimit_nofile)
826 global.rlimit_nofile = global.maxsock;
827
828 if (global.rlimit_nofile) {
829 limit.rlim_cur = limit.rlim_max = global.rlimit_nofile;
830 if (setrlimit(RLIMIT_NOFILE, &limit) == -1) {
831 Warning("[%s.main()] Cannot raise FD limit to %d.\n", argv[0], global.rlimit_nofile);
832 }
833 }
834
835 if (global.rlimit_memmax) {
836 limit.rlim_cur = limit.rlim_max =
837 global.rlimit_memmax * 1048576 / global.nbproc;
838#ifdef RLIMIT_AS
839 if (setrlimit(RLIMIT_AS, &limit) == -1) {
840 Warning("[%s.main()] Cannot fix MEM limit to %d megs.\n",
841 argv[0], global.rlimit_memmax);
842 }
843#else
844 if (setrlimit(RLIMIT_DATA, &limit) == -1) {
845 Warning("[%s.main()] Cannot fix MEM limit to %d megs.\n",
846 argv[0], global.rlimit_memmax);
847 }
848#endif
849 }
850
Willy Tarreau6d1a9882007-01-07 02:03:04 +0100851#ifdef CONFIG_HAP_TCPSPLICE
852 if (global.last_checks & LSTCHK_TCPSPLICE) {
853 if (tcp_splice_start() < 0) {
854 Alert("[%s.main()] Cannot enable tcp_splice.\n"
855 " Make sure you have enough permissions and that the module is loadable.\n"
856 " Alternatively, you may disable the 'tcpsplice' options in the configuration.\n"
857 "", argv[0], global.gid);
858 exit(1);
859 }
860 }
861#endif
862
Willy Tarreaub38651a2007-03-24 17:24:39 +0100863#ifdef CONFIG_HAP_CTTPROXY
864 if (global.last_checks & LSTCHK_CTTPROXY) {
865 int ret;
866
867 ret = check_cttproxy_version();
868 if (ret < 0) {
869 Alert("[%s.main()] Cannot enable cttproxy.\n%s",
870 argv[0],
871 (ret == -1) ? " Incorrect module version.\n"
872 : " Make sure you have enough permissions and that the module is loaded.\n");
873 exit(1);
874 }
875 }
876#endif
877
878 if ((global.last_checks & LSTCHK_NETADM) && global.uid) {
879 Alert("[%s.main()] Some configuration options require full privileges, so global.uid cannot be changed.\n"
880 "", argv[0], global.gid);
881 exit(1);
882 }
883
Willy Tarreaubaaee002006-06-26 02:48:02 +0200884 if (nb_oldpids)
885 tell_old_pids(oldpids_sig);
886
887 /* Note that any error at this stage will be fatal because we will not
888 * be able to restart the old pids.
889 */
890
891 /* setgid / setuid */
892 if (global.gid && setgid(global.gid) == -1) {
893 Alert("[%s.main()] Cannot set gid %d.\n", argv[0], global.gid);
894 exit(1);
895 }
896
897 if (global.uid && setuid(global.uid) == -1) {
898 Alert("[%s.main()] Cannot set uid %d.\n", argv[0], global.uid);
899 exit(1);
900 }
901
902 /* check ulimits */
903 limit.rlim_cur = limit.rlim_max = 0;
904 getrlimit(RLIMIT_NOFILE, &limit);
905 if (limit.rlim_cur < global.maxsock) {
906 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",
907 argv[0], limit.rlim_cur, global.maxconn, global.maxsock, global.maxsock);
908 }
909
910 if (global.mode & MODE_DAEMON) {
911 int ret = 0;
912 int proc;
913
914 /* the father launches the required number of processes */
915 for (proc = 0; proc < global.nbproc; proc++) {
916 ret = fork();
917 if (ret < 0) {
918 Alert("[%s.main()] Cannot fork.\n", argv[0]);
919 if (nb_oldpids)
920 exit(1); /* there has been an error */
921 }
922 else if (ret == 0) /* child breaks here */
923 break;
924 if (pidfile != NULL) {
925 fprintf(pidfile, "%d\n", ret);
926 fflush(pidfile);
927 }
928 }
929 /* close the pidfile both in children and father */
930 if (pidfile != NULL)
931 fclose(pidfile);
932 free(global.pidfile);
933
934 if (proc == global.nbproc)
935 exit(0); /* parent must leave */
936
937 /* if we're NOT in QUIET mode, we should now close the 3 first FDs to ensure
938 * that we can detach from the TTY. We MUST NOT do it in other cases since
939 * it would have already be done, and 0-2 would have been affected to listening
940 * sockets
941 */
942 if (!(global.mode & MODE_QUIET)) {
943 /* detach from the tty */
944 fclose(stdin); fclose(stdout); fclose(stderr);
945 close(0); close(1); close(2); /* close all fd's */
946 global.mode |= MODE_QUIET; /* ensure that we won't say anything from now */
947 }
948 pid = getpid(); /* update child's pid */
949 setsid();
Willy Tarreau2ff76222007-04-09 19:29:56 +0200950 fork_poller();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200951 }
952
Willy Tarreau4f60f162007-04-08 16:39:58 +0200953 /*
954 * That's it : the central polling loop. Run until we stop.
955 */
956 run_poll_loop();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200957
958 /* Free all Hash Keys and all Hash elements */
959 appsession_cleanup();
960 /* Do some cleanup */
961 deinit();
962
963 exit(0);
964}
965
966
967/*
968 * Local variables:
969 * c-indent-level: 8
970 * c-basic-offset: 8
971 * End:
972 */