blob: c57e23f89fd9c9e7481b37c144e501504ad19a7e [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,
Willy Tarreaufbee7132007-10-18 13:53:22 +0200119 .stats_timeout = { .tv_sec = 10, .tv_usec = 0 }, /* stats timeout = 10 seconds */
120 .stats_sock.timeout = &global.stats_timeout,
121 .stats_sock.maxconn = 10, /* 10 concurrent stats connections */
122 .stats_sock.perm.ux.uid = -1,
123 .stats_sock.perm.ux.gid = -1,
124 .stats_sock.perm.ux.mode = 0,
Willy Tarreaubaaee002006-06-26 02:48:02 +0200125 /* others NULL OK */
126};
127
128/*********************************************************************/
129
130int stopping; /* non zero means stopping in progress */
131
132/* Here we store informations about the pids of the processes we may pause
133 * or kill. We will send them a signal every 10 ms until we can bind to all
134 * our ports. With 200 retries, that's about 2 seconds.
135 */
136#define MAX_START_RETRIES 200
137static int nb_oldpids = 0;
138static int *oldpids = NULL;
139static int oldpids_sig; /* use USR1 or TERM */
140
141/* this is used to drain data, and as a temporary buffer for sprintf()... */
142char trash[BUFSIZE];
143
144const int zero = 0;
145const int one = 1;
Alexandre Cassen87ea5482007-10-11 20:48:58 +0200146const struct linger nolinger = { .l_onoff = 1, .l_linger = 0 };
Willy Tarreaubaaee002006-06-26 02:48:02 +0200147
148/*
149 * Syslog facilities and levels. Conforming to RFC3164.
150 */
151
152#define MAX_HOSTNAME_LEN 32
153static char hostname[MAX_HOSTNAME_LEN] = "";
154
155
156/*********************************************************************/
157/* general purpose functions ***************************************/
158/*********************************************************************/
159
160void display_version()
161{
162 printf("HA-Proxy version " HAPROXY_VERSION " " HAPROXY_DATE"\n");
Willy Tarreau49e1ee82007-01-22 00:56:46 +0100163 printf("Copyright 2000-2007 Willy Tarreau <w@1wt.eu>\n\n");
Willy Tarreaubaaee002006-06-26 02:48:02 +0200164}
165
166/*
167 * This function prints the command line usage and exits
168 */
169void usage(char *name)
170{
171 display_version();
172 fprintf(stderr,
173 "Usage : %s -f <cfgfile> [ -vdV"
174 "D ] [ -n <maxconn> ] [ -N <maxpconn> ]\n"
175 " [ -p <pidfile> ] [ -m <max megs> ]\n"
176 " -v displays version\n"
177 " -d enters debug mode ; -db only disables background mode.\n"
178 " -V enters verbose mode (disables quiet mode)\n"
179 " -D goes daemon ; implies -q\n"
180 " -q quiet mode : don't display messages\n"
181 " -c check mode : only check config file and exit\n"
182 " -n sets the maximum total # of connections (%d)\n"
183 " -m limits the usable amount of memory (in MB)\n"
184 " -N sets the default, per-proxy maximum # of connections (%d)\n"
185 " -p writes pids of all children to this file\n"
186#if defined(ENABLE_EPOLL)
187 " -de disables epoll() usage even when available\n"
188#endif
Willy Tarreaude99e992007-04-16 00:53:59 +0200189#if defined(ENABLE_SEPOLL)
190 " -ds disables speculative epoll() usage even when available\n"
191#endif
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200192#if defined(ENABLE_KQUEUE)
193 " -dk disables kqueue() usage even when available\n"
194#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200195#if defined(ENABLE_POLL)
196 " -dp disables poll() usage even when available\n"
197#endif
198 " -sf/-st [pid ]* finishes/terminates old pids. Must be last arguments.\n"
199 "\n",
200 name, DEFAULT_MAXCONN, cfg_maxpconn);
201 exit(1);
202}
203
204
205
206/*********************************************************************/
207/* more specific functions ***************************************/
208/*********************************************************************/
209
210/*
211 * upon SIGUSR1, let's have a soft stop.
212 */
213void sig_soft_stop(int sig)
214{
215 soft_stop();
Willy Tarreau4d2d0982007-05-14 00:39:29 +0200216 pool_gc2();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200217 signal(sig, SIG_IGN);
218}
219
220/*
221 * upon SIGTTOU, we pause everything
222 */
223void sig_pause(int sig)
224{
225 pause_proxies();
Willy Tarreau4d2d0982007-05-14 00:39:29 +0200226 pool_gc2();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200227 signal(sig, sig_pause);
228}
229
230/*
231 * upon SIGTTIN, let's have a soft stop.
232 */
233void sig_listen(int sig)
234{
235 listen_proxies();
236 signal(sig, sig_listen);
237}
238
239/*
240 * this function dumps every server's state when the process receives SIGHUP.
241 */
242void sig_dump_state(int sig)
243{
244 struct proxy *p = proxy;
245
246 Warning("SIGHUP received, dumping servers states.\n");
247 while (p) {
248 struct server *s = p->srv;
249
250 send_log(p, LOG_NOTICE, "SIGHUP received, dumping servers states for proxy %s.\n", p->id);
251 while (s) {
252 snprintf(trash, sizeof(trash),
253 "SIGHUP: Server %s/%s is %s. Conn: %d act, %d pend, %d tot.",
254 p->id, s->id,
255 (s->state & SRV_RUNNING) ? "UP" : "DOWN",
256 s->cur_sess, s->nbpend, s->cum_sess);
257 Warning("%s\n", trash);
258 send_log(p, LOG_NOTICE, "%s\n", trash);
259 s = s->next;
260 }
261
Willy Tarreau5fcc8f12007-09-17 11:27:09 +0200262 /* FIXME: those info are a bit outdated. We should be able to distinguish between FE and BE. */
263 if (!p->srv) {
264 snprintf(trash, sizeof(trash),
265 "SIGHUP: Proxy %s has no servers. Conn: act(FE+BE): %d+%d, %d pend (%d unass), tot(FE+BE): %d+%d.",
266 p->id,
267 p->feconn, p->beconn, p->totpend, p->nbpend, p->cum_feconn, p->cum_beconn);
268 } else if (p->srv_act == 0) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200269 snprintf(trash, sizeof(trash),
Willy Tarreauf1221aa2006-12-17 22:14:12 +0100270 "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 +0200271 p->id,
272 (p->srv_bck) ? "is running on backup servers" : "has no server available",
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 } else {
275 snprintf(trash, sizeof(trash),
276 "SIGHUP: Proxy %s has %d active servers and %d backup servers available."
Willy Tarreauf1221aa2006-12-17 22:14:12 +0100277 " Conn: act(FE+BE): %d+%d, %d pend (%d unass), tot(FE+BE): %d+%d.",
Willy Tarreaubaaee002006-06-26 02:48:02 +0200278 p->id, p->srv_act, p->srv_bck,
Willy Tarreauf1221aa2006-12-17 22:14:12 +0100279 p->feconn, p->beconn, p->totpend, p->nbpend, p->cum_feconn, p->cum_beconn);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200280 }
281 Warning("%s\n", trash);
282 send_log(p, LOG_NOTICE, "%s\n", trash);
283
284 p = p->next;
285 }
286 signal(sig, sig_dump_state);
287}
288
289void dump(int sig)
290{
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200291#if 0
Willy Tarreau964c9362007-01-07 00:38:00 +0100292 struct task *t;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200293 struct session *s;
Willy Tarreau964c9362007-01-07 00:38:00 +0100294 struct rb_node *node;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200295
Willy Tarreau964c9362007-01-07 00:38:00 +0100296 for(node = rb_first(&wait_queue[0]);
297 node != NULL; node = rb_next(node)) {
298 t = rb_entry(node, struct task, rb_node);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200299 s = t->context;
300 qfprintf(stderr,"[dump] wq: task %p, still %ld ms, "
301 "cli=%d, srv=%d, cr=%d, cw=%d, sr=%d, sw=%d, "
302 "req=%d, rep=%d, clifd=%d\n",
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200303 s, tv_ms_remain(&now, &t->expire),
Willy Tarreaubaaee002006-06-26 02:48:02 +0200304 s->cli_state,
305 s->srv_state,
Willy Tarreauf161a342007-04-08 16:59:42 +0200306 EV_FD_ISSET(s->cli_fd, DIR_RD),
307 EV_FD_ISSET(s->cli_fd, DIR_WR),
308 EV_FD_ISSET(s->srv_fd, DIR_RD),
309 EV_FD_ISSET(s->srv_fd, DIR_WR),
Willy Tarreaubaaee002006-06-26 02:48:02 +0200310 s->req->l, s->rep?s->rep->l:0, s->cli_fd
311 );
312 }
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200313#endif
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200314 /* dump memory usage then free everything possible */
315 dump_pools();
316 pool_gc2();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200317}
318
319#ifdef DEBUG_MEMORY
320static void fast_stop(void)
321{
322 struct proxy *p;
323 p = proxy;
324 while (p) {
325 p->grace = 0;
326 p = p->next;
327 }
328 soft_stop();
329}
330
331void sig_int(int sig)
332{
333 /* This would normally be a hard stop,
334 but we want to be sure about deallocation,
335 and so on, so we do a soft stop with
336 0 GRACE time
337 */
338 fast_stop();
Willy Tarreau4d2d0982007-05-14 00:39:29 +0200339 pool_gc2();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200340 /* If we are killed twice, we decide to die*/
341 signal(sig, SIG_DFL);
342}
343
344void sig_term(int sig)
345{
346 /* This would normally be a hard stop,
347 but we want to be sure about deallocation,
348 and so on, so we do a soft stop with
349 0 GRACE time
350 */
351 fast_stop();
Willy Tarreau4d2d0982007-05-14 00:39:29 +0200352 pool_gc2();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200353 /* If we are killed twice, we decide to die*/
354 signal(sig, SIG_DFL);
355}
356#endif
357
358
359/*
360 * This function initializes all the necessary variables. It only returns
361 * if everything is OK. If something fails, it exits.
362 */
363void init(int argc, char **argv)
364{
365 int i;
366 int arg_mode = 0; /* MODE_DEBUG, ... */
367 char *old_argv = *argv;
368 char *tmp;
369 char *cfg_pidfile = NULL;
370
371 if (1<<INTBITS != sizeof(int)*8) {
372 fprintf(stderr,
373 "Error: wrong architecture. Recompile so that sizeof(int)=%d\n",
374 (int)(sizeof(int)*8));
375 exit(1);
376 }
377
378 /*
379 * Initialize the previously static variables.
380 */
381
382 totalconn = actconn = maxfd = listeners = stopping = 0;
383
384
385#ifdef HAPROXY_MEMMAX
386 global.rlimit_memmax = HAPROXY_MEMMAX;
387#endif
388
389 /* initialize the libc's localtime structures once for all so that we
390 * won't be missing memory if we want to send alerts under OOM conditions.
Willy Tarreau2b35c952006-10-15 15:25:48 +0200391 * Also, the Alert() and Warning() functions need <now> to be initialized.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200392 */
393 tv_now(&now);
Willy Tarreaubf736132006-10-15 22:54:47 +0200394 localtime((time_t *)&now.tv_sec);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200395 start_date = now;
396
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200397 init_task();
398 init_session();
Willy Tarreaue4d7e552007-05-13 20:19:55 +0200399 init_buffer();
400 init_pendconn();
Willy Tarreau80587432006-12-24 17:47:20 +0100401 init_proto_http();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200402
403 cfg_polling_mechanism = POLL_USE_SELECT; /* select() is always available */
404#if defined(ENABLE_POLL)
405 cfg_polling_mechanism |= POLL_USE_POLL;
406#endif
407#if defined(ENABLE_EPOLL)
408 cfg_polling_mechanism |= POLL_USE_EPOLL;
409#endif
Willy Tarreaude99e992007-04-16 00:53:59 +0200410#if defined(ENABLE_SEPOLL)
411 cfg_polling_mechanism |= POLL_USE_SEPOLL;
412#endif
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200413#if defined(ENABLE_KQUEUE)
414 cfg_polling_mechanism |= POLL_USE_KQUEUE;
415#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200416
417 pid = getpid();
418 progname = *argv;
419 while ((tmp = strchr(progname, '/')) != NULL)
420 progname = tmp + 1;
421
422 argc--; argv++;
423 while (argc > 0) {
424 char *flag;
425
426 if (**argv == '-') {
427 flag = *argv+1;
428
429 /* 1 arg */
430 if (*flag == 'v') {
431 display_version();
432 exit(0);
433 }
434#if defined(ENABLE_EPOLL)
435 else if (*flag == 'd' && flag[1] == 'e')
436 cfg_polling_mechanism &= ~POLL_USE_EPOLL;
437#endif
Willy Tarreaude99e992007-04-16 00:53:59 +0200438#if defined(ENABLE_SEPOLL)
439 else if (*flag == 'd' && flag[1] == 's')
440 cfg_polling_mechanism &= ~POLL_USE_SEPOLL;
441#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200442#if defined(ENABLE_POLL)
443 else if (*flag == 'd' && flag[1] == 'p')
444 cfg_polling_mechanism &= ~POLL_USE_POLL;
445#endif
Willy Tarreau69cad1a2007-04-10 22:45:11 +0200446#if defined(ENABLE_KQUEUE)
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200447 else if (*flag == 'd' && flag[1] == 'k')
448 cfg_polling_mechanism &= ~POLL_USE_KQUEUE;
449#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200450 else if (*flag == 'V')
451 arg_mode |= MODE_VERBOSE;
452 else if (*flag == 'd' && flag[1] == 'b')
453 arg_mode |= MODE_FOREGROUND;
454 else if (*flag == 'd')
455 arg_mode |= MODE_DEBUG;
456 else if (*flag == 'c')
457 arg_mode |= MODE_CHECK;
458 else if (*flag == 'D')
459 arg_mode |= MODE_DAEMON | MODE_QUIET;
460 else if (*flag == 'q')
461 arg_mode |= MODE_QUIET;
462 else if (*flag == 's' && (flag[1] == 'f' || flag[1] == 't')) {
463 /* list of pids to finish ('f') or terminate ('t') */
464
465 if (flag[1] == 'f')
466 oldpids_sig = SIGUSR1; /* finish then exit */
467 else
468 oldpids_sig = SIGTERM; /* terminate immediately */
469 argv++; argc--;
470
471 if (argc > 0) {
472 oldpids = calloc(argc, sizeof(int));
473 while (argc > 0) {
474 oldpids[nb_oldpids] = atol(*argv);
475 if (oldpids[nb_oldpids] <= 0)
476 usage(old_argv);
477 argc--; argv++;
478 nb_oldpids++;
479 }
480 }
481 }
482 else { /* >=2 args */
483 argv++; argc--;
484 if (argc == 0)
485 usage(old_argv);
486
487 switch (*flag) {
488 case 'n' : cfg_maxconn = atol(*argv); break;
489 case 'm' : global.rlimit_memmax = atol(*argv); break;
490 case 'N' : cfg_maxpconn = atol(*argv); break;
491 case 'f' : cfg_cfgfile = *argv; break;
492 case 'p' : cfg_pidfile = *argv; break;
493 default: usage(old_argv);
494 }
495 }
496 }
497 else
498 usage(old_argv);
499 argv++; argc--;
500 }
501
502 global.mode = MODE_STARTING | /* during startup, we want most of the alerts */
503 (arg_mode & (MODE_DAEMON | MODE_FOREGROUND | MODE_VERBOSE
504 | MODE_QUIET | MODE_CHECK | MODE_DEBUG));
505
506 if (!cfg_cfgfile)
507 usage(old_argv);
508
509 gethostname(hostname, MAX_HOSTNAME_LEN);
510
511 have_appsession = 0;
512 global.maxsock = 10; /* reserve 10 fds ; will be incremented by socket eaters */
513 if (readcfgfile(cfg_cfgfile) < 0) {
514 Alert("Error reading configuration file : %s\n", cfg_cfgfile);
515 exit(1);
516 }
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +0200517
Willy Tarreaubaaee002006-06-26 02:48:02 +0200518 if (have_appsession)
519 appsession_init();
520
521 if (global.mode & MODE_CHECK) {
522 qfprintf(stdout, "Configuration file is valid : %s\n", cfg_cfgfile);
523 exit(0);
524 }
525
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +0200526 if (start_checks() < 0)
527 exit(1);
528
Willy Tarreaubaaee002006-06-26 02:48:02 +0200529 if (cfg_maxconn > 0)
530 global.maxconn = cfg_maxconn;
531
532 if (cfg_pidfile) {
533 if (global.pidfile)
534 free(global.pidfile);
535 global.pidfile = strdup(cfg_pidfile);
536 }
537
538 if (global.maxconn == 0)
539 global.maxconn = DEFAULT_MAXCONN;
540
541 global.maxsock += global.maxconn * 2; /* each connection needs two sockets */
542
Willy Tarreau1db37712007-06-03 17:16:49 +0200543 if (global.tune.maxpollevents <= 0)
544 global.tune.maxpollevents = MAX_POLL_EVENTS;
545
Willy Tarreaubaaee002006-06-26 02:48:02 +0200546 if (arg_mode & (MODE_DEBUG | MODE_FOREGROUND)) {
547 /* command line debug mode inhibits configuration mode */
548 global.mode &= ~(MODE_DAEMON | MODE_QUIET);
549 }
550 global.mode |= (arg_mode & (MODE_DAEMON | MODE_FOREGROUND | MODE_QUIET |
Willy Tarreaufbee7132007-10-18 13:53:22 +0200551 MODE_VERBOSE | MODE_DEBUG ));
Willy Tarreaubaaee002006-06-26 02:48:02 +0200552
553 if ((global.mode & MODE_DEBUG) && (global.mode & (MODE_DAEMON | MODE_QUIET))) {
554 Warning("<debug> mode incompatible with <quiet> and <daemon>. Keeping <debug> only.\n");
555 global.mode &= ~(MODE_DAEMON | MODE_QUIET);
556 }
557
558 if ((global.nbproc > 1) && !(global.mode & MODE_DAEMON)) {
559 if (!(global.mode & (MODE_FOREGROUND | MODE_DEBUG)))
560 Warning("<nbproc> is only meaningful in daemon mode. Setting limit to 1 process.\n");
561 global.nbproc = 1;
562 }
563
564 if (global.nbproc < 1)
565 global.nbproc = 1;
566
Willy Tarreaubaaee002006-06-26 02:48:02 +0200567 fdtab = (struct fdtab *)calloc(1,
568 sizeof(struct fdtab) * (global.maxsock));
569 for (i = 0; i < global.maxsock; i++) {
570 fdtab[i].state = FD_STCLOSE;
571 }
Willy Tarreau4f60f162007-04-08 16:39:58 +0200572
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200573 /*
574 * Note: we could register external pollers here.
575 * Built-in pollers have been registered before main().
576 */
Willy Tarreau4f60f162007-04-08 16:39:58 +0200577
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200578 if (!(cfg_polling_mechanism & POLL_USE_KQUEUE))
579 disable_poller("kqueue");
580
Willy Tarreau4f60f162007-04-08 16:39:58 +0200581 if (!(cfg_polling_mechanism & POLL_USE_EPOLL))
582 disable_poller("epoll");
583
Willy Tarreaude99e992007-04-16 00:53:59 +0200584 if (!(cfg_polling_mechanism & POLL_USE_SEPOLL))
585 disable_poller("sepoll");
586
Willy Tarreau4f60f162007-04-08 16:39:58 +0200587 if (!(cfg_polling_mechanism & POLL_USE_POLL))
588 disable_poller("poll");
589
590 if (!(cfg_polling_mechanism & POLL_USE_SELECT))
591 disable_poller("select");
592
593 /* Note: we could disable any poller by name here */
594
Willy Tarreau2ff76222007-04-09 19:29:56 +0200595 if (global.mode & (MODE_VERBOSE|MODE_DEBUG))
596 list_pollers(stderr);
597
Willy Tarreau4f60f162007-04-08 16:39:58 +0200598 if (!init_pollers()) {
Willy Tarreau2ff76222007-04-09 19:29:56 +0200599 Alert("No polling mechanism available.\n");
Willy Tarreau4f60f162007-04-08 16:39:58 +0200600 exit(1);
601 }
Willy Tarreau2ff76222007-04-09 19:29:56 +0200602 if (global.mode & (MODE_VERBOSE|MODE_DEBUG)) {
603 printf("Using %s() as the polling mechanism.\n", cur_poller.name);
Willy Tarreau4f60f162007-04-08 16:39:58 +0200604 }
605
Willy Tarreaubaaee002006-06-26 02:48:02 +0200606}
607
608void deinit(void)
609{
Willy Tarreau4d2d0982007-05-14 00:39:29 +0200610 struct proxy *p = proxy, *p0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200611 struct cap_hdr *h,*h_next;
612 struct server *s,*s_next;
613 struct listener *l,*l_next;
Willy Tarreau0fc45a72007-06-17 00:36:03 +0200614 struct acl_cond *cond, *condb;
615 struct hdr_exp *exp, *expb;
616 int i;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200617
618 while (p) {
619 if (p->id)
620 free(p->id);
621
622 if (p->check_req)
623 free(p->check_req);
624
625 if (p->cookie_name)
626 free(p->cookie_name);
627
628 if (p->capture_name)
629 free(p->capture_name);
630
Willy Tarreau0fc45a72007-06-17 00:36:03 +0200631 if (p->monitor_uri)
632 free(p->monitor_uri);
633
634 for (i = 0; i < HTTP_ERR_SIZE; i++) {
635 if (p->errmsg[i].len)
636 free(p->errmsg[i].str);
637 }
638
639 for (i = 0; i < p->nb_reqadd; i++) {
640 if (p->req_add[i])
641 free(p->req_add[i]);
642 }
643
644 for (i = 0; i < p->nb_rspadd; i++) {
645 if (p->rsp_add[i])
646 free(p->rsp_add[i]);
647 }
648
649 list_for_each_entry_safe(cond, condb, &p->block_cond, list) {
650 LIST_DEL(&cond->list);
651 prune_acl_cond(cond);
652 free(cond);
653 }
654
655 for (exp = p->req_exp; exp != NULL; ) {
656 if (exp->preg)
657 regfree((regex_t *)exp->preg);
658 if (exp->replace && exp->action != ACT_SETBE)
659 free((char *)exp->replace);
660 expb = exp;
661 exp = exp->next;
662 free(expb);
663 }
664
665 for (exp = p->rsp_exp; exp != NULL; ) {
666 if (exp->preg)
667 regfree((regex_t *)exp->preg);
668 if (exp->replace && exp->action != ACT_SETBE)
669 free((char *)exp->replace);
670 expb = exp;
671 exp = exp->next;
672 free(expb);
673 }
674
675 /* FIXME: this must also be freed :
676 * - ACLs
677 * - uri_auth (but it's shared)
678 */
679
Willy Tarreaubaaee002006-06-26 02:48:02 +0200680 if (p->appsession_name)
681 free(p->appsession_name);
682
683 h = p->req_cap;
684 while (h) {
685 h_next = h->next;
686 if (h->name)
687 free(h->name);
Willy Tarreaucf7f3202007-05-13 22:46:04 +0200688 pool_destroy2(h->pool);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200689 free(h);
690 h = h_next;
691 }/* end while(h) */
692
693 h = p->rsp_cap;
694 while (h) {
695 h_next = h->next;
696 if (h->name)
697 free(h->name);
698
Willy Tarreaucf7f3202007-05-13 22:46:04 +0200699 pool_destroy2(h->pool);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200700 free(h);
701 h = h_next;
702 }/* end while(h) */
703
704 s = p->srv;
705 while (s) {
706 s_next = s->next;
707 if (s->id)
708 free(s->id);
709
710 if (s->cookie)
711 free(s->cookie);
712
713 free(s);
714 s = s_next;
715 }/* end while(s) */
716
717 l = p->listen;
718 while (l) {
719 l_next = l->next;
720 free(l);
721 l = l_next;
722 }/* end while(l) */
723
Willy Tarreaucf7f3202007-05-13 22:46:04 +0200724 pool_destroy2(p->req_cap_pool);
725 pool_destroy2(p->rsp_cap_pool);
Willy Tarreau4d2d0982007-05-14 00:39:29 +0200726 p0 = p;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200727 p = p->next;
Willy Tarreau4d2d0982007-05-14 00:39:29 +0200728 free(p0);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200729 }/* end while(p) */
Willy Tarreaudd815982007-10-16 12:25:14 +0200730
731 protocol_unbind_all();
732
Willy Tarreaubaaee002006-06-26 02:48:02 +0200733 if (global.chroot) free(global.chroot);
Willy Tarreau0fc45a72007-06-17 00:36:03 +0200734 global.chroot = NULL;
735
Willy Tarreaubaaee002006-06-26 02:48:02 +0200736 if (global.pidfile) free(global.pidfile);
Willy Tarreau0fc45a72007-06-17 00:36:03 +0200737 global.pidfile = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200738
Willy Tarreaubaaee002006-06-26 02:48:02 +0200739 if (fdtab) free(fdtab);
Willy Tarreau0fc45a72007-06-17 00:36:03 +0200740 fdtab = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200741
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200742 pool_destroy2(pool2_session);
Willy Tarreau7341d942007-05-13 19:56:02 +0200743 pool_destroy2(pool2_buffer);
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200744 pool_destroy2(pool2_requri);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200745 pool_destroy2(pool2_task);
Willy Tarreau086b3b42007-05-13 21:45:51 +0200746 pool_destroy2(pool2_capture);
Willy Tarreau63963c62007-05-13 21:29:55 +0200747 pool_destroy2(pool2_appsess);
Willy Tarreaue4d7e552007-05-13 20:19:55 +0200748 pool_destroy2(pool2_pendconn);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200749
750 if (have_appsession) {
Willy Tarreau63963c62007-05-13 21:29:55 +0200751 pool_destroy2(apools.serverid);
752 pool_destroy2(apools.sessid);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200753 }
754} /* end deinit() */
755
756/* sends the signal <sig> to all pids found in <oldpids> */
757static void tell_old_pids(int sig)
758{
759 int p;
760 for (p = 0; p < nb_oldpids; p++)
761 kill(oldpids[p], sig);
762}
763
Willy Tarreau4f60f162007-04-08 16:39:58 +0200764/*
765 * Runs the polling loop
766 *
767 * FIXME:
768 * - we still use 'listeners' to check whether we want to stop or not.
769 *
770 */
771void run_poll_loop()
772{
Willy Tarreaud825eef2007-05-12 22:35:00 +0200773 struct timeval next;
Willy Tarreau4f60f162007-04-08 16:39:58 +0200774
Willy Tarreaud825eef2007-05-12 22:35:00 +0200775 tv_now(&now);
Willy Tarreau4f60f162007-04-08 16:39:58 +0200776 while (1) {
Willy Tarreaud825eef2007-05-12 22:35:00 +0200777 process_runnable_tasks(&next);
Willy Tarreau4f60f162007-04-08 16:39:58 +0200778
779 /* stop when there's no connection left and we don't allow them anymore */
780 if (!actconn && listeners == 0)
781 break;
782
Willy Tarreaud825eef2007-05-12 22:35:00 +0200783 cur_poller.poll(&cur_poller, &next);
Willy Tarreau4f60f162007-04-08 16:39:58 +0200784 }
785}
786
787
Willy Tarreaubaaee002006-06-26 02:48:02 +0200788int main(int argc, char **argv)
789{
790 int err, retry;
791 struct rlimit limit;
792 FILE *pidfile = NULL;
793 init(argc, argv);
794
795 signal(SIGQUIT, dump);
796 signal(SIGUSR1, sig_soft_stop);
797 signal(SIGHUP, sig_dump_state);
798#ifdef DEBUG_MEMORY
799 signal(SIGINT, sig_int);
800 signal(SIGTERM, sig_term);
801#endif
802
803 /* on very high loads, a sigpipe sometimes happen just between the
804 * getsockopt() which tells "it's OK to write", and the following write :-(
805 */
806#ifndef MSG_NOSIGNAL
807 signal(SIGPIPE, SIG_IGN);
808#endif
809
810 /* We will loop at most 100 times with 10 ms delay each time.
811 * That's at most 1 second. We only send a signal to old pids
812 * if we cannot grab at least one port.
813 */
814 retry = MAX_START_RETRIES;
815 err = ERR_NONE;
816 while (retry >= 0) {
817 struct timeval w;
818 err = start_proxies(retry == 0 || nb_oldpids == 0);
819 if (err != ERR_RETRYABLE)
820 break;
821 if (nb_oldpids == 0)
822 break;
823
824 /* FIXME-20060514: Solaris and OpenBSD do not support shutdown() on
825 * listening sockets. So on those platforms, it would be wiser to
826 * simply send SIGUSR1, which will not be undoable.
827 */
828 tell_old_pids(SIGTTOU);
829 /* give some time to old processes to stop listening */
830 w.tv_sec = 0;
831 w.tv_usec = 10*1000;
832 select(0, NULL, NULL, NULL, &w);
833 retry--;
834 }
835
836 /* Note: start_proxies() sends an alert when it fails. */
837 if (err != ERR_NONE) {
838 if (retry != MAX_START_RETRIES && nb_oldpids)
839 tell_old_pids(SIGTTIN);
840 exit(1);
841 }
842
843 if (listeners == 0) {
844 Alert("[%s.main()] No enabled listener found (check the <listen> keywords) ! Exiting.\n", argv[0]);
845 /* Note: we don't have to send anything to the old pids because we
846 * never stopped them. */
847 exit(1);
848 }
849
Willy Tarreaudd815982007-10-16 12:25:14 +0200850 if (protocol_bind_all() != ERR_NONE) {
851 Alert("[%s.main()] Some protocols failed to start their listeners! Exiting.\n", argv[0]);
852 protocol_unbind_all(); /* cleanup everything we can */
853 if (nb_oldpids)
854 tell_old_pids(SIGTTIN);
855 exit(1);
856 }
857
Willy Tarreaubaaee002006-06-26 02:48:02 +0200858 /* prepare pause/play signals */
859 signal(SIGTTOU, sig_pause);
860 signal(SIGTTIN, sig_listen);
861
862 if (global.mode & MODE_DAEMON) {
863 global.mode &= ~MODE_VERBOSE;
864 global.mode |= MODE_QUIET;
865 }
866
867 /* MODE_QUIET can inhibit alerts and warnings below this line */
868
869 global.mode &= ~MODE_STARTING;
870 if ((global.mode & MODE_QUIET) && !(global.mode & MODE_VERBOSE)) {
871 /* detach from the tty */
872 fclose(stdin); fclose(stdout); fclose(stderr);
873 close(0); close(1); close(2);
874 }
875
876 /* open log & pid files before the chroot */
877 if (global.mode & MODE_DAEMON && global.pidfile != NULL) {
878 int pidfd;
879 unlink(global.pidfile);
880 pidfd = open(global.pidfile, O_CREAT | O_WRONLY | O_TRUNC, 0644);
881 if (pidfd < 0) {
882 Alert("[%s.main()] Cannot create pidfile %s\n", argv[0], global.pidfile);
883 if (nb_oldpids)
884 tell_old_pids(SIGTTIN);
Willy Tarreaudd815982007-10-16 12:25:14 +0200885 protocol_unbind_all();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200886 exit(1);
887 }
888 pidfile = fdopen(pidfd, "w");
889 }
890
Willy Tarreaubaaee002006-06-26 02:48:02 +0200891 /* ulimits */
892 if (!global.rlimit_nofile)
893 global.rlimit_nofile = global.maxsock;
894
895 if (global.rlimit_nofile) {
896 limit.rlim_cur = limit.rlim_max = global.rlimit_nofile;
897 if (setrlimit(RLIMIT_NOFILE, &limit) == -1) {
898 Warning("[%s.main()] Cannot raise FD limit to %d.\n", argv[0], global.rlimit_nofile);
899 }
900 }
901
902 if (global.rlimit_memmax) {
903 limit.rlim_cur = limit.rlim_max =
904 global.rlimit_memmax * 1048576 / global.nbproc;
905#ifdef RLIMIT_AS
906 if (setrlimit(RLIMIT_AS, &limit) == -1) {
907 Warning("[%s.main()] Cannot fix MEM limit to %d megs.\n",
908 argv[0], global.rlimit_memmax);
909 }
910#else
911 if (setrlimit(RLIMIT_DATA, &limit) == -1) {
912 Warning("[%s.main()] Cannot fix MEM limit to %d megs.\n",
913 argv[0], global.rlimit_memmax);
914 }
915#endif
916 }
917
Willy Tarreau6d1a9882007-01-07 02:03:04 +0100918#ifdef CONFIG_HAP_TCPSPLICE
919 if (global.last_checks & LSTCHK_TCPSPLICE) {
920 if (tcp_splice_start() < 0) {
921 Alert("[%s.main()] Cannot enable tcp_splice.\n"
922 " Make sure you have enough permissions and that the module is loadable.\n"
923 " Alternatively, you may disable the 'tcpsplice' options in the configuration.\n"
924 "", argv[0], global.gid);
Willy Tarreaudd815982007-10-16 12:25:14 +0200925 protocol_unbind_all();
Willy Tarreau6d1a9882007-01-07 02:03:04 +0100926 exit(1);
927 }
928 }
929#endif
930
Willy Tarreaub38651a2007-03-24 17:24:39 +0100931#ifdef CONFIG_HAP_CTTPROXY
932 if (global.last_checks & LSTCHK_CTTPROXY) {
933 int ret;
934
935 ret = check_cttproxy_version();
936 if (ret < 0) {
937 Alert("[%s.main()] Cannot enable cttproxy.\n%s",
938 argv[0],
939 (ret == -1) ? " Incorrect module version.\n"
940 : " Make sure you have enough permissions and that the module is loaded.\n");
Willy Tarreaudd815982007-10-16 12:25:14 +0200941 protocol_unbind_all();
Willy Tarreaub38651a2007-03-24 17:24:39 +0100942 exit(1);
943 }
944 }
945#endif
946
947 if ((global.last_checks & LSTCHK_NETADM) && global.uid) {
948 Alert("[%s.main()] Some configuration options require full privileges, so global.uid cannot be changed.\n"
949 "", argv[0], global.gid);
Willy Tarreaudd815982007-10-16 12:25:14 +0200950 protocol_unbind_all();
Willy Tarreaub38651a2007-03-24 17:24:39 +0100951 exit(1);
952 }
953
Willy Tarreauf223cc02007-10-15 18:57:08 +0200954 /* chroot if needed */
955 if (global.chroot != NULL) {
956 if (chroot(global.chroot) == -1) {
957 Alert("[%s.main()] Cannot chroot(%s).\n", argv[0], global.chroot);
958 if (nb_oldpids)
959 tell_old_pids(SIGTTIN);
Willy Tarreaudd815982007-10-16 12:25:14 +0200960 protocol_unbind_all();
Willy Tarreauf223cc02007-10-15 18:57:08 +0200961 exit(1);
962 }
963 chdir("/");
964 }
965
Willy Tarreaubaaee002006-06-26 02:48:02 +0200966 if (nb_oldpids)
967 tell_old_pids(oldpids_sig);
968
969 /* Note that any error at this stage will be fatal because we will not
970 * be able to restart the old pids.
971 */
972
973 /* setgid / setuid */
974 if (global.gid && setgid(global.gid) == -1) {
975 Alert("[%s.main()] Cannot set gid %d.\n", argv[0], global.gid);
Willy Tarreaudd815982007-10-16 12:25:14 +0200976 protocol_unbind_all();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200977 exit(1);
978 }
979
980 if (global.uid && setuid(global.uid) == -1) {
981 Alert("[%s.main()] Cannot set uid %d.\n", argv[0], global.uid);
Willy Tarreaudd815982007-10-16 12:25:14 +0200982 protocol_unbind_all();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200983 exit(1);
984 }
985
986 /* check ulimits */
987 limit.rlim_cur = limit.rlim_max = 0;
988 getrlimit(RLIMIT_NOFILE, &limit);
989 if (limit.rlim_cur < global.maxsock) {
990 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",
991 argv[0], limit.rlim_cur, global.maxconn, global.maxsock, global.maxsock);
992 }
993
994 if (global.mode & MODE_DAEMON) {
995 int ret = 0;
996 int proc;
997
998 /* the father launches the required number of processes */
999 for (proc = 0; proc < global.nbproc; proc++) {
1000 ret = fork();
1001 if (ret < 0) {
1002 Alert("[%s.main()] Cannot fork.\n", argv[0]);
Willy Tarreaudd815982007-10-16 12:25:14 +02001003 protocol_unbind_all();
Willy Tarreaud6803712007-10-16 07:44:56 +02001004 exit(1); /* there has been an error */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001005 }
1006 else if (ret == 0) /* child breaks here */
1007 break;
1008 if (pidfile != NULL) {
1009 fprintf(pidfile, "%d\n", ret);
1010 fflush(pidfile);
1011 }
1012 }
1013 /* close the pidfile both in children and father */
1014 if (pidfile != NULL)
1015 fclose(pidfile);
1016 free(global.pidfile);
Krzysztof Oledzki56f1e8b2007-10-11 18:30:14 +02001017 global.pidfile = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001018
1019 if (proc == global.nbproc)
1020 exit(0); /* parent must leave */
1021
1022 /* if we're NOT in QUIET mode, we should now close the 3 first FDs to ensure
1023 * that we can detach from the TTY. We MUST NOT do it in other cases since
1024 * it would have already be done, and 0-2 would have been affected to listening
1025 * sockets
1026 */
1027 if (!(global.mode & MODE_QUIET)) {
1028 /* detach from the tty */
1029 fclose(stdin); fclose(stdout); fclose(stderr);
1030 close(0); close(1); close(2); /* close all fd's */
1031 global.mode |= MODE_QUIET; /* ensure that we won't say anything from now */
1032 }
1033 pid = getpid(); /* update child's pid */
1034 setsid();
Willy Tarreau2ff76222007-04-09 19:29:56 +02001035 fork_poller();
Willy Tarreaubaaee002006-06-26 02:48:02 +02001036 }
1037
Willy Tarreaudd815982007-10-16 12:25:14 +02001038 protocol_enable_all();
Willy Tarreau4f60f162007-04-08 16:39:58 +02001039 /*
1040 * That's it : the central polling loop. Run until we stop.
1041 */
1042 run_poll_loop();
Willy Tarreaubaaee002006-06-26 02:48:02 +02001043
1044 /* Free all Hash Keys and all Hash elements */
1045 appsession_cleanup();
1046 /* Do some cleanup */
1047 deinit();
1048
1049 exit(0);
1050}
1051
1052
1053/*
1054 * Local variables:
1055 * c-indent-level: 8
1056 * c-basic-offset: 8
1057 * End:
1058 */