Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | * HA-Proxy : High Availability-enabled HTTP/TCP proxy |
Willy Tarreau | 49e1ee8 | 2007-01-22 00:56:46 +0100 | [diff] [blame] | 3 | * Copyright 2000-2007 Willy Tarreau <w@1wt.eu>. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4 | * |
| 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 Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 61 | #include <common/appsession.h> |
| 62 | #include <common/base64.h> |
| 63 | #include <common/cfgparse.h> |
| 64 | #include <common/compat.h> |
| 65 | #include <common/config.h> |
| 66 | #include <common/defaults.h> |
| 67 | #include <common/memory.h> |
| 68 | #include <common/mini-clist.h> |
| 69 | #include <common/regex.h> |
| 70 | #include <common/standard.h> |
| 71 | #include <common/time.h> |
| 72 | #include <common/uri_auth.h> |
| 73 | #include <common/version.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 74 | |
| 75 | #include <types/capture.h> |
| 76 | #include <types/global.h> |
| 77 | #include <types/httperr.h> |
Willy Tarreau | 69801b8 | 2007-04-09 15:28:51 +0200 | [diff] [blame] | 78 | #include <types/polling.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 79 | #include <types/proto_http.h> |
| 80 | |
| 81 | #include <proto/backend.h> |
| 82 | #include <proto/buffers.h> |
| 83 | #include <proto/client.h> |
| 84 | #include <proto/fd.h> |
| 85 | #include <proto/log.h> |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 86 | #include <proto/proto_http.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 87 | #include <proto/proxy.h> |
| 88 | #include <proto/queue.h> |
| 89 | #include <proto/server.h> |
Willy Tarreau | c6ca1a0 | 2007-05-13 19:43:47 +0200 | [diff] [blame] | 90 | #include <proto/session.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 91 | #include <proto/stream_sock.h> |
| 92 | #include <proto/task.h> |
| 93 | |
Willy Tarreau | 6d1a988 | 2007-01-07 02:03:04 +0100 | [diff] [blame] | 94 | #ifdef CONFIG_HAP_TCPSPLICE |
| 95 | #include <libtcpsplice.h> |
| 96 | #endif |
| 97 | |
Willy Tarreau | b38651a | 2007-03-24 17:24:39 +0100 | [diff] [blame] | 98 | #ifdef CONFIG_HAP_CTTPROXY |
| 99 | #include <proto/cttproxy.h> |
| 100 | #endif |
| 101 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 102 | /*********************************************************************/ |
| 103 | |
| 104 | /*********************************************************************/ |
| 105 | |
| 106 | char *cfg_cfgfile = NULL; /* configuration file */ |
| 107 | char *progname = NULL; /* program name */ |
| 108 | int pid; /* current process id */ |
| 109 | |
| 110 | /* global options */ |
| 111 | struct global global = { |
| 112 | logfac1 : -1, |
| 113 | logfac2 : -1, |
| 114 | loglev1 : 7, /* max syslog level : debug */ |
| 115 | loglev2 : 7, |
| 116 | /* others NULL OK */ |
| 117 | }; |
| 118 | |
| 119 | /*********************************************************************/ |
| 120 | |
| 121 | int stopping; /* non zero means stopping in progress */ |
| 122 | |
| 123 | /* Here we store informations about the pids of the processes we may pause |
| 124 | * or kill. We will send them a signal every 10 ms until we can bind to all |
| 125 | * our ports. With 200 retries, that's about 2 seconds. |
| 126 | */ |
| 127 | #define MAX_START_RETRIES 200 |
| 128 | static int nb_oldpids = 0; |
| 129 | static int *oldpids = NULL; |
| 130 | static int oldpids_sig; /* use USR1 or TERM */ |
| 131 | |
| 132 | /* this is used to drain data, and as a temporary buffer for sprintf()... */ |
| 133 | char trash[BUFSIZE]; |
| 134 | |
| 135 | const int zero = 0; |
| 136 | const int one = 1; |
| 137 | |
| 138 | /* |
| 139 | * Syslog facilities and levels. Conforming to RFC3164. |
| 140 | */ |
| 141 | |
| 142 | #define MAX_HOSTNAME_LEN 32 |
| 143 | static char hostname[MAX_HOSTNAME_LEN] = ""; |
| 144 | |
| 145 | |
| 146 | /*********************************************************************/ |
| 147 | /* general purpose functions ***************************************/ |
| 148 | /*********************************************************************/ |
| 149 | |
| 150 | void display_version() |
| 151 | { |
| 152 | printf("HA-Proxy version " HAPROXY_VERSION " " HAPROXY_DATE"\n"); |
Willy Tarreau | 49e1ee8 | 2007-01-22 00:56:46 +0100 | [diff] [blame] | 153 | printf("Copyright 2000-2007 Willy Tarreau <w@1wt.eu>\n\n"); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | /* |
| 157 | * This function prints the command line usage and exits |
| 158 | */ |
| 159 | void usage(char *name) |
| 160 | { |
| 161 | display_version(); |
| 162 | fprintf(stderr, |
| 163 | "Usage : %s -f <cfgfile> [ -vdV" |
| 164 | "D ] [ -n <maxconn> ] [ -N <maxpconn> ]\n" |
| 165 | " [ -p <pidfile> ] [ -m <max megs> ]\n" |
| 166 | " -v displays version\n" |
| 167 | " -d enters debug mode ; -db only disables background mode.\n" |
| 168 | " -V enters verbose mode (disables quiet mode)\n" |
| 169 | " -D goes daemon ; implies -q\n" |
| 170 | " -q quiet mode : don't display messages\n" |
| 171 | " -c check mode : only check config file and exit\n" |
| 172 | " -n sets the maximum total # of connections (%d)\n" |
| 173 | " -m limits the usable amount of memory (in MB)\n" |
| 174 | " -N sets the default, per-proxy maximum # of connections (%d)\n" |
| 175 | " -p writes pids of all children to this file\n" |
| 176 | #if defined(ENABLE_EPOLL) |
| 177 | " -de disables epoll() usage even when available\n" |
| 178 | #endif |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 179 | #if defined(ENABLE_SEPOLL) |
| 180 | " -ds disables speculative epoll() usage even when available\n" |
| 181 | #endif |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 182 | #if defined(ENABLE_KQUEUE) |
| 183 | " -dk disables kqueue() usage even when available\n" |
| 184 | #endif |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 185 | #if defined(ENABLE_POLL) |
| 186 | " -dp disables poll() usage even when available\n" |
| 187 | #endif |
| 188 | " -sf/-st [pid ]* finishes/terminates old pids. Must be last arguments.\n" |
| 189 | "\n", |
| 190 | name, DEFAULT_MAXCONN, cfg_maxpconn); |
| 191 | exit(1); |
| 192 | } |
| 193 | |
| 194 | |
| 195 | |
| 196 | /*********************************************************************/ |
| 197 | /* more specific functions ***************************************/ |
| 198 | /*********************************************************************/ |
| 199 | |
| 200 | /* |
| 201 | * upon SIGUSR1, let's have a soft stop. |
| 202 | */ |
| 203 | void sig_soft_stop(int sig) |
| 204 | { |
| 205 | soft_stop(); |
| 206 | signal(sig, SIG_IGN); |
| 207 | } |
| 208 | |
| 209 | /* |
| 210 | * upon SIGTTOU, we pause everything |
| 211 | */ |
| 212 | void sig_pause(int sig) |
| 213 | { |
| 214 | pause_proxies(); |
| 215 | signal(sig, sig_pause); |
| 216 | } |
| 217 | |
| 218 | /* |
| 219 | * upon SIGTTIN, let's have a soft stop. |
| 220 | */ |
| 221 | void sig_listen(int sig) |
| 222 | { |
| 223 | listen_proxies(); |
| 224 | signal(sig, sig_listen); |
| 225 | } |
| 226 | |
| 227 | /* |
| 228 | * this function dumps every server's state when the process receives SIGHUP. |
| 229 | */ |
| 230 | void sig_dump_state(int sig) |
| 231 | { |
| 232 | struct proxy *p = proxy; |
| 233 | |
| 234 | Warning("SIGHUP received, dumping servers states.\n"); |
| 235 | while (p) { |
| 236 | struct server *s = p->srv; |
| 237 | |
| 238 | send_log(p, LOG_NOTICE, "SIGHUP received, dumping servers states for proxy %s.\n", p->id); |
| 239 | while (s) { |
| 240 | snprintf(trash, sizeof(trash), |
| 241 | "SIGHUP: Server %s/%s is %s. Conn: %d act, %d pend, %d tot.", |
| 242 | p->id, s->id, |
| 243 | (s->state & SRV_RUNNING) ? "UP" : "DOWN", |
| 244 | s->cur_sess, s->nbpend, s->cum_sess); |
| 245 | Warning("%s\n", trash); |
| 246 | send_log(p, LOG_NOTICE, "%s\n", trash); |
| 247 | s = s->next; |
| 248 | } |
| 249 | |
| 250 | if (p->srv_act == 0) { |
| 251 | snprintf(trash, sizeof(trash), |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 252 | "SIGHUP: Proxy %s %s ! Conn: act(FE+BE): %d+%d, %d pend (%d unass), tot(FE+BE): %d+%d.", |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 253 | p->id, |
| 254 | (p->srv_bck) ? "is running on backup servers" : "has no server available", |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 255 | p->feconn, p->beconn, p->totpend, p->nbpend, p->cum_feconn, p->cum_beconn); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 256 | } else { |
| 257 | snprintf(trash, sizeof(trash), |
| 258 | "SIGHUP: Proxy %s has %d active servers and %d backup servers available." |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 259 | " Conn: act(FE+BE): %d+%d, %d pend (%d unass), tot(FE+BE): %d+%d.", |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 260 | p->id, p->srv_act, p->srv_bck, |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 261 | p->feconn, p->beconn, p->totpend, p->nbpend, p->cum_feconn, p->cum_beconn); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 262 | } |
| 263 | Warning("%s\n", trash); |
| 264 | send_log(p, LOG_NOTICE, "%s\n", trash); |
| 265 | |
| 266 | p = p->next; |
| 267 | } |
| 268 | signal(sig, sig_dump_state); |
| 269 | } |
| 270 | |
| 271 | void dump(int sig) |
| 272 | { |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 273 | #if 0 |
Willy Tarreau | 964c936 | 2007-01-07 00:38:00 +0100 | [diff] [blame] | 274 | struct task *t; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 275 | struct session *s; |
Willy Tarreau | 964c936 | 2007-01-07 00:38:00 +0100 | [diff] [blame] | 276 | struct rb_node *node; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 277 | |
Willy Tarreau | 964c936 | 2007-01-07 00:38:00 +0100 | [diff] [blame] | 278 | for(node = rb_first(&wait_queue[0]); |
| 279 | node != NULL; node = rb_next(node)) { |
| 280 | t = rb_entry(node, struct task, rb_node); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 281 | s = t->context; |
| 282 | qfprintf(stderr,"[dump] wq: task %p, still %ld ms, " |
| 283 | "cli=%d, srv=%d, cr=%d, cw=%d, sr=%d, sw=%d, " |
| 284 | "req=%d, rep=%d, clifd=%d\n", |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 285 | s, tv_ms_remain(&now, &t->expire), |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 286 | s->cli_state, |
| 287 | s->srv_state, |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 288 | EV_FD_ISSET(s->cli_fd, DIR_RD), |
| 289 | EV_FD_ISSET(s->cli_fd, DIR_WR), |
| 290 | EV_FD_ISSET(s->srv_fd, DIR_RD), |
| 291 | EV_FD_ISSET(s->srv_fd, DIR_WR), |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 292 | s->req->l, s->rep?s->rep->l:0, s->cli_fd |
| 293 | ); |
| 294 | } |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 295 | #endif |
Willy Tarreau | c6ca1a0 | 2007-05-13 19:43:47 +0200 | [diff] [blame] | 296 | /* dump memory usage then free everything possible */ |
| 297 | dump_pools(); |
| 298 | pool_gc2(); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | #ifdef DEBUG_MEMORY |
| 302 | static void fast_stop(void) |
| 303 | { |
| 304 | struct proxy *p; |
| 305 | p = proxy; |
| 306 | while (p) { |
| 307 | p->grace = 0; |
| 308 | p = p->next; |
| 309 | } |
| 310 | soft_stop(); |
| 311 | } |
| 312 | |
| 313 | void sig_int(int sig) |
| 314 | { |
| 315 | /* This would normally be a hard stop, |
| 316 | but we want to be sure about deallocation, |
| 317 | and so on, so we do a soft stop with |
| 318 | 0 GRACE time |
| 319 | */ |
| 320 | fast_stop(); |
| 321 | /* If we are killed twice, we decide to die*/ |
| 322 | signal(sig, SIG_DFL); |
| 323 | } |
| 324 | |
| 325 | void sig_term(int sig) |
| 326 | { |
| 327 | /* This would normally be a hard stop, |
| 328 | but we want to be sure about deallocation, |
| 329 | and so on, so we do a soft stop with |
| 330 | 0 GRACE time |
| 331 | */ |
| 332 | fast_stop(); |
| 333 | /* If we are killed twice, we decide to die*/ |
| 334 | signal(sig, SIG_DFL); |
| 335 | } |
| 336 | #endif |
| 337 | |
| 338 | |
| 339 | /* |
| 340 | * This function initializes all the necessary variables. It only returns |
| 341 | * if everything is OK. If something fails, it exits. |
| 342 | */ |
| 343 | void init(int argc, char **argv) |
| 344 | { |
| 345 | int i; |
| 346 | int arg_mode = 0; /* MODE_DEBUG, ... */ |
| 347 | char *old_argv = *argv; |
| 348 | char *tmp; |
| 349 | char *cfg_pidfile = NULL; |
| 350 | |
| 351 | if (1<<INTBITS != sizeof(int)*8) { |
| 352 | fprintf(stderr, |
| 353 | "Error: wrong architecture. Recompile so that sizeof(int)=%d\n", |
| 354 | (int)(sizeof(int)*8)); |
| 355 | exit(1); |
| 356 | } |
| 357 | |
| 358 | /* |
| 359 | * Initialize the previously static variables. |
| 360 | */ |
| 361 | |
| 362 | totalconn = actconn = maxfd = listeners = stopping = 0; |
| 363 | |
| 364 | |
| 365 | #ifdef HAPROXY_MEMMAX |
| 366 | global.rlimit_memmax = HAPROXY_MEMMAX; |
| 367 | #endif |
| 368 | |
| 369 | /* initialize the libc's localtime structures once for all so that we |
| 370 | * won't be missing memory if we want to send alerts under OOM conditions. |
Willy Tarreau | 2b35c95 | 2006-10-15 15:25:48 +0200 | [diff] [blame] | 371 | * Also, the Alert() and Warning() functions need <now> to be initialized. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 372 | */ |
| 373 | tv_now(&now); |
Willy Tarreau | bf73613 | 2006-10-15 22:54:47 +0200 | [diff] [blame] | 374 | localtime((time_t *)&now.tv_sec); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 375 | start_date = now; |
| 376 | |
Willy Tarreau | c6ca1a0 | 2007-05-13 19:43:47 +0200 | [diff] [blame] | 377 | init_task(); |
| 378 | init_session(); |
Willy Tarreau | e4d7e55 | 2007-05-13 20:19:55 +0200 | [diff] [blame] | 379 | init_buffer(); |
| 380 | init_pendconn(); |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 381 | init_proto_http(); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 382 | |
| 383 | cfg_polling_mechanism = POLL_USE_SELECT; /* select() is always available */ |
| 384 | #if defined(ENABLE_POLL) |
| 385 | cfg_polling_mechanism |= POLL_USE_POLL; |
| 386 | #endif |
| 387 | #if defined(ENABLE_EPOLL) |
| 388 | cfg_polling_mechanism |= POLL_USE_EPOLL; |
| 389 | #endif |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 390 | #if defined(ENABLE_SEPOLL) |
| 391 | cfg_polling_mechanism |= POLL_USE_SEPOLL; |
| 392 | #endif |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 393 | #if defined(ENABLE_KQUEUE) |
| 394 | cfg_polling_mechanism |= POLL_USE_KQUEUE; |
| 395 | #endif |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 396 | |
| 397 | pid = getpid(); |
| 398 | progname = *argv; |
| 399 | while ((tmp = strchr(progname, '/')) != NULL) |
| 400 | progname = tmp + 1; |
| 401 | |
| 402 | argc--; argv++; |
| 403 | while (argc > 0) { |
| 404 | char *flag; |
| 405 | |
| 406 | if (**argv == '-') { |
| 407 | flag = *argv+1; |
| 408 | |
| 409 | /* 1 arg */ |
| 410 | if (*flag == 'v') { |
| 411 | display_version(); |
| 412 | exit(0); |
| 413 | } |
| 414 | #if defined(ENABLE_EPOLL) |
| 415 | else if (*flag == 'd' && flag[1] == 'e') |
| 416 | cfg_polling_mechanism &= ~POLL_USE_EPOLL; |
| 417 | #endif |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 418 | #if defined(ENABLE_SEPOLL) |
| 419 | else if (*flag == 'd' && flag[1] == 's') |
| 420 | cfg_polling_mechanism &= ~POLL_USE_SEPOLL; |
| 421 | #endif |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 422 | #if defined(ENABLE_POLL) |
| 423 | else if (*flag == 'd' && flag[1] == 'p') |
| 424 | cfg_polling_mechanism &= ~POLL_USE_POLL; |
| 425 | #endif |
Willy Tarreau | 69cad1a | 2007-04-10 22:45:11 +0200 | [diff] [blame] | 426 | #if defined(ENABLE_KQUEUE) |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 427 | else if (*flag == 'd' && flag[1] == 'k') |
| 428 | cfg_polling_mechanism &= ~POLL_USE_KQUEUE; |
| 429 | #endif |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 430 | else if (*flag == 'V') |
| 431 | arg_mode |= MODE_VERBOSE; |
| 432 | else if (*flag == 'd' && flag[1] == 'b') |
| 433 | arg_mode |= MODE_FOREGROUND; |
| 434 | else if (*flag == 'd') |
| 435 | arg_mode |= MODE_DEBUG; |
| 436 | else if (*flag == 'c') |
| 437 | arg_mode |= MODE_CHECK; |
| 438 | else if (*flag == 'D') |
| 439 | arg_mode |= MODE_DAEMON | MODE_QUIET; |
| 440 | else if (*flag == 'q') |
| 441 | arg_mode |= MODE_QUIET; |
| 442 | else if (*flag == 's' && (flag[1] == 'f' || flag[1] == 't')) { |
| 443 | /* list of pids to finish ('f') or terminate ('t') */ |
| 444 | |
| 445 | if (flag[1] == 'f') |
| 446 | oldpids_sig = SIGUSR1; /* finish then exit */ |
| 447 | else |
| 448 | oldpids_sig = SIGTERM; /* terminate immediately */ |
| 449 | argv++; argc--; |
| 450 | |
| 451 | if (argc > 0) { |
| 452 | oldpids = calloc(argc, sizeof(int)); |
| 453 | while (argc > 0) { |
| 454 | oldpids[nb_oldpids] = atol(*argv); |
| 455 | if (oldpids[nb_oldpids] <= 0) |
| 456 | usage(old_argv); |
| 457 | argc--; argv++; |
| 458 | nb_oldpids++; |
| 459 | } |
| 460 | } |
| 461 | } |
| 462 | else { /* >=2 args */ |
| 463 | argv++; argc--; |
| 464 | if (argc == 0) |
| 465 | usage(old_argv); |
| 466 | |
| 467 | switch (*flag) { |
| 468 | case 'n' : cfg_maxconn = atol(*argv); break; |
| 469 | case 'm' : global.rlimit_memmax = atol(*argv); break; |
| 470 | case 'N' : cfg_maxpconn = atol(*argv); break; |
| 471 | case 'f' : cfg_cfgfile = *argv; break; |
| 472 | case 'p' : cfg_pidfile = *argv; break; |
| 473 | default: usage(old_argv); |
| 474 | } |
| 475 | } |
| 476 | } |
| 477 | else |
| 478 | usage(old_argv); |
| 479 | argv++; argc--; |
| 480 | } |
| 481 | |
| 482 | global.mode = MODE_STARTING | /* during startup, we want most of the alerts */ |
| 483 | (arg_mode & (MODE_DAEMON | MODE_FOREGROUND | MODE_VERBOSE |
| 484 | | MODE_QUIET | MODE_CHECK | MODE_DEBUG)); |
| 485 | |
| 486 | if (!cfg_cfgfile) |
| 487 | usage(old_argv); |
| 488 | |
| 489 | gethostname(hostname, MAX_HOSTNAME_LEN); |
| 490 | |
| 491 | have_appsession = 0; |
| 492 | global.maxsock = 10; /* reserve 10 fds ; will be incremented by socket eaters */ |
| 493 | if (readcfgfile(cfg_cfgfile) < 0) { |
| 494 | Alert("Error reading configuration file : %s\n", cfg_cfgfile); |
| 495 | exit(1); |
| 496 | } |
| 497 | if (have_appsession) |
| 498 | appsession_init(); |
| 499 | |
| 500 | if (global.mode & MODE_CHECK) { |
| 501 | qfprintf(stdout, "Configuration file is valid : %s\n", cfg_cfgfile); |
| 502 | exit(0); |
| 503 | } |
| 504 | |
| 505 | if (cfg_maxconn > 0) |
| 506 | global.maxconn = cfg_maxconn; |
| 507 | |
| 508 | if (cfg_pidfile) { |
| 509 | if (global.pidfile) |
| 510 | free(global.pidfile); |
| 511 | global.pidfile = strdup(cfg_pidfile); |
| 512 | } |
| 513 | |
| 514 | if (global.maxconn == 0) |
| 515 | global.maxconn = DEFAULT_MAXCONN; |
| 516 | |
| 517 | global.maxsock += global.maxconn * 2; /* each connection needs two sockets */ |
| 518 | |
| 519 | if (arg_mode & (MODE_DEBUG | MODE_FOREGROUND)) { |
| 520 | /* command line debug mode inhibits configuration mode */ |
| 521 | global.mode &= ~(MODE_DAEMON | MODE_QUIET); |
| 522 | } |
| 523 | global.mode |= (arg_mode & (MODE_DAEMON | MODE_FOREGROUND | MODE_QUIET | |
| 524 | MODE_VERBOSE | MODE_DEBUG | MODE_STATS | MODE_LOG)); |
| 525 | |
| 526 | if ((global.mode & MODE_DEBUG) && (global.mode & (MODE_DAEMON | MODE_QUIET))) { |
| 527 | Warning("<debug> mode incompatible with <quiet> and <daemon>. Keeping <debug> only.\n"); |
| 528 | global.mode &= ~(MODE_DAEMON | MODE_QUIET); |
| 529 | } |
| 530 | |
| 531 | if ((global.nbproc > 1) && !(global.mode & MODE_DAEMON)) { |
| 532 | if (!(global.mode & (MODE_FOREGROUND | MODE_DEBUG))) |
| 533 | Warning("<nbproc> is only meaningful in daemon mode. Setting limit to 1 process.\n"); |
| 534 | global.nbproc = 1; |
| 535 | } |
| 536 | |
| 537 | if (global.nbproc < 1) |
| 538 | global.nbproc = 1; |
| 539 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 540 | fdtab = (struct fdtab *)calloc(1, |
| 541 | sizeof(struct fdtab) * (global.maxsock)); |
| 542 | for (i = 0; i < global.maxsock; i++) { |
| 543 | fdtab[i].state = FD_STCLOSE; |
| 544 | } |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 545 | |
Willy Tarreau | ef1d1f8 | 2007-04-16 00:25:25 +0200 | [diff] [blame] | 546 | /* |
| 547 | * Note: we could register external pollers here. |
| 548 | * Built-in pollers have been registered before main(). |
| 549 | */ |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 550 | |
Willy Tarreau | 1e63130a | 2007-04-09 12:03:06 +0200 | [diff] [blame] | 551 | if (!(cfg_polling_mechanism & POLL_USE_KQUEUE)) |
| 552 | disable_poller("kqueue"); |
| 553 | |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 554 | if (!(cfg_polling_mechanism & POLL_USE_EPOLL)) |
| 555 | disable_poller("epoll"); |
| 556 | |
Willy Tarreau | de99e99 | 2007-04-16 00:53:59 +0200 | [diff] [blame] | 557 | if (!(cfg_polling_mechanism & POLL_USE_SEPOLL)) |
| 558 | disable_poller("sepoll"); |
| 559 | |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 560 | if (!(cfg_polling_mechanism & POLL_USE_POLL)) |
| 561 | disable_poller("poll"); |
| 562 | |
| 563 | if (!(cfg_polling_mechanism & POLL_USE_SELECT)) |
| 564 | disable_poller("select"); |
| 565 | |
| 566 | /* Note: we could disable any poller by name here */ |
| 567 | |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 568 | if (global.mode & (MODE_VERBOSE|MODE_DEBUG)) |
| 569 | list_pollers(stderr); |
| 570 | |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 571 | if (!init_pollers()) { |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 572 | Alert("No polling mechanism available.\n"); |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 573 | exit(1); |
| 574 | } |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 575 | if (global.mode & (MODE_VERBOSE|MODE_DEBUG)) { |
| 576 | printf("Using %s() as the polling mechanism.\n", cur_poller.name); |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 577 | } |
| 578 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 579 | } |
| 580 | |
| 581 | void deinit(void) |
| 582 | { |
| 583 | struct proxy *p = proxy; |
| 584 | struct cap_hdr *h,*h_next; |
| 585 | struct server *s,*s_next; |
| 586 | struct listener *l,*l_next; |
| 587 | |
| 588 | while (p) { |
| 589 | if (p->id) |
| 590 | free(p->id); |
| 591 | |
| 592 | if (p->check_req) |
| 593 | free(p->check_req); |
| 594 | |
| 595 | if (p->cookie_name) |
| 596 | free(p->cookie_name); |
| 597 | |
| 598 | if (p->capture_name) |
| 599 | free(p->capture_name); |
| 600 | |
| 601 | /* only strup if the user have set in config. |
| 602 | When should we free it?! |
| 603 | if (p->errmsg.msg400) free(p->errmsg.msg400); |
| 604 | if (p->errmsg.msg403) free(p->errmsg.msg403); |
| 605 | if (p->errmsg.msg408) free(p->errmsg.msg408); |
| 606 | if (p->errmsg.msg500) free(p->errmsg.msg500); |
| 607 | if (p->errmsg.msg502) free(p->errmsg.msg502); |
| 608 | if (p->errmsg.msg503) free(p->errmsg.msg503); |
| 609 | if (p->errmsg.msg504) free(p->errmsg.msg504); |
| 610 | */ |
| 611 | if (p->appsession_name) |
| 612 | free(p->appsession_name); |
| 613 | |
| 614 | h = p->req_cap; |
| 615 | while (h) { |
| 616 | h_next = h->next; |
| 617 | if (h->name) |
| 618 | free(h->name); |
| 619 | pool_destroy(h->pool); |
| 620 | free(h); |
| 621 | h = h_next; |
| 622 | }/* end while(h) */ |
| 623 | |
| 624 | h = p->rsp_cap; |
| 625 | while (h) { |
| 626 | h_next = h->next; |
| 627 | if (h->name) |
| 628 | free(h->name); |
| 629 | |
| 630 | pool_destroy(h->pool); |
| 631 | free(h); |
| 632 | h = h_next; |
| 633 | }/* end while(h) */ |
| 634 | |
| 635 | s = p->srv; |
| 636 | while (s) { |
| 637 | s_next = s->next; |
| 638 | if (s->id) |
| 639 | free(s->id); |
| 640 | |
| 641 | if (s->cookie) |
| 642 | free(s->cookie); |
| 643 | |
| 644 | free(s); |
| 645 | s = s_next; |
| 646 | }/* end while(s) */ |
| 647 | |
| 648 | l = p->listen; |
| 649 | while (l) { |
| 650 | l_next = l->next; |
| 651 | free(l); |
| 652 | l = l_next; |
| 653 | }/* end while(l) */ |
| 654 | |
| 655 | pool_destroy((void **) p->req_cap_pool); |
| 656 | pool_destroy((void **) p->rsp_cap_pool); |
| 657 | p = p->next; |
| 658 | }/* end while(p) */ |
| 659 | |
| 660 | if (global.chroot) free(global.chroot); |
| 661 | if (global.pidfile) free(global.pidfile); |
| 662 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 663 | if (fdtab) free(fdtab); |
| 664 | |
Willy Tarreau | c6ca1a0 | 2007-05-13 19:43:47 +0200 | [diff] [blame] | 665 | pool_destroy2(pool2_session); |
Willy Tarreau | 7341d94 | 2007-05-13 19:56:02 +0200 | [diff] [blame] | 666 | pool_destroy2(pool2_buffer); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 667 | pool_destroy(pool_requri); |
Willy Tarreau | c6ca1a0 | 2007-05-13 19:43:47 +0200 | [diff] [blame] | 668 | pool_destroy2(pool2_task); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 669 | pool_destroy(pool_capture); |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame^] | 670 | pool_destroy2(pool2_appsess); |
Willy Tarreau | e4d7e55 | 2007-05-13 20:19:55 +0200 | [diff] [blame] | 671 | pool_destroy2(pool2_pendconn); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 672 | |
| 673 | if (have_appsession) { |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame^] | 674 | pool_destroy2(apools.serverid); |
| 675 | pool_destroy2(apools.sessid); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 676 | } |
| 677 | } /* end deinit() */ |
| 678 | |
| 679 | /* sends the signal <sig> to all pids found in <oldpids> */ |
| 680 | static void tell_old_pids(int sig) |
| 681 | { |
| 682 | int p; |
| 683 | for (p = 0; p < nb_oldpids; p++) |
| 684 | kill(oldpids[p], sig); |
| 685 | } |
| 686 | |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 687 | /* |
| 688 | * Runs the polling loop |
| 689 | * |
| 690 | * FIXME: |
| 691 | * - we still use 'listeners' to check whether we want to stop or not. |
| 692 | * |
| 693 | */ |
| 694 | void run_poll_loop() |
| 695 | { |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 696 | struct timeval next; |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 697 | |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 698 | tv_now(&now); |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 699 | while (1) { |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 700 | process_runnable_tasks(&next); |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 701 | |
| 702 | /* stop when there's no connection left and we don't allow them anymore */ |
| 703 | if (!actconn && listeners == 0) |
| 704 | break; |
| 705 | |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 706 | cur_poller.poll(&cur_poller, &next); |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 707 | } |
| 708 | } |
| 709 | |
| 710 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 711 | int main(int argc, char **argv) |
| 712 | { |
| 713 | int err, retry; |
| 714 | struct rlimit limit; |
| 715 | FILE *pidfile = NULL; |
| 716 | init(argc, argv); |
| 717 | |
| 718 | signal(SIGQUIT, dump); |
| 719 | signal(SIGUSR1, sig_soft_stop); |
| 720 | signal(SIGHUP, sig_dump_state); |
| 721 | #ifdef DEBUG_MEMORY |
| 722 | signal(SIGINT, sig_int); |
| 723 | signal(SIGTERM, sig_term); |
| 724 | #endif |
| 725 | |
| 726 | /* on very high loads, a sigpipe sometimes happen just between the |
| 727 | * getsockopt() which tells "it's OK to write", and the following write :-( |
| 728 | */ |
| 729 | #ifndef MSG_NOSIGNAL |
| 730 | signal(SIGPIPE, SIG_IGN); |
| 731 | #endif |
| 732 | |
| 733 | /* We will loop at most 100 times with 10 ms delay each time. |
| 734 | * That's at most 1 second. We only send a signal to old pids |
| 735 | * if we cannot grab at least one port. |
| 736 | */ |
| 737 | retry = MAX_START_RETRIES; |
| 738 | err = ERR_NONE; |
| 739 | while (retry >= 0) { |
| 740 | struct timeval w; |
| 741 | err = start_proxies(retry == 0 || nb_oldpids == 0); |
| 742 | if (err != ERR_RETRYABLE) |
| 743 | break; |
| 744 | if (nb_oldpids == 0) |
| 745 | break; |
| 746 | |
| 747 | /* FIXME-20060514: Solaris and OpenBSD do not support shutdown() on |
| 748 | * listening sockets. So on those platforms, it would be wiser to |
| 749 | * simply send SIGUSR1, which will not be undoable. |
| 750 | */ |
| 751 | tell_old_pids(SIGTTOU); |
| 752 | /* give some time to old processes to stop listening */ |
| 753 | w.tv_sec = 0; |
| 754 | w.tv_usec = 10*1000; |
| 755 | select(0, NULL, NULL, NULL, &w); |
| 756 | retry--; |
| 757 | } |
| 758 | |
| 759 | /* Note: start_proxies() sends an alert when it fails. */ |
| 760 | if (err != ERR_NONE) { |
| 761 | if (retry != MAX_START_RETRIES && nb_oldpids) |
| 762 | tell_old_pids(SIGTTIN); |
| 763 | exit(1); |
| 764 | } |
| 765 | |
| 766 | if (listeners == 0) { |
| 767 | Alert("[%s.main()] No enabled listener found (check the <listen> keywords) ! Exiting.\n", argv[0]); |
| 768 | /* Note: we don't have to send anything to the old pids because we |
| 769 | * never stopped them. */ |
| 770 | exit(1); |
| 771 | } |
| 772 | |
| 773 | /* prepare pause/play signals */ |
| 774 | signal(SIGTTOU, sig_pause); |
| 775 | signal(SIGTTIN, sig_listen); |
| 776 | |
| 777 | if (global.mode & MODE_DAEMON) { |
| 778 | global.mode &= ~MODE_VERBOSE; |
| 779 | global.mode |= MODE_QUIET; |
| 780 | } |
| 781 | |
| 782 | /* MODE_QUIET can inhibit alerts and warnings below this line */ |
| 783 | |
| 784 | global.mode &= ~MODE_STARTING; |
| 785 | if ((global.mode & MODE_QUIET) && !(global.mode & MODE_VERBOSE)) { |
| 786 | /* detach from the tty */ |
| 787 | fclose(stdin); fclose(stdout); fclose(stderr); |
| 788 | close(0); close(1); close(2); |
| 789 | } |
| 790 | |
| 791 | /* open log & pid files before the chroot */ |
| 792 | if (global.mode & MODE_DAEMON && global.pidfile != NULL) { |
| 793 | int pidfd; |
| 794 | unlink(global.pidfile); |
| 795 | pidfd = open(global.pidfile, O_CREAT | O_WRONLY | O_TRUNC, 0644); |
| 796 | if (pidfd < 0) { |
| 797 | Alert("[%s.main()] Cannot create pidfile %s\n", argv[0], global.pidfile); |
| 798 | if (nb_oldpids) |
| 799 | tell_old_pids(SIGTTIN); |
| 800 | exit(1); |
| 801 | } |
| 802 | pidfile = fdopen(pidfd, "w"); |
| 803 | } |
| 804 | |
| 805 | /* chroot if needed */ |
| 806 | if (global.chroot != NULL) { |
| 807 | if (chroot(global.chroot) == -1) { |
| 808 | Alert("[%s.main()] Cannot chroot(%s).\n", argv[0], global.chroot); |
| 809 | if (nb_oldpids) |
| 810 | tell_old_pids(SIGTTIN); |
| 811 | } |
| 812 | chdir("/"); |
| 813 | } |
| 814 | |
| 815 | /* ulimits */ |
| 816 | if (!global.rlimit_nofile) |
| 817 | global.rlimit_nofile = global.maxsock; |
| 818 | |
| 819 | if (global.rlimit_nofile) { |
| 820 | limit.rlim_cur = limit.rlim_max = global.rlimit_nofile; |
| 821 | if (setrlimit(RLIMIT_NOFILE, &limit) == -1) { |
| 822 | Warning("[%s.main()] Cannot raise FD limit to %d.\n", argv[0], global.rlimit_nofile); |
| 823 | } |
| 824 | } |
| 825 | |
| 826 | if (global.rlimit_memmax) { |
| 827 | limit.rlim_cur = limit.rlim_max = |
| 828 | global.rlimit_memmax * 1048576 / global.nbproc; |
| 829 | #ifdef RLIMIT_AS |
| 830 | if (setrlimit(RLIMIT_AS, &limit) == -1) { |
| 831 | Warning("[%s.main()] Cannot fix MEM limit to %d megs.\n", |
| 832 | argv[0], global.rlimit_memmax); |
| 833 | } |
| 834 | #else |
| 835 | if (setrlimit(RLIMIT_DATA, &limit) == -1) { |
| 836 | Warning("[%s.main()] Cannot fix MEM limit to %d megs.\n", |
| 837 | argv[0], global.rlimit_memmax); |
| 838 | } |
| 839 | #endif |
| 840 | } |
| 841 | |
Willy Tarreau | 6d1a988 | 2007-01-07 02:03:04 +0100 | [diff] [blame] | 842 | #ifdef CONFIG_HAP_TCPSPLICE |
| 843 | if (global.last_checks & LSTCHK_TCPSPLICE) { |
| 844 | if (tcp_splice_start() < 0) { |
| 845 | Alert("[%s.main()] Cannot enable tcp_splice.\n" |
| 846 | " Make sure you have enough permissions and that the module is loadable.\n" |
| 847 | " Alternatively, you may disable the 'tcpsplice' options in the configuration.\n" |
| 848 | "", argv[0], global.gid); |
| 849 | exit(1); |
| 850 | } |
| 851 | } |
| 852 | #endif |
| 853 | |
Willy Tarreau | b38651a | 2007-03-24 17:24:39 +0100 | [diff] [blame] | 854 | #ifdef CONFIG_HAP_CTTPROXY |
| 855 | if (global.last_checks & LSTCHK_CTTPROXY) { |
| 856 | int ret; |
| 857 | |
| 858 | ret = check_cttproxy_version(); |
| 859 | if (ret < 0) { |
| 860 | Alert("[%s.main()] Cannot enable cttproxy.\n%s", |
| 861 | argv[0], |
| 862 | (ret == -1) ? " Incorrect module version.\n" |
| 863 | : " Make sure you have enough permissions and that the module is loaded.\n"); |
| 864 | exit(1); |
| 865 | } |
| 866 | } |
| 867 | #endif |
| 868 | |
| 869 | if ((global.last_checks & LSTCHK_NETADM) && global.uid) { |
| 870 | Alert("[%s.main()] Some configuration options require full privileges, so global.uid cannot be changed.\n" |
| 871 | "", argv[0], global.gid); |
| 872 | exit(1); |
| 873 | } |
| 874 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 875 | if (nb_oldpids) |
| 876 | tell_old_pids(oldpids_sig); |
| 877 | |
| 878 | /* Note that any error at this stage will be fatal because we will not |
| 879 | * be able to restart the old pids. |
| 880 | */ |
| 881 | |
| 882 | /* setgid / setuid */ |
| 883 | if (global.gid && setgid(global.gid) == -1) { |
| 884 | Alert("[%s.main()] Cannot set gid %d.\n", argv[0], global.gid); |
| 885 | exit(1); |
| 886 | } |
| 887 | |
| 888 | if (global.uid && setuid(global.uid) == -1) { |
| 889 | Alert("[%s.main()] Cannot set uid %d.\n", argv[0], global.uid); |
| 890 | exit(1); |
| 891 | } |
| 892 | |
| 893 | /* check ulimits */ |
| 894 | limit.rlim_cur = limit.rlim_max = 0; |
| 895 | getrlimit(RLIMIT_NOFILE, &limit); |
| 896 | if (limit.rlim_cur < global.maxsock) { |
| 897 | 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", |
| 898 | argv[0], limit.rlim_cur, global.maxconn, global.maxsock, global.maxsock); |
| 899 | } |
| 900 | |
| 901 | if (global.mode & MODE_DAEMON) { |
| 902 | int ret = 0; |
| 903 | int proc; |
| 904 | |
| 905 | /* the father launches the required number of processes */ |
| 906 | for (proc = 0; proc < global.nbproc; proc++) { |
| 907 | ret = fork(); |
| 908 | if (ret < 0) { |
| 909 | Alert("[%s.main()] Cannot fork.\n", argv[0]); |
| 910 | if (nb_oldpids) |
| 911 | exit(1); /* there has been an error */ |
| 912 | } |
| 913 | else if (ret == 0) /* child breaks here */ |
| 914 | break; |
| 915 | if (pidfile != NULL) { |
| 916 | fprintf(pidfile, "%d\n", ret); |
| 917 | fflush(pidfile); |
| 918 | } |
| 919 | } |
| 920 | /* close the pidfile both in children and father */ |
| 921 | if (pidfile != NULL) |
| 922 | fclose(pidfile); |
| 923 | free(global.pidfile); |
| 924 | |
| 925 | if (proc == global.nbproc) |
| 926 | exit(0); /* parent must leave */ |
| 927 | |
| 928 | /* if we're NOT in QUIET mode, we should now close the 3 first FDs to ensure |
| 929 | * that we can detach from the TTY. We MUST NOT do it in other cases since |
| 930 | * it would have already be done, and 0-2 would have been affected to listening |
| 931 | * sockets |
| 932 | */ |
| 933 | if (!(global.mode & MODE_QUIET)) { |
| 934 | /* detach from the tty */ |
| 935 | fclose(stdin); fclose(stdout); fclose(stderr); |
| 936 | close(0); close(1); close(2); /* close all fd's */ |
| 937 | global.mode |= MODE_QUIET; /* ensure that we won't say anything from now */ |
| 938 | } |
| 939 | pid = getpid(); /* update child's pid */ |
| 940 | setsid(); |
Willy Tarreau | 2ff7622 | 2007-04-09 19:29:56 +0200 | [diff] [blame] | 941 | fork_poller(); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 942 | } |
| 943 | |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 944 | /* |
| 945 | * That's it : the central polling loop. Run until we stop. |
| 946 | */ |
| 947 | run_poll_loop(); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 948 | |
| 949 | /* Free all Hash Keys and all Hash elements */ |
| 950 | appsession_cleanup(); |
| 951 | /* Do some cleanup */ |
| 952 | deinit(); |
| 953 | |
| 954 | exit(0); |
| 955 | } |
| 956 | |
| 957 | |
| 958 | /* |
| 959 | * Local variables: |
| 960 | * c-indent-level: 8 |
| 961 | * c-basic-offset: 8 |
| 962 | * End: |
| 963 | */ |