/* * qemuAgentFSThaw: * @mon: Agent * * Issue guest-fsfreeze-thaw command to guest agent, * which unfreezes all mounted file systems and returns * number of thawed file systems on success. * * Returns: number of file system thawed on success, * -1 on error. */ int qemuAgentFSThaw(qemuAgentPtr mon) { int ret = -1; virJSONValuePtr cmd; virJSONValuePtr reply = NULL; cmd = qemuAgentMakeCommand("guest-fsfreeze-thaw", NULL); if (!cmd) return -1; if (qemuAgentCommand(mon, cmd, &reply, VIR_DOMAIN_QEMU_AGENT_COMMAND_BLOCK) < 0 || qemuAgentCheckError(cmd, reply) < 0) goto cleanup; if (virJSONValueObjectGetNumberInt(reply, "return", &ret) < 0) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("malformed return value")); } cleanup: virJSONValueFree(cmd); virJSONValueFree(reply); return ret; }
/** * 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; }
static virLogHandlerLogFilePtr virLogHandlerLogFilePostExecRestart(virLogHandlerPtr handler, virJSONValuePtr object) { virLogHandlerLogFilePtr file; const char *path; if (VIR_ALLOC(file) < 0) return NULL; handler->inhibitor(true, handler->opaque); if ((path = virJSONValueObjectGetString(object, "path")) == NULL) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Missing file path in JSON document")); goto error; } if ((file->file = virRotatingFileWriterNew(path, DEFAULT_FILE_SIZE, DEFAULT_MAX_BACKUP, false, DEFAULT_MODE)) == NULL) goto error; if (virJSONValueObjectGetNumberInt(object, "pipefd", &file->pipefd) < 0) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Missing file pipefd in JSON document")); goto error; } if (virSetInherit(file->pipefd, false) < 0) { virReportSystemError(errno, "%s", _("Cannot enable close-on-exec flag")); goto error; } return file; error: handler->inhibitor(false, handler->opaque); virLogHandlerLogFileFree(file); return NULL; }
/* * qemuAgentFSFreeze: * @mon: Agent * @mountpoints: Array of mountpoint paths to be frozen, or NULL for all * @nmountpoints: Number of mountpoints to be frozen, or 0 for all * * Issue guest-fsfreeze-freeze command to guest agent, * which freezes file systems mounted on specified mountpoints * (or all file systems when @mountpoints is NULL), and returns * number of frozen file systems on success. * * Returns: number of file system frozen on success, * -1 on error. */ int qemuAgentFSFreeze(qemuAgentPtr mon, const char **mountpoints, unsigned int nmountpoints) { int ret = -1; virJSONValuePtr cmd, arg = NULL; virJSONValuePtr reply = NULL; if (mountpoints && nmountpoints) { arg = qemuAgentMakeStringsArray(mountpoints, nmountpoints); if (!arg) return -1; cmd = qemuAgentMakeCommand("guest-fsfreeze-freeze-list", "a:mountpoints", arg, NULL); } else { cmd = qemuAgentMakeCommand("guest-fsfreeze-freeze", NULL); } if (!cmd) goto cleanup; arg = 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(arg); virJSONValueFree(cmd); virJSONValueFree(reply); return ret; }
static int testJSONLookup(const void *data) { const struct testInfo *info = data; virJSONValuePtr json; virJSONValuePtr value = NULL; char *result = NULL; int rc; int number; const char *str; int ret = -1; json = virJSONValueFromString(info->doc); if (!json) { VIR_TEST_VERBOSE("Fail to parse %s\n", info->doc); ret = -1; goto cleanup; } value = virJSONValueObjectGetObject(json, "a"); if (value) { if (!info->pass) { VIR_TEST_VERBOSE("lookup for 'a' in '%s' should have failed\n", info->doc); goto cleanup; } else { result = virJSONValueToString(value, false); if (STRNEQ_NULLABLE(result, "{}")) { VIR_TEST_VERBOSE("lookup for 'a' in '%s' found '%s' but " "should have found '{}'\n", info->doc, NULLSTR(result)); goto cleanup; } VIR_FREE(result); } } else if (info->pass) { VIR_TEST_VERBOSE("lookup for 'a' in '%s' should have succeeded\n", info->doc); goto cleanup; } number = 2; rc = virJSONValueObjectGetNumberInt(json, "b", &number); if (rc == 0) { if (!info->pass) { VIR_TEST_VERBOSE("lookup for 'b' in '%s' should have failed\n", info->doc); goto cleanup; } else if (number != 1) { VIR_TEST_VERBOSE("lookup for 'b' in '%s' found %d but " "should have found 1\n", info->doc, number); goto cleanup; } } else if (info->pass) { VIR_TEST_VERBOSE("lookup for 'b' in '%s' should have succeeded\n", info->doc); goto cleanup; } str = virJSONValueObjectGetString(json, "c"); if (str) { if (!info->pass) { VIR_TEST_VERBOSE("lookup for 'c' in '%s' should have failed\n", info->doc); goto cleanup; } else if (STRNEQ(str, "str")) { VIR_TEST_VERBOSE("lookup for 'c' in '%s' found '%s' but " "should have found 'str'\n", info->doc, str); goto cleanup; } } else if (info->pass) { VIR_TEST_VERBOSE("lookup for 'c' in '%s' should have succeeded\n", info->doc); goto cleanup; } value = virJSONValueObjectGetArray(json, "d"); if (value) { if (!info->pass) { VIR_TEST_VERBOSE("lookup for 'd' in '%s' should have failed\n", info->doc); goto cleanup; } else { result = virJSONValueToString(value, false); if (STRNEQ_NULLABLE(result, "[]")) { VIR_TEST_VERBOSE("lookup for 'd' in '%s' found '%s' but " "should have found '[]'\n", info->doc, NULLSTR(result)); goto cleanup; } VIR_FREE(result); } } else if (info->pass) { VIR_TEST_VERBOSE("lookup for 'd' in '%s' should have succeeded\n", info->doc); goto cleanup; } ret = 0; cleanup: virJSONValueFree(json); VIR_FREE(result); return ret; }
virNetServerClientPtr virNetServerClientNewPostExecRestart(virJSONValuePtr object, virNetServerClientPrivNewPostExecRestart privNew, virNetServerClientPrivPreExecRestart privPreExecRestart, virFreeCallback privFree, void *privOpaque) { virJSONValuePtr child; virNetServerClientPtr client = NULL; virNetSocketPtr sock; int auth; bool readonly; unsigned int nrequests_max; if (virJSONValueObjectGetNumberInt(object, "auth", &auth) < 0) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Missing auth field in JSON state document")); return NULL; } if (virJSONValueObjectGetBoolean(object, "readonly", &readonly) < 0) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Missing readonly field in JSON state document")); return NULL; } if (virJSONValueObjectGetNumberUint(object, "nrequests_max", (unsigned int *)&nrequests_max) < 0) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Missing nrequests_client_max field in JSON state document")); return NULL; } if (!(child = virJSONValueObjectGet(object, "sock"))) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Missing sock field in JSON state document")); return NULL; } if (!(sock = virNetSocketNewPostExecRestart(child))) { virObjectUnref(sock); return NULL; } if (!(client = virNetServerClientNewInternal(sock, auth, #ifdef WITH_GNUTLS NULL, #endif readonly, nrequests_max))) { virObjectUnref(sock); return NULL; } virObjectUnref(sock); if (privNew) { if (!(child = virJSONValueObjectGet(object, "privateData"))) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Missing privateData field in JSON state document")); goto error; } if (!(client->privateData = privNew(client, child, privOpaque))) goto error; client->privateDataFreeFunc = privFree; client->privateDataPreExecRestart = privPreExecRestart; } return client; error: virObjectUnref(client); return NULL; }
virNetServerClientPtr virNetServerClientNewPostExecRestart(virJSONValuePtr object, virNetServerClientPrivNewPostExecRestart privNew, virNetServerClientPrivPreExecRestart privPreExecRestart, virFreeCallback privFree, void *privOpaque, void *opaque) { virJSONValuePtr child; virNetServerClientPtr client = NULL; virNetSocketPtr sock; int auth; bool readonly; unsigned int nrequests_max; unsigned long long id; long long timestamp; if (virJSONValueObjectGetNumberInt(object, "auth", &auth) < 0) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Missing auth field in JSON state document")); return NULL; } if (virJSONValueObjectGetBoolean(object, "readonly", &readonly) < 0) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Missing readonly field in JSON state document")); return NULL; } if (virJSONValueObjectGetNumberUint(object, "nrequests_max", &nrequests_max) < 0) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Missing nrequests_client_max field in JSON state document")); return NULL; } if (!(child = virJSONValueObjectGet(object, "sock"))) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Missing sock field in JSON state document")); return NULL; } if (!virJSONValueObjectHasKey(object, "id")) { /* no ID found in, a new one must be generated */ id = virNetServerNextClientID((virNetServerPtr) opaque); } else { if (virJSONValueObjectGetNumberUlong(object, "id", &id) < 0) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Malformed id field in JSON state document")); return NULL; } } if (!virJSONValueObjectHasKey(object, "conn_time")) { timestamp = 0; } else { if (virJSONValueObjectGetNumberLong(object, "conn_time", ×tamp) < 0) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Malformed conn_time field in JSON " "state document")); return NULL; } } if (!(sock = virNetSocketNewPostExecRestart(child))) { virObjectUnref(sock); return NULL; } if (!(client = virNetServerClientNewInternal(id, sock, auth, #ifdef WITH_GNUTLS NULL, #endif readonly, nrequests_max, timestamp))) { virObjectUnref(sock); return NULL; } virObjectUnref(sock); if (privNew) { if (!(child = virJSONValueObjectGet(object, "privateData"))) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Missing privateData field in JSON state document")); goto error; } if (!(client->privateData = privNew(client, child, privOpaque))) goto error; client->privateDataFreeFunc = privFree; client->privateDataPreExecRestart = privPreExecRestart; } return client; error: virObjectUnref(client); return NULL; }
virNetServerClientPtr virNetServerClientNewPostExecRestart(virNetServerPtr srv, virJSONValuePtr object, virNetServerClientPrivNewPostExecRestart privNew, virNetServerClientPrivPreExecRestart privPreExecRestart, virFreeCallback privFree, void *privOpaque) { virJSONValuePtr child; virNetServerClientPtr client = NULL; virNetSocketPtr sock; int auth; bool readonly, auth_pending; unsigned int nrequests_max; unsigned long long id; long long timestamp; if (virJSONValueObjectGetNumberInt(object, "auth", &auth) < 0) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Missing auth field in JSON state document")); return NULL; } if (!virJSONValueObjectHasKey(object, "auth_pending")) { auth_pending = !virNetServerClientAuthMethodImpliesAuthenticated(auth); } else { if (virJSONValueObjectGetBoolean(object, "auth_pending", &auth_pending) < 0) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Malformed auth_pending field in JSON state document")); return NULL; } /* If the used authentication method implies that the new * client is automatically authenticated, the authentication * cannot be pending */ if (auth_pending && virNetServerClientAuthMethodImpliesAuthenticated(auth)) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid auth_pending and auth combination in JSON state document")); return NULL; } } if (virJSONValueObjectGetBoolean(object, "readonly", &readonly) < 0) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Missing readonly field in JSON state document")); return NULL; } if (virJSONValueObjectGetNumberUint(object, "nrequests_max", &nrequests_max) < 0) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Missing nrequests_client_max field in JSON state document")); return NULL; } if (!(child = virJSONValueObjectGet(object, "sock"))) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Missing sock field in JSON state document")); return NULL; } if (!virJSONValueObjectHasKey(object, "id")) { /* no ID found in, a new one must be generated */ id = virNetServerNextClientID(srv); } else { if (virJSONValueObjectGetNumberUlong(object, "id", &id) < 0) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Malformed id field in JSON state document")); return NULL; } } if (!virJSONValueObjectHasKey(object, "conn_time")) { timestamp = 0; } else { if (virJSONValueObjectGetNumberLong(object, "conn_time", ×tamp) < 0) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Malformed conn_time field in JSON " "state document")); return NULL; } } if (!(sock = virNetSocketNewPostExecRestart(child))) { virObjectUnref(sock); return NULL; } if (!(client = virNetServerClientNewInternal(id, sock, auth, auth_pending, NULL, readonly, nrequests_max, timestamp))) { virObjectUnref(sock); return NULL; } virObjectUnref(sock); if (!(child = virJSONValueObjectGet(object, "privateData"))) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Missing privateData field in JSON state document")); goto error; } if (!(client->privateData = privNew(client, child, privOpaque))) goto error; client->privateDataFreeFunc = privFree; client->privateDataPreExecRestart = privPreExecRestart; return client; error: virObjectUnref(client); return NULL; }