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(a)redhat.com>
---
src/rpc/virnetdaemon.c | 108 +++++++++++++++++++++++++----------------
1 file changed, 66 insertions(+), 42 deletions(-)
diff --git a/src/rpc/virnetdaemon.c b/src/rpc/virnetdaemon.c
index 1f32bae35f..7bf27eed9d 100644
--- a/src/rpc/virnetdaemon.c
+++ b/src/rpc/virnetdaemon.c
@@ -77,6 +77,8 @@ struct _virNetDaemon {
bool execRestart;
unsigned int autoShutdownTimeout;
+ int autoShutdownTimerID;
+ bool autoShutdownTimerActive;
size_t autoShutdownInhibitions;
int autoShutdownInhibitFd;
};
@@ -153,6 +155,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;
@@ -403,6 +407,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)
@@ -657,19 +720,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,
@@ -743,11 +793,10 @@ virNetDaemonFinishTimer(int timerid G_GNUC_UNUSED,
dmn->finished = true;
}
+
void
virNetDaemonRun(virNetDaemon *dmn)
{
- int timerid = -1;
- bool timerActive = false;
virThread shutdownThread;
virObjectLock(dmn);
@@ -763,14 +812,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. */
@@ -778,26 +821,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) {
--
2.36.1