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.

117 lines
3.4 KiB

  1. From 7a7eada6f4ecfc54325a18cf20c3035994a901c4 Mon Sep 17 00:00:00 2001
  2. From: Willy Tarreau <w@1wt.eu>
  3. Date: Tue, 25 Oct 2016 16:49:31 +0200
  4. Subject: [PATCH 13/26] BUG/MINOR: systemd: always restore signals before
  5. execve()
  6. Since signals are inherited, we must restore them before calling execve()
  7. and intercept them again after a failed execve(). In order to cleanly deal
  8. with the SIGUSR2/SIGHUP loops where we re-exec the wrapper, we ignore these
  9. two signals during a re-exec, and restore them to defaults when spawning
  10. haproxy.
  11. This should be backported to 1.6 and 1.5.
  12. (cherry picked from commit 4351ea61fbddf88c960179d60b0e0f1b090f0b70)
  13. ---
  14. src/haproxy-systemd-wrapper.c | 49 ++++++++++++++++++++++++++++++++++++-------
  15. 1 file changed, 41 insertions(+), 8 deletions(-)
  16. diff --git a/src/haproxy-systemd-wrapper.c b/src/haproxy-systemd-wrapper.c
  17. index a78e75b..84d2e17 100644
  18. --- a/src/haproxy-systemd-wrapper.c
  19. +++ b/src/haproxy-systemd-wrapper.c
  20. @@ -28,6 +28,11 @@ static char *pid_file = "/run/haproxy.pid";
  21. static int wrapper_argc;
  22. static char **wrapper_argv;
  23. +static void setup_signal_handler();
  24. +static void pause_signal_handler();
  25. +static void reset_signal_handler();
  26. +
  27. +
  28. /* returns the path to the haproxy binary into <buffer>, whose size indicated
  29. * in <buffer_size> must be at least 1 byte long.
  30. */
  31. @@ -76,6 +81,8 @@ static void spawn_haproxy(char **pid_strv, int nb_pid)
  32. char **argv = calloc(4 + main_argc + nb_pid + 1, sizeof(char *));
  33. int i;
  34. int argno = 0;
  35. +
  36. + reset_signal_handler();
  37. locate_haproxy(haproxy_bin, 512);
  38. argv[argno++] = haproxy_bin;
  39. for (i = 0; i < main_argc; ++i)
  40. @@ -127,6 +134,34 @@ static void signal_handler(int signum)
  41. caught_signal = signum;
  42. }
  43. +static void setup_signal_handler()
  44. +{
  45. + struct sigaction sa;
  46. +
  47. + memset(&sa, 0, sizeof(struct sigaction));
  48. + sa.sa_handler = &signal_handler;
  49. + sigaction(SIGUSR2, &sa, NULL);
  50. + sigaction(SIGHUP, &sa, NULL);
  51. + sigaction(SIGINT, &sa, NULL);
  52. + sigaction(SIGTERM, &sa, NULL);
  53. +}
  54. +
  55. +static void pause_signal_handler()
  56. +{
  57. + signal(SIGUSR2, SIG_IGN);
  58. + signal(SIGHUP, SIG_IGN);
  59. + signal(SIGINT, SIG_DFL);
  60. + signal(SIGTERM, SIG_DFL);
  61. +}
  62. +
  63. +static void reset_signal_handler()
  64. +{
  65. + signal(SIGUSR2, SIG_DFL);
  66. + signal(SIGHUP, SIG_DFL);
  67. + signal(SIGINT, SIG_DFL);
  68. + signal(SIGTERM, SIG_DFL);
  69. +}
  70. +
  71. /* handles SIGUSR2 and SIGHUP only */
  72. static void do_restart(int sig)
  73. {
  74. @@ -134,7 +169,11 @@ static void do_restart(int sig)
  75. fprintf(stderr, SD_NOTICE "haproxy-systemd-wrapper: re-executing on %s.\n",
  76. sig == SIGUSR2 ? "SIGUSR2" : "SIGHUP");
  77. + /* don't let the other process take one of those signals by accident */
  78. + pause_signal_handler();
  79. execv(wrapper_argv[0], wrapper_argv);
  80. + /* failed, let's reinstall the signal handler and continue */
  81. + setup_signal_handler();
  82. }
  83. /* handles SIGTERM and SIGINT only */
  84. @@ -168,7 +207,8 @@ static void init(int argc, char **argv)
  85. int main(int argc, char **argv)
  86. {
  87. int status;
  88. - struct sigaction sa;
  89. +
  90. + setup_signal_handler();
  91. wrapper_argc = argc;
  92. wrapper_argv = argv;
  93. @@ -176,13 +216,6 @@ int main(int argc, char **argv)
  94. --argc; ++argv;
  95. init(argc, argv);
  96. - memset(&sa, 0, sizeof(struct sigaction));
  97. - sa.sa_handler = &signal_handler;
  98. - sigaction(SIGUSR2, &sa, NULL);
  99. - sigaction(SIGHUP, &sa, NULL);
  100. - sigaction(SIGINT, &sa, NULL);
  101. - sigaction(SIGTERM, &sa, NULL);
  102. -
  103. if (getenv(REEXEC_FLAG) != NULL) {
  104. /* We are being re-executed: restart HAProxy gracefully */
  105. int i;
  106. --
  107. 2.7.3