blob: 59c61497891e81f48e646020c079cc4f307b9171 [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 Tarreau7341d942007-05-13 19:56:02 +0200377 init_buffer();
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200378 init_task();
379 init_session();
Willy Tarreau80587432006-12-24 17:47:20 +0100380 init_proto_http();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200381
382 cfg_polling_mechanism = POLL_USE_SELECT; /* select() is always available */
383#if defined(ENABLE_POLL)
384 cfg_polling_mechanism |= POLL_USE_POLL;
385#endif
386#if defined(ENABLE_EPOLL)
387 cfg_polling_mechanism |= POLL_USE_EPOLL;
388#endif
Willy Tarreaude99e992007-04-16 00:53:59 +0200389#if defined(ENABLE_SEPOLL)
390 cfg_polling_mechanism |= POLL_USE_SEPOLL;
391#endif
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200392#if defined(ENABLE_KQUEUE)
393 cfg_polling_mechanism |= POLL_USE_KQUEUE;
394#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200395
396 pid = getpid();
397 progname = *argv;
398 while ((tmp = strchr(progname, '/')) != NULL)
399 progname = tmp + 1;
400
401 argc--; argv++;
402 while (argc > 0) {
403 char *flag;
404
405 if (**argv == '-') {
406 flag = *argv+1;
407
408 /* 1 arg */
409 if (*flag == 'v') {
410 display_version();
411 exit(0);
412 }
413#if defined(ENABLE_EPOLL)
414 else if (*flag == 'd' && flag[1] == 'e')
415 cfg_polling_mechanism &= ~POLL_USE_EPOLL;
416#endif
Willy Tarreaude99e992007-04-16 00:53:59 +0200417#if defined(ENABLE_SEPOLL)
418 else if (*flag == 'd' && flag[1] == 's')
419 cfg_polling_mechanism &= ~POLL_USE_SEPOLL;
420#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200421#if defined(ENABLE_POLL)
422 else if (*flag == 'd' && flag[1] == 'p')
423 cfg_polling_mechanism &= ~POLL_USE_POLL;
424#endif
Willy Tarreau69cad1a2007-04-10 22:45:11 +0200425#if defined(ENABLE_KQUEUE)
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200426 else if (*flag == 'd' && flag[1] == 'k')
427 cfg_polling_mechanism &= ~POLL_USE_KQUEUE;
428#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200429 else if (*flag == 'V')
430 arg_mode |= MODE_VERBOSE;
431 else if (*flag == 'd' && flag[1] == 'b')
432 arg_mode |= MODE_FOREGROUND;
433 else if (*flag == 'd')
434 arg_mode |= MODE_DEBUG;
435 else if (*flag == 'c')
436 arg_mode |= MODE_CHECK;
437 else if (*flag == 'D')
438 arg_mode |= MODE_DAEMON | MODE_QUIET;
439 else if (*flag == 'q')
440 arg_mode |= MODE_QUIET;
441 else if (*flag == 's' && (flag[1] == 'f' || flag[1] == 't')) {
442 /* list of pids to finish ('f') or terminate ('t') */
443
444 if (flag[1] == 'f')
445 oldpids_sig = SIGUSR1; /* finish then exit */
446 else
447 oldpids_sig = SIGTERM; /* terminate immediately */
448 argv++; argc--;
449
450 if (argc > 0) {
451 oldpids = calloc(argc, sizeof(int));
452 while (argc > 0) {
453 oldpids[nb_oldpids] = atol(*argv);
454 if (oldpids[nb_oldpids] <= 0)
455 usage(old_argv);
456 argc--; argv++;
457 nb_oldpids++;
458 }
459 }
460 }
461 else { /* >=2 args */
462 argv++; argc--;
463 if (argc == 0)
464 usage(old_argv);
465
466 switch (*flag) {
467 case 'n' : cfg_maxconn = atol(*argv); break;
468 case 'm' : global.rlimit_memmax = atol(*argv); break;
469 case 'N' : cfg_maxpconn = atol(*argv); break;
470 case 'f' : cfg_cfgfile = *argv; break;
471 case 'p' : cfg_pidfile = *argv; break;
472 default: usage(old_argv);
473 }
474 }
475 }
476 else
477 usage(old_argv);
478 argv++; argc--;
479 }
480
481 global.mode = MODE_STARTING | /* during startup, we want most of the alerts */
482 (arg_mode & (MODE_DAEMON | MODE_FOREGROUND | MODE_VERBOSE
483 | MODE_QUIET | MODE_CHECK | MODE_DEBUG));
484
485 if (!cfg_cfgfile)
486 usage(old_argv);
487
488 gethostname(hostname, MAX_HOSTNAME_LEN);
489
490 have_appsession = 0;
491 global.maxsock = 10; /* reserve 10 fds ; will be incremented by socket eaters */
492 if (readcfgfile(cfg_cfgfile) < 0) {
493 Alert("Error reading configuration file : %s\n", cfg_cfgfile);
494 exit(1);
495 }
496 if (have_appsession)
497 appsession_init();
498
499 if (global.mode & MODE_CHECK) {
500 qfprintf(stdout, "Configuration file is valid : %s\n", cfg_cfgfile);
501 exit(0);
502 }
503
504 if (cfg_maxconn > 0)
505 global.maxconn = cfg_maxconn;
506
507 if (cfg_pidfile) {
508 if (global.pidfile)
509 free(global.pidfile);
510 global.pidfile = strdup(cfg_pidfile);
511 }
512
513 if (global.maxconn == 0)
514 global.maxconn = DEFAULT_MAXCONN;
515
516 global.maxsock += global.maxconn * 2; /* each connection needs two sockets */
517
518 if (arg_mode & (MODE_DEBUG | MODE_FOREGROUND)) {
519 /* command line debug mode inhibits configuration mode */
520 global.mode &= ~(MODE_DAEMON | MODE_QUIET);
521 }
522 global.mode |= (arg_mode & (MODE_DAEMON | MODE_FOREGROUND | MODE_QUIET |
523 MODE_VERBOSE | MODE_DEBUG | MODE_STATS | MODE_LOG));
524
525 if ((global.mode & MODE_DEBUG) && (global.mode & (MODE_DAEMON | MODE_QUIET))) {
526 Warning("<debug> mode incompatible with <quiet> and <daemon>. Keeping <debug> only.\n");
527 global.mode &= ~(MODE_DAEMON | MODE_QUIET);
528 }
529
530 if ((global.nbproc > 1) && !(global.mode & MODE_DAEMON)) {
531 if (!(global.mode & (MODE_FOREGROUND | MODE_DEBUG)))
532 Warning("<nbproc> is only meaningful in daemon mode. Setting limit to 1 process.\n");
533 global.nbproc = 1;
534 }
535
536 if (global.nbproc < 1)
537 global.nbproc = 1;
538
Willy Tarreaubaaee002006-06-26 02:48:02 +0200539 fdtab = (struct fdtab *)calloc(1,
540 sizeof(struct fdtab) * (global.maxsock));
541 for (i = 0; i < global.maxsock; i++) {
542 fdtab[i].state = FD_STCLOSE;
543 }
Willy Tarreau4f60f162007-04-08 16:39:58 +0200544
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200545 /*
546 * Note: we could register external pollers here.
547 * Built-in pollers have been registered before main().
548 */
Willy Tarreau4f60f162007-04-08 16:39:58 +0200549
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200550 if (!(cfg_polling_mechanism & POLL_USE_KQUEUE))
551 disable_poller("kqueue");
552
Willy Tarreau4f60f162007-04-08 16:39:58 +0200553 if (!(cfg_polling_mechanism & POLL_USE_EPOLL))
554 disable_poller("epoll");
555
Willy Tarreaude99e992007-04-16 00:53:59 +0200556 if (!(cfg_polling_mechanism & POLL_USE_SEPOLL))
557 disable_poller("sepoll");
558
Willy Tarreau4f60f162007-04-08 16:39:58 +0200559 if (!(cfg_polling_mechanism & POLL_USE_POLL))
560 disable_poller("poll");
561
562 if (!(cfg_polling_mechanism & POLL_USE_SELECT))
563 disable_poller("select");
564
565 /* Note: we could disable any poller by name here */
566
Willy Tarreau2ff76222007-04-09 19:29:56 +0200567 if (global.mode & (MODE_VERBOSE|MODE_DEBUG))
568 list_pollers(stderr);
569
Willy Tarreau4f60f162007-04-08 16:39:58 +0200570 if (!init_pollers()) {
Willy Tarreau2ff76222007-04-09 19:29:56 +0200571 Alert("No polling mechanism available.\n");
Willy Tarreau4f60f162007-04-08 16:39:58 +0200572 exit(1);
573 }
Willy Tarreau2ff76222007-04-09 19:29:56 +0200574 if (global.mode & (MODE_VERBOSE|MODE_DEBUG)) {
575 printf("Using %s() as the polling mechanism.\n", cur_poller.name);
Willy Tarreau4f60f162007-04-08 16:39:58 +0200576 }
577
Willy Tarreaubaaee002006-06-26 02:48:02 +0200578}
579
580void deinit(void)
581{
582 struct proxy *p = proxy;
583 struct cap_hdr *h,*h_next;
584 struct server *s,*s_next;
585 struct listener *l,*l_next;
586
587 while (p) {
588 if (p->id)
589 free(p->id);
590
591 if (p->check_req)
592 free(p->check_req);
593
594 if (p->cookie_name)
595 free(p->cookie_name);
596
597 if (p->capture_name)
598 free(p->capture_name);
599
600 /* only strup if the user have set in config.
601 When should we free it?!
602 if (p->errmsg.msg400) free(p->errmsg.msg400);
603 if (p->errmsg.msg403) free(p->errmsg.msg403);
604 if (p->errmsg.msg408) free(p->errmsg.msg408);
605 if (p->errmsg.msg500) free(p->errmsg.msg500);
606 if (p->errmsg.msg502) free(p->errmsg.msg502);
607 if (p->errmsg.msg503) free(p->errmsg.msg503);
608 if (p->errmsg.msg504) free(p->errmsg.msg504);
609 */
610 if (p->appsession_name)
611 free(p->appsession_name);
612
613 h = p->req_cap;
614 while (h) {
615 h_next = h->next;
616 if (h->name)
617 free(h->name);
618 pool_destroy(h->pool);
619 free(h);
620 h = h_next;
621 }/* end while(h) */
622
623 h = p->rsp_cap;
624 while (h) {
625 h_next = h->next;
626 if (h->name)
627 free(h->name);
628
629 pool_destroy(h->pool);
630 free(h);
631 h = h_next;
632 }/* end while(h) */
633
634 s = p->srv;
635 while (s) {
636 s_next = s->next;
637 if (s->id)
638 free(s->id);
639
640 if (s->cookie)
641 free(s->cookie);
642
643 free(s);
644 s = s_next;
645 }/* end while(s) */
646
647 l = p->listen;
648 while (l) {
649 l_next = l->next;
650 free(l);
651 l = l_next;
652 }/* end while(l) */
653
654 pool_destroy((void **) p->req_cap_pool);
655 pool_destroy((void **) p->rsp_cap_pool);
656 p = p->next;
657 }/* end while(p) */
658
659 if (global.chroot) free(global.chroot);
660 if (global.pidfile) free(global.pidfile);
661
Willy Tarreaubaaee002006-06-26 02:48:02 +0200662 if (fdtab) free(fdtab);
663
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200664 pool_destroy2(pool2_session);
Willy Tarreau7341d942007-05-13 19:56:02 +0200665 pool_destroy2(pool2_buffer);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200666 pool_destroy(pool_requri);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200667 pool_destroy2(pool2_task);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200668 pool_destroy(pool_capture);
669 pool_destroy(pool_appsess);
670
671 if (have_appsession) {
672 pool_destroy(apools.serverid);
673 pool_destroy(apools.sessid);
674 }
675} /* end deinit() */
676
677/* sends the signal <sig> to all pids found in <oldpids> */
678static void tell_old_pids(int sig)
679{
680 int p;
681 for (p = 0; p < nb_oldpids; p++)
682 kill(oldpids[p], sig);
683}
684
Willy Tarreau4f60f162007-04-08 16:39:58 +0200685/*
686 * Runs the polling loop
687 *
688 * FIXME:
689 * - we still use 'listeners' to check whether we want to stop or not.
690 *
691 */
692void run_poll_loop()
693{
Willy Tarreaud825eef2007-05-12 22:35:00 +0200694 struct timeval next;
Willy Tarreau4f60f162007-04-08 16:39:58 +0200695
Willy Tarreaud825eef2007-05-12 22:35:00 +0200696 tv_now(&now);
Willy Tarreau4f60f162007-04-08 16:39:58 +0200697 while (1) {
Willy Tarreaud825eef2007-05-12 22:35:00 +0200698 process_runnable_tasks(&next);
Willy Tarreau4f60f162007-04-08 16:39:58 +0200699
700 /* stop when there's no connection left and we don't allow them anymore */
701 if (!actconn && listeners == 0)
702 break;
703
Willy Tarreaud825eef2007-05-12 22:35:00 +0200704 cur_poller.poll(&cur_poller, &next);
Willy Tarreau4f60f162007-04-08 16:39:58 +0200705 }
706}
707
708
Willy Tarreaubaaee002006-06-26 02:48:02 +0200709int main(int argc, char **argv)
710{
711 int err, retry;
712 struct rlimit limit;
713 FILE *pidfile = NULL;
714 init(argc, argv);
715
716 signal(SIGQUIT, dump);
717 signal(SIGUSR1, sig_soft_stop);
718 signal(SIGHUP, sig_dump_state);
719#ifdef DEBUG_MEMORY
720 signal(SIGINT, sig_int);
721 signal(SIGTERM, sig_term);
722#endif
723
724 /* on very high loads, a sigpipe sometimes happen just between the
725 * getsockopt() which tells "it's OK to write", and the following write :-(
726 */
727#ifndef MSG_NOSIGNAL
728 signal(SIGPIPE, SIG_IGN);
729#endif
730
731 /* We will loop at most 100 times with 10 ms delay each time.
732 * That's at most 1 second. We only send a signal to old pids
733 * if we cannot grab at least one port.
734 */
735 retry = MAX_START_RETRIES;
736 err = ERR_NONE;
737 while (retry >= 0) {
738 struct timeval w;
739 err = start_proxies(retry == 0 || nb_oldpids == 0);
740 if (err != ERR_RETRYABLE)
741 break;
742 if (nb_oldpids == 0)
743 break;
744
745 /* FIXME-20060514: Solaris and OpenBSD do not support shutdown() on
746 * listening sockets. So on those platforms, it would be wiser to
747 * simply send SIGUSR1, which will not be undoable.
748 */
749 tell_old_pids(SIGTTOU);
750 /* give some time to old processes to stop listening */
751 w.tv_sec = 0;
752 w.tv_usec = 10*1000;
753 select(0, NULL, NULL, NULL, &w);
754 retry--;
755 }
756
757 /* Note: start_proxies() sends an alert when it fails. */
758 if (err != ERR_NONE) {
759 if (retry != MAX_START_RETRIES && nb_oldpids)
760 tell_old_pids(SIGTTIN);
761 exit(1);
762 }
763
764 if (listeners == 0) {
765 Alert("[%s.main()] No enabled listener found (check the <listen> keywords) ! Exiting.\n", argv[0]);
766 /* Note: we don't have to send anything to the old pids because we
767 * never stopped them. */
768 exit(1);
769 }
770
771 /* prepare pause/play signals */
772 signal(SIGTTOU, sig_pause);
773 signal(SIGTTIN, sig_listen);
774
775 if (global.mode & MODE_DAEMON) {
776 global.mode &= ~MODE_VERBOSE;
777 global.mode |= MODE_QUIET;
778 }
779
780 /* MODE_QUIET can inhibit alerts and warnings below this line */
781
782 global.mode &= ~MODE_STARTING;
783 if ((global.mode & MODE_QUIET) && !(global.mode & MODE_VERBOSE)) {
784 /* detach from the tty */
785 fclose(stdin); fclose(stdout); fclose(stderr);
786 close(0); close(1); close(2);
787 }
788
789 /* open log & pid files before the chroot */
790 if (global.mode & MODE_DAEMON && global.pidfile != NULL) {
791 int pidfd;
792 unlink(global.pidfile);
793 pidfd = open(global.pidfile, O_CREAT | O_WRONLY | O_TRUNC, 0644);
794 if (pidfd < 0) {
795 Alert("[%s.main()] Cannot create pidfile %s\n", argv[0], global.pidfile);
796 if (nb_oldpids)
797 tell_old_pids(SIGTTIN);
798 exit(1);
799 }
800 pidfile = fdopen(pidfd, "w");
801 }
802
803 /* chroot if needed */
804 if (global.chroot != NULL) {
805 if (chroot(global.chroot) == -1) {
806 Alert("[%s.main()] Cannot chroot(%s).\n", argv[0], global.chroot);
807 if (nb_oldpids)
808 tell_old_pids(SIGTTIN);
809 }
810 chdir("/");
811 }
812
813 /* ulimits */
814 if (!global.rlimit_nofile)
815 global.rlimit_nofile = global.maxsock;
816
817 if (global.rlimit_nofile) {
818 limit.rlim_cur = limit.rlim_max = global.rlimit_nofile;
819 if (setrlimit(RLIMIT_NOFILE, &limit) == -1) {
820 Warning("[%s.main()] Cannot raise FD limit to %d.\n", argv[0], global.rlimit_nofile);
821 }
822 }
823
824 if (global.rlimit_memmax) {
825 limit.rlim_cur = limit.rlim_max =
826 global.rlimit_memmax * 1048576 / global.nbproc;
827#ifdef RLIMIT_AS
828 if (setrlimit(RLIMIT_AS, &limit) == -1) {
829 Warning("[%s.main()] Cannot fix MEM limit to %d megs.\n",
830 argv[0], global.rlimit_memmax);
831 }
832#else
833 if (setrlimit(RLIMIT_DATA, &limit) == -1) {
834 Warning("[%s.main()] Cannot fix MEM limit to %d megs.\n",
835 argv[0], global.rlimit_memmax);
836 }
837#endif
838 }
839
Willy Tarreau6d1a9882007-01-07 02:03:04 +0100840#ifdef CONFIG_HAP_TCPSPLICE
841 if (global.last_checks & LSTCHK_TCPSPLICE) {
842 if (tcp_splice_start() < 0) {
843 Alert("[%s.main()] Cannot enable tcp_splice.\n"
844 " Make sure you have enough permissions and that the module is loadable.\n"
845 " Alternatively, you may disable the 'tcpsplice' options in the configuration.\n"
846 "", argv[0], global.gid);
847 exit(1);
848 }
849 }
850#endif
851
Willy Tarreaub38651a2007-03-24 17:24:39 +0100852#ifdef CONFIG_HAP_CTTPROXY
853 if (global.last_checks & LSTCHK_CTTPROXY) {
854 int ret;
855
856 ret = check_cttproxy_version();
857 if (ret < 0) {
858 Alert("[%s.main()] Cannot enable cttproxy.\n%s",
859 argv[0],
860 (ret == -1) ? " Incorrect module version.\n"
861 : " Make sure you have enough permissions and that the module is loaded.\n");
862 exit(1);
863 }
864 }
865#endif
866
867 if ((global.last_checks & LSTCHK_NETADM) && global.uid) {
868 Alert("[%s.main()] Some configuration options require full privileges, so global.uid cannot be changed.\n"
869 "", argv[0], global.gid);
870 exit(1);
871 }
872
Willy Tarreaubaaee002006-06-26 02:48:02 +0200873 if (nb_oldpids)
874 tell_old_pids(oldpids_sig);
875
876 /* Note that any error at this stage will be fatal because we will not
877 * be able to restart the old pids.
878 */
879
880 /* setgid / setuid */
881 if (global.gid && setgid(global.gid) == -1) {
882 Alert("[%s.main()] Cannot set gid %d.\n", argv[0], global.gid);
883 exit(1);
884 }
885
886 if (global.uid && setuid(global.uid) == -1) {
887 Alert("[%s.main()] Cannot set uid %d.\n", argv[0], global.uid);
888 exit(1);
889 }
890
891 /* check ulimits */
892 limit.rlim_cur = limit.rlim_max = 0;
893 getrlimit(RLIMIT_NOFILE, &limit);
894 if (limit.rlim_cur < global.maxsock) {
895 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",
896 argv[0], limit.rlim_cur, global.maxconn, global.maxsock, global.maxsock);
897 }
898
899 if (global.mode & MODE_DAEMON) {
900 int ret = 0;
901 int proc;
902
903 /* the father launches the required number of processes */
904 for (proc = 0; proc < global.nbproc; proc++) {
905 ret = fork();
906 if (ret < 0) {
907 Alert("[%s.main()] Cannot fork.\n", argv[0]);
908 if (nb_oldpids)
909 exit(1); /* there has been an error */
910 }
911 else if (ret == 0) /* child breaks here */
912 break;
913 if (pidfile != NULL) {
914 fprintf(pidfile, "%d\n", ret);
915 fflush(pidfile);
916 }
917 }
918 /* close the pidfile both in children and father */
919 if (pidfile != NULL)
920 fclose(pidfile);
921 free(global.pidfile);
922
923 if (proc == global.nbproc)
924 exit(0); /* parent must leave */
925
926 /* if we're NOT in QUIET mode, we should now close the 3 first FDs to ensure
927 * that we can detach from the TTY. We MUST NOT do it in other cases since
928 * it would have already be done, and 0-2 would have been affected to listening
929 * sockets
930 */
931 if (!(global.mode & MODE_QUIET)) {
932 /* detach from the tty */
933 fclose(stdin); fclose(stdout); fclose(stderr);
934 close(0); close(1); close(2); /* close all fd's */
935 global.mode |= MODE_QUIET; /* ensure that we won't say anything from now */
936 }
937 pid = getpid(); /* update child's pid */
938 setsid();
Willy Tarreau2ff76222007-04-09 19:29:56 +0200939 fork_poller();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200940 }
941
Willy Tarreau4f60f162007-04-08 16:39:58 +0200942 /*
943 * That's it : the central polling loop. Run until we stop.
944 */
945 run_poll_loop();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200946
947 /* Free all Hash Keys and all Hash elements */
948 appsession_cleanup();
949 /* Do some cleanup */
950 deinit();
951
952 exit(0);
953}
954
955
956/*
957 * Local variables:
958 * c-indent-level: 8
959 * c-basic-offset: 8
960 * End:
961 */