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

In unprivileged libvirtd this ends up locking

  /run/user/$UID/libvirt/bhyve/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 7cefe61172
commit dbfdbd9acc
2 changed files with 12 additions and 0 deletions

View File

@ -43,6 +43,7 @@
#include "virthread.h"
#include "virlog.h"
#include "virfile.h"
#include "virpidfile.h"
#include "virtypedparam.h"
#include "virrandom.h"
#include "virstring.h"
@ -1203,6 +1204,9 @@ bhyveStateCleanup(void)
virObjectUnref(bhyve_driver->config);
virPortAllocatorRangeFree(bhyve_driver->remotePorts);
if (bhyve_driver->lockFD != -1)
virPidFileRelease(BHYVE_STATE_DIR, "driver", bhyve_driver->lockFD);
virMutexDestroy(&bhyve_driver->lock);
VIR_FREE(bhyve_driver);
@ -1222,6 +1226,7 @@ bhyveStateInitialize(bool privileged,
if (VIR_ALLOC(bhyve_driver) < 0)
return -1;
bhyve_driver->lockFD = -1;
if (virMutexInit(&bhyve_driver->lock) < 0) {
VIR_FREE(bhyve_driver);
return -1;
@ -1274,6 +1279,10 @@ bhyveStateInitialize(bool privileged,
goto cleanup;
}
if ((bhyve_driver->lockFD =
virPidFileAcquire(BHYVE_STATE_DIR, "driver", true, getpid())) < 0)
goto cleanup;
if (virDomainObjListLoadAllConfigs(bhyve_driver->domains,
BHYVE_STATE_DIR,
NULL, true,

View File

@ -48,6 +48,9 @@ struct _bhyveConn {
virBhyveDriverConfigPtr config;
/* pid file FD, ensures two copies of the driver can't use the same root */
int lockFD;
virDomainObjListPtr domains;
virCapsPtr caps;
virDomainXMLOptionPtr xmlopt;