Skip to content

Commit

Permalink
core: make startup/shutdown scripts work when not using --dont-fork
Browse files Browse the repository at this point in the history
check_start_stop_script_secure() checks that the parent process has
not changed while it is doing its checks, so we need to set the pid
of the parent process (main_pid) before calling the function.

There is a further complication that called getppid() too soon after
a fork() with the parent process exiting after the fork means that
we don't get the pid of the new parent, so we need to loop until
getppid() returns a diffweent pid.

Signed-off-by: Quentin Armitage <quentin@armitage.org.uk>
  • Loading branch information
pqarmitage committed Mar 19, 2024
1 parent 43a0582 commit 7b5c03f
Showing 1 changed file with 23 additions and 6 deletions.
29 changes: 23 additions & 6 deletions keepalived/core/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -2781,19 +2781,36 @@ keepalived_main(int argc, char **argv)
}

/* daemonize process */
if (!__test_bit(DONT_FORK_BIT, &debug) && xdaemon() > 0) {
closelog();
FREE_CONST_PTR(config_id);
FREE_PTR(orig_core_dump_pattern);
close_std_fd();
exit(0);
if (!__test_bit(DONT_FORK_BIT, &debug)) {
pid_t old_ppid = getpid();

if (xdaemon() > 0) {
/* Parent process */
closelog();
FREE_CONST_PTR(config_id);
FREE_PTR(orig_core_dump_pattern);
close_std_fd();
exit(0);
}

/* Child process */

/* check_start_stop_script_secure() called below makes a check
* that our parent process hasn't changed, but it can take a while
* for the parent of the fork to exit which is when our parent pid
* changes. We need to loop until that has happened. */
while (old_ppid == getppid())
usleep(10);
}

#ifdef _MEM_CHECK_
enable_mem_log_termination();
#endif

if (global_data->startup_script || global_data->shutdown_script) {
/* This is a workaround for the check in check_start_stop_script_secure() */
main_pid = getppid();

magic = ka_magic_open();
script_flags = 0;
if (global_data->startup_script)
Expand Down

0 comments on commit 7b5c03f

Please sign in to comment.