blob: 7f5641c4f568b29f1d6ae137db70fddaf2afee38 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * HA-Proxy : High Availability-enabled HTTP/TCP proxy
Willy Tarreau49e1ee82007-01-22 00:56:46 +01003 * Copyright 2000-2007 Willy Tarreau <w@1wt.eu>.
Willy Tarreaubaaee002006-06-26 02:48:02 +02004 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version
8 * 2 of the License, or (at your option) any later version.
9 *
10 * Please refer to RFC2068 or RFC2616 for informations about HTTP protocol, and
11 * RFC2965 for informations about cookies usage. More generally, the IETF HTTP
12 * Working Group's web site should be consulted for protocol related changes :
13 *
14 * http://ftp.ics.uci.edu/pub/ietf/http/
15 *
16 * Pending bugs (may be not fixed because never reproduced) :
17 * - solaris only : sometimes, an HTTP proxy with only a dispatch address causes
18 * the proxy to terminate (no core) if the client breaks the connection during
19 * the response. Seen on 1.1.8pre4, but never reproduced. May not be related to
20 * the snprintf() bug since requests were simple (GET / HTTP/1.0), but may be
21 * related to missing setsid() (fixed in 1.1.15)
22 * - a proxy with an invalid config will prevent the startup even if disabled.
23 *
24 * ChangeLog has moved to the CHANGELOG file.
25 *
26 * TODO:
27 * - handle properly intermediate incomplete server headers. Done ?
28 * - handle hot-reconfiguration
29 * - fix client/server state transition when server is in connect or headers state
30 * and client suddenly disconnects. The server *should* switch to SHUT_WR, but
31 * still handle HTTP headers.
32 * - remove MAX_NEWHDR
33 * - cut this huge file into several ones
34 *
35 */
36
37#include <stdio.h>
38#include <stdlib.h>
39#include <unistd.h>
40#include <string.h>
41#include <ctype.h>
42#include <sys/time.h>
43#include <sys/types.h>
44#include <sys/socket.h>
45#include <netinet/tcp.h>
46#include <netinet/in.h>
47#include <arpa/inet.h>
48#include <netdb.h>
49#include <fcntl.h>
50#include <errno.h>
51#include <signal.h>
52#include <stdarg.h>
53#include <sys/resource.h>
54#include <time.h>
55#include <syslog.h>
56
57#ifdef DEBUG_FULL
58#include <assert.h>
59#endif
60
Willy Tarreau2dd0d472006-06-29 17:53:05 +020061#include <common/appsession.h>
62#include <common/base64.h>
63#include <common/cfgparse.h>
64#include <common/compat.h>
65#include <common/config.h>
66#include <common/defaults.h>
67#include <common/memory.h>
68#include <common/mini-clist.h>
69#include <common/regex.h>
70#include <common/standard.h>
71#include <common/time.h>
72#include <common/uri_auth.h>
73#include <common/version.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020074
75#include <types/capture.h>
76#include <types/global.h>
77#include <types/httperr.h>
Willy Tarreau69801b82007-04-09 15:28:51 +020078#include <types/polling.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020079#include <types/proto_http.h>
80
81#include <proto/backend.h>
82#include <proto/buffers.h>
83#include <proto/client.h>
84#include <proto/fd.h>
85#include <proto/log.h>
Willy Tarreau80587432006-12-24 17:47:20 +010086#include <proto/proto_http.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020087#include <proto/proxy.h>
88#include <proto/queue.h>
89#include <proto/server.h>
Willy Tarreauc6ca1a02007-05-13 19:43:47 +020090#include <proto/session.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020091#include <proto/stream_sock.h>
92#include <proto/task.h>
93
Willy Tarreau6d1a9882007-01-07 02:03:04 +010094#ifdef CONFIG_HAP_TCPSPLICE
95#include <libtcpsplice.h>
96#endif
97
Willy Tarreaub38651a2007-03-24 17:24:39 +010098#ifdef CONFIG_HAP_CTTPROXY
99#include <proto/cttproxy.h>
100#endif
101
Willy Tarreaubaaee002006-06-26 02:48:02 +0200102/*********************************************************************/
103
104/*********************************************************************/
105
106char *cfg_cfgfile = NULL; /* configuration file */
107char *progname = NULL; /* program name */
108int pid; /* current process id */
109
110/* global options */
111struct global global = {
112 logfac1 : -1,
113 logfac2 : -1,
114 loglev1 : 7, /* max syslog level : debug */
115 loglev2 : 7,
116 /* others NULL OK */
117};
118
119/*********************************************************************/
120
121int stopping; /* non zero means stopping in progress */
122
123/* Here we store informations about the pids of the processes we may pause
124 * or kill. We will send them a signal every 10 ms until we can bind to all
125 * our ports. With 200 retries, that's about 2 seconds.
126 */
127#define MAX_START_RETRIES 200
128static int nb_oldpids = 0;
129static int *oldpids = NULL;
130static int oldpids_sig; /* use USR1 or TERM */
131
132/* this is used to drain data, and as a temporary buffer for sprintf()... */
133char trash[BUFSIZE];
134
135const int zero = 0;
136const int one = 1;
137
138/*
139 * Syslog facilities and levels. Conforming to RFC3164.
140 */
141
142#define MAX_HOSTNAME_LEN 32
143static char hostname[MAX_HOSTNAME_LEN] = "";
144
145
146/*********************************************************************/
147/* general purpose functions ***************************************/
148/*********************************************************************/
149
150void display_version()
151{
152 printf("HA-Proxy version " HAPROXY_VERSION " " HAPROXY_DATE"\n");
Willy Tarreau49e1ee82007-01-22 00:56:46 +0100153 printf("Copyright 2000-2007 Willy Tarreau <w@1wt.eu>\n\n");
Willy Tarreaubaaee002006-06-26 02:48:02 +0200154}
155
156/*
157 * This function prints the command line usage and exits
158 */
159void usage(char *name)
160{
161 display_version();
162 fprintf(stderr,
163 "Usage : %s -f <cfgfile> [ -vdV"
164 "D ] [ -n <maxconn> ] [ -N <maxpconn> ]\n"
165 " [ -p <pidfile> ] [ -m <max megs> ]\n"
166 " -v displays version\n"
167 " -d enters debug mode ; -db only disables background mode.\n"
168 " -V enters verbose mode (disables quiet mode)\n"
169 " -D goes daemon ; implies -q\n"
170 " -q quiet mode : don't display messages\n"
171 " -c check mode : only check config file and exit\n"
172 " -n sets the maximum total # of connections (%d)\n"
173 " -m limits the usable amount of memory (in MB)\n"
174 " -N sets the default, per-proxy maximum # of connections (%d)\n"
175 " -p writes pids of all children to this file\n"
176#if defined(ENABLE_EPOLL)
177 " -de disables epoll() usage even when available\n"
178#endif
Willy Tarreaude99e992007-04-16 00:53:59 +0200179#if defined(ENABLE_SEPOLL)
180 " -ds disables speculative epoll() usage even when available\n"
181#endif
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200182#if defined(ENABLE_KQUEUE)
183 " -dk disables kqueue() usage even when available\n"
184#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200185#if defined(ENABLE_POLL)
186 " -dp disables poll() usage even when available\n"
187#endif
188 " -sf/-st [pid ]* finishes/terminates old pids. Must be last arguments.\n"
189 "\n",
190 name, DEFAULT_MAXCONN, cfg_maxpconn);
191 exit(1);
192}
193
194
195
196/*********************************************************************/
197/* more specific functions ***************************************/
198/*********************************************************************/
199
200/*
201 * upon SIGUSR1, let's have a soft stop.
202 */
203void sig_soft_stop(int sig)
204{
205 soft_stop();
206 signal(sig, SIG_IGN);
207}
208
209/*
210 * upon SIGTTOU, we pause everything
211 */
212void sig_pause(int sig)
213{
214 pause_proxies();
215 signal(sig, sig_pause);
216}
217
218/*
219 * upon SIGTTIN, let's have a soft stop.
220 */
221void sig_listen(int sig)
222{
223 listen_proxies();
224 signal(sig, sig_listen);
225}
226
227/*
228 * this function dumps every server's state when the process receives SIGHUP.
229 */
230void sig_dump_state(int sig)
231{
232 struct proxy *p = proxy;
233
234 Warning("SIGHUP received, dumping servers states.\n");
235 while (p) {
236 struct server *s = p->srv;
237
238 send_log(p, LOG_NOTICE, "SIGHUP received, dumping servers states for proxy %s.\n", p->id);
239 while (s) {
240 snprintf(trash, sizeof(trash),
241 "SIGHUP: Server %s/%s is %s. Conn: %d act, %d pend, %d tot.",
242 p->id, s->id,
243 (s->state & SRV_RUNNING) ? "UP" : "DOWN",
244 s->cur_sess, s->nbpend, s->cum_sess);
245 Warning("%s\n", trash);
246 send_log(p, LOG_NOTICE, "%s\n", trash);
247 s = s->next;
248 }
249
250 if (p->srv_act == 0) {
251 snprintf(trash, sizeof(trash),
Willy Tarreauf1221aa2006-12-17 22:14:12 +0100252 "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 +0200253 p->id,
254 (p->srv_bck) ? "is running on backup servers" : "has no server available",
Willy Tarreauf1221aa2006-12-17 22:14:12 +0100255 p->feconn, p->beconn, p->totpend, p->nbpend, p->cum_feconn, p->cum_beconn);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200256 } else {
257 snprintf(trash, sizeof(trash),
258 "SIGHUP: Proxy %s has %d active servers and %d backup servers available."
Willy Tarreauf1221aa2006-12-17 22:14:12 +0100259 " Conn: act(FE+BE): %d+%d, %d pend (%d unass), tot(FE+BE): %d+%d.",
Willy Tarreaubaaee002006-06-26 02:48:02 +0200260 p->id, p->srv_act, p->srv_bck,
Willy Tarreauf1221aa2006-12-17 22:14:12 +0100261 p->feconn, p->beconn, p->totpend, p->nbpend, p->cum_feconn, p->cum_beconn);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200262 }
263 Warning("%s\n", trash);
264 send_log(p, LOG_NOTICE, "%s\n", trash);
265
266 p = p->next;
267 }
268 signal(sig, sig_dump_state);
269}
270
271void dump(int sig)
272{
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200273#if 0
Willy Tarreau964c9362007-01-07 00:38:00 +0100274 struct task *t;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200275 struct session *s;
Willy Tarreau964c9362007-01-07 00:38:00 +0100276 struct rb_node *node;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200277
Willy Tarreau964c9362007-01-07 00:38:00 +0100278 for(node = rb_first(&wait_queue[0]);
279 node != NULL; node = rb_next(node)) {
280 t = rb_entry(node, struct task, rb_node);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200281 s = t->context;
282 qfprintf(stderr,"[dump] wq: task %p, still %ld ms, "
283 "cli=%d, srv=%d, cr=%d, cw=%d, sr=%d, sw=%d, "
284 "req=%d, rep=%d, clifd=%d\n",
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200285 s, tv_ms_remain(&now, &t->expire),
Willy Tarreaubaaee002006-06-26 02:48:02 +0200286 s->cli_state,
287 s->srv_state,
Willy Tarreauf161a342007-04-08 16:59:42 +0200288 EV_FD_ISSET(s->cli_fd, DIR_RD),
289 EV_FD_ISSET(s->cli_fd, DIR_WR),
290 EV_FD_ISSET(s->srv_fd, DIR_RD),
291 EV_FD_ISSET(s->srv_fd, DIR_WR),
Willy Tarreaubaaee002006-06-26 02:48:02 +0200292 s->req->l, s->rep?s->rep->l:0, s->cli_fd
293 );
294 }
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200295#endif
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200296 /* dump memory usage then free everything possible */
297 dump_pools();
298 pool_gc2();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200299}
300
301#ifdef DEBUG_MEMORY
302static void fast_stop(void)
303{
304 struct proxy *p;
305 p = proxy;
306 while (p) {
307 p->grace = 0;
308 p = p->next;
309 }
310 soft_stop();
311}
312
313void sig_int(int sig)
314{
315 /* This would normally be a hard stop,
316 but we want to be sure about deallocation,
317 and so on, so we do a soft stop with
318 0 GRACE time
319 */
320 fast_stop();
321 /* If we are killed twice, we decide to die*/
322 signal(sig, SIG_DFL);
323}
324
325void sig_term(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();
333 /* If we are killed twice, we decide to die*/
334 signal(sig, SIG_DFL);
335}
336#endif
337
338
339/*
340 * This function initializes all the necessary variables. It only returns
341 * if everything is OK. If something fails, it exits.
342 */
343void init(int argc, char **argv)
344{
345 int i;
346 int arg_mode = 0; /* MODE_DEBUG, ... */
347 char *old_argv = *argv;
348 char *tmp;
349 char *cfg_pidfile = NULL;
350
351 if (1<<INTBITS != sizeof(int)*8) {
352 fprintf(stderr,
353 "Error: wrong architecture. Recompile so that sizeof(int)=%d\n",
354 (int)(sizeof(int)*8));
355 exit(1);
356 }
357
358 /*
359 * Initialize the previously static variables.
360 */
361
362 totalconn = actconn = maxfd = listeners = stopping = 0;
363
364
365#ifdef HAPROXY_MEMMAX
366 global.rlimit_memmax = HAPROXY_MEMMAX;
367#endif
368
369 /* initialize the libc's localtime structures once for all so that we
370 * won't be missing memory if we want to send alerts under OOM conditions.
Willy Tarreau2b35c952006-10-15 15:25:48 +0200371 * Also, the Alert() and Warning() functions need <now> to be initialized.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200372 */
373 tv_now(&now);
Willy Tarreaubf736132006-10-15 22:54:47 +0200374 localtime((time_t *)&now.tv_sec);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200375 start_date = now;
376
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200377 init_task();
378 init_session();
Willy Tarreaue4d7e552007-05-13 20:19:55 +0200379 init_buffer();
380 init_pendconn();
Willy Tarreau80587432006-12-24 17:47:20 +0100381 init_proto_http();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200382
383 cfg_polling_mechanism = POLL_USE_SELECT; /* select() is always available */
384#if defined(ENABLE_POLL)
385 cfg_polling_mechanism |= POLL_USE_POLL;
386#endif
387#if defined(ENABLE_EPOLL)
388 cfg_polling_mechanism |= POLL_USE_EPOLL;
389#endif
Willy Tarreaude99e992007-04-16 00:53:59 +0200390#if defined(ENABLE_SEPOLL)
391 cfg_polling_mechanism |= POLL_USE_SEPOLL;
392#endif
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200393#if defined(ENABLE_KQUEUE)
394 cfg_polling_mechanism |= POLL_USE_KQUEUE;
395#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200396
397 pid = getpid();
398 progname = *argv;
399 while ((tmp = strchr(progname, '/')) != NULL)
400 progname = tmp + 1;
401
402 argc--; argv++;
403 while (argc > 0) {
404 char *flag;
405
406 if (**argv == '-') {
407 flag = *argv+1;
408
409 /* 1 arg */
410 if (*flag == 'v') {
411 display_version();
412 exit(0);
413 }
414#if defined(ENABLE_EPOLL)
415 else if (*flag == 'd' && flag[1] == 'e')
416 cfg_polling_mechanism &= ~POLL_USE_EPOLL;
417#endif
Willy Tarreaude99e992007-04-16 00:53:59 +0200418#if defined(ENABLE_SEPOLL)
419 else if (*flag == 'd' && flag[1] == 's')
420 cfg_polling_mechanism &= ~POLL_USE_SEPOLL;
421#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200422#if defined(ENABLE_POLL)
423 else if (*flag == 'd' && flag[1] == 'p')
424 cfg_polling_mechanism &= ~POLL_USE_POLL;
425#endif
Willy Tarreau69cad1a2007-04-10 22:45:11 +0200426#if defined(ENABLE_KQUEUE)
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200427 else if (*flag == 'd' && flag[1] == 'k')
428 cfg_polling_mechanism &= ~POLL_USE_KQUEUE;
429#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200430 else if (*flag == 'V')
431 arg_mode |= MODE_VERBOSE;
432 else if (*flag == 'd' && flag[1] == 'b')
433 arg_mode |= MODE_FOREGROUND;
434 else if (*flag == 'd')
435 arg_mode |= MODE_DEBUG;
436 else if (*flag == 'c')
437 arg_mode |= MODE_CHECK;
438 else if (*flag == 'D')
439 arg_mode |= MODE_DAEMON | MODE_QUIET;
440 else if (*flag == 'q')
441 arg_mode |= MODE_QUIET;
442 else if (*flag == 's' && (flag[1] == 'f' || flag[1] == 't')) {
443 /* list of pids to finish ('f') or terminate ('t') */
444
445 if (flag[1] == 'f')
446 oldpids_sig = SIGUSR1; /* finish then exit */
447 else
448 oldpids_sig = SIGTERM; /* terminate immediately */
449 argv++; argc--;
450
451 if (argc > 0) {
452 oldpids = calloc(argc, sizeof(int));
453 while (argc > 0) {
454 oldpids[nb_oldpids] = atol(*argv);
455 if (oldpids[nb_oldpids] <= 0)
456 usage(old_argv);
457 argc--; argv++;
458 nb_oldpids++;
459 }
460 }
461 }
462 else { /* >=2 args */
463 argv++; argc--;
464 if (argc == 0)
465 usage(old_argv);
466
467 switch (*flag) {
468 case 'n' : cfg_maxconn = atol(*argv); break;
469 case 'm' : global.rlimit_memmax = atol(*argv); break;
470 case 'N' : cfg_maxpconn = atol(*argv); break;
471 case 'f' : cfg_cfgfile = *argv; break;
472 case 'p' : cfg_pidfile = *argv; break;
473 default: usage(old_argv);
474 }
475 }
476 }
477 else
478 usage(old_argv);
479 argv++; argc--;
480 }
481
482 global.mode = MODE_STARTING | /* during startup, we want most of the alerts */
483 (arg_mode & (MODE_DAEMON | MODE_FOREGROUND | MODE_VERBOSE
484 | MODE_QUIET | MODE_CHECK | MODE_DEBUG));
485
486 if (!cfg_cfgfile)
487 usage(old_argv);
488
489 gethostname(hostname, MAX_HOSTNAME_LEN);
490
491 have_appsession = 0;
492 global.maxsock = 10; /* reserve 10 fds ; will be incremented by socket eaters */
493 if (readcfgfile(cfg_cfgfile) < 0) {
494 Alert("Error reading configuration file : %s\n", cfg_cfgfile);
495 exit(1);
496 }
497 if (have_appsession)
498 appsession_init();
499
500 if (global.mode & MODE_CHECK) {
501 qfprintf(stdout, "Configuration file is valid : %s\n", cfg_cfgfile);
502 exit(0);
503 }
504
505 if (cfg_maxconn > 0)
506 global.maxconn = cfg_maxconn;
507
508 if (cfg_pidfile) {
509 if (global.pidfile)
510 free(global.pidfile);
511 global.pidfile = strdup(cfg_pidfile);
512 }
513
514 if (global.maxconn == 0)
515 global.maxconn = DEFAULT_MAXCONN;
516
517 global.maxsock += global.maxconn * 2; /* each connection needs two sockets */
518
519 if (arg_mode & (MODE_DEBUG | MODE_FOREGROUND)) {
520 /* command line debug mode inhibits configuration mode */
521 global.mode &= ~(MODE_DAEMON | MODE_QUIET);
522 }
523 global.mode |= (arg_mode & (MODE_DAEMON | MODE_FOREGROUND | MODE_QUIET |
524 MODE_VERBOSE | MODE_DEBUG | MODE_STATS | MODE_LOG));
525
526 if ((global.mode & MODE_DEBUG) && (global.mode & (MODE_DAEMON | MODE_QUIET))) {
527 Warning("<debug> mode incompatible with <quiet> and <daemon>. Keeping <debug> only.\n");
528 global.mode &= ~(MODE_DAEMON | MODE_QUIET);
529 }
530
531 if ((global.nbproc > 1) && !(global.mode & MODE_DAEMON)) {
532 if (!(global.mode & (MODE_FOREGROUND | MODE_DEBUG)))
533 Warning("<nbproc> is only meaningful in daemon mode. Setting limit to 1 process.\n");
534 global.nbproc = 1;
535 }
536
537 if (global.nbproc < 1)
538 global.nbproc = 1;
539
Willy Tarreaubaaee002006-06-26 02:48:02 +0200540 fdtab = (struct fdtab *)calloc(1,
541 sizeof(struct fdtab) * (global.maxsock));
542 for (i = 0; i < global.maxsock; i++) {
543 fdtab[i].state = FD_STCLOSE;
544 }
Willy Tarreau4f60f162007-04-08 16:39:58 +0200545
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200546 /*
547 * Note: we could register external pollers here.
548 * Built-in pollers have been registered before main().
549 */
Willy Tarreau4f60f162007-04-08 16:39:58 +0200550
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200551 if (!(cfg_polling_mechanism & POLL_USE_KQUEUE))
552 disable_poller("kqueue");
553
Willy Tarreau4f60f162007-04-08 16:39:58 +0200554 if (!(cfg_polling_mechanism & POLL_USE_EPOLL))
555 disable_poller("epoll");
556
Willy Tarreaude99e992007-04-16 00:53:59 +0200557 if (!(cfg_polling_mechanism & POLL_USE_SEPOLL))
558 disable_poller("sepoll");
559
Willy Tarreau4f60f162007-04-08 16:39:58 +0200560 if (!(cfg_polling_mechanism & POLL_USE_POLL))
561 disable_poller("poll");
562
563 if (!(cfg_polling_mechanism & POLL_USE_SELECT))
564 disable_poller("select");
565
566 /* Note: we could disable any poller by name here */
567
Willy Tarreau2ff76222007-04-09 19:29:56 +0200568 if (global.mode & (MODE_VERBOSE|MODE_DEBUG))
569 list_pollers(stderr);
570
Willy Tarreau4f60f162007-04-08 16:39:58 +0200571 if (!init_pollers()) {
Willy Tarreau2ff76222007-04-09 19:29:56 +0200572 Alert("No polling mechanism available.\n");
Willy Tarreau4f60f162007-04-08 16:39:58 +0200573 exit(1);
574 }
Willy Tarreau2ff76222007-04-09 19:29:56 +0200575 if (global.mode & (MODE_VERBOSE|MODE_DEBUG)) {
576 printf("Using %s() as the polling mechanism.\n", cur_poller.name);
Willy Tarreau4f60f162007-04-08 16:39:58 +0200577 }
578
Willy Tarreaubaaee002006-06-26 02:48:02 +0200579}
580
581void deinit(void)
582{
583 struct proxy *p = proxy;
584 struct cap_hdr *h,*h_next;
585 struct server *s,*s_next;
586 struct listener *l,*l_next;
587
588 while (p) {
589 if (p->id)
590 free(p->id);
591
592 if (p->check_req)
593 free(p->check_req);
594
595 if (p->cookie_name)
596 free(p->cookie_name);
597
598 if (p->capture_name)
599 free(p->capture_name);
600
601 /* only strup if the user have set in config.
602 When should we free it?!
603 if (p->errmsg.msg400) free(p->errmsg.msg400);
604 if (p->errmsg.msg403) free(p->errmsg.msg403);
605 if (p->errmsg.msg408) free(p->errmsg.msg408);
606 if (p->errmsg.msg500) free(p->errmsg.msg500);
607 if (p->errmsg.msg502) free(p->errmsg.msg502);
608 if (p->errmsg.msg503) free(p->errmsg.msg503);
609 if (p->errmsg.msg504) free(p->errmsg.msg504);
610 */
611 if (p->appsession_name)
612 free(p->appsession_name);
613
614 h = p->req_cap;
615 while (h) {
616 h_next = h->next;
617 if (h->name)
618 free(h->name);
619 pool_destroy(h->pool);
620 free(h);
621 h = h_next;
622 }/* end while(h) */
623
624 h = p->rsp_cap;
625 while (h) {
626 h_next = h->next;
627 if (h->name)
628 free(h->name);
629
630 pool_destroy(h->pool);
631 free(h);
632 h = h_next;
633 }/* end while(h) */
634
635 s = p->srv;
636 while (s) {
637 s_next = s->next;
638 if (s->id)
639 free(s->id);
640
641 if (s->cookie)
642 free(s->cookie);
643
644 free(s);
645 s = s_next;
646 }/* end while(s) */
647
648 l = p->listen;
649 while (l) {
650 l_next = l->next;
651 free(l);
652 l = l_next;
653 }/* end while(l) */
654
655 pool_destroy((void **) p->req_cap_pool);
656 pool_destroy((void **) p->rsp_cap_pool);
657 p = p->next;
658 }/* end while(p) */
659
660 if (global.chroot) free(global.chroot);
661 if (global.pidfile) free(global.pidfile);
662
Willy Tarreaubaaee002006-06-26 02:48:02 +0200663 if (fdtab) free(fdtab);
664
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200665 pool_destroy2(pool2_session);
Willy Tarreau7341d942007-05-13 19:56:02 +0200666 pool_destroy2(pool2_buffer);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200667 pool_destroy(pool_requri);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200668 pool_destroy2(pool2_task);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200669 pool_destroy(pool_capture);
Willy Tarreau63963c62007-05-13 21:29:55 +0200670 pool_destroy2(pool2_appsess);
Willy Tarreaue4d7e552007-05-13 20:19:55 +0200671 pool_destroy2(pool2_pendconn);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200672
673 if (have_appsession) {
Willy Tarreau63963c62007-05-13 21:29:55 +0200674 pool_destroy2(apools.serverid);
675 pool_destroy2(apools.sessid);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200676 }
677} /* end deinit() */
678
679/* sends the signal <sig> to all pids found in <oldpids> */
680static void tell_old_pids(int sig)
681{
682 int p;
683 for (p = 0; p < nb_oldpids; p++)
684 kill(oldpids[p], sig);
685}
686
Willy Tarreau4f60f162007-04-08 16:39:58 +0200687/*
688 * Runs the polling loop
689 *
690 * FIXME:
691 * - we still use 'listeners' to check whether we want to stop or not.
692 *
693 */
694void run_poll_loop()
695{
Willy Tarreaud825eef2007-05-12 22:35:00 +0200696 struct timeval next;
Willy Tarreau4f60f162007-04-08 16:39:58 +0200697
Willy Tarreaud825eef2007-05-12 22:35:00 +0200698 tv_now(&now);
Willy Tarreau4f60f162007-04-08 16:39:58 +0200699 while (1) {
Willy Tarreaud825eef2007-05-12 22:35:00 +0200700 process_runnable_tasks(&next);
Willy Tarreau4f60f162007-04-08 16:39:58 +0200701
702 /* stop when there's no connection left and we don't allow them anymore */
703 if (!actconn && listeners == 0)
704 break;
705
Willy Tarreaud825eef2007-05-12 22:35:00 +0200706 cur_poller.poll(&cur_poller, &next);
Willy Tarreau4f60f162007-04-08 16:39:58 +0200707 }
708}
709
710
Willy Tarreaubaaee002006-06-26 02:48:02 +0200711int main(int argc, char **argv)
712{
713 int err, retry;
714 struct rlimit limit;
715 FILE *pidfile = NULL;
716 init(argc, argv);
717
718 signal(SIGQUIT, dump);
719 signal(SIGUSR1, sig_soft_stop);
720 signal(SIGHUP, sig_dump_state);
721#ifdef DEBUG_MEMORY
722 signal(SIGINT, sig_int);
723 signal(SIGTERM, sig_term);
724#endif
725
726 /* on very high loads, a sigpipe sometimes happen just between the
727 * getsockopt() which tells "it's OK to write", and the following write :-(
728 */
729#ifndef MSG_NOSIGNAL
730 signal(SIGPIPE, SIG_IGN);
731#endif
732
733 /* We will loop at most 100 times with 10 ms delay each time.
734 * That's at most 1 second. We only send a signal to old pids
735 * if we cannot grab at least one port.
736 */
737 retry = MAX_START_RETRIES;
738 err = ERR_NONE;
739 while (retry >= 0) {
740 struct timeval w;
741 err = start_proxies(retry == 0 || nb_oldpids == 0);
742 if (err != ERR_RETRYABLE)
743 break;
744 if (nb_oldpids == 0)
745 break;
746
747 /* FIXME-20060514: Solaris and OpenBSD do not support shutdown() on
748 * listening sockets. So on those platforms, it would be wiser to
749 * simply send SIGUSR1, which will not be undoable.
750 */
751 tell_old_pids(SIGTTOU);
752 /* give some time to old processes to stop listening */
753 w.tv_sec = 0;
754 w.tv_usec = 10*1000;
755 select(0, NULL, NULL, NULL, &w);
756 retry--;
757 }
758
759 /* Note: start_proxies() sends an alert when it fails. */
760 if (err != ERR_NONE) {
761 if (retry != MAX_START_RETRIES && nb_oldpids)
762 tell_old_pids(SIGTTIN);
763 exit(1);
764 }
765
766 if (listeners == 0) {
767 Alert("[%s.main()] No enabled listener found (check the <listen> keywords) ! Exiting.\n", argv[0]);
768 /* Note: we don't have to send anything to the old pids because we
769 * never stopped them. */
770 exit(1);
771 }
772
773 /* prepare pause/play signals */
774 signal(SIGTTOU, sig_pause);
775 signal(SIGTTIN, sig_listen);
776
777 if (global.mode & MODE_DAEMON) {
778 global.mode &= ~MODE_VERBOSE;
779 global.mode |= MODE_QUIET;
780 }
781
782 /* MODE_QUIET can inhibit alerts and warnings below this line */
783
784 global.mode &= ~MODE_STARTING;
785 if ((global.mode & MODE_QUIET) && !(global.mode & MODE_VERBOSE)) {
786 /* detach from the tty */
787 fclose(stdin); fclose(stdout); fclose(stderr);
788 close(0); close(1); close(2);
789 }
790
791 /* open log & pid files before the chroot */
792 if (global.mode & MODE_DAEMON && global.pidfile != NULL) {
793 int pidfd;
794 unlink(global.pidfile);
795 pidfd = open(global.pidfile, O_CREAT | O_WRONLY | O_TRUNC, 0644);
796 if (pidfd < 0) {
797 Alert("[%s.main()] Cannot create pidfile %s\n", argv[0], global.pidfile);
798 if (nb_oldpids)
799 tell_old_pids(SIGTTIN);
800 exit(1);
801 }
802 pidfile = fdopen(pidfd, "w");
803 }
804
805 /* chroot if needed */
806 if (global.chroot != NULL) {
807 if (chroot(global.chroot) == -1) {
808 Alert("[%s.main()] Cannot chroot(%s).\n", argv[0], global.chroot);
809 if (nb_oldpids)
810 tell_old_pids(SIGTTIN);
811 }
812 chdir("/");
813 }
814
815 /* ulimits */
816 if (!global.rlimit_nofile)
817 global.rlimit_nofile = global.maxsock;
818
819 if (global.rlimit_nofile) {
820 limit.rlim_cur = limit.rlim_max = global.rlimit_nofile;
821 if (setrlimit(RLIMIT_NOFILE, &limit) == -1) {
822 Warning("[%s.main()] Cannot raise FD limit to %d.\n", argv[0], global.rlimit_nofile);
823 }
824 }
825
826 if (global.rlimit_memmax) {
827 limit.rlim_cur = limit.rlim_max =
828 global.rlimit_memmax * 1048576 / global.nbproc;
829#ifdef RLIMIT_AS
830 if (setrlimit(RLIMIT_AS, &limit) == -1) {
831 Warning("[%s.main()] Cannot fix MEM limit to %d megs.\n",
832 argv[0], global.rlimit_memmax);
833 }
834#else
835 if (setrlimit(RLIMIT_DATA, &limit) == -1) {
836 Warning("[%s.main()] Cannot fix MEM limit to %d megs.\n",
837 argv[0], global.rlimit_memmax);
838 }
839#endif
840 }
841
Willy Tarreau6d1a9882007-01-07 02:03:04 +0100842#ifdef CONFIG_HAP_TCPSPLICE
843 if (global.last_checks & LSTCHK_TCPSPLICE) {
844 if (tcp_splice_start() < 0) {
845 Alert("[%s.main()] Cannot enable tcp_splice.\n"
846 " Make sure you have enough permissions and that the module is loadable.\n"
847 " Alternatively, you may disable the 'tcpsplice' options in the configuration.\n"
848 "", argv[0], global.gid);
849 exit(1);
850 }
851 }
852#endif
853
Willy Tarreaub38651a2007-03-24 17:24:39 +0100854#ifdef CONFIG_HAP_CTTPROXY
855 if (global.last_checks & LSTCHK_CTTPROXY) {
856 int ret;
857
858 ret = check_cttproxy_version();
859 if (ret < 0) {
860 Alert("[%s.main()] Cannot enable cttproxy.\n%s",
861 argv[0],
862 (ret == -1) ? " Incorrect module version.\n"
863 : " Make sure you have enough permissions and that the module is loaded.\n");
864 exit(1);
865 }
866 }
867#endif
868
869 if ((global.last_checks & LSTCHK_NETADM) && global.uid) {
870 Alert("[%s.main()] Some configuration options require full privileges, so global.uid cannot be changed.\n"
871 "", argv[0], global.gid);
872 exit(1);
873 }
874
Willy Tarreaubaaee002006-06-26 02:48:02 +0200875 if (nb_oldpids)
876 tell_old_pids(oldpids_sig);
877
878 /* Note that any error at this stage will be fatal because we will not
879 * be able to restart the old pids.
880 */
881
882 /* setgid / setuid */
883 if (global.gid && setgid(global.gid) == -1) {
884 Alert("[%s.main()] Cannot set gid %d.\n", argv[0], global.gid);
885 exit(1);
886 }
887
888 if (global.uid && setuid(global.uid) == -1) {
889 Alert("[%s.main()] Cannot set uid %d.\n", argv[0], global.uid);
890 exit(1);
891 }
892
893 /* check ulimits */
894 limit.rlim_cur = limit.rlim_max = 0;
895 getrlimit(RLIMIT_NOFILE, &limit);
896 if (limit.rlim_cur < global.maxsock) {
897 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",
898 argv[0], limit.rlim_cur, global.maxconn, global.maxsock, global.maxsock);
899 }
900
901 if (global.mode & MODE_DAEMON) {
902 int ret = 0;
903 int proc;
904
905 /* the father launches the required number of processes */
906 for (proc = 0; proc < global.nbproc; proc++) {
907 ret = fork();
908 if (ret < 0) {
909 Alert("[%s.main()] Cannot fork.\n", argv[0]);
910 if (nb_oldpids)
911 exit(1); /* there has been an error */
912 }
913 else if (ret == 0) /* child breaks here */
914 break;
915 if (pidfile != NULL) {
916 fprintf(pidfile, "%d\n", ret);
917 fflush(pidfile);
918 }
919 }
920 /* close the pidfile both in children and father */
921 if (pidfile != NULL)
922 fclose(pidfile);
923 free(global.pidfile);
924
925 if (proc == global.nbproc)
926 exit(0); /* parent must leave */
927
928 /* if we're NOT in QUIET mode, we should now close the 3 first FDs to ensure
929 * that we can detach from the TTY. We MUST NOT do it in other cases since
930 * it would have already be done, and 0-2 would have been affected to listening
931 * sockets
932 */
933 if (!(global.mode & MODE_QUIET)) {
934 /* detach from the tty */
935 fclose(stdin); fclose(stdout); fclose(stderr);
936 close(0); close(1); close(2); /* close all fd's */
937 global.mode |= MODE_QUIET; /* ensure that we won't say anything from now */
938 }
939 pid = getpid(); /* update child's pid */
940 setsid();
Willy Tarreau2ff76222007-04-09 19:29:56 +0200941 fork_poller();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200942 }
943
Willy Tarreau4f60f162007-04-08 16:39:58 +0200944 /*
945 * That's it : the central polling loop. Run until we stop.
946 */
947 run_poll_loop();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200948
949 /* Free all Hash Keys and all Hash elements */
950 appsession_cleanup();
951 /* Do some cleanup */
952 deinit();
953
954 exit(0);
955}
956
957
958/*
959 * Local variables:
960 * c-indent-level: 8
961 * c-basic-offset: 8
962 * End:
963 */