blob: 887c162e85c8dd528496d2ad1f182003ec2a80a1 [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
Willy Tarreau7b066db2007-12-02 11:28:59 +0100174void display_build_opts()
175{
176 printf("Build options :"
177#ifdef BUILD_TARGET
178 "\n TARGET = " BUILD_TARGET
179#endif
180#ifdef BUILD_CPU
181 "\n CPU = " BUILD_CPU
182#endif
183#ifdef BUILD_REGEX
184 "\n REGEX = " BUILD_REGEX
185#endif
186#ifdef BUILD_CC
187 "\n CC = " BUILD_CC
188#endif
189#ifdef BUILD_OPTS
190 "\n COPTS = " BUILD_OPTS
191#endif
192 "\n\n");
193}
194
Willy Tarreaubaaee002006-06-26 02:48:02 +0200195/*
196 * This function prints the command line usage and exits
197 */
198void usage(char *name)
199{
200 display_version();
201 fprintf(stderr,
202 "Usage : %s -f <cfgfile> [ -vdV"
203 "D ] [ -n <maxconn> ] [ -N <maxpconn> ]\n"
204 " [ -p <pidfile> ] [ -m <max megs> ]\n"
Willy Tarreau7b066db2007-12-02 11:28:59 +0100205 " -v displays version ; -vv shows known build options.\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200206 " -d enters debug mode ; -db only disables background mode.\n"
207 " -V enters verbose mode (disables quiet mode)\n"
208 " -D goes daemon ; implies -q\n"
209 " -q quiet mode : don't display messages\n"
210 " -c check mode : only check config file and exit\n"
211 " -n sets the maximum total # of connections (%d)\n"
212 " -m limits the usable amount of memory (in MB)\n"
213 " -N sets the default, per-proxy maximum # of connections (%d)\n"
214 " -p writes pids of all children to this file\n"
215#if defined(ENABLE_EPOLL)
216 " -de disables epoll() usage even when available\n"
217#endif
Willy Tarreaude99e992007-04-16 00:53:59 +0200218#if defined(ENABLE_SEPOLL)
219 " -ds disables speculative epoll() usage even when available\n"
220#endif
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200221#if defined(ENABLE_KQUEUE)
222 " -dk disables kqueue() usage even when available\n"
223#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200224#if defined(ENABLE_POLL)
225 " -dp disables poll() usage even when available\n"
226#endif
227 " -sf/-st [pid ]* finishes/terminates old pids. Must be last arguments.\n"
228 "\n",
229 name, DEFAULT_MAXCONN, cfg_maxpconn);
230 exit(1);
231}
232
233
234
235/*********************************************************************/
236/* more specific functions ***************************************/
237/*********************************************************************/
238
239/*
240 * upon SIGUSR1, let's have a soft stop.
241 */
242void sig_soft_stop(int sig)
243{
244 soft_stop();
Willy Tarreau4d2d0982007-05-14 00:39:29 +0200245 pool_gc2();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200246 signal(sig, SIG_IGN);
247}
248
249/*
250 * upon SIGTTOU, we pause everything
251 */
252void sig_pause(int sig)
253{
254 pause_proxies();
Willy Tarreau4d2d0982007-05-14 00:39:29 +0200255 pool_gc2();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200256 signal(sig, sig_pause);
257}
258
259/*
260 * upon SIGTTIN, let's have a soft stop.
261 */
262void sig_listen(int sig)
263{
264 listen_proxies();
265 signal(sig, sig_listen);
266}
267
268/*
269 * this function dumps every server's state when the process receives SIGHUP.
270 */
271void sig_dump_state(int sig)
272{
273 struct proxy *p = proxy;
274
275 Warning("SIGHUP received, dumping servers states.\n");
276 while (p) {
277 struct server *s = p->srv;
278
279 send_log(p, LOG_NOTICE, "SIGHUP received, dumping servers states for proxy %s.\n", p->id);
280 while (s) {
281 snprintf(trash, sizeof(trash),
282 "SIGHUP: Server %s/%s is %s. Conn: %d act, %d pend, %d tot.",
283 p->id, s->id,
284 (s->state & SRV_RUNNING) ? "UP" : "DOWN",
285 s->cur_sess, s->nbpend, s->cum_sess);
286 Warning("%s\n", trash);
287 send_log(p, LOG_NOTICE, "%s\n", trash);
288 s = s->next;
289 }
290
Willy Tarreau5fcc8f12007-09-17 11:27:09 +0200291 /* FIXME: those info are a bit outdated. We should be able to distinguish between FE and BE. */
292 if (!p->srv) {
293 snprintf(trash, sizeof(trash),
294 "SIGHUP: Proxy %s has no servers. Conn: act(FE+BE): %d+%d, %d pend (%d unass), tot(FE+BE): %d+%d.",
295 p->id,
296 p->feconn, p->beconn, p->totpend, p->nbpend, p->cum_feconn, p->cum_beconn);
297 } else if (p->srv_act == 0) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200298 snprintf(trash, sizeof(trash),
Willy Tarreauf1221aa2006-12-17 22:14:12 +0100299 "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 +0200300 p->id,
301 (p->srv_bck) ? "is running on backup servers" : "has no server available",
Willy Tarreauf1221aa2006-12-17 22:14:12 +0100302 p->feconn, p->beconn, p->totpend, p->nbpend, p->cum_feconn, p->cum_beconn);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200303 } else {
304 snprintf(trash, sizeof(trash),
305 "SIGHUP: Proxy %s has %d active servers and %d backup servers available."
Willy Tarreauf1221aa2006-12-17 22:14:12 +0100306 " Conn: act(FE+BE): %d+%d, %d pend (%d unass), tot(FE+BE): %d+%d.",
Willy Tarreaubaaee002006-06-26 02:48:02 +0200307 p->id, p->srv_act, p->srv_bck,
Willy Tarreauf1221aa2006-12-17 22:14:12 +0100308 p->feconn, p->beconn, p->totpend, p->nbpend, p->cum_feconn, p->cum_beconn);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200309 }
310 Warning("%s\n", trash);
311 send_log(p, LOG_NOTICE, "%s\n", trash);
312
313 p = p->next;
314 }
315 signal(sig, sig_dump_state);
316}
317
318void dump(int sig)
319{
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200320#if 0
Willy Tarreau964c9362007-01-07 00:38:00 +0100321 struct task *t;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200322 struct session *s;
Willy Tarreau964c9362007-01-07 00:38:00 +0100323 struct rb_node *node;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200324
Willy Tarreau964c9362007-01-07 00:38:00 +0100325 for(node = rb_first(&wait_queue[0]);
326 node != NULL; node = rb_next(node)) {
327 t = rb_entry(node, struct task, rb_node);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200328 s = t->context;
329 qfprintf(stderr,"[dump] wq: task %p, still %ld ms, "
330 "cli=%d, srv=%d, cr=%d, cw=%d, sr=%d, sw=%d, "
331 "req=%d, rep=%d, clifd=%d\n",
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200332 s, tv_ms_remain(&now, &t->expire),
Willy Tarreaubaaee002006-06-26 02:48:02 +0200333 s->cli_state,
334 s->srv_state,
Willy Tarreauf161a342007-04-08 16:59:42 +0200335 EV_FD_ISSET(s->cli_fd, DIR_RD),
336 EV_FD_ISSET(s->cli_fd, DIR_WR),
337 EV_FD_ISSET(s->srv_fd, DIR_RD),
338 EV_FD_ISSET(s->srv_fd, DIR_WR),
Willy Tarreaubaaee002006-06-26 02:48:02 +0200339 s->req->l, s->rep?s->rep->l:0, s->cli_fd
340 );
341 }
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200342#endif
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200343 /* dump memory usage then free everything possible */
344 dump_pools();
345 pool_gc2();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200346}
347
348#ifdef DEBUG_MEMORY
349static void fast_stop(void)
350{
351 struct proxy *p;
352 p = proxy;
353 while (p) {
354 p->grace = 0;
355 p = p->next;
356 }
357 soft_stop();
358}
359
360void sig_int(int sig)
361{
362 /* This would normally be a hard stop,
363 but we want to be sure about deallocation,
364 and so on, so we do a soft stop with
365 0 GRACE time
366 */
367 fast_stop();
Willy Tarreau4d2d0982007-05-14 00:39:29 +0200368 pool_gc2();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200369 /* If we are killed twice, we decide to die*/
370 signal(sig, SIG_DFL);
371}
372
373void sig_term(int sig)
374{
375 /* This would normally be a hard stop,
376 but we want to be sure about deallocation,
377 and so on, so we do a soft stop with
378 0 GRACE time
379 */
380 fast_stop();
Willy Tarreau4d2d0982007-05-14 00:39:29 +0200381 pool_gc2();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200382 /* If we are killed twice, we decide to die*/
383 signal(sig, SIG_DFL);
384}
385#endif
386
387
388/*
389 * This function initializes all the necessary variables. It only returns
390 * if everything is OK. If something fails, it exits.
391 */
392void init(int argc, char **argv)
393{
394 int i;
395 int arg_mode = 0; /* MODE_DEBUG, ... */
396 char *old_argv = *argv;
397 char *tmp;
398 char *cfg_pidfile = NULL;
399
400 if (1<<INTBITS != sizeof(int)*8) {
401 fprintf(stderr,
402 "Error: wrong architecture. Recompile so that sizeof(int)=%d\n",
403 (int)(sizeof(int)*8));
404 exit(1);
405 }
406
407 /*
408 * Initialize the previously static variables.
409 */
410
411 totalconn = actconn = maxfd = listeners = stopping = 0;
412
413
414#ifdef HAPROXY_MEMMAX
415 global.rlimit_memmax = HAPROXY_MEMMAX;
416#endif
417
Willy Tarreaubaaee002006-06-26 02:48:02 +0200418 tv_now(&now);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200419 start_date = now;
420
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200421 init_task();
422 init_session();
Willy Tarreaue4d7e552007-05-13 20:19:55 +0200423 init_buffer();
424 init_pendconn();
Willy Tarreau80587432006-12-24 17:47:20 +0100425 init_proto_http();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200426
427 cfg_polling_mechanism = POLL_USE_SELECT; /* select() is always available */
428#if defined(ENABLE_POLL)
429 cfg_polling_mechanism |= POLL_USE_POLL;
430#endif
431#if defined(ENABLE_EPOLL)
432 cfg_polling_mechanism |= POLL_USE_EPOLL;
433#endif
Willy Tarreaude99e992007-04-16 00:53:59 +0200434#if defined(ENABLE_SEPOLL)
435 cfg_polling_mechanism |= POLL_USE_SEPOLL;
436#endif
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200437#if defined(ENABLE_KQUEUE)
438 cfg_polling_mechanism |= POLL_USE_KQUEUE;
439#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200440
441 pid = getpid();
442 progname = *argv;
443 while ((tmp = strchr(progname, '/')) != NULL)
444 progname = tmp + 1;
445
446 argc--; argv++;
447 while (argc > 0) {
448 char *flag;
449
450 if (**argv == '-') {
451 flag = *argv+1;
452
453 /* 1 arg */
454 if (*flag == 'v') {
455 display_version();
Willy Tarreau7b066db2007-12-02 11:28:59 +0100456 if (flag[1] == 'v') /* -vv */
457 display_build_opts();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200458 exit(0);
459 }
460#if defined(ENABLE_EPOLL)
461 else if (*flag == 'd' && flag[1] == 'e')
462 cfg_polling_mechanism &= ~POLL_USE_EPOLL;
463#endif
Willy Tarreaude99e992007-04-16 00:53:59 +0200464#if defined(ENABLE_SEPOLL)
465 else if (*flag == 'd' && flag[1] == 's')
466 cfg_polling_mechanism &= ~POLL_USE_SEPOLL;
467#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200468#if defined(ENABLE_POLL)
469 else if (*flag == 'd' && flag[1] == 'p')
470 cfg_polling_mechanism &= ~POLL_USE_POLL;
471#endif
Willy Tarreau69cad1a2007-04-10 22:45:11 +0200472#if defined(ENABLE_KQUEUE)
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200473 else if (*flag == 'd' && flag[1] == 'k')
474 cfg_polling_mechanism &= ~POLL_USE_KQUEUE;
475#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200476 else if (*flag == 'V')
477 arg_mode |= MODE_VERBOSE;
478 else if (*flag == 'd' && flag[1] == 'b')
479 arg_mode |= MODE_FOREGROUND;
480 else if (*flag == 'd')
481 arg_mode |= MODE_DEBUG;
482 else if (*flag == 'c')
483 arg_mode |= MODE_CHECK;
484 else if (*flag == 'D')
485 arg_mode |= MODE_DAEMON | MODE_QUIET;
486 else if (*flag == 'q')
487 arg_mode |= MODE_QUIET;
488 else if (*flag == 's' && (flag[1] == 'f' || flag[1] == 't')) {
489 /* list of pids to finish ('f') or terminate ('t') */
490
491 if (flag[1] == 'f')
492 oldpids_sig = SIGUSR1; /* finish then exit */
493 else
494 oldpids_sig = SIGTERM; /* terminate immediately */
495 argv++; argc--;
496
497 if (argc > 0) {
498 oldpids = calloc(argc, sizeof(int));
499 while (argc > 0) {
500 oldpids[nb_oldpids] = atol(*argv);
501 if (oldpids[nb_oldpids] <= 0)
502 usage(old_argv);
503 argc--; argv++;
504 nb_oldpids++;
505 }
506 }
507 }
508 else { /* >=2 args */
509 argv++; argc--;
510 if (argc == 0)
511 usage(old_argv);
512
513 switch (*flag) {
514 case 'n' : cfg_maxconn = atol(*argv); break;
515 case 'm' : global.rlimit_memmax = atol(*argv); break;
516 case 'N' : cfg_maxpconn = atol(*argv); break;
517 case 'f' : cfg_cfgfile = *argv; break;
518 case 'p' : cfg_pidfile = *argv; break;
519 default: usage(old_argv);
520 }
521 }
522 }
523 else
524 usage(old_argv);
525 argv++; argc--;
526 }
527
528 global.mode = MODE_STARTING | /* during startup, we want most of the alerts */
529 (arg_mode & (MODE_DAEMON | MODE_FOREGROUND | MODE_VERBOSE
530 | MODE_QUIET | MODE_CHECK | MODE_DEBUG));
531
532 if (!cfg_cfgfile)
533 usage(old_argv);
534
535 gethostname(hostname, MAX_HOSTNAME_LEN);
536
537 have_appsession = 0;
538 global.maxsock = 10; /* reserve 10 fds ; will be incremented by socket eaters */
539 if (readcfgfile(cfg_cfgfile) < 0) {
540 Alert("Error reading configuration file : %s\n", cfg_cfgfile);
541 exit(1);
542 }
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +0200543
Willy Tarreaubaaee002006-06-26 02:48:02 +0200544 if (have_appsession)
545 appsession_init();
546
547 if (global.mode & MODE_CHECK) {
548 qfprintf(stdout, "Configuration file is valid : %s\n", cfg_cfgfile);
549 exit(0);
550 }
551
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +0200552 if (start_checks() < 0)
553 exit(1);
554
Willy Tarreaubaaee002006-06-26 02:48:02 +0200555 if (cfg_maxconn > 0)
556 global.maxconn = cfg_maxconn;
557
558 if (cfg_pidfile) {
559 if (global.pidfile)
560 free(global.pidfile);
561 global.pidfile = strdup(cfg_pidfile);
562 }
563
564 if (global.maxconn == 0)
565 global.maxconn = DEFAULT_MAXCONN;
566
567 global.maxsock += global.maxconn * 2; /* each connection needs two sockets */
568
Willy Tarreau1db37712007-06-03 17:16:49 +0200569 if (global.tune.maxpollevents <= 0)
570 global.tune.maxpollevents = MAX_POLL_EVENTS;
571
Willy Tarreaubaaee002006-06-26 02:48:02 +0200572 if (arg_mode & (MODE_DEBUG | MODE_FOREGROUND)) {
573 /* command line debug mode inhibits configuration mode */
574 global.mode &= ~(MODE_DAEMON | MODE_QUIET);
575 }
576 global.mode |= (arg_mode & (MODE_DAEMON | MODE_FOREGROUND | MODE_QUIET |
Willy Tarreaufbee7132007-10-18 13:53:22 +0200577 MODE_VERBOSE | MODE_DEBUG ));
Willy Tarreaubaaee002006-06-26 02:48:02 +0200578
579 if ((global.mode & MODE_DEBUG) && (global.mode & (MODE_DAEMON | MODE_QUIET))) {
580 Warning("<debug> mode incompatible with <quiet> and <daemon>. Keeping <debug> only.\n");
581 global.mode &= ~(MODE_DAEMON | MODE_QUIET);
582 }
583
584 if ((global.nbproc > 1) && !(global.mode & MODE_DAEMON)) {
585 if (!(global.mode & (MODE_FOREGROUND | MODE_DEBUG)))
586 Warning("<nbproc> is only meaningful in daemon mode. Setting limit to 1 process.\n");
587 global.nbproc = 1;
588 }
589
590 if (global.nbproc < 1)
591 global.nbproc = 1;
592
Willy Tarreaubaaee002006-06-26 02:48:02 +0200593 fdtab = (struct fdtab *)calloc(1,
594 sizeof(struct fdtab) * (global.maxsock));
595 for (i = 0; i < global.maxsock; i++) {
596 fdtab[i].state = FD_STCLOSE;
597 }
Willy Tarreau4f60f162007-04-08 16:39:58 +0200598
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200599 /*
600 * Note: we could register external pollers here.
601 * Built-in pollers have been registered before main().
602 */
Willy Tarreau4f60f162007-04-08 16:39:58 +0200603
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200604 if (!(cfg_polling_mechanism & POLL_USE_KQUEUE))
605 disable_poller("kqueue");
606
Willy Tarreau4f60f162007-04-08 16:39:58 +0200607 if (!(cfg_polling_mechanism & POLL_USE_EPOLL))
608 disable_poller("epoll");
609
Willy Tarreaude99e992007-04-16 00:53:59 +0200610 if (!(cfg_polling_mechanism & POLL_USE_SEPOLL))
611 disable_poller("sepoll");
612
Willy Tarreau4f60f162007-04-08 16:39:58 +0200613 if (!(cfg_polling_mechanism & POLL_USE_POLL))
614 disable_poller("poll");
615
616 if (!(cfg_polling_mechanism & POLL_USE_SELECT))
617 disable_poller("select");
618
619 /* Note: we could disable any poller by name here */
620
Willy Tarreau2ff76222007-04-09 19:29:56 +0200621 if (global.mode & (MODE_VERBOSE|MODE_DEBUG))
622 list_pollers(stderr);
623
Willy Tarreau4f60f162007-04-08 16:39:58 +0200624 if (!init_pollers()) {
Willy Tarreau2ff76222007-04-09 19:29:56 +0200625 Alert("No polling mechanism available.\n");
Willy Tarreau4f60f162007-04-08 16:39:58 +0200626 exit(1);
627 }
Willy Tarreau2ff76222007-04-09 19:29:56 +0200628 if (global.mode & (MODE_VERBOSE|MODE_DEBUG)) {
629 printf("Using %s() as the polling mechanism.\n", cur_poller.name);
Willy Tarreau4f60f162007-04-08 16:39:58 +0200630 }
631
Willy Tarreaubaaee002006-06-26 02:48:02 +0200632}
633
634void deinit(void)
635{
Willy Tarreau4d2d0982007-05-14 00:39:29 +0200636 struct proxy *p = proxy, *p0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200637 struct cap_hdr *h,*h_next;
638 struct server *s,*s_next;
639 struct listener *l,*l_next;
Willy Tarreau0fc45a72007-06-17 00:36:03 +0200640 struct acl_cond *cond, *condb;
641 struct hdr_exp *exp, *expb;
642 int i;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200643
644 while (p) {
645 if (p->id)
646 free(p->id);
647
648 if (p->check_req)
649 free(p->check_req);
650
651 if (p->cookie_name)
652 free(p->cookie_name);
653
Willy Tarreau01732802007-11-01 22:48:15 +0100654 if (p->url_param_name)
655 free(p->url_param_name);
656
Willy Tarreaubaaee002006-06-26 02:48:02 +0200657 if (p->capture_name)
658 free(p->capture_name);
659
Willy Tarreau0fc45a72007-06-17 00:36:03 +0200660 if (p->monitor_uri)
661 free(p->monitor_uri);
662
663 for (i = 0; i < HTTP_ERR_SIZE; i++) {
664 if (p->errmsg[i].len)
665 free(p->errmsg[i].str);
666 }
667
668 for (i = 0; i < p->nb_reqadd; i++) {
669 if (p->req_add[i])
670 free(p->req_add[i]);
671 }
672
673 for (i = 0; i < p->nb_rspadd; i++) {
674 if (p->rsp_add[i])
675 free(p->rsp_add[i]);
676 }
677
678 list_for_each_entry_safe(cond, condb, &p->block_cond, list) {
679 LIST_DEL(&cond->list);
680 prune_acl_cond(cond);
681 free(cond);
682 }
683
Willy Tarreaub80c2302007-11-30 20:51:32 +0100684 list_for_each_entry_safe(cond, condb, &p->mon_fail_cond, list) {
685 LIST_DEL(&cond->list);
686 prune_acl_cond(cond);
687 free(cond);
688 }
689
Willy Tarreau0fc45a72007-06-17 00:36:03 +0200690 for (exp = p->req_exp; exp != NULL; ) {
691 if (exp->preg)
692 regfree((regex_t *)exp->preg);
693 if (exp->replace && exp->action != ACT_SETBE)
694 free((char *)exp->replace);
695 expb = exp;
696 exp = exp->next;
697 free(expb);
698 }
699
700 for (exp = p->rsp_exp; exp != NULL; ) {
701 if (exp->preg)
702 regfree((regex_t *)exp->preg);
703 if (exp->replace && exp->action != ACT_SETBE)
704 free((char *)exp->replace);
705 expb = exp;
706 exp = exp->next;
707 free(expb);
708 }
709
710 /* FIXME: this must also be freed :
711 * - ACLs
712 * - uri_auth (but it's shared)
713 */
714
Willy Tarreaubaaee002006-06-26 02:48:02 +0200715 if (p->appsession_name)
716 free(p->appsession_name);
717
718 h = p->req_cap;
719 while (h) {
720 h_next = h->next;
721 if (h->name)
722 free(h->name);
Willy Tarreaucf7f3202007-05-13 22:46:04 +0200723 pool_destroy2(h->pool);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200724 free(h);
725 h = h_next;
726 }/* end while(h) */
727
728 h = p->rsp_cap;
729 while (h) {
730 h_next = h->next;
731 if (h->name)
732 free(h->name);
733
Willy Tarreaucf7f3202007-05-13 22:46:04 +0200734 pool_destroy2(h->pool);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200735 free(h);
736 h = h_next;
737 }/* end while(h) */
738
739 s = p->srv;
740 while (s) {
741 s_next = s->next;
742 if (s->id)
743 free(s->id);
744
745 if (s->cookie)
746 free(s->cookie);
747
748 free(s);
749 s = s_next;
750 }/* end while(s) */
751
752 l = p->listen;
753 while (l) {
754 l_next = l->next;
755 free(l);
756 l = l_next;
757 }/* end while(l) */
758
Willy Tarreaucf7f3202007-05-13 22:46:04 +0200759 pool_destroy2(p->req_cap_pool);
760 pool_destroy2(p->rsp_cap_pool);
Willy Tarreau4d2d0982007-05-14 00:39:29 +0200761 p0 = p;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200762 p = p->next;
Willy Tarreau4d2d0982007-05-14 00:39:29 +0200763 free(p0);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200764 }/* end while(p) */
Willy Tarreaudd815982007-10-16 12:25:14 +0200765
766 protocol_unbind_all();
767
Willy Tarreaubaaee002006-06-26 02:48:02 +0200768 if (global.chroot) free(global.chroot);
Willy Tarreau0fc45a72007-06-17 00:36:03 +0200769 global.chroot = NULL;
770
Willy Tarreaubaaee002006-06-26 02:48:02 +0200771 if (global.pidfile) free(global.pidfile);
Willy Tarreau0fc45a72007-06-17 00:36:03 +0200772 global.pidfile = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200773
Willy Tarreaubaaee002006-06-26 02:48:02 +0200774 if (fdtab) free(fdtab);
Willy Tarreau0fc45a72007-06-17 00:36:03 +0200775 fdtab = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200776
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200777 pool_destroy2(pool2_session);
Willy Tarreau7341d942007-05-13 19:56:02 +0200778 pool_destroy2(pool2_buffer);
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200779 pool_destroy2(pool2_requri);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200780 pool_destroy2(pool2_task);
Willy Tarreau086b3b42007-05-13 21:45:51 +0200781 pool_destroy2(pool2_capture);
Willy Tarreau63963c62007-05-13 21:29:55 +0200782 pool_destroy2(pool2_appsess);
Willy Tarreaue4d7e552007-05-13 20:19:55 +0200783 pool_destroy2(pool2_pendconn);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200784
785 if (have_appsession) {
Willy Tarreau63963c62007-05-13 21:29:55 +0200786 pool_destroy2(apools.serverid);
787 pool_destroy2(apools.sessid);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200788 }
789} /* end deinit() */
790
791/* sends the signal <sig> to all pids found in <oldpids> */
792static void tell_old_pids(int sig)
793{
794 int p;
795 for (p = 0; p < nb_oldpids; p++)
796 kill(oldpids[p], sig);
797}
798
Willy Tarreau4f60f162007-04-08 16:39:58 +0200799/*
800 * Runs the polling loop
801 *
802 * FIXME:
803 * - we still use 'listeners' to check whether we want to stop or not.
804 *
805 */
806void run_poll_loop()
807{
Willy Tarreaud825eef2007-05-12 22:35:00 +0200808 struct timeval next;
Willy Tarreau4f60f162007-04-08 16:39:58 +0200809
Willy Tarreaud825eef2007-05-12 22:35:00 +0200810 tv_now(&now);
Willy Tarreau4f60f162007-04-08 16:39:58 +0200811 while (1) {
Willy Tarreaud825eef2007-05-12 22:35:00 +0200812 process_runnable_tasks(&next);
Willy Tarreau4f60f162007-04-08 16:39:58 +0200813
814 /* stop when there's no connection left and we don't allow them anymore */
815 if (!actconn && listeners == 0)
816 break;
817
Willy Tarreaud825eef2007-05-12 22:35:00 +0200818 cur_poller.poll(&cur_poller, &next);
Willy Tarreau4f60f162007-04-08 16:39:58 +0200819 }
820}
821
822
Willy Tarreaubaaee002006-06-26 02:48:02 +0200823int main(int argc, char **argv)
824{
825 int err, retry;
826 struct rlimit limit;
827 FILE *pidfile = NULL;
828 init(argc, argv);
829
830 signal(SIGQUIT, dump);
831 signal(SIGUSR1, sig_soft_stop);
832 signal(SIGHUP, sig_dump_state);
833#ifdef DEBUG_MEMORY
834 signal(SIGINT, sig_int);
835 signal(SIGTERM, sig_term);
836#endif
837
838 /* on very high loads, a sigpipe sometimes happen just between the
839 * getsockopt() which tells "it's OK to write", and the following write :-(
840 */
841#ifndef MSG_NOSIGNAL
842 signal(SIGPIPE, SIG_IGN);
843#endif
844
845 /* We will loop at most 100 times with 10 ms delay each time.
846 * That's at most 1 second. We only send a signal to old pids
847 * if we cannot grab at least one port.
848 */
849 retry = MAX_START_RETRIES;
850 err = ERR_NONE;
851 while (retry >= 0) {
852 struct timeval w;
853 err = start_proxies(retry == 0 || nb_oldpids == 0);
854 if (err != ERR_RETRYABLE)
855 break;
856 if (nb_oldpids == 0)
857 break;
858
859 /* FIXME-20060514: Solaris and OpenBSD do not support shutdown() on
860 * listening sockets. So on those platforms, it would be wiser to
861 * simply send SIGUSR1, which will not be undoable.
862 */
863 tell_old_pids(SIGTTOU);
864 /* give some time to old processes to stop listening */
865 w.tv_sec = 0;
866 w.tv_usec = 10*1000;
867 select(0, NULL, NULL, NULL, &w);
868 retry--;
869 }
870
871 /* Note: start_proxies() sends an alert when it fails. */
872 if (err != ERR_NONE) {
873 if (retry != MAX_START_RETRIES && nb_oldpids)
874 tell_old_pids(SIGTTIN);
875 exit(1);
876 }
877
878 if (listeners == 0) {
879 Alert("[%s.main()] No enabled listener found (check the <listen> keywords) ! Exiting.\n", argv[0]);
880 /* Note: we don't have to send anything to the old pids because we
881 * never stopped them. */
882 exit(1);
883 }
884
Willy Tarreaudd815982007-10-16 12:25:14 +0200885 if (protocol_bind_all() != ERR_NONE) {
886 Alert("[%s.main()] Some protocols failed to start their listeners! Exiting.\n", argv[0]);
887 protocol_unbind_all(); /* cleanup everything we can */
888 if (nb_oldpids)
889 tell_old_pids(SIGTTIN);
890 exit(1);
891 }
892
Willy Tarreaubaaee002006-06-26 02:48:02 +0200893 /* prepare pause/play signals */
894 signal(SIGTTOU, sig_pause);
895 signal(SIGTTIN, sig_listen);
896
897 if (global.mode & MODE_DAEMON) {
898 global.mode &= ~MODE_VERBOSE;
899 global.mode |= MODE_QUIET;
900 }
901
902 /* MODE_QUIET can inhibit alerts and warnings below this line */
903
904 global.mode &= ~MODE_STARTING;
905 if ((global.mode & MODE_QUIET) && !(global.mode & MODE_VERBOSE)) {
906 /* detach from the tty */
907 fclose(stdin); fclose(stdout); fclose(stderr);
908 close(0); close(1); close(2);
909 }
910
911 /* open log & pid files before the chroot */
912 if (global.mode & MODE_DAEMON && global.pidfile != NULL) {
913 int pidfd;
914 unlink(global.pidfile);
915 pidfd = open(global.pidfile, O_CREAT | O_WRONLY | O_TRUNC, 0644);
916 if (pidfd < 0) {
917 Alert("[%s.main()] Cannot create pidfile %s\n", argv[0], global.pidfile);
918 if (nb_oldpids)
919 tell_old_pids(SIGTTIN);
Willy Tarreaudd815982007-10-16 12:25:14 +0200920 protocol_unbind_all();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200921 exit(1);
922 }
923 pidfile = fdopen(pidfd, "w");
924 }
925
Willy Tarreaubaaee002006-06-26 02:48:02 +0200926 /* ulimits */
927 if (!global.rlimit_nofile)
928 global.rlimit_nofile = global.maxsock;
929
930 if (global.rlimit_nofile) {
931 limit.rlim_cur = limit.rlim_max = global.rlimit_nofile;
932 if (setrlimit(RLIMIT_NOFILE, &limit) == -1) {
933 Warning("[%s.main()] Cannot raise FD limit to %d.\n", argv[0], global.rlimit_nofile);
934 }
935 }
936
937 if (global.rlimit_memmax) {
938 limit.rlim_cur = limit.rlim_max =
939 global.rlimit_memmax * 1048576 / global.nbproc;
940#ifdef RLIMIT_AS
941 if (setrlimit(RLIMIT_AS, &limit) == -1) {
942 Warning("[%s.main()] Cannot fix MEM limit to %d megs.\n",
943 argv[0], global.rlimit_memmax);
944 }
945#else
946 if (setrlimit(RLIMIT_DATA, &limit) == -1) {
947 Warning("[%s.main()] Cannot fix MEM limit to %d megs.\n",
948 argv[0], global.rlimit_memmax);
949 }
950#endif
951 }
952
Willy Tarreau6d1a9882007-01-07 02:03:04 +0100953#ifdef CONFIG_HAP_TCPSPLICE
954 if (global.last_checks & LSTCHK_TCPSPLICE) {
955 if (tcp_splice_start() < 0) {
956 Alert("[%s.main()] Cannot enable tcp_splice.\n"
957 " Make sure you have enough permissions and that the module is loadable.\n"
958 " Alternatively, you may disable the 'tcpsplice' options in the configuration.\n"
959 "", argv[0], global.gid);
Willy Tarreaudd815982007-10-16 12:25:14 +0200960 protocol_unbind_all();
Willy Tarreau6d1a9882007-01-07 02:03:04 +0100961 exit(1);
962 }
963 }
964#endif
965
Willy Tarreaub38651a2007-03-24 17:24:39 +0100966#ifdef CONFIG_HAP_CTTPROXY
967 if (global.last_checks & LSTCHK_CTTPROXY) {
968 int ret;
969
970 ret = check_cttproxy_version();
971 if (ret < 0) {
972 Alert("[%s.main()] Cannot enable cttproxy.\n%s",
973 argv[0],
974 (ret == -1) ? " Incorrect module version.\n"
975 : " Make sure you have enough permissions and that the module is loaded.\n");
Willy Tarreaudd815982007-10-16 12:25:14 +0200976 protocol_unbind_all();
Willy Tarreaub38651a2007-03-24 17:24:39 +0100977 exit(1);
978 }
979 }
980#endif
981
982 if ((global.last_checks & LSTCHK_NETADM) && global.uid) {
983 Alert("[%s.main()] Some configuration options require full privileges, so global.uid cannot be changed.\n"
984 "", argv[0], global.gid);
Willy Tarreaudd815982007-10-16 12:25:14 +0200985 protocol_unbind_all();
Willy Tarreaub38651a2007-03-24 17:24:39 +0100986 exit(1);
987 }
988
Willy Tarreauf223cc02007-10-15 18:57:08 +0200989 /* chroot if needed */
990 if (global.chroot != NULL) {
991 if (chroot(global.chroot) == -1) {
992 Alert("[%s.main()] Cannot chroot(%s).\n", argv[0], global.chroot);
993 if (nb_oldpids)
994 tell_old_pids(SIGTTIN);
Willy Tarreaudd815982007-10-16 12:25:14 +0200995 protocol_unbind_all();
Willy Tarreauf223cc02007-10-15 18:57:08 +0200996 exit(1);
997 }
998 chdir("/");
999 }
1000
Willy Tarreaubaaee002006-06-26 02:48:02 +02001001 if (nb_oldpids)
1002 tell_old_pids(oldpids_sig);
1003
1004 /* Note that any error at this stage will be fatal because we will not
1005 * be able to restart the old pids.
1006 */
1007
1008 /* setgid / setuid */
1009 if (global.gid && setgid(global.gid) == -1) {
1010 Alert("[%s.main()] Cannot set gid %d.\n", argv[0], global.gid);
Willy Tarreaudd815982007-10-16 12:25:14 +02001011 protocol_unbind_all();
Willy Tarreaubaaee002006-06-26 02:48:02 +02001012 exit(1);
1013 }
1014
1015 if (global.uid && setuid(global.uid) == -1) {
1016 Alert("[%s.main()] Cannot set uid %d.\n", argv[0], global.uid);
Willy Tarreaudd815982007-10-16 12:25:14 +02001017 protocol_unbind_all();
Willy Tarreaubaaee002006-06-26 02:48:02 +02001018 exit(1);
1019 }
1020
1021 /* check ulimits */
1022 limit.rlim_cur = limit.rlim_max = 0;
1023 getrlimit(RLIMIT_NOFILE, &limit);
1024 if (limit.rlim_cur < global.maxsock) {
1025 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",
1026 argv[0], limit.rlim_cur, global.maxconn, global.maxsock, global.maxsock);
1027 }
1028
1029 if (global.mode & MODE_DAEMON) {
1030 int ret = 0;
1031 int proc;
1032
1033 /* the father launches the required number of processes */
1034 for (proc = 0; proc < global.nbproc; proc++) {
1035 ret = fork();
1036 if (ret < 0) {
1037 Alert("[%s.main()] Cannot fork.\n", argv[0]);
Willy Tarreaudd815982007-10-16 12:25:14 +02001038 protocol_unbind_all();
Willy Tarreaud6803712007-10-16 07:44:56 +02001039 exit(1); /* there has been an error */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001040 }
1041 else if (ret == 0) /* child breaks here */
1042 break;
1043 if (pidfile != NULL) {
1044 fprintf(pidfile, "%d\n", ret);
1045 fflush(pidfile);
1046 }
Willy Tarreaudcd47712007-11-04 23:35:08 +01001047 relative_pid++; /* each child will get a different one */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001048 }
1049 /* close the pidfile both in children and father */
1050 if (pidfile != NULL)
1051 fclose(pidfile);
1052 free(global.pidfile);
Krzysztof Oledzki56f1e8b2007-10-11 18:30:14 +02001053 global.pidfile = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001054
1055 if (proc == global.nbproc)
1056 exit(0); /* parent must leave */
1057
1058 /* if we're NOT in QUIET mode, we should now close the 3 first FDs to ensure
1059 * that we can detach from the TTY. We MUST NOT do it in other cases since
1060 * it would have already be done, and 0-2 would have been affected to listening
1061 * sockets
1062 */
1063 if (!(global.mode & MODE_QUIET)) {
1064 /* detach from the tty */
1065 fclose(stdin); fclose(stdout); fclose(stderr);
1066 close(0); close(1); close(2); /* close all fd's */
1067 global.mode |= MODE_QUIET; /* ensure that we won't say anything from now */
1068 }
1069 pid = getpid(); /* update child's pid */
1070 setsid();
Willy Tarreau2ff76222007-04-09 19:29:56 +02001071 fork_poller();
Willy Tarreaubaaee002006-06-26 02:48:02 +02001072 }
1073
Willy Tarreaudd815982007-10-16 12:25:14 +02001074 protocol_enable_all();
Willy Tarreau4f60f162007-04-08 16:39:58 +02001075 /*
1076 * That's it : the central polling loop. Run until we stop.
1077 */
1078 run_poll_loop();
Willy Tarreaubaaee002006-06-26 02:48:02 +02001079
1080 /* Free all Hash Keys and all Hash elements */
1081 appsession_cleanup();
1082 /* Do some cleanup */
1083 deinit();
1084
1085 exit(0);
1086}
1087
1088
1089/*
1090 * Local variables:
1091 * c-indent-level: 8
1092 * c-basic-offset: 8
1093 * End:
1094 */