int main(int argc, char **argv) { int status; wrapper_argc = argc; wrapper_argv = argv; --argc; ++argv; init(argc, argv); struct sigaction sa; memset(&sa, 0, sizeof(struct sigaction)); sa.sa_handler = &signal_handler; sigaction(SIGUSR2, &sa, NULL); sigaction(SIGHUP, &sa, NULL); sigaction(SIGINT, &sa, NULL); sigaction(SIGTERM, &sa, NULL); if (getenv(REEXEC_FLAG) != NULL) { /* We are being re-executed: restart HAProxy gracefully */ int i; char **pid_strv = NULL; int nb_pid = read_pids(&pid_strv); unsetenv(REEXEC_FLAG); spawn_haproxy(pid_strv, nb_pid); for (i = 0; i < nb_pid; ++i) free(pid_strv[i]); free(pid_strv); } else { /* Start a fresh copy of HAProxy */ spawn_haproxy(NULL, 0); } status = -1; while (caught_signal || wait(&status) != -1 || errno == EINTR) { int sig = caught_signal; if (caught_signal == SIGUSR2 || caught_signal == SIGHUP) { caught_signal = 0; do_restart(sig); } else if (caught_signal == SIGINT || caught_signal == SIGTERM) { caught_signal = 0; do_shutdown(sig); } } fprintf(stderr, SD_NOTICE "haproxy-systemd-wrapper: exit, haproxy RC=%d\n", status); return status; }
static void signal_handler(int signum __attribute__((unused))) { int i; char **pid_strv = NULL; int nb_pid = read_pids(&pid_strv); spawn_haproxy(pid_strv, nb_pid); for (i = 0; i < nb_pid; ++i) free(pid_strv[i]); free(pid_strv); }
static void do_shutdown(void) { int i, pid; char **pid_strv = NULL; int nb_pid = read_pids(&pid_strv); for (i = 0; i < nb_pid; ++i) { pid = atoi(pid_strv[i]); if (pid > 0) { fprintf(stderr, SD_DEBUG "haproxy-systemd-wrapper: SIGINT -> %d\n", pid); kill(pid, SIGINT); free(pid_strv[i]); } } free(pid_strv); }
static void sigint_handler(int signum __attribute__((unused))) { int i, pid; char **pid_strv = NULL; int nb_pid = read_pids(&pid_strv); for (i = 0; i < nb_pid; ++i) { pid = atoi(pid_strv[i]); if (pid > 0) { fprintf(stderr, SD_DEBUG "haproxy-smf-wrapper: SIGINT -> %d\n", pid); kill(pid, SIGINT); free(pid_strv[i]); } } free(pid_strv); }
/* handles SIGTERM and SIGINT only */ static void do_shutdown(int sig) { int i, pid; char **pid_strv = NULL; int nb_pid = read_pids(&pid_strv); for (i = 0; i < nb_pid; ++i) { pid = atoi(pid_strv[i]); if (pid > 0) { fprintf(stderr, SD_DEBUG "haproxy-systemd-wrapper: %s -> %d.\n", sig == SIGTERM ? "SIGTERM" : "SIGINT", pid); kill(pid, sig); free(pid_strv[i]); } } free(pid_strv); }
int main(int argc, char **argv) { int status; wrapper_argc = argc; wrapper_argv = argv; --argc; ++argv; init(argc, argv); signal(SIGINT, &sigint_handler); signal(SIGHUP, &sighup_handler); if (getenv(REEXEC_FLAG) != NULL) { /* We are being re-executed: restart HAProxy gracefully */ int i; char **pid_strv = NULL; int nb_pid = read_pids(&pid_strv); sigset_t sigs; unsetenv(REEXEC_FLAG); spawn_haproxy(pid_strv, nb_pid); /* Unblock SIGHUP which was blocked by the signal handler * before re-exec */ sigprocmask(SIG_BLOCK, NULL, &sigs); sigdelset(&sigs, SIGHUP); sigprocmask(SIG_SETMASK, &sigs, NULL); for (i = 0; i < nb_pid; ++i) free(pid_strv[i]); free(pid_strv); } else { /* Start a fresh copy of HAProxy */ spawn_haproxy(NULL, 0); } status = -1; while (-1 != wait(&status) || errno == EINTR) ; fprintf(stderr, SD_NOTICE "haproxy-smf-wrapper: exit, haproxy RC=%d\n", status); return status; }