]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
virnetdaemon: Extract autoShutdownTimer operations from virNetDaemonRun
authorPeter Krempa <pkrempa@redhat.com>
Mon, 13 Jun 2022 11:58:13 +0000 (13:58 +0200)
committerPeter Krempa <pkrempa@redhat.com>
Thu, 7 Jul 2022 12:35:30 +0000 (14:35 +0200)
Introduce 'virNetDaemonShutdownTimerRegister' and
'virNetDaemonShutdownTimerUpdate' to aggregate the code to deal with the
auto-shutdown timer.

The code is also placed so that it can be called from
'virNetDaemonAutoShutdown' which involved the move of
'virNetDaemonAutoShutdownTimer'.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
src/rpc/virnetdaemon.c

index 3c4adb2ca2d574e779765da75e765414ce23b2b1..9355c7a5fa6fe159b71a37345fba9e4cdbff32e4 100644 (file)
@@ -75,6 +75,8 @@ struct _virNetDaemon {
     bool execRestart;
 
     unsigned int autoShutdownTimeout;
+    int autoShutdownTimerID;
+    bool autoShutdownTimerActive;
     size_t autoShutdownInhibitions;
     int autoShutdownInhibitFd;
 };
@@ -151,6 +153,8 @@ virNetDaemonNew(void)
     if (virEventRegisterDefaultImpl() < 0)
         goto error;
 
+    dmn->autoShutdownTimerID = -1;
+
 #ifndef WIN32
     memset(&sig_action, 0, sizeof(sig_action));
     sig_action.sa_handler = SIG_IGN;
@@ -401,6 +405,65 @@ virNetDaemonIsPrivileged(virNetDaemon *dmn)
 }
 
 
+static void
+virNetDaemonAutoShutdownTimer(int timerid G_GNUC_UNUSED,
+                              void *opaque)
+{
+    virNetDaemon *dmn = opaque;
+    VIR_LOCK_GUARD lock = virObjectLockGuard(dmn);
+
+    if (!dmn->autoShutdownInhibitions) {
+        VIR_DEBUG("Automatic shutdown triggered");
+        dmn->quit = true;
+    }
+}
+
+
+static int
+virNetDaemonShutdownTimerRegister(virNetDaemon *dmn)
+{
+    if (dmn->autoShutdownTimeout == 0)
+        return 0;
+
+    if ((dmn->autoShutdownTimerID = virEventAddTimeout(-1,
+                                                       virNetDaemonAutoShutdownTimer,
+                                                       dmn, NULL)) < 0) {
+        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+                       _("Failed to register shutdown timeout"));
+        return -1;
+    }
+
+    return 0;
+}
+
+
+static void
+virNetDaemonShutdownTimerUpdate(virNetDaemon *dmn)
+{
+    if (dmn->autoShutdownTimeout == 0)
+        return;
+
+    /* A shutdown timeout is specified, so check
+     * if any drivers have active state, if not
+     * shutdown after timeout seconds
+     */
+    if (dmn->autoShutdownTimerActive) {
+        if (virNetDaemonHasClients(dmn)) {
+            VIR_DEBUG("Deactivating shutdown timer %d", dmn->autoShutdownTimerID);
+            virEventUpdateTimeout(dmn->autoShutdownTimerID, -1);
+            dmn->autoShutdownTimerActive = false;
+        }
+    } else {
+        if (!virNetDaemonHasClients(dmn)) {
+            VIR_DEBUG("Activating shutdown timer %d", dmn->autoShutdownTimerID);
+            virEventUpdateTimeout(dmn->autoShutdownTimerID,
+                                  dmn->autoShutdownTimeout * 1000);
+            dmn->autoShutdownTimerActive = true;
+        }
+    }
+}
+
+
 void
 virNetDaemonAutoShutdown(virNetDaemon *dmn,
                          unsigned int timeout)
@@ -655,19 +718,6 @@ virNetDaemonAddSignalHandler(virNetDaemon *dmn G_GNUC_UNUSED,
 #endif /* WIN32 */
 
 
-static void
-virNetDaemonAutoShutdownTimer(int timerid G_GNUC_UNUSED,
-                              void *opaque)
-{
-    virNetDaemon *dmn = opaque;
-    VIR_LOCK_GUARD lock = virObjectLockGuard(dmn);
-
-    if (!dmn->autoShutdownInhibitions) {
-        VIR_DEBUG("Automatic shutdown triggered");
-        dmn->quit = true;
-    }
-}
-
 static int
 daemonServerUpdateServices(void *payload,
                            const char *key G_GNUC_UNUSED,
@@ -741,11 +791,10 @@ virNetDaemonFinishTimer(int timerid G_GNUC_UNUSED,
     dmn->finished = true;
 }
 
+
 void
 virNetDaemonRun(virNetDaemon *dmn)
 {
-    int timerid = -1;
-    bool timerActive = false;
     virThread shutdownThread;
 
     virObjectLock(dmn);
@@ -761,14 +810,8 @@ virNetDaemonRun(virNetDaemon *dmn)
     dmn->finished = false;
     dmn->graceful = false;
 
-    if (dmn->autoShutdownTimeout &&
-        (timerid = virEventAddTimeout(-1,
-                                      virNetDaemonAutoShutdownTimer,
-                                      dmn, NULL)) < 0) {
-        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
-                       _("Failed to register shutdown timeout"));
+    if (virNetDaemonShutdownTimerRegister(dmn) < 0)
         goto cleanup;
-    }
 
     /* We are accepting connections now. Notify systemd
      * so it can start dependent services. */
@@ -776,26 +819,7 @@ virNetDaemonRun(virNetDaemon *dmn)
 
     VIR_DEBUG("dmn=%p quit=%d", dmn, dmn->quit);
     while (!dmn->finished) {
-        /* A shutdown timeout is specified, so check
-         * if any drivers have active state, if not
-         * shutdown after timeout seconds
-         */
-        if (dmn->autoShutdownTimeout) {
-            if (timerActive) {
-                if (virNetDaemonHasClients(dmn)) {
-                    VIR_DEBUG("Deactivating shutdown timer %d", timerid);
-                    virEventUpdateTimeout(timerid, -1);
-                    timerActive = false;
-                }
-            } else {
-                if (!virNetDaemonHasClients(dmn)) {
-                    VIR_DEBUG("Activating shutdown timer %d", timerid);
-                    virEventUpdateTimeout(timerid,
-                                          dmn->autoShutdownTimeout * 1000);
-                    timerActive = true;
-                }
-            }
-        }
+        virNetDaemonShutdownTimerUpdate(dmn);
 
         virObjectUnlock(dmn);
         if (virEventRunDefaultImpl() < 0) {