blob: f8946f93680a181246914a0e8492af7df35b7810 [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 Tarreau964c9362007-01-07 00:38:00 +0100272 struct task *t;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200273 struct session *s;
Willy Tarreau964c9362007-01-07 00:38:00 +0100274 struct rb_node *node;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200275
Willy Tarreau964c9362007-01-07 00:38:00 +0100276 for(node = rb_first(&wait_queue[0]);
277 node != NULL; node = rb_next(node)) {
278 t = rb_entry(node, struct task, rb_node);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200279 s = t->context;
280 qfprintf(stderr,"[dump] wq: task %p, still %ld ms, "
281 "cli=%d, srv=%d, cr=%d, cw=%d, sr=%d, sw=%d, "
282 "req=%d, rep=%d, clifd=%d\n",
283 s, tv_remain(&now, &t->expire),
284 s->cli_state,
285 s->srv_state,
Willy Tarreauf161a342007-04-08 16:59:42 +0200286 EV_FD_ISSET(s->cli_fd, DIR_RD),
287 EV_FD_ISSET(s->cli_fd, DIR_WR),
288 EV_FD_ISSET(s->srv_fd, DIR_RD),
289 EV_FD_ISSET(s->srv_fd, DIR_WR),
Willy Tarreaubaaee002006-06-26 02:48:02 +0200290 s->req->l, s->rep?s->rep->l:0, s->cli_fd
291 );
292 }
293}
294
295#ifdef DEBUG_MEMORY
296static void fast_stop(void)
297{
298 struct proxy *p;
299 p = proxy;
300 while (p) {
301 p->grace = 0;
302 p = p->next;
303 }
304 soft_stop();
305}
306
307void sig_int(int sig)
308{
309 /* This would normally be a hard stop,
310 but we want to be sure about deallocation,
311 and so on, so we do a soft stop with
312 0 GRACE time
313 */
314 fast_stop();
315 /* If we are killed twice, we decide to die*/
316 signal(sig, SIG_DFL);
317}
318
319void sig_term(int sig)
320{
321 /* This would normally be a hard stop,
322 but we want to be sure about deallocation,
323 and so on, so we do a soft stop with
324 0 GRACE time
325 */
326 fast_stop();
327 /* If we are killed twice, we decide to die*/
328 signal(sig, SIG_DFL);
329}
330#endif
331
332
333/*
334 * This function initializes all the necessary variables. It only returns
335 * if everything is OK. If something fails, it exits.
336 */
337void init(int argc, char **argv)
338{
339 int i;
340 int arg_mode = 0; /* MODE_DEBUG, ... */
341 char *old_argv = *argv;
342 char *tmp;
343 char *cfg_pidfile = NULL;
344
345 if (1<<INTBITS != sizeof(int)*8) {
346 fprintf(stderr,
347 "Error: wrong architecture. Recompile so that sizeof(int)=%d\n",
348 (int)(sizeof(int)*8));
349 exit(1);
350 }
351
352 /*
353 * Initialize the previously static variables.
354 */
355
356 totalconn = actconn = maxfd = listeners = stopping = 0;
357
358
359#ifdef HAPROXY_MEMMAX
360 global.rlimit_memmax = HAPROXY_MEMMAX;
361#endif
362
363 /* initialize the libc's localtime structures once for all so that we
364 * won't be missing memory if we want to send alerts under OOM conditions.
Willy Tarreau2b35c952006-10-15 15:25:48 +0200365 * Also, the Alert() and Warning() functions need <now> to be initialized.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200366 */
367 tv_now(&now);
Willy Tarreaubf736132006-10-15 22:54:47 +0200368 localtime((time_t *)&now.tv_sec);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200369 start_date = now;
370
Willy Tarreau80587432006-12-24 17:47:20 +0100371 init_proto_http();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200372
373 cfg_polling_mechanism = POLL_USE_SELECT; /* select() is always available */
374#if defined(ENABLE_POLL)
375 cfg_polling_mechanism |= POLL_USE_POLL;
376#endif
377#if defined(ENABLE_EPOLL)
378 cfg_polling_mechanism |= POLL_USE_EPOLL;
379#endif
Willy Tarreaude99e992007-04-16 00:53:59 +0200380#if defined(ENABLE_SEPOLL)
381 cfg_polling_mechanism |= POLL_USE_SEPOLL;
382#endif
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200383#if defined(ENABLE_KQUEUE)
384 cfg_polling_mechanism |= POLL_USE_KQUEUE;
385#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200386
387 pid = getpid();
388 progname = *argv;
389 while ((tmp = strchr(progname, '/')) != NULL)
390 progname = tmp + 1;
391
392 argc--; argv++;
393 while (argc > 0) {
394 char *flag;
395
396 if (**argv == '-') {
397 flag = *argv+1;
398
399 /* 1 arg */
400 if (*flag == 'v') {
401 display_version();
402 exit(0);
403 }
404#if defined(ENABLE_EPOLL)
405 else if (*flag == 'd' && flag[1] == 'e')
406 cfg_polling_mechanism &= ~POLL_USE_EPOLL;
407#endif
Willy Tarreaude99e992007-04-16 00:53:59 +0200408#if defined(ENABLE_SEPOLL)
409 else if (*flag == 'd' && flag[1] == 's')
410 cfg_polling_mechanism &= ~POLL_USE_SEPOLL;
411#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200412#if defined(ENABLE_POLL)
413 else if (*flag == 'd' && flag[1] == 'p')
414 cfg_polling_mechanism &= ~POLL_USE_POLL;
415#endif
Willy Tarreau69cad1a2007-04-10 22:45:11 +0200416#if defined(ENABLE_KQUEUE)
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200417 else if (*flag == 'd' && flag[1] == 'k')
418 cfg_polling_mechanism &= ~POLL_USE_KQUEUE;
419#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200420 else if (*flag == 'V')
421 arg_mode |= MODE_VERBOSE;
422 else if (*flag == 'd' && flag[1] == 'b')
423 arg_mode |= MODE_FOREGROUND;
424 else if (*flag == 'd')
425 arg_mode |= MODE_DEBUG;
426 else if (*flag == 'c')
427 arg_mode |= MODE_CHECK;
428 else if (*flag == 'D')
429 arg_mode |= MODE_DAEMON | MODE_QUIET;
430 else if (*flag == 'q')
431 arg_mode |= MODE_QUIET;
432 else if (*flag == 's' && (flag[1] == 'f' || flag[1] == 't')) {
433 /* list of pids to finish ('f') or terminate ('t') */
434
435 if (flag[1] == 'f')
436 oldpids_sig = SIGUSR1; /* finish then exit */
437 else
438 oldpids_sig = SIGTERM; /* terminate immediately */
439 argv++; argc--;
440
441 if (argc > 0) {
442 oldpids = calloc(argc, sizeof(int));
443 while (argc > 0) {
444 oldpids[nb_oldpids] = atol(*argv);
445 if (oldpids[nb_oldpids] <= 0)
446 usage(old_argv);
447 argc--; argv++;
448 nb_oldpids++;
449 }
450 }
451 }
452 else { /* >=2 args */
453 argv++; argc--;
454 if (argc == 0)
455 usage(old_argv);
456
457 switch (*flag) {
458 case 'n' : cfg_maxconn = atol(*argv); break;
459 case 'm' : global.rlimit_memmax = atol(*argv); break;
460 case 'N' : cfg_maxpconn = atol(*argv); break;
461 case 'f' : cfg_cfgfile = *argv; break;
462 case 'p' : cfg_pidfile = *argv; break;
463 default: usage(old_argv);
464 }
465 }
466 }
467 else
468 usage(old_argv);
469 argv++; argc--;
470 }
471
472 global.mode = MODE_STARTING | /* during startup, we want most of the alerts */
473 (arg_mode & (MODE_DAEMON | MODE_FOREGROUND | MODE_VERBOSE
474 | MODE_QUIET | MODE_CHECK | MODE_DEBUG));
475
476 if (!cfg_cfgfile)
477 usage(old_argv);
478
479 gethostname(hostname, MAX_HOSTNAME_LEN);
480
481 have_appsession = 0;
482 global.maxsock = 10; /* reserve 10 fds ; will be incremented by socket eaters */
483 if (readcfgfile(cfg_cfgfile) < 0) {
484 Alert("Error reading configuration file : %s\n", cfg_cfgfile);
485 exit(1);
486 }
487 if (have_appsession)
488 appsession_init();
489
490 if (global.mode & MODE_CHECK) {
491 qfprintf(stdout, "Configuration file is valid : %s\n", cfg_cfgfile);
492 exit(0);
493 }
494
495 if (cfg_maxconn > 0)
496 global.maxconn = cfg_maxconn;
497
498 if (cfg_pidfile) {
499 if (global.pidfile)
500 free(global.pidfile);
501 global.pidfile = strdup(cfg_pidfile);
502 }
503
504 if (global.maxconn == 0)
505 global.maxconn = DEFAULT_MAXCONN;
506
507 global.maxsock += global.maxconn * 2; /* each connection needs two sockets */
508
509 if (arg_mode & (MODE_DEBUG | MODE_FOREGROUND)) {
510 /* command line debug mode inhibits configuration mode */
511 global.mode &= ~(MODE_DAEMON | MODE_QUIET);
512 }
513 global.mode |= (arg_mode & (MODE_DAEMON | MODE_FOREGROUND | MODE_QUIET |
514 MODE_VERBOSE | MODE_DEBUG | MODE_STATS | MODE_LOG));
515
516 if ((global.mode & MODE_DEBUG) && (global.mode & (MODE_DAEMON | MODE_QUIET))) {
517 Warning("<debug> mode incompatible with <quiet> and <daemon>. Keeping <debug> only.\n");
518 global.mode &= ~(MODE_DAEMON | MODE_QUIET);
519 }
520
521 if ((global.nbproc > 1) && !(global.mode & MODE_DAEMON)) {
522 if (!(global.mode & (MODE_FOREGROUND | MODE_DEBUG)))
523 Warning("<nbproc> is only meaningful in daemon mode. Setting limit to 1 process.\n");
524 global.nbproc = 1;
525 }
526
527 if (global.nbproc < 1)
528 global.nbproc = 1;
529
Willy Tarreaubaaee002006-06-26 02:48:02 +0200530 fdtab = (struct fdtab *)calloc(1,
531 sizeof(struct fdtab) * (global.maxsock));
532 for (i = 0; i < global.maxsock; i++) {
533 fdtab[i].state = FD_STCLOSE;
534 }
Willy Tarreau4f60f162007-04-08 16:39:58 +0200535
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200536 /*
537 * Note: we could register external pollers here.
538 * Built-in pollers have been registered before main().
539 */
Willy Tarreau4f60f162007-04-08 16:39:58 +0200540
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200541 if (!(cfg_polling_mechanism & POLL_USE_KQUEUE))
542 disable_poller("kqueue");
543
Willy Tarreau4f60f162007-04-08 16:39:58 +0200544 if (!(cfg_polling_mechanism & POLL_USE_EPOLL))
545 disable_poller("epoll");
546
Willy Tarreaude99e992007-04-16 00:53:59 +0200547 if (!(cfg_polling_mechanism & POLL_USE_SEPOLL))
548 disable_poller("sepoll");
549
Willy Tarreau4f60f162007-04-08 16:39:58 +0200550 if (!(cfg_polling_mechanism & POLL_USE_POLL))
551 disable_poller("poll");
552
553 if (!(cfg_polling_mechanism & POLL_USE_SELECT))
554 disable_poller("select");
555
556 /* Note: we could disable any poller by name here */
557
Willy Tarreau2ff76222007-04-09 19:29:56 +0200558 if (global.mode & (MODE_VERBOSE|MODE_DEBUG))
559 list_pollers(stderr);
560
Willy Tarreau4f60f162007-04-08 16:39:58 +0200561 if (!init_pollers()) {
Willy Tarreau2ff76222007-04-09 19:29:56 +0200562 Alert("No polling mechanism available.\n");
Willy Tarreau4f60f162007-04-08 16:39:58 +0200563 exit(1);
564 }
Willy Tarreau2ff76222007-04-09 19:29:56 +0200565 if (global.mode & (MODE_VERBOSE|MODE_DEBUG)) {
566 printf("Using %s() as the polling mechanism.\n", cur_poller.name);
Willy Tarreau4f60f162007-04-08 16:39:58 +0200567 }
568
Willy Tarreaubaaee002006-06-26 02:48:02 +0200569}
570
571void deinit(void)
572{
573 struct proxy *p = proxy;
574 struct cap_hdr *h,*h_next;
575 struct server *s,*s_next;
576 struct listener *l,*l_next;
577
578 while (p) {
579 if (p->id)
580 free(p->id);
581
582 if (p->check_req)
583 free(p->check_req);
584
585 if (p->cookie_name)
586 free(p->cookie_name);
587
588 if (p->capture_name)
589 free(p->capture_name);
590
591 /* only strup if the user have set in config.
592 When should we free it?!
593 if (p->errmsg.msg400) free(p->errmsg.msg400);
594 if (p->errmsg.msg403) free(p->errmsg.msg403);
595 if (p->errmsg.msg408) free(p->errmsg.msg408);
596 if (p->errmsg.msg500) free(p->errmsg.msg500);
597 if (p->errmsg.msg502) free(p->errmsg.msg502);
598 if (p->errmsg.msg503) free(p->errmsg.msg503);
599 if (p->errmsg.msg504) free(p->errmsg.msg504);
600 */
601 if (p->appsession_name)
602 free(p->appsession_name);
603
604 h = p->req_cap;
605 while (h) {
606 h_next = h->next;
607 if (h->name)
608 free(h->name);
609 pool_destroy(h->pool);
610 free(h);
611 h = h_next;
612 }/* end while(h) */
613
614 h = p->rsp_cap;
615 while (h) {
616 h_next = h->next;
617 if (h->name)
618 free(h->name);
619
620 pool_destroy(h->pool);
621 free(h);
622 h = h_next;
623 }/* end while(h) */
624
625 s = p->srv;
626 while (s) {
627 s_next = s->next;
628 if (s->id)
629 free(s->id);
630
631 if (s->cookie)
632 free(s->cookie);
633
634 free(s);
635 s = s_next;
636 }/* end while(s) */
637
638 l = p->listen;
639 while (l) {
640 l_next = l->next;
641 free(l);
642 l = l_next;
643 }/* end while(l) */
644
645 pool_destroy((void **) p->req_cap_pool);
646 pool_destroy((void **) p->rsp_cap_pool);
647 p = p->next;
648 }/* end while(p) */
649
650 if (global.chroot) free(global.chroot);
651 if (global.pidfile) free(global.pidfile);
652
Willy Tarreaubaaee002006-06-26 02:48:02 +0200653 if (fdtab) free(fdtab);
654
655 pool_destroy(pool_session);
656 pool_destroy(pool_buffer);
657 pool_destroy(pool_requri);
658 pool_destroy(pool_task);
659 pool_destroy(pool_capture);
660 pool_destroy(pool_appsess);
661
662 if (have_appsession) {
663 pool_destroy(apools.serverid);
664 pool_destroy(apools.sessid);
665 }
666} /* end deinit() */
667
668/* sends the signal <sig> to all pids found in <oldpids> */
669static void tell_old_pids(int sig)
670{
671 int p;
672 for (p = 0; p < nb_oldpids; p++)
673 kill(oldpids[p], sig);
674}
675
Willy Tarreau4f60f162007-04-08 16:39:58 +0200676/*
677 * Runs the polling loop
678 *
679 * FIXME:
680 * - we still use 'listeners' to check whether we want to stop or not.
681 *
682 */
683void run_poll_loop()
684{
685 int next_time;
686 tv_now(&now);
687
688 while (1) {
689 next_time = process_runnable_tasks();
690
691 /* stop when there's no connection left and we don't allow them anymore */
692 if (!actconn && listeners == 0)
693 break;
694
695 cur_poller.poll(&cur_poller, next_time);
696 }
697}
698
699
Willy Tarreaubaaee002006-06-26 02:48:02 +0200700int main(int argc, char **argv)
701{
702 int err, retry;
703 struct rlimit limit;
704 FILE *pidfile = NULL;
705 init(argc, argv);
706
707 signal(SIGQUIT, dump);
708 signal(SIGUSR1, sig_soft_stop);
709 signal(SIGHUP, sig_dump_state);
710#ifdef DEBUG_MEMORY
711 signal(SIGINT, sig_int);
712 signal(SIGTERM, sig_term);
713#endif
714
715 /* on very high loads, a sigpipe sometimes happen just between the
716 * getsockopt() which tells "it's OK to write", and the following write :-(
717 */
718#ifndef MSG_NOSIGNAL
719 signal(SIGPIPE, SIG_IGN);
720#endif
721
722 /* We will loop at most 100 times with 10 ms delay each time.
723 * That's at most 1 second. We only send a signal to old pids
724 * if we cannot grab at least one port.
725 */
726 retry = MAX_START_RETRIES;
727 err = ERR_NONE;
728 while (retry >= 0) {
729 struct timeval w;
730 err = start_proxies(retry == 0 || nb_oldpids == 0);
731 if (err != ERR_RETRYABLE)
732 break;
733 if (nb_oldpids == 0)
734 break;
735
736 /* FIXME-20060514: Solaris and OpenBSD do not support shutdown() on
737 * listening sockets. So on those platforms, it would be wiser to
738 * simply send SIGUSR1, which will not be undoable.
739 */
740 tell_old_pids(SIGTTOU);
741 /* give some time to old processes to stop listening */
742 w.tv_sec = 0;
743 w.tv_usec = 10*1000;
744 select(0, NULL, NULL, NULL, &w);
745 retry--;
746 }
747
748 /* Note: start_proxies() sends an alert when it fails. */
749 if (err != ERR_NONE) {
750 if (retry != MAX_START_RETRIES && nb_oldpids)
751 tell_old_pids(SIGTTIN);
752 exit(1);
753 }
754
755 if (listeners == 0) {
756 Alert("[%s.main()] No enabled listener found (check the <listen> keywords) ! Exiting.\n", argv[0]);
757 /* Note: we don't have to send anything to the old pids because we
758 * never stopped them. */
759 exit(1);
760 }
761
762 /* prepare pause/play signals */
763 signal(SIGTTOU, sig_pause);
764 signal(SIGTTIN, sig_listen);
765
766 if (global.mode & MODE_DAEMON) {
767 global.mode &= ~MODE_VERBOSE;
768 global.mode |= MODE_QUIET;
769 }
770
771 /* MODE_QUIET can inhibit alerts and warnings below this line */
772
773 global.mode &= ~MODE_STARTING;
774 if ((global.mode & MODE_QUIET) && !(global.mode & MODE_VERBOSE)) {
775 /* detach from the tty */
776 fclose(stdin); fclose(stdout); fclose(stderr);
777 close(0); close(1); close(2);
778 }
779
780 /* open log & pid files before the chroot */
781 if (global.mode & MODE_DAEMON && global.pidfile != NULL) {
782 int pidfd;
783 unlink(global.pidfile);
784 pidfd = open(global.pidfile, O_CREAT | O_WRONLY | O_TRUNC, 0644);
785 if (pidfd < 0) {
786 Alert("[%s.main()] Cannot create pidfile %s\n", argv[0], global.pidfile);
787 if (nb_oldpids)
788 tell_old_pids(SIGTTIN);
789 exit(1);
790 }
791 pidfile = fdopen(pidfd, "w");
792 }
793
794 /* chroot if needed */
795 if (global.chroot != NULL) {
796 if (chroot(global.chroot) == -1) {
797 Alert("[%s.main()] Cannot chroot(%s).\n", argv[0], global.chroot);
798 if (nb_oldpids)
799 tell_old_pids(SIGTTIN);
800 }
801 chdir("/");
802 }
803
804 /* ulimits */
805 if (!global.rlimit_nofile)
806 global.rlimit_nofile = global.maxsock;
807
808 if (global.rlimit_nofile) {
809 limit.rlim_cur = limit.rlim_max = global.rlimit_nofile;
810 if (setrlimit(RLIMIT_NOFILE, &limit) == -1) {
811 Warning("[%s.main()] Cannot raise FD limit to %d.\n", argv[0], global.rlimit_nofile);
812 }
813 }
814
815 if (global.rlimit_memmax) {
816 limit.rlim_cur = limit.rlim_max =
817 global.rlimit_memmax * 1048576 / global.nbproc;
818#ifdef RLIMIT_AS
819 if (setrlimit(RLIMIT_AS, &limit) == -1) {
820 Warning("[%s.main()] Cannot fix MEM limit to %d megs.\n",
821 argv[0], global.rlimit_memmax);
822 }
823#else
824 if (setrlimit(RLIMIT_DATA, &limit) == -1) {
825 Warning("[%s.main()] Cannot fix MEM limit to %d megs.\n",
826 argv[0], global.rlimit_memmax);
827 }
828#endif
829 }
830
Willy Tarreau6d1a9882007-01-07 02:03:04 +0100831#ifdef CONFIG_HAP_TCPSPLICE
832 if (global.last_checks & LSTCHK_TCPSPLICE) {
833 if (tcp_splice_start() < 0) {
834 Alert("[%s.main()] Cannot enable tcp_splice.\n"
835 " Make sure you have enough permissions and that the module is loadable.\n"
836 " Alternatively, you may disable the 'tcpsplice' options in the configuration.\n"
837 "", argv[0], global.gid);
838 exit(1);
839 }
840 }
841#endif
842
Willy Tarreaub38651a2007-03-24 17:24:39 +0100843#ifdef CONFIG_HAP_CTTPROXY
844 if (global.last_checks & LSTCHK_CTTPROXY) {
845 int ret;
846
847 ret = check_cttproxy_version();
848 if (ret < 0) {
849 Alert("[%s.main()] Cannot enable cttproxy.\n%s",
850 argv[0],
851 (ret == -1) ? " Incorrect module version.\n"
852 : " Make sure you have enough permissions and that the module is loaded.\n");
853 exit(1);
854 }
855 }
856#endif
857
858 if ((global.last_checks & LSTCHK_NETADM) && global.uid) {
859 Alert("[%s.main()] Some configuration options require full privileges, so global.uid cannot be changed.\n"
860 "", argv[0], global.gid);
861 exit(1);
862 }
863
Willy Tarreaubaaee002006-06-26 02:48:02 +0200864 if (nb_oldpids)
865 tell_old_pids(oldpids_sig);
866
867 /* Note that any error at this stage will be fatal because we will not
868 * be able to restart the old pids.
869 */
870
871 /* setgid / setuid */
872 if (global.gid && setgid(global.gid) == -1) {
873 Alert("[%s.main()] Cannot set gid %d.\n", argv[0], global.gid);
874 exit(1);
875 }
876
877 if (global.uid && setuid(global.uid) == -1) {
878 Alert("[%s.main()] Cannot set uid %d.\n", argv[0], global.uid);
879 exit(1);
880 }
881
882 /* check ulimits */
883 limit.rlim_cur = limit.rlim_max = 0;
884 getrlimit(RLIMIT_NOFILE, &limit);
885 if (limit.rlim_cur < global.maxsock) {
886 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",
887 argv[0], limit.rlim_cur, global.maxconn, global.maxsock, global.maxsock);
888 }
889
890 if (global.mode & MODE_DAEMON) {
891 int ret = 0;
892 int proc;
893
894 /* the father launches the required number of processes */
895 for (proc = 0; proc < global.nbproc; proc++) {
896 ret = fork();
897 if (ret < 0) {
898 Alert("[%s.main()] Cannot fork.\n", argv[0]);
899 if (nb_oldpids)
900 exit(1); /* there has been an error */
901 }
902 else if (ret == 0) /* child breaks here */
903 break;
904 if (pidfile != NULL) {
905 fprintf(pidfile, "%d\n", ret);
906 fflush(pidfile);
907 }
908 }
909 /* close the pidfile both in children and father */
910 if (pidfile != NULL)
911 fclose(pidfile);
912 free(global.pidfile);
913
914 if (proc == global.nbproc)
915 exit(0); /* parent must leave */
916
917 /* if we're NOT in QUIET mode, we should now close the 3 first FDs to ensure
918 * that we can detach from the TTY. We MUST NOT do it in other cases since
919 * it would have already be done, and 0-2 would have been affected to listening
920 * sockets
921 */
922 if (!(global.mode & MODE_QUIET)) {
923 /* detach from the tty */
924 fclose(stdin); fclose(stdout); fclose(stderr);
925 close(0); close(1); close(2); /* close all fd's */
926 global.mode |= MODE_QUIET; /* ensure that we won't say anything from now */
927 }
928 pid = getpid(); /* update child's pid */
929 setsid();
Willy Tarreau2ff76222007-04-09 19:29:56 +0200930 fork_poller();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200931 }
932
Willy Tarreau4f60f162007-04-08 16:39:58 +0200933 /*
934 * That's it : the central polling loop. Run until we stop.
935 */
936 run_poll_loop();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200937
938 /* Free all Hash Keys and all Hash elements */
939 appsession_cleanup();
940 /* Do some cleanup */
941 deinit();
942
943 exit(0);
944}
945
946
947/*
948 * Local variables:
949 * c-indent-level: 8
950 * c-basic-offset: 8
951 * End:
952 */