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

remote: in per-driver daemons ensure that state initialize succeeds

When running in libvirtd, we are happy for any of the drivers to simply
skip their initialization in virStateInitialize, as other drivers are
still potentially useful.

When running in per-driver daemons though, we want the daemon to abort
startup if the driver cannot initialize itself, as the daemon will be
useless without it.
Reviewed-by: NAndrea Bolognani <abologna@redhat.com>
Signed-off-by: NDaniel P. Berrangé <berrange@redhat.com>
上级 808b0d2d
......@@ -1220,16 +1220,16 @@ bhyveStateInitialize(bool privileged,
{
if (!privileged) {
VIR_INFO("Not running privileged, disabling driver");
return 0;
return VIR_DRV_STATE_INIT_SKIPPED;
}
if (VIR_ALLOC(bhyve_driver) < 0)
return -1;
return VIR_DRV_STATE_INIT_ERROR;
bhyve_driver->lockFD = -1;
if (virMutexInit(&bhyve_driver->lock) < 0) {
VIR_FREE(bhyve_driver);
return -1;
return VIR_DRV_STATE_INIT_ERROR;
}
if (!(bhyve_driver->closeCallbacks = virCloseCallbacksNew()))
......@@ -1303,11 +1303,11 @@ bhyveStateInitialize(bool privileged,
bhyveAutostartDomains(bhyve_driver);
return 0;
return VIR_DRV_STATE_INIT_COMPLETE;
cleanup:
bhyveStateCleanup();
return -1;
return VIR_DRV_STATE_INIT_ERROR;
}
unsigned
......
......@@ -24,7 +24,13 @@
# error "Don't include this file directly, only use driver.h"
#endif
typedef int
typedef enum {
VIR_DRV_STATE_INIT_ERROR = -1,
VIR_DRV_STATE_INIT_SKIPPED,
VIR_DRV_STATE_INIT_COMPLETE,
} virDrvStateInitResult;
typedef virDrvStateInitResult
(*virDrvStateInitialize)(bool privileged,
virStateInhibitCallback callback,
void *opaque);
......
......@@ -93,10 +93,10 @@ netcfStateInitialize(bool privileged,
void *opaque ATTRIBUTE_UNUSED)
{
if (virNetcfDriverStateInitialize() < 0)
return -1;
return VIR_DRV_STATE_INIT_ERROR;
if (!(driver = virObjectLockableNew(virNetcfDriverStateClass)))
return -1;
return VIR_DRV_STATE_INIT_ERROR;
driver->privileged = privileged;
......@@ -129,12 +129,12 @@ netcfStateInitialize(bool privileged,
_("failed to initialize netcf"));
goto error;
}
return 0;
return VIR_DRV_STATE_INIT_COMPLETE;
error:
virObjectUnref(driver);
driver = NULL;
return -1;
return VIR_DRV_STATE_INIT_ERROR;
}
......
......@@ -1172,7 +1172,7 @@ udevStateInitialize(bool privileged,
virStateInhibitCallback callback ATTRIBUTE_UNUSED,
void *opaque ATTRIBUTE_UNUSED)
{
int ret = -1;
int ret = VIR_DRV_STATE_INIT_ERROR;
if (VIR_ALLOC(driver) < 0)
goto cleanup;
......@@ -1210,7 +1210,7 @@ udevStateInitialize(bool privileged,
}
driver->privileged = privileged;
ret = 0;
ret = VIR_DRV_STATE_INIT_COMPLETE;
cleanup:
if (ret < 0)
......
......@@ -622,6 +622,7 @@ virRegisterStateDriver(virStateDriverPtr driver)
/**
* virStateInitialize:
* @privileged: set to true if running with root privilege, false otherwise
* @mandatory: set to true if all drivers must report success, not skipped
* @callback: callback to invoke to inhibit shutdown of the daemon
* @opaque: data to pass to @callback
*
......@@ -631,6 +632,7 @@ virRegisterStateDriver(virStateDriverPtr driver)
*/
int
virStateInitialize(bool privileged,
bool mandatory,
virStateInhibitCallback callback,
void *opaque)
{
......@@ -641,15 +643,22 @@ virStateInitialize(bool privileged,
for (i = 0; i < virStateDriverTabCount; i++) {
if (virStateDriverTab[i]->stateInitialize) {
virDrvStateInitResult ret;
VIR_DEBUG("Running global init for %s state driver",
virStateDriverTab[i]->name);
if (virStateDriverTab[i]->stateInitialize(privileged,
callback,
opaque) < 0) {
ret = virStateDriverTab[i]->stateInitialize(privileged,
callback,
opaque);
VIR_DEBUG("State init result %d (mandatory=%d)", ret, mandatory);
if (ret == VIR_DRV_STATE_INIT_ERROR) {
VIR_ERROR(_("Initialization of %s state driver failed: %s"),
virStateDriverTab[i]->name,
virGetLastErrorMessage());
return -1;
} else if (ret == VIR_DRV_STATE_INIT_SKIPPED && mandatory) {
VIR_ERROR(_("Initialization of mandatory %s state driver skipped"),
virStateDriverTab[i]->name);
return -1;
}
}
}
......
......@@ -30,6 +30,7 @@ typedef void (*virStateInhibitCallback)(bool inhibit,
void *opaque);
int virStateInitialize(bool privileged,
bool mandatory,
virStateInhibitCallback inhibit,
void *opaque);
int virStateCleanup(void);
......
......@@ -657,17 +657,17 @@ libxlStateInitialize(bool privileged,
char ebuf[1024];
if (!libxlDriverShouldLoad(privileged))
return 0;
return VIR_DRV_STATE_INIT_SKIPPED;
if (VIR_ALLOC(libxl_driver) < 0)
return -1;
return VIR_DRV_STATE_INIT_ERROR;
libxl_driver->lockFD = -1;
if (virMutexInit(&libxl_driver->lock) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
"%s", _("cannot initialize mutex"));
VIR_FREE(libxl_driver);
return -1;
return VIR_DRV_STATE_INIT_ERROR;
}
/* Allocate bitmap for vnc port reservation */
......@@ -806,12 +806,12 @@ libxlStateInitialize(bool privileged,
virDomainObjListForEach(libxl_driver->domains, libxlDomainManagedSaveLoad,
libxl_driver);
return 0;
return VIR_DRV_STATE_INIT_COMPLETE;
error:
VIR_FREE(driverConf);
libxlStateCleanup();
return -1;
return VIR_DRV_STATE_INIT_ERROR;
}
static int
......
......@@ -1545,7 +1545,7 @@ static int lxcStateInitialize(bool privileged,
/* Check that the user is root, silently disable if not */
if (!privileged) {
VIR_INFO("Not running privileged, disabling driver");
return 0;
return VIR_DRV_STATE_INIT_SKIPPED;
}
/* Check that this is a container enabled kernel */
......@@ -1554,15 +1554,15 @@ static int lxcStateInitialize(bool privileged,
VIR_PROCESS_NAMESPACE_UTS |
VIR_PROCESS_NAMESPACE_IPC) < 0) {
VIR_INFO("LXC support not available in this kernel, disabling driver");
return 0;
return VIR_DRV_STATE_INIT_SKIPPED;
}
if (VIR_ALLOC(lxc_driver) < 0)
return -1;
return VIR_DRV_STATE_INIT_ERROR;
lxc_driver->lockFD = -1;
if (virMutexInit(&lxc_driver->lock) < 0) {
VIR_FREE(lxc_driver);
return -1;
return VIR_DRV_STATE_INIT_ERROR;
}
if (!(lxc_driver->domains = virDomainObjListNew()))
......@@ -1633,12 +1633,12 @@ static int lxcStateInitialize(bool privileged,
virLXCProcessAutostartAll(lxc_driver);
virObjectUnref(caps);
return 0;
return VIR_DRV_STATE_INIT_COMPLETE;
cleanup:
virObjectUnref(caps);
lxcStateCleanup();
return -1;
return VIR_DRV_STATE_INIT_ERROR;
}
static void lxcNotifyLoadDomain(virDomainObjPtr vm, int newVM, void *opaque)
......
......@@ -713,7 +713,7 @@ networkStateInitialize(bool privileged,
virStateInhibitCallback callback ATTRIBUTE_UNUSED,
void *opaque ATTRIBUTE_UNUSED)
{
int ret = -1;
int ret = VIR_DRV_STATE_INIT_ERROR;
char *configdir = NULL;
char *rundir = NULL;
#ifdef WITH_FIREWALLD
......@@ -847,7 +847,7 @@ networkStateInitialize(bool privileged,
}
#endif
ret = 0;
ret = VIR_DRV_STATE_INIT_COMPLETE;
cleanup:
VIR_FREE(configdir);
VIR_FREE(rundir);
......
......@@ -599,7 +599,7 @@ nodeStateInitialize(bool privileged ATTRIBUTE_UNUSED,
char **udi = NULL;
int num_devs;
size_t i;
int ret = -1;
int ret = VIR_DRV_STATE_INIT_ERROR;
DBusConnection *sysbus;
DBusError err;
......@@ -608,12 +608,12 @@ nodeStateInitialize(bool privileged ATTRIBUTE_UNUSED,
cmpstringp);
if (VIR_ALLOC(driver) < 0)
return -1;
return VIR_DRV_STATE_INIT_ERROR;
driver->lockFD = -1;
if (virMutexInit(&driver->lock) < 0) {
VIR_FREE(driver);
return -1;
return VIR_DRV_STATE_INIT_ERROR;
}
nodeDeviceLock();
......@@ -648,7 +648,7 @@ nodeStateInitialize(bool privileged ATTRIBUTE_UNUSED,
virReportError(VIR_ERR_INTERNAL_ERROR,
_("DBus not available, disabling HAL driver: %s"),
virGetLastErrorMessage());
ret = 0;
ret = VIR_DRV_STATE_INIT_SKIPPED;
goto failure;
}
......@@ -671,7 +671,7 @@ nodeStateInitialize(bool privileged ATTRIBUTE_UNUSED,
/* We don't want to show a fatal error here,
otherwise entire libvirtd shuts down when
hald isn't running */
ret = 0;
ret = VIR_DRV_STATE_INIT_SKIPPED;
goto failure;
}
......@@ -709,7 +709,7 @@ nodeStateInitialize(bool privileged ATTRIBUTE_UNUSED,
}
VIR_FREE(udi);
return 0;
return VIR_DRV_STATE_INIT_COMPLETE;
failure:
if (dbus_error_is_set(&err)) {
......
......@@ -1816,14 +1816,14 @@ nodeStateInitialize(bool privileged,
virThread enumThread;
if (VIR_ALLOC(driver) < 0)
return -1;
return VIR_DRV_STATE_INIT_ERROR;
driver->lockFD = -1;
if (virMutexInit(&driver->lock) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("Unable to initialize mutex"));
VIR_FREE(driver);
return -1;
return VIR_DRV_STATE_INIT_ERROR;
}
driver->privileged = privileged;
......@@ -1919,11 +1919,11 @@ nodeStateInitialize(bool privileged,
goto cleanup;
}
return 0;
return VIR_DRV_STATE_INIT_COMPLETE;
cleanup:
nodeStateCleanup();
return -1;
return VIR_DRV_STATE_INIT_ERROR;
unlock:
virObjectUnlock(priv);
......
......@@ -184,10 +184,10 @@ nwfilterStateInitialize(bool privileged,
if (virDBusHasSystemBus() &&
!(sysbus = virDBusGetSystemBus()))
return -1;
return VIR_DRV_STATE_INIT_ERROR;
if (VIR_ALLOC(driver) < 0)
return -1;
return VIR_DRV_STATE_INIT_ERROR;
driver->lockFD = -1;
if (virMutexInit(&driver->lock) < 0)
......@@ -201,7 +201,7 @@ nwfilterStateInitialize(bool privileged,
goto error;
if (!privileged)
return 0;
return VIR_DRV_STATE_INIT_SKIPPED;
nwfilterDriverLock();
......@@ -281,13 +281,13 @@ nwfilterStateInitialize(bool privileged,
nwfilterDriverUnlock();
return 0;
return VIR_DRV_STATE_INIT_COMPLETE;
error:
nwfilterDriverUnlock();
nwfilterStateCleanup();
return -1;
return VIR_DRV_STATE_INIT_ERROR;
err_techdrivers_shutdown:
virNWFilterTechDriversShutdown();
......@@ -302,7 +302,7 @@ nwfilterStateInitialize(bool privileged,
virNWFilterObjListFree(driver->nwfilters);
VIR_FREE(driver);
return -1;
return VIR_DRV_STATE_INIT_ERROR;
}
/**
......
......@@ -738,7 +738,7 @@ qemuStateInitialize(bool privileged,
size_t i;
if (VIR_ALLOC(qemu_driver) < 0)
return -1;
return VIR_DRV_STATE_INIT_ERROR;
qemu_driver->lockFD = -1;
......@@ -746,7 +746,7 @@ qemuStateInitialize(bool privileged,
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("cannot initialize mutex"));
VIR_FREE(qemu_driver);
return -1;
return VIR_DRV_STATE_INIT_ERROR;
}
qemu_driver->inhibitCallback = callback;
......@@ -1082,14 +1082,14 @@ qemuStateInitialize(bool privileged,
qemuAutostartDomains(qemu_driver);
return 0;
return VIR_DRV_STATE_INIT_COMPLETE;
error:
VIR_FREE(driverConf);
VIR_FREE(hugepagePath);
VIR_FREE(memoryBackingPath);
qemuStateCleanup();
return -1;
return VIR_DRV_STATE_INIT_ERROR;
}
static void qemuNotifyLoadDomain(virDomainObjPtr vm, int newVM, void *opaque)
......
......@@ -792,6 +792,11 @@ static void daemonRunStateInit(void *opaque)
{
virNetDaemonPtr dmn = opaque;
virIdentityPtr sysident = virIdentityGetSystem();
#ifdef MODULE_NAME
bool mandatory = true;
#else /* ! MODULE_NAME */
bool mandatory = false;
#endif /* ! MODULE_NAME */
virIdentitySetCurrent(sysident);
......@@ -804,6 +809,7 @@ static void daemonRunStateInit(void *opaque)
* we're ready, since it can take a long time and this will
* seriously delay OS bootup process */
if (virStateInitialize(virNetDaemonIsPrivileged(dmn),
mandatory,
daemonInhibitCallback,
dmn) < 0) {
VIR_ERROR(_("Driver state initialization failed"));
......
......@@ -195,7 +195,7 @@ remoteStateInitialize(bool privileged ATTRIBUTE_UNUSED,
* re-entering ourselves
*/
inside_daemon = true;
return 0;
return VIR_DRV_STATE_INIT_COMPLETE;
}
......
......@@ -457,12 +457,12 @@ secretStateInitialize(bool privileged,
void *opaque ATTRIBUTE_UNUSED)
{
if (VIR_ALLOC(driver) < 0)
return -1;
return VIR_DRV_STATE_INIT_ERROR;
driver->lockFD = -1;
if (virMutexInit(&driver->lock) < 0) {
VIR_FREE(driver);
return -1;
return VIR_DRV_STATE_INIT_ERROR;
}
secretDriverLock();
......@@ -514,12 +514,12 @@ secretStateInitialize(bool privileged,
goto error;
secretDriverUnlock();
return 0;
return VIR_DRV_STATE_INIT_COMPLETE;
error:
secretDriverUnlock();
secretStateCleanup();
return -1;
return VIR_DRV_STATE_INIT_ERROR;
}
......
......@@ -255,12 +255,12 @@ storageStateInitialize(bool privileged,
VIR_AUTOFREE(char *) rundir = NULL;
if (VIR_ALLOC(driver) < 0)
return -1;
return VIR_DRV_STATE_INIT_ERROR;
driver->lockFD = -1;
if (virMutexInit(&driver->lock) < 0) {
VIR_FREE(driver);
return -1;
return VIR_DRV_STATE_INIT_ERROR;
}
storageDriverLock();
......@@ -326,12 +326,12 @@ storageStateInitialize(bool privileged,
storageDriverUnlock();
return 0;
return VIR_DRV_STATE_INIT_COMPLETE;
error:
storageDriverUnlock();
storageStateCleanup();
return -1;
return VIR_DRV_STATE_INIT_ERROR;
}
/**
......
......@@ -4118,36 +4118,36 @@ vzStateInitialize(bool privileged,
void *opaque ATTRIBUTE_UNUSED)
{
if (!privileged)
return 0;
return VIR_DRV_STATE_INIT_SKIPPED;
vz_driver_privileged = privileged;
if (virFileMakePathWithMode(VZ_STATEDIR, S_IRWXU) < 0) {
virReportSystemError(errno, _("cannot create state directory '%s'"),
VZ_STATEDIR);
return -1;
return VIR_DRV_STATE_INIT_ERROR;
}
if ((vz_driver_lock_fd =
virPidFileAcquire(VZ_STATEDIR, "driver", false, getpid())) < 0)
return -1;
return VIR_DRV_STATE_INIT_ERROR;
if (prlsdkInit() < 0) {
VIR_DEBUG("%s", _("Can't initialize Parallels SDK"));
return -1;
return VIR_DRV_STATE_INIT_ERROR;
}
if (virMutexInit(&vz_driver_lock) < 0)
if (virMutexInit(&vz_driver_lock) < 0)
goto error;
/* Failing to create driver here is not fatal and only means
* that next driver client will try once more when connecting */
vz_driver = vzDriverObjNew();
return 0;
return VIR_DRV_STATE_INIT_COMPLETE;
error:
vzStateCleanup();
return -1;
return VIR_DRV_STATE_INIT_ERROR;
}
static virStateDriver vzStateDriver = {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册