[PATCH v2 2/4] virpidfile: Refactor virPidFileReadPathIfAlive

Vasiliy Ulyanov vulyanov at suse.de
Thu Jan 13 12:42:36 UTC 2022


If the binary path is not provided check that the pid file is locked by
the owner process.

Signed-off-by: Vasiliy Ulyanov <vulyanov at suse.de>
---
 src/util/virpidfile.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)

diff --git a/src/util/virpidfile.c b/src/util/virpidfile.c
index 7069f8343d..8ddc336d6c 100644
--- a/src/util/virpidfile.c
+++ b/src/util/virpidfile.c
@@ -216,6 +216,26 @@ int virPidFileReadPathIfAlive(const char *path,
 #endif
 
     if (!binPath) {
+        int fd;
+        pid_t ownerPid;
+
+        if ((fd = open(path, O_RDONLY)) < 0)
+            return -1;
+
+        if (virFileGetLockOwner(fd, 0, 1, &ownerPid) < 0) {
+            VIR_FORCE_CLOSE(fd);
+            return -1;
+        }
+
+        if (VIR_CLOSE(fd) < 0)
+            return -1;
+
+        /* A pid file should be locked by the process owning it. */
+        if (ownerPid != retPid) {
+            *pid = -1;
+            return 0;
+        }
+
         /* we only knew the pid, and that pid is alive, so we can
          * return it.
          */
-- 
2.34.1





More information about the libvir-list mailing list