Skip to content

Commit

Permalink
qsd: Unlink absolute PID file path
Browse files Browse the repository at this point in the history
After writing the PID file, we register an atexit() handler to unlink it
when the process terminates.  However, if the process has changed its
working directory in the meantime (e.g. in os_setup_post() when
daemonizing), this will not work when the PID file path was relative.
Therefore, pass the absolute path (created with realpath()) to the
unlink() call in the atexit() handler.

(realpath() needs a path pointing to an existing file, so we cannot use
it before qemu_write_pidfile().)

Reproducer:
$ cd /tmp
$ qemu-storage-daemon --daemonize --pidfile qsd.pid
$ file qsd.pid
qsd.pid: ASCII text
$ kill $(cat qsd.pid)
$ file qsd.pid
qsd.pid: ASCII text

(qsd.pid should be gone after the process has terminated.)

Buglink: https://bugzilla.redhat.com/show_bug.cgi?id=2092322
Signed-off-by: Hanna Reitz <[email protected]>
Message-Id: <[email protected]>
Reviewed-by: Daniel P. Berrangé <[email protected]>
  • Loading branch information
XanClic committed Jul 12, 2022
1 parent 9907dba commit 9d8f823
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion storage-daemon/qemu-storage-daemon.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
#include "trace/control.h"

static const char *pid_file;
static char *pid_file_realpath;
static volatile bool exit_requested = false;

void qemu_system_killed(int signal, pid_t pid)
Expand Down Expand Up @@ -363,7 +364,7 @@ static void process_options(int argc, char *argv[], bool pre_init_pass)

static void pid_file_cleanup(void)
{
unlink(pid_file);
unlink(pid_file_realpath);
}

static void pid_file_init(void)
Expand All @@ -379,6 +380,14 @@ static void pid_file_init(void)
exit(EXIT_FAILURE);
}

pid_file_realpath = g_malloc(PATH_MAX);
if (!realpath(pid_file, pid_file_realpath)) {
error_report("cannot resolve PID file path: %s: %s",
pid_file, strerror(errno));
unlink(pid_file);
exit(EXIT_FAILURE);
}

atexit(pid_file_cleanup);
}

Expand Down

0 comments on commit 9d8f823

Please sign in to comment.