diff --git a/include/haproxy/global.h b/include/haproxy/global.h index 13fbe7c7c..3c09968c5 100644 --- a/include/haproxy/global.h +++ b/include/haproxy/global.h @@ -68,7 +68,7 @@ void hap_register_feature(const char *name); int split_version(const char *version, unsigned int *value); int compare_current_version(const char *version); void display_version(); -void handle_pidfile(void); +int handle_pidfile(void); void mworker_accept_wrapper(int fd); diff --git a/src/cli.c b/src/cli.c index e5d8fc562..1befd8ee0 100644 --- a/src/cli.c +++ b/src/cli.c @@ -2523,8 +2523,12 @@ static int _send_status(char **args, char *payload, struct appctx *appctx, void * so we can write our PID in a pidfile, if provided. Master doesn't * perform chroot. */ - if (global.pidfile != NULL) - handle_pidfile(); + if (global.pidfile != NULL) { + if (handle_pidfile() < 0) { + ha_alert("Fatal error(s) found, exiting.\n"); + exit(1); + } + } /* either send USR1/TERM to old master, case when we launched as -W -D ... -sf $(cat pidfile), * or send USR1/TERM to old worker processes. diff --git a/src/haproxy.c b/src/haproxy.c index eac6329a1..120822ba6 100644 --- a/src/haproxy.c +++ b/src/haproxy.c @@ -1776,8 +1776,8 @@ static void apply_daemon_mode() } } -/* Only returns if everything is OK. If something fails, it exits. */ -void handle_pidfile(void) +/* Returns 0, if everything is OK. If open() fails, returns -1. */ +int handle_pidfile(void) { char pidstr[100]; @@ -1785,16 +1785,15 @@ void handle_pidfile(void) pidfd = open(global.pidfile, O_CREAT | O_WRONLY | O_TRUNC, 0644); if (pidfd < 0) { ha_alert("[%s.main()] Cannot create pidfile %s\n", progname, global.pidfile); - if (nb_oldpids) - tell_old_pids(SIGTTIN); - protocol_unbind_all(); - exit(1); + return -1; } snprintf(pidstr, sizeof(pidstr), "%d\n", (int)getpid()); DISGUISE(write(pidfd, pidstr, strlen(pidstr))); close(pidfd); /* We won't ever use this anymore */ ha_free(&global.pidfile); + + return 0; } static void get_listeners_fd() @@ -3529,7 +3528,13 @@ int main(int argc, char **argv) */ if (!(global.mode & MODE_MWORKER)) { if (global.mode & MODE_DAEMON && (global.pidfile != NULL)) { - handle_pidfile(); + if (handle_pidfile() < 0) { + if (nb_oldpids) { + tell_old_pids(SIGTTIN); + protocol_unbind_all(); + } + exit(1); + } } }