mirror of
https://github.com/libvirt/libvirt.git
synced 2025-02-25 18:55:26 -06:00
virnetdaemon: Extract autoShutdownTimer operations from virNetDaemonRun
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>
This commit is contained in:
parent
cc161c26d0
commit
fbc18725f2
@ -75,6 +75,8 @@ struct _virNetDaemon {
|
|||||||
bool execRestart;
|
bool execRestart;
|
||||||
|
|
||||||
unsigned int autoShutdownTimeout;
|
unsigned int autoShutdownTimeout;
|
||||||
|
int autoShutdownTimerID;
|
||||||
|
bool autoShutdownTimerActive;
|
||||||
size_t autoShutdownInhibitions;
|
size_t autoShutdownInhibitions;
|
||||||
int autoShutdownInhibitFd;
|
int autoShutdownInhibitFd;
|
||||||
};
|
};
|
||||||
@ -151,6 +153,8 @@ virNetDaemonNew(void)
|
|||||||
if (virEventRegisterDefaultImpl() < 0)
|
if (virEventRegisterDefaultImpl() < 0)
|
||||||
goto error;
|
goto error;
|
||||||
|
|
||||||
|
dmn->autoShutdownTimerID = -1;
|
||||||
|
|
||||||
#ifndef WIN32
|
#ifndef WIN32
|
||||||
memset(&sig_action, 0, sizeof(sig_action));
|
memset(&sig_action, 0, sizeof(sig_action));
|
||||||
sig_action.sa_handler = SIG_IGN;
|
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
|
void
|
||||||
virNetDaemonAutoShutdown(virNetDaemon *dmn,
|
virNetDaemonAutoShutdown(virNetDaemon *dmn,
|
||||||
unsigned int timeout)
|
unsigned int timeout)
|
||||||
@ -655,19 +718,6 @@ virNetDaemonAddSignalHandler(virNetDaemon *dmn G_GNUC_UNUSED,
|
|||||||
#endif /* WIN32 */
|
#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
|
static int
|
||||||
daemonServerUpdateServices(void *payload,
|
daemonServerUpdateServices(void *payload,
|
||||||
const char *key G_GNUC_UNUSED,
|
const char *key G_GNUC_UNUSED,
|
||||||
@ -741,11 +791,10 @@ virNetDaemonFinishTimer(int timerid G_GNUC_UNUSED,
|
|||||||
dmn->finished = true;
|
dmn->finished = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void
|
void
|
||||||
virNetDaemonRun(virNetDaemon *dmn)
|
virNetDaemonRun(virNetDaemon *dmn)
|
||||||
{
|
{
|
||||||
int timerid = -1;
|
|
||||||
bool timerActive = false;
|
|
||||||
virThread shutdownThread;
|
virThread shutdownThread;
|
||||||
|
|
||||||
virObjectLock(dmn);
|
virObjectLock(dmn);
|
||||||
@ -761,14 +810,8 @@ virNetDaemonRun(virNetDaemon *dmn)
|
|||||||
dmn->finished = false;
|
dmn->finished = false;
|
||||||
dmn->graceful = false;
|
dmn->graceful = false;
|
||||||
|
|
||||||
if (dmn->autoShutdownTimeout &&
|
if (virNetDaemonShutdownTimerRegister(dmn) < 0)
|
||||||
(timerid = virEventAddTimeout(-1,
|
|
||||||
virNetDaemonAutoShutdownTimer,
|
|
||||||
dmn, NULL)) < 0) {
|
|
||||||
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
||||||
_("Failed to register shutdown timeout"));
|
|
||||||
goto cleanup;
|
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. */
|
||||||
@ -776,26 +819,7 @@ virNetDaemonRun(virNetDaemon *dmn)
|
|||||||
|
|
||||||
VIR_DEBUG("dmn=%p quit=%d", dmn, dmn->quit);
|
VIR_DEBUG("dmn=%p quit=%d", dmn, dmn->quit);
|
||||||
while (!dmn->finished) {
|
while (!dmn->finished) {
|
||||||
/* A shutdown timeout is specified, so check
|
virNetDaemonShutdownTimerUpdate(dmn);
|
||||||
* 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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
virObjectUnlock(dmn);
|
virObjectUnlock(dmn);
|
||||||
if (virEventRunDefaultImpl() < 0) {
|
if (virEventRunDefaultImpl() < 0) {
|
||||||
|
Loading…
Reference in New Issue
Block a user