]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MEDIUM: systemd-wrapper: support multiple executable versions and names
authorWilly Tarreau <w@1wt.eu>
Fri, 19 Sep 2014 13:42:30 +0000 (15:42 +0200)
committerWilly Tarreau <w@1wt.eu>
Fri, 19 Sep 2014 13:42:30 +0000 (15:42 +0200)
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.

src/haproxy-systemd-wrapper.c

index cc8baa87b2ae45c862e913e9469e10380a4edcc4..446f28f0bb265a8ad97243c5aa06337aaf248899 100644 (file)
@@ -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 <buffer>, whose size indicated
+ * in <buffer_size> 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 *));