2
* HA-Proxy : High Availability-enabled HTTP/TCP proxy
3
* Copyright 2000-2007 Willy Tarreau <w@1wt.eu>.
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.
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 :
14
* http://ftp.ics.uci.edu/pub/ietf/http/
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.
24
* ChangeLog has moved to the CHANGELOG file.
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.
33
* - cut this huge file into several ones
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>
53
#include <sys/resource.h>
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>
75
#include <types/capture.h>
76
#include <types/global.h>
77
#include <types/httperr.h>
78
#include <types/polling.h>
79
#include <types/proto_http.h>
81
#include <proto/acl.h>
82
#include <proto/backend.h>
83
#include <proto/buffers.h>
84
#include <proto/client.h>
86
#include <proto/log.h>
87
#include <proto/proto_http.h>
88
#include <proto/proxy.h>
89
#include <proto/queue.h>
90
#include <proto/server.h>
91
#include <proto/session.h>
92
#include <proto/stream_sock.h>
93
#include <proto/task.h>
95
#ifdef CONFIG_HAP_TCPSPLICE
96
#include <libtcpsplice.h>
99
#ifdef CONFIG_HAP_CTTPROXY
100
#include <proto/cttproxy.h>
103
/*********************************************************************/
105
/*********************************************************************/
107
char *cfg_cfgfile = NULL; /* configuration file */
108
char *progname = NULL; /* program name */
109
int pid; /* current process id */
112
struct global global = {
115
loglev1 : 7, /* max syslog level : debug */
120
/*********************************************************************/
122
int stopping; /* non zero means stopping in progress */
124
/* Here we store informations about the pids of the processes we may pause
125
* or kill. We will send them a signal every 10 ms until we can bind to all
126
* our ports. With 200 retries, that's about 2 seconds.
128
#define MAX_START_RETRIES 200
129
static int nb_oldpids = 0;
130
static int *oldpids = NULL;
131
static int oldpids_sig; /* use USR1 or TERM */
133
/* this is used to drain data, and as a temporary buffer for sprintf()... */
140
* Syslog facilities and levels. Conforming to RFC3164.
143
#define MAX_HOSTNAME_LEN 32
144
static char hostname[MAX_HOSTNAME_LEN] = "";
147
/*********************************************************************/
148
/* general purpose functions ***************************************/
149
/*********************************************************************/
151
void display_version()
153
printf("HA-Proxy version " HAPROXY_VERSION " " HAPROXY_DATE"\n");
154
printf("Copyright 2000-2007 Willy Tarreau <w@1wt.eu>\n\n");
158
* This function prints the command line usage and exits
160
void usage(char *name)
164
"Usage : %s -f <cfgfile> [ -vdV"
165
"D ] [ -n <maxconn> ] [ -N <maxpconn> ]\n"
166
" [ -p <pidfile> ] [ -m <max megs> ]\n"
167
" -v displays version\n"
168
" -d enters debug mode ; -db only disables background mode.\n"
169
" -V enters verbose mode (disables quiet mode)\n"
170
" -D goes daemon ; implies -q\n"
171
" -q quiet mode : don't display messages\n"
172
" -c check mode : only check config file and exit\n"
173
" -n sets the maximum total # of connections (%d)\n"
174
" -m limits the usable amount of memory (in MB)\n"
175
" -N sets the default, per-proxy maximum # of connections (%d)\n"
176
" -p writes pids of all children to this file\n"
177
#if defined(ENABLE_EPOLL)
178
" -de disables epoll() usage even when available\n"
180
#if defined(ENABLE_SEPOLL)
181
" -ds disables speculative epoll() usage even when available\n"
183
#if defined(ENABLE_KQUEUE)
184
" -dk disables kqueue() usage even when available\n"
186
#if defined(ENABLE_POLL)
187
" -dp disables poll() usage even when available\n"
189
" -sf/-st [pid ]* finishes/terminates old pids. Must be last arguments.\n"
191
name, DEFAULT_MAXCONN, cfg_maxpconn);
197
/*********************************************************************/
198
/* more specific functions ***************************************/
199
/*********************************************************************/
202
* upon SIGUSR1, let's have a soft stop.
204
void sig_soft_stop(int sig)
208
signal(sig, SIG_IGN);
212
* upon SIGTTOU, we pause everything
214
void sig_pause(int sig)
218
signal(sig, sig_pause);
222
* upon SIGTTIN, let's have a soft stop.
224
void sig_listen(int sig)
227
signal(sig, sig_listen);
231
* this function dumps every server's state when the process receives SIGHUP.
233
void sig_dump_state(int sig)
235
struct proxy *p = proxy;
237
Warning("SIGHUP received, dumping servers states.\n");
239
struct server *s = p->srv;
241
send_log(p, LOG_NOTICE, "SIGHUP received, dumping servers states for proxy %s.\n", p->id);
243
snprintf(trash, sizeof(trash),
244
"SIGHUP: Server %s/%s is %s. Conn: %d act, %d pend, %d tot.",
246
(s->state & SRV_RUNNING) ? "UP" : "DOWN",
247
s->cur_sess, s->nbpend, s->cum_sess);
248
Warning("%s\n", trash);
249
send_log(p, LOG_NOTICE, "%s\n", trash);
253
if (p->srv_act == 0) {
254
snprintf(trash, sizeof(trash),
255
"SIGHUP: Proxy %s %s ! Conn: act(FE+BE): %d+%d, %d pend (%d unass), tot(FE+BE): %d+%d.",
257
(p->srv_bck) ? "is running on backup servers" : "has no server available",
258
p->feconn, p->beconn, p->totpend, p->nbpend, p->cum_feconn, p->cum_beconn);
260
snprintf(trash, sizeof(trash),
261
"SIGHUP: Proxy %s has %d active servers and %d backup servers available."
262
" Conn: act(FE+BE): %d+%d, %d pend (%d unass), tot(FE+BE): %d+%d.",
263
p->id, p->srv_act, p->srv_bck,
264
p->feconn, p->beconn, p->totpend, p->nbpend, p->cum_feconn, p->cum_beconn);
266
Warning("%s\n", trash);
267
send_log(p, LOG_NOTICE, "%s\n", trash);
271
signal(sig, sig_dump_state);
279
struct rb_node *node;
281
for(node = rb_first(&wait_queue[0]);
282
node != NULL; node = rb_next(node)) {
283
t = rb_entry(node, struct task, rb_node);
285
qfprintf(stderr,"[dump] wq: task %p, still %ld ms, "
286
"cli=%d, srv=%d, cr=%d, cw=%d, sr=%d, sw=%d, "
287
"req=%d, rep=%d, clifd=%d\n",
288
s, tv_ms_remain(&now, &t->expire),
291
EV_FD_ISSET(s->cli_fd, DIR_RD),
292
EV_FD_ISSET(s->cli_fd, DIR_WR),
293
EV_FD_ISSET(s->srv_fd, DIR_RD),
294
EV_FD_ISSET(s->srv_fd, DIR_WR),
295
s->req->l, s->rep?s->rep->l:0, s->cli_fd
299
/* dump memory usage then free everything possible */
305
static void fast_stop(void)
316
void sig_int(int sig)
318
/* This would normally be a hard stop,
319
but we want to be sure about deallocation,
320
and so on, so we do a soft stop with
325
/* If we are killed twice, we decide to die*/
326
signal(sig, SIG_DFL);
329
void sig_term(int sig)
331
/* This would normally be a hard stop,
332
but we want to be sure about deallocation,
333
and so on, so we do a soft stop with
338
/* If we are killed twice, we decide to die*/
339
signal(sig, SIG_DFL);
345
* This function initializes all the necessary variables. It only returns
346
* if everything is OK. If something fails, it exits.
348
void init(int argc, char **argv)
351
int arg_mode = 0; /* MODE_DEBUG, ... */
352
char *old_argv = *argv;
354
char *cfg_pidfile = NULL;
356
if (1<<INTBITS != sizeof(int)*8) {
358
"Error: wrong architecture. Recompile so that sizeof(int)=%d\n",
359
(int)(sizeof(int)*8));
364
* Initialize the previously static variables.
367
totalconn = actconn = maxfd = listeners = stopping = 0;
370
#ifdef HAPROXY_MEMMAX
371
global.rlimit_memmax = HAPROXY_MEMMAX;
374
/* initialize the libc's localtime structures once for all so that we
375
* won't be missing memory if we want to send alerts under OOM conditions.
376
* Also, the Alert() and Warning() functions need <now> to be initialized.
379
localtime((time_t *)&now.tv_sec);
388
cfg_polling_mechanism = POLL_USE_SELECT; /* select() is always available */
389
#if defined(ENABLE_POLL)
390
cfg_polling_mechanism |= POLL_USE_POLL;
392
#if defined(ENABLE_EPOLL)
393
cfg_polling_mechanism |= POLL_USE_EPOLL;
395
#if defined(ENABLE_SEPOLL)
396
cfg_polling_mechanism |= POLL_USE_SEPOLL;
398
#if defined(ENABLE_KQUEUE)
399
cfg_polling_mechanism |= POLL_USE_KQUEUE;
404
while ((tmp = strchr(progname, '/')) != NULL)
419
#if defined(ENABLE_EPOLL)
420
else if (*flag == 'd' && flag[1] == 'e')
421
cfg_polling_mechanism &= ~POLL_USE_EPOLL;
423
#if defined(ENABLE_SEPOLL)
424
else if (*flag == 'd' && flag[1] == 's')
425
cfg_polling_mechanism &= ~POLL_USE_SEPOLL;
427
#if defined(ENABLE_POLL)
428
else if (*flag == 'd' && flag[1] == 'p')
429
cfg_polling_mechanism &= ~POLL_USE_POLL;
431
#if defined(ENABLE_KQUEUE)
432
else if (*flag == 'd' && flag[1] == 'k')
433
cfg_polling_mechanism &= ~POLL_USE_KQUEUE;
435
else if (*flag == 'V')
436
arg_mode |= MODE_VERBOSE;
437
else if (*flag == 'd' && flag[1] == 'b')
438
arg_mode |= MODE_FOREGROUND;
439
else if (*flag == 'd')
440
arg_mode |= MODE_DEBUG;
441
else if (*flag == 'c')
442
arg_mode |= MODE_CHECK;
443
else if (*flag == 'D')
444
arg_mode |= MODE_DAEMON | MODE_QUIET;
445
else if (*flag == 'q')
446
arg_mode |= MODE_QUIET;
447
else if (*flag == 's' && (flag[1] == 'f' || flag[1] == 't')) {
448
/* list of pids to finish ('f') or terminate ('t') */
451
oldpids_sig = SIGUSR1; /* finish then exit */
453
oldpids_sig = SIGTERM; /* terminate immediately */
457
oldpids = calloc(argc, sizeof(int));
459
oldpids[nb_oldpids] = atol(*argv);
460
if (oldpids[nb_oldpids] <= 0)
467
else { /* >=2 args */
473
case 'n' : cfg_maxconn = atol(*argv); break;
474
case 'm' : global.rlimit_memmax = atol(*argv); break;
475
case 'N' : cfg_maxpconn = atol(*argv); break;
476
case 'f' : cfg_cfgfile = *argv; break;
477
case 'p' : cfg_pidfile = *argv; break;
478
default: usage(old_argv);
487
global.mode = MODE_STARTING | /* during startup, we want most of the alerts */
488
(arg_mode & (MODE_DAEMON | MODE_FOREGROUND | MODE_VERBOSE
489
| MODE_QUIET | MODE_CHECK | MODE_DEBUG));
494
gethostname(hostname, MAX_HOSTNAME_LEN);
497
global.maxsock = 10; /* reserve 10 fds ; will be incremented by socket eaters */
498
if (readcfgfile(cfg_cfgfile) < 0) {
499
Alert("Error reading configuration file : %s\n", cfg_cfgfile);
505
if (global.mode & MODE_CHECK) {
506
qfprintf(stdout, "Configuration file is valid : %s\n", cfg_cfgfile);
511
global.maxconn = cfg_maxconn;
515
free(global.pidfile);
516
global.pidfile = strdup(cfg_pidfile);
519
if (global.maxconn == 0)
520
global.maxconn = DEFAULT_MAXCONN;
522
global.maxsock += global.maxconn * 2; /* each connection needs two sockets */
524
if (global.tune.maxpollevents <= 0)
525
global.tune.maxpollevents = MAX_POLL_EVENTS;
527
if (arg_mode & (MODE_DEBUG | MODE_FOREGROUND)) {
528
/* command line debug mode inhibits configuration mode */
529
global.mode &= ~(MODE_DAEMON | MODE_QUIET);
531
global.mode |= (arg_mode & (MODE_DAEMON | MODE_FOREGROUND | MODE_QUIET |
532
MODE_VERBOSE | MODE_DEBUG | MODE_STATS | MODE_LOG));
534
if ((global.mode & MODE_DEBUG) && (global.mode & (MODE_DAEMON | MODE_QUIET))) {
535
Warning("<debug> mode incompatible with <quiet> and <daemon>. Keeping <debug> only.\n");
536
global.mode &= ~(MODE_DAEMON | MODE_QUIET);
539
if ((global.nbproc > 1) && !(global.mode & MODE_DAEMON)) {
540
if (!(global.mode & (MODE_FOREGROUND | MODE_DEBUG)))
541
Warning("<nbproc> is only meaningful in daemon mode. Setting limit to 1 process.\n");
545
if (global.nbproc < 1)
548
fdtab = (struct fdtab *)calloc(1,
549
sizeof(struct fdtab) * (global.maxsock));
550
for (i = 0; i < global.maxsock; i++) {
551
fdtab[i].state = FD_STCLOSE;
555
* Note: we could register external pollers here.
556
* Built-in pollers have been registered before main().
559
if (!(cfg_polling_mechanism & POLL_USE_KQUEUE))
560
disable_poller("kqueue");
562
if (!(cfg_polling_mechanism & POLL_USE_EPOLL))
563
disable_poller("epoll");
565
if (!(cfg_polling_mechanism & POLL_USE_SEPOLL))
566
disable_poller("sepoll");
568
if (!(cfg_polling_mechanism & POLL_USE_POLL))
569
disable_poller("poll");
571
if (!(cfg_polling_mechanism & POLL_USE_SELECT))
572
disable_poller("select");
574
/* Note: we could disable any poller by name here */
576
if (global.mode & (MODE_VERBOSE|MODE_DEBUG))
577
list_pollers(stderr);
579
if (!init_pollers()) {
580
Alert("No polling mechanism available.\n");
583
if (global.mode & (MODE_VERBOSE|MODE_DEBUG)) {
584
printf("Using %s() as the polling mechanism.\n", cur_poller.name);
591
struct proxy *p = proxy, *p0;
592
struct cap_hdr *h,*h_next;
593
struct server *s,*s_next;
594
struct listener *l,*l_next;
595
struct acl_cond *cond, *condb;
596
struct hdr_exp *exp, *expb;
607
free(p->cookie_name);
610
free(p->capture_name);
613
free(p->monitor_uri);
615
for (i = 0; i < HTTP_ERR_SIZE; i++) {
616
if (p->errmsg[i].len)
617
free(p->errmsg[i].str);
620
for (i = 0; i < p->nb_reqadd; i++) {
625
for (i = 0; i < p->nb_rspadd; i++) {
630
list_for_each_entry_safe(cond, condb, &p->block_cond, list) {
631
LIST_DEL(&cond->list);
632
prune_acl_cond(cond);
636
for (exp = p->req_exp; exp != NULL; ) {
638
regfree((regex_t *)exp->preg);
639
if (exp->replace && exp->action != ACT_SETBE)
640
free((char *)exp->replace);
646
for (exp = p->rsp_exp; exp != NULL; ) {
648
regfree((regex_t *)exp->preg);
649
if (exp->replace && exp->action != ACT_SETBE)
650
free((char *)exp->replace);
656
/* FIXME: this must also be freed :
658
* - uri_auth (but it's shared)
661
if (p->appsession_name)
662
free(p->appsession_name);
669
pool_destroy2(h->pool);
680
pool_destroy2(h->pool);
705
pool_destroy2(p->req_cap_pool);
706
pool_destroy2(p->rsp_cap_pool);
712
if (global.chroot) free(global.chroot);
713
global.chroot = NULL;
715
if (global.pidfile) free(global.pidfile);
716
global.pidfile = NULL;
718
if (fdtab) free(fdtab);
721
pool_destroy2(pool2_session);
722
pool_destroy2(pool2_buffer);
723
pool_destroy2(pool2_requri);
724
pool_destroy2(pool2_task);
725
pool_destroy2(pool2_capture);
726
pool_destroy2(pool2_appsess);
727
pool_destroy2(pool2_pendconn);
729
if (have_appsession) {
730
pool_destroy2(apools.serverid);
731
pool_destroy2(apools.sessid);
735
/* sends the signal <sig> to all pids found in <oldpids> */
736
static void tell_old_pids(int sig)
739
for (p = 0; p < nb_oldpids; p++)
740
kill(oldpids[p], sig);
744
* Runs the polling loop
747
* - we still use 'listeners' to check whether we want to stop or not.
756
process_runnable_tasks(&next);
758
/* stop when there's no connection left and we don't allow them anymore */
759
if (!actconn && listeners == 0)
762
cur_poller.poll(&cur_poller, &next);
767
int main(int argc, char **argv)
771
FILE *pidfile = NULL;
774
signal(SIGQUIT, dump);
775
signal(SIGUSR1, sig_soft_stop);
776
signal(SIGHUP, sig_dump_state);
778
signal(SIGINT, sig_int);
779
signal(SIGTERM, sig_term);
782
/* on very high loads, a sigpipe sometimes happen just between the
783
* getsockopt() which tells "it's OK to write", and the following write :-(
786
signal(SIGPIPE, SIG_IGN);
789
/* We will loop at most 100 times with 10 ms delay each time.
790
* That's at most 1 second. We only send a signal to old pids
791
* if we cannot grab at least one port.
793
retry = MAX_START_RETRIES;
797
err = start_proxies(retry == 0 || nb_oldpids == 0);
798
if (err != ERR_RETRYABLE)
803
/* FIXME-20060514: Solaris and OpenBSD do not support shutdown() on
804
* listening sockets. So on those platforms, it would be wiser to
805
* simply send SIGUSR1, which will not be undoable.
807
tell_old_pids(SIGTTOU);
808
/* give some time to old processes to stop listening */
811
select(0, NULL, NULL, NULL, &w);
815
/* Note: start_proxies() sends an alert when it fails. */
816
if (err != ERR_NONE) {
817
if (retry != MAX_START_RETRIES && nb_oldpids)
818
tell_old_pids(SIGTTIN);
822
if (listeners == 0) {
823
Alert("[%s.main()] No enabled listener found (check the <listen> keywords) ! Exiting.\n", argv[0]);
824
/* Note: we don't have to send anything to the old pids because we
825
* never stopped them. */
829
/* prepare pause/play signals */
830
signal(SIGTTOU, sig_pause);
831
signal(SIGTTIN, sig_listen);
833
if (global.mode & MODE_DAEMON) {
834
global.mode &= ~MODE_VERBOSE;
835
global.mode |= MODE_QUIET;
838
/* MODE_QUIET can inhibit alerts and warnings below this line */
840
global.mode &= ~MODE_STARTING;
841
if ((global.mode & MODE_QUIET) && !(global.mode & MODE_VERBOSE)) {
842
/* detach from the tty */
843
fclose(stdin); fclose(stdout); fclose(stderr);
844
close(0); close(1); close(2);
847
/* open log & pid files before the chroot */
848
if (global.mode & MODE_DAEMON && global.pidfile != NULL) {
850
unlink(global.pidfile);
851
pidfd = open(global.pidfile, O_CREAT | O_WRONLY | O_TRUNC, 0644);
853
Alert("[%s.main()] Cannot create pidfile %s\n", argv[0], global.pidfile);
855
tell_old_pids(SIGTTIN);
858
pidfile = fdopen(pidfd, "w");
861
/* chroot if needed */
862
if (global.chroot != NULL) {
863
if (chroot(global.chroot) == -1) {
864
Alert("[%s.main()] Cannot chroot(%s).\n", argv[0], global.chroot);
866
tell_old_pids(SIGTTIN);
872
if (!global.rlimit_nofile)
873
global.rlimit_nofile = global.maxsock;
875
if (global.rlimit_nofile) {
876
limit.rlim_cur = limit.rlim_max = global.rlimit_nofile;
877
if (setrlimit(RLIMIT_NOFILE, &limit) == -1) {
878
Warning("[%s.main()] Cannot raise FD limit to %d.\n", argv[0], global.rlimit_nofile);
882
if (global.rlimit_memmax) {
883
limit.rlim_cur = limit.rlim_max =
884
global.rlimit_memmax * 1048576 / global.nbproc;
886
if (setrlimit(RLIMIT_AS, &limit) == -1) {
887
Warning("[%s.main()] Cannot fix MEM limit to %d megs.\n",
888
argv[0], global.rlimit_memmax);
891
if (setrlimit(RLIMIT_DATA, &limit) == -1) {
892
Warning("[%s.main()] Cannot fix MEM limit to %d megs.\n",
893
argv[0], global.rlimit_memmax);
898
#ifdef CONFIG_HAP_TCPSPLICE
899
if (global.last_checks & LSTCHK_TCPSPLICE) {
900
if (tcp_splice_start() < 0) {
901
Alert("[%s.main()] Cannot enable tcp_splice.\n"
902
" Make sure you have enough permissions and that the module is loadable.\n"
903
" Alternatively, you may disable the 'tcpsplice' options in the configuration.\n"
904
"", argv[0], global.gid);
910
#ifdef CONFIG_HAP_CTTPROXY
911
if (global.last_checks & LSTCHK_CTTPROXY) {
914
ret = check_cttproxy_version();
916
Alert("[%s.main()] Cannot enable cttproxy.\n%s",
918
(ret == -1) ? " Incorrect module version.\n"
919
: " Make sure you have enough permissions and that the module is loaded.\n");
925
if ((global.last_checks & LSTCHK_NETADM) && global.uid) {
926
Alert("[%s.main()] Some configuration options require full privileges, so global.uid cannot be changed.\n"
927
"", argv[0], global.gid);
932
tell_old_pids(oldpids_sig);
934
/* Note that any error at this stage will be fatal because we will not
935
* be able to restart the old pids.
938
/* setgid / setuid */
939
if (global.gid && setgid(global.gid) == -1) {
940
Alert("[%s.main()] Cannot set gid %d.\n", argv[0], global.gid);
944
if (global.uid && setuid(global.uid) == -1) {
945
Alert("[%s.main()] Cannot set uid %d.\n", argv[0], global.uid);
950
limit.rlim_cur = limit.rlim_max = 0;
951
getrlimit(RLIMIT_NOFILE, &limit);
952
if (limit.rlim_cur < global.maxsock) {
953
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",
954
argv[0], limit.rlim_cur, global.maxconn, global.maxsock, global.maxsock);
957
if (global.mode & MODE_DAEMON) {
961
/* the father launches the required number of processes */
962
for (proc = 0; proc < global.nbproc; proc++) {
965
Alert("[%s.main()] Cannot fork.\n", argv[0]);
967
exit(1); /* there has been an error */
969
else if (ret == 0) /* child breaks here */
971
if (pidfile != NULL) {
972
fprintf(pidfile, "%d\n", ret);
976
/* close the pidfile both in children and father */
979
free(global.pidfile);
981
if (proc == global.nbproc)
982
exit(0); /* parent must leave */
984
/* if we're NOT in QUIET mode, we should now close the 3 first FDs to ensure
985
* that we can detach from the TTY. We MUST NOT do it in other cases since
986
* it would have already be done, and 0-2 would have been affected to listening
989
if (!(global.mode & MODE_QUIET)) {
990
/* detach from the tty */
991
fclose(stdin); fclose(stdout); fclose(stderr);
992
close(0); close(1); close(2); /* close all fd's */
993
global.mode |= MODE_QUIET; /* ensure that we won't say anything from now */
995
pid = getpid(); /* update child's pid */
1001
* That's it : the central polling loop. Run until we stop.
1005
/* Free all Hash Keys and all Hash elements */
1006
appsession_cleanup();
1007
/* Do some cleanup */