storage: 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/storage/driver.pid

In unprivileged libvirtd this ends up locking

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

NB, the latter can vary depending on $XDG_RUNTIME_DIR

Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
This commit is contained in:
Daniel P. Berrangé 2019-05-23 11:34:08 +01:00
parent c03aef7c87
commit 44a5ba2af8
2 changed files with 14 additions and 0 deletions

View File

@ -37,6 +37,9 @@ typedef virStorageDriverState *virStorageDriverStatePtr;
struct _virStorageDriverState {
virMutex lock;
/* pid file FD, ensures two copies of the driver can't use the same root */
int lockFD;
virStoragePoolObjListPtr pools;
char *configDir;

View File

@ -43,6 +43,7 @@
#include "virlog.h"
#include "virfile.h"
#include "virfdstream.h"
#include "virpidfile.h"
#include "configmake.h"
#include "virsecret.h"
#include "virstring.h"
@ -256,6 +257,7 @@ storageStateInitialize(bool privileged,
if (VIR_ALLOC(driver) < 0)
return -1;
driver->lockFD = -1;
if (virMutexInit(&driver->lock) < 0) {
VIR_FREE(driver);
return -1;
@ -296,6 +298,11 @@ storageStateInitialize(bool privileged,
goto error;
}
if ((driver->lockFD =
virPidFileAcquire(driver->stateDir, "driver",
true, getpid())) < 0)
goto error;
if (virStoragePoolObjLoadAllState(driver->pools,
driver->stateDir) < 0)
goto error;
@ -371,6 +378,10 @@ storageStateCleanup(void)
/* free inactive pools */
virObjectUnref(driver->pools);
if (driver->lockFD != -1)
virPidFileRelease(driver->stateDir, "driver",
driver->lockFD);
VIR_FREE(driver->configDir);
VIR_FREE(driver->autostartDir);
VIR_FREE(driver->stateDir);