blob: cc2514b265e640018fb34b7b01148faec1bfd1e7 [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 */
Willy Tarreau03f6d672007-10-18 15:15:57 +0200120 .stats_sock = {
121 .timeout = &global.stats_timeout,
122 .maxconn = 10, /* 10 concurrent stats connections */
123 .perm = {
124 .ux = {
125 .uid = -1,
126 .gid = -1,
127 .mode = 0,
128 }
129 }
130 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200131 /* others NULL OK */
132};
133
134/*********************************************************************/
135
136int stopping; /* non zero means stopping in progress */
137
138/* Here we store informations about the pids of the processes we may pause
139 * or kill. We will send them a signal every 10 ms until we can bind to all
140 * our ports. With 200 retries, that's about 2 seconds.
141 */
142#define MAX_START_RETRIES 200
143static int nb_oldpids = 0;
144static int *oldpids = NULL;
145static int oldpids_sig; /* use USR1 or TERM */
146
147/* this is used to drain data, and as a temporary buffer for sprintf()... */
148char trash[BUFSIZE];
149
150const int zero = 0;
151const int one = 1;
Alexandre Cassen87ea5482007-10-11 20:48:58 +0200152const struct linger nolinger = { .l_onoff = 1, .l_linger = 0 };
Willy Tarreaubaaee002006-06-26 02:48:02 +0200153
154/*
155 * Syslog facilities and levels. Conforming to RFC3164.
156 */
157
158#define MAX_HOSTNAME_LEN 32
159static char hostname[MAX_HOSTNAME_LEN] = "";
160
161
162/*********************************************************************/
163/* general purpose functions ***************************************/
164/*********************************************************************/
165
166void display_version()
167{
168 printf("HA-Proxy version " HAPROXY_VERSION " " HAPROXY_DATE"\n");
Willy Tarreau49e1ee82007-01-22 00:56:46 +0100169 printf("Copyright 2000-2007 Willy Tarreau <w@1wt.eu>\n\n");
Willy Tarreaubaaee002006-06-26 02:48:02 +0200170}
171
172/*
173 * This function prints the command line usage and exits
174 */
175void usage(char *name)
176{
177 display_version();
178 fprintf(stderr,
179 "Usage : %s -f <cfgfile> [ -vdV"
180 "D ] [ -n <maxconn> ] [ -N <maxpconn> ]\n"
181 " [ -p <pidfile> ] [ -m <max megs> ]\n"
182 " -v displays version\n"
183 " -d enters debug mode ; -db only disables background mode.\n"
184 " -V enters verbose mode (disables quiet mode)\n"
185 " -D goes daemon ; implies -q\n"
186 " -q quiet mode : don't display messages\n"
187 " -c check mode : only check config file and exit\n"
188 " -n sets the maximum total # of connections (%d)\n"
189 " -m limits the usable amount of memory (in MB)\n"
190 " -N sets the default, per-proxy maximum # of connections (%d)\n"
191 " -p writes pids of all children to this file\n"
192#if defined(ENABLE_EPOLL)
193 " -de disables epoll() usage even when available\n"
194#endif
Willy Tarreaude99e992007-04-16 00:53:59 +0200195#if defined(ENABLE_SEPOLL)
196 " -ds disables speculative epoll() usage even when available\n"
197#endif
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200198#if defined(ENABLE_KQUEUE)
199 " -dk disables kqueue() usage even when available\n"
200#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200201#if defined(ENABLE_POLL)
202 " -dp disables poll() usage even when available\n"
203#endif
204 " -sf/-st [pid ]* finishes/terminates old pids. Must be last arguments.\n"
205 "\n",
206 name, DEFAULT_MAXCONN, cfg_maxpconn);
207 exit(1);
208}
209
210
211
212/*********************************************************************/
213/* more specific functions ***************************************/
214/*********************************************************************/
215
216/*
217 * upon SIGUSR1, let's have a soft stop.
218 */
219void sig_soft_stop(int sig)
220{
221 soft_stop();
Willy Tarreau4d2d0982007-05-14 00:39:29 +0200222 pool_gc2();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200223 signal(sig, SIG_IGN);
224}
225
226/*
227 * upon SIGTTOU, we pause everything
228 */
229void sig_pause(int sig)
230{
231 pause_proxies();
Willy Tarreau4d2d0982007-05-14 00:39:29 +0200232 pool_gc2();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200233 signal(sig, sig_pause);
234}
235
236/*
237 * upon SIGTTIN, let's have a soft stop.
238 */
239void sig_listen(int sig)
240{
241 listen_proxies();
242 signal(sig, sig_listen);
243}
244
245/*
246 * this function dumps every server's state when the process receives SIGHUP.
247 */
248void sig_dump_state(int sig)
249{
250 struct proxy *p = proxy;
251
252 Warning("SIGHUP received, dumping servers states.\n");
253 while (p) {
254 struct server *s = p->srv;
255
256 send_log(p, LOG_NOTICE, "SIGHUP received, dumping servers states for proxy %s.\n", p->id);
257 while (s) {
258 snprintf(trash, sizeof(trash),
259 "SIGHUP: Server %s/%s is %s. Conn: %d act, %d pend, %d tot.",
260 p->id, s->id,
261 (s->state & SRV_RUNNING) ? "UP" : "DOWN",
262 s->cur_sess, s->nbpend, s->cum_sess);
263 Warning("%s\n", trash);
264 send_log(p, LOG_NOTICE, "%s\n", trash);
265 s = s->next;
266 }
267
Willy Tarreau5fcc8f12007-09-17 11:27:09 +0200268 /* FIXME: those info are a bit outdated. We should be able to distinguish between FE and BE. */
269 if (!p->srv) {
270 snprintf(trash, sizeof(trash),
271 "SIGHUP: Proxy %s has no servers. Conn: act(FE+BE): %d+%d, %d pend (%d unass), tot(FE+BE): %d+%d.",
272 p->id,
273 p->feconn, p->beconn, p->totpend, p->nbpend, p->cum_feconn, p->cum_beconn);
274 } else if (p->srv_act == 0) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200275 snprintf(trash, sizeof(trash),
Willy Tarreauf1221aa2006-12-17 22:14:12 +0100276 "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 +0200277 p->id,
278 (p->srv_bck) ? "is running on backup servers" : "has no server available",
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 } else {
281 snprintf(trash, sizeof(trash),
282 "SIGHUP: Proxy %s has %d active servers and %d backup servers available."
Willy Tarreauf1221aa2006-12-17 22:14:12 +0100283 " Conn: act(FE+BE): %d+%d, %d pend (%d unass), tot(FE+BE): %d+%d.",
Willy Tarreaubaaee002006-06-26 02:48:02 +0200284 p->id, p->srv_act, p->srv_bck,
Willy Tarreauf1221aa2006-12-17 22:14:12 +0100285 p->feconn, p->beconn, p->totpend, p->nbpend, p->cum_feconn, p->cum_beconn);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200286 }
287 Warning("%s\n", trash);
288 send_log(p, LOG_NOTICE, "%s\n", trash);
289
290 p = p->next;
291 }
292 signal(sig, sig_dump_state);
293}
294
295void dump(int sig)
296{
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200297#if 0
Willy Tarreau964c9362007-01-07 00:38:00 +0100298 struct task *t;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200299 struct session *s;
Willy Tarreau964c9362007-01-07 00:38:00 +0100300 struct rb_node *node;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200301
Willy Tarreau964c9362007-01-07 00:38:00 +0100302 for(node = rb_first(&wait_queue[0]);
303 node != NULL; node = rb_next(node)) {
304 t = rb_entry(node, struct task, rb_node);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200305 s = t->context;
306 qfprintf(stderr,"[dump] wq: task %p, still %ld ms, "
307 "cli=%d, srv=%d, cr=%d, cw=%d, sr=%d, sw=%d, "
308 "req=%d, rep=%d, clifd=%d\n",
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200309 s, tv_ms_remain(&now, &t->expire),
Willy Tarreaubaaee002006-06-26 02:48:02 +0200310 s->cli_state,
311 s->srv_state,
Willy Tarreauf161a342007-04-08 16:59:42 +0200312 EV_FD_ISSET(s->cli_fd, DIR_RD),
313 EV_FD_ISSET(s->cli_fd, DIR_WR),
314 EV_FD_ISSET(s->srv_fd, DIR_RD),
315 EV_FD_ISSET(s->srv_fd, DIR_WR),
Willy Tarreaubaaee002006-06-26 02:48:02 +0200316 s->req->l, s->rep?s->rep->l:0, s->cli_fd
317 );
318 }
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200319#endif
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200320 /* dump memory usage then free everything possible */
321 dump_pools();
322 pool_gc2();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200323}
324
325#ifdef DEBUG_MEMORY
326static void fast_stop(void)
327{
328 struct proxy *p;
329 p = proxy;
330 while (p) {
331 p->grace = 0;
332 p = p->next;
333 }
334 soft_stop();
335}
336
337void sig_int(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
350void sig_term(int sig)
351{
352 /* This would normally be a hard stop,
353 but we want to be sure about deallocation,
354 and so on, so we do a soft stop with
355 0 GRACE time
356 */
357 fast_stop();
Willy Tarreau4d2d0982007-05-14 00:39:29 +0200358 pool_gc2();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200359 /* If we are killed twice, we decide to die*/
360 signal(sig, SIG_DFL);
361}
362#endif
363
364
365/*
366 * This function initializes all the necessary variables. It only returns
367 * if everything is OK. If something fails, it exits.
368 */
369void init(int argc, char **argv)
370{
371 int i;
372 int arg_mode = 0; /* MODE_DEBUG, ... */
373 char *old_argv = *argv;
374 char *tmp;
375 char *cfg_pidfile = NULL;
376
377 if (1<<INTBITS != sizeof(int)*8) {
378 fprintf(stderr,
379 "Error: wrong architecture. Recompile so that sizeof(int)=%d\n",
380 (int)(sizeof(int)*8));
381 exit(1);
382 }
383
384 /*
385 * Initialize the previously static variables.
386 */
387
388 totalconn = actconn = maxfd = listeners = stopping = 0;
389
390
391#ifdef HAPROXY_MEMMAX
392 global.rlimit_memmax = HAPROXY_MEMMAX;
393#endif
394
395 /* initialize the libc's localtime structures once for all so that we
396 * won't be missing memory if we want to send alerts under OOM conditions.
Willy Tarreau2b35c952006-10-15 15:25:48 +0200397 * Also, the Alert() and Warning() functions need <now> to be initialized.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200398 */
399 tv_now(&now);
Willy Tarreaubf736132006-10-15 22:54:47 +0200400 localtime((time_t *)&now.tv_sec);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200401 start_date = now;
402
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200403 init_task();
404 init_session();
Willy Tarreaue4d7e552007-05-13 20:19:55 +0200405 init_buffer();
406 init_pendconn();
Willy Tarreau80587432006-12-24 17:47:20 +0100407 init_proto_http();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200408
409 cfg_polling_mechanism = POLL_USE_SELECT; /* select() is always available */
410#if defined(ENABLE_POLL)
411 cfg_polling_mechanism |= POLL_USE_POLL;
412#endif
413#if defined(ENABLE_EPOLL)
414 cfg_polling_mechanism |= POLL_USE_EPOLL;
415#endif
Willy Tarreaude99e992007-04-16 00:53:59 +0200416#if defined(ENABLE_SEPOLL)
417 cfg_polling_mechanism |= POLL_USE_SEPOLL;
418#endif
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200419#if defined(ENABLE_KQUEUE)
420 cfg_polling_mechanism |= POLL_USE_KQUEUE;
421#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200422
423 pid = getpid();
424 progname = *argv;
425 while ((tmp = strchr(progname, '/')) != NULL)
426 progname = tmp + 1;
427
428 argc--; argv++;
429 while (argc > 0) {
430 char *flag;
431
432 if (**argv == '-') {
433 flag = *argv+1;
434
435 /* 1 arg */
436 if (*flag == 'v') {
437 display_version();
438 exit(0);
439 }
440#if defined(ENABLE_EPOLL)
441 else if (*flag == 'd' && flag[1] == 'e')
442 cfg_polling_mechanism &= ~POLL_USE_EPOLL;
443#endif
Willy Tarreaude99e992007-04-16 00:53:59 +0200444#if defined(ENABLE_SEPOLL)
445 else if (*flag == 'd' && flag[1] == 's')
446 cfg_polling_mechanism &= ~POLL_USE_SEPOLL;
447#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200448#if defined(ENABLE_POLL)
449 else if (*flag == 'd' && flag[1] == 'p')
450 cfg_polling_mechanism &= ~POLL_USE_POLL;
451#endif
Willy Tarreau69cad1a2007-04-10 22:45:11 +0200452#if defined(ENABLE_KQUEUE)
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200453 else if (*flag == 'd' && flag[1] == 'k')
454 cfg_polling_mechanism &= ~POLL_USE_KQUEUE;
455#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200456 else if (*flag == 'V')
457 arg_mode |= MODE_VERBOSE;
458 else if (*flag == 'd' && flag[1] == 'b')
459 arg_mode |= MODE_FOREGROUND;
460 else if (*flag == 'd')
461 arg_mode |= MODE_DEBUG;
462 else if (*flag == 'c')
463 arg_mode |= MODE_CHECK;
464 else if (*flag == 'D')
465 arg_mode |= MODE_DAEMON | MODE_QUIET;
466 else if (*flag == 'q')
467 arg_mode |= MODE_QUIET;
468 else if (*flag == 's' && (flag[1] == 'f' || flag[1] == 't')) {
469 /* list of pids to finish ('f') or terminate ('t') */
470
471 if (flag[1] == 'f')
472 oldpids_sig = SIGUSR1; /* finish then exit */
473 else
474 oldpids_sig = SIGTERM; /* terminate immediately */
475 argv++; argc--;
476
477 if (argc > 0) {
478 oldpids = calloc(argc, sizeof(int));
479 while (argc > 0) {
480 oldpids[nb_oldpids] = atol(*argv);
481 if (oldpids[nb_oldpids] <= 0)
482 usage(old_argv);
483 argc--; argv++;
484 nb_oldpids++;
485 }
486 }
487 }
488 else { /* >=2 args */
489 argv++; argc--;
490 if (argc == 0)
491 usage(old_argv);
492
493 switch (*flag) {
494 case 'n' : cfg_maxconn = atol(*argv); break;
495 case 'm' : global.rlimit_memmax = atol(*argv); break;
496 case 'N' : cfg_maxpconn = atol(*argv); break;
497 case 'f' : cfg_cfgfile = *argv; break;
498 case 'p' : cfg_pidfile = *argv; break;
499 default: usage(old_argv);
500 }
501 }
502 }
503 else
504 usage(old_argv);
505 argv++; argc--;
506 }
507
508 global.mode = MODE_STARTING | /* during startup, we want most of the alerts */
509 (arg_mode & (MODE_DAEMON | MODE_FOREGROUND | MODE_VERBOSE
510 | MODE_QUIET | MODE_CHECK | MODE_DEBUG));
511
512 if (!cfg_cfgfile)
513 usage(old_argv);
514
515 gethostname(hostname, MAX_HOSTNAME_LEN);
516
517 have_appsession = 0;
518 global.maxsock = 10; /* reserve 10 fds ; will be incremented by socket eaters */
519 if (readcfgfile(cfg_cfgfile) < 0) {
520 Alert("Error reading configuration file : %s\n", cfg_cfgfile);
521 exit(1);
522 }
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +0200523
Willy Tarreaubaaee002006-06-26 02:48:02 +0200524 if (have_appsession)
525 appsession_init();
526
527 if (global.mode & MODE_CHECK) {
528 qfprintf(stdout, "Configuration file is valid : %s\n", cfg_cfgfile);
529 exit(0);
530 }
531
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +0200532 if (start_checks() < 0)
533 exit(1);
534
Willy Tarreaubaaee002006-06-26 02:48:02 +0200535 if (cfg_maxconn > 0)
536 global.maxconn = cfg_maxconn;
537
538 if (cfg_pidfile) {
539 if (global.pidfile)
540 free(global.pidfile);
541 global.pidfile = strdup(cfg_pidfile);
542 }
543
544 if (global.maxconn == 0)
545 global.maxconn = DEFAULT_MAXCONN;
546
547 global.maxsock += global.maxconn * 2; /* each connection needs two sockets */
548
Willy Tarreau1db37712007-06-03 17:16:49 +0200549 if (global.tune.maxpollevents <= 0)
550 global.tune.maxpollevents = MAX_POLL_EVENTS;
551
Willy Tarreaubaaee002006-06-26 02:48:02 +0200552 if (arg_mode & (MODE_DEBUG | MODE_FOREGROUND)) {
553 /* command line debug mode inhibits configuration mode */
554 global.mode &= ~(MODE_DAEMON | MODE_QUIET);
555 }
556 global.mode |= (arg_mode & (MODE_DAEMON | MODE_FOREGROUND | MODE_QUIET |
Willy Tarreaufbee7132007-10-18 13:53:22 +0200557 MODE_VERBOSE | MODE_DEBUG ));
Willy Tarreaubaaee002006-06-26 02:48:02 +0200558
559 if ((global.mode & MODE_DEBUG) && (global.mode & (MODE_DAEMON | MODE_QUIET))) {
560 Warning("<debug> mode incompatible with <quiet> and <daemon>. Keeping <debug> only.\n");
561 global.mode &= ~(MODE_DAEMON | MODE_QUIET);
562 }
563
564 if ((global.nbproc > 1) && !(global.mode & MODE_DAEMON)) {
565 if (!(global.mode & (MODE_FOREGROUND | MODE_DEBUG)))
566 Warning("<nbproc> is only meaningful in daemon mode. Setting limit to 1 process.\n");
567 global.nbproc = 1;
568 }
569
570 if (global.nbproc < 1)
571 global.nbproc = 1;
572
Willy Tarreaubaaee002006-06-26 02:48:02 +0200573 fdtab = (struct fdtab *)calloc(1,
574 sizeof(struct fdtab) * (global.maxsock));
575 for (i = 0; i < global.maxsock; i++) {
576 fdtab[i].state = FD_STCLOSE;
577 }
Willy Tarreau4f60f162007-04-08 16:39:58 +0200578
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200579 /*
580 * Note: we could register external pollers here.
581 * Built-in pollers have been registered before main().
582 */
Willy Tarreau4f60f162007-04-08 16:39:58 +0200583
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200584 if (!(cfg_polling_mechanism & POLL_USE_KQUEUE))
585 disable_poller("kqueue");
586
Willy Tarreau4f60f162007-04-08 16:39:58 +0200587 if (!(cfg_polling_mechanism & POLL_USE_EPOLL))
588 disable_poller("epoll");
589
Willy Tarreaude99e992007-04-16 00:53:59 +0200590 if (!(cfg_polling_mechanism & POLL_USE_SEPOLL))
591 disable_poller("sepoll");
592
Willy Tarreau4f60f162007-04-08 16:39:58 +0200593 if (!(cfg_polling_mechanism & POLL_USE_POLL))
594 disable_poller("poll");
595
596 if (!(cfg_polling_mechanism & POLL_USE_SELECT))
597 disable_poller("select");
598
599 /* Note: we could disable any poller by name here */
600
Willy Tarreau2ff76222007-04-09 19:29:56 +0200601 if (global.mode & (MODE_VERBOSE|MODE_DEBUG))
602 list_pollers(stderr);
603
Willy Tarreau4f60f162007-04-08 16:39:58 +0200604 if (!init_pollers()) {
Willy Tarreau2ff76222007-04-09 19:29:56 +0200605 Alert("No polling mechanism available.\n");
Willy Tarreau4f60f162007-04-08 16:39:58 +0200606 exit(1);
607 }
Willy Tarreau2ff76222007-04-09 19:29:56 +0200608 if (global.mode & (MODE_VERBOSE|MODE_DEBUG)) {
609 printf("Using %s() as the polling mechanism.\n", cur_poller.name);
Willy Tarreau4f60f162007-04-08 16:39:58 +0200610 }
611
Willy Tarreaubaaee002006-06-26 02:48:02 +0200612}
613
614void deinit(void)
615{
Willy Tarreau4d2d0982007-05-14 00:39:29 +0200616 struct proxy *p = proxy, *p0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200617 struct cap_hdr *h,*h_next;
618 struct server *s,*s_next;
619 struct listener *l,*l_next;
Willy Tarreau0fc45a72007-06-17 00:36:03 +0200620 struct acl_cond *cond, *condb;
621 struct hdr_exp *exp, *expb;
622 int i;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200623
624 while (p) {
625 if (p->id)
626 free(p->id);
627
628 if (p->check_req)
629 free(p->check_req);
630
631 if (p->cookie_name)
632 free(p->cookie_name);
633
634 if (p->capture_name)
635 free(p->capture_name);
636
Willy Tarreau0fc45a72007-06-17 00:36:03 +0200637 if (p->monitor_uri)
638 free(p->monitor_uri);
639
640 for (i = 0; i < HTTP_ERR_SIZE; i++) {
641 if (p->errmsg[i].len)
642 free(p->errmsg[i].str);
643 }
644
645 for (i = 0; i < p->nb_reqadd; i++) {
646 if (p->req_add[i])
647 free(p->req_add[i]);
648 }
649
650 for (i = 0; i < p->nb_rspadd; i++) {
651 if (p->rsp_add[i])
652 free(p->rsp_add[i]);
653 }
654
655 list_for_each_entry_safe(cond, condb, &p->block_cond, list) {
656 LIST_DEL(&cond->list);
657 prune_acl_cond(cond);
658 free(cond);
659 }
660
661 for (exp = p->req_exp; exp != NULL; ) {
662 if (exp->preg)
663 regfree((regex_t *)exp->preg);
664 if (exp->replace && exp->action != ACT_SETBE)
665 free((char *)exp->replace);
666 expb = exp;
667 exp = exp->next;
668 free(expb);
669 }
670
671 for (exp = p->rsp_exp; exp != NULL; ) {
672 if (exp->preg)
673 regfree((regex_t *)exp->preg);
674 if (exp->replace && exp->action != ACT_SETBE)
675 free((char *)exp->replace);
676 expb = exp;
677 exp = exp->next;
678 free(expb);
679 }
680
681 /* FIXME: this must also be freed :
682 * - ACLs
683 * - uri_auth (but it's shared)
684 */
685
Willy Tarreaubaaee002006-06-26 02:48:02 +0200686 if (p->appsession_name)
687 free(p->appsession_name);
688
689 h = p->req_cap;
690 while (h) {
691 h_next = h->next;
692 if (h->name)
693 free(h->name);
Willy Tarreaucf7f3202007-05-13 22:46:04 +0200694 pool_destroy2(h->pool);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200695 free(h);
696 h = h_next;
697 }/* end while(h) */
698
699 h = p->rsp_cap;
700 while (h) {
701 h_next = h->next;
702 if (h->name)
703 free(h->name);
704
Willy Tarreaucf7f3202007-05-13 22:46:04 +0200705 pool_destroy2(h->pool);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200706 free(h);
707 h = h_next;
708 }/* end while(h) */
709
710 s = p->srv;
711 while (s) {
712 s_next = s->next;
713 if (s->id)
714 free(s->id);
715
716 if (s->cookie)
717 free(s->cookie);
718
719 free(s);
720 s = s_next;
721 }/* end while(s) */
722
723 l = p->listen;
724 while (l) {
725 l_next = l->next;
726 free(l);
727 l = l_next;
728 }/* end while(l) */
729
Willy Tarreaucf7f3202007-05-13 22:46:04 +0200730 pool_destroy2(p->req_cap_pool);
731 pool_destroy2(p->rsp_cap_pool);
Willy Tarreau4d2d0982007-05-14 00:39:29 +0200732 p0 = p;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200733 p = p->next;
Willy Tarreau4d2d0982007-05-14 00:39:29 +0200734 free(p0);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200735 }/* end while(p) */
Willy Tarreaudd815982007-10-16 12:25:14 +0200736
737 protocol_unbind_all();
738
Willy Tarreaubaaee002006-06-26 02:48:02 +0200739 if (global.chroot) free(global.chroot);
Willy Tarreau0fc45a72007-06-17 00:36:03 +0200740 global.chroot = NULL;
741
Willy Tarreaubaaee002006-06-26 02:48:02 +0200742 if (global.pidfile) free(global.pidfile);
Willy Tarreau0fc45a72007-06-17 00:36:03 +0200743 global.pidfile = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200744
Willy Tarreaubaaee002006-06-26 02:48:02 +0200745 if (fdtab) free(fdtab);
Willy Tarreau0fc45a72007-06-17 00:36:03 +0200746 fdtab = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200747
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200748 pool_destroy2(pool2_session);
Willy Tarreau7341d942007-05-13 19:56:02 +0200749 pool_destroy2(pool2_buffer);
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200750 pool_destroy2(pool2_requri);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200751 pool_destroy2(pool2_task);
Willy Tarreau086b3b42007-05-13 21:45:51 +0200752 pool_destroy2(pool2_capture);
Willy Tarreau63963c62007-05-13 21:29:55 +0200753 pool_destroy2(pool2_appsess);
Willy Tarreaue4d7e552007-05-13 20:19:55 +0200754 pool_destroy2(pool2_pendconn);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200755
756 if (have_appsession) {
Willy Tarreau63963c62007-05-13 21:29:55 +0200757 pool_destroy2(apools.serverid);
758 pool_destroy2(apools.sessid);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200759 }
760} /* end deinit() */
761
762/* sends the signal <sig> to all pids found in <oldpids> */
763static void tell_old_pids(int sig)
764{
765 int p;
766 for (p = 0; p < nb_oldpids; p++)
767 kill(oldpids[p], sig);
768}
769
Willy Tarreau4f60f162007-04-08 16:39:58 +0200770/*
771 * Runs the polling loop
772 *
773 * FIXME:
774 * - we still use 'listeners' to check whether we want to stop or not.
775 *
776 */
777void run_poll_loop()
778{
Willy Tarreaud825eef2007-05-12 22:35:00 +0200779 struct timeval next;
Willy Tarreau4f60f162007-04-08 16:39:58 +0200780
Willy Tarreaud825eef2007-05-12 22:35:00 +0200781 tv_now(&now);
Willy Tarreau4f60f162007-04-08 16:39:58 +0200782 while (1) {
Willy Tarreaud825eef2007-05-12 22:35:00 +0200783 process_runnable_tasks(&next);
Willy Tarreau4f60f162007-04-08 16:39:58 +0200784
785 /* stop when there's no connection left and we don't allow them anymore */
786 if (!actconn && listeners == 0)
787 break;
788
Willy Tarreaud825eef2007-05-12 22:35:00 +0200789 cur_poller.poll(&cur_poller, &next);
Willy Tarreau4f60f162007-04-08 16:39:58 +0200790 }
791}
792
793
Willy Tarreaubaaee002006-06-26 02:48:02 +0200794int main(int argc, char **argv)
795{
796 int err, retry;
797 struct rlimit limit;
798 FILE *pidfile = NULL;
799 init(argc, argv);
800
801 signal(SIGQUIT, dump);
802 signal(SIGUSR1, sig_soft_stop);
803 signal(SIGHUP, sig_dump_state);
804#ifdef DEBUG_MEMORY
805 signal(SIGINT, sig_int);
806 signal(SIGTERM, sig_term);
807#endif
808
809 /* on very high loads, a sigpipe sometimes happen just between the
810 * getsockopt() which tells "it's OK to write", and the following write :-(
811 */
812#ifndef MSG_NOSIGNAL
813 signal(SIGPIPE, SIG_IGN);
814#endif
815
816 /* We will loop at most 100 times with 10 ms delay each time.
817 * That's at most 1 second. We only send a signal to old pids
818 * if we cannot grab at least one port.
819 */
820 retry = MAX_START_RETRIES;
821 err = ERR_NONE;
822 while (retry >= 0) {
823 struct timeval w;
824 err = start_proxies(retry == 0 || nb_oldpids == 0);
825 if (err != ERR_RETRYABLE)
826 break;
827 if (nb_oldpids == 0)
828 break;
829
830 /* FIXME-20060514: Solaris and OpenBSD do not support shutdown() on
831 * listening sockets. So on those platforms, it would be wiser to
832 * simply send SIGUSR1, which will not be undoable.
833 */
834 tell_old_pids(SIGTTOU);
835 /* give some time to old processes to stop listening */
836 w.tv_sec = 0;
837 w.tv_usec = 10*1000;
838 select(0, NULL, NULL, NULL, &w);
839 retry--;
840 }
841
842 /* Note: start_proxies() sends an alert when it fails. */
843 if (err != ERR_NONE) {
844 if (retry != MAX_START_RETRIES && nb_oldpids)
845 tell_old_pids(SIGTTIN);
846 exit(1);
847 }
848
849 if (listeners == 0) {
850 Alert("[%s.main()] No enabled listener found (check the <listen> keywords) ! Exiting.\n", argv[0]);
851 /* Note: we don't have to send anything to the old pids because we
852 * never stopped them. */
853 exit(1);
854 }
855
Willy Tarreaudd815982007-10-16 12:25:14 +0200856 if (protocol_bind_all() != ERR_NONE) {
857 Alert("[%s.main()] Some protocols failed to start their listeners! Exiting.\n", argv[0]);
858 protocol_unbind_all(); /* cleanup everything we can */
859 if (nb_oldpids)
860 tell_old_pids(SIGTTIN);
861 exit(1);
862 }
863
Willy Tarreaubaaee002006-06-26 02:48:02 +0200864 /* prepare pause/play signals */
865 signal(SIGTTOU, sig_pause);
866 signal(SIGTTIN, sig_listen);
867
868 if (global.mode & MODE_DAEMON) {
869 global.mode &= ~MODE_VERBOSE;
870 global.mode |= MODE_QUIET;
871 }
872
873 /* MODE_QUIET can inhibit alerts and warnings below this line */
874
875 global.mode &= ~MODE_STARTING;
876 if ((global.mode & MODE_QUIET) && !(global.mode & MODE_VERBOSE)) {
877 /* detach from the tty */
878 fclose(stdin); fclose(stdout); fclose(stderr);
879 close(0); close(1); close(2);
880 }
881
882 /* open log & pid files before the chroot */
883 if (global.mode & MODE_DAEMON && global.pidfile != NULL) {
884 int pidfd;
885 unlink(global.pidfile);
886 pidfd = open(global.pidfile, O_CREAT | O_WRONLY | O_TRUNC, 0644);
887 if (pidfd < 0) {
888 Alert("[%s.main()] Cannot create pidfile %s\n", argv[0], global.pidfile);
889 if (nb_oldpids)
890 tell_old_pids(SIGTTIN);
Willy Tarreaudd815982007-10-16 12:25:14 +0200891 protocol_unbind_all();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200892 exit(1);
893 }
894 pidfile = fdopen(pidfd, "w");
895 }
896
Willy Tarreaubaaee002006-06-26 02:48:02 +0200897 /* ulimits */
898 if (!global.rlimit_nofile)
899 global.rlimit_nofile = global.maxsock;
900
901 if (global.rlimit_nofile) {
902 limit.rlim_cur = limit.rlim_max = global.rlimit_nofile;
903 if (setrlimit(RLIMIT_NOFILE, &limit) == -1) {
904 Warning("[%s.main()] Cannot raise FD limit to %d.\n", argv[0], global.rlimit_nofile);
905 }
906 }
907
908 if (global.rlimit_memmax) {
909 limit.rlim_cur = limit.rlim_max =
910 global.rlimit_memmax * 1048576 / global.nbproc;
911#ifdef RLIMIT_AS
912 if (setrlimit(RLIMIT_AS, &limit) == -1) {
913 Warning("[%s.main()] Cannot fix MEM limit to %d megs.\n",
914 argv[0], global.rlimit_memmax);
915 }
916#else
917 if (setrlimit(RLIMIT_DATA, &limit) == -1) {
918 Warning("[%s.main()] Cannot fix MEM limit to %d megs.\n",
919 argv[0], global.rlimit_memmax);
920 }
921#endif
922 }
923
Willy Tarreau6d1a9882007-01-07 02:03:04 +0100924#ifdef CONFIG_HAP_TCPSPLICE
925 if (global.last_checks & LSTCHK_TCPSPLICE) {
926 if (tcp_splice_start() < 0) {
927 Alert("[%s.main()] Cannot enable tcp_splice.\n"
928 " Make sure you have enough permissions and that the module is loadable.\n"
929 " Alternatively, you may disable the 'tcpsplice' options in the configuration.\n"
930 "", argv[0], global.gid);
Willy Tarreaudd815982007-10-16 12:25:14 +0200931 protocol_unbind_all();
Willy Tarreau6d1a9882007-01-07 02:03:04 +0100932 exit(1);
933 }
934 }
935#endif
936
Willy Tarreaub38651a2007-03-24 17:24:39 +0100937#ifdef CONFIG_HAP_CTTPROXY
938 if (global.last_checks & LSTCHK_CTTPROXY) {
939 int ret;
940
941 ret = check_cttproxy_version();
942 if (ret < 0) {
943 Alert("[%s.main()] Cannot enable cttproxy.\n%s",
944 argv[0],
945 (ret == -1) ? " Incorrect module version.\n"
946 : " Make sure you have enough permissions and that the module is loaded.\n");
Willy Tarreaudd815982007-10-16 12:25:14 +0200947 protocol_unbind_all();
Willy Tarreaub38651a2007-03-24 17:24:39 +0100948 exit(1);
949 }
950 }
951#endif
952
953 if ((global.last_checks & LSTCHK_NETADM) && global.uid) {
954 Alert("[%s.main()] Some configuration options require full privileges, so global.uid cannot be changed.\n"
955 "", argv[0], global.gid);
Willy Tarreaudd815982007-10-16 12:25:14 +0200956 protocol_unbind_all();
Willy Tarreaub38651a2007-03-24 17:24:39 +0100957 exit(1);
958 }
959
Willy Tarreauf223cc02007-10-15 18:57:08 +0200960 /* chroot if needed */
961 if (global.chroot != NULL) {
962 if (chroot(global.chroot) == -1) {
963 Alert("[%s.main()] Cannot chroot(%s).\n", argv[0], global.chroot);
964 if (nb_oldpids)
965 tell_old_pids(SIGTTIN);
Willy Tarreaudd815982007-10-16 12:25:14 +0200966 protocol_unbind_all();
Willy Tarreauf223cc02007-10-15 18:57:08 +0200967 exit(1);
968 }
969 chdir("/");
970 }
971
Willy Tarreaubaaee002006-06-26 02:48:02 +0200972 if (nb_oldpids)
973 tell_old_pids(oldpids_sig);
974
975 /* Note that any error at this stage will be fatal because we will not
976 * be able to restart the old pids.
977 */
978
979 /* setgid / setuid */
980 if (global.gid && setgid(global.gid) == -1) {
981 Alert("[%s.main()] Cannot set gid %d.\n", argv[0], global.gid);
Willy Tarreaudd815982007-10-16 12:25:14 +0200982 protocol_unbind_all();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200983 exit(1);
984 }
985
986 if (global.uid && setuid(global.uid) == -1) {
987 Alert("[%s.main()] Cannot set uid %d.\n", argv[0], global.uid);
Willy Tarreaudd815982007-10-16 12:25:14 +0200988 protocol_unbind_all();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200989 exit(1);
990 }
991
992 /* check ulimits */
993 limit.rlim_cur = limit.rlim_max = 0;
994 getrlimit(RLIMIT_NOFILE, &limit);
995 if (limit.rlim_cur < global.maxsock) {
996 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",
997 argv[0], limit.rlim_cur, global.maxconn, global.maxsock, global.maxsock);
998 }
999
1000 if (global.mode & MODE_DAEMON) {
1001 int ret = 0;
1002 int proc;
1003
1004 /* the father launches the required number of processes */
1005 for (proc = 0; proc < global.nbproc; proc++) {
1006 ret = fork();
1007 if (ret < 0) {
1008 Alert("[%s.main()] Cannot fork.\n", argv[0]);
Willy Tarreaudd815982007-10-16 12:25:14 +02001009 protocol_unbind_all();
Willy Tarreaud6803712007-10-16 07:44:56 +02001010 exit(1); /* there has been an error */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001011 }
1012 else if (ret == 0) /* child breaks here */
1013 break;
1014 if (pidfile != NULL) {
1015 fprintf(pidfile, "%d\n", ret);
1016 fflush(pidfile);
1017 }
1018 }
1019 /* close the pidfile both in children and father */
1020 if (pidfile != NULL)
1021 fclose(pidfile);
1022 free(global.pidfile);
Krzysztof Oledzki56f1e8b2007-10-11 18:30:14 +02001023 global.pidfile = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001024
1025 if (proc == global.nbproc)
1026 exit(0); /* parent must leave */
1027
1028 /* if we're NOT in QUIET mode, we should now close the 3 first FDs to ensure
1029 * that we can detach from the TTY. We MUST NOT do it in other cases since
1030 * it would have already be done, and 0-2 would have been affected to listening
1031 * sockets
1032 */
1033 if (!(global.mode & MODE_QUIET)) {
1034 /* detach from the tty */
1035 fclose(stdin); fclose(stdout); fclose(stderr);
1036 close(0); close(1); close(2); /* close all fd's */
1037 global.mode |= MODE_QUIET; /* ensure that we won't say anything from now */
1038 }
1039 pid = getpid(); /* update child's pid */
1040 setsid();
Willy Tarreau2ff76222007-04-09 19:29:56 +02001041 fork_poller();
Willy Tarreaubaaee002006-06-26 02:48:02 +02001042 }
1043
Willy Tarreaudd815982007-10-16 12:25:14 +02001044 protocol_enable_all();
Willy Tarreau4f60f162007-04-08 16:39:58 +02001045 /*
1046 * That's it : the central polling loop. Run until we stop.
1047 */
1048 run_poll_loop();
Willy Tarreaubaaee002006-06-26 02:48:02 +02001049
1050 /* Free all Hash Keys and all Hash elements */
1051 appsession_cleanup();
1052 /* Do some cleanup */
1053 deinit();
1054
1055 exit(0);
1056}
1057
1058
1059/*
1060 * Local variables:
1061 * c-indent-level: 8
1062 * c-basic-offset: 8
1063 * End:
1064 */