~ubuntu-branches/ubuntu/utopic/haproxy/utopic-proposed

« back to all changes in this revision

Viewing changes to src/haproxy.c

  • Committer: Bazaar Package Importer
  • Author(s): Christo Buschek
  • Date: 2011-03-11 12:41:59 UTC
  • mfrom: (1.1.10 upstream)
  • Revision ID: james.westby@ubuntu.com-20110311124159-9foyp4juf1ilqipo
Tags: 1.4.13-1
* New maintainer upload (Closes: #615246)
* New upstream release
* Standards-version goes 3.9.1 (no change)
* Added patch bashism (Closes: #581109)
* Added a README.source file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
95
95
 
96
96
/* list of config files */
97
97
static struct list cfg_cfgfiles = LIST_HEAD_INIT(cfg_cfgfiles);
98
 
char *progname = NULL;          /* program name */
99
98
int  pid;                       /* current process id */
100
99
int  relative_pid = 1;          /* process id starting at 1 */
101
100
 
118
117
        .tune = {
119
118
                .bufsize = BUFSIZE,
120
119
                .maxrewrite = MAXREWRITE,
 
120
                .chksize = BUFSIZE,
121
121
        },
122
122
        /* others NULL OK */
123
123
};
131
131
 * our ports. With 200 retries, that's about 2 seconds.
132
132
 */
133
133
#define MAX_START_RETRIES       200
134
 
static int nb_oldpids = 0;
135
134
static int *oldpids = NULL;
136
135
static int oldpids_sig; /* use USR1 or TERM */
137
136
 
143
142
 */
144
143
char *swap_buffer = NULL;
145
144
 
 
145
int nb_oldpids = 0;
146
146
const int zero = 0;
147
147
const int one = 1;
148
148
const struct linger nolinger = { .l_onoff = 1, .l_linger = 0 };
397
397
        char *cfg_pidfile = NULL;
398
398
        int err_code = 0;
399
399
        struct wordlist *wl;
 
400
        char *progname;
400
401
 
401
402
        /*
402
403
         * Initialize the previously static variables.
441
442
        while ((tmp = strchr(progname, '/')) != NULL)
442
443
                progname = tmp + 1;
443
444
 
 
445
        /* the process name is used for the logs only */
 
446
        global.log_tag = strdup(progname);
 
447
 
444
448
        argc--; argv++;
445
449
        while (argc > 0) {
446
450
                char *flag;
867
871
                l = p->listen;
868
872
                while (l) {
869
873
                        l_next = l->next;
 
874
                        unbind_listener(l);
 
875
                        delete_listener(l);
870
876
                        free(l->name);
871
877
                        free(l->counters);
872
878
                        free(l);
906
912
 
907
913
        protocol_unbind_all();
908
914
 
 
915
        free(global.log_send_hostname); global.log_send_hostname = NULL;
 
916
        free(global.log_tag); global.log_tag = NULL;
909
917
        free(global.chroot);  global.chroot = NULL;
910
918
        free(global.pidfile); global.pidfile = NULL;
911
919
        free(global.node);    global.node = NULL;
935
943
 
936
944
} /* end deinit() */
937
945
 
938
 
/* sends the signal <sig> to all pids found in <oldpids> */
939
 
static void tell_old_pids(int sig)
 
946
/* sends the signal <sig> to all pids found in <oldpids>. Returns the number of
 
947
 * pids the signal was correctly delivered to.
 
948
 */
 
949
static int tell_old_pids(int sig)
940
950
{
941
951
        int p;
 
952
        int ret = 0;
942
953
        for (p = 0; p < nb_oldpids; p++)
943
 
                kill(oldpids[p], sig);
 
954
                if (kill(oldpids[p], sig) == 0)
 
955
                        ret++;
 
956
        return ret;
944
957
}
945
958
 
946
959
/*
1001
1014
         */
1002
1015
        signal(SIGPIPE, SIG_IGN);
1003
1016
 
 
1017
        /* ulimits */
 
1018
        if (!global.rlimit_nofile)
 
1019
                global.rlimit_nofile = global.maxsock;
 
1020
 
 
1021
        if (global.rlimit_nofile) {
 
1022
                limit.rlim_cur = limit.rlim_max = global.rlimit_nofile;
 
1023
                if (setrlimit(RLIMIT_NOFILE, &limit) == -1) {
 
1024
                        Warning("[%s.main()] Cannot raise FD limit to %d.\n", argv[0], global.rlimit_nofile);
 
1025
                }
 
1026
        }
 
1027
 
 
1028
        if (global.rlimit_memmax) {
 
1029
                limit.rlim_cur = limit.rlim_max =
 
1030
                        global.rlimit_memmax * 1048576 / global.nbproc;
 
1031
#ifdef RLIMIT_AS
 
1032
                if (setrlimit(RLIMIT_AS, &limit) == -1) {
 
1033
                        Warning("[%s.main()] Cannot fix MEM limit to %d megs.\n",
 
1034
                                argv[0], global.rlimit_memmax);
 
1035
                }
 
1036
#else
 
1037
                if (setrlimit(RLIMIT_DATA, &limit) == -1) {
 
1038
                        Warning("[%s.main()] Cannot fix MEM limit to %d megs.\n",
 
1039
                                argv[0], global.rlimit_memmax);
 
1040
                }
 
1041
#endif
 
1042
        }
 
1043
 
1004
1044
        /* We will loop at most 100 times with 10 ms delay each time.
1005
1045
         * That's at most 1 second. We only send a signal to old pids
1006
1046
         * if we cannot grab at least one port.
1013
1053
                /* exit the loop on no error or fatal error */
1014
1054
                if ((err & (ERR_RETRYABLE|ERR_FATAL)) != ERR_RETRYABLE)
1015
1055
                        break;
1016
 
                if (nb_oldpids == 0)
 
1056
                if (nb_oldpids == 0 || retry == 0)
1017
1057
                        break;
1018
1058
 
1019
1059
                /* FIXME-20060514: Solaris and OpenBSD do not support shutdown() on
1020
1060
                 * listening sockets. So on those platforms, it would be wiser to
1021
1061
                 * simply send SIGUSR1, which will not be undoable.
1022
1062
                 */
1023
 
                tell_old_pids(SIGTTOU);
 
1063
                if (tell_old_pids(SIGTTOU) == 0) {
 
1064
                        /* no need to wait if we can't contact old pids */
 
1065
                        retry = 0;
 
1066
                        continue;
 
1067
                }
1024
1068
                /* give some time to old processes to stop listening */
1025
1069
                w.tv_sec = 0;
1026
1070
                w.tv_usec = 10*1000;
1079
1123
                pidfile = fdopen(pidfd, "w");
1080
1124
        }
1081
1125
 
1082
 
        /* ulimits */
1083
 
        if (!global.rlimit_nofile)
1084
 
                global.rlimit_nofile = global.maxsock;
1085
 
 
1086
 
        if (global.rlimit_nofile) {
1087
 
                limit.rlim_cur = limit.rlim_max = global.rlimit_nofile;
1088
 
                if (setrlimit(RLIMIT_NOFILE, &limit) == -1) {
1089
 
                        Warning("[%s.main()] Cannot raise FD limit to %d.\n", argv[0], global.rlimit_nofile);
1090
 
                }
1091
 
        }
1092
 
 
1093
 
        if (global.rlimit_memmax) {
1094
 
                limit.rlim_cur = limit.rlim_max =
1095
 
                        global.rlimit_memmax * 1048576 / global.nbproc;
1096
 
#ifdef RLIMIT_AS
1097
 
                if (setrlimit(RLIMIT_AS, &limit) == -1) {
1098
 
                        Warning("[%s.main()] Cannot fix MEM limit to %d megs.\n",
1099
 
                                argv[0], global.rlimit_memmax);
1100
 
                }
1101
 
#else
1102
 
                if (setrlimit(RLIMIT_DATA, &limit) == -1) {
1103
 
                        Warning("[%s.main()] Cannot fix MEM limit to %d megs.\n",
1104
 
                                argv[0], global.rlimit_memmax);
1105
 
                }
1106
 
#endif
1107
 
        }
1108
 
 
1109
1126
#ifdef CONFIG_HAP_CTTPROXY
1110
1127
        if (global.last_checks & LSTCHK_CTTPROXY) {
1111
1128
                int ret;
1150
1167
        }
1151
1168
 
1152
1169
        if (nb_oldpids)
1153
 
                tell_old_pids(oldpids_sig);
 
1170
                nb_oldpids = tell_old_pids(oldpids_sig);
1154
1171
 
1155
1172
        /* Note that any error at this stage will be fatal because we will not
1156
1173
         * be able to restart the old pids.
1201
1218
                /* close the pidfile both in children and father */
1202
1219
                if (pidfile != NULL)
1203
1220
                        fclose(pidfile);
1204
 
                free(global.pidfile);
1205
 
                global.pidfile = NULL;
 
1221
 
 
1222
                /* We won't ever use this anymore */
 
1223
                free(oldpids);        oldpids = NULL;
 
1224
                free(global.chroot);  global.chroot = NULL;
 
1225
                free(global.pidfile); global.pidfile = NULL;
1206
1226
 
1207
1227
                /* we might have to unbind some proxies from some processes */
1208
1228
                px = proxy;