blob: 5a79646ba396e2f67fbbf9517a5934c00432f022 [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>
Willy Tarreaud740bab2007-10-28 11:14:07 +010067#include <common/errors.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020068#include <common/memory.h>
69#include <common/mini-clist.h>
70#include <common/regex.h>
71#include <common/standard.h>
72#include <common/time.h>
73#include <common/uri_auth.h>
74#include <common/version.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020075
76#include <types/capture.h>
77#include <types/global.h>
78#include <types/httperr.h>
Willy Tarreau69801b82007-04-09 15:28:51 +020079#include <types/polling.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020080#include <types/proto_http.h>
81
Willy Tarreau0fc45a72007-06-17 00:36:03 +020082#include <proto/acl.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020083#include <proto/backend.h>
84#include <proto/buffers.h>
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +020085#include <proto/checks.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020086#include <proto/client.h>
87#include <proto/fd.h>
88#include <proto/log.h>
Willy Tarreaudd815982007-10-16 12:25:14 +020089#include <proto/protocols.h>
Willy Tarreau80587432006-12-24 17:47:20 +010090#include <proto/proto_http.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020091#include <proto/proxy.h>
92#include <proto/queue.h>
93#include <proto/server.h>
Willy Tarreauc6ca1a02007-05-13 19:43:47 +020094#include <proto/session.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020095#include <proto/stream_sock.h>
96#include <proto/task.h>
97
Willy Tarreau6d1a9882007-01-07 02:03:04 +010098#ifdef CONFIG_HAP_TCPSPLICE
99#include <libtcpsplice.h>
100#endif
101
Willy Tarreaub38651a2007-03-24 17:24:39 +0100102#ifdef CONFIG_HAP_CTTPROXY
103#include <proto/cttproxy.h>
104#endif
105
Willy Tarreaubaaee002006-06-26 02:48:02 +0200106/*********************************************************************/
107
108/*********************************************************************/
109
110char *cfg_cfgfile = NULL; /* configuration file */
111char *progname = NULL; /* program name */
112int pid; /* current process id */
Willy Tarreau28156642007-11-26 16:13:36 +0100113int relative_pid = 1; /* process id starting at 1 */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200114
115/* global options */
116struct global global = {
117 logfac1 : -1,
118 logfac2 : -1,
119 loglev1 : 7, /* max syslog level : debug */
120 loglev2 : 7,
Willy Tarreaufbee7132007-10-18 13:53:22 +0200121 .stats_timeout = { .tv_sec = 10, .tv_usec = 0 }, /* stats timeout = 10 seconds */
Willy Tarreau03f6d672007-10-18 15:15:57 +0200122 .stats_sock = {
123 .timeout = &global.stats_timeout,
124 .maxconn = 10, /* 10 concurrent stats connections */
125 .perm = {
126 .ux = {
127 .uid = -1,
128 .gid = -1,
129 .mode = 0,
130 }
131 }
132 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200133 /* others NULL OK */
134};
135
136/*********************************************************************/
137
138int stopping; /* non zero means stopping in progress */
139
140/* Here we store informations about the pids of the processes we may pause
141 * or kill. We will send them a signal every 10 ms until we can bind to all
142 * our ports. With 200 retries, that's about 2 seconds.
143 */
144#define MAX_START_RETRIES 200
145static int nb_oldpids = 0;
146static int *oldpids = NULL;
147static int oldpids_sig; /* use USR1 or TERM */
148
149/* this is used to drain data, and as a temporary buffer for sprintf()... */
150char trash[BUFSIZE];
151
152const int zero = 0;
153const int one = 1;
Alexandre Cassen87ea5482007-10-11 20:48:58 +0200154const struct linger nolinger = { .l_onoff = 1, .l_linger = 0 };
Willy Tarreaubaaee002006-06-26 02:48:02 +0200155
156/*
157 * Syslog facilities and levels. Conforming to RFC3164.
158 */
159
160#define MAX_HOSTNAME_LEN 32
161static char hostname[MAX_HOSTNAME_LEN] = "";
162
163
164/*********************************************************************/
165/* general purpose functions ***************************************/
166/*********************************************************************/
167
168void display_version()
169{
170 printf("HA-Proxy version " HAPROXY_VERSION " " HAPROXY_DATE"\n");
Willy Tarreau49e1ee82007-01-22 00:56:46 +0100171 printf("Copyright 2000-2007 Willy Tarreau <w@1wt.eu>\n\n");
Willy Tarreaubaaee002006-06-26 02:48:02 +0200172}
173
174/*
175 * This function prints the command line usage and exits
176 */
177void usage(char *name)
178{
179 display_version();
180 fprintf(stderr,
181 "Usage : %s -f <cfgfile> [ -vdV"
182 "D ] [ -n <maxconn> ] [ -N <maxpconn> ]\n"
183 " [ -p <pidfile> ] [ -m <max megs> ]\n"
184 " -v displays version\n"
185 " -d enters debug mode ; -db only disables background mode.\n"
186 " -V enters verbose mode (disables quiet mode)\n"
187 " -D goes daemon ; implies -q\n"
188 " -q quiet mode : don't display messages\n"
189 " -c check mode : only check config file and exit\n"
190 " -n sets the maximum total # of connections (%d)\n"
191 " -m limits the usable amount of memory (in MB)\n"
192 " -N sets the default, per-proxy maximum # of connections (%d)\n"
193 " -p writes pids of all children to this file\n"
194#if defined(ENABLE_EPOLL)
195 " -de disables epoll() usage even when available\n"
196#endif
Willy Tarreaude99e992007-04-16 00:53:59 +0200197#if defined(ENABLE_SEPOLL)
198 " -ds disables speculative epoll() usage even when available\n"
199#endif
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200200#if defined(ENABLE_KQUEUE)
201 " -dk disables kqueue() usage even when available\n"
202#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200203#if defined(ENABLE_POLL)
204 " -dp disables poll() usage even when available\n"
205#endif
206 " -sf/-st [pid ]* finishes/terminates old pids. Must be last arguments.\n"
207 "\n",
208 name, DEFAULT_MAXCONN, cfg_maxpconn);
209 exit(1);
210}
211
212
213
214/*********************************************************************/
215/* more specific functions ***************************************/
216/*********************************************************************/
217
218/*
219 * upon SIGUSR1, let's have a soft stop.
220 */
221void sig_soft_stop(int sig)
222{
223 soft_stop();
Willy Tarreau4d2d0982007-05-14 00:39:29 +0200224 pool_gc2();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200225 signal(sig, SIG_IGN);
226}
227
228/*
229 * upon SIGTTOU, we pause everything
230 */
231void sig_pause(int sig)
232{
233 pause_proxies();
Willy Tarreau4d2d0982007-05-14 00:39:29 +0200234 pool_gc2();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200235 signal(sig, sig_pause);
236}
237
238/*
239 * upon SIGTTIN, let's have a soft stop.
240 */
241void sig_listen(int sig)
242{
243 listen_proxies();
244 signal(sig, sig_listen);
245}
246
247/*
248 * this function dumps every server's state when the process receives SIGHUP.
249 */
250void sig_dump_state(int sig)
251{
252 struct proxy *p = proxy;
253
254 Warning("SIGHUP received, dumping servers states.\n");
255 while (p) {
256 struct server *s = p->srv;
257
258 send_log(p, LOG_NOTICE, "SIGHUP received, dumping servers states for proxy %s.\n", p->id);
259 while (s) {
260 snprintf(trash, sizeof(trash),
261 "SIGHUP: Server %s/%s is %s. Conn: %d act, %d pend, %d tot.",
262 p->id, s->id,
263 (s->state & SRV_RUNNING) ? "UP" : "DOWN",
264 s->cur_sess, s->nbpend, s->cum_sess);
265 Warning("%s\n", trash);
266 send_log(p, LOG_NOTICE, "%s\n", trash);
267 s = s->next;
268 }
269
Willy Tarreau5fcc8f12007-09-17 11:27:09 +0200270 /* FIXME: those info are a bit outdated. We should be able to distinguish between FE and BE. */
271 if (!p->srv) {
272 snprintf(trash, sizeof(trash),
273 "SIGHUP: Proxy %s has no servers. Conn: act(FE+BE): %d+%d, %d pend (%d unass), tot(FE+BE): %d+%d.",
274 p->id,
275 p->feconn, p->beconn, p->totpend, p->nbpend, p->cum_feconn, p->cum_beconn);
276 } else if (p->srv_act == 0) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200277 snprintf(trash, sizeof(trash),
Willy Tarreauf1221aa2006-12-17 22:14:12 +0100278 "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 +0200279 p->id,
280 (p->srv_bck) ? "is running on backup servers" : "has no server available",
Willy Tarreauf1221aa2006-12-17 22:14:12 +0100281 p->feconn, p->beconn, p->totpend, p->nbpend, p->cum_feconn, p->cum_beconn);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200282 } else {
283 snprintf(trash, sizeof(trash),
284 "SIGHUP: Proxy %s has %d active servers and %d backup servers available."
Willy Tarreauf1221aa2006-12-17 22:14:12 +0100285 " Conn: act(FE+BE): %d+%d, %d pend (%d unass), tot(FE+BE): %d+%d.",
Willy Tarreaubaaee002006-06-26 02:48:02 +0200286 p->id, p->srv_act, p->srv_bck,
Willy Tarreauf1221aa2006-12-17 22:14:12 +0100287 p->feconn, p->beconn, p->totpend, p->nbpend, p->cum_feconn, p->cum_beconn);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200288 }
289 Warning("%s\n", trash);
290 send_log(p, LOG_NOTICE, "%s\n", trash);
291
292 p = p->next;
293 }
294 signal(sig, sig_dump_state);
295}
296
297void dump(int sig)
298{
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200299#if 0
Willy Tarreau964c9362007-01-07 00:38:00 +0100300 struct task *t;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200301 struct session *s;
Willy Tarreau964c9362007-01-07 00:38:00 +0100302 struct rb_node *node;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200303
Willy Tarreau964c9362007-01-07 00:38:00 +0100304 for(node = rb_first(&wait_queue[0]);
305 node != NULL; node = rb_next(node)) {
306 t = rb_entry(node, struct task, rb_node);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200307 s = t->context;
308 qfprintf(stderr,"[dump] wq: task %p, still %ld ms, "
309 "cli=%d, srv=%d, cr=%d, cw=%d, sr=%d, sw=%d, "
310 "req=%d, rep=%d, clifd=%d\n",
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200311 s, tv_ms_remain(&now, &t->expire),
Willy Tarreaubaaee002006-06-26 02:48:02 +0200312 s->cli_state,
313 s->srv_state,
Willy Tarreauf161a342007-04-08 16:59:42 +0200314 EV_FD_ISSET(s->cli_fd, DIR_RD),
315 EV_FD_ISSET(s->cli_fd, DIR_WR),
316 EV_FD_ISSET(s->srv_fd, DIR_RD),
317 EV_FD_ISSET(s->srv_fd, DIR_WR),
Willy Tarreaubaaee002006-06-26 02:48:02 +0200318 s->req->l, s->rep?s->rep->l:0, s->cli_fd
319 );
320 }
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200321#endif
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200322 /* dump memory usage then free everything possible */
323 dump_pools();
324 pool_gc2();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200325}
326
327#ifdef DEBUG_MEMORY
328static void fast_stop(void)
329{
330 struct proxy *p;
331 p = proxy;
332 while (p) {
333 p->grace = 0;
334 p = p->next;
335 }
336 soft_stop();
337}
338
339void sig_int(int sig)
340{
341 /* This would normally be a hard stop,
342 but we want to be sure about deallocation,
343 and so on, so we do a soft stop with
344 0 GRACE time
345 */
346 fast_stop();
Willy Tarreau4d2d0982007-05-14 00:39:29 +0200347 pool_gc2();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200348 /* If we are killed twice, we decide to die*/
349 signal(sig, SIG_DFL);
350}
351
352void sig_term(int sig)
353{
354 /* This would normally be a hard stop,
355 but we want to be sure about deallocation,
356 and so on, so we do a soft stop with
357 0 GRACE time
358 */
359 fast_stop();
Willy Tarreau4d2d0982007-05-14 00:39:29 +0200360 pool_gc2();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200361 /* If we are killed twice, we decide to die*/
362 signal(sig, SIG_DFL);
363}
364#endif
365
366
367/*
368 * This function initializes all the necessary variables. It only returns
369 * if everything is OK. If something fails, it exits.
370 */
371void init(int argc, char **argv)
372{
373 int i;
374 int arg_mode = 0; /* MODE_DEBUG, ... */
375 char *old_argv = *argv;
376 char *tmp;
377 char *cfg_pidfile = NULL;
378
379 if (1<<INTBITS != sizeof(int)*8) {
380 fprintf(stderr,
381 "Error: wrong architecture. Recompile so that sizeof(int)=%d\n",
382 (int)(sizeof(int)*8));
383 exit(1);
384 }
385
386 /*
387 * Initialize the previously static variables.
388 */
389
390 totalconn = actconn = maxfd = listeners = stopping = 0;
391
392
393#ifdef HAPROXY_MEMMAX
394 global.rlimit_memmax = HAPROXY_MEMMAX;
395#endif
396
Willy Tarreaubaaee002006-06-26 02:48:02 +0200397 tv_now(&now);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200398 start_date = now;
399
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200400 init_task();
401 init_session();
Willy Tarreaue4d7e552007-05-13 20:19:55 +0200402 init_buffer();
403 init_pendconn();
Willy Tarreau80587432006-12-24 17:47:20 +0100404 init_proto_http();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200405
406 cfg_polling_mechanism = POLL_USE_SELECT; /* select() is always available */
407#if defined(ENABLE_POLL)
408 cfg_polling_mechanism |= POLL_USE_POLL;
409#endif
410#if defined(ENABLE_EPOLL)
411 cfg_polling_mechanism |= POLL_USE_EPOLL;
412#endif
Willy Tarreaude99e992007-04-16 00:53:59 +0200413#if defined(ENABLE_SEPOLL)
414 cfg_polling_mechanism |= POLL_USE_SEPOLL;
415#endif
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200416#if defined(ENABLE_KQUEUE)
417 cfg_polling_mechanism |= POLL_USE_KQUEUE;
418#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200419
420 pid = getpid();
421 progname = *argv;
422 while ((tmp = strchr(progname, '/')) != NULL)
423 progname = tmp + 1;
424
425 argc--; argv++;
426 while (argc > 0) {
427 char *flag;
428
429 if (**argv == '-') {
430 flag = *argv+1;
431
432 /* 1 arg */
433 if (*flag == 'v') {
434 display_version();
435 exit(0);
436 }
437#if defined(ENABLE_EPOLL)
438 else if (*flag == 'd' && flag[1] == 'e')
439 cfg_polling_mechanism &= ~POLL_USE_EPOLL;
440#endif
Willy Tarreaude99e992007-04-16 00:53:59 +0200441#if defined(ENABLE_SEPOLL)
442 else if (*flag == 'd' && flag[1] == 's')
443 cfg_polling_mechanism &= ~POLL_USE_SEPOLL;
444#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200445#if defined(ENABLE_POLL)
446 else if (*flag == 'd' && flag[1] == 'p')
447 cfg_polling_mechanism &= ~POLL_USE_POLL;
448#endif
Willy Tarreau69cad1a2007-04-10 22:45:11 +0200449#if defined(ENABLE_KQUEUE)
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200450 else if (*flag == 'd' && flag[1] == 'k')
451 cfg_polling_mechanism &= ~POLL_USE_KQUEUE;
452#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200453 else if (*flag == 'V')
454 arg_mode |= MODE_VERBOSE;
455 else if (*flag == 'd' && flag[1] == 'b')
456 arg_mode |= MODE_FOREGROUND;
457 else if (*flag == 'd')
458 arg_mode |= MODE_DEBUG;
459 else if (*flag == 'c')
460 arg_mode |= MODE_CHECK;
461 else if (*flag == 'D')
462 arg_mode |= MODE_DAEMON | MODE_QUIET;
463 else if (*flag == 'q')
464 arg_mode |= MODE_QUIET;
465 else if (*flag == 's' && (flag[1] == 'f' || flag[1] == 't')) {
466 /* list of pids to finish ('f') or terminate ('t') */
467
468 if (flag[1] == 'f')
469 oldpids_sig = SIGUSR1; /* finish then exit */
470 else
471 oldpids_sig = SIGTERM; /* terminate immediately */
472 argv++; argc--;
473
474 if (argc > 0) {
475 oldpids = calloc(argc, sizeof(int));
476 while (argc > 0) {
477 oldpids[nb_oldpids] = atol(*argv);
478 if (oldpids[nb_oldpids] <= 0)
479 usage(old_argv);
480 argc--; argv++;
481 nb_oldpids++;
482 }
483 }
484 }
485 else { /* >=2 args */
486 argv++; argc--;
487 if (argc == 0)
488 usage(old_argv);
489
490 switch (*flag) {
491 case 'n' : cfg_maxconn = atol(*argv); break;
492 case 'm' : global.rlimit_memmax = atol(*argv); break;
493 case 'N' : cfg_maxpconn = atol(*argv); break;
494 case 'f' : cfg_cfgfile = *argv; break;
495 case 'p' : cfg_pidfile = *argv; break;
496 default: usage(old_argv);
497 }
498 }
499 }
500 else
501 usage(old_argv);
502 argv++; argc--;
503 }
504
505 global.mode = MODE_STARTING | /* during startup, we want most of the alerts */
506 (arg_mode & (MODE_DAEMON | MODE_FOREGROUND | MODE_VERBOSE
507 | MODE_QUIET | MODE_CHECK | MODE_DEBUG));
508
509 if (!cfg_cfgfile)
510 usage(old_argv);
511
512 gethostname(hostname, MAX_HOSTNAME_LEN);
513
514 have_appsession = 0;
515 global.maxsock = 10; /* reserve 10 fds ; will be incremented by socket eaters */
516 if (readcfgfile(cfg_cfgfile) < 0) {
517 Alert("Error reading configuration file : %s\n", cfg_cfgfile);
518 exit(1);
519 }
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +0200520
Willy Tarreaubaaee002006-06-26 02:48:02 +0200521 if (have_appsession)
522 appsession_init();
523
524 if (global.mode & MODE_CHECK) {
525 qfprintf(stdout, "Configuration file is valid : %s\n", cfg_cfgfile);
526 exit(0);
527 }
528
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +0200529 if (start_checks() < 0)
530 exit(1);
531
Willy Tarreaubaaee002006-06-26 02:48:02 +0200532 if (cfg_maxconn > 0)
533 global.maxconn = cfg_maxconn;
534
535 if (cfg_pidfile) {
536 if (global.pidfile)
537 free(global.pidfile);
538 global.pidfile = strdup(cfg_pidfile);
539 }
540
541 if (global.maxconn == 0)
542 global.maxconn = DEFAULT_MAXCONN;
543
544 global.maxsock += global.maxconn * 2; /* each connection needs two sockets */
545
Willy Tarreau1db37712007-06-03 17:16:49 +0200546 if (global.tune.maxpollevents <= 0)
547 global.tune.maxpollevents = MAX_POLL_EVENTS;
548
Willy Tarreaubaaee002006-06-26 02:48:02 +0200549 if (arg_mode & (MODE_DEBUG | MODE_FOREGROUND)) {
550 /* command line debug mode inhibits configuration mode */
551 global.mode &= ~(MODE_DAEMON | MODE_QUIET);
552 }
553 global.mode |= (arg_mode & (MODE_DAEMON | MODE_FOREGROUND | MODE_QUIET |
Willy Tarreaufbee7132007-10-18 13:53:22 +0200554 MODE_VERBOSE | MODE_DEBUG ));
Willy Tarreaubaaee002006-06-26 02:48:02 +0200555
556 if ((global.mode & MODE_DEBUG) && (global.mode & (MODE_DAEMON | MODE_QUIET))) {
557 Warning("<debug> mode incompatible with <quiet> and <daemon>. Keeping <debug> only.\n");
558 global.mode &= ~(MODE_DAEMON | MODE_QUIET);
559 }
560
561 if ((global.nbproc > 1) && !(global.mode & MODE_DAEMON)) {
562 if (!(global.mode & (MODE_FOREGROUND | MODE_DEBUG)))
563 Warning("<nbproc> is only meaningful in daemon mode. Setting limit to 1 process.\n");
564 global.nbproc = 1;
565 }
566
567 if (global.nbproc < 1)
568 global.nbproc = 1;
569
Willy Tarreaubaaee002006-06-26 02:48:02 +0200570 fdtab = (struct fdtab *)calloc(1,
571 sizeof(struct fdtab) * (global.maxsock));
572 for (i = 0; i < global.maxsock; i++) {
573 fdtab[i].state = FD_STCLOSE;
574 }
Willy Tarreau4f60f162007-04-08 16:39:58 +0200575
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200576 /*
577 * Note: we could register external pollers here.
578 * Built-in pollers have been registered before main().
579 */
Willy Tarreau4f60f162007-04-08 16:39:58 +0200580
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200581 if (!(cfg_polling_mechanism & POLL_USE_KQUEUE))
582 disable_poller("kqueue");
583
Willy Tarreau4f60f162007-04-08 16:39:58 +0200584 if (!(cfg_polling_mechanism & POLL_USE_EPOLL))
585 disable_poller("epoll");
586
Willy Tarreaude99e992007-04-16 00:53:59 +0200587 if (!(cfg_polling_mechanism & POLL_USE_SEPOLL))
588 disable_poller("sepoll");
589
Willy Tarreau4f60f162007-04-08 16:39:58 +0200590 if (!(cfg_polling_mechanism & POLL_USE_POLL))
591 disable_poller("poll");
592
593 if (!(cfg_polling_mechanism & POLL_USE_SELECT))
594 disable_poller("select");
595
596 /* Note: we could disable any poller by name here */
597
Willy Tarreau2ff76222007-04-09 19:29:56 +0200598 if (global.mode & (MODE_VERBOSE|MODE_DEBUG))
599 list_pollers(stderr);
600
Willy Tarreau4f60f162007-04-08 16:39:58 +0200601 if (!init_pollers()) {
Willy Tarreau2ff76222007-04-09 19:29:56 +0200602 Alert("No polling mechanism available.\n");
Willy Tarreau4f60f162007-04-08 16:39:58 +0200603 exit(1);
604 }
Willy Tarreau2ff76222007-04-09 19:29:56 +0200605 if (global.mode & (MODE_VERBOSE|MODE_DEBUG)) {
606 printf("Using %s() as the polling mechanism.\n", cur_poller.name);
Willy Tarreau4f60f162007-04-08 16:39:58 +0200607 }
608
Willy Tarreaubaaee002006-06-26 02:48:02 +0200609}
610
611void deinit(void)
612{
Willy Tarreau4d2d0982007-05-14 00:39:29 +0200613 struct proxy *p = proxy, *p0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200614 struct cap_hdr *h,*h_next;
615 struct server *s,*s_next;
616 struct listener *l,*l_next;
Willy Tarreau0fc45a72007-06-17 00:36:03 +0200617 struct acl_cond *cond, *condb;
618 struct hdr_exp *exp, *expb;
619 int i;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200620
621 while (p) {
622 if (p->id)
623 free(p->id);
624
625 if (p->check_req)
626 free(p->check_req);
627
628 if (p->cookie_name)
629 free(p->cookie_name);
630
Willy Tarreau01732802007-11-01 22:48:15 +0100631 if (p->url_param_name)
632 free(p->url_param_name);
633
Willy Tarreaubaaee002006-06-26 02:48:02 +0200634 if (p->capture_name)
635 free(p->capture_name);
636
Willy Tarreau0fc45a72007-06-17 00:36:03 +0200637 if (p->monitor_uri)
638 free(p->monitor_uri);
639
640 for (i = 0; i < HTTP_ERR_SIZE; i++) {
641 if (p->errmsg[i].len)
642 free(p->errmsg[i].str);
643 }
644
645 for (i = 0; i < p->nb_reqadd; i++) {
646 if (p->req_add[i])
647 free(p->req_add[i]);
648 }
649
650 for (i = 0; i < p->nb_rspadd; i++) {
651 if (p->rsp_add[i])
652 free(p->rsp_add[i]);
653 }
654
655 list_for_each_entry_safe(cond, condb, &p->block_cond, list) {
656 LIST_DEL(&cond->list);
657 prune_acl_cond(cond);
658 free(cond);
659 }
660
661 for (exp = p->req_exp; exp != NULL; ) {
662 if (exp->preg)
663 regfree((regex_t *)exp->preg);
664 if (exp->replace && exp->action != ACT_SETBE)
665 free((char *)exp->replace);
666 expb = exp;
667 exp = exp->next;
668 free(expb);
669 }
670
671 for (exp = p->rsp_exp; exp != NULL; ) {
672 if (exp->preg)
673 regfree((regex_t *)exp->preg);
674 if (exp->replace && exp->action != ACT_SETBE)
675 free((char *)exp->replace);
676 expb = exp;
677 exp = exp->next;
678 free(expb);
679 }
680
681 /* FIXME: this must also be freed :
682 * - ACLs
683 * - uri_auth (but it's shared)
684 */
685
Willy Tarreaubaaee002006-06-26 02:48:02 +0200686 if (p->appsession_name)
687 free(p->appsession_name);
688
689 h = p->req_cap;
690 while (h) {
691 h_next = h->next;
692 if (h->name)
693 free(h->name);
Willy Tarreaucf7f3202007-05-13 22:46:04 +0200694 pool_destroy2(h->pool);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200695 free(h);
696 h = h_next;
697 }/* end while(h) */
698
699 h = p->rsp_cap;
700 while (h) {
701 h_next = h->next;
702 if (h->name)
703 free(h->name);
704
Willy Tarreaucf7f3202007-05-13 22:46:04 +0200705 pool_destroy2(h->pool);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200706 free(h);
707 h = h_next;
708 }/* end while(h) */
709
710 s = p->srv;
711 while (s) {
712 s_next = s->next;
713 if (s->id)
714 free(s->id);
715
716 if (s->cookie)
717 free(s->cookie);
718
719 free(s);
720 s = s_next;
721 }/* end while(s) */
722
723 l = p->listen;
724 while (l) {
725 l_next = l->next;
726 free(l);
727 l = l_next;
728 }/* end while(l) */
729
Willy Tarreaucf7f3202007-05-13 22:46:04 +0200730 pool_destroy2(p->req_cap_pool);
731 pool_destroy2(p->rsp_cap_pool);
Willy Tarreau4d2d0982007-05-14 00:39:29 +0200732 p0 = p;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200733 p = p->next;
Willy Tarreau4d2d0982007-05-14 00:39:29 +0200734 free(p0);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200735 }/* end while(p) */
Willy Tarreaudd815982007-10-16 12:25:14 +0200736
737 protocol_unbind_all();
738
Willy Tarreaubaaee002006-06-26 02:48:02 +0200739 if (global.chroot) free(global.chroot);
Willy Tarreau0fc45a72007-06-17 00:36:03 +0200740 global.chroot = NULL;
741
Willy Tarreaubaaee002006-06-26 02:48:02 +0200742 if (global.pidfile) free(global.pidfile);
Willy Tarreau0fc45a72007-06-17 00:36:03 +0200743 global.pidfile = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200744
Willy Tarreaubaaee002006-06-26 02:48:02 +0200745 if (fdtab) free(fdtab);
Willy Tarreau0fc45a72007-06-17 00:36:03 +0200746 fdtab = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200747
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200748 pool_destroy2(pool2_session);
Willy Tarreau7341d942007-05-13 19:56:02 +0200749 pool_destroy2(pool2_buffer);
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200750 pool_destroy2(pool2_requri);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200751 pool_destroy2(pool2_task);
Willy Tarreau086b3b42007-05-13 21:45:51 +0200752 pool_destroy2(pool2_capture);
Willy Tarreau63963c62007-05-13 21:29:55 +0200753 pool_destroy2(pool2_appsess);
Willy Tarreaue4d7e552007-05-13 20:19:55 +0200754 pool_destroy2(pool2_pendconn);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200755
756 if (have_appsession) {
Willy Tarreau63963c62007-05-13 21:29:55 +0200757 pool_destroy2(apools.serverid);
758 pool_destroy2(apools.sessid);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200759 }
760} /* end deinit() */
761
762/* sends the signal <sig> to all pids found in <oldpids> */
763static void tell_old_pids(int sig)
764{
765 int p;
766 for (p = 0; p < nb_oldpids; p++)
767 kill(oldpids[p], sig);
768}
769
Willy Tarreau4f60f162007-04-08 16:39:58 +0200770/*
771 * Runs the polling loop
772 *
773 * FIXME:
774 * - we still use 'listeners' to check whether we want to stop or not.
775 *
776 */
777void run_poll_loop()
778{
Willy Tarreaud825eef2007-05-12 22:35:00 +0200779 struct timeval next;
Willy Tarreau4f60f162007-04-08 16:39:58 +0200780
Willy Tarreaud825eef2007-05-12 22:35:00 +0200781 tv_now(&now);
Willy Tarreau4f60f162007-04-08 16:39:58 +0200782 while (1) {
Willy Tarreaud825eef2007-05-12 22:35:00 +0200783 process_runnable_tasks(&next);
Willy Tarreau4f60f162007-04-08 16:39:58 +0200784
785 /* stop when there's no connection left and we don't allow them anymore */
786 if (!actconn && listeners == 0)
787 break;
788
Willy Tarreaud825eef2007-05-12 22:35:00 +0200789 cur_poller.poll(&cur_poller, &next);
Willy Tarreau4f60f162007-04-08 16:39:58 +0200790 }
791}
792
793
Willy Tarreaubaaee002006-06-26 02:48:02 +0200794int main(int argc, char **argv)
795{
796 int err, retry;
797 struct rlimit limit;
798 FILE *pidfile = NULL;
799 init(argc, argv);
800
801 signal(SIGQUIT, dump);
802 signal(SIGUSR1, sig_soft_stop);
803 signal(SIGHUP, sig_dump_state);
804#ifdef DEBUG_MEMORY
805 signal(SIGINT, sig_int);
806 signal(SIGTERM, sig_term);
807#endif
808
809 /* on very high loads, a sigpipe sometimes happen just between the
810 * getsockopt() which tells "it's OK to write", and the following write :-(
811 */
812#ifndef MSG_NOSIGNAL
813 signal(SIGPIPE, SIG_IGN);
814#endif
815
816 /* We will loop at most 100 times with 10 ms delay each time.
817 * That's at most 1 second. We only send a signal to old pids
818 * if we cannot grab at least one port.
819 */
820 retry = MAX_START_RETRIES;
821 err = ERR_NONE;
822 while (retry >= 0) {
823 struct timeval w;
824 err = start_proxies(retry == 0 || nb_oldpids == 0);
825 if (err != ERR_RETRYABLE)
826 break;
827 if (nb_oldpids == 0)
828 break;
829
830 /* FIXME-20060514: Solaris and OpenBSD do not support shutdown() on
831 * listening sockets. So on those platforms, it would be wiser to
832 * simply send SIGUSR1, which will not be undoable.
833 */
834 tell_old_pids(SIGTTOU);
835 /* give some time to old processes to stop listening */
836 w.tv_sec = 0;
837 w.tv_usec = 10*1000;
838 select(0, NULL, NULL, NULL, &w);
839 retry--;
840 }
841
842 /* Note: start_proxies() sends an alert when it fails. */
843 if (err != ERR_NONE) {
844 if (retry != MAX_START_RETRIES && nb_oldpids)
845 tell_old_pids(SIGTTIN);
846 exit(1);
847 }
848
849 if (listeners == 0) {
850 Alert("[%s.main()] No enabled listener found (check the <listen> keywords) ! Exiting.\n", argv[0]);
851 /* Note: we don't have to send anything to the old pids because we
852 * never stopped them. */
853 exit(1);
854 }
855
Willy Tarreaudd815982007-10-16 12:25:14 +0200856 if (protocol_bind_all() != ERR_NONE) {
857 Alert("[%s.main()] Some protocols failed to start their listeners! Exiting.\n", argv[0]);
858 protocol_unbind_all(); /* cleanup everything we can */
859 if (nb_oldpids)
860 tell_old_pids(SIGTTIN);
861 exit(1);
862 }
863
Willy Tarreaubaaee002006-06-26 02:48:02 +0200864 /* prepare pause/play signals */
865 signal(SIGTTOU, sig_pause);
866 signal(SIGTTIN, sig_listen);
867
868 if (global.mode & MODE_DAEMON) {
869 global.mode &= ~MODE_VERBOSE;
870 global.mode |= MODE_QUIET;
871 }
872
873 /* MODE_QUIET can inhibit alerts and warnings below this line */
874
875 global.mode &= ~MODE_STARTING;
876 if ((global.mode & MODE_QUIET) && !(global.mode & MODE_VERBOSE)) {
877 /* detach from the tty */
878 fclose(stdin); fclose(stdout); fclose(stderr);
879 close(0); close(1); close(2);
880 }
881
882 /* open log & pid files before the chroot */
883 if (global.mode & MODE_DAEMON && global.pidfile != NULL) {
884 int pidfd;
885 unlink(global.pidfile);
886 pidfd = open(global.pidfile, O_CREAT | O_WRONLY | O_TRUNC, 0644);
887 if (pidfd < 0) {
888 Alert("[%s.main()] Cannot create pidfile %s\n", argv[0], global.pidfile);
889 if (nb_oldpids)
890 tell_old_pids(SIGTTIN);
Willy Tarreaudd815982007-10-16 12:25:14 +0200891 protocol_unbind_all();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200892 exit(1);
893 }
894 pidfile = fdopen(pidfd, "w");
895 }
896
Willy Tarreaubaaee002006-06-26 02:48:02 +0200897 /* ulimits */
898 if (!global.rlimit_nofile)
899 global.rlimit_nofile = global.maxsock;
900
901 if (global.rlimit_nofile) {
902 limit.rlim_cur = limit.rlim_max = global.rlimit_nofile;
903 if (setrlimit(RLIMIT_NOFILE, &limit) == -1) {
904 Warning("[%s.main()] Cannot raise FD limit to %d.\n", argv[0], global.rlimit_nofile);
905 }
906 }
907
908 if (global.rlimit_memmax) {
909 limit.rlim_cur = limit.rlim_max =
910 global.rlimit_memmax * 1048576 / global.nbproc;
911#ifdef RLIMIT_AS
912 if (setrlimit(RLIMIT_AS, &limit) == -1) {
913 Warning("[%s.main()] Cannot fix MEM limit to %d megs.\n",
914 argv[0], global.rlimit_memmax);
915 }
916#else
917 if (setrlimit(RLIMIT_DATA, &limit) == -1) {
918 Warning("[%s.main()] Cannot fix MEM limit to %d megs.\n",
919 argv[0], global.rlimit_memmax);
920 }
921#endif
922 }
923
Willy Tarreau6d1a9882007-01-07 02:03:04 +0100924#ifdef CONFIG_HAP_TCPSPLICE
925 if (global.last_checks & LSTCHK_TCPSPLICE) {
926 if (tcp_splice_start() < 0) {
927 Alert("[%s.main()] Cannot enable tcp_splice.\n"
928 " Make sure you have enough permissions and that the module is loadable.\n"
929 " Alternatively, you may disable the 'tcpsplice' options in the configuration.\n"
930 "", argv[0], global.gid);
Willy Tarreaudd815982007-10-16 12:25:14 +0200931 protocol_unbind_all();
Willy Tarreau6d1a9882007-01-07 02:03:04 +0100932 exit(1);
933 }
934 }
935#endif
936
Willy Tarreaub38651a2007-03-24 17:24:39 +0100937#ifdef CONFIG_HAP_CTTPROXY
938 if (global.last_checks & LSTCHK_CTTPROXY) {
939 int ret;
940
941 ret = check_cttproxy_version();
942 if (ret < 0) {
943 Alert("[%s.main()] Cannot enable cttproxy.\n%s",
944 argv[0],
945 (ret == -1) ? " Incorrect module version.\n"
946 : " Make sure you have enough permissions and that the module is loaded.\n");
Willy Tarreaudd815982007-10-16 12:25:14 +0200947 protocol_unbind_all();
Willy Tarreaub38651a2007-03-24 17:24:39 +0100948 exit(1);
949 }
950 }
951#endif
952
953 if ((global.last_checks & LSTCHK_NETADM) && global.uid) {
954 Alert("[%s.main()] Some configuration options require full privileges, so global.uid cannot be changed.\n"
955 "", argv[0], global.gid);
Willy Tarreaudd815982007-10-16 12:25:14 +0200956 protocol_unbind_all();
Willy Tarreaub38651a2007-03-24 17:24:39 +0100957 exit(1);
958 }
959
Willy Tarreauf223cc02007-10-15 18:57:08 +0200960 /* chroot if needed */
961 if (global.chroot != NULL) {
962 if (chroot(global.chroot) == -1) {
963 Alert("[%s.main()] Cannot chroot(%s).\n", argv[0], global.chroot);
964 if (nb_oldpids)
965 tell_old_pids(SIGTTIN);
Willy Tarreaudd815982007-10-16 12:25:14 +0200966 protocol_unbind_all();
Willy Tarreauf223cc02007-10-15 18:57:08 +0200967 exit(1);
968 }
969 chdir("/");
970 }
971
Willy Tarreaubaaee002006-06-26 02:48:02 +0200972 if (nb_oldpids)
973 tell_old_pids(oldpids_sig);
974
975 /* Note that any error at this stage will be fatal because we will not
976 * be able to restart the old pids.
977 */
978
979 /* setgid / setuid */
980 if (global.gid && setgid(global.gid) == -1) {
981 Alert("[%s.main()] Cannot set gid %d.\n", argv[0], global.gid);
Willy Tarreaudd815982007-10-16 12:25:14 +0200982 protocol_unbind_all();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200983 exit(1);
984 }
985
986 if (global.uid && setuid(global.uid) == -1) {
987 Alert("[%s.main()] Cannot set uid %d.\n", argv[0], global.uid);
Willy Tarreaudd815982007-10-16 12:25:14 +0200988 protocol_unbind_all();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200989 exit(1);
990 }
991
992 /* check ulimits */
993 limit.rlim_cur = limit.rlim_max = 0;
994 getrlimit(RLIMIT_NOFILE, &limit);
995 if (limit.rlim_cur < global.maxsock) {
996 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",
997 argv[0], limit.rlim_cur, global.maxconn, global.maxsock, global.maxsock);
998 }
999
1000 if (global.mode & MODE_DAEMON) {
1001 int ret = 0;
1002 int proc;
1003
1004 /* the father launches the required number of processes */
1005 for (proc = 0; proc < global.nbproc; proc++) {
1006 ret = fork();
1007 if (ret < 0) {
1008 Alert("[%s.main()] Cannot fork.\n", argv[0]);
Willy Tarreaudd815982007-10-16 12:25:14 +02001009 protocol_unbind_all();
Willy Tarreaud6803712007-10-16 07:44:56 +02001010 exit(1); /* there has been an error */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001011 }
1012 else if (ret == 0) /* child breaks here */
1013 break;
1014 if (pidfile != NULL) {
1015 fprintf(pidfile, "%d\n", ret);
1016 fflush(pidfile);
1017 }
Willy Tarreaudcd47712007-11-04 23:35:08 +01001018 relative_pid++; /* each child will get a different one */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001019 }
1020 /* close the pidfile both in children and father */
1021 if (pidfile != NULL)
1022 fclose(pidfile);
1023 free(global.pidfile);
Krzysztof Oledzki56f1e8b2007-10-11 18:30:14 +02001024 global.pidfile = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001025
1026 if (proc == global.nbproc)
1027 exit(0); /* parent must leave */
1028
1029 /* if we're NOT in QUIET mode, we should now close the 3 first FDs to ensure
1030 * that we can detach from the TTY. We MUST NOT do it in other cases since
1031 * it would have already be done, and 0-2 would have been affected to listening
1032 * sockets
1033 */
1034 if (!(global.mode & MODE_QUIET)) {
1035 /* detach from the tty */
1036 fclose(stdin); fclose(stdout); fclose(stderr);
1037 close(0); close(1); close(2); /* close all fd's */
1038 global.mode |= MODE_QUIET; /* ensure that we won't say anything from now */
1039 }
1040 pid = getpid(); /* update child's pid */
1041 setsid();
Willy Tarreau2ff76222007-04-09 19:29:56 +02001042 fork_poller();
Willy Tarreaubaaee002006-06-26 02:48:02 +02001043 }
1044
Willy Tarreaudd815982007-10-16 12:25:14 +02001045 protocol_enable_all();
Willy Tarreau4f60f162007-04-08 16:39:58 +02001046 /*
1047 * That's it : the central polling loop. Run until we stop.
1048 */
1049 run_poll_loop();
Willy Tarreaubaaee002006-06-26 02:48:02 +02001050
1051 /* Free all Hash Keys and all Hash elements */
1052 appsession_cleanup();
1053 /* Do some cleanup */
1054 deinit();
1055
1056 exit(0);
1057}
1058
1059
1060/*
1061 * Local variables:
1062 * c-indent-level: 8
1063 * c-basic-offset: 8
1064 * End:
1065 */