From nobody Mon Feb 9 06:48:32 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1562789280; cv=none; d=zoho.com; s=zohoarc; b=WkjTp4c+0XFm8aQe6ccKOqMPKiqgeGZUATfQIaitBgwoItaxWR6qS54n5jwawKQcoMbXktX9NdFOszC/LhImcMPzYGEK2YwhhSSzX29XUSx6IK5sDhLijPOHUAcxxSfIp8KVy5DDzbwZ6TCbZlfRWkhky0Y3U5MnYcawNru7KYY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1562789280; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=vy0vcWFpNNDnrwlnutdh9y/AQ2qTZ2C3vp0vZ8jpXr8=; b=A6Jb0lzgDjbdKn7GOEVOROxJNkx0gE8Y5XhGrQVDxzRkY+FI4I/hE7KSFRXhZjkdpptVv66Nr7glyEaGeRbeWNa52FDA/8Tl4PQECw7MHbrfJkzB2SbvbpzDZlregzWO4pZrofVMxh3gER+v5JR720ueODgmjUZ5V8zLVlSe30k= ARC-Authentication-Results: i=1; mx.zoho.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1562789280510191.39859389667242; Wed, 10 Jul 2019 13:08:00 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 17B973092664; Wed, 10 Jul 2019 20:07:58 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 68236871F9; Wed, 10 Jul 2019 20:07:57 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 2C84D41F63; Wed, 10 Jul 2019 20:07:57 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id x6AK7rEs030800 for ; Wed, 10 Jul 2019 16:07:53 -0400 Received: by smtp.corp.redhat.com (Postfix) id C065F17D76; Wed, 10 Jul 2019 20:07:53 +0000 (UTC) Received: from aegolius.redhat.com (ovpn-116-68.phx2.redhat.com [10.3.116.68]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 8E6805DD93 for ; Wed, 10 Jul 2019 20:07:53 +0000 (UTC) From: Jonathon Jongsma To: libvir-list@redhat.com Date: Wed, 10 Jul 2019 15:07:46 -0500 Message-Id: <20190710200747.745-5-jjongsma@redhat.com> In-Reply-To: <20190710200747.745-1-jjongsma@redhat.com> References: <20190710200747.745-1-jjongsma@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH 4/5] qemu_agent: add helper for getting guest users X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.43]); Wed, 10 Jul 2019 20:07:59 +0000 (UTC) Content-Type: text/plain; charset="utf-8" This function fetches the list of logged-in users from the qemu agent and converts them into a form that can be used internally in libvirt. Also add some basic tests for the function. Signed-off-by: Jonathon Jongsma --- src/qemu/qemu_agent.c | 92 +++++++++++++++++++++++++++++++ src/qemu/qemu_agent.h | 2 + tests/qemuagenttest.c | 125 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 219 insertions(+) diff --git a/src/qemu/qemu_agent.c b/src/qemu/qemu_agent.c index 361db299a5..b70d6806cd 100644 --- a/src/qemu/qemu_agent.c +++ b/src/qemu/qemu_agent.c @@ -2240,3 +2240,95 @@ qemuAgentSetUserPassword(qemuAgentPtr mon, VIR_FREE(password64); return ret; } + +int +qemuAgentGetUsers(qemuAgentPtr mon, + virDomainUserInfoPtr **info) +{ + int ret =3D -1; + size_t i; + virJSONValuePtr cmd; + virJSONValuePtr reply =3D NULL; + virJSONValuePtr data =3D NULL; + size_t ndata; + const char *result; + virDomainUserInfoPtr *users =3D NULL; + + if (!(cmd =3D qemuAgentMakeCommand("guest-get-users", NULL))) + return -1; + + if (qemuAgentCommand(mon, cmd, &reply, true, + VIR_DOMAIN_QEMU_AGENT_COMMAND_BLOCK) < 0) + goto cleanup; + + if (!(data =3D virJSONValueObjectGetArray(reply, "return"))) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("guest-get-users reply was missing return data")); + goto cleanup; + } + + if (!virJSONValueIsArray(data)) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("Malformed guest-get-users data array")); + goto cleanup; + } + + ndata =3D virJSONValueArraySize(data); + + if (VIR_ALLOC_N(users, ndata) < 0) + goto cleanup; + + for (i =3D 0; i < ndata; i++) { + virJSONValuePtr entry =3D virJSONValueArrayGet(data, i); + if (VIR_ALLOC(users[i]) < 0) + goto cleanup; + + if (!entry) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("array element missing in guest-get-users ret= urn " + "value")); + goto cleanup; + } + + if (!(result =3D virJSONValueObjectGetString(entry, "user"))) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("'user' missing in reply of guest-get-users")= ); + goto cleanup; + } + + if (VIR_STRDUP(users[i]->user, result) < 0) { + goto cleanup; + } + + /* 'domain' is only present for windows guests */ + if ((result =3D virJSONValueObjectGetString(entry, "domain"))) { + if (VIR_STRDUP(users[i]->domain, result) < 0) { + goto cleanup; + } + } + + double loginseconds; + if (virJSONValueObjectGetNumberDouble(entry, "login-time", &logins= econds) < 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("'login-time' missing in reply of guest-get-u= sers")); + goto cleanup; + } + /* convert to milliseconds */ + users[i]->loginTime =3D loginseconds * 1000; + } + + *info =3D users; + users =3D NULL; + ret =3D ndata; + + cleanup: + if (users) { + for (i =3D 0; i < ndata; i++) { + virDomainUserInfoFree(users[i]); + } + VIR_FREE(users); + } + virJSONValueFree(cmd); + virJSONValueFree(reply); + return ret; +} diff --git a/src/qemu/qemu_agent.h b/src/qemu/qemu_agent.h index 6ae9fe54da..70797dc894 100644 --- a/src/qemu/qemu_agent.h +++ b/src/qemu/qemu_agent.h @@ -120,3 +120,5 @@ int qemuAgentSetUserPassword(qemuAgentPtr mon, const char *user, const char *password, bool crypted); + +int qemuAgentGetUsers(qemuAgentPtr mon, virDomainUserInfoPtr **info); diff --git a/tests/qemuagenttest.c b/tests/qemuagenttest.c index 2f79986207..d97f05102e 100644 --- a/tests/qemuagenttest.c +++ b/tests/qemuagenttest.c @@ -902,6 +902,130 @@ testQemuAgentGetInterfaces(const void *data) return ret; } =20 +static const char testQemuAgentUsersResponse[] =3D + "{\"return\": " + " [" + " {\"user\": \"test\"," + " \"login-time\": 1561739203.584038" + " }," + " {\"user\": \"test2\"," + " \"login-time\": 1561739229.190697" + " }" + " ]" + "}"; + +static const char testQemuAgentUsersResponse2[] =3D + "{\"return\": " + " [" + " {\"user\": \"test\"," + " \"domain\": \"DOMAIN\"," + " \"login-time\": 1561739203.584038" + " }" + " ]" + "}"; + +static int +testQemuAgentUsers(const void *data) +{ + virDomainXMLOptionPtr xmlopt =3D (virDomainXMLOptionPtr)data; + qemuMonitorTestPtr test =3D qemuMonitorTestNewAgent(xmlopt); + virDomainUserInfoPtr *userinfo =3D NULL; + int nusers; + int ret =3D -1; + int i; + + if (!test) + return -1; + + if (qemuMonitorTestAddAgentSyncResponse(test) < 0) + goto cleanup; + + if (qemuMonitorTestAddItem(test, "guest-get-users", + testQemuAgentUsersResponse) < 0) + goto cleanup; + + /* get users */ + if ((nusers =3D qemuAgentGetUsers(qemuMonitorTestGetAgent(test), + &userinfo)) < 0) + goto cleanup; + + if (nusers !=3D 2) { + virReportError(VIR_ERR_INTERNAL_ERROR, + "Expected '2' users, got '%d'", nusers); + goto cleanup; + } + if (STRNEQ(userinfo[0]->user, "test")) { + virReportError(VIR_ERR_INTERNAL_ERROR, + "Expected user name 'test', got '%s'", userinfo[0]-= >user); + goto cleanup; + } + if (userinfo[0]->loginTime !=3D 1561739203.584038) { + virReportError(VIR_ERR_INTERNAL_ERROR, + "Expected login time of '1561739203584', got '%llu'= ", + userinfo[0]->loginTime); + goto cleanup; + } + + if (STRNEQ(userinfo[1]->user, "test2")) { + virReportError(VIR_ERR_INTERNAL_ERROR, + "Expected user name 'test2', got '%s'", userinfo[1]= ->user); + goto cleanup; + } + if (userinfo[1]->loginTime !=3D 1561739229.190697) { + virReportError(VIR_ERR_INTERNAL_ERROR, + "Expected login time of '1561739229190', got '%llu'= ", + userinfo[1]->loginTime); + goto cleanup; + } + + if (qemuMonitorTestAddAgentSyncResponse(test) < 0) + goto cleanup; + + if (qemuMonitorTestAddItem(test, "guest-get-users", + testQemuAgentUsersResponse2) < 0) + goto cleanup; + + VIR_FREE(userinfo); + /* get users with domain */ + if ((nusers =3D qemuAgentGetUsers(qemuMonitorTestGetAgent(test), + &userinfo)) < 0) + goto cleanup; + + if (nusers !=3D 1) { + virReportError(VIR_ERR_INTERNAL_ERROR, + "Expected '1' user, got '%d'", nusers); + goto cleanup; + } + + if (STRNEQ(userinfo[0]->user, "test")) { + virReportError(VIR_ERR_INTERNAL_ERROR, + "Expected user name 'test', got '%s'", userinfo[0]-= >user); + goto cleanup; + } + if (userinfo[0]->loginTime !=3D 1561739203.584038) { + virReportError(VIR_ERR_INTERNAL_ERROR, + "Expected login time of '1561739203584', got '%llu'= ", + userinfo[0]->loginTime); + goto cleanup; + } + if (STRNEQ(userinfo[0]->domain, "DOMAIN")) { + virReportError(VIR_ERR_INTERNAL_ERROR, + "Expected domain 'DOMAIN', got '%s'", userinfo[0]->= domain); + goto cleanup; + } + ret =3D 0; + + cleanup: + if (nusers >=3D 0) { + for (i =3D 0; i < nusers; i++) + virDomainUserInfoFree(userinfo[i]); + } + VIR_FREE(userinfo); + qemuMonitorTestFree(test); + return ret; +} + + static int mymain(void) { @@ -931,6 +1055,7 @@ mymain(void) DO_TEST(CPU); DO_TEST(ArbitraryCommand); DO_TEST(GetInterfaces); + DO_TEST(Users); =20 DO_TEST(Timeout); /* Timeout should always be called last */ =20 --=20 2.20.1 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list