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