]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
util: add quiet parameter to virPidFileAcquirePathFull
authorJán Tomko <jtomko@redhat.com>
Tue, 7 Mar 2023 14:43:40 +0000 (15:43 +0100)
committerJán Tomko <jtomko@redhat.com>
Wed, 8 Mar 2023 11:16:56 +0000 (12:16 +0100)
Signed-off-by: Ján Tomko <jtomko@redhat.com>
Reviewed-by: Martin Kletzander <mkletzan@redhat.com>
src/network/leaseshelper.c
src/util/virpidfile.c
src/util/virpidfile.h

index b1ce313e6a2cc78a6671d4ca44753239db27cff1..9ee42045d90213bc6c2529146afb12d3a863e49b 100644 (file)
@@ -159,7 +159,7 @@ main(int argc, char **argv)
     pid_file = g_strdup(RUNSTATEDIR "/leaseshelper.pid");
 
     /* Try to claim the pidfile, exiting if we can't */
-    if ((pid_file_fd = virPidFileAcquirePathFull(pid_file, true, getpid())) < 0) {
+    if ((pid_file_fd = virPidFileAcquirePathFull(pid_file, true, false, getpid())) < 0) {
         fprintf(stderr,
                 _("Unable to acquire PID file: %s\n errno=%d"),
                 pid_file, errno);
index 252100cdc3f8904796df17cccd89adbf96fbd1ec..05d19100c6f01ddeedb549fd2812d0bff7112016 100644 (file)
@@ -364,6 +364,7 @@ int virPidFileDelete(const char *dir,
 
 int virPidFileAcquirePathFull(const char *path,
                               bool waitForLock,
+                              bool quiet,
                               pid_t pid)
 {
     int fd = -1;
@@ -375,32 +376,40 @@ int virPidFileAcquirePathFull(const char *path,
     while (1) {
         struct stat a, b;
         if ((fd = open(path, O_WRONLY|O_CREAT, 0644)) < 0) {
-            virReportSystemError(errno,
-                                 _("Failed to open pid file '%s'"),
-                                 path);
+            if (!quiet) {
+                virReportSystemError(errno,
+                                     _("Failed to open pid file '%s'"),
+                                     path);
+            }
             return -1;
         }
 
         if (virSetCloseExec(fd) < 0) {
-            virReportSystemError(errno,
-                                 _("Failed to set close-on-exec flag '%s'"),
-                                 path);
+            if (!quiet) {
+                virReportSystemError(errno,
+                                     _("Failed to set close-on-exec flag '%s'"),
+                                     path);
+            }
             VIR_FORCE_CLOSE(fd);
             return -1;
         }
 
         if (fstat(fd, &b) < 0) {
-            virReportSystemError(errno,
-                                 _("Unable to check status of pid file '%s'"),
-                                 path);
+            if (!quiet) {
+                virReportSystemError(errno,
+                                     _("Unable to check status of pid file '%s'"),
+                                     path);
+            }
             VIR_FORCE_CLOSE(fd);
             return -1;
         }
 
         if (virFileLock(fd, false, 0, 1, waitForLock) < 0) {
-            virReportSystemError(errno,
-                                 _("Failed to acquire pid file '%s'"),
-                                 path);
+            if (!quiet) {
+                virReportSystemError(errno,
+                                     _("Failed to acquire pid file '%s'"),
+                                     path);
+            }
             VIR_FORCE_CLOSE(fd);
             return -1;
         }
@@ -427,17 +436,21 @@ int virPidFileAcquirePathFull(const char *path,
     g_snprintf(pidstr, sizeof(pidstr), "%lld", (long long) pid);
 
     if (ftruncate(fd, 0) < 0) {
-        virReportSystemError(errno,
-                             _("Failed to truncate pid file '%s'"),
-                             path);
+        if (!quiet) {
+            virReportSystemError(errno,
+                                 _("Failed to truncate pid file '%s'"),
+                                 path);
+        }
         VIR_FORCE_CLOSE(fd);
         return -1;
     }
 
     if (safewrite(fd, pidstr, strlen(pidstr)) < 0) {
-        virReportSystemError(errno,
-                             _("Failed to write to pid file '%s'"),
-                             path);
+        if (!quiet) {
+            virReportSystemError(errno,
+                                 _("Failed to write to pid file '%s'"),
+                                 path);
+        }
         VIR_FORCE_CLOSE(fd);
     }
 
@@ -448,7 +461,7 @@ int virPidFileAcquirePathFull(const char *path,
 int virPidFileAcquirePath(const char *path,
                           pid_t pid)
 {
-    return virPidFileAcquirePathFull(path, false, pid);
+    return virPidFileAcquirePathFull(path, false, false, pid);
 }
 
 
index 6db0fb843f42b2c9c97deb40a49562ece1c96a10..5d31f02702a21a1b0c3f78a33132b2fff28fe44d 100644 (file)
@@ -58,6 +58,7 @@ int virPidFileDelete(const char *dir,
 
 int virPidFileAcquirePathFull(const char *path,
                               bool waitForLock,
+                              bool quiet,
                               pid_t pid) G_GNUC_WARN_UNUSED_RESULT;
 int virPidFileAcquirePath(const char *path,
                           pid_t pid) G_GNUC_WARN_UNUSED_RESULT;