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

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: NDaniel P. Berrangé <berrange@redhat.com>
上级 8292b9e3
...@@ -597,6 +597,7 @@ networkStateInitialize(bool privileged, ...@@ -597,6 +597,7 @@ networkStateInitialize(bool privileged,
if (VIR_ALLOC(network_driver) < 0) if (VIR_ALLOC(network_driver) < 0)
goto error; goto error;
network_driver->lockFD = -1;
if (virMutexInit(&network_driver->lock) < 0) { if (virMutexInit(&network_driver->lock) < 0) {
VIR_FREE(network_driver); VIR_FREE(network_driver);
goto error; goto error;
...@@ -651,6 +652,11 @@ networkStateInitialize(bool privileged, ...@@ -651,6 +652,11 @@ networkStateInitialize(bool privileged,
goto error; 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() */ /* if this fails now, it will be retried later with dnsmasqCapsRefresh() */
network_driver->dnsmasqCaps = dnsmasqCapsNewFromBinary(DNSMASQ); network_driver->dnsmasqCaps = dnsmasqCapsNewFromBinary(DNSMASQ);
...@@ -764,6 +770,10 @@ networkStateCleanup(void) ...@@ -764,6 +770,10 @@ networkStateCleanup(void)
/* free inactive networks */ /* free inactive networks */
virObjectUnref(network_driver->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->networkConfigDir);
VIR_FREE(network_driver->networkAutostartDir); VIR_FREE(network_driver->networkAutostartDir);
VIR_FREE(network_driver->stateDir); VIR_FREE(network_driver->stateDir);
......
...@@ -34,6 +34,9 @@ struct _virNetworkDriverState { ...@@ -34,6 +34,9 @@ struct _virNetworkDriverState {
/* Read-only */ /* Read-only */
bool privileged; bool privileged;
/* pid file FD, ensures two copies of the driver can't use the same root */
int lockFD;
/* Immutable pointer, self-locking APIs */ /* Immutable pointer, self-locking APIs */
virNetworkObjListPtr networks; virNetworkObjListPtr networks;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册