]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
util: Introduce virPidFileForceCleanupPath
authorMartin Kletzander <mkletzan@redhat.com>
Sun, 12 Oct 2014 11:40:36 +0000 (13:40 +0200)
committerCole Robinson <crobinso@redhat.com>
Sat, 15 Nov 2014 21:02:07 +0000 (16:02 -0500)
This function is used to cleanup a pidfile doing whatever it takes, even
killing the owning process.

Signed-off-by: Martin Kletzander <mkletzan@redhat.com>
(cherry picked from commit d1fd086eb41d5505b7dc595abbf6027b9428071b)

src/libvirt_private.syms
src/util/virpidfile.c
src/util/virpidfile.h

index 9b98d4c86da304304d619f5f01367b573d52a406..ce14d78bb6b8198e1d15586d6f8d2d365d6bbeb9 100644 (file)
@@ -1805,6 +1805,7 @@ virPidFileBuildPath;
 virPidFileConstructPath;
 virPidFileDelete;
 virPidFileDeletePath;
+virPidFileForceCleanupPath;
 virPidFileRead;
 virPidFileReadIfAlive;
 virPidFileReadPath;
index a3b8846aae44d7924ffe046baaa6118af5c1883c..098458f7eb2e75e3245f6dcabfb8a52e60134afb 100644 (file)
@@ -37,6 +37,7 @@
 #include "c-ctype.h"
 #include "areadlink.h"
 #include "virstring.h"
+#include "virprocess.h"
 
 #define VIR_FROM_THIS VIR_FROM_NONE
 
@@ -567,3 +568,44 @@ virPidFileConstructPath(bool privileged,
     VIR_FREE(rundir);
     return ret;
 }
+
+
+/**
+ * virPidFileForceCleanupPath:
+ *
+ * Check if the pidfile is left around and clean it up whatever it
+ * takes.  This doesn't raise an error.  This function must not be
+ * called multiple times with the same path, be it in threads or
+ * processes.  This function does not raise any errors.
+ *
+ * Returns 0 if the pidfile was successfully cleaned up, -1 otherwise.
+ */
+int
+virPidFileForceCleanupPath(const char *path)
+{
+    pid_t pid = 0;
+    int fd = -1;
+
+    if (!virFileExists(path))
+        return 0;
+
+    if (virPidFileReadPath(path, &pid) < 0)
+        return -1;
+
+    if (virPidFileAcquirePath(path, false, 0) == 0) {
+        virPidFileReleasePath(path, fd);
+    } else {
+        virResetLastError();
+
+        /* Only kill the process if the pid is valid one.  0 means
+         * there is somebody else doing the same pidfile cleanup
+         * machinery. */
+        if (pid)
+            virProcessKillPainfully(pid, true);
+
+        if (virPidFileDeletePath(path) < 0)
+            return -1;
+    }
+
+    return 0;
+}
index ca1dbff2e38099aceb43729bd210a789a4e85484..eb6516ca1503bec54c0a6026a411507441ed1985 100644 (file)
@@ -74,4 +74,6 @@ int virPidFileConstructPath(bool privileged,
                             const char *progname,
                             char **pidfile);
 
+int virPidFileForceCleanupPath(const char *path) ATTRIBUTE_NONNULL(1);
+
 #endif /* __VIR_PIDFILE_H__ */