1
0

lxc: Use automatic mutex management

Signed-off-by: Tim Wiederhake <twiederh@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
This commit is contained in:
Tim Wiederhake 2022-02-04 16:38:10 +01:00
parent 370fc9f47c
commit 241c13a7e5
4 changed files with 30 additions and 47 deletions

View File

@ -159,24 +159,24 @@ virCaps *virLXCDriverGetCapabilities(virLXCDriver *driver,
virCaps *ret = NULL; virCaps *ret = NULL;
virCaps *caps = NULL; virCaps *caps = NULL;
lxcDriverLock(driver); VIR_WITH_MUTEX_LOCK_GUARD(&driver->lock) {
if (!refresh && !driver->caps) { if (!refresh && !driver->caps) {
VIR_DEBUG("Capabilities didn't detect any guests. Forcing a refresh."); VIR_DEBUG("Capabilities didn't detect any guests. Forcing a refresh.");
refresh = true; refresh = true;
}
} }
lxcDriverUnlock(driver);
if (refresh && !(caps = virLXCDriverCapsInit(driver))) if (refresh && !(caps = virLXCDriverCapsInit(driver)))
return NULL; return NULL;
lxcDriverLock(driver); VIR_WITH_MUTEX_LOCK_GUARD(&driver->lock) {
if (refresh) { if (refresh) {
virObjectUnref(driver->caps); virObjectUnref(driver->caps);
driver->caps = caps; driver->caps = caps;
} }
ret = virObjectRef(driver->caps); ret = virObjectRef(driver->caps);
lxcDriverUnlock(driver); }
return ret; return ret;
} }
@ -248,11 +248,8 @@ virLXCLoadDriverConfig(virLXCDriverConfig *cfg,
virLXCDriverConfig *virLXCDriverGetConfig(virLXCDriver *driver) virLXCDriverConfig *virLXCDriverGetConfig(virLXCDriver *driver)
{ {
virLXCDriverConfig *cfg; VIR_LOCK_GUARD lock = virLockGuardLock(&driver->lock);
lxcDriverLock(driver); return virObjectRef(driver->config);
cfg = virObjectRef(driver->config);
lxcDriverUnlock(driver);
return cfg;
} }
static void static void

View File

@ -113,12 +113,3 @@ virCaps *virLXCDriverGetCapabilities(virLXCDriver *driver,
bool refresh); bool refresh);
virDomainXMLOption *lxcDomainXMLConfInit(virLXCDriver *driver, virDomainXMLOption *lxcDomainXMLConfInit(virLXCDriver *driver,
const char *defsecmodel); const char *defsecmodel);
static inline void lxcDriverLock(virLXCDriver *driver)
{
virMutexLock(&driver->lock);
}
static inline void lxcDriverUnlock(virLXCDriver *driver)
{
virMutexUnlock(&driver->lock);
}

View File

@ -992,7 +992,7 @@ static int lxcControllerClearCapabilities(void)
} }
static bool wantReboot; static bool wantReboot;
static virMutex lock = VIR_MUTEX_INITIALIZER; static virMutex mutex = VIR_MUTEX_INITIALIZER;
static int static int
virLXCControllerEventSendExit(virLXCController *ctrl, virLXCControllerEventSendExit(virLXCController *ctrl,
@ -1009,13 +1009,13 @@ static void virLXCControllerSignalChildIO(virNetDaemon *dmn G_GNUC_UNUSED,
ret = waitpid(-1, &status, WNOHANG); ret = waitpid(-1, &status, WNOHANG);
VIR_DEBUG("Got sig child %d vs %lld", ret, (long long)ctrl->initpid); VIR_DEBUG("Got sig child %d vs %lld", ret, (long long)ctrl->initpid);
if (ret == ctrl->initpid) { if (ret == ctrl->initpid) {
virMutexLock(&lock); VIR_WITH_MUTEX_LOCK_GUARD(&mutex) {
if (WIFSIGNALED(status) && if (WIFSIGNALED(status) &&
WTERMSIG(status) == SIGHUP) { WTERMSIG(status) == SIGHUP) {
VIR_DEBUG("Status indicates reboot"); VIR_DEBUG("Status indicates reboot");
wantReboot = true; wantReboot = true;
}
} }
virMutexUnlock(&lock);
virLXCControllerEventSendExit(ctrl, wantReboot ? 1 : 0); virLXCControllerEventSendExit(ctrl, wantReboot ? 1 : 0);
} }
} }
@ -1129,8 +1129,8 @@ static void virLXCControllerConsoleUpdateWatch(virLXCControllerConsole *console)
static void virLXCControllerConsoleEPoll(int watch, int fd, int events, void *opaque) static void virLXCControllerConsoleEPoll(int watch, int fd, int events, void *opaque)
{ {
virLXCControllerConsole *console = opaque; virLXCControllerConsole *console = opaque;
VIR_LOCK_GUARD lock = virLockGuardLock(&mutex);
virMutexLock(&lock);
VIR_DEBUG("IO event watch=%d fd=%d events=%d fromHost=%zu fromcont=%zu", VIR_DEBUG("IO event watch=%d fd=%d events=%d fromHost=%zu fromcont=%zu",
watch, fd, events, watch, fd, events,
console->fromHostLen, console->fromHostLen,
@ -1146,7 +1146,7 @@ static void virLXCControllerConsoleEPoll(int watch, int fd, int events, void *op
virReportSystemError(errno, "%s", virReportSystemError(errno, "%s",
_("Unable to wait on epoll")); _("Unable to wait on epoll"));
virNetDaemonQuit(console->daemon); virNetDaemonQuit(console->daemon);
goto cleanup; return;
} }
if (ret == 0) if (ret == 0)
@ -1168,16 +1168,13 @@ static void virLXCControllerConsoleEPoll(int watch, int fd, int events, void *op
break; break;
} }
} }
cleanup:
virMutexUnlock(&lock);
} }
static void virLXCControllerConsoleIO(int watch, int fd, int events, void *opaque) static void virLXCControllerConsoleIO(int watch, int fd, int events, void *opaque)
{ {
virLXCControllerConsole *console = opaque; virLXCControllerConsole *console = opaque;
VIR_LOCK_GUARD lock = virLockGuardLock(&mutex);
virMutexLock(&lock);
VIR_DEBUG("IO event watch=%d fd=%d events=%d fromHost=%zu fromcont=%zu", VIR_DEBUG("IO event watch=%d fd=%d events=%d fromHost=%zu fromcont=%zu",
watch, fd, events, watch, fd, events,
console->fromHostLen, console->fromHostLen,
@ -1251,7 +1248,6 @@ static void virLXCControllerConsoleIO(int watch, int fd, int events, void *opaqu
} }
virLXCControllerConsoleUpdateWatch(console); virLXCControllerConsoleUpdateWatch(console);
virMutexUnlock(&lock);
return; return;
error: error:
@ -1259,7 +1255,6 @@ static void virLXCControllerConsoleIO(int watch, int fd, int events, void *opaqu
virEventRemoveHandle(console->hostWatch); virEventRemoveHandle(console->hostWatch);
console->contWatch = console->hostWatch = -1; console->contWatch = console->hostWatch = -1;
virNetDaemonQuit(console->daemon); virNetDaemonQuit(console->daemon);
virMutexUnlock(&lock);
} }

View File

@ -260,10 +260,10 @@ static struct fuse_operations lxcProcOper = {
static void lxcFuseDestroy(struct virLXCFuse *fuse) static void lxcFuseDestroy(struct virLXCFuse *fuse)
{ {
virMutexLock(&fuse->lock); VIR_LOCK_GUARD lock = virLockGuardLock(&fuse->lock);
fuse_unmount(fuse->mountpoint, fuse->ch); fuse_unmount(fuse->mountpoint, fuse->ch);
g_clear_pointer(&fuse->fuse, fuse_destroy); g_clear_pointer(&fuse->fuse, fuse_destroy);
virMutexUnlock(&fuse->lock);
} }
static void lxcFuseRun(void *opaque) static void lxcFuseRun(void *opaque)
@ -345,10 +345,10 @@ void lxcFreeFuse(struct virLXCFuse **f)
if (fuse) { if (fuse) {
/* exit fuse_loop, lxcFuseRun thread may try to destroy /* exit fuse_loop, lxcFuseRun thread may try to destroy
* fuse->fuse at the same time,so add a lock here. */ * fuse->fuse at the same time,so add a lock here. */
virMutexLock(&fuse->lock); VIR_WITH_MUTEX_LOCK_GUARD(&fuse->lock) {
if (fuse->fuse) if (fuse->fuse)
fuse_exit(fuse->fuse); fuse_exit(fuse->fuse);
virMutexUnlock(&fuse->lock); }
g_free(fuse->mountpoint); g_free(fuse->mountpoint);
g_free(*f); g_free(*f);