Switch over to passing a callback table to QEMU monitor

With addition of events there will be alot of callbacks.
To avoid having to add many APIs to register callbacks,
provide them all at once in a big table

* src/qemu/qemu_driver.c: Pass in a callback table to QEMU
  monitor code
* src/qemu/qemu_monitor.c, src/qemu/qemu_monitor.h Replace
  the EOF and disk secret callbacks with a callback table
This commit is contained in:
Daniel P. Berrange 2009-10-15 18:56:52 +01:00
parent 3a4f172fdd
commit e9f4c94301
3 changed files with 45 additions and 36 deletions

View File

@ -794,6 +794,11 @@ cleanup:
return ret; return ret;
} }
static qemuMonitorCallbacks monitorCallbacks = {
.eofNotify = qemuHandleMonitorEOF,
.diskSecretLookup = findVolumeQcowPassphrase,
};
static int static int
qemuConnectMonitor(virDomainObjPtr vm) qemuConnectMonitor(virDomainObjPtr vm)
{ {
@ -806,14 +811,11 @@ qemuConnectMonitor(virDomainObjPtr vm)
if ((priv->mon = qemuMonitorOpen(vm, if ((priv->mon = qemuMonitorOpen(vm,
priv->monConfig, priv->monConfig,
priv->monJSON, priv->monJSON,
qemuHandleMonitorEOF)) == NULL) { &monitorCallbacks)) == NULL) {
VIR_ERROR(_("Failed to connect monitor for %s\n"), vm->def->name); VIR_ERROR(_("Failed to connect monitor for %s\n"), vm->def->name);
return -1; return -1;
} }
qemuMonitorRegisterDiskSecretLookup(priv->mon,
findVolumeQcowPassphrase);
return 0; return 0;
} }

View File

@ -51,8 +51,7 @@ struct _qemuMonitor {
virDomainObjPtr vm; virDomainObjPtr vm;
qemuMonitorEOFNotify eofCB; qemuMonitorCallbacksPtr cb;
qemuMonitorDiskSecretLookup secretCB;
/* If there's a command being processed this will be /* If there's a command being processed this will be
* non-NULL */ * non-NULL */
@ -517,14 +516,15 @@ qemuMonitorIO(int watch, int fd, int events, void *opaque) {
* but is this safe ? I think it is, because the callback * but is this safe ? I think it is, because the callback
* will try to acquire the virDomainObjPtr mutex next */ * will try to acquire the virDomainObjPtr mutex next */
if (failed || quit) { if (failed || quit) {
qemuMonitorEOFNotify eofCB = mon->eofCB; void (*eofNotify)(qemuMonitorPtr, virDomainObjPtr, int)
= mon->cb->eofNotify;
virDomainObjPtr vm = mon->vm; virDomainObjPtr vm = mon->vm;
/* Make sure anyone waiting wakes up now */ /* Make sure anyone waiting wakes up now */
virCondSignal(&mon->notify); virCondSignal(&mon->notify);
if (qemuMonitorUnref(mon) > 0) if (qemuMonitorUnref(mon) > 0)
qemuMonitorUnlock(mon); qemuMonitorUnlock(mon);
VIR_DEBUG("Triggering EOF callback error? %d", failed); VIR_DEBUG("Triggering EOF callback error? %d", failed);
(eofCB)(mon, vm, failed); (eofNotify)(mon, vm, failed);
} else { } else {
if (qemuMonitorUnref(mon) > 0) if (qemuMonitorUnref(mon) > 0)
qemuMonitorUnlock(mon); qemuMonitorUnlock(mon);
@ -536,10 +536,16 @@ qemuMonitorPtr
qemuMonitorOpen(virDomainObjPtr vm, qemuMonitorOpen(virDomainObjPtr vm,
virDomainChrDefPtr config, virDomainChrDefPtr config,
int json, int json,
qemuMonitorEOFNotify eofCB) qemuMonitorCallbacksPtr cb)
{ {
qemuMonitorPtr mon; qemuMonitorPtr mon;
if (!cb || !cb->eofNotify) {
qemudReportError(NULL, NULL, NULL, VIR_ERR_INTERNAL_ERROR, "%s",
_("EOF notify callback must be supplied"));
return NULL;
}
if (VIR_ALLOC(mon) < 0) { if (VIR_ALLOC(mon) < 0) {
virReportOOMError(NULL); virReportOOMError(NULL);
return NULL; return NULL;
@ -561,8 +567,8 @@ qemuMonitorOpen(virDomainObjPtr vm,
mon->fd = -1; mon->fd = -1;
mon->refs = 1; mon->refs = 1;
mon->vm = vm; mon->vm = vm;
mon->eofCB = eofCB;
mon->json = json; mon->json = json;
mon->cb = cb;
qemuMonitorLock(mon); qemuMonitorLock(mon);
switch (config->type) { switch (config->type) {
@ -648,13 +654,6 @@ int qemuMonitorClose(qemuMonitorPtr mon)
} }
void qemuMonitorRegisterDiskSecretLookup(qemuMonitorPtr mon,
qemuMonitorDiskSecretLookup secretCB)
{
mon->secretCB = secretCB;
}
int qemuMonitorSend(qemuMonitorPtr mon, int qemuMonitorSend(qemuMonitorPtr mon,
qemuMonitorMessagePtr msg) qemuMonitorMessagePtr msg)
{ {
@ -690,10 +689,17 @@ int qemuMonitorGetDiskSecret(qemuMonitorPtr mon,
char **secret, char **secret,
size_t *secretLen) size_t *secretLen)
{ {
int ret = -1;
*secret = NULL; *secret = NULL;
*secretLen = 0; *secretLen = 0;
return mon->secretCB(mon, conn, mon->vm, path, secret, secretLen); qemuMonitorRef(mon);
qemuMonitorUnlock(mon);
if (mon->cb && mon->cb->diskSecretLookup)
ret = mon->cb->diskSecretLookup(mon, conn, mon->vm, path, secret, secretLen);
qemuMonitorLock(mon);
qemuMonitorUnref(mon);
return ret;
} }

View File

@ -59,21 +59,25 @@ struct _qemuMonitorMessage {
void *passwordOpaque; void *passwordOpaque;
}; };
typedef void (*qemuMonitorEOFNotify)(qemuMonitorPtr mon, typedef struct _qemuMonitorCallbacks qemuMonitorCallbacks;
typedef qemuMonitorCallbacks *qemuMonitorCallbacksPtr;
struct _qemuMonitorCallbacks {
void (*eofNotify)(qemuMonitorPtr mon,
virDomainObjPtr vm, virDomainObjPtr vm,
int withError); int withError);
/* XXX we'd really like to avoid virCOnnectPtr here /* XXX we'd really like to avoid virCOnnectPtr here
* It is required so the callback can find the active * It is required so the callback can find the active
* secret driver. Need to change this to work like the * secret driver. Need to change this to work like the
* security drivers do, to avoid this * security drivers do, to avoid this
*/ */
typedef int (*qemuMonitorDiskSecretLookup)(qemuMonitorPtr mon, int (*diskSecretLookup)(qemuMonitorPtr mon,
virConnectPtr conn, virConnectPtr conn,
virDomainObjPtr vm, virDomainObjPtr vm,
const char *path, const char *path,
char **secret, char **secret,
size_t *secretLen); size_t *secretLen);
};
char *qemuMonitorEscapeArg(const char *in); char *qemuMonitorEscapeArg(const char *in);
char *qemuMonitorEscapeShell(const char *in); char *qemuMonitorEscapeShell(const char *in);
@ -81,7 +85,7 @@ char *qemuMonitorEscapeShell(const char *in);
qemuMonitorPtr qemuMonitorOpen(virDomainObjPtr vm, qemuMonitorPtr qemuMonitorOpen(virDomainObjPtr vm,
virDomainChrDefPtr config, virDomainChrDefPtr config,
int json, int json,
qemuMonitorEOFNotify eofCB); qemuMonitorCallbacksPtr cb);
int qemuMonitorClose(qemuMonitorPtr mon); int qemuMonitorClose(qemuMonitorPtr mon);
@ -91,9 +95,6 @@ void qemuMonitorUnlock(qemuMonitorPtr mon);
int qemuMonitorRef(qemuMonitorPtr mon); int qemuMonitorRef(qemuMonitorPtr mon);
int qemuMonitorUnref(qemuMonitorPtr mon); int qemuMonitorUnref(qemuMonitorPtr mon);
void qemuMonitorRegisterDiskSecretLookup(qemuMonitorPtr mon,
qemuMonitorDiskSecretLookup secretCB);
/* This API is for use by the internal Text/JSON monitor impl code only */ /* This API is for use by the internal Text/JSON monitor impl code only */
int qemuMonitorSend(qemuMonitorPtr mon, int qemuMonitorSend(qemuMonitorPtr mon,
qemuMonitorMessagePtr msg); qemuMonitorMessagePtr msg);