virNetDaemonAutoShutdown: Allow live update of shutdown timeout

Modify the code so that calling 'virNetDaemonAutoShutdown' will update
the auto shutdown timeout also for running daemons.

This involves changing the logic when to do the update of the timer so
that it can be called from both when the daemon is not yet runnign and
when doing a live update.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
This commit is contained in:
Peter Krempa 2022-06-13 14:25:06 +02:00
parent fbc18725f2
commit c53e0c9535
5 changed files with 29 additions and 18 deletions

View File

@ -1049,9 +1049,8 @@ int main(int argc, char **argv) {
} }
if (timeout > 0) { if (timeout > 0) {
VIR_DEBUG("Registering shutdown timeout %d", timeout); if (virNetDaemonAutoShutdown(lockDaemon->dmn, timeout) < 0)
virNetDaemonAutoShutdown(lockDaemon->dmn, goto cleanup;
timeout);
} }
if ((virLockDaemonSetupSignals(lockDaemon->dmn)) < 0) { if ((virLockDaemonSetupSignals(lockDaemon->dmn)) < 0) {

View File

@ -854,9 +854,8 @@ int main(int argc, char **argv) {
} }
if (timeout > 0) { if (timeout > 0) {
VIR_DEBUG("Registering shutdown timeout %d", timeout); if (virNetDaemonAutoShutdown(logDaemon->dmn, timeout) < 0)
virNetDaemonAutoShutdown(logDaemon->dmn, return -1;
timeout);
} }
if ((virLogDaemonSetupSignals(logDaemon->dmn)) < 0) { if ((virLogDaemonSetupSignals(logDaemon->dmn)) < 0) {

View File

@ -1123,8 +1123,8 @@ int main(int argc, char **argv) {
} }
if (timeout > 0) { if (timeout > 0) {
VIR_DEBUG("Registering shutdown timeout %d", timeout); if (virNetDaemonAutoShutdown(dmn, timeout) < 0)
virNetDaemonAutoShutdown(dmn, timeout); goto cleanup;
} }
if ((daemonSetupSignals(dmn)) < 0) { if ((daemonSetupSignals(dmn)) < 0) {

View File

@ -73,6 +73,7 @@ struct _virNetDaemon {
bool finished; bool finished;
bool graceful; bool graceful;
bool execRestart; bool execRestart;
bool running; /* the daemon has reached the running phase */
unsigned int autoShutdownTimeout; unsigned int autoShutdownTimeout;
int autoShutdownTimerID; int autoShutdownTimerID;
@ -422,7 +423,7 @@ virNetDaemonAutoShutdownTimer(int timerid G_GNUC_UNUSED,
static int static int
virNetDaemonShutdownTimerRegister(virNetDaemon *dmn) virNetDaemonShutdownTimerRegister(virNetDaemon *dmn)
{ {
if (dmn->autoShutdownTimeout == 0) if (dmn->autoShutdownTimerID != -1)
return 0; return 0;
if ((dmn->autoShutdownTimerID = virEventAddTimeout(-1, if ((dmn->autoShutdownTimerID = virEventAddTimeout(-1,
@ -440,7 +441,7 @@ virNetDaemonShutdownTimerRegister(virNetDaemon *dmn)
static void static void
virNetDaemonShutdownTimerUpdate(virNetDaemon *dmn) virNetDaemonShutdownTimerUpdate(virNetDaemon *dmn)
{ {
if (dmn->autoShutdownTimeout == 0) if (dmn->autoShutdownTimerID == -1)
return; return;
/* A shutdown timeout is specified, so check /* A shutdown timeout is specified, so check
@ -448,13 +449,15 @@ virNetDaemonShutdownTimerUpdate(virNetDaemon *dmn)
* shutdown after timeout seconds * shutdown after timeout seconds
*/ */
if (dmn->autoShutdownTimerActive) { if (dmn->autoShutdownTimerActive) {
if (virNetDaemonHasClients(dmn)) { if (virNetDaemonHasClients(dmn) ||
dmn->autoShutdownTimeout == 0) {
VIR_DEBUG("Deactivating shutdown timer %d", dmn->autoShutdownTimerID); VIR_DEBUG("Deactivating shutdown timer %d", dmn->autoShutdownTimerID);
virEventUpdateTimeout(dmn->autoShutdownTimerID, -1); virEventUpdateTimeout(dmn->autoShutdownTimerID, -1);
dmn->autoShutdownTimerActive = false; dmn->autoShutdownTimerActive = false;
} }
} else { } else {
if (!virNetDaemonHasClients(dmn)) { if (!virNetDaemonHasClients(dmn) &&
dmn->autoShutdownTimeout != 0) {
VIR_DEBUG("Activating shutdown timer %d", dmn->autoShutdownTimerID); VIR_DEBUG("Activating shutdown timer %d", dmn->autoShutdownTimerID);
virEventUpdateTimeout(dmn->autoShutdownTimerID, virEventUpdateTimeout(dmn->autoShutdownTimerID,
dmn->autoShutdownTimeout * 1000); dmn->autoShutdownTimeout * 1000);
@ -464,13 +467,25 @@ virNetDaemonShutdownTimerUpdate(virNetDaemon *dmn)
} }
void int
virNetDaemonAutoShutdown(virNetDaemon *dmn, virNetDaemonAutoShutdown(virNetDaemon *dmn,
unsigned int timeout) unsigned int timeout)
{ {
VIR_LOCK_GUARD lock = virObjectLockGuard(dmn); 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; dmn->autoShutdownTimeout = timeout;
if (dmn->running)
virNetDaemonShutdownTimerUpdate(dmn);
return 0;
} }
@ -809,9 +824,7 @@ virNetDaemonRun(virNetDaemon *dmn)
dmn->finishTimer = -1; dmn->finishTimer = -1;
dmn->finished = false; dmn->finished = false;
dmn->graceful = false; dmn->graceful = false;
dmn->running = true;
if (virNetDaemonShutdownTimerRegister(dmn) < 0)
goto cleanup;
/* We are accepting connections now. Notify systemd /* We are accepting connections now. Notify systemd
* so it can start dependent services. */ * so it can start dependent services. */

View File

@ -46,8 +46,8 @@ virJSONValue *virNetDaemonPreExecRestart(virNetDaemon *dmn);
bool virNetDaemonIsPrivileged(virNetDaemon *dmn); bool virNetDaemonIsPrivileged(virNetDaemon *dmn);
void virNetDaemonAutoShutdown(virNetDaemon *dmn, int virNetDaemonAutoShutdown(virNetDaemon *dmn,
unsigned int timeout); unsigned int timeout) G_GNUC_WARN_UNUSED_RESULT;
void virNetDaemonAddShutdownInhibition(virNetDaemon *dmn); void virNetDaemonAddShutdownInhibition(virNetDaemon *dmn);
void virNetDaemonRemoveShutdownInhibition(virNetDaemon *dmn); void virNetDaemonRemoveShutdownInhibition(virNetDaemon *dmn);