blob: c13c392d6c942ee79b6ea00ed5ae70c2ca3738df [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 Tarreaudd815982007-10-16 12:25:14 +020088#include <proto/protocols.h>
Willy Tarreau80587432006-12-24 17:47:20 +010089#include <proto/proto_http.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020090#include <proto/proxy.h>
91#include <proto/queue.h>
92#include <proto/server.h>
Willy Tarreauc6ca1a02007-05-13 19:43:47 +020093#include <proto/session.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020094#include <proto/stream_sock.h>
95#include <proto/task.h>
96
Willy Tarreau6d1a9882007-01-07 02:03:04 +010097#ifdef CONFIG_HAP_TCPSPLICE
98#include <libtcpsplice.h>
99#endif
100
Willy Tarreaub38651a2007-03-24 17:24:39 +0100101#ifdef CONFIG_HAP_CTTPROXY
102#include <proto/cttproxy.h>
103#endif
104
Willy Tarreaubaaee002006-06-26 02:48:02 +0200105/*********************************************************************/
106
107/*********************************************************************/
108
109char *cfg_cfgfile = NULL; /* configuration file */
110char *progname = NULL; /* program name */
111int pid; /* current process id */
112
113/* global options */
114struct global global = {
115 logfac1 : -1,
116 logfac2 : -1,
117 loglev1 : 7, /* max syslog level : debug */
118 loglev2 : 7,
119 /* others NULL OK */
120};
121
122/*********************************************************************/
123
124int stopping; /* non zero means stopping in progress */
125
126/* Here we store informations about the pids of the processes we may pause
127 * or kill. We will send them a signal every 10 ms until we can bind to all
128 * our ports. With 200 retries, that's about 2 seconds.
129 */
130#define MAX_START_RETRIES 200
131static int nb_oldpids = 0;
132static int *oldpids = NULL;
133static int oldpids_sig; /* use USR1 or TERM */
134
135/* this is used to drain data, and as a temporary buffer for sprintf()... */
136char trash[BUFSIZE];
137
138const int zero = 0;
139const int one = 1;
Alexandre Cassen87ea5482007-10-11 20:48:58 +0200140const struct linger nolinger = { .l_onoff = 1, .l_linger = 0 };
Willy Tarreaubaaee002006-06-26 02:48:02 +0200141
142/*
143 * Syslog facilities and levels. Conforming to RFC3164.
144 */
145
146#define MAX_HOSTNAME_LEN 32
147static char hostname[MAX_HOSTNAME_LEN] = "";
148
149
150/*********************************************************************/
151/* general purpose functions ***************************************/
152/*********************************************************************/
153
154void display_version()
155{
156 printf("HA-Proxy version " HAPROXY_VERSION " " HAPROXY_DATE"\n");
Willy Tarreau49e1ee82007-01-22 00:56:46 +0100157 printf("Copyright 2000-2007 Willy Tarreau <w@1wt.eu>\n\n");
Willy Tarreaubaaee002006-06-26 02:48:02 +0200158}
159
160/*
161 * This function prints the command line usage and exits
162 */
163void usage(char *name)
164{
165 display_version();
166 fprintf(stderr,
167 "Usage : %s -f <cfgfile> [ -vdV"
168 "D ] [ -n <maxconn> ] [ -N <maxpconn> ]\n"
169 " [ -p <pidfile> ] [ -m <max megs> ]\n"
170 " -v displays version\n"
171 " -d enters debug mode ; -db only disables background mode.\n"
172 " -V enters verbose mode (disables quiet mode)\n"
173 " -D goes daemon ; implies -q\n"
174 " -q quiet mode : don't display messages\n"
175 " -c check mode : only check config file and exit\n"
176 " -n sets the maximum total # of connections (%d)\n"
177 " -m limits the usable amount of memory (in MB)\n"
178 " -N sets the default, per-proxy maximum # of connections (%d)\n"
179 " -p writes pids of all children to this file\n"
180#if defined(ENABLE_EPOLL)
181 " -de disables epoll() usage even when available\n"
182#endif
Willy Tarreaude99e992007-04-16 00:53:59 +0200183#if defined(ENABLE_SEPOLL)
184 " -ds disables speculative epoll() usage even when available\n"
185#endif
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200186#if defined(ENABLE_KQUEUE)
187 " -dk disables kqueue() usage even when available\n"
188#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200189#if defined(ENABLE_POLL)
190 " -dp disables poll() usage even when available\n"
191#endif
192 " -sf/-st [pid ]* finishes/terminates old pids. Must be last arguments.\n"
193 "\n",
194 name, DEFAULT_MAXCONN, cfg_maxpconn);
195 exit(1);
196}
197
198
199
200/*********************************************************************/
201/* more specific functions ***************************************/
202/*********************************************************************/
203
204/*
205 * upon SIGUSR1, let's have a soft stop.
206 */
207void sig_soft_stop(int sig)
208{
209 soft_stop();
Willy Tarreau4d2d0982007-05-14 00:39:29 +0200210 pool_gc2();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200211 signal(sig, SIG_IGN);
212}
213
214/*
215 * upon SIGTTOU, we pause everything
216 */
217void sig_pause(int sig)
218{
219 pause_proxies();
Willy Tarreau4d2d0982007-05-14 00:39:29 +0200220 pool_gc2();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200221 signal(sig, sig_pause);
222}
223
224/*
225 * upon SIGTTIN, let's have a soft stop.
226 */
227void sig_listen(int sig)
228{
229 listen_proxies();
230 signal(sig, sig_listen);
231}
232
233/*
234 * this function dumps every server's state when the process receives SIGHUP.
235 */
236void sig_dump_state(int sig)
237{
238 struct proxy *p = proxy;
239
240 Warning("SIGHUP received, dumping servers states.\n");
241 while (p) {
242 struct server *s = p->srv;
243
244 send_log(p, LOG_NOTICE, "SIGHUP received, dumping servers states for proxy %s.\n", p->id);
245 while (s) {
246 snprintf(trash, sizeof(trash),
247 "SIGHUP: Server %s/%s is %s. Conn: %d act, %d pend, %d tot.",
248 p->id, s->id,
249 (s->state & SRV_RUNNING) ? "UP" : "DOWN",
250 s->cur_sess, s->nbpend, s->cum_sess);
251 Warning("%s\n", trash);
252 send_log(p, LOG_NOTICE, "%s\n", trash);
253 s = s->next;
254 }
255
Willy Tarreau5fcc8f12007-09-17 11:27:09 +0200256 /* FIXME: those info are a bit outdated. We should be able to distinguish between FE and BE. */
257 if (!p->srv) {
258 snprintf(trash, sizeof(trash),
259 "SIGHUP: Proxy %s has no servers. Conn: act(FE+BE): %d+%d, %d pend (%d unass), tot(FE+BE): %d+%d.",
260 p->id,
261 p->feconn, p->beconn, p->totpend, p->nbpend, p->cum_feconn, p->cum_beconn);
262 } else if (p->srv_act == 0) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200263 snprintf(trash, sizeof(trash),
Willy Tarreauf1221aa2006-12-17 22:14:12 +0100264 "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 +0200265 p->id,
266 (p->srv_bck) ? "is running on backup servers" : "has no server available",
Willy Tarreauf1221aa2006-12-17 22:14:12 +0100267 p->feconn, p->beconn, p->totpend, p->nbpend, p->cum_feconn, p->cum_beconn);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200268 } else {
269 snprintf(trash, sizeof(trash),
270 "SIGHUP: Proxy %s has %d active servers and %d backup servers available."
Willy Tarreauf1221aa2006-12-17 22:14:12 +0100271 " Conn: act(FE+BE): %d+%d, %d pend (%d unass), tot(FE+BE): %d+%d.",
Willy Tarreaubaaee002006-06-26 02:48:02 +0200272 p->id, p->srv_act, p->srv_bck,
Willy Tarreauf1221aa2006-12-17 22:14:12 +0100273 p->feconn, p->beconn, p->totpend, p->nbpend, p->cum_feconn, p->cum_beconn);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200274 }
275 Warning("%s\n", trash);
276 send_log(p, LOG_NOTICE, "%s\n", trash);
277
278 p = p->next;
279 }
280 signal(sig, sig_dump_state);
281}
282
283void dump(int sig)
284{
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200285#if 0
Willy Tarreau964c9362007-01-07 00:38:00 +0100286 struct task *t;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200287 struct session *s;
Willy Tarreau964c9362007-01-07 00:38:00 +0100288 struct rb_node *node;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200289
Willy Tarreau964c9362007-01-07 00:38:00 +0100290 for(node = rb_first(&wait_queue[0]);
291 node != NULL; node = rb_next(node)) {
292 t = rb_entry(node, struct task, rb_node);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200293 s = t->context;
294 qfprintf(stderr,"[dump] wq: task %p, still %ld ms, "
295 "cli=%d, srv=%d, cr=%d, cw=%d, sr=%d, sw=%d, "
296 "req=%d, rep=%d, clifd=%d\n",
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200297 s, tv_ms_remain(&now, &t->expire),
Willy Tarreaubaaee002006-06-26 02:48:02 +0200298 s->cli_state,
299 s->srv_state,
Willy Tarreauf161a342007-04-08 16:59:42 +0200300 EV_FD_ISSET(s->cli_fd, DIR_RD),
301 EV_FD_ISSET(s->cli_fd, DIR_WR),
302 EV_FD_ISSET(s->srv_fd, DIR_RD),
303 EV_FD_ISSET(s->srv_fd, DIR_WR),
Willy Tarreaubaaee002006-06-26 02:48:02 +0200304 s->req->l, s->rep?s->rep->l:0, s->cli_fd
305 );
306 }
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200307#endif
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200308 /* dump memory usage then free everything possible */
309 dump_pools();
310 pool_gc2();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200311}
312
313#ifdef DEBUG_MEMORY
314static void fast_stop(void)
315{
316 struct proxy *p;
317 p = proxy;
318 while (p) {
319 p->grace = 0;
320 p = p->next;
321 }
322 soft_stop();
323}
324
325void sig_int(int sig)
326{
327 /* This would normally be a hard stop,
328 but we want to be sure about deallocation,
329 and so on, so we do a soft stop with
330 0 GRACE time
331 */
332 fast_stop();
Willy Tarreau4d2d0982007-05-14 00:39:29 +0200333 pool_gc2();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200334 /* If we are killed twice, we decide to die*/
335 signal(sig, SIG_DFL);
336}
337
338void sig_term(int sig)
339{
340 /* This would normally be a hard stop,
341 but we want to be sure about deallocation,
342 and so on, so we do a soft stop with
343 0 GRACE time
344 */
345 fast_stop();
Willy Tarreau4d2d0982007-05-14 00:39:29 +0200346 pool_gc2();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200347 /* If we are killed twice, we decide to die*/
348 signal(sig, SIG_DFL);
349}
350#endif
351
352
353/*
354 * This function initializes all the necessary variables. It only returns
355 * if everything is OK. If something fails, it exits.
356 */
357void init(int argc, char **argv)
358{
359 int i;
360 int arg_mode = 0; /* MODE_DEBUG, ... */
361 char *old_argv = *argv;
362 char *tmp;
363 char *cfg_pidfile = NULL;
364
365 if (1<<INTBITS != sizeof(int)*8) {
366 fprintf(stderr,
367 "Error: wrong architecture. Recompile so that sizeof(int)=%d\n",
368 (int)(sizeof(int)*8));
369 exit(1);
370 }
371
372 /*
373 * Initialize the previously static variables.
374 */
375
376 totalconn = actconn = maxfd = listeners = stopping = 0;
377
378
379#ifdef HAPROXY_MEMMAX
380 global.rlimit_memmax = HAPROXY_MEMMAX;
381#endif
382
383 /* initialize the libc's localtime structures once for all so that we
384 * won't be missing memory if we want to send alerts under OOM conditions.
Willy Tarreau2b35c952006-10-15 15:25:48 +0200385 * Also, the Alert() and Warning() functions need <now> to be initialized.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200386 */
387 tv_now(&now);
Willy Tarreaubf736132006-10-15 22:54:47 +0200388 localtime((time_t *)&now.tv_sec);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200389 start_date = now;
390
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200391 init_task();
392 init_session();
Willy Tarreaue4d7e552007-05-13 20:19:55 +0200393 init_buffer();
394 init_pendconn();
Willy Tarreau80587432006-12-24 17:47:20 +0100395 init_proto_http();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200396
397 cfg_polling_mechanism = POLL_USE_SELECT; /* select() is always available */
398#if defined(ENABLE_POLL)
399 cfg_polling_mechanism |= POLL_USE_POLL;
400#endif
401#if defined(ENABLE_EPOLL)
402 cfg_polling_mechanism |= POLL_USE_EPOLL;
403#endif
Willy Tarreaude99e992007-04-16 00:53:59 +0200404#if defined(ENABLE_SEPOLL)
405 cfg_polling_mechanism |= POLL_USE_SEPOLL;
406#endif
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200407#if defined(ENABLE_KQUEUE)
408 cfg_polling_mechanism |= POLL_USE_KQUEUE;
409#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200410
411 pid = getpid();
412 progname = *argv;
413 while ((tmp = strchr(progname, '/')) != NULL)
414 progname = tmp + 1;
415
416 argc--; argv++;
417 while (argc > 0) {
418 char *flag;
419
420 if (**argv == '-') {
421 flag = *argv+1;
422
423 /* 1 arg */
424 if (*flag == 'v') {
425 display_version();
426 exit(0);
427 }
428#if defined(ENABLE_EPOLL)
429 else if (*flag == 'd' && flag[1] == 'e')
430 cfg_polling_mechanism &= ~POLL_USE_EPOLL;
431#endif
Willy Tarreaude99e992007-04-16 00:53:59 +0200432#if defined(ENABLE_SEPOLL)
433 else if (*flag == 'd' && flag[1] == 's')
434 cfg_polling_mechanism &= ~POLL_USE_SEPOLL;
435#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200436#if defined(ENABLE_POLL)
437 else if (*flag == 'd' && flag[1] == 'p')
438 cfg_polling_mechanism &= ~POLL_USE_POLL;
439#endif
Willy Tarreau69cad1a2007-04-10 22:45:11 +0200440#if defined(ENABLE_KQUEUE)
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200441 else if (*flag == 'd' && flag[1] == 'k')
442 cfg_polling_mechanism &= ~POLL_USE_KQUEUE;
443#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200444 else if (*flag == 'V')
445 arg_mode |= MODE_VERBOSE;
446 else if (*flag == 'd' && flag[1] == 'b')
447 arg_mode |= MODE_FOREGROUND;
448 else if (*flag == 'd')
449 arg_mode |= MODE_DEBUG;
450 else if (*flag == 'c')
451 arg_mode |= MODE_CHECK;
452 else if (*flag == 'D')
453 arg_mode |= MODE_DAEMON | MODE_QUIET;
454 else if (*flag == 'q')
455 arg_mode |= MODE_QUIET;
456 else if (*flag == 's' && (flag[1] == 'f' || flag[1] == 't')) {
457 /* list of pids to finish ('f') or terminate ('t') */
458
459 if (flag[1] == 'f')
460 oldpids_sig = SIGUSR1; /* finish then exit */
461 else
462 oldpids_sig = SIGTERM; /* terminate immediately */
463 argv++; argc--;
464
465 if (argc > 0) {
466 oldpids = calloc(argc, sizeof(int));
467 while (argc > 0) {
468 oldpids[nb_oldpids] = atol(*argv);
469 if (oldpids[nb_oldpids] <= 0)
470 usage(old_argv);
471 argc--; argv++;
472 nb_oldpids++;
473 }
474 }
475 }
476 else { /* >=2 args */
477 argv++; argc--;
478 if (argc == 0)
479 usage(old_argv);
480
481 switch (*flag) {
482 case 'n' : cfg_maxconn = atol(*argv); break;
483 case 'm' : global.rlimit_memmax = atol(*argv); break;
484 case 'N' : cfg_maxpconn = atol(*argv); break;
485 case 'f' : cfg_cfgfile = *argv; break;
486 case 'p' : cfg_pidfile = *argv; break;
487 default: usage(old_argv);
488 }
489 }
490 }
491 else
492 usage(old_argv);
493 argv++; argc--;
494 }
495
496 global.mode = MODE_STARTING | /* during startup, we want most of the alerts */
497 (arg_mode & (MODE_DAEMON | MODE_FOREGROUND | MODE_VERBOSE
498 | MODE_QUIET | MODE_CHECK | MODE_DEBUG));
499
500 if (!cfg_cfgfile)
501 usage(old_argv);
502
503 gethostname(hostname, MAX_HOSTNAME_LEN);
504
505 have_appsession = 0;
506 global.maxsock = 10; /* reserve 10 fds ; will be incremented by socket eaters */
507 if (readcfgfile(cfg_cfgfile) < 0) {
508 Alert("Error reading configuration file : %s\n", cfg_cfgfile);
509 exit(1);
510 }
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +0200511
Willy Tarreaubaaee002006-06-26 02:48:02 +0200512 if (have_appsession)
513 appsession_init();
514
515 if (global.mode & MODE_CHECK) {
516 qfprintf(stdout, "Configuration file is valid : %s\n", cfg_cfgfile);
517 exit(0);
518 }
519
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +0200520 if (start_checks() < 0)
521 exit(1);
522
Willy Tarreaubaaee002006-06-26 02:48:02 +0200523 if (cfg_maxconn > 0)
524 global.maxconn = cfg_maxconn;
525
526 if (cfg_pidfile) {
527 if (global.pidfile)
528 free(global.pidfile);
529 global.pidfile = strdup(cfg_pidfile);
530 }
531
532 if (global.maxconn == 0)
533 global.maxconn = DEFAULT_MAXCONN;
534
535 global.maxsock += global.maxconn * 2; /* each connection needs two sockets */
536
Willy Tarreau1db37712007-06-03 17:16:49 +0200537 if (global.tune.maxpollevents <= 0)
538 global.tune.maxpollevents = MAX_POLL_EVENTS;
539
Willy Tarreaubaaee002006-06-26 02:48:02 +0200540 if (arg_mode & (MODE_DEBUG | MODE_FOREGROUND)) {
541 /* command line debug mode inhibits configuration mode */
542 global.mode &= ~(MODE_DAEMON | MODE_QUIET);
543 }
544 global.mode |= (arg_mode & (MODE_DAEMON | MODE_FOREGROUND | MODE_QUIET |
545 MODE_VERBOSE | MODE_DEBUG | MODE_STATS | MODE_LOG));
546
547 if ((global.mode & MODE_DEBUG) && (global.mode & (MODE_DAEMON | MODE_QUIET))) {
548 Warning("<debug> mode incompatible with <quiet> and <daemon>. Keeping <debug> only.\n");
549 global.mode &= ~(MODE_DAEMON | MODE_QUIET);
550 }
551
552 if ((global.nbproc > 1) && !(global.mode & MODE_DAEMON)) {
553 if (!(global.mode & (MODE_FOREGROUND | MODE_DEBUG)))
554 Warning("<nbproc> is only meaningful in daemon mode. Setting limit to 1 process.\n");
555 global.nbproc = 1;
556 }
557
558 if (global.nbproc < 1)
559 global.nbproc = 1;
560
Willy Tarreaubaaee002006-06-26 02:48:02 +0200561 fdtab = (struct fdtab *)calloc(1,
562 sizeof(struct fdtab) * (global.maxsock));
563 for (i = 0; i < global.maxsock; i++) {
564 fdtab[i].state = FD_STCLOSE;
565 }
Willy Tarreau4f60f162007-04-08 16:39:58 +0200566
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200567 /*
568 * Note: we could register external pollers here.
569 * Built-in pollers have been registered before main().
570 */
Willy Tarreau4f60f162007-04-08 16:39:58 +0200571
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200572 if (!(cfg_polling_mechanism & POLL_USE_KQUEUE))
573 disable_poller("kqueue");
574
Willy Tarreau4f60f162007-04-08 16:39:58 +0200575 if (!(cfg_polling_mechanism & POLL_USE_EPOLL))
576 disable_poller("epoll");
577
Willy Tarreaude99e992007-04-16 00:53:59 +0200578 if (!(cfg_polling_mechanism & POLL_USE_SEPOLL))
579 disable_poller("sepoll");
580
Willy Tarreau4f60f162007-04-08 16:39:58 +0200581 if (!(cfg_polling_mechanism & POLL_USE_POLL))
582 disable_poller("poll");
583
584 if (!(cfg_polling_mechanism & POLL_USE_SELECT))
585 disable_poller("select");
586
587 /* Note: we could disable any poller by name here */
588
Willy Tarreau2ff76222007-04-09 19:29:56 +0200589 if (global.mode & (MODE_VERBOSE|MODE_DEBUG))
590 list_pollers(stderr);
591
Willy Tarreau4f60f162007-04-08 16:39:58 +0200592 if (!init_pollers()) {
Willy Tarreau2ff76222007-04-09 19:29:56 +0200593 Alert("No polling mechanism available.\n");
Willy Tarreau4f60f162007-04-08 16:39:58 +0200594 exit(1);
595 }
Willy Tarreau2ff76222007-04-09 19:29:56 +0200596 if (global.mode & (MODE_VERBOSE|MODE_DEBUG)) {
597 printf("Using %s() as the polling mechanism.\n", cur_poller.name);
Willy Tarreau4f60f162007-04-08 16:39:58 +0200598 }
599
Willy Tarreaubaaee002006-06-26 02:48:02 +0200600}
601
602void deinit(void)
603{
Willy Tarreau4d2d0982007-05-14 00:39:29 +0200604 struct proxy *p = proxy, *p0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200605 struct cap_hdr *h,*h_next;
606 struct server *s,*s_next;
607 struct listener *l,*l_next;
Willy Tarreau0fc45a72007-06-17 00:36:03 +0200608 struct acl_cond *cond, *condb;
609 struct hdr_exp *exp, *expb;
610 int i;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200611
612 while (p) {
613 if (p->id)
614 free(p->id);
615
616 if (p->check_req)
617 free(p->check_req);
618
619 if (p->cookie_name)
620 free(p->cookie_name);
621
622 if (p->capture_name)
623 free(p->capture_name);
624
Willy Tarreau0fc45a72007-06-17 00:36:03 +0200625 if (p->monitor_uri)
626 free(p->monitor_uri);
627
628 for (i = 0; i < HTTP_ERR_SIZE; i++) {
629 if (p->errmsg[i].len)
630 free(p->errmsg[i].str);
631 }
632
633 for (i = 0; i < p->nb_reqadd; i++) {
634 if (p->req_add[i])
635 free(p->req_add[i]);
636 }
637
638 for (i = 0; i < p->nb_rspadd; i++) {
639 if (p->rsp_add[i])
640 free(p->rsp_add[i]);
641 }
642
643 list_for_each_entry_safe(cond, condb, &p->block_cond, list) {
644 LIST_DEL(&cond->list);
645 prune_acl_cond(cond);
646 free(cond);
647 }
648
649 for (exp = p->req_exp; exp != NULL; ) {
650 if (exp->preg)
651 regfree((regex_t *)exp->preg);
652 if (exp->replace && exp->action != ACT_SETBE)
653 free((char *)exp->replace);
654 expb = exp;
655 exp = exp->next;
656 free(expb);
657 }
658
659 for (exp = p->rsp_exp; exp != NULL; ) {
660 if (exp->preg)
661 regfree((regex_t *)exp->preg);
662 if (exp->replace && exp->action != ACT_SETBE)
663 free((char *)exp->replace);
664 expb = exp;
665 exp = exp->next;
666 free(expb);
667 }
668
669 /* FIXME: this must also be freed :
670 * - ACLs
671 * - uri_auth (but it's shared)
672 */
673
Willy Tarreaubaaee002006-06-26 02:48:02 +0200674 if (p->appsession_name)
675 free(p->appsession_name);
676
677 h = p->req_cap;
678 while (h) {
679 h_next = h->next;
680 if (h->name)
681 free(h->name);
Willy Tarreaucf7f3202007-05-13 22:46:04 +0200682 pool_destroy2(h->pool);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200683 free(h);
684 h = h_next;
685 }/* end while(h) */
686
687 h = p->rsp_cap;
688 while (h) {
689 h_next = h->next;
690 if (h->name)
691 free(h->name);
692
Willy Tarreaucf7f3202007-05-13 22:46:04 +0200693 pool_destroy2(h->pool);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200694 free(h);
695 h = h_next;
696 }/* end while(h) */
697
698 s = p->srv;
699 while (s) {
700 s_next = s->next;
701 if (s->id)
702 free(s->id);
703
704 if (s->cookie)
705 free(s->cookie);
706
707 free(s);
708 s = s_next;
709 }/* end while(s) */
710
711 l = p->listen;
712 while (l) {
713 l_next = l->next;
714 free(l);
715 l = l_next;
716 }/* end while(l) */
717
Willy Tarreaucf7f3202007-05-13 22:46:04 +0200718 pool_destroy2(p->req_cap_pool);
719 pool_destroy2(p->rsp_cap_pool);
Willy Tarreau4d2d0982007-05-14 00:39:29 +0200720 p0 = p;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200721 p = p->next;
Willy Tarreau4d2d0982007-05-14 00:39:29 +0200722 free(p0);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200723 }/* end while(p) */
Willy Tarreaudd815982007-10-16 12:25:14 +0200724
725 protocol_unbind_all();
726
Willy Tarreaubaaee002006-06-26 02:48:02 +0200727 if (global.chroot) free(global.chroot);
Willy Tarreau0fc45a72007-06-17 00:36:03 +0200728 global.chroot = NULL;
729
Willy Tarreaubaaee002006-06-26 02:48:02 +0200730 if (global.pidfile) free(global.pidfile);
Willy Tarreau0fc45a72007-06-17 00:36:03 +0200731 global.pidfile = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200732
Willy Tarreaubaaee002006-06-26 02:48:02 +0200733 if (fdtab) free(fdtab);
Willy Tarreau0fc45a72007-06-17 00:36:03 +0200734 fdtab = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200735
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200736 pool_destroy2(pool2_session);
Willy Tarreau7341d942007-05-13 19:56:02 +0200737 pool_destroy2(pool2_buffer);
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200738 pool_destroy2(pool2_requri);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200739 pool_destroy2(pool2_task);
Willy Tarreau086b3b42007-05-13 21:45:51 +0200740 pool_destroy2(pool2_capture);
Willy Tarreau63963c62007-05-13 21:29:55 +0200741 pool_destroy2(pool2_appsess);
Willy Tarreaue4d7e552007-05-13 20:19:55 +0200742 pool_destroy2(pool2_pendconn);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200743
744 if (have_appsession) {
Willy Tarreau63963c62007-05-13 21:29:55 +0200745 pool_destroy2(apools.serverid);
746 pool_destroy2(apools.sessid);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200747 }
748} /* end deinit() */
749
750/* sends the signal <sig> to all pids found in <oldpids> */
751static void tell_old_pids(int sig)
752{
753 int p;
754 for (p = 0; p < nb_oldpids; p++)
755 kill(oldpids[p], sig);
756}
757
Willy Tarreau4f60f162007-04-08 16:39:58 +0200758/*
759 * Runs the polling loop
760 *
761 * FIXME:
762 * - we still use 'listeners' to check whether we want to stop or not.
763 *
764 */
765void run_poll_loop()
766{
Willy Tarreaud825eef2007-05-12 22:35:00 +0200767 struct timeval next;
Willy Tarreau4f60f162007-04-08 16:39:58 +0200768
Willy Tarreaud825eef2007-05-12 22:35:00 +0200769 tv_now(&now);
Willy Tarreau4f60f162007-04-08 16:39:58 +0200770 while (1) {
Willy Tarreaud825eef2007-05-12 22:35:00 +0200771 process_runnable_tasks(&next);
Willy Tarreau4f60f162007-04-08 16:39:58 +0200772
773 /* stop when there's no connection left and we don't allow them anymore */
774 if (!actconn && listeners == 0)
775 break;
776
Willy Tarreaud825eef2007-05-12 22:35:00 +0200777 cur_poller.poll(&cur_poller, &next);
Willy Tarreau4f60f162007-04-08 16:39:58 +0200778 }
779}
780
781
Willy Tarreaubaaee002006-06-26 02:48:02 +0200782int main(int argc, char **argv)
783{
784 int err, retry;
785 struct rlimit limit;
786 FILE *pidfile = NULL;
787 init(argc, argv);
788
789 signal(SIGQUIT, dump);
790 signal(SIGUSR1, sig_soft_stop);
791 signal(SIGHUP, sig_dump_state);
792#ifdef DEBUG_MEMORY
793 signal(SIGINT, sig_int);
794 signal(SIGTERM, sig_term);
795#endif
796
797 /* on very high loads, a sigpipe sometimes happen just between the
798 * getsockopt() which tells "it's OK to write", and the following write :-(
799 */
800#ifndef MSG_NOSIGNAL
801 signal(SIGPIPE, SIG_IGN);
802#endif
803
804 /* We will loop at most 100 times with 10 ms delay each time.
805 * That's at most 1 second. We only send a signal to old pids
806 * if we cannot grab at least one port.
807 */
808 retry = MAX_START_RETRIES;
809 err = ERR_NONE;
810 while (retry >= 0) {
811 struct timeval w;
812 err = start_proxies(retry == 0 || nb_oldpids == 0);
813 if (err != ERR_RETRYABLE)
814 break;
815 if (nb_oldpids == 0)
816 break;
817
818 /* FIXME-20060514: Solaris and OpenBSD do not support shutdown() on
819 * listening sockets. So on those platforms, it would be wiser to
820 * simply send SIGUSR1, which will not be undoable.
821 */
822 tell_old_pids(SIGTTOU);
823 /* give some time to old processes to stop listening */
824 w.tv_sec = 0;
825 w.tv_usec = 10*1000;
826 select(0, NULL, NULL, NULL, &w);
827 retry--;
828 }
829
830 /* Note: start_proxies() sends an alert when it fails. */
831 if (err != ERR_NONE) {
832 if (retry != MAX_START_RETRIES && nb_oldpids)
833 tell_old_pids(SIGTTIN);
834 exit(1);
835 }
836
837 if (listeners == 0) {
838 Alert("[%s.main()] No enabled listener found (check the <listen> keywords) ! Exiting.\n", argv[0]);
839 /* Note: we don't have to send anything to the old pids because we
840 * never stopped them. */
841 exit(1);
842 }
843
Willy Tarreaudd815982007-10-16 12:25:14 +0200844 if (protocol_bind_all() != ERR_NONE) {
845 Alert("[%s.main()] Some protocols failed to start their listeners! Exiting.\n", argv[0]);
846 protocol_unbind_all(); /* cleanup everything we can */
847 if (nb_oldpids)
848 tell_old_pids(SIGTTIN);
849 exit(1);
850 }
851
Willy Tarreaubaaee002006-06-26 02:48:02 +0200852 /* prepare pause/play signals */
853 signal(SIGTTOU, sig_pause);
854 signal(SIGTTIN, sig_listen);
855
856 if (global.mode & MODE_DAEMON) {
857 global.mode &= ~MODE_VERBOSE;
858 global.mode |= MODE_QUIET;
859 }
860
861 /* MODE_QUIET can inhibit alerts and warnings below this line */
862
863 global.mode &= ~MODE_STARTING;
864 if ((global.mode & MODE_QUIET) && !(global.mode & MODE_VERBOSE)) {
865 /* detach from the tty */
866 fclose(stdin); fclose(stdout); fclose(stderr);
867 close(0); close(1); close(2);
868 }
869
870 /* open log & pid files before the chroot */
871 if (global.mode & MODE_DAEMON && global.pidfile != NULL) {
872 int pidfd;
873 unlink(global.pidfile);
874 pidfd = open(global.pidfile, O_CREAT | O_WRONLY | O_TRUNC, 0644);
875 if (pidfd < 0) {
876 Alert("[%s.main()] Cannot create pidfile %s\n", argv[0], global.pidfile);
877 if (nb_oldpids)
878 tell_old_pids(SIGTTIN);
Willy Tarreaudd815982007-10-16 12:25:14 +0200879 protocol_unbind_all();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200880 exit(1);
881 }
882 pidfile = fdopen(pidfd, "w");
883 }
884
Willy Tarreaubaaee002006-06-26 02:48:02 +0200885 /* ulimits */
886 if (!global.rlimit_nofile)
887 global.rlimit_nofile = global.maxsock;
888
889 if (global.rlimit_nofile) {
890 limit.rlim_cur = limit.rlim_max = global.rlimit_nofile;
891 if (setrlimit(RLIMIT_NOFILE, &limit) == -1) {
892 Warning("[%s.main()] Cannot raise FD limit to %d.\n", argv[0], global.rlimit_nofile);
893 }
894 }
895
896 if (global.rlimit_memmax) {
897 limit.rlim_cur = limit.rlim_max =
898 global.rlimit_memmax * 1048576 / global.nbproc;
899#ifdef RLIMIT_AS
900 if (setrlimit(RLIMIT_AS, &limit) == -1) {
901 Warning("[%s.main()] Cannot fix MEM limit to %d megs.\n",
902 argv[0], global.rlimit_memmax);
903 }
904#else
905 if (setrlimit(RLIMIT_DATA, &limit) == -1) {
906 Warning("[%s.main()] Cannot fix MEM limit to %d megs.\n",
907 argv[0], global.rlimit_memmax);
908 }
909#endif
910 }
911
Willy Tarreau6d1a9882007-01-07 02:03:04 +0100912#ifdef CONFIG_HAP_TCPSPLICE
913 if (global.last_checks & LSTCHK_TCPSPLICE) {
914 if (tcp_splice_start() < 0) {
915 Alert("[%s.main()] Cannot enable tcp_splice.\n"
916 " Make sure you have enough permissions and that the module is loadable.\n"
917 " Alternatively, you may disable the 'tcpsplice' options in the configuration.\n"
918 "", argv[0], global.gid);
Willy Tarreaudd815982007-10-16 12:25:14 +0200919 protocol_unbind_all();
Willy Tarreau6d1a9882007-01-07 02:03:04 +0100920 exit(1);
921 }
922 }
923#endif
924
Willy Tarreaub38651a2007-03-24 17:24:39 +0100925#ifdef CONFIG_HAP_CTTPROXY
926 if (global.last_checks & LSTCHK_CTTPROXY) {
927 int ret;
928
929 ret = check_cttproxy_version();
930 if (ret < 0) {
931 Alert("[%s.main()] Cannot enable cttproxy.\n%s",
932 argv[0],
933 (ret == -1) ? " Incorrect module version.\n"
934 : " Make sure you have enough permissions and that the module is loaded.\n");
Willy Tarreaudd815982007-10-16 12:25:14 +0200935 protocol_unbind_all();
Willy Tarreaub38651a2007-03-24 17:24:39 +0100936 exit(1);
937 }
938 }
939#endif
940
941 if ((global.last_checks & LSTCHK_NETADM) && global.uid) {
942 Alert("[%s.main()] Some configuration options require full privileges, so global.uid cannot be changed.\n"
943 "", argv[0], global.gid);
Willy Tarreaudd815982007-10-16 12:25:14 +0200944 protocol_unbind_all();
Willy Tarreaub38651a2007-03-24 17:24:39 +0100945 exit(1);
946 }
947
Willy Tarreauf223cc02007-10-15 18:57:08 +0200948 /* chroot if needed */
949 if (global.chroot != NULL) {
950 if (chroot(global.chroot) == -1) {
951 Alert("[%s.main()] Cannot chroot(%s).\n", argv[0], global.chroot);
952 if (nb_oldpids)
953 tell_old_pids(SIGTTIN);
Willy Tarreaudd815982007-10-16 12:25:14 +0200954 protocol_unbind_all();
Willy Tarreauf223cc02007-10-15 18:57:08 +0200955 exit(1);
956 }
957 chdir("/");
958 }
959
Willy Tarreaubaaee002006-06-26 02:48:02 +0200960 if (nb_oldpids)
961 tell_old_pids(oldpids_sig);
962
963 /* Note that any error at this stage will be fatal because we will not
964 * be able to restart the old pids.
965 */
966
967 /* setgid / setuid */
968 if (global.gid && setgid(global.gid) == -1) {
969 Alert("[%s.main()] Cannot set gid %d.\n", argv[0], global.gid);
Willy Tarreaudd815982007-10-16 12:25:14 +0200970 protocol_unbind_all();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200971 exit(1);
972 }
973
974 if (global.uid && setuid(global.uid) == -1) {
975 Alert("[%s.main()] Cannot set uid %d.\n", argv[0], global.uid);
Willy Tarreaudd815982007-10-16 12:25:14 +0200976 protocol_unbind_all();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200977 exit(1);
978 }
979
980 /* check ulimits */
981 limit.rlim_cur = limit.rlim_max = 0;
982 getrlimit(RLIMIT_NOFILE, &limit);
983 if (limit.rlim_cur < global.maxsock) {
984 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",
985 argv[0], limit.rlim_cur, global.maxconn, global.maxsock, global.maxsock);
986 }
987
988 if (global.mode & MODE_DAEMON) {
989 int ret = 0;
990 int proc;
991
992 /* the father launches the required number of processes */
993 for (proc = 0; proc < global.nbproc; proc++) {
994 ret = fork();
995 if (ret < 0) {
996 Alert("[%s.main()] Cannot fork.\n", argv[0]);
Willy Tarreaudd815982007-10-16 12:25:14 +0200997 protocol_unbind_all();
Willy Tarreaud6803712007-10-16 07:44:56 +0200998 exit(1); /* there has been an error */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200999 }
1000 else if (ret == 0) /* child breaks here */
1001 break;
1002 if (pidfile != NULL) {
1003 fprintf(pidfile, "%d\n", ret);
1004 fflush(pidfile);
1005 }
1006 }
1007 /* close the pidfile both in children and father */
1008 if (pidfile != NULL)
1009 fclose(pidfile);
1010 free(global.pidfile);
Krzysztof Oledzki56f1e8b2007-10-11 18:30:14 +02001011 global.pidfile = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001012
1013 if (proc == global.nbproc)
1014 exit(0); /* parent must leave */
1015
1016 /* if we're NOT in QUIET mode, we should now close the 3 first FDs to ensure
1017 * that we can detach from the TTY. We MUST NOT do it in other cases since
1018 * it would have already be done, and 0-2 would have been affected to listening
1019 * sockets
1020 */
1021 if (!(global.mode & MODE_QUIET)) {
1022 /* detach from the tty */
1023 fclose(stdin); fclose(stdout); fclose(stderr);
1024 close(0); close(1); close(2); /* close all fd's */
1025 global.mode |= MODE_QUIET; /* ensure that we won't say anything from now */
1026 }
1027 pid = getpid(); /* update child's pid */
1028 setsid();
Willy Tarreau2ff76222007-04-09 19:29:56 +02001029 fork_poller();
Willy Tarreaubaaee002006-06-26 02:48:02 +02001030 }
1031
Willy Tarreaudd815982007-10-16 12:25:14 +02001032 protocol_enable_all();
Willy Tarreau4f60f162007-04-08 16:39:58 +02001033 /*
1034 * That's it : the central polling loop. Run until we stop.
1035 */
1036 run_poll_loop();
Willy Tarreaubaaee002006-06-26 02:48:02 +02001037
1038 /* Free all Hash Keys and all Hash elements */
1039 appsession_cleanup();
1040 /* Do some cleanup */
1041 deinit();
1042
1043 exit(0);
1044}
1045
1046
1047/*
1048 * Local variables:
1049 * c-indent-level: 8
1050 * c-basic-offset: 8
1051 * End:
1052 */