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>
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;
#include "virlog.h"
#include "virfile.h"
#include "virfdstream.h"
+#include "virpidfile.h"
#include "configmake.h"
#include "virsecret.h"
#include "virstring.h"
if (VIR_ALLOC(driver) < 0)
return -1;
+ driver->lockFD = -1;
if (virMutexInit(&driver->lock) < 0) {
VIR_FREE(driver);
return -1;
goto error;
}
+ if ((driver->lockFD =
+ virPidFileAcquire(driver->stateDir, "driver",
+ true, getpid())) < 0)
+ goto error;
+
if (virStoragePoolObjLoadAllState(driver->pools,
driver->stateDir) < 0)
goto error;
/* 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);