From afbfc27c0f2cac29e18f87b36335ea821c633b9d Mon Sep 17 00:00:00 2001 From: Willy Tarreau Date: Fri, 19 Sep 2014 15:42:30 +0200 Subject: [PATCH 14/14] MEDIUM: systemd-wrapper: support multiple executable versions and names Having to use a hard-coded "haproxy" executable name next to the systemd wrapper is not always convenient, as it's sometimes desirable to run with multiple versions in parallel. Thus this patch performs a minor change to the wrapper : if the name ends with "-systemd-wrapper", then it trims that part off and what remains becomes the target haproxy executable. That makes it easy to have for example : haproxy-1.5.4-systemd-wrapper haproxy-1.5.4 haproxy-1.5.3-systemd-wrapper haproxy-1.5.3 and so on, in a same directory. This patch also fixes a rare bug caused by readlink() not adding the trailing zero and leaving possible existing contents, including possibly a randomly placed "/" which would make it unable to locate the correct binary. This case is not totally unlikely as I got a \177 a few times at the end of the executable names, so I could have got a '/' as well. Back-porting to 1.5 is desirable. (cherry picked from commit ceaf2aec1ec1612da461c61798e944693144bee9) --- src/haproxy-systemd-wrapper.c | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/src/haproxy-systemd-wrapper.c b/src/haproxy-systemd-wrapper.c index cc8baa8..446f28f 100644 --- a/src/haproxy-systemd-wrapper.c +++ b/src/haproxy-systemd-wrapper.c @@ -28,20 +28,36 @@ static char *pid_file = "/run/haproxy.pid"; static int wrapper_argc; static char **wrapper_argv; +/* returns the path to the haproxy binary into , whose size indicated + * in must be at least 1 byte long. + */ static void locate_haproxy(char *buffer, size_t buffer_size) { char *end = NULL; + int len; + + len = readlink("/proc/self/exe", buffer, buffer_size - 1); + if (len == -1) + goto fail; - if (readlink("/proc/self/exe", buffer, buffer_size) > 0) - end = strrchr(buffer, '/'); + buffer[len] = 0; + end = strrchr(buffer, '/'); + if (end == NULL) + goto fail; - if (end == NULL) { - strncpy(buffer, "/usr/sbin/haproxy", buffer_size); + if (strcmp(end + strlen(end) - 16, "-systemd-wrapper") == 0) { + end[strlen(end) - 16] = '\0'; return; } + end[1] = '\0'; strncpy(end + 1, "haproxy", buffer + buffer_size - (end + 1)); buffer[buffer_size - 1] = '\0'; + return; + fail: + strncpy(buffer, "/usr/sbin/haproxy", buffer_size); + buffer[buffer_size - 1] = '\0'; + return; } static void spawn_haproxy(char **pid_strv, int nb_pid) @@ -54,7 +70,8 @@ static void spawn_haproxy(char **pid_strv, int nb_pid) main_argc = wrapper_argc - 1; main_argv = wrapper_argv + 1; - pid = fork(); + //pid = fork(); + pid=0; if (!pid) { /* 3 for "haproxy -Ds -sf" */ char **argv = calloc(4 + main_argc + nb_pid + 1, sizeof(char *)); -- 1.8.5.5