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.

105 lines
3.0 KiB

  1. From 62c8565cd5bbda6ac0dd818fa26922eeaef1605c Mon Sep 17 00:00:00 2001
  2. From: Conrad Hoffmann <conrad@soundcloud.com>
  3. Date: Mon, 28 Jul 2014 23:52:20 +0200
  4. Subject: [PATCH 03/13] MEDIUM: Improve signal handling in systemd wrapper.
  5. Move all code out of the signal handlers, since this is potentially
  6. dangerous. To make sure the signal handlers behave as expected, use
  7. sigaction() instead of signal(). That also obsoletes messing with
  8. the signal mask after restart.
  9. Signed-off-by: Conrad Hoffmann <conrad@soundcloud.com>
  10. (cherry picked from commit 5b5ea9c93384da49eea0f67ebed0966d4167b17a)
  11. ---
  12. src/haproxy-systemd-wrapper.c | 37 ++++++++++++++++++++++++-------------
  13. 1 file changed, 24 insertions(+), 13 deletions(-)
  14. diff --git a/src/haproxy-systemd-wrapper.c b/src/haproxy-systemd-wrapper.c
  15. index 529b213..90a94ce 100644
  16. --- a/src/haproxy-systemd-wrapper.c
  17. +++ b/src/haproxy-systemd-wrapper.c
  18. @@ -22,6 +22,8 @@
  19. #define SD_DEBUG "<7>"
  20. #define SD_NOTICE "<5>"
  21. +static volatile sig_atomic_t caught_signal;
  22. +
  23. static char *pid_file = "/run/haproxy.pid";
  24. static int wrapper_argc;
  25. static char **wrapper_argv;
  26. @@ -103,7 +105,12 @@ static int read_pids(char ***pid_strv)
  27. return read;
  28. }
  29. -static void sigusr2_handler(int signum __attribute__((unused)))
  30. +static void signal_handler(int signum)
  31. +{
  32. + caught_signal = signum;
  33. +}
  34. +
  35. +static void do_restart(void)
  36. {
  37. setenv(REEXEC_FLAG, "1", 1);
  38. fprintf(stderr, SD_NOTICE "haproxy-systemd-wrapper: re-executing\n");
  39. @@ -111,7 +118,7 @@ static void sigusr2_handler(int signum __attribute__((unused)))
  40. execv(wrapper_argv[0], wrapper_argv);
  41. }
  42. -static void sigint_handler(int signum __attribute__((unused)))
  43. +static void do_shutdown(void)
  44. {
  45. int i, pid;
  46. char **pid_strv = NULL;
  47. @@ -147,25 +154,21 @@ int main(int argc, char **argv)
  48. --argc; ++argv;
  49. init(argc, argv);
  50. - signal(SIGINT, &sigint_handler);
  51. - signal(SIGUSR2, &sigusr2_handler);
  52. + struct sigaction sa;
  53. + memset(&sa, 0, sizeof(struct sigaction));
  54. + sa.sa_handler = &signal_handler;
  55. + sigaction(SIGUSR2, &sa, NULL);
  56. + sigaction(SIGINT, &sa, NULL);
  57. if (getenv(REEXEC_FLAG) != NULL) {
  58. /* We are being re-executed: restart HAProxy gracefully */
  59. int i;
  60. char **pid_strv = NULL;
  61. int nb_pid = read_pids(&pid_strv);
  62. - sigset_t sigs;
  63. unsetenv(REEXEC_FLAG);
  64. spawn_haproxy(pid_strv, nb_pid);
  65. - /* Unblock SIGUSR2 which was blocked by the signal handler
  66. - * before re-exec */
  67. - sigprocmask(SIG_BLOCK, NULL, &sigs);
  68. - sigdelset(&sigs, SIGUSR2);
  69. - sigprocmask(SIG_SETMASK, &sigs, NULL);
  70. -
  71. for (i = 0; i < nb_pid; ++i)
  72. free(pid_strv[i]);
  73. free(pid_strv);
  74. @@ -176,8 +179,16 @@ int main(int argc, char **argv)
  75. }
  76. status = -1;
  77. - while (-1 != wait(&status) || errno == EINTR)
  78. - ;
  79. + while (-1 != wait(&status) || errno == EINTR) {
  80. + if (caught_signal == SIGUSR2) {
  81. + caught_signal = 0;
  82. + do_restart();
  83. + }
  84. + else if (caught_signal == SIGINT) {
  85. + caught_signal = 0;
  86. + do_shutdown();
  87. + }
  88. + }
  89. fprintf(stderr, SD_NOTICE "haproxy-systemd-wrapper: exit, haproxy RC=%d\n",
  90. status);
  91. --
  92. 1.8.5.5