#include "interface_conf.h"
#include "viralloc.h"
#include "virlog.h"
+#include "virfile.h"
+#include "virpidfile.h"
#include "virstring.h"
#include "viraccessapicheck.h"
#include "virinterfaceobj.h"
+#include "configmake.h"
+
#define VIR_FROM_THIS VIR_FROM_INTERFACE
VIR_LOG_INIT("interface.interface_backend_netcf");
typedef struct
{
virObjectLockable parent;
+ /* pid file FD, ensures two copies of the driver can't use the same root */
+ int lockFD;
+
+ char *stateDir;
struct netcf *netcf;
bool privileged;
} virNetcfDriverState, *virNetcfDriverStatePtr;
if (_driver->netcf)
ncf_close(_driver->netcf);
+
+ if (_driver->lockFD != -1)
+ virPidFileRelease(_driver->stateDir, "driver", _driver->lockFD);
+
+ VIR_FREE(_driver->stateDir);
}
driver->privileged = privileged;
+ if (privileged) {
+ if (virAsprintf(&driver->stateDir,
+ "%s/run/libvirt/nodedev", LOCALSTATEDIR) < 0)
+ goto error;
+ } else {
+ VIR_AUTOFREE(char *) rundir = NULL;
+
+ if (!(rundir = virGetUserRuntimeDirectory()))
+ goto error;
+ if (virAsprintf(&driver->stateDir, "%s/nodedev/run", rundir) < 0)
+ goto error;
+ }
+
+ if (virFileMakePathWithMode(driver->stateDir, S_IRWXU) < 0) {
+ virReportSystemError(errno, _("cannot create state directory '%s'"),
+ driver->stateDir);
+ goto error;
+ }
+
+ if ((driver->lockFD =
+ virPidFileAcquire(driver->stateDir, "driver", true, getpid())) < 0)
+ goto error;
+
/* open netcf */
if (ncf_init(&driver->netcf, NULL) != 0) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("failed to initialize netcf"));
- virObjectUnref(driver);
- driver = NULL;
- return -1;
+ goto error;
}
return 0;
+
+ error:
+ virObjectUnref(driver);
+ driver = NULL;
+ return -1;
}
#include "interface_conf.h"
#include "viralloc.h"
#include "virstring.h"
+#include "virpidfile.h"
#include "viraccessapicheck.h"
#include "virinterfaceobj.h"
#include "virnetdev.h"
+#include "configmake.h"
+
#define VIR_FROM_THIS VIR_FROM_INTERFACE
struct udev_iface_driver {
struct udev *udev;
+ /* pid file FD, ensures two copies of the driver can't use the same root */
+ int lockFD;
+
+ char *stateDir;
bool privileged;
};
}
+static int
+udevStateCleanup(void);
+
static int
udevStateInitialize(bool privileged,
virStateInhibitCallback callback ATTRIBUTE_UNUSED,
if (VIR_ALLOC(driver) < 0)
goto cleanup;
+ driver->lockFD = -1;
+
+ if (privileged) {
+ if (virAsprintf(&driver->stateDir,
+ "%s/run/libvirt/nodedev", LOCALSTATEDIR) < 0)
+ goto cleanup;
+ } else {
+ VIR_AUTOFREE(char *) rundir = NULL;
+
+ if (!(rundir = virGetUserRuntimeDirectory()))
+ goto cleanup;
+ if (virAsprintf(&driver->stateDir, "%s/nodedev/run", rundir) < 0)
+ goto cleanup;
+ }
+
+ if (virFileMakePathWithMode(driver->stateDir, S_IRWXU) < 0) {
+ virReportSystemError(errno, _("cannot create state directory '%s'"),
+ driver->stateDir);
+ goto cleanup;
+ }
+
+ if ((driver->lockFD =
+ virPidFileAcquire(driver->stateDir, "driver", true, getpid())) < 0)
+ goto cleanup;
+
driver->udev = udev_new();
if (!driver->udev) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
ret = 0;
cleanup:
+ if (ret < 0)
+ udevStateCleanup();
return ret;
}
if (!driver)
return -1;
- udev_unref(driver->udev);
+ if (driver->udev)
+ udev_unref(driver->udev);
+
+ if (driver->lockFD != -1)
+ virPidFileRelease(driver->stateDir, "driver", driver->lockFD);
+ VIR_FREE(driver->stateDir);
VIR_FREE(driver);
return 0;
}