提交 59080d84 编写于 作者: D Daniel P. Berrangé

libxl: acquire a pidfile in the driver root directory

When we allow multiple instances of the driver for the same user
account, using a separate root directory, we need to ensure mutual
exclusion. Use a pidfile to guarantee this.

In privileged libvirtd this ends up locking

   /var/run/libvirt/libxl/driver.pid

In unprivileged libvirtd this ends up locking

  /run/user/$UID/libvirt/libxl/run/driver.pid

NB, the latter can vary depending on $XDG_RUNTIME_DIR
Signed-off-by: NDaniel P. Berrangé <berrange@redhat.com>
上级 defddd04
...@@ -111,6 +111,9 @@ struct _libxlDriverPrivate { ...@@ -111,6 +111,9 @@ struct _libxlDriverPrivate {
* then lockless thereafter */ * then lockless thereafter */
libxlDriverConfigPtr config; libxlDriverConfigPtr config;
/* pid file FD, ensures two copies of the driver can't use the same root */
int lockFD;
/* Atomic inc/dec only */ /* Atomic inc/dec only */
unsigned int nactive; unsigned int nactive;
......
...@@ -53,6 +53,7 @@ ...@@ -53,6 +53,7 @@
#include "viraccessapicheck.h" #include "viraccessapicheck.h"
#include "viratomic.h" #include "viratomic.h"
#include "virhostdev.h" #include "virhostdev.h"
#include "virpidfile.h"
#include "locking/domain_lock.h" #include "locking/domain_lock.h"
#include "virnetdevtap.h" #include "virnetdevtap.h"
#include "cpu/cpu.h" #include "cpu/cpu.h"
...@@ -506,7 +507,6 @@ libxlStateCleanup(void) ...@@ -506,7 +507,6 @@ libxlStateCleanup(void)
return -1; return -1;
virObjectUnref(libxl_driver->hostdevMgr); virObjectUnref(libxl_driver->hostdevMgr);
virObjectUnref(libxl_driver->config);
virObjectUnref(libxl_driver->xmlopt); virObjectUnref(libxl_driver->xmlopt);
virObjectUnref(libxl_driver->domains); virObjectUnref(libxl_driver->domains);
virPortAllocatorRangeFree(libxl_driver->reservedGraphicsPorts); virPortAllocatorRangeFree(libxl_driver->reservedGraphicsPorts);
...@@ -516,6 +516,10 @@ libxlStateCleanup(void) ...@@ -516,6 +516,10 @@ libxlStateCleanup(void)
virObjectUnref(libxl_driver->domainEventState); virObjectUnref(libxl_driver->domainEventState);
virSysinfoDefFree(libxl_driver->hostsysinfo); virSysinfoDefFree(libxl_driver->hostsysinfo);
if (libxl_driver->lockFD != -1)
virPidFileRelease(libxl_driver->config->stateDir, "driver", libxl_driver->lockFD);
virObjectUnref(libxl_driver->config);
virMutexDestroy(&libxl_driver->lock); virMutexDestroy(&libxl_driver->lock);
VIR_FREE(libxl_driver); VIR_FREE(libxl_driver);
...@@ -658,6 +662,7 @@ libxlStateInitialize(bool privileged, ...@@ -658,6 +662,7 @@ libxlStateInitialize(bool privileged,
if (VIR_ALLOC(libxl_driver) < 0) if (VIR_ALLOC(libxl_driver) < 0)
return -1; return -1;
libxl_driver->lockFD = -1;
if (virMutexInit(&libxl_driver->lock) < 0) { if (virMutexInit(&libxl_driver->lock) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR, virReportError(VIR_ERR_INTERNAL_ERROR,
"%s", _("cannot initialize mutex")); "%s", _("cannot initialize mutex"));
...@@ -741,6 +746,10 @@ libxlStateInitialize(bool privileged, ...@@ -741,6 +746,10 @@ libxlStateInitialize(bool privileged,
goto error; goto error;
} }
if ((libxl_driver->lockFD =
virPidFileAcquire(cfg->stateDir, "driver", true, getpid())) < 0)
goto error;
if (!(libxl_driver->lockManager = if (!(libxl_driver->lockManager =
virLockManagerPluginNew(cfg->lockManagerName ? virLockManagerPluginNew(cfg->lockManagerName ?
cfg->lockManagerName : "nop", cfg->lockManagerName : "nop",
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册