blob: 5776d184615b10a03361277709c5d3bc1d00d210 [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>
90#include <proto/stream_sock.h>
91#include <proto/task.h>
92
Willy Tarreau6d1a9882007-01-07 02:03:04 +010093#ifdef CONFIG_HAP_TCPSPLICE
94#include <libtcpsplice.h>
95#endif
96
Willy Tarreaub38651a2007-03-24 17:24:39 +010097#ifdef CONFIG_HAP_CTTPROXY
98#include <proto/cttproxy.h>
99#endif
100
Willy Tarreaubaaee002006-06-26 02:48:02 +0200101/*********************************************************************/
102
103/*********************************************************************/
104
105char *cfg_cfgfile = NULL; /* configuration file */
106char *progname = NULL; /* program name */
107int pid; /* current process id */
108
109/* global options */
110struct global global = {
111 logfac1 : -1,
112 logfac2 : -1,
113 loglev1 : 7, /* max syslog level : debug */
114 loglev2 : 7,
115 /* others NULL OK */
116};
117
118/*********************************************************************/
119
120int stopping; /* non zero means stopping in progress */
121
122/* Here we store informations about the pids of the processes we may pause
123 * or kill. We will send them a signal every 10 ms until we can bind to all
124 * our ports. With 200 retries, that's about 2 seconds.
125 */
126#define MAX_START_RETRIES 200
127static int nb_oldpids = 0;
128static int *oldpids = NULL;
129static int oldpids_sig; /* use USR1 or TERM */
130
131/* this is used to drain data, and as a temporary buffer for sprintf()... */
132char trash[BUFSIZE];
133
134const int zero = 0;
135const int one = 1;
136
137/*
138 * Syslog facilities and levels. Conforming to RFC3164.
139 */
140
141#define MAX_HOSTNAME_LEN 32
142static char hostname[MAX_HOSTNAME_LEN] = "";
143
144
145/*********************************************************************/
146/* general purpose functions ***************************************/
147/*********************************************************************/
148
149void display_version()
150{
151 printf("HA-Proxy version " HAPROXY_VERSION " " HAPROXY_DATE"\n");
Willy Tarreau49e1ee82007-01-22 00:56:46 +0100152 printf("Copyright 2000-2007 Willy Tarreau <w@1wt.eu>\n\n");
Willy Tarreaubaaee002006-06-26 02:48:02 +0200153}
154
155/*
156 * This function prints the command line usage and exits
157 */
158void usage(char *name)
159{
160 display_version();
161 fprintf(stderr,
162 "Usage : %s -f <cfgfile> [ -vdV"
163 "D ] [ -n <maxconn> ] [ -N <maxpconn> ]\n"
164 " [ -p <pidfile> ] [ -m <max megs> ]\n"
165 " -v displays version\n"
166 " -d enters debug mode ; -db only disables background mode.\n"
167 " -V enters verbose mode (disables quiet mode)\n"
168 " -D goes daemon ; implies -q\n"
169 " -q quiet mode : don't display messages\n"
170 " -c check mode : only check config file and exit\n"
171 " -n sets the maximum total # of connections (%d)\n"
172 " -m limits the usable amount of memory (in MB)\n"
173 " -N sets the default, per-proxy maximum # of connections (%d)\n"
174 " -p writes pids of all children to this file\n"
175#if defined(ENABLE_EPOLL)
176 " -de disables epoll() usage even when available\n"
177#endif
Willy Tarreaude99e992007-04-16 00:53:59 +0200178#if defined(ENABLE_SEPOLL)
179 " -ds disables speculative epoll() usage even when available\n"
180#endif
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200181#if defined(ENABLE_KQUEUE)
182 " -dk disables kqueue() usage even when available\n"
183#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200184#if defined(ENABLE_POLL)
185 " -dp disables poll() usage even when available\n"
186#endif
187 " -sf/-st [pid ]* finishes/terminates old pids. Must be last arguments.\n"
188 "\n",
189 name, DEFAULT_MAXCONN, cfg_maxpconn);
190 exit(1);
191}
192
193
194
195/*********************************************************************/
196/* more specific functions ***************************************/
197/*********************************************************************/
198
199/*
200 * upon SIGUSR1, let's have a soft stop.
201 */
202void sig_soft_stop(int sig)
203{
204 soft_stop();
205 signal(sig, SIG_IGN);
206}
207
208/*
209 * upon SIGTTOU, we pause everything
210 */
211void sig_pause(int sig)
212{
213 pause_proxies();
214 signal(sig, sig_pause);
215}
216
217/*
218 * upon SIGTTIN, let's have a soft stop.
219 */
220void sig_listen(int sig)
221{
222 listen_proxies();
223 signal(sig, sig_listen);
224}
225
226/*
227 * this function dumps every server's state when the process receives SIGHUP.
228 */
229void sig_dump_state(int sig)
230{
231 struct proxy *p = proxy;
232
233 Warning("SIGHUP received, dumping servers states.\n");
234 while (p) {
235 struct server *s = p->srv;
236
237 send_log(p, LOG_NOTICE, "SIGHUP received, dumping servers states for proxy %s.\n", p->id);
238 while (s) {
239 snprintf(trash, sizeof(trash),
240 "SIGHUP: Server %s/%s is %s. Conn: %d act, %d pend, %d tot.",
241 p->id, s->id,
242 (s->state & SRV_RUNNING) ? "UP" : "DOWN",
243 s->cur_sess, s->nbpend, s->cum_sess);
244 Warning("%s\n", trash);
245 send_log(p, LOG_NOTICE, "%s\n", trash);
246 s = s->next;
247 }
248
249 if (p->srv_act == 0) {
250 snprintf(trash, sizeof(trash),
Willy Tarreauf1221aa2006-12-17 22:14:12 +0100251 "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 +0200252 p->id,
253 (p->srv_bck) ? "is running on backup servers" : "has no server available",
Willy Tarreauf1221aa2006-12-17 22:14:12 +0100254 p->feconn, p->beconn, p->totpend, p->nbpend, p->cum_feconn, p->cum_beconn);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200255 } else {
256 snprintf(trash, sizeof(trash),
257 "SIGHUP: Proxy %s has %d active servers and %d backup servers available."
Willy Tarreauf1221aa2006-12-17 22:14:12 +0100258 " Conn: act(FE+BE): %d+%d, %d pend (%d unass), tot(FE+BE): %d+%d.",
Willy Tarreaubaaee002006-06-26 02:48:02 +0200259 p->id, p->srv_act, p->srv_bck,
Willy Tarreauf1221aa2006-12-17 22:14:12 +0100260 p->feconn, p->beconn, p->totpend, p->nbpend, p->cum_feconn, p->cum_beconn);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200261 }
262 Warning("%s\n", trash);
263 send_log(p, LOG_NOTICE, "%s\n", trash);
264
265 p = p->next;
266 }
267 signal(sig, sig_dump_state);
268}
269
270void dump(int sig)
271{
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200272#if 0
Willy Tarreau964c9362007-01-07 00:38:00 +0100273 struct task *t;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200274 struct session *s;
Willy Tarreau964c9362007-01-07 00:38:00 +0100275 struct rb_node *node;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200276
Willy Tarreau964c9362007-01-07 00:38:00 +0100277 for(node = rb_first(&wait_queue[0]);
278 node != NULL; node = rb_next(node)) {
279 t = rb_entry(node, struct task, rb_node);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200280 s = t->context;
281 qfprintf(stderr,"[dump] wq: task %p, still %ld ms, "
282 "cli=%d, srv=%d, cr=%d, cw=%d, sr=%d, sw=%d, "
283 "req=%d, rep=%d, clifd=%d\n",
284 s, tv_remain(&now, &t->expire),
285 s->cli_state,
286 s->srv_state,
Willy Tarreauf161a342007-04-08 16:59:42 +0200287 EV_FD_ISSET(s->cli_fd, DIR_RD),
288 EV_FD_ISSET(s->cli_fd, DIR_WR),
289 EV_FD_ISSET(s->srv_fd, DIR_RD),
290 EV_FD_ISSET(s->srv_fd, DIR_WR),
Willy Tarreaubaaee002006-06-26 02:48:02 +0200291 s->req->l, s->rep?s->rep->l:0, s->cli_fd
292 );
293 }
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200294#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200295}
296
297#ifdef DEBUG_MEMORY
298static void fast_stop(void)
299{
300 struct proxy *p;
301 p = proxy;
302 while (p) {
303 p->grace = 0;
304 p = p->next;
305 }
306 soft_stop();
307}
308
309void sig_int(int sig)
310{
311 /* This would normally be a hard stop,
312 but we want to be sure about deallocation,
313 and so on, so we do a soft stop with
314 0 GRACE time
315 */
316 fast_stop();
317 /* If we are killed twice, we decide to die*/
318 signal(sig, SIG_DFL);
319}
320
321void sig_term(int sig)
322{
323 /* This would normally be a hard stop,
324 but we want to be sure about deallocation,
325 and so on, so we do a soft stop with
326 0 GRACE time
327 */
328 fast_stop();
329 /* If we are killed twice, we decide to die*/
330 signal(sig, SIG_DFL);
331}
332#endif
333
334
335/*
336 * This function initializes all the necessary variables. It only returns
337 * if everything is OK. If something fails, it exits.
338 */
339void init(int argc, char **argv)
340{
341 int i;
342 int arg_mode = 0; /* MODE_DEBUG, ... */
343 char *old_argv = *argv;
344 char *tmp;
345 char *cfg_pidfile = NULL;
346
347 if (1<<INTBITS != sizeof(int)*8) {
348 fprintf(stderr,
349 "Error: wrong architecture. Recompile so that sizeof(int)=%d\n",
350 (int)(sizeof(int)*8));
351 exit(1);
352 }
353
354 /*
355 * Initialize the previously static variables.
356 */
357
358 totalconn = actconn = maxfd = listeners = stopping = 0;
359
360
361#ifdef HAPROXY_MEMMAX
362 global.rlimit_memmax = HAPROXY_MEMMAX;
363#endif
364
365 /* initialize the libc's localtime structures once for all so that we
366 * won't be missing memory if we want to send alerts under OOM conditions.
Willy Tarreau2b35c952006-10-15 15:25:48 +0200367 * Also, the Alert() and Warning() functions need <now> to be initialized.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200368 */
369 tv_now(&now);
Willy Tarreaubf736132006-10-15 22:54:47 +0200370 localtime((time_t *)&now.tv_sec);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200371 start_date = now;
372
Willy Tarreau80587432006-12-24 17:47:20 +0100373 init_proto_http();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200374
375 cfg_polling_mechanism = POLL_USE_SELECT; /* select() is always available */
376#if defined(ENABLE_POLL)
377 cfg_polling_mechanism |= POLL_USE_POLL;
378#endif
379#if defined(ENABLE_EPOLL)
380 cfg_polling_mechanism |= POLL_USE_EPOLL;
381#endif
Willy Tarreaude99e992007-04-16 00:53:59 +0200382#if defined(ENABLE_SEPOLL)
383 cfg_polling_mechanism |= POLL_USE_SEPOLL;
384#endif
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200385#if defined(ENABLE_KQUEUE)
386 cfg_polling_mechanism |= POLL_USE_KQUEUE;
387#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200388
389 pid = getpid();
390 progname = *argv;
391 while ((tmp = strchr(progname, '/')) != NULL)
392 progname = tmp + 1;
393
394 argc--; argv++;
395 while (argc > 0) {
396 char *flag;
397
398 if (**argv == '-') {
399 flag = *argv+1;
400
401 /* 1 arg */
402 if (*flag == 'v') {
403 display_version();
404 exit(0);
405 }
406#if defined(ENABLE_EPOLL)
407 else if (*flag == 'd' && flag[1] == 'e')
408 cfg_polling_mechanism &= ~POLL_USE_EPOLL;
409#endif
Willy Tarreaude99e992007-04-16 00:53:59 +0200410#if defined(ENABLE_SEPOLL)
411 else if (*flag == 'd' && flag[1] == 's')
412 cfg_polling_mechanism &= ~POLL_USE_SEPOLL;
413#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200414#if defined(ENABLE_POLL)
415 else if (*flag == 'd' && flag[1] == 'p')
416 cfg_polling_mechanism &= ~POLL_USE_POLL;
417#endif
Willy Tarreau69cad1a2007-04-10 22:45:11 +0200418#if defined(ENABLE_KQUEUE)
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200419 else if (*flag == 'd' && flag[1] == 'k')
420 cfg_polling_mechanism &= ~POLL_USE_KQUEUE;
421#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200422 else if (*flag == 'V')
423 arg_mode |= MODE_VERBOSE;
424 else if (*flag == 'd' && flag[1] == 'b')
425 arg_mode |= MODE_FOREGROUND;
426 else if (*flag == 'd')
427 arg_mode |= MODE_DEBUG;
428 else if (*flag == 'c')
429 arg_mode |= MODE_CHECK;
430 else if (*flag == 'D')
431 arg_mode |= MODE_DAEMON | MODE_QUIET;
432 else if (*flag == 'q')
433 arg_mode |= MODE_QUIET;
434 else if (*flag == 's' && (flag[1] == 'f' || flag[1] == 't')) {
435 /* list of pids to finish ('f') or terminate ('t') */
436
437 if (flag[1] == 'f')
438 oldpids_sig = SIGUSR1; /* finish then exit */
439 else
440 oldpids_sig = SIGTERM; /* terminate immediately */
441 argv++; argc--;
442
443 if (argc > 0) {
444 oldpids = calloc(argc, sizeof(int));
445 while (argc > 0) {
446 oldpids[nb_oldpids] = atol(*argv);
447 if (oldpids[nb_oldpids] <= 0)
448 usage(old_argv);
449 argc--; argv++;
450 nb_oldpids++;
451 }
452 }
453 }
454 else { /* >=2 args */
455 argv++; argc--;
456 if (argc == 0)
457 usage(old_argv);
458
459 switch (*flag) {
460 case 'n' : cfg_maxconn = atol(*argv); break;
461 case 'm' : global.rlimit_memmax = atol(*argv); break;
462 case 'N' : cfg_maxpconn = atol(*argv); break;
463 case 'f' : cfg_cfgfile = *argv; break;
464 case 'p' : cfg_pidfile = *argv; break;
465 default: usage(old_argv);
466 }
467 }
468 }
469 else
470 usage(old_argv);
471 argv++; argc--;
472 }
473
474 global.mode = MODE_STARTING | /* during startup, we want most of the alerts */
475 (arg_mode & (MODE_DAEMON | MODE_FOREGROUND | MODE_VERBOSE
476 | MODE_QUIET | MODE_CHECK | MODE_DEBUG));
477
478 if (!cfg_cfgfile)
479 usage(old_argv);
480
481 gethostname(hostname, MAX_HOSTNAME_LEN);
482
483 have_appsession = 0;
484 global.maxsock = 10; /* reserve 10 fds ; will be incremented by socket eaters */
485 if (readcfgfile(cfg_cfgfile) < 0) {
486 Alert("Error reading configuration file : %s\n", cfg_cfgfile);
487 exit(1);
488 }
489 if (have_appsession)
490 appsession_init();
491
492 if (global.mode & MODE_CHECK) {
493 qfprintf(stdout, "Configuration file is valid : %s\n", cfg_cfgfile);
494 exit(0);
495 }
496
497 if (cfg_maxconn > 0)
498 global.maxconn = cfg_maxconn;
499
500 if (cfg_pidfile) {
501 if (global.pidfile)
502 free(global.pidfile);
503 global.pidfile = strdup(cfg_pidfile);
504 }
505
506 if (global.maxconn == 0)
507 global.maxconn = DEFAULT_MAXCONN;
508
509 global.maxsock += global.maxconn * 2; /* each connection needs two sockets */
510
511 if (arg_mode & (MODE_DEBUG | MODE_FOREGROUND)) {
512 /* command line debug mode inhibits configuration mode */
513 global.mode &= ~(MODE_DAEMON | MODE_QUIET);
514 }
515 global.mode |= (arg_mode & (MODE_DAEMON | MODE_FOREGROUND | MODE_QUIET |
516 MODE_VERBOSE | MODE_DEBUG | MODE_STATS | MODE_LOG));
517
518 if ((global.mode & MODE_DEBUG) && (global.mode & (MODE_DAEMON | MODE_QUIET))) {
519 Warning("<debug> mode incompatible with <quiet> and <daemon>. Keeping <debug> only.\n");
520 global.mode &= ~(MODE_DAEMON | MODE_QUIET);
521 }
522
523 if ((global.nbproc > 1) && !(global.mode & MODE_DAEMON)) {
524 if (!(global.mode & (MODE_FOREGROUND | MODE_DEBUG)))
525 Warning("<nbproc> is only meaningful in daemon mode. Setting limit to 1 process.\n");
526 global.nbproc = 1;
527 }
528
529 if (global.nbproc < 1)
530 global.nbproc = 1;
531
Willy Tarreaubaaee002006-06-26 02:48:02 +0200532 fdtab = (struct fdtab *)calloc(1,
533 sizeof(struct fdtab) * (global.maxsock));
534 for (i = 0; i < global.maxsock; i++) {
535 fdtab[i].state = FD_STCLOSE;
536 }
Willy Tarreau4f60f162007-04-08 16:39:58 +0200537
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200538 /*
539 * Note: we could register external pollers here.
540 * Built-in pollers have been registered before main().
541 */
Willy Tarreau4f60f162007-04-08 16:39:58 +0200542
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200543 if (!(cfg_polling_mechanism & POLL_USE_KQUEUE))
544 disable_poller("kqueue");
545
Willy Tarreau4f60f162007-04-08 16:39:58 +0200546 if (!(cfg_polling_mechanism & POLL_USE_EPOLL))
547 disable_poller("epoll");
548
Willy Tarreaude99e992007-04-16 00:53:59 +0200549 if (!(cfg_polling_mechanism & POLL_USE_SEPOLL))
550 disable_poller("sepoll");
551
Willy Tarreau4f60f162007-04-08 16:39:58 +0200552 if (!(cfg_polling_mechanism & POLL_USE_POLL))
553 disable_poller("poll");
554
555 if (!(cfg_polling_mechanism & POLL_USE_SELECT))
556 disable_poller("select");
557
558 /* Note: we could disable any poller by name here */
559
Willy Tarreau2ff76222007-04-09 19:29:56 +0200560 if (global.mode & (MODE_VERBOSE|MODE_DEBUG))
561 list_pollers(stderr);
562
Willy Tarreau4f60f162007-04-08 16:39:58 +0200563 if (!init_pollers()) {
Willy Tarreau2ff76222007-04-09 19:29:56 +0200564 Alert("No polling mechanism available.\n");
Willy Tarreau4f60f162007-04-08 16:39:58 +0200565 exit(1);
566 }
Willy Tarreau2ff76222007-04-09 19:29:56 +0200567 if (global.mode & (MODE_VERBOSE|MODE_DEBUG)) {
568 printf("Using %s() as the polling mechanism.\n", cur_poller.name);
Willy Tarreau4f60f162007-04-08 16:39:58 +0200569 }
570
Willy Tarreaubaaee002006-06-26 02:48:02 +0200571}
572
573void deinit(void)
574{
575 struct proxy *p = proxy;
576 struct cap_hdr *h,*h_next;
577 struct server *s,*s_next;
578 struct listener *l,*l_next;
579
580 while (p) {
581 if (p->id)
582 free(p->id);
583
584 if (p->check_req)
585 free(p->check_req);
586
587 if (p->cookie_name)
588 free(p->cookie_name);
589
590 if (p->capture_name)
591 free(p->capture_name);
592
593 /* only strup if the user have set in config.
594 When should we free it?!
595 if (p->errmsg.msg400) free(p->errmsg.msg400);
596 if (p->errmsg.msg403) free(p->errmsg.msg403);
597 if (p->errmsg.msg408) free(p->errmsg.msg408);
598 if (p->errmsg.msg500) free(p->errmsg.msg500);
599 if (p->errmsg.msg502) free(p->errmsg.msg502);
600 if (p->errmsg.msg503) free(p->errmsg.msg503);
601 if (p->errmsg.msg504) free(p->errmsg.msg504);
602 */
603 if (p->appsession_name)
604 free(p->appsession_name);
605
606 h = p->req_cap;
607 while (h) {
608 h_next = h->next;
609 if (h->name)
610 free(h->name);
611 pool_destroy(h->pool);
612 free(h);
613 h = h_next;
614 }/* end while(h) */
615
616 h = p->rsp_cap;
617 while (h) {
618 h_next = h->next;
619 if (h->name)
620 free(h->name);
621
622 pool_destroy(h->pool);
623 free(h);
624 h = h_next;
625 }/* end while(h) */
626
627 s = p->srv;
628 while (s) {
629 s_next = s->next;
630 if (s->id)
631 free(s->id);
632
633 if (s->cookie)
634 free(s->cookie);
635
636 free(s);
637 s = s_next;
638 }/* end while(s) */
639
640 l = p->listen;
641 while (l) {
642 l_next = l->next;
643 free(l);
644 l = l_next;
645 }/* end while(l) */
646
647 pool_destroy((void **) p->req_cap_pool);
648 pool_destroy((void **) p->rsp_cap_pool);
649 p = p->next;
650 }/* end while(p) */
651
652 if (global.chroot) free(global.chroot);
653 if (global.pidfile) free(global.pidfile);
654
Willy Tarreaubaaee002006-06-26 02:48:02 +0200655 if (fdtab) free(fdtab);
656
657 pool_destroy(pool_session);
658 pool_destroy(pool_buffer);
659 pool_destroy(pool_requri);
660 pool_destroy(pool_task);
661 pool_destroy(pool_capture);
662 pool_destroy(pool_appsess);
663
664 if (have_appsession) {
665 pool_destroy(apools.serverid);
666 pool_destroy(apools.sessid);
667 }
668} /* end deinit() */
669
670/* sends the signal <sig> to all pids found in <oldpids> */
671static void tell_old_pids(int sig)
672{
673 int p;
674 for (p = 0; p < nb_oldpids; p++)
675 kill(oldpids[p], sig);
676}
677
Willy Tarreau4f60f162007-04-08 16:39:58 +0200678/*
679 * Runs the polling loop
680 *
681 * FIXME:
682 * - we still use 'listeners' to check whether we want to stop or not.
683 *
684 */
685void run_poll_loop()
686{
687 int next_time;
688 tv_now(&now);
689
690 while (1) {
691 next_time = process_runnable_tasks();
692
693 /* stop when there's no connection left and we don't allow them anymore */
694 if (!actconn && listeners == 0)
695 break;
696
697 cur_poller.poll(&cur_poller, next_time);
698 }
699}
700
701
Willy Tarreaubaaee002006-06-26 02:48:02 +0200702int main(int argc, char **argv)
703{
704 int err, retry;
705 struct rlimit limit;
706 FILE *pidfile = NULL;
707 init(argc, argv);
708
709 signal(SIGQUIT, dump);
710 signal(SIGUSR1, sig_soft_stop);
711 signal(SIGHUP, sig_dump_state);
712#ifdef DEBUG_MEMORY
713 signal(SIGINT, sig_int);
714 signal(SIGTERM, sig_term);
715#endif
716
717 /* on very high loads, a sigpipe sometimes happen just between the
718 * getsockopt() which tells "it's OK to write", and the following write :-(
719 */
720#ifndef MSG_NOSIGNAL
721 signal(SIGPIPE, SIG_IGN);
722#endif
723
724 /* We will loop at most 100 times with 10 ms delay each time.
725 * That's at most 1 second. We only send a signal to old pids
726 * if we cannot grab at least one port.
727 */
728 retry = MAX_START_RETRIES;
729 err = ERR_NONE;
730 while (retry >= 0) {
731 struct timeval w;
732 err = start_proxies(retry == 0 || nb_oldpids == 0);
733 if (err != ERR_RETRYABLE)
734 break;
735 if (nb_oldpids == 0)
736 break;
737
738 /* FIXME-20060514: Solaris and OpenBSD do not support shutdown() on
739 * listening sockets. So on those platforms, it would be wiser to
740 * simply send SIGUSR1, which will not be undoable.
741 */
742 tell_old_pids(SIGTTOU);
743 /* give some time to old processes to stop listening */
744 w.tv_sec = 0;
745 w.tv_usec = 10*1000;
746 select(0, NULL, NULL, NULL, &w);
747 retry--;
748 }
749
750 /* Note: start_proxies() sends an alert when it fails. */
751 if (err != ERR_NONE) {
752 if (retry != MAX_START_RETRIES && nb_oldpids)
753 tell_old_pids(SIGTTIN);
754 exit(1);
755 }
756
757 if (listeners == 0) {
758 Alert("[%s.main()] No enabled listener found (check the <listen> keywords) ! Exiting.\n", argv[0]);
759 /* Note: we don't have to send anything to the old pids because we
760 * never stopped them. */
761 exit(1);
762 }
763
764 /* prepare pause/play signals */
765 signal(SIGTTOU, sig_pause);
766 signal(SIGTTIN, sig_listen);
767
768 if (global.mode & MODE_DAEMON) {
769 global.mode &= ~MODE_VERBOSE;
770 global.mode |= MODE_QUIET;
771 }
772
773 /* MODE_QUIET can inhibit alerts and warnings below this line */
774
775 global.mode &= ~MODE_STARTING;
776 if ((global.mode & MODE_QUIET) && !(global.mode & MODE_VERBOSE)) {
777 /* detach from the tty */
778 fclose(stdin); fclose(stdout); fclose(stderr);
779 close(0); close(1); close(2);
780 }
781
782 /* open log & pid files before the chroot */
783 if (global.mode & MODE_DAEMON && global.pidfile != NULL) {
784 int pidfd;
785 unlink(global.pidfile);
786 pidfd = open(global.pidfile, O_CREAT | O_WRONLY | O_TRUNC, 0644);
787 if (pidfd < 0) {
788 Alert("[%s.main()] Cannot create pidfile %s\n", argv[0], global.pidfile);
789 if (nb_oldpids)
790 tell_old_pids(SIGTTIN);
791 exit(1);
792 }
793 pidfile = fdopen(pidfd, "w");
794 }
795
796 /* chroot if needed */
797 if (global.chroot != NULL) {
798 if (chroot(global.chroot) == -1) {
799 Alert("[%s.main()] Cannot chroot(%s).\n", argv[0], global.chroot);
800 if (nb_oldpids)
801 tell_old_pids(SIGTTIN);
802 }
803 chdir("/");
804 }
805
806 /* ulimits */
807 if (!global.rlimit_nofile)
808 global.rlimit_nofile = global.maxsock;
809
810 if (global.rlimit_nofile) {
811 limit.rlim_cur = limit.rlim_max = global.rlimit_nofile;
812 if (setrlimit(RLIMIT_NOFILE, &limit) == -1) {
813 Warning("[%s.main()] Cannot raise FD limit to %d.\n", argv[0], global.rlimit_nofile);
814 }
815 }
816
817 if (global.rlimit_memmax) {
818 limit.rlim_cur = limit.rlim_max =
819 global.rlimit_memmax * 1048576 / global.nbproc;
820#ifdef RLIMIT_AS
821 if (setrlimit(RLIMIT_AS, &limit) == -1) {
822 Warning("[%s.main()] Cannot fix MEM limit to %d megs.\n",
823 argv[0], global.rlimit_memmax);
824 }
825#else
826 if (setrlimit(RLIMIT_DATA, &limit) == -1) {
827 Warning("[%s.main()] Cannot fix MEM limit to %d megs.\n",
828 argv[0], global.rlimit_memmax);
829 }
830#endif
831 }
832
Willy Tarreau6d1a9882007-01-07 02:03:04 +0100833#ifdef CONFIG_HAP_TCPSPLICE
834 if (global.last_checks & LSTCHK_TCPSPLICE) {
835 if (tcp_splice_start() < 0) {
836 Alert("[%s.main()] Cannot enable tcp_splice.\n"
837 " Make sure you have enough permissions and that the module is loadable.\n"
838 " Alternatively, you may disable the 'tcpsplice' options in the configuration.\n"
839 "", argv[0], global.gid);
840 exit(1);
841 }
842 }
843#endif
844
Willy Tarreaub38651a2007-03-24 17:24:39 +0100845#ifdef CONFIG_HAP_CTTPROXY
846 if (global.last_checks & LSTCHK_CTTPROXY) {
847 int ret;
848
849 ret = check_cttproxy_version();
850 if (ret < 0) {
851 Alert("[%s.main()] Cannot enable cttproxy.\n%s",
852 argv[0],
853 (ret == -1) ? " Incorrect module version.\n"
854 : " Make sure you have enough permissions and that the module is loaded.\n");
855 exit(1);
856 }
857 }
858#endif
859
860 if ((global.last_checks & LSTCHK_NETADM) && global.uid) {
861 Alert("[%s.main()] Some configuration options require full privileges, so global.uid cannot be changed.\n"
862 "", argv[0], global.gid);
863 exit(1);
864 }
865
Willy Tarreaubaaee002006-06-26 02:48:02 +0200866 if (nb_oldpids)
867 tell_old_pids(oldpids_sig);
868
869 /* Note that any error at this stage will be fatal because we will not
870 * be able to restart the old pids.
871 */
872
873 /* setgid / setuid */
874 if (global.gid && setgid(global.gid) == -1) {
875 Alert("[%s.main()] Cannot set gid %d.\n", argv[0], global.gid);
876 exit(1);
877 }
878
879 if (global.uid && setuid(global.uid) == -1) {
880 Alert("[%s.main()] Cannot set uid %d.\n", argv[0], global.uid);
881 exit(1);
882 }
883
884 /* check ulimits */
885 limit.rlim_cur = limit.rlim_max = 0;
886 getrlimit(RLIMIT_NOFILE, &limit);
887 if (limit.rlim_cur < global.maxsock) {
888 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",
889 argv[0], limit.rlim_cur, global.maxconn, global.maxsock, global.maxsock);
890 }
891
892 if (global.mode & MODE_DAEMON) {
893 int ret = 0;
894 int proc;
895
896 /* the father launches the required number of processes */
897 for (proc = 0; proc < global.nbproc; proc++) {
898 ret = fork();
899 if (ret < 0) {
900 Alert("[%s.main()] Cannot fork.\n", argv[0]);
901 if (nb_oldpids)
902 exit(1); /* there has been an error */
903 }
904 else if (ret == 0) /* child breaks here */
905 break;
906 if (pidfile != NULL) {
907 fprintf(pidfile, "%d\n", ret);
908 fflush(pidfile);
909 }
910 }
911 /* close the pidfile both in children and father */
912 if (pidfile != NULL)
913 fclose(pidfile);
914 free(global.pidfile);
915
916 if (proc == global.nbproc)
917 exit(0); /* parent must leave */
918
919 /* if we're NOT in QUIET mode, we should now close the 3 first FDs to ensure
920 * that we can detach from the TTY. We MUST NOT do it in other cases since
921 * it would have already be done, and 0-2 would have been affected to listening
922 * sockets
923 */
924 if (!(global.mode & MODE_QUIET)) {
925 /* detach from the tty */
926 fclose(stdin); fclose(stdout); fclose(stderr);
927 close(0); close(1); close(2); /* close all fd's */
928 global.mode |= MODE_QUIET; /* ensure that we won't say anything from now */
929 }
930 pid = getpid(); /* update child's pid */
931 setsid();
Willy Tarreau2ff76222007-04-09 19:29:56 +0200932 fork_poller();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200933 }
934
Willy Tarreau4f60f162007-04-08 16:39:58 +0200935 /*
936 * That's it : the central polling loop. Run until we stop.
937 */
938 run_poll_loop();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200939
940 /* Free all Hash Keys and all Hash elements */
941 appsession_cleanup();
942 /* Do some cleanup */
943 deinit();
944
945 exit(0);
946}
947
948
949/*
950 * Local variables:
951 * c-indent-level: 8
952 * c-basic-offset: 8
953 * End:
954 */