[MINOR] startup: don't wait for nothing when no old pid remains
In case of binding failure during startup, we wait for some time sending
signals to old pids so that they release the ports we need. But if there
aren't any old pids anymore, it's useless to wait, we prefer to fail fast.
Along with this change, we now have the number of old pids really found
in the nb_oldpids variable.
diff --git a/include/types/global.h b/include/types/global.h
index 9c62461..12ba8d4 100644
--- a/include/types/global.h
+++ b/include/types/global.h
@@ -104,6 +104,7 @@
extern int listeners;
extern char trash[BUFSIZE];
extern char *swap_buffer;
+extern int nb_oldpids; /* contains the number of old pids found */
extern const int zero;
extern const int one;
extern const struct linger nolinger;
diff --git a/src/haproxy.c b/src/haproxy.c
index 75a310d..ece9526 100644
--- a/src/haproxy.c
+++ b/src/haproxy.c
@@ -130,7 +130,6 @@
* our ports. With 200 retries, that's about 2 seconds.
*/
#define MAX_START_RETRIES 200
-static int nb_oldpids = 0;
static int *oldpids = NULL;
static int oldpids_sig; /* use USR1 or TERM */
@@ -142,6 +141,7 @@
*/
char *swap_buffer = NULL;
+int nb_oldpids = 0;
const int zero = 0;
const int one = 1;
const struct linger nolinger = { .l_onoff = 1, .l_linger = 0 };
@@ -934,12 +934,17 @@
} /* end deinit() */
-/* sends the signal <sig> to all pids found in <oldpids> */
-static void tell_old_pids(int sig)
+/* sends the signal <sig> to all pids found in <oldpids>. Returns the number of
+ * pids the signal was correctly delivered to.
+ */
+static int tell_old_pids(int sig)
{
int p;
+ int ret = 0;
for (p = 0; p < nb_oldpids; p++)
- kill(oldpids[p], sig);
+ if (kill(oldpids[p], sig) == 0)
+ ret++;
+ return ret;
}
/*
@@ -1012,14 +1017,18 @@
/* exit the loop on no error or fatal error */
if ((err & (ERR_RETRYABLE|ERR_FATAL)) != ERR_RETRYABLE)
break;
- if (nb_oldpids == 0)
+ if (nb_oldpids == 0 || retry == 0)
break;
/* FIXME-20060514: Solaris and OpenBSD do not support shutdown() on
* listening sockets. So on those platforms, it would be wiser to
* simply send SIGUSR1, which will not be undoable.
*/
- tell_old_pids(SIGTTOU);
+ if (tell_old_pids(SIGTTOU) == 0) {
+ /* no need to wait if we can't contact old pids */
+ retry = 0;
+ continue;
+ }
/* give some time to old processes to stop listening */
w.tv_sec = 0;
w.tv_usec = 10*1000;
@@ -1149,7 +1158,7 @@
}
if (nb_oldpids)
- tell_old_pids(oldpids_sig);
+ nb_oldpids = tell_old_pids(oldpids_sig);
/* Note that any error at this stage will be fatal because we will not
* be able to restart the old pids.