On 2/2/22 17:28, Vasiliy Ulyanov wrote:
The function will attempt to read a pid from @path, and store it in
@pid. The @pid will only be set, however, if @path is locked by
virFileLock() at byte 0 and the pid in @path is running.
Signed-off-by: Vasiliy Ulyanov <vulyanov(a)suse.de>
---
src/libvirt_private.syms | 1 +
src/util/virpidfile.c | 34 ++++++++++++++++++++++++++++++++++
src/util/virpidfile.h | 2 ++
3 files changed, 37 insertions(+)
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 9bc3d9530b..447ba9d82b 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -3070,6 +3070,7 @@ virPidFileRead;
virPidFileReadIfAlive;
virPidFileReadPath;
virPidFileReadPathIfAlive;
+virPidFileReadPathIfLocked;
virPidFileRelease;
virPidFileReleasePath;
virPidFileWrite;
diff --git a/src/util/virpidfile.c b/src/util/virpidfile.c
index 7069f8343d..b8bb455e5e 100644
--- a/src/util/virpidfile.c
+++ b/src/util/virpidfile.c
@@ -302,6 +302,40 @@ int virPidFileReadIfAlive(const char *dir,
return 0;
}
+/**
+ * virPidFileReadPathIfLocked:
+ * @path: path to pidfile
+ * @pid: variable to return pid in
+ *
+ * This will attempt to read a pid from @path, and store it
+ * in @pid. The @pid will only be set, however, if the
+ * pid in @path is running, and @path is locked by virFileLock()
+ * at byte 0. This adds protection against returning a stale pid.
I'd mention here virCommandSetPidFile(), but otherwise looking good.
Michal