qemu: monitor: Rename and improve qemuMonitorGetPtyPaths

To unify future additions that require information from "query-chardev"
rename qemuMonitorGetPtyPaths and friends to qemuMonitorGetChardevInfo
and move the allocation of the returned hash into the top level
function.
This commit is contained in:
Peter Krempa 2014-11-13 16:17:21 +01:00
parent c5942a9faa
commit e9a4506963
8 changed files with 66 additions and 52 deletions

View File

@ -2968,24 +2968,39 @@ qemuMonitorQueryRxFilter(qemuMonitorPtr mon, const char *alias,
} }
int qemuMonitorGetPtyPaths(qemuMonitorPtr mon, int
virHashTablePtr paths) qemuMonitorGetChardevInfo(qemuMonitorPtr mon,
virHashTablePtr *retinfo)
{ {
int ret; int ret;
VIR_DEBUG("mon=%p", virHashTablePtr info = NULL;
mon);
VIR_DEBUG("mon=%p retinfo=%p", mon, retinfo);
if (!mon) { if (!mon) {
virReportError(VIR_ERR_INVALID_ARG, "%s", virReportError(VIR_ERR_INVALID_ARG, "%s",
_("monitor must not be NULL")); _("monitor must not be NULL"));
return -1; goto error;
} }
if (!(info = virHashCreate(10, virHashValueFree)))
goto error;
if (mon->json) if (mon->json)
ret = qemuMonitorJSONGetPtyPaths(mon, paths); ret = qemuMonitorJSONGetChardevInfo(mon, info);
else else
ret = qemuMonitorTextGetPtyPaths(mon, paths); ret = qemuMonitorTextGetChardevInfo(mon, info);
return ret;
if (ret < 0)
goto error;
*retinfo = info;
return 0;
error:
virHashFree(info);
*retinfo = NULL;
return -1;
} }

View File

@ -639,8 +639,8 @@ int qemuMonitorRemoveNetdev(qemuMonitorPtr mon,
int qemuMonitorQueryRxFilter(qemuMonitorPtr mon, const char *alias, int qemuMonitorQueryRxFilter(qemuMonitorPtr mon, const char *alias,
virNetDevRxFilterPtr *filter); virNetDevRxFilterPtr *filter);
int qemuMonitorGetPtyPaths(qemuMonitorPtr mon, int qemuMonitorGetChardevInfo(qemuMonitorPtr mon,
virHashTablePtr paths); virHashTablePtr *retinfo);
int qemuMonitorAttachPCIDiskController(qemuMonitorPtr mon, int qemuMonitorAttachPCIDiskController(qemuMonitorPtr mon,
const char *bus, const char *bus,

View File

@ -3396,8 +3396,9 @@ qemuMonitorJSONQueryRxFilter(qemuMonitorPtr mon, const char *alias,
* ]} * ]}
* *
*/ */
static int qemuMonitorJSONExtractPtyPaths(virJSONValuePtr reply, static int
virHashTablePtr paths) qemuMonitorJSONExtractChardevInfo(virJSONValuePtr reply,
virHashTablePtr info)
{ {
virJSONValuePtr data; virJSONValuePtr data;
int ret = -1; int ret = -1;
@ -3442,7 +3443,7 @@ static int qemuMonitorJSONExtractPtyPaths(virJSONValuePtr reply,
if (VIR_STRDUP(path, type + strlen("pty:")) < 0) if (VIR_STRDUP(path, type + strlen("pty:")) < 0)
goto cleanup; goto cleanup;
if (virHashAddEntry(paths, id, path) < 0) { if (virHashAddEntry(info, id, path) < 0) {
virReportError(VIR_ERR_OPERATION_FAILED, virReportError(VIR_ERR_OPERATION_FAILED,
_("failed to save chardev path '%s'"), path); _("failed to save chardev path '%s'"), path);
VIR_FREE(path); VIR_FREE(path);
@ -3457,8 +3458,10 @@ static int qemuMonitorJSONExtractPtyPaths(virJSONValuePtr reply,
return ret; return ret;
} }
int qemuMonitorJSONGetPtyPaths(qemuMonitorPtr mon,
virHashTablePtr paths) int
qemuMonitorJSONGetChardevInfo(qemuMonitorPtr mon,
virHashTablePtr info)
{ {
int ret; int ret;
@ -3475,7 +3478,7 @@ int qemuMonitorJSONGetPtyPaths(qemuMonitorPtr mon,
ret = qemuMonitorJSONCheckError(cmd, reply); ret = qemuMonitorJSONCheckError(cmd, reply);
if (ret == 0) if (ret == 0)
ret = qemuMonitorJSONExtractPtyPaths(reply, paths); ret = qemuMonitorJSONExtractChardevInfo(reply, info);
virJSONValueFree(cmd); virJSONValueFree(cmd);
virJSONValueFree(reply); virJSONValueFree(reply);

View File

@ -214,8 +214,8 @@ int qemuMonitorJSONRemoveNetdev(qemuMonitorPtr mon,
int qemuMonitorJSONQueryRxFilter(qemuMonitorPtr mon, const char *alias, int qemuMonitorJSONQueryRxFilter(qemuMonitorPtr mon, const char *alias,
virNetDevRxFilterPtr *filter); virNetDevRxFilterPtr *filter);
int qemuMonitorJSONGetPtyPaths(qemuMonitorPtr mon, int qemuMonitorJSONGetChardevInfo(qemuMonitorPtr mon,
virHashTablePtr paths); virHashTablePtr info);
int qemuMonitorJSONAttachPCIDiskController(qemuMonitorPtr mon, int qemuMonitorJSONAttachPCIDiskController(qemuMonitorPtr mon,
const char *bus, const char *bus,

View File

@ -2170,8 +2170,8 @@ int qemuMonitorTextRemoveNetdev(qemuMonitorPtr mon,
* '/dev/pty/7'. The hash will contain only a single value. * '/dev/pty/7'. The hash will contain only a single value.
*/ */
int qemuMonitorTextGetPtyPaths(qemuMonitorPtr mon, int qemuMonitorTextGetChardevInfo(qemuMonitorPtr mon,
virHashTablePtr paths) virHashTablePtr info)
{ {
char *reply = NULL; char *reply = NULL;
int ret = -1; int ret = -1;
@ -2222,7 +2222,7 @@ int qemuMonitorTextGetPtyPaths(qemuMonitorPtr mon,
if (VIR_STRDUP(path, needle + strlen(NEEDLE)) < 0) if (VIR_STRDUP(path, needle + strlen(NEEDLE)) < 0)
goto cleanup; goto cleanup;
if (virHashAddEntry(paths, id, path) < 0) { if (virHashAddEntry(info, id, path) < 0) {
virReportError(VIR_ERR_OPERATION_FAILED, virReportError(VIR_ERR_OPERATION_FAILED,
_("failed to save chardev path '%s'"), _("failed to save chardev path '%s'"),
path); path);

View File

@ -179,8 +179,8 @@ int qemuMonitorTextAddNetdev(qemuMonitorPtr mon,
int qemuMonitorTextRemoveNetdev(qemuMonitorPtr mon, int qemuMonitorTextRemoveNetdev(qemuMonitorPtr mon,
const char *alias); const char *alias);
int qemuMonitorTextGetPtyPaths(qemuMonitorPtr mon, int qemuMonitorTextGetChardevInfo(qemuMonitorPtr mon,
virHashTablePtr paths); virHashTablePtr info);
int qemuMonitorTextAttachPCIDiskController(qemuMonitorPtr mon, int qemuMonitorTextAttachPCIDiskController(qemuMonitorPtr mon,
const char *bus, const char *bus,

View File

@ -1865,7 +1865,7 @@ qemuProcessLookupPTYs(virDomainDefPtr def,
virQEMUCapsPtr qemuCaps, virQEMUCapsPtr qemuCaps,
virDomainChrDefPtr *devices, virDomainChrDefPtr *devices,
int count, int count,
virHashTablePtr paths) virHashTablePtr info)
{ {
size_t i; size_t i;
@ -1886,7 +1886,7 @@ qemuProcessLookupPTYs(virDomainDefPtr def,
return -1; return -1;
} }
path = (const char *) virHashLookup(paths, id); path = (const char *) virHashLookup(info, id);
if (path == NULL) { if (path == NULL) {
if (chr->source.data.file.path == NULL) { if (chr->source.data.file.path == NULL) {
/* neither the log output nor 'info chardev' had a /* neither the log output nor 'info chardev' had a
@ -1915,23 +1915,23 @@ qemuProcessLookupPTYs(virDomainDefPtr def,
static int static int
qemuProcessFindCharDevicePTYsMonitor(virDomainObjPtr vm, qemuProcessFindCharDevicePTYsMonitor(virDomainObjPtr vm,
virQEMUCapsPtr qemuCaps, virQEMUCapsPtr qemuCaps,
virHashTablePtr paths) virHashTablePtr info)
{ {
size_t i = 0; size_t i = 0;
if (qemuProcessLookupPTYs(vm->def, qemuCaps, if (qemuProcessLookupPTYs(vm->def, qemuCaps,
vm->def->serials, vm->def->nserials, vm->def->serials, vm->def->nserials,
paths) < 0) info) < 0)
return -1; return -1;
if (qemuProcessLookupPTYs(vm->def, qemuCaps, if (qemuProcessLookupPTYs(vm->def, qemuCaps,
vm->def->parallels, vm->def->nparallels, vm->def->parallels, vm->def->nparallels,
paths) < 0) info) < 0)
return -1; return -1;
if (qemuProcessLookupPTYs(vm->def, qemuCaps, if (qemuProcessLookupPTYs(vm->def, qemuCaps,
vm->def->channels, vm->def->nchannels, vm->def->channels, vm->def->nchannels,
paths) < 0) info) < 0)
return -1; return -1;
/* For historical reasons, console[0] can be just an alias /* For historical reasons, console[0] can be just an alias
* for serial[0]. That's why we need to update it as well. */ * for serial[0]. That's why we need to update it as well. */
@ -1951,7 +1951,7 @@ qemuProcessFindCharDevicePTYsMonitor(virDomainObjPtr vm,
if (qemuProcessLookupPTYs(vm->def, qemuCaps, if (qemuProcessLookupPTYs(vm->def, qemuCaps,
vm->def->consoles + i, vm->def->nconsoles - i, vm->def->consoles + i, vm->def->nconsoles - i,
paths) < 0) info) < 0)
return -1; return -1;
return 0; return 0;
@ -2035,7 +2035,7 @@ qemuProcessWaitForMonitor(virQEMUDriverPtr driver,
size_t buf_size = 4096; /* Plenty of space to get startup greeting */ size_t buf_size = 4096; /* Plenty of space to get startup greeting */
int logfd = -1; int logfd = -1;
int ret = -1; int ret = -1;
virHashTablePtr paths = NULL; virHashTablePtr info = NULL;
qemuDomainObjPrivatePtr priv; qemuDomainObjPrivatePtr priv;
if (pos != -1 && if (pos != -1 &&
@ -2060,22 +2060,18 @@ qemuProcessWaitForMonitor(virQEMUDriverPtr driver,
* reliable if it's available. * reliable if it's available.
* Note that the monitor itself can be on a pty, so we still need to try the * Note that the monitor itself can be on a pty, so we still need to try the
* log output method. */ * log output method. */
paths = virHashCreate(0, virHashValueFree);
if (paths == NULL)
goto cleanup;
priv = vm->privateData; priv = vm->privateData;
if (qemuDomainObjEnterMonitorAsync(driver, vm, asyncJob) < 0) if (qemuDomainObjEnterMonitorAsync(driver, vm, asyncJob) < 0)
goto cleanup; goto cleanup;
ret = qemuMonitorGetPtyPaths(priv->mon, paths); ret = qemuMonitorGetChardevInfo(priv->mon, &info);
qemuDomainObjExitMonitor(driver, vm); qemuDomainObjExitMonitor(driver, vm);
VIR_DEBUG("qemuMonitorGetPtyPaths returned %i", ret); VIR_DEBUG("qemuMonitorGetChardevInfo returned %i", ret);
if (ret == 0) if (ret == 0)
ret = qemuProcessFindCharDevicePTYsMonitor(vm, qemuCaps, paths); ret = qemuProcessFindCharDevicePTYsMonitor(vm, qemuCaps, info);
cleanup: cleanup:
virHashFree(paths); virHashFree(info);
if (pos != -1 && kill(vm->pid, 0) == -1 && errno == ESRCH) { if (pos != -1 && kill(vm->pid, 0) == -1 && errno == ESRCH) {
int len; int len;

View File

@ -1765,24 +1765,24 @@ testHashEqualString(const void *value1, const void *value2)
} }
static int static int
testQemuMonitorJSONqemuMonitorJSONGetPtyPaths(const void *data) testQemuMonitorJSONqemuMonitorJSONGetChardevInfo(const void *data)
{ {
virDomainXMLOptionPtr xmlopt = (virDomainXMLOptionPtr)data; virDomainXMLOptionPtr xmlopt = (virDomainXMLOptionPtr)data;
qemuMonitorTestPtr test = qemuMonitorTestNewSimple(true, xmlopt); qemuMonitorTestPtr test = qemuMonitorTestNewSimple(true, xmlopt);
int ret = -1; int ret = -1;
virHashTablePtr paths = NULL, expectedPaths = NULL; virHashTablePtr info = NULL, expectedInfo = NULL;
if (!test) if (!test)
return -1; return -1;
if (!(paths = virHashCreate(32, (virHashDataFree) free)) || if (!(info = virHashCreate(32, (virHashDataFree) free)) ||
!(expectedPaths = virHashCreate(32, NULL))) !(expectedInfo = virHashCreate(32, NULL)))
goto cleanup; goto cleanup;
if (virHashAddEntry(expectedPaths, "charserial1", (void *) "/dev/pts/21") < 0 || if (virHashAddEntry(expectedInfo, "charserial1", (void *) "/dev/pts/21") < 0 ||
virHashAddEntry(expectedPaths, "charserial0", (void *) "/dev/pts/20") < 0) { virHashAddEntry(expectedInfo, "charserial0", (void *) "/dev/pts/20") < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
"Unable to create expectedPaths hash table"); "Unable to create expectedInfo hash table");
goto cleanup; goto cleanup;
} }
@ -1806,11 +1806,11 @@ testQemuMonitorJSONqemuMonitorJSONGetPtyPaths(const void *data)
"}") < 0) "}") < 0)
goto cleanup; goto cleanup;
if (qemuMonitorJSONGetPtyPaths(qemuMonitorTestGetMonitor(test), if (qemuMonitorJSONGetChardevInfo(qemuMonitorTestGetMonitor(test),
paths) < 0) info) < 0)
goto cleanup; goto cleanup;
if (!virHashEqual(paths, expectedPaths, testHashEqualString)) { if (!virHashEqual(info, expectedInfo, testHashEqualString)) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
"Hashtable is different to the expected one"); "Hashtable is different to the expected one");
goto cleanup; goto cleanup;
@ -1818,8 +1818,8 @@ testQemuMonitorJSONqemuMonitorJSONGetPtyPaths(const void *data)
ret = 0; ret = 0;
cleanup: cleanup:
virHashFree(paths); virHashFree(info);
virHashFree(expectedPaths); virHashFree(expectedInfo);
qemuMonitorTestFree(test); qemuMonitorTestFree(test);
return ret; return ret;
} }
@ -2387,7 +2387,7 @@ mymain(void)
DO_TEST(qemuMonitorJSONGetMigrationCacheSize); DO_TEST(qemuMonitorJSONGetMigrationCacheSize);
DO_TEST(qemuMonitorJSONGetMigrationStatus); DO_TEST(qemuMonitorJSONGetMigrationStatus);
DO_TEST(qemuMonitorJSONGetSpiceMigrationStatus); DO_TEST(qemuMonitorJSONGetSpiceMigrationStatus);
DO_TEST(qemuMonitorJSONGetPtyPaths); DO_TEST(qemuMonitorJSONGetChardevInfo);
DO_TEST(qemuMonitorJSONSetBlockIoThrottle); DO_TEST(qemuMonitorJSONSetBlockIoThrottle);
DO_TEST(qemuMonitorJSONGetTargetArch); DO_TEST(qemuMonitorJSONGetTargetArch);
DO_TEST(qemuMonitorJSONGetMigrationCapability); DO_TEST(qemuMonitorJSONGetMigrationCapability);