From patchwork Tue Nov 3 01:11:24 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 316359 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-12.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, MSGID_FROM_MTA_HEADER, SIGNED_OFF_BY, SPF_HELO_NONE, SPF_PASS, URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id D0724C00A89 for ; Tue, 3 Nov 2020 01:16:35 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 3DE3820870 for ; Tue, 3 Nov 2020 01:16:35 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=amdcloud.onmicrosoft.com header.i=@amdcloud.onmicrosoft.com header.b="rQljEs4I" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 3DE3820870 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=amd.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Received: from localhost ([::1]:36072 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kZkw6-0008P9-AU for qemu-devel@archiver.kernel.org; Mon, 02 Nov 2020 20:16:34 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:50940) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kZksX-0006c1-JX for qemu-devel@nongnu.org; Mon, 02 Nov 2020 20:12:53 -0500 Received: from mail-mw2nam10on2058.outbound.protection.outlook.com ([40.107.94.58]:64832 helo=NAM10-MW2-obe.outbound.protection.outlook.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kZksW-0005WU-2S for qemu-devel@nongnu.org; Mon, 02 Nov 2020 20:12:53 -0500 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=NTqY3kRWKh2Dgh5mBuLPfTGsWMsD48ATck8O2qQsa2c5xIRV8mTXqsxiSGb/vlRIhHc7vZeI7xdS0ZUToIr51TLa7oOnHrP/EtVLumPr2FqZ1s0bXHn/faHAP0miTo1as9a29b7mhF+IeOzkiHaltvV9GNpfZ6BH8whj7An1AzzThfyVt7e/XN0jsAX0P4UqQaUVicnZui7LoU8u1ngpldnA7ONWZIQ5wj9FpCOiHkpEpgJUaESz0LfDYnaRCZDusxoXW4HWzN0YC6aSRyCkdLZ/SsPvgZDCJHMmPpvgeADDrxqTTx/jvDBvIc80CLHCSCqjCiLiegSI9pMocF0Xgw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=T8O8TEJqM9sAjKrgZxD2Qze0Mz6pcAyVSjw/Ml6CHMk=; b=PxHLEw8a7v01EP75fQB+gFUWaGUiFYQirn0MayZDz2HeAiTM/NmJ7taAWGvlhoQO6RzeIVnvhE99mCWZWXkOiotSNWVZWGIwpMfU11PP8kg7gupYS4VCbaHFhO0xoLfwpJLJ4rwPBwAgfqBs8ndSMH28lrRXs2E0qch9CLqEnYPSNjycnocwVufmI6sk7bTTSIbrLCbF4Gb8WB99//RwdI8EuR/SS6bX0dFT3IUbVp3yqas8ms1SLZhfZmup55W3onwc4Huo7tIlPcSFfGxrZtgKGyc/mo/0qD0r3YLMaqiEX+yWK7Uz2Mgf6mQZLTc8Tocl8vQDKTMT4QG/Q6eH7Q== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=amd.com; dmarc=pass action=none header.from=amd.com; dkim=pass header.d=amd.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amdcloud.onmicrosoft.com; s=selector2-amdcloud-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=T8O8TEJqM9sAjKrgZxD2Qze0Mz6pcAyVSjw/Ml6CHMk=; b=rQljEs4IpQD2cUiY6OyDPpHNrXzMScfVthK8ETbCQ4c+gvjvnQDjCJC1IVShatlwRyRHU6jrr0Zl9ti8WhsPQ/MUjw3owFEMQEHEv3Jm6B/NcaRS7s+wvZG6mkW/VNVPnLdIyWq6HHJeWpvt0uUpZkMBlAopZC/B8ydNrsLeN2M= Authentication-Results: nongnu.org; dkim=none (message not signed) header.d=none; nongnu.org; dmarc=none action=none header.from=amd.com; Received: from CH2PR12MB4133.namprd12.prod.outlook.com (2603:10b6:610:7a::13) by CH2PR12MB5001.namprd12.prod.outlook.com (2603:10b6:610:61::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3499.27; Tue, 3 Nov 2020 01:12:24 +0000 Received: from CH2PR12MB4133.namprd12.prod.outlook.com ([fe80::f428:769b:3e9:8300]) by CH2PR12MB4133.namprd12.prod.outlook.com ([fe80::f428:769b:3e9:8300%5]) with mapi id 15.20.3499.030; Tue, 3 Nov 2020 01:12:24 +0000 From: Michael Roth To: qemu-devel@nongnu.org Subject: [PULL v2 02/12] qga: Use common time encoding for guest-get-devices 'driver-date' Date: Mon, 2 Nov 2020 19:11:24 -0600 Message-Id: <20201103011134.887744-3-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20201103011134.887744-1-michael.roth@amd.com> References: <20201103011134.887744-1-michael.roth@amd.com> X-Originating-IP: [165.204.77.11] X-ClientProxiedBy: SA9PR13CA0226.namprd13.prod.outlook.com (2603:10b6:806:25::21) To CH2PR12MB4133.namprd12.prod.outlook.com (2603:10b6:610:7a::13) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from localhost (165.204.77.11) by SA9PR13CA0226.namprd13.prod.outlook.com (2603:10b6:806:25::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3541.10 via Frontend Transport; Tue, 3 Nov 2020 01:12:23 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: b64cbae6-7eaf-4652-f9f6-08d87f958598 X-MS-TrafficTypeDiagnostic: CH2PR12MB5001: X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:60; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: EV0LKqNhghtYIgFCL4mbqB8Pd7dr+lnZXOC7ksfVqhljxs8M3K5ANO1FW+55oimE0C+IEMYIBsPB2FaGjOWAbucm9LMOEo3uYXw3ST5Fk1h0oG6CUfNPmXDQaqHFBMqqJtlRtXrB/701EBMf/47SOYdutHWHDI02oj/VVx3fHlzlRtc6hGGm/RQkXWRggMZe13gPXOg+YEksrsH+CFAQD0XTDOoCsQZkQnNxjxsayEqvMo9PwH8ngnSPx6LLLCSvK/sETUhokK/c55kI1+2fZRiTwZr/xwDfJ23zgJbpvSbC7P9SS/3lnmdygywgD54vsHR1shB7sRxn5EjPaECnpQ== X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:CH2PR12MB4133.namprd12.prod.outlook.com; PTR:; CAT:NONE; SFS:(4636009)(366004)(39860400002)(136003)(376002)(346002)(396003)(26005)(44832011)(66556008)(66476007)(66946007)(8936002)(86362001)(6486002)(83380400001)(4326008)(36756003)(54906003)(2906002)(8676002)(5660300002)(52116002)(478600001)(186003)(316002)(6916009)(1076003)(16526019)(2616005)(956004)(6496006); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData: HsVhfMtcvLbJZySQE5nw7C14PvDjhXCyG/96Bv3FG8H/oWRa7azCiB7BeAysStwkYuXJqBmulPuVQuTfBf2GO5v5b5quN9rOE7e9O2TQ1uYGI8AwCN1Nl5WO+Qf72AoP/ChBfQ7bs8Jkn1qGw0jFLHAv3bSBAIrDoKPeZEDy/d5gIMmC6adMNhwfrrSJlssBc+HFB2oKe+EogZHUiFh0ods+PcfYMquLe0x8xXxV24rQEzETYJoHzM4DoS3R1dss+VnCnvRAbPdEpNszAzBmivJvsCDOE5l6jPZDklfCLLQA/FFIOe3nsorUA6sdi4/BVSP5N/12MOg7c8qtdtgK1zb6AewrNhK2c17g1Owm/169Xr+CyOHbVtFR/jAIpMOZEXNWDnZuHi5fQB1dPk8KYawhje5WMXF0JuBIhOgfmPL/IOk/ck0irxR6Svwi5edf0uAchqc5uTScENxI8FuxSP/6zaFMwEom1qUl2pI5I7sOJAcfRBdxaiRDjv9p+j+AUNf0y7pi1+AGGgSYVAdJNvVL2+arOo2C/vgJEwezkE2DSyrPRRWoThSQTqyf7HEf+t70+GLSV7MK0+fWA2K1EK1f8xutYKsRG78VcG4AKR3UmMk8qcjvSNYcZJQ+MneTEFsTb+iUSkgJyNMSPLoduw== X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: b64cbae6-7eaf-4652-f9f6-08d87f958598 X-MS-Exchange-CrossTenant-AuthSource: CH2PR12MB4133.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 03 Nov 2020 01:12:24.3435 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: sYnXiv8rogEWI+xD1gy5DolQOXBHrJFbW4BK5BgGmJUTJRElzfi6tyDq9CS0ObtdjmbtxKj2mjO0Q7dnnTv1Cg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH2PR12MB5001 Received-SPF: none client-ip=40.107.94.58; envelope-from=Michael.Roth@amd.com; helo=NAM10-MW2-obe.outbound.protection.outlook.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/11/02 20:12:23 X-ACL-Warn: Detected OS = Windows NT kernel [generic] [fuzzy] X-Spam_score_int: -8 X-Spam_score: -0.9 X-Spam_bar: / X-Spam_report: (-0.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, FORGED_SPF_HELO=1, MSGID_FROM_MTA_HEADER=0.001, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_PASS=-0.001, SPF_NONE=0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: peter.maydell@linaro.org, Markus Armbruster , =?utf-8?q?Marc-Andr=C3=A9_Lureau?= Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" From: Markus Armbruster guest-get-devices returns 'driver-date' as string in the format YYYY-MM-DD. Goes back to recent commit 2e4211cee4 "qga: add command guest-get-devices for reporting VirtIO devices". We should avoid use of multiple encodings for the same kind of data. Especially string encodings. Change it to return nanoseconds since the epoch, like guest-get-time does. Signed-off-by: Markus Armbruster Reviewed-by: Marc-André Lureau Signed-off-by: Michael Roth --- qga/commands-win32.c | 19 +++++++++++-------- qga/qapi-schema.json | 4 ++-- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/qga/commands-win32.c b/qga/commands-win32.c index 879b02b6c3..b01616a992 100644 --- a/qga/commands-win32.c +++ b/qga/commands-win32.c @@ -1641,6 +1641,12 @@ out: return head; } +static int64_t filetime_to_ns(const FILETIME *tf) +{ + return ((((int64_t)tf->dwHighDateTime << 32) | tf->dwLowDateTime) + - W32_FT_OFFSET) * 100; +} + int64_t qmp_guest_get_time(Error **errp) { SYSTEMTIME ts = {0}; @@ -1657,8 +1663,7 @@ int64_t qmp_guest_get_time(Error **errp) return -1; } - return ((((int64_t)tf.dwHighDateTime << 32) | tf.dwLowDateTime) - - W32_FT_OFFSET) * 100; + return filetime_to_ns(&tf); } void qmp_guest_set_time(bool has_time, int64_t time_ns, Error **errp) @@ -2363,7 +2368,6 @@ GuestDeviceInfoList *qmp_guest_get_devices(Error **errp) slog("enumerating devices"); for (i = 0; SetupDiEnumDeviceInfo(dev_info, i, &dev_info_data); i++) { bool skip = true; - SYSTEMTIME utc_date; g_autofree LPWSTR name = NULL; g_autofree LPFILETIME date = NULL; g_autofree LPWSTR version = NULL; @@ -2434,13 +2438,12 @@ GuestDeviceInfoList *qmp_guest_get_devices(Error **errp) slog("failed to get driver date"); continue; } - FileTimeToSystemTime(date, &utc_date); - device->driver_date = g_strdup_printf("%04d-%02d-%02d", - utc_date.wYear, utc_date.wMonth, utc_date.wDay); + device->driver_date = filetime_to_ns(date); device->has_driver_date = true; - slog("driver: %s\ndriver version: %s,%s\n", device->driver_name, - device->driver_date, device->driver_version); + slog("driver: %s\ndriver version: %" PRId64 ",%s\n", + device->driver_name, device->driver_date, + device->driver_version); item = g_new0(GuestDeviceInfoList, 1); item->value = g_steal_pointer(&device); if (!cur_item) { diff --git a/qga/qapi-schema.json b/qga/qapi-schema.json index f2c81cda2b..c7bfb8bf6a 100644 --- a/qga/qapi-schema.json +++ b/qga/qapi-schema.json @@ -1282,7 +1282,7 @@ # @GuestDeviceInfo: # # @driver-name: name of the associated driver -# @driver-date: driver release date in format YYYY-MM-DD +# @driver-date: driver release date, in nanoseconds since the epoch # @driver-version: driver version # @id: device ID # @@ -1291,7 +1291,7 @@ { 'struct': 'GuestDeviceInfo', 'data': { 'driver-name': 'str', - '*driver-date': 'str', + '*driver-date': 'int', '*driver-version': 'str', '*id': 'GuestDeviceId' } } From patchwork Tue Nov 3 01:11:25 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 316357 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-12.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, MSGID_FROM_MTA_HEADER, SIGNED_OFF_BY, SPF_HELO_NONE, SPF_PASS, URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6C2F3C4742C for ; Tue, 3 Nov 2020 01:22:25 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id D68F42063A for ; Tue, 3 Nov 2020 01:22:24 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=amdcloud.onmicrosoft.com header.i=@amdcloud.onmicrosoft.com header.b="RfxoJ30o" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org D68F42063A Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=amd.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Received: from localhost ([::1]:44506 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kZl1j-0003jP-OT for qemu-devel@archiver.kernel.org; Mon, 02 Nov 2020 20:22:23 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:50966) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kZksc-0006eO-F4 for qemu-devel@nongnu.org; Mon, 02 Nov 2020 20:13:04 -0500 Received: from mail-mw2nam10on2055.outbound.protection.outlook.com ([40.107.94.55]:25952 helo=NAM10-MW2-obe.outbound.protection.outlook.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kZksZ-0005Yp-C1 for qemu-devel@nongnu.org; Mon, 02 Nov 2020 20:12:56 -0500 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=DXlsDg3xH5BwYboSh02F0yS2JQfoubi3HyxRdFD7AFUUnRp6qlmvacXnQr4TzoHW9TuHYp9adCQipC+kcr1E3ZmuytlurBsq8RzC0+GGV3f4EBd5YKzqqGL6WzI9tlaPIa4jhYEVHNislkmj/MAWGUCLrujnBGYdb3S3EF+BfZftRJADnXYhP9tfItxY7S7stpoUhnEu+ePFl+Vd10j8GOp8LJr2YUEPwerVZvrHX1JHFEc8N8qbLjuj1WAQ6bGWKV1ui47XPlzYSnjD1xDA8vA126iOqlg3qZolYIrpi0TvZRlBS/kRQFFspW2bozRTqM/Pqsn1ZUKyjcgXriCTAg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=UVOT+Ji/XN+UthHk4J8n5dOXuOASmrPvPQAuUjq7el8=; b=JMJyTisuSzJvVplPp01WwYaPgSeGouJibjVqNCLPMTy/pWEObvfAHk/XL+vUnY+rOHTi6tTJWOsQUN/gIj6EMo9K9dJqPT1DiLyY8QmOJeph/n9RYqvGCZxheg7Suog4kxd075o+MY5D0JVe/4PyqfQQpEPtsIWRJ5RQO0DFWrlQ9BtzfWxntifEtgiBEqRGOa97tojkpKrJsoP3yr6krsA9epqwtsE1AharYJ76kBL1d5bgaVAh0ugn7++tRNCBVYMFefTyhky8cj/sDEjfZaXI+9yamaH/Cfj0VYZU4ccIe5RTQ70LKv9wDnMjn55MdukIBotNJ5/lr4TxDQz2UA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=amd.com; dmarc=pass action=none header.from=amd.com; dkim=pass header.d=amd.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amdcloud.onmicrosoft.com; s=selector2-amdcloud-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=UVOT+Ji/XN+UthHk4J8n5dOXuOASmrPvPQAuUjq7el8=; b=RfxoJ30oeDw6UoRQYIN1fArZxeqkrv1Ztv6JdC4mJySkknZfF6quKEnd1Qef78ZarmLGEMSyBNfASpd2w++3GzXs2S4yoxm8pONo/9pO0SJzQGUnSA9xiY2J133YTYENh5V3KeYotJUaQxpkgElhcGSdMVbh7QkLG2BQGzTsMEM= Authentication-Results: nongnu.org; dkim=none (message not signed) header.d=none; nongnu.org; dmarc=none action=none header.from=amd.com; Received: from CH2PR12MB4133.namprd12.prod.outlook.com (2603:10b6:610:7a::13) by CH2PR12MB5001.namprd12.prod.outlook.com (2603:10b6:610:61::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3499.27; Tue, 3 Nov 2020 01:12:27 +0000 Received: from CH2PR12MB4133.namprd12.prod.outlook.com ([fe80::f428:769b:3e9:8300]) by CH2PR12MB4133.namprd12.prod.outlook.com ([fe80::f428:769b:3e9:8300%5]) with mapi id 15.20.3499.030; Tue, 3 Nov 2020 01:12:27 +0000 From: Michael Roth To: qemu-devel@nongnu.org Subject: [PULL v2 03/12] qga-win: Fix guest-get-devices error API violations Date: Mon, 2 Nov 2020 19:11:25 -0600 Message-Id: <20201103011134.887744-4-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20201103011134.887744-1-michael.roth@amd.com> References: <20201103011134.887744-1-michael.roth@amd.com> X-Originating-IP: [165.204.77.11] X-ClientProxiedBy: SA0PR11CA0008.namprd11.prod.outlook.com (2603:10b6:806:d3::13) To CH2PR12MB4133.namprd12.prod.outlook.com (2603:10b6:610:7a::13) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from localhost (165.204.77.11) by SA0PR11CA0008.namprd11.prod.outlook.com (2603:10b6:806:d3::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3499.18 via Frontend Transport; Tue, 3 Nov 2020 01:12:26 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: c2f5fac4-1329-471d-54eb-08d87f958750 X-MS-TrafficTypeDiagnostic: CH2PR12MB5001: X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:8882; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: wKjG9BhYFo4MsueLZlWV5x4KKb8CfXbtL/mqw2o1goTZipNAg2NSFHwX4MO4M3bx2sDFJcwftfd4doTjL/7CiTVMgmHnONHvhphkf76dyMqPIVgEFIE3sZZgI+/IMrxNBZA8eIci5kjIj05rvRz0kLCJofVQDsAiWn5gFkRNFMsVJ7EBSR+7dHLUlEDcdjJzS02w6nAsgJmhfU5sgmq5zbQ5L+C1HhmLxKE9I7srG5ONvOXaISpQNwCKjMreLWFdYTjFk5K2kznq3Bh8xEU4Pjid4gPFIP0WcX4GKB4657tWhW6NWts4D4PcXqs1QqX7 X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:CH2PR12MB4133.namprd12.prod.outlook.com; PTR:; CAT:NONE; SFS:(4636009)(366004)(39860400002)(136003)(376002)(346002)(396003)(26005)(44832011)(66556008)(66476007)(66946007)(8936002)(86362001)(6486002)(83380400001)(4326008)(36756003)(54906003)(2906002)(8676002)(5660300002)(52116002)(478600001)(186003)(316002)(6916009)(1076003)(16526019)(2616005)(956004)(6496006); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData: q9Z3p1E0V14ps5mD/AIV6dNmb7AwQNE/H5inB6z6tuXe0XeREkMRT8gg5Q9fn9ujyVw7oHA3qaOPH7taBR3q1XnRCK25IZm6p0CXQa4LRDkAx94AAakyUPI7Yv7BuAG02B0wCAHepiOjJVi+REk6LDmjvgTf7Z68c8G9lD4A7stx5oT5hj7X0MOcmtTYM4Uj2n/uTHaNSw0opjcMv42qI92SPdFWhcwzgnAJCctwrA3nOjLtvgt8tpK5T4Komj9l5LdZ8mTuyPOE87K9xWjzefWpbv89CpsLG1wVZSbvEBUjs4IyZR2evQezBC9b+Fq046WjluS5HMeRWGErxU+1eCAdpofgIyy8uXpmNmpuHAuKQFuj7tB9dLcCb4KlDEYh99v9COqzMJ3asfJs/MfmX4jQP5cJ1eHr6BHkQtKVeEGFYsyA6GcQiplMJ96NuiK777Q8+UwkXQ/vIR968ojBOlc6UPKQZ3UtoE9rhw0mnWXRhXE7AOn+zN0sX3ld6R58OpFcdhhKJeQ1EOQklGWMybVz3+cAhBly68c1ATnU+VV7hOKvbFEvWCVfNY/MIAjpOGyg2pOMMM3A43oprRCemB0razrMceF80+fQfcPFuy9vPJSpACJvbyRNJQLC1rC54LapeN0RJrbS6K/DQq1YFA== X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: c2f5fac4-1329-471d-54eb-08d87f958750 X-MS-Exchange-CrossTenant-AuthSource: CH2PR12MB4133.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 03 Nov 2020 01:12:27.2149 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: RpJ86uVZeHDfKvdDBYzoM0sF0Vg5xztX79mrlMl+wo6p6i1Rp20WqAqEWFM1FVYU9i7Vp+MyAU7Cqna9t9jPmg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH2PR12MB5001 Received-SPF: none client-ip=40.107.94.55; envelope-from=Michael.Roth@amd.com; helo=NAM10-MW2-obe.outbound.protection.outlook.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/11/02 20:12:53 X-ACL-Warn: Detected OS = Windows NT kernel [generic] [fuzzy] X-Spam_score_int: -8 X-Spam_score: -0.9 X-Spam_bar: / X-Spam_report: (-0.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, FORGED_SPF_HELO=1, MSGID_FROM_MTA_HEADER=0.001, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_PASS=-0.001, SPF_NONE=0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: peter.maydell@linaro.org, =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= , Markus Armbruster , =?utf-8?q?Marc-Andr=C3=A9_Lure?= =?utf-8?q?au?= Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" From: Markus Armbruster The Error ** argument must be NULL, &error_abort, &error_fatal, or a pointer to a variable containing NULL. Passing an argument of the latter kind twice without clearing it in between is wrong: if the first call sets an error, it no longer points to NULL for the second call. qmp_guest_get_devices() is wrong that way: it calls error_setg() in a loop. If no iteration fails, the function returns a value and sets no error. Okay. If exactly one iteration fails, the function returns a value and sets an error. Wrong. If multiple iterations fail, the function trips error_setv()'s assertion. Fix it to return immediately on error. Perhaps the failure to convert the driver version to UTF-8 should not be an error. We could simply not report the botched version string instead. Drop a superfluous continue while there. Signed-off-by: Markus Armbruster Reviewed-by: Philippe Mathieu-Daudé Reviewed-by: Marc-André Lureau Signed-off-by: Michael Roth --- qga/commands-win32.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/qga/commands-win32.c b/qga/commands-win32.c index b01616a992..1efe3ba076 100644 --- a/qga/commands-win32.c +++ b/qga/commands-win32.c @@ -2385,7 +2385,7 @@ GuestDeviceInfoList *qmp_guest_get_devices(Error **errp) device->driver_name = g_utf16_to_utf8(name, -1, NULL, NULL, NULL); if (device->driver_name == NULL) { error_setg(errp, "conversion to utf8 failed (driver name)"); - continue; + return NULL; } slog("querying device: %s", device->driver_name); hw_ids = ga_get_hardware_ids(dev_info_data.DevInst); @@ -2428,7 +2428,7 @@ GuestDeviceInfoList *qmp_guest_get_devices(Error **errp) NULL, NULL); if (device->driver_version == NULL) { error_setg(errp, "conversion to utf8 failed (driver version)"); - continue; + return NULL; } device->has_driver_version = true; @@ -2452,7 +2452,6 @@ GuestDeviceInfoList *qmp_guest_get_devices(Error **errp) cur_item->next = item; cur_item = item; } - continue; } if (dev_info != INVALID_HANDLE_VALUE) { From patchwork Tue Nov 3 01:11:26 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 316356 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-12.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, MSGID_FROM_MTA_HEADER, SIGNED_OFF_BY, SPF_HELO_NONE, SPF_PASS, URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 51B50C4742C for ; Tue, 3 Nov 2020 01:22:57 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id C750E22450 for ; Tue, 3 Nov 2020 01:22:56 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=amdcloud.onmicrosoft.com header.i=@amdcloud.onmicrosoft.com header.b="rrhHQJsf" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org C750E22450 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=amd.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Received: from localhost ([::1]:45858 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kZl2F-0004Jh-Gk for qemu-devel@archiver.kernel.org; Mon, 02 Nov 2020 20:22:55 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:50990) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kZksi-0006f6-Ou for qemu-devel@nongnu.org; Mon, 02 Nov 2020 20:13:04 -0500 Received: from mail-mw2nam10on2055.outbound.protection.outlook.com ([40.107.94.55]:25952 helo=NAM10-MW2-obe.outbound.protection.outlook.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kZksc-0005Yp-PB for qemu-devel@nongnu.org; Mon, 02 Nov 2020 20:13:00 -0500 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=WkyQv0eSJzgbGNpM5F9r2fUbJdc77zf1bom0zZuvlW5o3mpoTlsB3jp5U+fXZ1k/fuP0NdxTLtZp31QYThT+F20waU8yAZTFAVDVgfoupP9+zX8nAYssO52lT+bJTJ3v3Z7QLTml9cy+lohlblWcU+RDq3hL+g8cfeuk97A/zsKsHP35nRw0J1ivDRqyT8BfwP6w30JjU7W0vG9J2rV4eb5uBtFIJ6e+AbX9Fs98nff9cJvpMAXXq9M2zpDcpWGoCgiostHNfYLE8W2+UyBHHpc20mlJLCuIUaBtQ12PgDUs0pjcXLUgjtvqJnx4RF0rar1RuT2IZXaCjBzWkH+lPw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=1gqDMia7c4BktleQtq4MXU98cKjxCSJ/JIUaGfLlL6E=; b=DKtIW7jT7oax636SXuZsN1IuvNgfqyFPeG6gZgdB90ggjaJOMYYUMRbn3JnVLCuCnVfwFAsJqwAneQnD/JxwMoK/Iny22g9VHZsKr+tDxM9/Tum+BaNOiDje519z/QxwGHnt+VAh2jjzxgmzfVdrsafKS3p1IgN2j4hgDCCPe8Wu4aC11awDhNZm0bbmEIyUt45HI5U2cpzZAKsjWZoh68B0ZiY843zPfaZiSoFWrfgXGf7HsGyHp6ja/fRxABibw49TFFGqRSoxcOd+Cl3728Qr3yPW3628gnhHYpGDMz69A6g5KGkxFngFNr3TYIOegUyNO/7z0FyeECKRM6m+Dw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=amd.com; dmarc=pass action=none header.from=amd.com; dkim=pass header.d=amd.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amdcloud.onmicrosoft.com; s=selector2-amdcloud-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=1gqDMia7c4BktleQtq4MXU98cKjxCSJ/JIUaGfLlL6E=; b=rrhHQJsfuWVpeCLKgQGVJZ8U9TZDoGb1kkdoE2JW/0t8G73Wf8BIecWCEm1b46Hn/fWYOPLfVQ8dotSi5qFMMFCHp2SdZnLbv2JJsAUGc8VRm+YQjWR6kMcQ56RTM8trxtINQqFdMleXjpIuKnbWe01nwqJUjxdnlbKUsOFi6U0= Authentication-Results: nongnu.org; dkim=none (message not signed) header.d=none; nongnu.org; dmarc=none action=none header.from=amd.com; Received: from CH2PR12MB4133.namprd12.prod.outlook.com (2603:10b6:610:7a::13) by CH2PR12MB5001.namprd12.prod.outlook.com (2603:10b6:610:61::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3499.27; Tue, 3 Nov 2020 01:12:30 +0000 Received: from CH2PR12MB4133.namprd12.prod.outlook.com ([fe80::f428:769b:3e9:8300]) by CH2PR12MB4133.namprd12.prod.outlook.com ([fe80::f428:769b:3e9:8300%5]) with mapi id 15.20.3499.030; Tue, 3 Nov 2020 01:12:30 +0000 From: Michael Roth To: qemu-devel@nongnu.org Subject: [PULL v2 04/12] qga: Flatten simple union GuestDeviceId Date: Mon, 2 Nov 2020 19:11:26 -0600 Message-Id: <20201103011134.887744-5-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20201103011134.887744-1-michael.roth@amd.com> References: <20201103011134.887744-1-michael.roth@amd.com> X-Originating-IP: [165.204.77.11] X-ClientProxiedBy: SA0PR11CA0002.namprd11.prod.outlook.com (2603:10b6:806:d3::7) To CH2PR12MB4133.namprd12.prod.outlook.com (2603:10b6:610:7a::13) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from localhost (165.204.77.11) by SA0PR11CA0002.namprd11.prod.outlook.com (2603:10b6:806:d3::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3499.18 via Frontend Transport; Tue, 3 Nov 2020 01:12:29 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: b8e7634c-3db2-45ad-1eac-08d87f958916 X-MS-TrafficTypeDiagnostic: CH2PR12MB5001: X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:4941; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: PZNl1igfe7Luv+s86zNbiEsZgfNZcqQw2UEfYrR2dvmbi/ZTyjGJdzeEHdOMHKBobI1K+h2jNqZpLzkBt3t/l0MIwir0VLrvyGc71eh+6nRvx2xJKtDigZ14ugTiZy3XId6o0HyEQ0ptENvcpG6CItTMESeLrBeBxxNfLW/K6vf24HASlF+pdDvAD2HoBnY2bNE/m5Sik0J4LQNjG2uZxfh8j+0Sr7KuZk6LOw9As47RaFapW/dnonE8KJSmOxcPJ9pxME3sNBbiOyyMzVS7ypEakZMDhZ3yllMLLO0lZn5BXQ4hzBpZuCKo0B8LolyP5VMF37foMkJPc7xNrAKoTA== X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:CH2PR12MB4133.namprd12.prod.outlook.com; PTR:; CAT:NONE; SFS:(4636009)(366004)(39860400002)(136003)(376002)(346002)(396003)(26005)(44832011)(66556008)(66476007)(66946007)(8936002)(86362001)(6486002)(6666004)(83380400001)(4326008)(36756003)(54906003)(2906002)(8676002)(5660300002)(52116002)(478600001)(186003)(316002)(6916009)(1076003)(16526019)(2616005)(956004)(6496006); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData: MWJ7fc/i1UrflMM5apT9q7vBxeT/WVSxHCG35gRKVmg0AqR4czSCVOLpizQl9WcyFf4SirGQPv3SHa1DJ+slGveZpFMRsAm9wil6EbM3zwN9fjL2rY3plgE0ivQ5+wAI34qiKbosc18tA4I3SFFPj8e0MfdhXGrc23b6Kl9nJ0VW50FZg/CunugBsf9VWx0nBESutXKNjUxWhKTChgx6L3kaZYvzOEjpsNVEKMlD7+oeTGcnOktSvL0SYYOYaMvAtvLeukgukNvV1ifVXT0Y8pWAujnTl1ThPumQHvfPZiei1P6lutNB0vo4cOu1gUAcIR5i2OFALp0sNucZJWv8szQQJUzl1N5Gcy+OSrd3UopZ+MgxYT2rnQO2kysTHvGJhrvlTXkQpOTbNIcov3OUqFUo8r+aWCxUjCxe0jPuE0ufmo1giLnhnUDgZeQcjimc71RxwvsG7e/Znc+6hRWt8c8yGRyGnW/srNzQySGkmFBq8uDiWYDdLkLHOgmbRJWK4dwFPMMcj8MIMITjfdldgsacBX3wMd0aiimYngnz7Z4UeCzVFkW04N6f9+n5UGtwI4WAUFexJPPTQ+abmELz2LBr0Tw8a4URZFTlCsZlURqiKmoV9RVDZBIysXAbP3e4tlH6Ym/S2saKam93Ig9YAA== X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: b8e7634c-3db2-45ad-1eac-08d87f958916 X-MS-Exchange-CrossTenant-AuthSource: CH2PR12MB4133.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 03 Nov 2020 01:12:30.1251 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: IpliANdpVBktPPc/9gU1VXhOqLXowWBJyEtK9kZWc/MP+yIUFnhq5y8EHpDmsd4+8S5DKTITe75OHFGDkz6LRQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH2PR12MB5001 Received-SPF: none client-ip=40.107.94.55; envelope-from=Michael.Roth@amd.com; helo=NAM10-MW2-obe.outbound.protection.outlook.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/11/02 20:12:53 X-ACL-Warn: Detected OS = Windows NT kernel [generic] [fuzzy] X-Spam_score_int: -8 X-Spam_score: -0.9 X-Spam_bar: / X-Spam_report: (-0.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, FORGED_SPF_HELO=1, MSGID_FROM_MTA_HEADER=0.001, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_PASS=-0.001, SPF_NONE=0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: peter.maydell@linaro.org, =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= , Markus Armbruster , =?utf-8?q?Marc-Andr=C3=A9_Lure?= =?utf-8?q?au?= Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" From: Markus Armbruster Simple unions are simpler than flat unions in the schema, but more complicated in C and on the QMP wire: there's extra indirection in C and extra nesting on the wire, both pointless. They should be avoided in new code. GuestDeviceId was recently added for guest-get-devices. Convert it to a flat union. Signed-off-by: Markus Armbruster Reviewed-by: Marc-André Lureau Reviewed-by: Philippe Mathieu-Daudé Signed-off-by: Michael Roth --- qga/commands-win32.c | 9 ++++----- qga/qapi-schema.json | 8 ++++++++ 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/qga/commands-win32.c b/qga/commands-win32.c index 1efe3ba076..0c33d48aaa 100644 --- a/qga/commands-win32.c +++ b/qga/commands-win32.c @@ -2400,16 +2400,15 @@ GuestDeviceInfoList *qmp_guest_get_devices(Error **errp) } skip = false; - id = g_new0(GuestDeviceIdPCI, 1); vendor_id = g_match_info_fetch(match_info, 1); device_id = g_match_info_fetch(match_info, 2); - id->vendor_id = g_ascii_strtoull(vendor_id, NULL, 16); - id->device_id = g_ascii_strtoull(device_id, NULL, 16); device->id = g_new0(GuestDeviceId, 1); device->has_id = true; - device->id->type = GUEST_DEVICE_ID_KIND_PCI; - device->id->u.pci.data = id; + device->id->type = GUEST_DEVICE_TYPE_PCI; + id = &device->id->u.pci; + id->vendor_id = g_ascii_strtoull(vendor_id, NULL, 16); + id->device_id = g_ascii_strtoull(device_id, NULL, 16); g_match_info_free(match_info); break; diff --git a/qga/qapi-schema.json b/qga/qapi-schema.json index c7bfb8bf6a..fe10631e4c 100644 --- a/qga/qapi-schema.json +++ b/qga/qapi-schema.json @@ -1256,6 +1256,12 @@ { 'command': 'guest-get-osinfo', 'returns': 'GuestOSInfo' } +## +# @GuestDeviceType: +## +{ 'enum': 'GuestDeviceType', + 'data': [ 'pci' ] } + ## # @GuestDeviceIdPCI: # @@ -1276,6 +1282,8 @@ # Since: 5.2 ## { 'union': 'GuestDeviceId', + 'base': { 'type': 'GuestDeviceType' }, + 'discriminator': 'type', 'data': { 'pci': 'GuestDeviceIdPCI' } } ## From patchwork Tue Nov 3 01:11:31 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 316353 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-12.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, MSGID_FROM_MTA_HEADER, SIGNED_OFF_BY, SPF_HELO_NONE, SPF_PASS, URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id C4F81C00A89 for ; Tue, 3 Nov 2020 01:32:24 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id D642D20691 for ; Tue, 3 Nov 2020 01:32:23 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=amdcloud.onmicrosoft.com header.i=@amdcloud.onmicrosoft.com header.b="fK4RHEp0" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org D642D20691 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=amd.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Received: from localhost ([::1]:34354 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kZlBO-0003C1-MX for qemu-devel@archiver.kernel.org; Mon, 02 Nov 2020 20:32:22 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:53572) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kZl6B-00081k-NA for qemu-devel@nongnu.org; Mon, 02 Nov 2020 20:27:00 -0500 Received: from mail-co1nam04on062d.outbound.protection.outlook.com ([2a01:111:f400:fe4d::62d]:16551 helo=NAM04-CO1-obe.outbound.protection.outlook.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kZl69-00077M-5v for qemu-devel@nongnu.org; Mon, 02 Nov 2020 20:26:59 -0500 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=TL0AaS3fvGviKq7m4cKsMhaJhmyB3sW+k6ZvsvHXszITTrLKpOvXxk8bYWuFrbXEnXDBJJzC9aTtAXdUWGLMeFA2qxUfIjAK42RMFODwCBsNipJ7vqYK8ZUE6fF5NW2tjS1D4+1W8/9pMks+BAQkOGjqgcIQCfjrKbQHck7h1fzhxI8w67IajiWWGd1plJGTjNozfwMdb6r4HhCkyqfMXF50DL5h2lPRrgF4bBlOtbAg0dQAf8uD9DqFoLpqXQL1uh+OcrSVpvfZerDnqajR+S782nxu60VIMwr7H6oNFWigvSM/cyKq5UkTpZnu32VX8c97dC8zyB4Oo9nt183vlQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=KbCvfrBHw1Tq7c4oWrWY0XirTx01iPkh9MhsS9wBa9k=; b=TiDxa12zpFfr7U9Q8YhOqJUJUKMXZXTmXMSOvTNosYf8rDQd7ZqWIGh2CrRHwhajOOZ+MAZsETusfjs7owFK5WwESVXrlcA7lvxrxUNf6Oq73yuE7UtzC7Dum69DSKConQJHgQR3ujEOHxr9T4G9xuyTe427jUcKgdu2d+bAS9ISo2nmpj8p33hkDrm/pGaKFlXJYNZ4+pme4T/cYULAT4VuBNIivbAYHQxQZ8sBNipiPHJ+4NAdm4y9okrzdhV+2ZM2D6q3ueJ+unfab5iHvN/xHBgeiDKq/3/ZpAbUXnb9zmnLiESQe8lNGWz1Rl6MKC1W8Yzo17TkYJmgH0TtIg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=amd.com; dmarc=pass action=none header.from=amd.com; dkim=pass header.d=amd.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amdcloud.onmicrosoft.com; s=selector2-amdcloud-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=KbCvfrBHw1Tq7c4oWrWY0XirTx01iPkh9MhsS9wBa9k=; b=fK4RHEp05cwfXAToDGoLURrRI+ae4ijgSZKdIWfJlBlb4abq0i0P9Yy4hQDl0qQlQ6ci8XDWNB6mcyiQdePPCxxCGV4rn3bhw/vCZID2+Q4Rz/rHf3yvgxO2bdkJLxVr/EKs2w6kTVySB/WZ8e9Pd7DqlULg7NJ4GzZ4xb9hins= Authentication-Results: nongnu.org; dkim=none (message not signed) header.d=none; nongnu.org; dmarc=none action=none header.from=amd.com; Received: from CH2PR12MB4133.namprd12.prod.outlook.com (2603:10b6:610:7a::13) by CH2PR12MB5001.namprd12.prod.outlook.com (2603:10b6:610:61::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3499.27; Tue, 3 Nov 2020 01:11:50 +0000 Received: from CH2PR12MB4133.namprd12.prod.outlook.com ([fe80::f428:769b:3e9:8300]) by CH2PR12MB4133.namprd12.prod.outlook.com ([fe80::f428:769b:3e9:8300%5]) with mapi id 15.20.3499.030; Tue, 3 Nov 2020 01:11:50 +0000 From: Michael Roth To: qemu-devel@nongnu.org Subject: [PULL v2 09/12] qga: add ssh-{add,remove}-authorized-keys Date: Mon, 2 Nov 2020 19:11:31 -0600 Message-Id: <20201103011134.887744-10-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20201103011134.887744-1-michael.roth@amd.com> References: <20201103011134.887744-1-michael.roth@amd.com> X-Originating-IP: [165.204.77.11] X-ClientProxiedBy: SN6PR08CA0036.namprd08.prod.outlook.com (2603:10b6:805:66::49) To CH2PR12MB4133.namprd12.prod.outlook.com (2603:10b6:610:7a::13) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from localhost (165.204.77.11) by SN6PR08CA0036.namprd08.prod.outlook.com (2603:10b6:805:66::49) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3499.19 via Frontend Transport; Tue, 3 Nov 2020 01:11:48 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: ab0a5460-73f4-47ff-0576-08d87f95707b X-MS-TrafficTypeDiagnostic: CH2PR12MB5001: X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:1751; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 6VuTTVfetvnl2Cxgrqi9htuOZH8n6RVi2W7hcoQpwK3HqDobj4zKavYut1OiwYCBGSV+NOZUzJyadZnq60lHuVFy7/eUoJSp8mOQzFLcyheL9nF29r5C+OwoPiVRquYlJBMV5OZNA10i0d57iIQCDv8QSv+48dW5sDjQg+iHeUK1yBQSvCYSWrTZOBYgEGRhKE/Q2/RbZ4+qZADOSjXuPaIVZ7R794ULSVJsh2JLfrWRIytlGQxsrZrccp2Xz7W2AI29Ri0M8GzKWEnrBc/RDxUiXMw+fsMdd5pJDli8G14wffZgaclWdu+Fg5z35z2g/4gh5cSfeS/L+dKr6HrMXBq0oLrY3XrVsrNz0xeGOnZQnWoW4u4tWU2Eh5tf1ybwaclxSAKR/6FjIqtC7HE5dQ== X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:CH2PR12MB4133.namprd12.prod.outlook.com; PTR:; CAT:NONE; SFS:(4636009)(366004)(39860400002)(136003)(376002)(346002)(396003)(26005)(44832011)(66556008)(66476007)(66946007)(8936002)(86362001)(6486002)(6666004)(83380400001)(4326008)(30864003)(36756003)(54906003)(2906002)(8676002)(5660300002)(52116002)(478600001)(186003)(966005)(316002)(6916009)(1076003)(16526019)(2616005)(956004)(6496006); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData: vjt+3zfZh8rcSDKnzeWa4qVKOIHvpsiC+yGEHLCDSUz76NMwHkk+p/noerqpAwtNR6N+5qjb2thIAxSNIZllwgjxZlMrdm1fibfzcSu0hBanJ0zy7EXTzG5oLjt3S2GenAD0ZHc2s9k90N0kZVB54QHbgraozGVHDk52lGtNes/Ouc03j9oN6Cf5SyuQxqQeYiooUEdKxG5Yyaj/eAGXygjBFeKkXzPNZzPNt/+Ws7tekUMIhzAGwsYSs2DZJtyCFfolEoOdop1eT/fda0qNgV8fT/ZySygs1jK0jaO4akEEHA5jcN62u6yWYqbLlQyQnhPOyU5nHD8yAubniwoB2PTXRzmsVCwKeMBh64//lf2qUlwb51XxxYRBWtKfGvCSfhVEFi0VPwOHBXJbhGoVCGbvGAYlAWrYiXcfm0J51MjUZzUUKJ0EOqUCVMA8Nf2353erZ7QfHJ7bVZ6OT3Tty7JqtXkUnUHFXu534qBPlRgalw07LoU9mFGVBQsjv9urOMB0QGBxCwc+WMJ8AyK6kwD+yv0JRKDiO1vG4Yx7ptkjBxRTh7gx1Nxfm2yIaXGiFAKhMrWWWLp39Jq6H0sC7E2O3AK0YfA7234gJVFzFwqXvhU+x/KIn7Cy8a2bV7wecijsn5vhsAslnhGCqBp3mw== X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: ab0a5460-73f4-47ff-0576-08d87f95707b X-MS-Exchange-CrossTenant-AuthSource: CH2PR12MB4133.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 03 Nov 2020 01:11:50.3119 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: wNl7vw5qLjSLT+4OMZsjDE9dnblykVrctHxLUxZWjnS7X+MYVhMHY21kfuSZzXg3B3jJonoW0bzN7XTjzRtQcA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH2PR12MB5001 Received-SPF: none client-ip=2a01:111:f400:fe4d::62d; envelope-from=Michael.Roth@amd.com; helo=NAM04-CO1-obe.outbound.protection.outlook.com X-detected-operating-system: by eggs.gnu.org: No matching host in p0f cache. That's all we know. X-Spam_score_int: -8 X-Spam_score: -0.9 X-Spam_bar: / X-Spam_report: (-0.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, FORGED_SPF_HELO=1, MSGID_FROM_MTA_HEADER=0.001, SPF_HELO_PASS=-0.001, SPF_NONE=0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Michal Privoznik , peter.maydell@linaro.org, =?utf-8?q?Daniel_P_=2E_Berrang=C3=A9?= , =?utf-8?q?Marc-Andr=C3=A9_Lureau?= Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" From: Marc-André Lureau Add new commands to add and remove SSH public keys from ~/.ssh/authorized_keys. I took a different approach for testing, including the unit tests right with the code. I wanted to overwrite the function to get the user details, I couldn't easily do that over QMP. Furthermore, I prefer having unit tests very close to the code, and unit files that are domain specific (commands-posix is too crowded already). FWIW, that coding/testing style is Rust-style (where tests can or should even be part of the documentation!). Fixes: https://bugzilla.redhat.com/show_bug.cgi?id=1885332 Signed-off-by: Marc-André Lureau Reviewed-by: Michal Privoznik Reviewed-by: Daniel P. Berrangé *squashed in fix-ups for setting file ownership and use of QAPI conditionals for CONFIG_POSIX instead of stub definitions *disable qga-ssh-test for now due to G_TEST_OPTION_ISOLATE_DIRS triggering leak detector in build-oss-fuzz Signed-off-by: Michael Roth --- qga/commands-posix-ssh.c | 407 +++++++++++++++++++++++++++++++++++++++ qga/meson.build | 25 ++- qga/qapi-schema.json | 35 ++++ 3 files changed, 466 insertions(+), 1 deletion(-) create mode 100644 qga/commands-posix-ssh.c diff --git a/qga/commands-posix-ssh.c b/qga/commands-posix-ssh.c new file mode 100644 index 0000000000..a7bc9a1c24 --- /dev/null +++ b/qga/commands-posix-ssh.c @@ -0,0 +1,407 @@ + /* + * This work is licensed under the terms of the GNU GPL, version 2 or later. + * See the COPYING file in the top-level directory. + */ +#include "qemu/osdep.h" + +#include +#include +#include +#include + +#include "qapi/error.h" +#include "qga-qapi-commands.h" + +#ifdef QGA_BUILD_UNIT_TEST +static struct passwd * +test_get_passwd_entry(const gchar *user_name, GError **error) +{ + struct passwd *p; + int ret; + + if (!user_name || g_strcmp0(user_name, g_get_user_name())) { + g_set_error(error, G_UNIX_ERROR, 0, "Invalid user name"); + return NULL; + } + + p = g_new0(struct passwd, 1); + p->pw_dir = (char *)g_get_home_dir(); + p->pw_uid = geteuid(); + p->pw_gid = getegid(); + + ret = g_mkdir_with_parents(p->pw_dir, 0700); + g_assert_cmpint(ret, ==, 0); + + return p; +} + +#define g_unix_get_passwd_entry_qemu(username, err) \ + test_get_passwd_entry(username, err) +#endif + +static struct passwd * +get_passwd_entry(const char *username, Error **errp) +{ + g_autoptr(GError) err = NULL; + struct passwd *p; + + ERRP_GUARD(); + + p = g_unix_get_passwd_entry_qemu(username, &err); + if (p == NULL) { + error_setg(errp, "failed to lookup user '%s': %s", + username, err->message); + return NULL; + } + + return p; +} + +static bool +mkdir_for_user(const char *path, const struct passwd *p, + mode_t mode, Error **errp) +{ + ERRP_GUARD(); + + if (g_mkdir(path, mode) == -1) { + error_setg(errp, "failed to create directory '%s': %s", + path, g_strerror(errno)); + return false; + } + + if (chown(path, p->pw_uid, p->pw_gid) == -1) { + error_setg(errp, "failed to set ownership of directory '%s': %s", + path, g_strerror(errno)); + return false; + } + + if (chmod(path, mode) == -1) { + error_setg(errp, "failed to set permissions of directory '%s': %s", + path, g_strerror(errno)); + return false; + } + + return true; +} + +static bool +check_openssh_pub_key(const char *key, Error **errp) +{ + ERRP_GUARD(); + + /* simple sanity-check, we may want more? */ + if (!key || key[0] == '#' || strchr(key, '\n')) { + error_setg(errp, "invalid OpenSSH public key: '%s'", key); + return false; + } + + return true; +} + +static bool +check_openssh_pub_keys(strList *keys, size_t *nkeys, Error **errp) +{ + size_t n = 0; + strList *k; + + ERRP_GUARD(); + + for (k = keys; k != NULL; k = k->next) { + if (!check_openssh_pub_key(k->value, errp)) { + return false; + } + n++; + } + + if (nkeys) { + *nkeys = n; + } + return true; +} + +static bool +write_authkeys(const char *path, const GStrv keys, + const struct passwd *p, Error **errp) +{ + g_autofree char *contents = NULL; + g_autoptr(GError) err = NULL; + + ERRP_GUARD(); + + contents = g_strjoinv("\n", keys); + if (!g_file_set_contents(path, contents, -1, &err)) { + error_setg(errp, "failed to write to '%s': %s", path, err->message); + return false; + } + + if (chown(path, p->pw_uid, p->pw_gid) == -1) { + error_setg(errp, "failed to set ownership of directory '%s': %s", + path, g_strerror(errno)); + return false; + } + + if (chmod(path, 0600) == -1) { + error_setg(errp, "failed to set permissions of '%s': %s", + path, g_strerror(errno)); + return false; + } + + return true; +} + +static GStrv +read_authkeys(const char *path, Error **errp) +{ + g_autoptr(GError) err = NULL; + g_autofree char *contents = NULL; + + ERRP_GUARD(); + + if (!g_file_get_contents(path, &contents, NULL, &err)) { + error_setg(errp, "failed to read '%s': %s", path, err->message); + return NULL; + } + + return g_strsplit(contents, "\n", -1); + +} + +void +qmp_guest_ssh_add_authorized_keys(const char *username, strList *keys, + Error **errp) +{ + g_autofree struct passwd *p = NULL; + g_autofree char *ssh_path = NULL; + g_autofree char *authkeys_path = NULL; + g_auto(GStrv) authkeys = NULL; + strList *k; + size_t nkeys, nauthkeys; + + ERRP_GUARD(); + + if (!check_openssh_pub_keys(keys, &nkeys, errp)) { + return; + } + + p = get_passwd_entry(username, errp); + if (p == NULL) { + return; + } + + ssh_path = g_build_filename(p->pw_dir, ".ssh", NULL); + authkeys_path = g_build_filename(ssh_path, "authorized_keys", NULL); + + authkeys = read_authkeys(authkeys_path, NULL); + if (authkeys == NULL) { + if (!g_file_test(ssh_path, G_FILE_TEST_IS_DIR) && + !mkdir_for_user(ssh_path, p, 0700, errp)) { + return; + } + } + + nauthkeys = authkeys ? g_strv_length(authkeys) : 0; + authkeys = g_realloc_n(authkeys, nauthkeys + nkeys + 1, sizeof(char *)); + memset(authkeys + nauthkeys, 0, (nkeys + 1) * sizeof(char *)); + + for (k = keys; k != NULL; k = k->next) { + if (g_strv_contains((const gchar * const *)authkeys, k->value)) { + continue; + } + authkeys[nauthkeys++] = g_strdup(k->value); + } + + write_authkeys(authkeys_path, authkeys, p, errp); +} + +void +qmp_guest_ssh_remove_authorized_keys(const char *username, strList *keys, + Error **errp) +{ + g_autofree struct passwd *p = NULL; + g_autofree char *authkeys_path = NULL; + g_autofree GStrv new_keys = NULL; /* do not own the strings */ + g_auto(GStrv) authkeys = NULL; + GStrv a; + size_t nkeys = 0; + + ERRP_GUARD(); + + if (!check_openssh_pub_keys(keys, NULL, errp)) { + return; + } + + p = get_passwd_entry(username, errp); + if (p == NULL) { + return; + } + + authkeys_path = g_build_filename(p->pw_dir, ".ssh", + "authorized_keys", NULL); + if (!g_file_test(authkeys_path, G_FILE_TEST_EXISTS)) { + return; + } + authkeys = read_authkeys(authkeys_path, errp); + if (authkeys == NULL) { + return; + } + + new_keys = g_new0(char *, g_strv_length(authkeys) + 1); + for (a = authkeys; *a != NULL; a++) { + strList *k; + + for (k = keys; k != NULL; k = k->next) { + if (g_str_equal(k->value, *a)) { + break; + } + } + if (k != NULL) { + continue; + } + + new_keys[nkeys++] = *a; + } + + write_authkeys(authkeys_path, new_keys, p, errp); +} + + +#ifdef QGA_BUILD_UNIT_TEST +#if GLIB_CHECK_VERSION(2, 60, 0) +static const strList test_key2 = { + .value = (char *)"algo key2 comments" +}; + +static const strList test_key1_2 = { + .value = (char *)"algo key1 comments", + .next = (strList *)&test_key2, +}; + +static char * +test_get_authorized_keys_path(void) +{ + return g_build_filename(g_get_home_dir(), ".ssh", "authorized_keys", NULL); +} + +static void +test_authorized_keys_set(const char *contents) +{ + g_autoptr(GError) err = NULL; + g_autofree char *path = NULL; + int ret; + + path = g_build_filename(g_get_home_dir(), ".ssh", NULL); + ret = g_mkdir_with_parents(path, 0700); + g_assert_cmpint(ret, ==, 0); + g_free(path); + + path = test_get_authorized_keys_path(); + g_file_set_contents(path, contents, -1, &err); + g_assert_no_error(err); +} + +static void +test_authorized_keys_equal(const char *expected) +{ + g_autoptr(GError) err = NULL; + g_autofree char *path = NULL; + g_autofree char *contents = NULL; + + path = test_get_authorized_keys_path(); + g_file_get_contents(path, &contents, NULL, &err); + g_assert_no_error(err); + + g_assert_cmpstr(contents, ==, expected); +} + +static void +test_invalid_user(void) +{ + Error *err = NULL; + + qmp_guest_ssh_add_authorized_keys("", NULL, &err); + error_free_or_abort(&err); + + qmp_guest_ssh_remove_authorized_keys("", NULL, &err); + error_free_or_abort(&err); +} + +static void +test_invalid_key(void) +{ + strList key = { + .value = (char *)"not a valid\nkey" + }; + Error *err = NULL; + + qmp_guest_ssh_add_authorized_keys(g_get_user_name(), &key, &err); + error_free_or_abort(&err); + + qmp_guest_ssh_remove_authorized_keys(g_get_user_name(), &key, &err); + error_free_or_abort(&err); +} + +static void +test_add_keys(void) +{ + Error *err = NULL; + + qmp_guest_ssh_add_authorized_keys(g_get_user_name(), + (strList *)&test_key2, &err); + g_assert_null(err); + + test_authorized_keys_equal("algo key2 comments"); + + qmp_guest_ssh_add_authorized_keys(g_get_user_name(), + (strList *)&test_key1_2, &err); + g_assert_null(err); + + /* key2 came first, and should'nt be duplicated */ + test_authorized_keys_equal("algo key2 comments\n" + "algo key1 comments"); +} + +static void +test_remove_keys(void) +{ + Error *err = NULL; + static const char *authkeys = + "algo key1 comments\n" + /* originally duplicated */ + "algo key1 comments\n" + "# a commented line\n" + "algo some-key another\n"; + + test_authorized_keys_set(authkeys); + qmp_guest_ssh_remove_authorized_keys(g_get_user_name(), + (strList *)&test_key2, &err); + g_assert_null(err); + test_authorized_keys_equal(authkeys); + + qmp_guest_ssh_remove_authorized_keys(g_get_user_name(), + (strList *)&test_key1_2, &err); + g_assert_null(err); + test_authorized_keys_equal("# a commented line\n" + "algo some-key another\n"); +} + +int main(int argc, char *argv[]) +{ + setlocale(LC_ALL, ""); + + g_test_init(&argc, &argv, G_TEST_OPTION_ISOLATE_DIRS, NULL); + + g_test_add_func("/qga/ssh/invalid_user", test_invalid_user); + g_test_add_func("/qga/ssh/invalid_key", test_invalid_key); + g_test_add_func("/qga/ssh/add_keys", test_add_keys); + g_test_add_func("/qga/ssh/remove_keys", test_remove_keys); + + return g_test_run(); +} +#else +int main(int argc, char *argv[]) +{ + g_test_message("test skipped, needs glib >= 2.60"); + return 0; +} +#endif /* GLIB_2_60 */ +#endif /* BUILD_UNIT_TEST */ diff --git a/qga/meson.build b/qga/meson.build index cd08bd953a..635de9af41 100644 --- a/qga/meson.build +++ b/qga/meson.build @@ -35,7 +35,9 @@ qga_ss.add(files( )) qga_ss.add(when: 'CONFIG_POSIX', if_true: files( 'channel-posix.c', - 'commands-posix.c')) + 'commands-posix.c', + 'commands-posix-ssh.c', +)) qga_ss.add(when: 'CONFIG_WIN32', if_true: files( 'channel-win32.c', 'commands-win32.c', @@ -87,3 +89,24 @@ else endif alias_target('qemu-ga', all_qga) + +test_env = environment() +test_env.set('G_TEST_SRCDIR', meson.current_source_dir()) +test_env.set('G_TEST_BUILDDIR', meson.current_build_dir()) + +# disable qga-ssh-test for now. glib's G_TEST_OPTION_ISOLATE_DIRS triggers +# the leak detector in build-oss-fuzz Gitlab CI test. we should re-enable +# this when an alternative is implemented or when the underlying glib +# issue is identified/fix +#if 'CONFIG_POSIX' in config_host +if false + qga_ssh_test = executable('qga-ssh-test', + files('commands-posix-ssh.c'), + dependencies: [qemuutil], + c_args: ['-DQGA_BUILD_UNIT_TEST']) + + test('qga-ssh-test', + qga_ssh_test, + env: test_env, + suite: ['unit', 'qga']) +endif diff --git a/qga/qapi-schema.json b/qga/qapi-schema.json index e123a000be..a2727ed86b 100644 --- a/qga/qapi-schema.json +++ b/qga/qapi-schema.json @@ -1346,3 +1346,38 @@ ## { 'command': 'guest-get-devices', 'returns': ['GuestDeviceInfo'] } + +## +# @guest-ssh-add-authorized-keys: +# +# @username: the user account to add the authorized keys +# @keys: the public keys to add (in OpenSSH/sshd(8) authorized_keys format) +# +# Append public keys to user .ssh/authorized_keys on Unix systems (not +# implemented for other systems). +# +# Returns: Nothing on success. +# +# Since: 5.2 +## +{ 'command': 'guest-ssh-add-authorized-keys', + 'data': { 'username': 'str', 'keys': ['str'] }, + 'if': 'defined(CONFIG_POSIX)' } + +## +# @guest-ssh-remove-authorized-keys: +# +# @username: the user account to remove the authorized keys +# @keys: the public keys to remove (in OpenSSH/sshd(8) authorized_keys format) +# +# Remove public keys from the user .ssh/authorized_keys on Unix systems (not +# implemented for other systems). It's not an error if the key is already +# missing. +# +# Returns: Nothing on success. +# +# Since: 5.2 +## +{ 'command': 'guest-ssh-remove-authorized-keys', + 'data': { 'username': 'str', 'keys': ['str'] }, + 'if': 'defined(CONFIG_POSIX)' }