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

In unprivileged libvirtd this ends up locking

  /run/user/$UID/libvirt/network/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 8292b9e364
commit c03aef7c87
2 changed files with 13 additions and 0 deletions

View File

@ -597,6 +597,7 @@ networkStateInitialize(bool privileged,
if (VIR_ALLOC(network_driver) < 0)
goto error;
network_driver->lockFD = -1;
if (virMutexInit(&network_driver->lock) < 0) {
VIR_FREE(network_driver);
goto error;
@ -651,6 +652,11 @@ networkStateInitialize(bool privileged,
goto error;
}
if ((network_driver->lockFD =
virPidFileAcquire(network_driver->stateDir, "driver",
true, getpid())) < 0)
goto error;
/* if this fails now, it will be retried later with dnsmasqCapsRefresh() */
network_driver->dnsmasqCaps = dnsmasqCapsNewFromBinary(DNSMASQ);
@ -764,6 +770,10 @@ networkStateCleanup(void)
/* free inactive networks */
virObjectUnref(network_driver->networks);
if (network_driver->lockFD != -1)
virPidFileRelease(network_driver->stateDir, "driver",
network_driver->lockFD);
VIR_FREE(network_driver->networkConfigDir);
VIR_FREE(network_driver->networkAutostartDir);
VIR_FREE(network_driver->stateDir);

View File

@ -34,6 +34,9 @@ struct _virNetworkDriverState {
/* Read-only */
bool privileged;
/* pid file FD, ensures two copies of the driver can't use the same root */
int lockFD;
/* Immutable pointer, self-locking APIs */
virNetworkObjListPtr networks;