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