qemu: Avoid using global qemu_driver in event handlers

We will have to pass a mock-up of the driver when testing monitor
events.
This commit is contained in:
Jiri Denemark 2013-07-25 19:26:15 +02:00
parent 6ac7cc8edc
commit 809ee6bad4
5 changed files with 130 additions and 76 deletions

View File

@ -2354,7 +2354,8 @@ cleanup:
static void virQEMUCapsMonitorNotify(qemuMonitorPtr mon ATTRIBUTE_UNUSED,
virDomainObjPtr vm ATTRIBUTE_UNUSED)
virDomainObjPtr vm ATTRIBUTE_UNUSED,
void *opaque ATTRIBUTE_UNUSED)
{
}
@ -2546,7 +2547,7 @@ virQEMUCapsInitQMP(virQEMUCapsPtr qemuCaps,
memset(&vm, 0, sizeof(vm));
vm.pid = pid;
if (!(mon = qemuMonitorOpen(&vm, &config, true, &callbacks))) {
if (!(mon = qemuMonitorOpen(&vm, &config, true, &callbacks, NULL))) {
ret = 0;
goto cleanup;
}

View File

@ -61,6 +61,7 @@ struct _qemuMonitor {
virDomainObjPtr vm;
qemuMonitorCallbacksPtr cb;
void *callbackOpaque;
/* If there's a command being processed this will be
* non-NULL */
@ -248,7 +249,7 @@ static void qemuMonitorDispose(void *obj)
VIR_DEBUG("mon=%p", mon);
if (mon->cb && mon->cb->destroy)
(mon->cb->destroy)(mon, mon->vm);
(mon->cb->destroy)(mon, mon->vm, mon->callbackOpaque);
virCondDestroy(&mon->notify);
VIR_FREE(mon->buffer);
virJSONValueFree(mon->options);
@ -668,7 +669,7 @@ qemuMonitorIO(int watch, int fd, int events, void *opaque) {
virObjectUnlock(mon);
virObjectUnref(mon);
VIR_DEBUG("Triggering EOF callback");
(eofNotify)(mon, vm);
(eofNotify)(mon, vm, mon->callbackOpaque);
} else if (error) {
qemuMonitorErrorNotifyCallback errorNotify = mon->cb->errorNotify;
virDomainObjPtr vm = mon->vm;
@ -678,7 +679,7 @@ qemuMonitorIO(int watch, int fd, int events, void *opaque) {
virObjectUnlock(mon);
virObjectUnref(mon);
VIR_DEBUG("Triggering error callback");
(errorNotify)(mon, vm);
(errorNotify)(mon, vm, mon->callbackOpaque);
} else {
virObjectUnlock(mon);
virObjectUnref(mon);
@ -691,7 +692,8 @@ qemuMonitorOpenInternal(virDomainObjPtr vm,
int fd,
bool hasSendFD,
bool json,
qemuMonitorCallbacksPtr cb)
qemuMonitorCallbacksPtr cb,
void *opaque)
{
qemuMonitorPtr mon;
@ -725,6 +727,7 @@ qemuMonitorOpenInternal(virDomainObjPtr vm,
if (json)
mon->waitGreeting = true;
mon->cb = cb;
mon->callbackOpaque = opaque;
if (virSetCloseExec(mon->fd) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
@ -778,7 +781,8 @@ qemuMonitorPtr
qemuMonitorOpen(virDomainObjPtr vm,
virDomainChrSourceDefPtr config,
bool json,
qemuMonitorCallbacksPtr cb)
qemuMonitorCallbacksPtr cb,
void *opaque)
{
int fd;
bool hasSendFD = false;
@ -803,7 +807,7 @@ qemuMonitorOpen(virDomainObjPtr vm,
return NULL;
}
ret = qemuMonitorOpenInternal(vm, fd, hasSendFD, json, cb);
ret = qemuMonitorOpenInternal(vm, fd, hasSendFD, json, cb, opaque);
if (!ret)
VIR_FORCE_CLOSE(fd);
return ret;
@ -813,9 +817,10 @@ qemuMonitorOpen(virDomainObjPtr vm,
qemuMonitorPtr qemuMonitorOpenFD(virDomainObjPtr vm,
int sockfd,
bool json,
qemuMonitorCallbacksPtr cb)
qemuMonitorCallbacksPtr cb,
void *opaque)
{
return qemuMonitorOpenInternal(vm, sockfd, true, json, cb);
return qemuMonitorOpenInternal(vm, sockfd, true, json, cb, opaque);
}
@ -1068,7 +1073,8 @@ cleanup:
virObjectRef(mon); \
virObjectUnlock(mon); \
if ((mon)->cb && (mon)->cb->callback) \
(ret) = ((mon)->cb->callback)(mon, __VA_ARGS__); \
(ret) = (mon)->cb->callback(mon, __VA_ARGS__, \
(mon)->callbackOpaque); \
virObjectLock(mon); \
virObjectUnref(mon); \
} while (0)

View File

@ -69,11 +69,14 @@ struct _qemuMonitorMessage {
typedef void (*qemuMonitorDestroyCallback)(qemuMonitorPtr mon,
virDomainObjPtr vm);
virDomainObjPtr vm,
void *opaque);
typedef void (*qemuMonitorEofNotifyCallback)(qemuMonitorPtr mon,
virDomainObjPtr vm);
virDomainObjPtr vm,
void *opaque);
typedef void (*qemuMonitorErrorNotifyCallback)(qemuMonitorPtr mon,
virDomainObjPtr vm);
virDomainObjPtr vm,
void *opaque);
/* XXX we'd really like to avoid virConnectPtr here
* It is required so the callback can find the active
* secret driver. Need to change this to work like the
@ -84,28 +87,37 @@ typedef int (*qemuMonitorDiskSecretLookupCallback)(qemuMonitorPtr mon,
virDomainObjPtr vm,
const char *path,
char **secret,
size_t *secretLen);
size_t *secretLen,
void *opaque);
typedef int (*qemuMonitorDomainShutdownCallback)(qemuMonitorPtr mon,
virDomainObjPtr vm);
virDomainObjPtr vm,
void *opaque);
typedef int (*qemuMonitorDomainResetCallback)(qemuMonitorPtr mon,
virDomainObjPtr vm);
virDomainObjPtr vm,
void *opaque);
typedef int (*qemuMonitorDomainPowerdownCallback)(qemuMonitorPtr mon,
virDomainObjPtr vm);
virDomainObjPtr vm,
void *opaque);
typedef int (*qemuMonitorDomainStopCallback)(qemuMonitorPtr mon,
virDomainObjPtr vm);
virDomainObjPtr vm,
void *opaque);
typedef int (*qemuMonitorDomainResumeCallback)(qemuMonitorPtr mon,
virDomainObjPtr vm);
virDomainObjPtr vm,
void *opaque);
typedef int (*qemuMonitorDomainRTCChangeCallback)(qemuMonitorPtr mon,
virDomainObjPtr vm,
long long offset);
long long offset,
void *opaque);
typedef int (*qemuMonitorDomainWatchdogCallback)(qemuMonitorPtr mon,
virDomainObjPtr vm,
int action);
int action,
void *opaque);
typedef int (*qemuMonitorDomainIOErrorCallback)(qemuMonitorPtr mon,
virDomainObjPtr vm,
const char *diskAlias,
int action,
const char *reason);
const char *reason,
void *opaque);
typedef int (*qemuMonitorDomainGraphicsCallback)(qemuMonitorPtr mon,
virDomainObjPtr vm,
int phase,
@ -117,30 +129,39 @@ typedef int (*qemuMonitorDomainGraphicsCallback)(qemuMonitorPtr mon,
const char *remoteService,
const char *authScheme,
const char *x509dname,
const char *saslUsername);
const char *saslUsername,
void *opaque);
typedef int (*qemuMonitorDomainBlockJobCallback)(qemuMonitorPtr mon,
virDomainObjPtr vm,
const char *diskAlias,
int type,
int status);
int status,
void *opaque);
typedef int (*qemuMonitorDomainTrayChangeCallback)(qemuMonitorPtr mon,
virDomainObjPtr vm,
const char *devAlias,
int reason);
int reason,
void *opaque);
typedef int (*qemuMonitorDomainPMWakeupCallback)(qemuMonitorPtr mon,
virDomainObjPtr vm);
virDomainObjPtr vm,
void *opaque);
typedef int (*qemuMonitorDomainPMSuspendCallback)(qemuMonitorPtr mon,
virDomainObjPtr vm);
virDomainObjPtr vm,
void *opaque);
typedef int (*qemuMonitorDomainBalloonChangeCallback)(qemuMonitorPtr mon,
virDomainObjPtr vm,
unsigned long long actual);
unsigned long long actual,
void *opaque);
typedef int (*qemuMonitorDomainPMSuspendDiskCallback)(qemuMonitorPtr mon,
virDomainObjPtr vm);
virDomainObjPtr vm,
void *opaque);
typedef int (*qemuMonitorDomainGuestPanicCallback)(qemuMonitorPtr mon,
virDomainObjPtr vm);
virDomainObjPtr vm,
void *opaque);
typedef int (*qemuMonitorDomainDeviceDeletedCallback)(qemuMonitorPtr mon,
virDomainObjPtr vm,
const char *devAlias);
const char *devAlias,
void *opaque);
typedef struct _qemuMonitorCallbacks qemuMonitorCallbacks;
typedef qemuMonitorCallbacks *qemuMonitorCallbacksPtr;
@ -174,12 +195,14 @@ char *qemuMonitorUnescapeArg(const char *in);
qemuMonitorPtr qemuMonitorOpen(virDomainObjPtr vm,
virDomainChrSourceDefPtr config,
bool json,
qemuMonitorCallbacksPtr cb)
qemuMonitorCallbacksPtr cb,
void *opaque)
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(4);
qemuMonitorPtr qemuMonitorOpenFD(virDomainObjPtr vm,
int sockfd,
bool json,
qemuMonitorCallbacksPtr cb)
qemuMonitorCallbacksPtr cb,
void *opaque)
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(4);
void qemuMonitorClose(qemuMonitorPtr mon);

View File

@ -280,9 +280,10 @@ cleanup:
*/
static void
qemuProcessHandleMonitorEOF(qemuMonitorPtr mon ATTRIBUTE_UNUSED,
virDomainObjPtr vm)
virDomainObjPtr vm,
void *opaque)
{
virQEMUDriverPtr driver = qemu_driver;
virQEMUDriverPtr driver = opaque;
virDomainEventPtr event = NULL;
qemuDomainObjPrivatePtr priv;
int eventReason = VIR_DOMAIN_EVENT_STOPPED_SHUTDOWN;
@ -341,9 +342,10 @@ cleanup:
*/
static void
qemuProcessHandleMonitorError(qemuMonitorPtr mon ATTRIBUTE_UNUSED,
virDomainObjPtr vm)
virDomainObjPtr vm,
void *opaque)
{
virQEMUDriverPtr driver = qemu_driver;
virQEMUDriverPtr driver = opaque;
virDomainEventPtr event = NULL;
VIR_DEBUG("Received error on %p '%s'", vm, vm->def->name);
@ -486,7 +488,8 @@ qemuProcessFindVolumeQcowPassphrase(qemuMonitorPtr mon ATTRIBUTE_UNUSED,
virDomainObjPtr vm,
const char *path,
char **secretRet,
size_t *secretLen)
size_t *secretLen,
void *opaque ATTRIBUTE_UNUSED)
{
virDomainDiskDefPtr disk;
int ret = -1;
@ -507,9 +510,10 @@ cleanup:
static int
qemuProcessHandleReset(qemuMonitorPtr mon ATTRIBUTE_UNUSED,
virDomainObjPtr vm)
virDomainObjPtr vm,
void *opaque)
{
virQEMUDriverPtr driver = qemu_driver;
virQEMUDriverPtr driver = opaque;
virDomainEventPtr event;
qemuDomainObjPrivatePtr priv;
@ -637,9 +641,10 @@ qemuProcessShutdownOrReboot(virQEMUDriverPtr driver,
static int
qemuProcessHandleShutdown(qemuMonitorPtr mon ATTRIBUTE_UNUSED,
virDomainObjPtr vm)
virDomainObjPtr vm,
void *opaque)
{
virQEMUDriverPtr driver = qemu_driver;
virQEMUDriverPtr driver = opaque;
qemuDomainObjPrivatePtr priv;
virDomainEventPtr event = NULL;
virQEMUDriverConfigPtr cfg = virQEMUDriverGetConfig(driver);
@ -691,9 +696,10 @@ unlock:
static int
qemuProcessHandleStop(qemuMonitorPtr mon ATTRIBUTE_UNUSED,
virDomainObjPtr vm)
virDomainObjPtr vm,
void *opaque)
{
virQEMUDriverPtr driver = qemu_driver;
virQEMUDriverPtr driver = opaque;
virDomainEventPtr event = NULL;
virQEMUDriverConfigPtr cfg = virQEMUDriverGetConfig(driver);
@ -737,9 +743,10 @@ unlock:
static int
qemuProcessHandleResume(qemuMonitorPtr mon ATTRIBUTE_UNUSED,
virDomainObjPtr vm)
virDomainObjPtr vm,
void *opaque)
{
virQEMUDriverPtr driver = qemu_driver;
virQEMUDriverPtr driver = opaque;
virDomainEventPtr event = NULL;
virQEMUDriverConfigPtr cfg = virQEMUDriverGetConfig(driver);
@ -790,9 +797,10 @@ unlock:
static int
qemuProcessHandleRTCChange(qemuMonitorPtr mon ATTRIBUTE_UNUSED,
virDomainObjPtr vm,
long long offset)
long long offset,
void *opaque)
{
virQEMUDriverPtr driver = qemu_driver;
virQEMUDriverPtr driver = opaque;
virDomainEventPtr event = NULL;
virQEMUDriverConfigPtr cfg = virQEMUDriverGetConfig(driver);
@ -830,9 +838,10 @@ qemuProcessHandleRTCChange(qemuMonitorPtr mon ATTRIBUTE_UNUSED,
static int
qemuProcessHandleWatchdog(qemuMonitorPtr mon ATTRIBUTE_UNUSED,
virDomainObjPtr vm,
int action)
int action,
void *opaque)
{
virQEMUDriverPtr driver = qemu_driver;
virQEMUDriverPtr driver = opaque;
virDomainEventPtr watchdogEvent = NULL;
virDomainEventPtr lifecycleEvent = NULL;
virQEMUDriverConfigPtr cfg = virQEMUDriverGetConfig(driver);
@ -896,9 +905,10 @@ qemuProcessHandleIOError(qemuMonitorPtr mon ATTRIBUTE_UNUSED,
virDomainObjPtr vm,
const char *diskAlias,
int action,
const char *reason)
const char *reason,
void *opaque)
{
virQEMUDriverPtr driver = qemu_driver;
virQEMUDriverPtr driver = opaque;
virDomainEventPtr ioErrorEvent = NULL;
virDomainEventPtr ioErrorEvent2 = NULL;
virDomainEventPtr lifecycleEvent = NULL;
@ -956,9 +966,10 @@ qemuProcessHandleBlockJob(qemuMonitorPtr mon ATTRIBUTE_UNUSED,
virDomainObjPtr vm,
const char *diskAlias,
int type,
int status)
int status,
void *opaque)
{
virQEMUDriverPtr driver = qemu_driver;
virQEMUDriverPtr driver = opaque;
virDomainEventPtr event = NULL;
const char *path;
virDomainDiskDefPtr disk;
@ -1008,9 +1019,10 @@ qemuProcessHandleGraphics(qemuMonitorPtr mon ATTRIBUTE_UNUSED,
const char *remoteService,
const char *authScheme,
const char *x509dname,
const char *saslUsername)
const char *saslUsername,
void *opaque)
{
virQEMUDriverPtr driver = qemu_driver;
virQEMUDriverPtr driver = opaque;
virDomainEventPtr event;
virDomainEventGraphicsAddressPtr localAddr = NULL;
virDomainEventGraphicsAddressPtr remoteAddr = NULL;
@ -1084,7 +1096,8 @@ error:
static void qemuProcessHandleMonitorDestroy(qemuMonitorPtr mon ATTRIBUTE_UNUSED,
virDomainObjPtr vm)
virDomainObjPtr vm,
void *opaque ATTRIBUTE_UNUSED)
{
virObjectUnref(vm);
}
@ -1093,9 +1106,10 @@ static int
qemuProcessHandleTrayChange(qemuMonitorPtr mon ATTRIBUTE_UNUSED,
virDomainObjPtr vm,
const char *devAlias,
int reason)
int reason,
void *opaque)
{
virQEMUDriverPtr driver = qemu_driver;
virQEMUDriverPtr driver = opaque;
virDomainEventPtr event = NULL;
virDomainDiskDefPtr disk;
virQEMUDriverConfigPtr cfg = virQEMUDriverGetConfig(driver);
@ -1128,9 +1142,10 @@ qemuProcessHandleTrayChange(qemuMonitorPtr mon ATTRIBUTE_UNUSED,
static int
qemuProcessHandlePMWakeup(qemuMonitorPtr mon ATTRIBUTE_UNUSED,
virDomainObjPtr vm)
virDomainObjPtr vm,
void *opaque)
{
virQEMUDriverPtr driver = qemu_driver;
virQEMUDriverPtr driver = opaque;
virDomainEventPtr event = NULL;
virDomainEventPtr lifecycleEvent = NULL;
virQEMUDriverConfigPtr cfg = virQEMUDriverGetConfig(driver);
@ -1168,9 +1183,10 @@ qemuProcessHandlePMWakeup(qemuMonitorPtr mon ATTRIBUTE_UNUSED,
static int
qemuProcessHandlePMSuspend(qemuMonitorPtr mon ATTRIBUTE_UNUSED,
virDomainObjPtr vm)
virDomainObjPtr vm,
void *opaque)
{
virQEMUDriverPtr driver = qemu_driver;
virQEMUDriverPtr driver = opaque;
virDomainEventPtr event = NULL;
virDomainEventPtr lifecycleEvent = NULL;
virQEMUDriverConfigPtr cfg = virQEMUDriverGetConfig(driver);
@ -1212,9 +1228,10 @@ qemuProcessHandlePMSuspend(qemuMonitorPtr mon ATTRIBUTE_UNUSED,
static int
qemuProcessHandleBalloonChange(qemuMonitorPtr mon ATTRIBUTE_UNUSED,
virDomainObjPtr vm,
unsigned long long actual)
unsigned long long actual,
void *opaque)
{
virQEMUDriverPtr driver = qemu_driver;
virQEMUDriverPtr driver = opaque;
virDomainEventPtr event = NULL;
virQEMUDriverConfigPtr cfg = virQEMUDriverGetConfig(driver);
@ -1238,9 +1255,10 @@ qemuProcessHandleBalloonChange(qemuMonitorPtr mon ATTRIBUTE_UNUSED,
static int
qemuProcessHandlePMSuspendDisk(qemuMonitorPtr mon ATTRIBUTE_UNUSED,
virDomainObjPtr vm)
virDomainObjPtr vm,
void *opaque)
{
virQEMUDriverPtr driver = qemu_driver;
virQEMUDriverPtr driver = opaque;
virDomainEventPtr event = NULL;
virDomainEventPtr lifecycleEvent = NULL;
virQEMUDriverConfigPtr cfg = virQEMUDriverGetConfig(driver);
@ -1283,9 +1301,10 @@ qemuProcessHandlePMSuspendDisk(qemuMonitorPtr mon ATTRIBUTE_UNUSED,
static int
qemuProcessHandleGuestPanic(qemuMonitorPtr mon ATTRIBUTE_UNUSED,
virDomainObjPtr vm)
virDomainObjPtr vm,
void *opaque)
{
virQEMUDriverPtr driver = qemu_driver;
virQEMUDriverPtr driver = opaque;
struct qemuProcessEvent *processEvent;
virObjectLock(vm);
@ -1316,9 +1335,10 @@ cleanup:
static int
qemuProcessHandleDeviceDeleted(qemuMonitorPtr mon ATTRIBUTE_UNUSED,
virDomainObjPtr vm,
const char *devAlias)
const char *devAlias,
void *opaque)
{
virQEMUDriverPtr driver = qemu_driver;
virQEMUDriverPtr driver = opaque;
virQEMUDriverConfigPtr cfg = virQEMUDriverGetConfig(driver);
virDomainDeviceDef dev;
@ -1391,7 +1411,8 @@ qemuConnectMonitor(virQEMUDriverPtr driver, virDomainObjPtr vm)
mon = qemuMonitorOpen(vm,
priv->monConfig,
priv->monJSON,
&monitorCallbacks);
&monitorCallbacks,
driver);
virObjectLock(vm);
priv->monStart = 0;

View File

@ -708,14 +708,16 @@ error:
static void
qemuMonitorTestEOFNotify(qemuMonitorPtr mon ATTRIBUTE_UNUSED,
virDomainObjPtr vm ATTRIBUTE_UNUSED)
virDomainObjPtr vm ATTRIBUTE_UNUSED,
void *opaque ATTRIBUTE_UNUSED)
{
}
static void
qemuMonitorTestErrorNotify(qemuMonitorPtr mon ATTRIBUTE_UNUSED,
virDomainObjPtr vm ATTRIBUTE_UNUSED)
virDomainObjPtr vm ATTRIBUTE_UNUSED,
void *opaque ATTRIBUTE_UNUSED)
{
}
@ -870,7 +872,8 @@ qemuMonitorTestNew(bool json, virDomainXMLOptionPtr xmlopt)
if (!(test->mon = qemuMonitorOpen(test->vm,
&src,
json,
&qemuMonitorTestCallbacks)))
&qemuMonitorTestCallbacks,
NULL)))
goto error;
virObjectLock(test->mon);