|
|
2cf05b |
From 9d0247153a70ab1909d0690ec9b7f4d20e8cb602 Mon Sep 17 00:00:00 2001
|
|
|
2cf05b |
Message-Id: <9d0247153a70ab1909d0690ec9b7f4d20e8cb602@dist-git>
|
|
|
2cf05b |
From: Vasiliy Ulyanov <vulyanov@suse.de>
|
|
|
2cf05b |
Date: Wed, 2 Feb 2022 17:28:15 +0100
|
|
|
2cf05b |
Subject: [PATCH] virpidfile: Add virPidFileReadPathIfLocked func
|
|
|
2cf05b |
|
|
|
2cf05b |
The function will attempt to read a pid from @path, and store it in
|
|
|
2cf05b |
@pid. The @pid will only be set, however, if @path is locked by
|
|
|
2cf05b |
virFileLock() at byte 0 and the pid in @path is running.
|
|
|
2cf05b |
|
|
|
2cf05b |
Signed-off-by: Vasiliy Ulyanov <vulyanov@suse.de>
|
|
|
2cf05b |
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
|
|
|
2cf05b |
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
|
|
|
2cf05b |
(cherry picked from commit 013ab22f79d1345daf6b2778ca498acb16939011)
|
|
|
2cf05b |
Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=2152188
|
|
|
2cf05b |
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
|
|
|
2cf05b |
---
|
|
|
2cf05b |
src/libvirt_private.syms | 1 +
|
|
|
2cf05b |
src/util/virpidfile.c | 35 +++++++++++++++++++++++++++++++++++
|
|
|
2cf05b |
src/util/virpidfile.h | 2 ++
|
|
|
2cf05b |
3 files changed, 38 insertions(+)
|
|
|
2cf05b |
|
|
|
2cf05b |
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
|
|
|
2cf05b |
index fa734dfd33..568b0f34a1 100644
|
|
|
2cf05b |
--- a/src/libvirt_private.syms
|
|
|
2cf05b |
+++ b/src/libvirt_private.syms
|
|
|
2cf05b |
@@ -3061,6 +3061,7 @@ virPidFileRead;
|
|
|
2cf05b |
virPidFileReadIfAlive;
|
|
|
2cf05b |
virPidFileReadPath;
|
|
|
2cf05b |
virPidFileReadPathIfAlive;
|
|
|
2cf05b |
+virPidFileReadPathIfLocked;
|
|
|
2cf05b |
virPidFileRelease;
|
|
|
2cf05b |
virPidFileReleasePath;
|
|
|
2cf05b |
virPidFileWrite;
|
|
|
2cf05b |
diff --git a/src/util/virpidfile.c b/src/util/virpidfile.c
|
|
|
2cf05b |
index 7069f8343d..9d194f7336 100644
|
|
|
2cf05b |
--- a/src/util/virpidfile.c
|
|
|
2cf05b |
+++ b/src/util/virpidfile.c
|
|
|
2cf05b |
@@ -302,6 +302,41 @@ int virPidFileReadIfAlive(const char *dir,
|
|
|
2cf05b |
return 0;
|
|
|
2cf05b |
}
|
|
|
2cf05b |
|
|
|
2cf05b |
+/**
|
|
|
2cf05b |
+ * virPidFileReadPathIfLocked:
|
|
|
2cf05b |
+ * @path: path to pidfile
|
|
|
2cf05b |
+ * @pid: variable to return pid in
|
|
|
2cf05b |
+ *
|
|
|
2cf05b |
+ * This will attempt to read a pid from @path, and store it in
|
|
|
2cf05b |
+ * @pid. The @pid will only be set, however, if the pid in @path
|
|
|
2cf05b |
+ * is running, and @path is locked by virFileLock() at byte 0
|
|
|
2cf05b |
+ * (which is exactly what virCommandSetPidFile() results in).
|
|
|
2cf05b |
+ * This adds protection against returning a stale pid.
|
|
|
2cf05b |
+ *
|
|
|
2cf05b |
+ * Returns -1 upon error, or zero on successful
|
|
|
2cf05b |
+ * reading of the pidfile. If @path is not locked
|
|
|
2cf05b |
+ * or if the PID was not still alive, zero will
|
|
|
2cf05b |
+ * be returned, but @pid will be set to -1.
|
|
|
2cf05b |
+ */
|
|
|
2cf05b |
+int virPidFileReadPathIfLocked(const char *path, pid_t *pid)
|
|
|
2cf05b |
+{
|
|
|
2cf05b |
+ VIR_AUTOCLOSE fd = -1;
|
|
|
2cf05b |
+
|
|
|
2cf05b |
+ if ((fd = open(path, O_RDWR)) < 0)
|
|
|
2cf05b |
+ return -1;
|
|
|
2cf05b |
+
|
|
|
2cf05b |
+ if (virFileLock(fd, false, 0, 1, false) >= 0) {
|
|
|
2cf05b |
+ /* The file isn't locked. PID is stale. */
|
|
|
2cf05b |
+ *pid = -1;
|
|
|
2cf05b |
+ return 0;
|
|
|
2cf05b |
+ }
|
|
|
2cf05b |
+
|
|
|
2cf05b |
+ if (virPidFileReadPathIfAlive(path, pid, NULL) < 0)
|
|
|
2cf05b |
+ return -1;
|
|
|
2cf05b |
+
|
|
|
2cf05b |
+ return 0;
|
|
|
2cf05b |
+}
|
|
|
2cf05b |
+
|
|
|
2cf05b |
|
|
|
2cf05b |
int virPidFileDeletePath(const char *pidfile)
|
|
|
2cf05b |
{
|
|
|
2cf05b |
diff --git a/src/util/virpidfile.h b/src/util/virpidfile.h
|
|
|
2cf05b |
index fd8013c41e..e84542f298 100644
|
|
|
2cf05b |
--- a/src/util/virpidfile.h
|
|
|
2cf05b |
+++ b/src/util/virpidfile.h
|
|
|
2cf05b |
@@ -48,6 +48,8 @@ int virPidFileReadIfAlive(const char *dir,
|
|
|
2cf05b |
const char *name,
|
|
|
2cf05b |
pid_t *pid,
|
|
|
2cf05b |
const char *binpath) G_GNUC_WARN_UNUSED_RESULT;
|
|
|
2cf05b |
+int virPidFileReadPathIfLocked(const char *path,
|
|
|
2cf05b |
+ pid_t *pid) G_GNUC_WARN_UNUSED_RESULT;
|
|
|
2cf05b |
|
|
|
2cf05b |
int virPidFileDeletePath(const char *path);
|
|
|
2cf05b |
int virPidFileDelete(const char *dir,
|
|
|
2cf05b |
--
|
|
|
2cf05b |
2.39.0
|
|
|
2cf05b |
|