blob: 607ff155f4321aafe02c52ceb6bdde4767e352d4 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * HA-Proxy : High Availability-enabled HTTP/TCP proxy
Willy Tarreaub4556912009-07-23 13:45:47 +02003 * Copyright 2000-2009 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>
Willy Tarreaubaaee002006-06-26 02:48:02 +020078
Willy Tarreau0fc45a72007-06-17 00:36:03 +020079#include <proto/acl.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020080#include <proto/backend.h>
81#include <proto/buffers.h>
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +020082#include <proto/checks.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020083#include <proto/client.h>
84#include <proto/fd.h>
85#include <proto/log.h>
Willy Tarreaudd815982007-10-16 12:25:14 +020086#include <proto/protocols.h>
Willy Tarreau80587432006-12-24 17:47:20 +010087#include <proto/proto_http.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020088#include <proto/proxy.h>
89#include <proto/queue.h>
90#include <proto/server.h>
Willy Tarreauc6ca1a02007-05-13 19:43:47 +020091#include <proto/session.h>
Willy Tarreau29857942009-05-10 09:01:21 +020092#include <proto/signal.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020093#include <proto/stream_sock.h>
94#include <proto/task.h>
95
Willy Tarreau6d1a9882007-01-07 02:03:04 +010096#ifdef CONFIG_HAP_TCPSPLICE
97#include <libtcpsplice.h>
98#endif
99
Willy Tarreaub38651a2007-03-24 17:24:39 +0100100#ifdef CONFIG_HAP_CTTPROXY
101#include <proto/cttproxy.h>
102#endif
103
Willy Tarreaubaaee002006-06-26 02:48:02 +0200104/*********************************************************************/
105
106/*********************************************************************/
107
Willy Tarreau5d01a632009-06-22 16:02:30 +0200108static int cfg_nbcfgfiles; /* number of config files */
109static char *cfg_cfgfile[10]; /* configuration files, stop at NULL */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200110char *progname = NULL; /* program name */
111int pid; /* current process id */
Willy Tarreau28156642007-11-26 16:13:36 +0100112int relative_pid = 1; /* process id starting at 1 */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200113
114/* global options */
115struct global global = {
116 logfac1 : -1,
117 logfac2 : -1,
118 loglev1 : 7, /* max syslog level : debug */
119 loglev2 : 7,
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200120 .stats_timeout = MS_TO_TICKS(10000), /* stats timeout = 10 seconds */
Willy Tarreau03f6d672007-10-18 15:15:57 +0200121 .stats_sock = {
122 .timeout = &global.stats_timeout,
123 .maxconn = 10, /* 10 concurrent stats connections */
124 .perm = {
125 .ux = {
126 .uid = -1,
127 .gid = -1,
128 .mode = 0,
129 }
130 }
131 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200132 /* others NULL OK */
133};
134
135/*********************************************************************/
136
137int stopping; /* non zero means stopping in progress */
138
139/* Here we store informations about the pids of the processes we may pause
140 * or kill. We will send them a signal every 10 ms until we can bind to all
141 * our ports. With 200 retries, that's about 2 seconds.
142 */
143#define MAX_START_RETRIES 200
144static int nb_oldpids = 0;
145static int *oldpids = NULL;
146static int oldpids_sig; /* use USR1 or TERM */
147
148/* this is used to drain data, and as a temporary buffer for sprintf()... */
149char trash[BUFSIZE];
150
151const int zero = 0;
152const int one = 1;
Alexandre Cassen87ea5482007-10-11 20:48:58 +0200153const struct linger nolinger = { .l_onoff = 1, .l_linger = 0 };
Willy Tarreaubaaee002006-06-26 02:48:02 +0200154
Willy Tarreau3ad6a762009-08-16 10:08:02 +0200155char hostname[MAX_HOSTNAME_LEN] = "";
Willy Tarreaubaaee002006-06-26 02:48:02 +0200156
157
158/*********************************************************************/
159/* general purpose functions ***************************************/
160/*********************************************************************/
161
162void display_version()
163{
164 printf("HA-Proxy version " HAPROXY_VERSION " " HAPROXY_DATE"\n");
Willy Tarreaub4556912009-07-23 13:45:47 +0200165 printf("Copyright 2000-2009 Willy Tarreau <w@1wt.eu>\n\n");
Willy Tarreaubaaee002006-06-26 02:48:02 +0200166}
167
Willy Tarreau7b066db2007-12-02 11:28:59 +0100168void display_build_opts()
169{
170 printf("Build options :"
171#ifdef BUILD_TARGET
Willy Tarreau9f2b7302008-01-02 20:48:34 +0100172 "\n TARGET = " BUILD_TARGET
Willy Tarreau7b066db2007-12-02 11:28:59 +0100173#endif
174#ifdef BUILD_CPU
Willy Tarreau9f2b7302008-01-02 20:48:34 +0100175 "\n CPU = " BUILD_CPU
Willy Tarreau7b066db2007-12-02 11:28:59 +0100176#endif
177#ifdef BUILD_CC
Willy Tarreau9f2b7302008-01-02 20:48:34 +0100178 "\n CC = " BUILD_CC
179#endif
180#ifdef BUILD_CFLAGS
181 "\n CFLAGS = " BUILD_CFLAGS
Willy Tarreau7b066db2007-12-02 11:28:59 +0100182#endif
Willy Tarreau9f2b7302008-01-02 20:48:34 +0100183#ifdef BUILD_OPTIONS
184 "\n OPTIONS = " BUILD_OPTIONS
Willy Tarreau7b066db2007-12-02 11:28:59 +0100185#endif
186 "\n\n");
187}
188
Willy Tarreaubaaee002006-06-26 02:48:02 +0200189/*
190 * This function prints the command line usage and exits
191 */
192void usage(char *name)
193{
194 display_version();
195 fprintf(stderr,
Willy Tarreau5d01a632009-06-22 16:02:30 +0200196 "Usage : %s [-f <cfgfile>]* [ -vdV"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200197 "D ] [ -n <maxconn> ] [ -N <maxpconn> ]\n"
198 " [ -p <pidfile> ] [ -m <max megs> ]\n"
Willy Tarreau7b066db2007-12-02 11:28:59 +0100199 " -v displays version ; -vv shows known build options.\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200200 " -d enters debug mode ; -db only disables background mode.\n"
201 " -V enters verbose mode (disables quiet mode)\n"
Willy Tarreau6bde87b2009-05-18 16:29:51 +0200202 " -D goes daemon\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200203 " -q quiet mode : don't display messages\n"
Willy Tarreau5d01a632009-06-22 16:02:30 +0200204 " -c check mode : only check config files and exit\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200205 " -n sets the maximum total # of connections (%d)\n"
206 " -m limits the usable amount of memory (in MB)\n"
207 " -N sets the default, per-proxy maximum # of connections (%d)\n"
208 " -p writes pids of all children to this file\n"
209#if defined(ENABLE_EPOLL)
210 " -de disables epoll() usage even when available\n"
211#endif
Willy Tarreaude99e992007-04-16 00:53:59 +0200212#if defined(ENABLE_SEPOLL)
213 " -ds disables speculative epoll() usage even when available\n"
214#endif
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200215#if defined(ENABLE_KQUEUE)
216 " -dk disables kqueue() usage even when available\n"
217#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200218#if defined(ENABLE_POLL)
219 " -dp disables poll() usage even when available\n"
220#endif
Willy Tarreau3ab68cf2009-01-25 16:03:28 +0100221#if defined(CONFIG_HAP_LINUX_SPLICE) || defined(CONFIG_HAP_TCPSPLICE)
222 " -dS disables splice usage (broken on old kernels)\n"
223#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200224 " -sf/-st [pid ]* finishes/terminates old pids. Must be last arguments.\n"
225 "\n",
226 name, DEFAULT_MAXCONN, cfg_maxpconn);
227 exit(1);
228}
229
230
231
232/*********************************************************************/
233/* more specific functions ***************************************/
234/*********************************************************************/
235
236/*
237 * upon SIGUSR1, let's have a soft stop.
238 */
239void sig_soft_stop(int sig)
240{
241 soft_stop();
Willy Tarreau4d2d0982007-05-14 00:39:29 +0200242 pool_gc2();
Willy Tarreau01b3a532009-05-10 09:59:50 +0200243 signal_register(sig, SIG_IGN);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200244}
245
246/*
247 * upon SIGTTOU, we pause everything
248 */
249void sig_pause(int sig)
250{
251 pause_proxies();
Willy Tarreau4d2d0982007-05-14 00:39:29 +0200252 pool_gc2();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200253}
254
255/*
256 * upon SIGTTIN, let's have a soft stop.
257 */
258void sig_listen(int sig)
259{
260 listen_proxies();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200261}
262
263/*
264 * this function dumps every server's state when the process receives SIGHUP.
265 */
266void sig_dump_state(int sig)
267{
268 struct proxy *p = proxy;
269
270 Warning("SIGHUP received, dumping servers states.\n");
271 while (p) {
272 struct server *s = p->srv;
273
274 send_log(p, LOG_NOTICE, "SIGHUP received, dumping servers states for proxy %s.\n", p->id);
275 while (s) {
276 snprintf(trash, sizeof(trash),
Willy Tarreau3b88d442009-04-11 20:44:08 +0200277 "SIGHUP: Server %s/%s is %s. Conn: %d act, %d pend, %lld tot.",
Willy Tarreaubaaee002006-06-26 02:48:02 +0200278 p->id, s->id,
279 (s->state & SRV_RUNNING) ? "UP" : "DOWN",
280 s->cur_sess, s->nbpend, s->cum_sess);
281 Warning("%s\n", trash);
282 send_log(p, LOG_NOTICE, "%s\n", trash);
283 s = s->next;
284 }
285
Willy Tarreau5fcc8f12007-09-17 11:27:09 +0200286 /* FIXME: those info are a bit outdated. We should be able to distinguish between FE and BE. */
287 if (!p->srv) {
288 snprintf(trash, sizeof(trash),
Willy Tarreau3b88d442009-04-11 20:44:08 +0200289 "SIGHUP: Proxy %s has no servers. Conn: act(FE+BE): %d+%d, %d pend (%d unass), tot(FE+BE): %lld+%lld.",
Willy Tarreau5fcc8f12007-09-17 11:27:09 +0200290 p->id,
291 p->feconn, p->beconn, p->totpend, p->nbpend, p->cum_feconn, p->cum_beconn);
292 } else if (p->srv_act == 0) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200293 snprintf(trash, sizeof(trash),
Willy Tarreau3b88d442009-04-11 20:44:08 +0200294 "SIGHUP: Proxy %s %s ! Conn: act(FE+BE): %d+%d, %d pend (%d unass), tot(FE+BE): %lld+%lld.",
Willy Tarreaubaaee002006-06-26 02:48:02 +0200295 p->id,
296 (p->srv_bck) ? "is running on backup servers" : "has no server available",
Willy Tarreauf1221aa2006-12-17 22:14:12 +0100297 p->feconn, p->beconn, p->totpend, p->nbpend, p->cum_feconn, p->cum_beconn);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200298 } else {
299 snprintf(trash, sizeof(trash),
300 "SIGHUP: Proxy %s has %d active servers and %d backup servers available."
Willy Tarreau3b88d442009-04-11 20:44:08 +0200301 " Conn: act(FE+BE): %d+%d, %d pend (%d unass), tot(FE+BE): %lld+%lld.",
Willy Tarreaubaaee002006-06-26 02:48:02 +0200302 p->id, p->srv_act, p->srv_bck,
Willy Tarreauf1221aa2006-12-17 22:14:12 +0100303 p->feconn, p->beconn, p->totpend, p->nbpend, p->cum_feconn, p->cum_beconn);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200304 }
305 Warning("%s\n", trash);
306 send_log(p, LOG_NOTICE, "%s\n", trash);
307
308 p = p->next;
309 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200310}
311
312void dump(int sig)
313{
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200314#if 0
Willy Tarreau964c9362007-01-07 00:38:00 +0100315 struct task *t;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200316 struct session *s;
Willy Tarreau964c9362007-01-07 00:38:00 +0100317 struct rb_node *node;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200318
Willy Tarreau964c9362007-01-07 00:38:00 +0100319 for(node = rb_first(&wait_queue[0]);
320 node != NULL; node = rb_next(node)) {
321 t = rb_entry(node, struct task, rb_node);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200322 s = t->context;
323 qfprintf(stderr,"[dump] wq: task %p, still %ld ms, "
Willy Tarreau7e5067d2008-12-07 16:27:56 +0100324 "cli=%d, srv=%d, req=%d, rep=%d\n",
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200325 s, tv_ms_remain(&now, &t->expire),
Willy Tarreau7e5067d2008-12-07 16:27:56 +0100326 s->si[0].state,
327 s->si[1].state,
328 s->req->l, s->rep?s->rep->l:0);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200329 }
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200330#endif
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200331 /* dump memory usage then free everything possible */
332 dump_pools();
333 pool_gc2();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200334}
335
336#ifdef DEBUG_MEMORY
337static void fast_stop(void)
338{
339 struct proxy *p;
340 p = proxy;
341 while (p) {
342 p->grace = 0;
343 p = p->next;
344 }
345 soft_stop();
346}
347
348void sig_int(int sig)
349{
350 /* This would normally be a hard stop,
351 but we want to be sure about deallocation,
352 and so on, so we do a soft stop with
353 0 GRACE time
354 */
355 fast_stop();
Willy Tarreau4d2d0982007-05-14 00:39:29 +0200356 pool_gc2();
Willy Tarreau01b3a532009-05-10 09:59:50 +0200357 /* If we are killed twice, we decide to die */
358 signal_register(sig, SIG_DFL);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200359}
360
361void sig_term(int sig)
362{
363 /* This would normally be a hard stop,
364 but we want to be sure about deallocation,
365 and so on, so we do a soft stop with
366 0 GRACE time
367 */
368 fast_stop();
Willy Tarreau4d2d0982007-05-14 00:39:29 +0200369 pool_gc2();
Willy Tarreau01b3a532009-05-10 09:59:50 +0200370 /* If we are killed twice, we decide to die */
371 signal_register(sig, SIG_DFL);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200372}
373#endif
374
375
376/*
377 * This function initializes all the necessary variables. It only returns
378 * if everything is OK. If something fails, it exits.
379 */
380void init(int argc, char **argv)
381{
382 int i;
383 int arg_mode = 0; /* MODE_DEBUG, ... */
384 char *old_argv = *argv;
385 char *tmp;
386 char *cfg_pidfile = NULL;
Willy Tarreau058e9072009-07-20 09:30:05 +0200387 int err_code = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200388
Willy Tarreaubaaee002006-06-26 02:48:02 +0200389 /*
390 * Initialize the previously static variables.
391 */
392
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100393 totalconn = actconn = maxfd = listeners = stopping = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200394
395
396#ifdef HAPROXY_MEMMAX
397 global.rlimit_memmax = HAPROXY_MEMMAX;
398#endif
399
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200400 tv_update_date(-1,-1);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200401 start_date = now;
402
Willy Tarreau29857942009-05-10 09:01:21 +0200403 signal_init();
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200404 init_task();
405 init_session();
Willy Tarreaue4d7e552007-05-13 20:19:55 +0200406 init_buffer();
407 init_pendconn();
Willy Tarreau80587432006-12-24 17:47:20 +0100408 init_proto_http();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200409
Willy Tarreau43b78992009-01-25 15:42:27 +0100410 global.tune.options |= GTUNE_USE_SELECT; /* select() is always available */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200411#if defined(ENABLE_POLL)
Willy Tarreau43b78992009-01-25 15:42:27 +0100412 global.tune.options |= GTUNE_USE_POLL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200413#endif
414#if defined(ENABLE_EPOLL)
Willy Tarreau43b78992009-01-25 15:42:27 +0100415 global.tune.options |= GTUNE_USE_EPOLL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200416#endif
Willy Tarreaude99e992007-04-16 00:53:59 +0200417#if defined(ENABLE_SEPOLL)
Willy Tarreau43b78992009-01-25 15:42:27 +0100418 global.tune.options |= GTUNE_USE_SEPOLL;
Willy Tarreaude99e992007-04-16 00:53:59 +0200419#endif
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200420#if defined(ENABLE_KQUEUE)
Willy Tarreau43b78992009-01-25 15:42:27 +0100421 global.tune.options |= GTUNE_USE_KQUEUE;
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200422#endif
Willy Tarreau3ab68cf2009-01-25 16:03:28 +0100423#if defined(CONFIG_HAP_LINUX_SPLICE) || defined(CONFIG_HAP_TCPSPLICE)
424 global.tune.options |= GTUNE_USE_SPLICE;
425#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200426
427 pid = getpid();
428 progname = *argv;
429 while ((tmp = strchr(progname, '/')) != NULL)
430 progname = tmp + 1;
431
432 argc--; argv++;
433 while (argc > 0) {
434 char *flag;
435
436 if (**argv == '-') {
437 flag = *argv+1;
438
439 /* 1 arg */
440 if (*flag == 'v') {
441 display_version();
Willy Tarreau7b066db2007-12-02 11:28:59 +0100442 if (flag[1] == 'v') /* -vv */
443 display_build_opts();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200444 exit(0);
445 }
446#if defined(ENABLE_EPOLL)
447 else if (*flag == 'd' && flag[1] == 'e')
Willy Tarreau43b78992009-01-25 15:42:27 +0100448 global.tune.options &= ~GTUNE_USE_EPOLL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200449#endif
Willy Tarreaude99e992007-04-16 00:53:59 +0200450#if defined(ENABLE_SEPOLL)
451 else if (*flag == 'd' && flag[1] == 's')
Willy Tarreau43b78992009-01-25 15:42:27 +0100452 global.tune.options &= ~GTUNE_USE_SEPOLL;
Willy Tarreaude99e992007-04-16 00:53:59 +0200453#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200454#if defined(ENABLE_POLL)
455 else if (*flag == 'd' && flag[1] == 'p')
Willy Tarreau43b78992009-01-25 15:42:27 +0100456 global.tune.options &= ~GTUNE_USE_POLL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200457#endif
Willy Tarreau69cad1a2007-04-10 22:45:11 +0200458#if defined(ENABLE_KQUEUE)
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200459 else if (*flag == 'd' && flag[1] == 'k')
Willy Tarreau43b78992009-01-25 15:42:27 +0100460 global.tune.options &= ~GTUNE_USE_KQUEUE;
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200461#endif
Willy Tarreau3ab68cf2009-01-25 16:03:28 +0100462#if defined(CONFIG_HAP_LINUX_SPLICE) || defined(CONFIG_HAP_TCPSPLICE)
463 else if (*flag == 'd' && flag[1] == 'S')
464 global.tune.options &= ~GTUNE_USE_SPLICE;
465#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200466 else if (*flag == 'V')
467 arg_mode |= MODE_VERBOSE;
468 else if (*flag == 'd' && flag[1] == 'b')
469 arg_mode |= MODE_FOREGROUND;
470 else if (*flag == 'd')
471 arg_mode |= MODE_DEBUG;
472 else if (*flag == 'c')
473 arg_mode |= MODE_CHECK;
474 else if (*flag == 'D')
Willy Tarreau6bde87b2009-05-18 16:29:51 +0200475 arg_mode |= MODE_DAEMON;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200476 else if (*flag == 'q')
477 arg_mode |= MODE_QUIET;
478 else if (*flag == 's' && (flag[1] == 'f' || flag[1] == 't')) {
479 /* list of pids to finish ('f') or terminate ('t') */
480
481 if (flag[1] == 'f')
482 oldpids_sig = SIGUSR1; /* finish then exit */
483 else
484 oldpids_sig = SIGTERM; /* terminate immediately */
485 argv++; argc--;
486
487 if (argc > 0) {
488 oldpids = calloc(argc, sizeof(int));
489 while (argc > 0) {
490 oldpids[nb_oldpids] = atol(*argv);
491 if (oldpids[nb_oldpids] <= 0)
492 usage(old_argv);
493 argc--; argv++;
494 nb_oldpids++;
495 }
496 }
497 }
498 else { /* >=2 args */
499 argv++; argc--;
500 if (argc == 0)
501 usage(old_argv);
502
503 switch (*flag) {
504 case 'n' : cfg_maxconn = atol(*argv); break;
505 case 'm' : global.rlimit_memmax = atol(*argv); break;
506 case 'N' : cfg_maxpconn = atol(*argv); break;
Willy Tarreau5d01a632009-06-22 16:02:30 +0200507 case 'f' :
508 if (cfg_nbcfgfiles > MAX_CFG_FILES) {
509 Alert("Cannot load configuration file %s : too many configuration files (max %d).\n",
510 *argv, MAX_CFG_FILES);
511 exit(1);
512 }
513 cfg_cfgfile[cfg_nbcfgfiles++] = *argv;
514 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200515 case 'p' : cfg_pidfile = *argv; break;
516 default: usage(old_argv);
517 }
518 }
519 }
520 else
521 usage(old_argv);
522 argv++; argc--;
523 }
524
525 global.mode = MODE_STARTING | /* during startup, we want most of the alerts */
526 (arg_mode & (MODE_DAEMON | MODE_FOREGROUND | MODE_VERBOSE
527 | MODE_QUIET | MODE_CHECK | MODE_DEBUG));
528
Willy Tarreau5d01a632009-06-22 16:02:30 +0200529 if (!cfg_nbcfgfiles)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200530 usage(old_argv);
531
532 gethostname(hostname, MAX_HOSTNAME_LEN);
533
534 have_appsession = 0;
535 global.maxsock = 10; /* reserve 10 fds ; will be incremented by socket eaters */
Willy Tarreau915e1eb2009-06-22 15:48:36 +0200536
537 init_default_instance();
538
Willy Tarreau5d01a632009-06-22 16:02:30 +0200539 for (i = 0; i < cfg_nbcfgfiles; i++) {
Willy Tarreau058e9072009-07-20 09:30:05 +0200540 err_code |= readcfgfile(cfg_cfgfile[i]);
541 if (err_code & (ERR_ABORT|ERR_FATAL))
542 Alert("Error(s) found in configuration file : %s\n", cfg_cfgfile[i]);
543 if (err_code & ERR_ABORT)
Willy Tarreau5d01a632009-06-22 16:02:30 +0200544 exit(1);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200545 }
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +0200546
Willy Tarreaubb925012009-07-23 13:36:36 +0200547 err_code |= check_config_validity();
548 if (err_code & (ERR_ABORT|ERR_FATAL)) {
549 Alert("Fatal errors found in configuration.\n");
Willy Tarreau915e1eb2009-06-22 15:48:36 +0200550 exit(1);
551 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200552
553 if (global.mode & MODE_CHECK) {
Willy Tarreau915e1eb2009-06-22 15:48:36 +0200554 qfprintf(stdout, "Configuration file is valid\n");
Willy Tarreaubaaee002006-06-26 02:48:02 +0200555 exit(0);
556 }
557
Willy Tarreau915e1eb2009-06-22 15:48:36 +0200558 if (have_appsession)
559 appsession_init();
560
Krzysztof Oledzkib304dc72007-10-14 23:40:01 +0200561 if (start_checks() < 0)
562 exit(1);
563
Willy Tarreaubaaee002006-06-26 02:48:02 +0200564 if (cfg_maxconn > 0)
565 global.maxconn = cfg_maxconn;
566
567 if (cfg_pidfile) {
Willy Tarreaua534fea2008-08-03 12:19:50 +0200568 free(global.pidfile);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200569 global.pidfile = strdup(cfg_pidfile);
570 }
571
572 if (global.maxconn == 0)
573 global.maxconn = DEFAULT_MAXCONN;
574
Willy Tarreau66aa61f2009-01-18 21:44:07 +0100575 if (!global.maxpipes) {
576 /* maxpipes not specified. Count how many frontends and backends
577 * may be using splicing, and bound that to maxconn.
578 */
579 struct proxy *cur;
580 int nbfe = 0, nbbe = 0;
581
582 for (cur = proxy; cur; cur = cur->next) {
583 if (cur->options2 & (PR_O2_SPLIC_ANY)) {
584 if (cur->cap & PR_CAP_FE)
585 nbfe += cur->maxconn;
586 if (cur->cap & PR_CAP_BE)
Willy Tarreauafb48762009-01-25 10:42:05 +0100587 nbbe += cur->fullconn ? cur->fullconn : global.maxconn;
Willy Tarreau66aa61f2009-01-18 21:44:07 +0100588 }
589 }
590 global.maxpipes = MAX(nbfe, nbbe);
591 if (global.maxpipes > global.maxconn)
592 global.maxpipes = global.maxconn;
Willy Tarreau686ac822009-01-25 14:06:58 +0100593 global.maxpipes /= 4;
Willy Tarreau66aa61f2009-01-18 21:44:07 +0100594 }
595
596
Willy Tarreaubaaee002006-06-26 02:48:02 +0200597 global.maxsock += global.maxconn * 2; /* each connection needs two sockets */
Willy Tarreau3ec79b92009-01-18 20:39:42 +0100598 global.maxsock += global.maxpipes * 2; /* each pipe needs two FDs */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200599
Willy Tarreau1db37712007-06-03 17:16:49 +0200600 if (global.tune.maxpollevents <= 0)
601 global.tune.maxpollevents = MAX_POLL_EVENTS;
602
Willy Tarreauf49d1df2009-03-01 08:35:41 +0100603 if (global.tune.maxaccept == 0) {
Willy Tarreaua0250ba2008-01-06 11:22:57 +0100604 if (global.nbproc > 1)
605 global.tune.maxaccept = 8; /* leave some conns to other processes */
606 else
Willy Tarreauf49d1df2009-03-01 08:35:41 +0100607 global.tune.maxaccept = 100; /* accept many incoming conns at once */
Willy Tarreaua0250ba2008-01-06 11:22:57 +0100608 }
609
Willy Tarreau6f4a82c2009-03-21 20:43:57 +0100610 if (global.tune.recv_enough == 0)
611 global.tune.recv_enough = MIN_RECV_AT_ONCE_ENOUGH;
612
Willy Tarreaubaaee002006-06-26 02:48:02 +0200613 if (arg_mode & (MODE_DEBUG | MODE_FOREGROUND)) {
614 /* command line debug mode inhibits configuration mode */
615 global.mode &= ~(MODE_DAEMON | MODE_QUIET);
616 }
617 global.mode |= (arg_mode & (MODE_DAEMON | MODE_FOREGROUND | MODE_QUIET |
Willy Tarreaufbee7132007-10-18 13:53:22 +0200618 MODE_VERBOSE | MODE_DEBUG ));
Willy Tarreaubaaee002006-06-26 02:48:02 +0200619
620 if ((global.mode & MODE_DEBUG) && (global.mode & (MODE_DAEMON | MODE_QUIET))) {
621 Warning("<debug> mode incompatible with <quiet> and <daemon>. Keeping <debug> only.\n");
622 global.mode &= ~(MODE_DAEMON | MODE_QUIET);
623 }
624
625 if ((global.nbproc > 1) && !(global.mode & MODE_DAEMON)) {
626 if (!(global.mode & (MODE_FOREGROUND | MODE_DEBUG)))
627 Warning("<nbproc> is only meaningful in daemon mode. Setting limit to 1 process.\n");
628 global.nbproc = 1;
629 }
630
631 if (global.nbproc < 1)
632 global.nbproc = 1;
633
Willy Tarreaubaaee002006-06-26 02:48:02 +0200634 fdtab = (struct fdtab *)calloc(1,
635 sizeof(struct fdtab) * (global.maxsock));
636 for (i = 0; i < global.maxsock; i++) {
637 fdtab[i].state = FD_STCLOSE;
638 }
Willy Tarreau4f60f162007-04-08 16:39:58 +0200639
Willy Tarreauef1d1f82007-04-16 00:25:25 +0200640 /*
641 * Note: we could register external pollers here.
642 * Built-in pollers have been registered before main().
643 */
Willy Tarreau4f60f162007-04-08 16:39:58 +0200644
Willy Tarreau43b78992009-01-25 15:42:27 +0100645 if (!(global.tune.options & GTUNE_USE_KQUEUE))
Willy Tarreau1e63130a2007-04-09 12:03:06 +0200646 disable_poller("kqueue");
647
Willy Tarreau43b78992009-01-25 15:42:27 +0100648 if (!(global.tune.options & GTUNE_USE_EPOLL))
Willy Tarreau4f60f162007-04-08 16:39:58 +0200649 disable_poller("epoll");
650
Willy Tarreau43b78992009-01-25 15:42:27 +0100651 if (!(global.tune.options & GTUNE_USE_SEPOLL))
Willy Tarreaude99e992007-04-16 00:53:59 +0200652 disable_poller("sepoll");
653
Willy Tarreau43b78992009-01-25 15:42:27 +0100654 if (!(global.tune.options & GTUNE_USE_POLL))
Willy Tarreau4f60f162007-04-08 16:39:58 +0200655 disable_poller("poll");
656
Willy Tarreau43b78992009-01-25 15:42:27 +0100657 if (!(global.tune.options & GTUNE_USE_SELECT))
Willy Tarreau4f60f162007-04-08 16:39:58 +0200658 disable_poller("select");
659
660 /* Note: we could disable any poller by name here */
661
Willy Tarreau2ff76222007-04-09 19:29:56 +0200662 if (global.mode & (MODE_VERBOSE|MODE_DEBUG))
663 list_pollers(stderr);
664
Willy Tarreau4f60f162007-04-08 16:39:58 +0200665 if (!init_pollers()) {
Willy Tarreau2ff76222007-04-09 19:29:56 +0200666 Alert("No polling mechanism available.\n");
Willy Tarreau4f60f162007-04-08 16:39:58 +0200667 exit(1);
668 }
Willy Tarreau2ff76222007-04-09 19:29:56 +0200669 if (global.mode & (MODE_VERBOSE|MODE_DEBUG)) {
670 printf("Using %s() as the polling mechanism.\n", cur_poller.name);
Willy Tarreau4f60f162007-04-08 16:39:58 +0200671 }
672
Willy Tarreaubaaee002006-06-26 02:48:02 +0200673}
674
675void deinit(void)
676{
Willy Tarreau4d2d0982007-05-14 00:39:29 +0200677 struct proxy *p = proxy, *p0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200678 struct cap_hdr *h,*h_next;
679 struct server *s,*s_next;
680 struct listener *l,*l_next;
Willy Tarreau0fc45a72007-06-17 00:36:03 +0200681 struct acl_cond *cond, *condb;
682 struct hdr_exp *exp, *expb;
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +0200683 struct acl *acl, *aclb;
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200684 struct switching_rule *rule, *ruleb;
Willy Tarreaub463dfb2008-06-07 23:08:56 +0200685 struct redirect_rule *rdr, *rdrb;
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200686 struct uri_auth *uap, *ua = NULL;
687 struct user_auth *user;
Willy Tarreau0fc45a72007-06-17 00:36:03 +0200688 int i;
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200689
Willy Tarreaubaaee002006-06-26 02:48:02 +0200690 while (p) {
Willy Tarreaua534fea2008-08-03 12:19:50 +0200691 free(p->id);
692 free(p->check_req);
693 free(p->cookie_name);
694 free(p->cookie_domain);
695 free(p->url_param_name);
696 free(p->capture_name);
697 free(p->monitor_uri);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200698
Willy Tarreaua534fea2008-08-03 12:19:50 +0200699 for (i = 0; i < HTTP_ERR_SIZE; i++)
700 free(p->errmsg[i].str);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200701
Willy Tarreaua534fea2008-08-03 12:19:50 +0200702 for (i = 0; i < p->nb_reqadd; i++)
703 free(p->req_add[i]);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200704
Willy Tarreaua534fea2008-08-03 12:19:50 +0200705 for (i = 0; i < p->nb_rspadd; i++)
706 free(p->rsp_add[i]);
Willy Tarreau0fc45a72007-06-17 00:36:03 +0200707
708 list_for_each_entry_safe(cond, condb, &p->block_cond, list) {
709 LIST_DEL(&cond->list);
710 prune_acl_cond(cond);
711 free(cond);
712 }
713
Willy Tarreaub80c2302007-11-30 20:51:32 +0100714 list_for_each_entry_safe(cond, condb, &p->mon_fail_cond, list) {
715 LIST_DEL(&cond->list);
716 prune_acl_cond(cond);
717 free(cond);
718 }
719
Willy Tarreau0fc45a72007-06-17 00:36:03 +0200720 for (exp = p->req_exp; exp != NULL; ) {
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200721 if (exp->preg) {
Willy Tarreau0fc45a72007-06-17 00:36:03 +0200722 regfree((regex_t *)exp->preg);
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200723 free((regex_t *)exp->preg);
724 }
725
Willy Tarreau0fc45a72007-06-17 00:36:03 +0200726 if (exp->replace && exp->action != ACT_SETBE)
727 free((char *)exp->replace);
728 expb = exp;
729 exp = exp->next;
730 free(expb);
731 }
732
733 for (exp = p->rsp_exp; exp != NULL; ) {
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200734 if (exp->preg) {
Willy Tarreau0fc45a72007-06-17 00:36:03 +0200735 regfree((regex_t *)exp->preg);
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200736 free((regex_t *)exp->preg);
737 }
738
Willy Tarreau0fc45a72007-06-17 00:36:03 +0200739 if (exp->replace && exp->action != ACT_SETBE)
740 free((char *)exp->replace);
741 expb = exp;
742 exp = exp->next;
743 free(expb);
744 }
745
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200746 /* build a list of unique uri_auths */
747 if (!ua)
748 ua = p->uri_auth;
749 else {
750 /* check if p->uri_auth is unique */
751 for (uap = ua; uap; uap=uap->next)
752 if (uap == p->uri_auth)
753 break;
754
Willy Tarreauaccc4e12008-06-24 11:14:45 +0200755 if (!uap && p->uri_auth) {
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200756 /* add it, if it is */
757 p->uri_auth->next = ua;
758 ua = p->uri_auth;
759 }
760 }
Willy Tarreau0fc45a72007-06-17 00:36:03 +0200761
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +0200762 list_for_each_entry_safe(acl, aclb, &p->acl, list) {
763 LIST_DEL(&acl->list);
764 prune_acl(acl);
765 free(acl);
766 }
767
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200768 list_for_each_entry_safe(rule, ruleb, &p->switching_rules, list) {
769 LIST_DEL(&rule->list);
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200770 prune_acl_cond(rule->cond);
771 free(rule->cond);
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200772 free(rule);
773 }
774
Willy Tarreaub463dfb2008-06-07 23:08:56 +0200775 list_for_each_entry_safe(rdr, rdrb, &p->redirect_rules, list) {
776 LIST_DEL(&rdr->list);
777 prune_acl_cond(rdr->cond);
778 free(rdr->cond);
779 free(rdr->rdr_str);
780 free(rdr);
781 }
782
Willy Tarreaua534fea2008-08-03 12:19:50 +0200783 free(p->appsession_name);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200784
785 h = p->req_cap;
786 while (h) {
787 h_next = h->next;
Willy Tarreaua534fea2008-08-03 12:19:50 +0200788 free(h->name);
Willy Tarreaucf7f3202007-05-13 22:46:04 +0200789 pool_destroy2(h->pool);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200790 free(h);
791 h = h_next;
792 }/* end while(h) */
793
794 h = p->rsp_cap;
795 while (h) {
796 h_next = h->next;
Willy Tarreaua534fea2008-08-03 12:19:50 +0200797 free(h->name);
Willy Tarreaucf7f3202007-05-13 22:46:04 +0200798 pool_destroy2(h->pool);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200799 free(h);
800 h = h_next;
801 }/* end while(h) */
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +0200802
Willy Tarreaubaaee002006-06-26 02:48:02 +0200803 s = p->srv;
804 while (s) {
805 s_next = s->next;
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +0200806
807 if (s->check) {
808 task_delete(s->check);
809 task_free(s->check);
810 }
811
Willy Tarreaua534fea2008-08-03 12:19:50 +0200812 free(s->id);
813 free(s->cookie);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200814 free(s);
815 s = s_next;
816 }/* end while(s) */
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +0200817
Willy Tarreaubaaee002006-06-26 02:48:02 +0200818 l = p->listen;
819 while (l) {
820 l_next = l->next;
821 free(l);
822 l = l_next;
823 }/* end while(l) */
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +0200824
Willy Tarreaucf7f3202007-05-13 22:46:04 +0200825 pool_destroy2(p->req_cap_pool);
826 pool_destroy2(p->rsp_cap_pool);
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +0200827 pool_destroy2(p->hdr_idx_pool);
Willy Tarreau4d2d0982007-05-14 00:39:29 +0200828 p0 = p;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200829 p = p->next;
Willy Tarreau4d2d0982007-05-14 00:39:29 +0200830 free(p0);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200831 }/* end while(p) */
Willy Tarreaudd815982007-10-16 12:25:14 +0200832
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200833 while (ua) {
834 uap = ua;
835 ua = ua->next;
836
Willy Tarreaua534fea2008-08-03 12:19:50 +0200837 free(uap->uri_prefix);
838 free(uap->auth_realm);
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200839
840 while (uap->users) {
841 user = uap->users;
842 uap->users = uap->users->next;
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200843 free(user->user_pwd);
844 free(user);
845 }
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200846 free(uap);
847 }
848
Willy Tarreaudd815982007-10-16 12:25:14 +0200849 protocol_unbind_all();
850
Willy Tarreaua534fea2008-08-03 12:19:50 +0200851 free(global.chroot); global.chroot = NULL;
852 free(global.pidfile); global.pidfile = NULL;
853 free(fdtab); fdtab = NULL;
854 free(oldpids); oldpids = NULL;
Krzysztof Piotr Oledzki8001d612008-05-31 13:53:23 +0200855
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200856 pool_destroy2(pool2_session);
Willy Tarreau7341d942007-05-13 19:56:02 +0200857 pool_destroy2(pool2_buffer);
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200858 pool_destroy2(pool2_requri);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200859 pool_destroy2(pool2_task);
Willy Tarreau086b3b42007-05-13 21:45:51 +0200860 pool_destroy2(pool2_capture);
Willy Tarreau63963c62007-05-13 21:29:55 +0200861 pool_destroy2(pool2_appsess);
Willy Tarreaue4d7e552007-05-13 20:19:55 +0200862 pool_destroy2(pool2_pendconn);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200863
864 if (have_appsession) {
Willy Tarreau63963c62007-05-13 21:29:55 +0200865 pool_destroy2(apools.serverid);
866 pool_destroy2(apools.sessid);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200867 }
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +0200868
869 deinit_pollers();
870
Willy Tarreaubaaee002006-06-26 02:48:02 +0200871} /* end deinit() */
872
873/* sends the signal <sig> to all pids found in <oldpids> */
874static void tell_old_pids(int sig)
875{
876 int p;
877 for (p = 0; p < nb_oldpids; p++)
878 kill(oldpids[p], sig);
879}
880
Willy Tarreau4f60f162007-04-08 16:39:58 +0200881/*
882 * Runs the polling loop
883 *
884 * FIXME:
885 * - we still use 'listeners' to check whether we want to stop or not.
886 *
887 */
888void run_poll_loop()
889{
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200890 int next;
Willy Tarreau4f60f162007-04-08 16:39:58 +0200891
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200892 tv_update_date(0,1);
Willy Tarreau4f60f162007-04-08 16:39:58 +0200893 while (1) {
Willy Tarreau29857942009-05-10 09:01:21 +0200894 /* check if we caught some signals and process them */
895 signal_process_queue();
896
Willy Tarreau58b458d2008-06-29 22:40:23 +0200897 /* Check if we can expire some tasks */
898 wake_expired_tasks(&next);
899
900 /* Process a few tasks */
Willy Tarreaud825eef2007-05-12 22:35:00 +0200901 process_runnable_tasks(&next);
Willy Tarreau4f60f162007-04-08 16:39:58 +0200902
Willy Tarreau58b458d2008-06-29 22:40:23 +0200903 /* maintain all proxies in a consistent state. This should quickly
904 * become a task because it becomes expensive when there are huge
905 * numbers of proxies. */
906 maintain_proxies(&next);
907
Willy Tarreau4f60f162007-04-08 16:39:58 +0200908 /* stop when there's no connection left and we don't allow them anymore */
909 if (!actconn && listeners == 0)
910 break;
911
Willy Tarreau58b458d2008-06-29 22:40:23 +0200912 /* The poller will ensure it returns around <next> */
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200913 cur_poller.poll(&cur_poller, next);
Willy Tarreau4f60f162007-04-08 16:39:58 +0200914 }
915}
916
917
Willy Tarreaubaaee002006-06-26 02:48:02 +0200918int main(int argc, char **argv)
919{
920 int err, retry;
921 struct rlimit limit;
922 FILE *pidfile = NULL;
923 init(argc, argv);
924
Willy Tarreau01b3a532009-05-10 09:59:50 +0200925 signal_register(SIGQUIT, dump);
926 signal_register(SIGUSR1, sig_soft_stop);
927 signal_register(SIGHUP, sig_dump_state);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200928#ifdef DEBUG_MEMORY
Willy Tarreau01b3a532009-05-10 09:59:50 +0200929 signal_register(SIGINT, sig_int);
930 signal_register(SIGTERM, sig_term);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200931#endif
932
933 /* on very high loads, a sigpipe sometimes happen just between the
934 * getsockopt() which tells "it's OK to write", and the following write :-(
935 */
Willy Tarreau5bd8c372009-01-19 00:32:22 +0100936#if !defined(MSG_NOSIGNAL) || defined(CONFIG_HAP_LINUX_SPLICE)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200937 signal(SIGPIPE, SIG_IGN);
938#endif
939
940 /* We will loop at most 100 times with 10 ms delay each time.
941 * That's at most 1 second. We only send a signal to old pids
942 * if we cannot grab at least one port.
943 */
944 retry = MAX_START_RETRIES;
945 err = ERR_NONE;
946 while (retry >= 0) {
947 struct timeval w;
948 err = start_proxies(retry == 0 || nb_oldpids == 0);
Willy Tarreaue13e9252007-12-20 23:05:50 +0100949 /* exit the loop on no error or fatal error */
950 if ((err & (ERR_RETRYABLE|ERR_FATAL)) != ERR_RETRYABLE)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200951 break;
952 if (nb_oldpids == 0)
953 break;
954
955 /* FIXME-20060514: Solaris and OpenBSD do not support shutdown() on
956 * listening sockets. So on those platforms, it would be wiser to
957 * simply send SIGUSR1, which will not be undoable.
958 */
959 tell_old_pids(SIGTTOU);
960 /* give some time to old processes to stop listening */
961 w.tv_sec = 0;
962 w.tv_usec = 10*1000;
963 select(0, NULL, NULL, NULL, &w);
964 retry--;
965 }
966
967 /* Note: start_proxies() sends an alert when it fails. */
Willy Tarreau0a3b9d92009-02-04 17:05:23 +0100968 if ((err & ~ERR_WARN) != ERR_NONE) {
Willy Tarreauf68da462009-06-09 14:36:00 +0200969 if (retry != MAX_START_RETRIES && nb_oldpids) {
970 protocol_unbind_all(); /* cleanup everything we can */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200971 tell_old_pids(SIGTTIN);
Willy Tarreauf68da462009-06-09 14:36:00 +0200972 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200973 exit(1);
974 }
975
976 if (listeners == 0) {
977 Alert("[%s.main()] No enabled listener found (check the <listen> keywords) ! Exiting.\n", argv[0]);
978 /* Note: we don't have to send anything to the old pids because we
979 * never stopped them. */
980 exit(1);
981 }
982
Willy Tarreau0a3b9d92009-02-04 17:05:23 +0100983 if ((protocol_bind_all() & ~ERR_WARN) != ERR_NONE) {
Willy Tarreaudd815982007-10-16 12:25:14 +0200984 Alert("[%s.main()] Some protocols failed to start their listeners! Exiting.\n", argv[0]);
985 protocol_unbind_all(); /* cleanup everything we can */
986 if (nb_oldpids)
987 tell_old_pids(SIGTTIN);
988 exit(1);
989 }
990
Willy Tarreaubaaee002006-06-26 02:48:02 +0200991 /* prepare pause/play signals */
Willy Tarreau01b3a532009-05-10 09:59:50 +0200992 signal_register(SIGTTOU, sig_pause);
993 signal_register(SIGTTIN, sig_listen);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200994
Willy Tarreaubaaee002006-06-26 02:48:02 +0200995 /* MODE_QUIET can inhibit alerts and warnings below this line */
996
997 global.mode &= ~MODE_STARTING;
998 if ((global.mode & MODE_QUIET) && !(global.mode & MODE_VERBOSE)) {
999 /* detach from the tty */
1000 fclose(stdin); fclose(stdout); fclose(stderr);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001001 }
1002
1003 /* open log & pid files before the chroot */
1004 if (global.mode & MODE_DAEMON && global.pidfile != NULL) {
1005 int pidfd;
1006 unlink(global.pidfile);
1007 pidfd = open(global.pidfile, O_CREAT | O_WRONLY | O_TRUNC, 0644);
1008 if (pidfd < 0) {
1009 Alert("[%s.main()] Cannot create pidfile %s\n", argv[0], global.pidfile);
1010 if (nb_oldpids)
1011 tell_old_pids(SIGTTIN);
Willy Tarreaudd815982007-10-16 12:25:14 +02001012 protocol_unbind_all();
Willy Tarreaubaaee002006-06-26 02:48:02 +02001013 exit(1);
1014 }
1015 pidfile = fdopen(pidfd, "w");
1016 }
1017
Willy Tarreaubaaee002006-06-26 02:48:02 +02001018 /* ulimits */
1019 if (!global.rlimit_nofile)
1020 global.rlimit_nofile = global.maxsock;
1021
1022 if (global.rlimit_nofile) {
1023 limit.rlim_cur = limit.rlim_max = global.rlimit_nofile;
1024 if (setrlimit(RLIMIT_NOFILE, &limit) == -1) {
1025 Warning("[%s.main()] Cannot raise FD limit to %d.\n", argv[0], global.rlimit_nofile);
1026 }
1027 }
1028
1029 if (global.rlimit_memmax) {
1030 limit.rlim_cur = limit.rlim_max =
1031 global.rlimit_memmax * 1048576 / global.nbproc;
1032#ifdef RLIMIT_AS
1033 if (setrlimit(RLIMIT_AS, &limit) == -1) {
1034 Warning("[%s.main()] Cannot fix MEM limit to %d megs.\n",
1035 argv[0], global.rlimit_memmax);
1036 }
1037#else
1038 if (setrlimit(RLIMIT_DATA, &limit) == -1) {
1039 Warning("[%s.main()] Cannot fix MEM limit to %d megs.\n",
1040 argv[0], global.rlimit_memmax);
1041 }
1042#endif
1043 }
1044
Willy Tarreau6d1a9882007-01-07 02:03:04 +01001045#ifdef CONFIG_HAP_TCPSPLICE
Willy Tarreau3ab68cf2009-01-25 16:03:28 +01001046 if ((global.tune.options & GTUNE_USE_SPLICE) && (global.last_checks & LSTCHK_TCPSPLICE)) {
Willy Tarreau6d1a9882007-01-07 02:03:04 +01001047 if (tcp_splice_start() < 0) {
1048 Alert("[%s.main()] Cannot enable tcp_splice.\n"
1049 " Make sure you have enough permissions and that the module is loadable.\n"
Willy Tarreau3ab68cf2009-01-25 16:03:28 +01001050 " Alternatively, you may disable the 'tcpsplice' options in the configuration\n"
1051 " or add 'nosplice' in the global section, or start with '-dS'.\n"
Willy Tarreaubc69d8b2009-07-26 20:40:05 +02001052 "", argv[0]);
Willy Tarreaudd815982007-10-16 12:25:14 +02001053 protocol_unbind_all();
Willy Tarreau6d1a9882007-01-07 02:03:04 +01001054 exit(1);
1055 }
1056 }
1057#endif
1058
Willy Tarreaub38651a2007-03-24 17:24:39 +01001059#ifdef CONFIG_HAP_CTTPROXY
1060 if (global.last_checks & LSTCHK_CTTPROXY) {
1061 int ret;
1062
1063 ret = check_cttproxy_version();
1064 if (ret < 0) {
1065 Alert("[%s.main()] Cannot enable cttproxy.\n%s",
1066 argv[0],
1067 (ret == -1) ? " Incorrect module version.\n"
1068 : " Make sure you have enough permissions and that the module is loaded.\n");
Willy Tarreaudd815982007-10-16 12:25:14 +02001069 protocol_unbind_all();
Willy Tarreaub38651a2007-03-24 17:24:39 +01001070 exit(1);
1071 }
1072 }
1073#endif
1074
1075 if ((global.last_checks & LSTCHK_NETADM) && global.uid) {
1076 Alert("[%s.main()] Some configuration options require full privileges, so global.uid cannot be changed.\n"
Willy Tarreau4e30ed72009-02-04 18:02:48 +01001077 "", argv[0]);
Willy Tarreaudd815982007-10-16 12:25:14 +02001078 protocol_unbind_all();
Willy Tarreaub38651a2007-03-24 17:24:39 +01001079 exit(1);
1080 }
1081
Willy Tarreau4e30ed72009-02-04 18:02:48 +01001082 /* If the user is not root, we'll still let him try the configuration
1083 * but we inform him that unexpected behaviour may occur.
1084 */
1085 if ((global.last_checks & LSTCHK_NETADM) && getuid())
1086 Warning("[%s.main()] Some options which require full privileges"
1087 " might not work well.\n"
1088 "", argv[0]);
1089
Willy Tarreauf223cc02007-10-15 18:57:08 +02001090 /* chroot if needed */
1091 if (global.chroot != NULL) {
1092 if (chroot(global.chroot) == -1) {
1093 Alert("[%s.main()] Cannot chroot(%s).\n", argv[0], global.chroot);
1094 if (nb_oldpids)
1095 tell_old_pids(SIGTTIN);
Willy Tarreaudd815982007-10-16 12:25:14 +02001096 protocol_unbind_all();
Willy Tarreauf223cc02007-10-15 18:57:08 +02001097 exit(1);
1098 }
1099 chdir("/");
1100 }
1101
Willy Tarreaubaaee002006-06-26 02:48:02 +02001102 if (nb_oldpids)
1103 tell_old_pids(oldpids_sig);
1104
1105 /* Note that any error at this stage will be fatal because we will not
1106 * be able to restart the old pids.
1107 */
1108
1109 /* setgid / setuid */
1110 if (global.gid && setgid(global.gid) == -1) {
1111 Alert("[%s.main()] Cannot set gid %d.\n", argv[0], global.gid);
Willy Tarreaudd815982007-10-16 12:25:14 +02001112 protocol_unbind_all();
Willy Tarreaubaaee002006-06-26 02:48:02 +02001113 exit(1);
1114 }
1115
1116 if (global.uid && setuid(global.uid) == -1) {
1117 Alert("[%s.main()] Cannot set uid %d.\n", argv[0], global.uid);
Willy Tarreaudd815982007-10-16 12:25:14 +02001118 protocol_unbind_all();
Willy Tarreaubaaee002006-06-26 02:48:02 +02001119 exit(1);
1120 }
1121
1122 /* check ulimits */
1123 limit.rlim_cur = limit.rlim_max = 0;
1124 getrlimit(RLIMIT_NOFILE, &limit);
1125 if (limit.rlim_cur < global.maxsock) {
1126 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",
Willy Tarreau1772ece2009-04-03 14:49:12 +02001127 argv[0], (int)limit.rlim_cur, global.maxconn, global.maxsock, global.maxsock);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001128 }
1129
1130 if (global.mode & MODE_DAEMON) {
Willy Tarreau0b9c02c2009-02-04 22:05:05 +01001131 struct proxy *px;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001132 int ret = 0;
1133 int proc;
1134
1135 /* the father launches the required number of processes */
1136 for (proc = 0; proc < global.nbproc; proc++) {
1137 ret = fork();
1138 if (ret < 0) {
1139 Alert("[%s.main()] Cannot fork.\n", argv[0]);
Willy Tarreaudd815982007-10-16 12:25:14 +02001140 protocol_unbind_all();
Willy Tarreaud6803712007-10-16 07:44:56 +02001141 exit(1); /* there has been an error */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001142 }
1143 else if (ret == 0) /* child breaks here */
1144 break;
1145 if (pidfile != NULL) {
1146 fprintf(pidfile, "%d\n", ret);
1147 fflush(pidfile);
1148 }
Willy Tarreaudcd47712007-11-04 23:35:08 +01001149 relative_pid++; /* each child will get a different one */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001150 }
1151 /* close the pidfile both in children and father */
1152 if (pidfile != NULL)
1153 fclose(pidfile);
1154 free(global.pidfile);
Krzysztof Oledzki56f1e8b2007-10-11 18:30:14 +02001155 global.pidfile = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001156
Willy Tarreau0b9c02c2009-02-04 22:05:05 +01001157 /* we might have to unbind some proxies from some processes */
1158 px = proxy;
1159 while (px != NULL) {
1160 if (px->bind_proc && px->state != PR_STSTOPPED) {
1161 if (!(px->bind_proc & (1 << proc)))
1162 stop_proxy(px);
1163 }
1164 px = px->next;
1165 }
1166
Willy Tarreaubaaee002006-06-26 02:48:02 +02001167 if (proc == global.nbproc)
1168 exit(0); /* parent must leave */
1169
1170 /* if we're NOT in QUIET mode, we should now close the 3 first FDs to ensure
1171 * that we can detach from the TTY. We MUST NOT do it in other cases since
1172 * it would have already be done, and 0-2 would have been affected to listening
1173 * sockets
1174 */
Willy Tarreau106cb762008-11-16 07:40:34 +01001175 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02001176 /* detach from the tty */
1177 fclose(stdin); fclose(stdout); fclose(stderr);
Willy Tarreau106cb762008-11-16 07:40:34 +01001178 global.mode &= ~MODE_VERBOSE;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001179 global.mode |= MODE_QUIET; /* ensure that we won't say anything from now */
1180 }
1181 pid = getpid(); /* update child's pid */
1182 setsid();
Willy Tarreau2ff76222007-04-09 19:29:56 +02001183 fork_poller();
Willy Tarreaubaaee002006-06-26 02:48:02 +02001184 }
1185
Willy Tarreaudd815982007-10-16 12:25:14 +02001186 protocol_enable_all();
Willy Tarreau4f60f162007-04-08 16:39:58 +02001187 /*
1188 * That's it : the central polling loop. Run until we stop.
1189 */
1190 run_poll_loop();
Willy Tarreaubaaee002006-06-26 02:48:02 +02001191
1192 /* Free all Hash Keys and all Hash elements */
1193 appsession_cleanup();
1194 /* Do some cleanup */
1195 deinit();
1196
1197 exit(0);
1198}
1199
1200
1201/*
1202 * Local variables:
1203 * c-indent-level: 8
1204 * c-basic-offset: 8
1205 * End:
1206 */