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