Blame SOURCES/haproxy-systemd-wrapper-exit-code.patch

5cd88c
From 8c8c86d008da636d208ddeb8ac9cf9c06c4164a3 Mon Sep 17 00:00:00 2001
5cd88c
From: Willy Tarreau <w@1wt.eu>
5cd88c
Date: Thu, 3 Nov 2016 20:31:40 +0100
5cd88c
Subject: [PATCH] BUG/MEDIUM: systemd-wrapper: return correct exit codes
5cd88c
5cd88c
Gabriele Cerami reported the the exit codes of the systemd-wrapper are
5cd88c
wrong. In short, it directly returns the output of the wait syscall's
5cd88c
status, which is a composite value made of error code an signal numbers.
5cd88c
In general it contains the signal number on the lower bits and the error
5cd88c
code on the higher bits, but exit() truncates it to the lowest 8 bits,
5cd88c
causing config validations to incorrectly report a success. Example :
5cd88c
5cd88c
  $ ./haproxy-systemd-wrapper -c -f /dev/null
5cd88c
  <7>haproxy-systemd-wrapper: executing /tmp/haproxy -c -f /dev/null -Ds
5cd88c
  Configuration file has no error but will not start (no listener) => exit(2).
5cd88c
  <5>haproxy-systemd-wrapper: exit, haproxy RC=512
5cd88c
  $ echo $?
5cd88c
  0
5cd88c
5cd88c
If the process is killed however, the signal number is directly reported
5cd88c
in the exit code.
5cd88c
5cd88c
Let's fix all this to ensure that the exit code matches what the shell does,
5cd88c
which means that codes 0..127 are for exit codes, codes 128..254 for signals,
5cd88c
and code 255 for unknown exit code. Now the return code is correct :
5cd88c
5cd88c
  $ ./haproxy-systemd-wrapper -c -f /dev/null
5cd88c
  <7>haproxy-systemd-wrapper: executing /tmp/haproxy -c -f /dev/null -Ds
5cd88c
  Configuration file has no error but will not start (no listener) => exit(2).
5cd88c
  <5>haproxy-systemd-wrapper: exit, haproxy RC=2
5cd88c
  $ echo $?
5cd88c
  2
5cd88c
5cd88c
  $ ./haproxy-systemd-wrapper -f /tmp/cfg.conf
5cd88c
  <7>haproxy-systemd-wrapper: executing /tmp/haproxy -f /dev/null -Ds
5cd88c
  ^C
5cd88c
  <5>haproxy-systemd-wrapper: exit, haproxy RC=130
5cd88c
  $ echo $?
5cd88c
  130
5cd88c
5cd88c
This fix must be backported to 1.6 and 1.5.
5cd88c
---
5cd88c
 src/haproxy-systemd-wrapper.c | 10 ++++++++++
5cd88c
 1 file changed, 10 insertions(+)
5cd88c
5cd88c
diff --git a/src/haproxy-systemd-wrapper.c b/src/haproxy-systemd-wrapper.c
5cd88c
index 4e4d039..86520ca 100644
5cd88c
--- a/src/haproxy-systemd-wrapper.c
5cd88c
+++ b/src/haproxy-systemd-wrapper.c
5cd88c
@@ -215,6 +215,16 @@ int main(int argc, char **argv)
5cd88c
 		}
5cd88c
 	}
5cd88c
 
5cd88c
+	/* return either exit code or signal+128 */
5cd88c
+	if (WIFEXITED(status))
5cd88c
+		status = WEXITSTATUS(status);
5cd88c
+	else if (WIFSIGNALED(status))
5cd88c
+		status = 128 + WTERMSIG(status);
5cd88c
+	else if (WIFSTOPPED(status))
5cd88c
+		status = 128 + WSTOPSIG(status);
5cd88c
+	else
5cd88c
+		status = 255;
5cd88c
+
5cd88c
 	fprintf(stderr, SD_NOTICE "haproxy-systemd-wrapper: exit, haproxy RC=%d\n",
5cd88c
 			status);
5cd88c
 	return status;
5cd88c
-- 
5cd88c
2.7.4
5cd88c