virJSONValuePtr virNetServerClientPreExecRestart(virNetServerClientPtr client) { virJSONValuePtr object = virJSONValueNewObject(); virJSONValuePtr child; if (!object) return NULL; virObjectLock(client); if (virJSONValueObjectAppendNumberUlong(object, "id", client->id) < 0) goto error; if (virJSONValueObjectAppendNumberInt(object, "auth", client->auth) < 0) goto error; if (virJSONValueObjectAppendBoolean(object, "auth_pending", client->auth_pending) < 0) goto error; if (virJSONValueObjectAppendBoolean(object, "readonly", client->readonly) < 0) goto error; if (virJSONValueObjectAppendNumberUint(object, "nrequests_max", client->nrequests_max) < 0) goto error; if (client->conn_time && virJSONValueObjectAppendNumberLong(object, "conn_time", client->conn_time) < 0) goto error; if (!(child = virNetSocketPreExecRestart(client->sock))) goto error; if (virJSONValueObjectAppend(object, "sock", child) < 0) { virJSONValueFree(child); goto error; } if (!(child = client->privateDataPreExecRestart(client, client->privateData))) goto error; if (virJSONValueObjectAppend(object, "privateData", child) < 0) { virJSONValueFree(child); goto error; } virObjectUnlock(client); return object; error: virObjectUnlock(client); virJSONValueFree(object); return NULL; }
/** * Set the VCPU state using guest agent. * * Returns -1 on error, ninfo in case everything was successful and less than * ninfo on a partial failure. */ int qemuAgentSetVCPUs(qemuAgentPtr mon, qemuAgentCPUInfoPtr info, size_t ninfo) { int ret = -1; virJSONValuePtr cmd = NULL; virJSONValuePtr reply = NULL; virJSONValuePtr cpus = NULL; virJSONValuePtr cpu = NULL; size_t i; /* create the key data array */ if (!(cpus = virJSONValueNewArray())) goto cleanup; for (i = 0; i < ninfo; i++) { qemuAgentCPUInfoPtr in = &info[i]; /* create single cpu object */ if (!(cpu = virJSONValueNewObject())) goto cleanup; if (virJSONValueObjectAppendNumberInt(cpu, "logical-id", in->id) < 0) goto cleanup; if (virJSONValueObjectAppendBoolean(cpu, "online", in->online) < 0) goto cleanup; if (virJSONValueArrayAppend(cpus, cpu) < 0) goto cleanup; cpu = NULL; } if (!(cmd = qemuAgentMakeCommand("guest-set-vcpus", "a:vcpus", cpus, NULL))) goto cleanup; cpus = NULL; if (qemuAgentCommand(mon, cmd, &reply, true, VIR_DOMAIN_QEMU_AGENT_COMMAND_BLOCK) < 0) goto cleanup; if (virJSONValueObjectGetNumberInt(reply, "return", &ret) < 0) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("malformed return value")); } cleanup: virJSONValueFree(cmd); virJSONValueFree(reply); virJSONValueFree(cpu); virJSONValueFree(cpus); return ret; }
virJSONValuePtr virNetServerPreExecRestart(virNetServerPtr srv) { virJSONValuePtr object; virJSONValuePtr clients; virJSONValuePtr services; size_t i; virObjectLock(srv); if (!(object = virJSONValueNewObject())) goto error; if (virJSONValueObjectAppendNumberUint(object, "min_workers", virThreadPoolGetMinWorkers(srv->workers)) < 0) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Cannot set min_workers data in JSON document")); goto error; } if (virJSONValueObjectAppendNumberUint(object, "max_workers", virThreadPoolGetMaxWorkers(srv->workers)) < 0) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Cannot set max_workers data in JSON document")); goto error; } if (virJSONValueObjectAppendNumberUint(object, "priority_workers", virThreadPoolGetPriorityWorkers(srv->workers)) < 0) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Cannot set priority_workers data in JSON document")); goto error; } if (virJSONValueObjectAppendNumberUint(object, "max_clients", srv->nclients_max) < 0) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Cannot set max_clients data in JSON document")); goto error; } if (virJSONValueObjectAppendNumberUint(object, "max_anonymous_clients", srv->nclients_unauth_max) < 0) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Cannot set max_anonymous_clients data in JSON document")); goto error; } if (virJSONValueObjectAppendNumberUint(object, "keepaliveInterval", srv->keepaliveInterval) < 0) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Cannot set keepaliveInterval data in JSON document")); goto error; } if (virJSONValueObjectAppendNumberUint(object, "keepaliveCount", srv->keepaliveCount) < 0) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Cannot set keepaliveCount data in JSON document")); goto error; } if (virJSONValueObjectAppendBoolean(object, "keepaliveRequired", srv->keepaliveRequired) < 0) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Cannot set keepaliveRequired data in JSON document")); goto error; } if (srv->mdnsGroupName && virJSONValueObjectAppendString(object, "mdnsGroupName", srv->mdnsGroupName) < 0) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Cannot set mdnsGroupName data in JSON document")); goto error; } services = virJSONValueNewArray(); if (virJSONValueObjectAppend(object, "services", services) < 0) { virJSONValueFree(services); goto error; } for (i = 0; i < srv->nservices; i++) { virJSONValuePtr child; if (!(child = virNetServerServicePreExecRestart(srv->services[i]))) goto error; if (virJSONValueArrayAppend(services, child) < 0) { virJSONValueFree(child); goto error; } } clients = virJSONValueNewArray(); if (virJSONValueObjectAppend(object, "clients", clients) < 0) { virJSONValueFree(clients); goto error; } for (i = 0; i < srv->nclients; i++) { virJSONValuePtr child; if (!(child = virNetServerClientPreExecRestart(srv->clients[i]))) goto error; if (virJSONValueArrayAppend(clients, child) < 0) { virJSONValueFree(child); goto error; } } virObjectUnlock(srv); return object; error: virJSONValueFree(object); virObjectUnlock(srv); return NULL; }
static virJSONValuePtr ATTRIBUTE_SENTINEL qemuAgentMakeCommand(const char *cmdname, ...) { virJSONValuePtr obj; virJSONValuePtr jargs = NULL; va_list args; char *key; va_start(args, cmdname); if (!(obj = virJSONValueNewObject())) goto no_memory; if (virJSONValueObjectAppendString(obj, "execute", cmdname) < 0) goto no_memory; while ((key = va_arg(args, char *)) != NULL) { int ret; char type; if (strlen(key) < 3) { virReportError(VIR_ERR_INTERNAL_ERROR, _("argument key '%s' is too short, missing type prefix"), key); goto error; } /* Keys look like s:name the first letter is a type code */ type = key[0]; key += 2; if (!jargs && !(jargs = virJSONValueNewObject())) goto no_memory; /* This doesn't support maps/arrays. This hasn't * proved to be a problem..... yet :-) */ switch (type) { case 's': { char *val = va_arg(args, char *); ret = virJSONValueObjectAppendString(jargs, key, val); } break; case 'i': { int val = va_arg(args, int); ret = virJSONValueObjectAppendNumberInt(jargs, key, val); } break; case 'u': { unsigned int val = va_arg(args, unsigned int); ret = virJSONValueObjectAppendNumberUint(jargs, key, val); } break; case 'I': { long long val = va_arg(args, long long); ret = virJSONValueObjectAppendNumberLong(jargs, key, val); } break; case 'U': { /* qemu silently truncates numbers larger than LLONG_MAX, * so passing the full range of unsigned 64 bit integers * is not safe here. Pass them as signed 64 bit integers * instead. */ long long val = va_arg(args, long long); ret = virJSONValueObjectAppendNumberLong(jargs, key, val); } break; case 'd': { double val = va_arg(args, double); ret = virJSONValueObjectAppendNumberDouble(jargs, key, val); } break; case 'b': { int val = va_arg(args, int); ret = virJSONValueObjectAppendBoolean(jargs, key, val); } break; case 'n': { ret = virJSONValueObjectAppendNull(jargs, key); } break; case 'a': { virJSONValuePtr val = va_arg(args, virJSONValuePtr); ret = virJSONValueObjectAppend(jargs, key, val); } break; default: virReportError(VIR_ERR_INTERNAL_ERROR, _("unsupported data type '%c' for arg '%s'"), type, key - 2); goto error; } if (ret < 0) goto no_memory; } if (jargs && virJSONValueObjectAppend(obj, "arguments", jargs) < 0) goto no_memory; va_end(args); return obj; no_memory: virReportOOMError(); error: virJSONValueFree(obj); virJSONValueFree(jargs); va_end(args); return NULL; }