You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

90 lines
2.9 KiB

  1. From afbfc27c0f2cac29e18f87b36335ea821c633b9d Mon Sep 17 00:00:00 2001
  2. From: Willy Tarreau <w@1wt.eu>
  3. Date: Fri, 19 Sep 2014 15:42:30 +0200
  4. Subject: [PATCH 14/14] MEDIUM: systemd-wrapper: support multiple executable
  5. versions and names
  6. Having to use a hard-coded "haproxy" executable name next to the systemd
  7. wrapper is not always convenient, as it's sometimes desirable to run with
  8. multiple versions in parallel.
  9. Thus this patch performs a minor change to the wrapper : if the name ends
  10. with "-systemd-wrapper", then it trims that part off and what remains
  11. becomes the target haproxy executable. That makes it easy to have for
  12. example :
  13. haproxy-1.5.4-systemd-wrapper haproxy-1.5.4
  14. haproxy-1.5.3-systemd-wrapper haproxy-1.5.3
  15. and so on, in a same directory.
  16. This patch also fixes a rare bug caused by readlink() not adding the
  17. trailing zero and leaving possible existing contents, including possibly
  18. a randomly placed "/" which would make it unable to locate the correct
  19. binary. This case is not totally unlikely as I got a \177 a few times
  20. at the end of the executable names, so I could have got a '/' as well.
  21. Back-porting to 1.5 is desirable.
  22. (cherry picked from commit ceaf2aec1ec1612da461c61798e944693144bee9)
  23. ---
  24. src/haproxy-systemd-wrapper.c | 27 ++++++++++++++++++++++-----
  25. 1 file changed, 22 insertions(+), 5 deletions(-)
  26. diff --git a/src/haproxy-systemd-wrapper.c b/src/haproxy-systemd-wrapper.c
  27. index cc8baa8..446f28f 100644
  28. --- a/src/haproxy-systemd-wrapper.c
  29. +++ b/src/haproxy-systemd-wrapper.c
  30. @@ -28,20 +28,36 @@ static char *pid_file = "/run/haproxy.pid";
  31. static int wrapper_argc;
  32. static char **wrapper_argv;
  33. +/* returns the path to the haproxy binary into <buffer>, whose size indicated
  34. + * in <buffer_size> must be at least 1 byte long.
  35. + */
  36. static void locate_haproxy(char *buffer, size_t buffer_size)
  37. {
  38. char *end = NULL;
  39. + int len;
  40. +
  41. + len = readlink("/proc/self/exe", buffer, buffer_size - 1);
  42. + if (len == -1)
  43. + goto fail;
  44. - if (readlink("/proc/self/exe", buffer, buffer_size) > 0)
  45. - end = strrchr(buffer, '/');
  46. + buffer[len] = 0;
  47. + end = strrchr(buffer, '/');
  48. + if (end == NULL)
  49. + goto fail;
  50. - if (end == NULL) {
  51. - strncpy(buffer, "/usr/sbin/haproxy", buffer_size);
  52. + if (strcmp(end + strlen(end) - 16, "-systemd-wrapper") == 0) {
  53. + end[strlen(end) - 16] = '\0';
  54. return;
  55. }
  56. +
  57. end[1] = '\0';
  58. strncpy(end + 1, "haproxy", buffer + buffer_size - (end + 1));
  59. buffer[buffer_size - 1] = '\0';
  60. + return;
  61. + fail:
  62. + strncpy(buffer, "/usr/sbin/haproxy", buffer_size);
  63. + buffer[buffer_size - 1] = '\0';
  64. + return;
  65. }
  66. static void spawn_haproxy(char **pid_strv, int nb_pid)
  67. @@ -54,7 +70,8 @@ static void spawn_haproxy(char **pid_strv, int nb_pid)
  68. main_argc = wrapper_argc - 1;
  69. main_argv = wrapper_argv + 1;
  70. - pid = fork();
  71. + //pid = fork();
  72. + pid=0;
  73. if (!pid) {
  74. /* 3 for "haproxy -Ds -sf" */
  75. char **argv = calloc(4 + main_argc + nb_pid + 1, sizeof(char *));
  76. --
  77. 1.8.5.5