diff --git a/src/locking/lock_daemon.c b/src/locking/lock_daemon.c index a83ea33b4f..c997d327c4 100644 --- a/src/locking/lock_daemon.c +++ b/src/locking/lock_daemon.c @@ -1049,9 +1049,8 @@ int main(int argc, char **argv) { } if (timeout > 0) { - VIR_DEBUG("Registering shutdown timeout %d", timeout); - virNetDaemonAutoShutdown(lockDaemon->dmn, - timeout); + if (virNetDaemonAutoShutdown(lockDaemon->dmn, timeout) < 0) + goto cleanup; } if ((virLockDaemonSetupSignals(lockDaemon->dmn)) < 0) { diff --git a/src/logging/log_daemon.c b/src/logging/log_daemon.c index 91ed119c7f..00fc4148fd 100644 --- a/src/logging/log_daemon.c +++ b/src/logging/log_daemon.c @@ -854,9 +854,8 @@ int main(int argc, char **argv) { } if (timeout > 0) { - VIR_DEBUG("Registering shutdown timeout %d", timeout); - virNetDaemonAutoShutdown(logDaemon->dmn, - timeout); + if (virNetDaemonAutoShutdown(logDaemon->dmn, timeout) < 0) + return -1; } if ((virLogDaemonSetupSignals(logDaemon->dmn)) < 0) { diff --git a/src/remote/remote_daemon.c b/src/remote/remote_daemon.c index 2e64e4da7f..23a5eeb200 100644 --- a/src/remote/remote_daemon.c +++ b/src/remote/remote_daemon.c @@ -1123,8 +1123,8 @@ int main(int argc, char **argv) { } if (timeout > 0) { - VIR_DEBUG("Registering shutdown timeout %d", timeout); - virNetDaemonAutoShutdown(dmn, timeout); + if (virNetDaemonAutoShutdown(dmn, timeout) < 0) + goto cleanup; } if ((daemonSetupSignals(dmn)) < 0) { diff --git a/src/rpc/virnetdaemon.c b/src/rpc/virnetdaemon.c index 9355c7a5fa..ac12d2d412 100644 --- a/src/rpc/virnetdaemon.c +++ b/src/rpc/virnetdaemon.c @@ -73,6 +73,7 @@ struct _virNetDaemon { bool finished; bool graceful; bool execRestart; + bool running; /* the daemon has reached the running phase */ unsigned int autoShutdownTimeout; int autoShutdownTimerID; @@ -422,7 +423,7 @@ virNetDaemonAutoShutdownTimer(int timerid G_GNUC_UNUSED, static int virNetDaemonShutdownTimerRegister(virNetDaemon *dmn) { - if (dmn->autoShutdownTimeout == 0) + if (dmn->autoShutdownTimerID != -1) return 0; if ((dmn->autoShutdownTimerID = virEventAddTimeout(-1, @@ -440,7 +441,7 @@ virNetDaemonShutdownTimerRegister(virNetDaemon *dmn) static void virNetDaemonShutdownTimerUpdate(virNetDaemon *dmn) { - if (dmn->autoShutdownTimeout == 0) + if (dmn->autoShutdownTimerID == -1) return; /* A shutdown timeout is specified, so check @@ -448,13 +449,15 @@ virNetDaemonShutdownTimerUpdate(virNetDaemon *dmn) * shutdown after timeout seconds */ if (dmn->autoShutdownTimerActive) { - if (virNetDaemonHasClients(dmn)) { + if (virNetDaemonHasClients(dmn) || + dmn->autoShutdownTimeout == 0) { VIR_DEBUG("Deactivating shutdown timer %d", dmn->autoShutdownTimerID); virEventUpdateTimeout(dmn->autoShutdownTimerID, -1); dmn->autoShutdownTimerActive = false; } } else { - if (!virNetDaemonHasClients(dmn)) { + if (!virNetDaemonHasClients(dmn) && + dmn->autoShutdownTimeout != 0) { VIR_DEBUG("Activating shutdown timer %d", dmn->autoShutdownTimerID); virEventUpdateTimeout(dmn->autoShutdownTimerID, dmn->autoShutdownTimeout * 1000); @@ -464,13 +467,25 @@ virNetDaemonShutdownTimerUpdate(virNetDaemon *dmn) } -void +int virNetDaemonAutoShutdown(virNetDaemon *dmn, unsigned int timeout) { VIR_LOCK_GUARD lock = virObjectLockGuard(dmn); + VIR_DEBUG("Registering shutdown timeout %u", timeout); + + if (timeout > 0) { + if (virNetDaemonShutdownTimerRegister(dmn) < 0) + return -1; + } + dmn->autoShutdownTimeout = timeout; + + if (dmn->running) + virNetDaemonShutdownTimerUpdate(dmn); + + return 0; } @@ -809,9 +824,7 @@ virNetDaemonRun(virNetDaemon *dmn) dmn->finishTimer = -1; dmn->finished = false; dmn->graceful = false; - - if (virNetDaemonShutdownTimerRegister(dmn) < 0) - goto cleanup; + dmn->running = true; /* We are accepting connections now. Notify systemd * so it can start dependent services. */ diff --git a/src/rpc/virnetdaemon.h b/src/rpc/virnetdaemon.h index d588f86799..31a355adb4 100644 --- a/src/rpc/virnetdaemon.h +++ b/src/rpc/virnetdaemon.h @@ -46,8 +46,8 @@ virJSONValue *virNetDaemonPreExecRestart(virNetDaemon *dmn); bool virNetDaemonIsPrivileged(virNetDaemon *dmn); -void virNetDaemonAutoShutdown(virNetDaemon *dmn, - unsigned int timeout); +int virNetDaemonAutoShutdown(virNetDaemon *dmn, + unsigned int timeout) G_GNUC_WARN_UNUSED_RESULT; void virNetDaemonAddShutdownInhibition(virNetDaemon *dmn); void virNetDaemonRemoveShutdownInhibition(virNetDaemon *dmn);