diff --git a/src/rpc/virnetdaemon.c b/src/rpc/virnetdaemon.c index 3c4adb2ca2..9355c7a5fa 100644 --- a/src/rpc/virnetdaemon.c +++ b/src/rpc/virnetdaemon.c @@ -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) {