BUG/MINOR: startup: fix error path for master, if can't open pidfile

If master process can't open a pidfile, there is no sense to send SIGTTIN to
oldpids, as it will exit. So, old workers will terminate as well. It's better
to send the last alert to the log about unrecoverable error, because master is
already in its polling loop.

For the standalone mode we should keep the previous logic in this case: send
SIGTTIN to old process and unbind listeners for the new one. So, it's better
to put this error path in main(), as it's done when other configuration settings
can't be applied.

This patch should be backported only in 3.1.
This commit is contained in:
Valentine Krasnobaeva 2024-12-03 21:33:55 +01:00 committed by William Lallemand
parent ee111d2004
commit cd0b58e23e
3 changed files with 19 additions and 10 deletions

View File

@ -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);

View File

@ -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.

View File

@ -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);
}
}
}