From patchwork Tue Jun 19 19:38:05 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Henrique Barboza X-Patchwork-Id: 10475313 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 9FF8260230 for ; Tue, 19 Jun 2018 19:44:57 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9028528BD4 for ; Tue, 19 Jun 2018 19:44:57 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 852A128BDA; Tue, 19 Jun 2018 19:44:57 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.8 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI, T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id AC74B28BD4 for ; Tue, 19 Jun 2018 19:44:56 +0000 (UTC) Received: from localhost ([::1]:44665 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fVMYh-0006Kj-WA for patchwork-qemu-devel@patchwork.kernel.org; Tue, 19 Jun 2018 15:44:56 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:52890) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fVMSi-0001rk-O7 for qemu-devel@nongnu.org; Tue, 19 Jun 2018 15:38:46 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fVMSh-00037q-DY for qemu-devel@nongnu.org; Tue, 19 Jun 2018 15:38:44 -0400 Received: from mail-qt0-x243.google.com ([2607:f8b0:400d:c0d::243]:44709) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1fVMSh-00037T-8d for qemu-devel@nongnu.org; Tue, 19 Jun 2018 15:38:43 -0400 Received: by mail-qt0-x243.google.com with SMTP id l33-v6so854514qta.11 for ; Tue, 19 Jun 2018 12:38:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=pY7mInYInchvRIVhw0qCZ+4QWNwnOQ0I2TjYXJjdRks=; b=D25g/t0Q1vTmqukB2rEYZhLWLC1B8blg0weVaDlLOZBcSb6ePHTo7y/0Ra8ZAAwFAj 6qYXrNSL2TxAffbBDP0lazSQQcVk7xrXGC1idyROeDiyeHMFVxVXAX6RB8kjSE2QQLQH +GDTHR51ouXIgpI5tdq2Jj25Ol2Ip8nK45klI7dRHguTMlOy14E1lA90MnVzlWbEC7wC qeHbWS1bAQ8NmcGA/i89iVha8x8aC8sn+iIt/UQINbHumuhdOaMbd6gHPSSJ4BmjgbJG i9+UrbPZezPD+O49AhXPFbDSKIblI4q6nVXnzDi9LiGhqf4tUl/sMSNDcqRgWiU8TvEw JgBw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=pY7mInYInchvRIVhw0qCZ+4QWNwnOQ0I2TjYXJjdRks=; b=DAWgyg5bNwqzbsl3FL3Z/m1gE8IH5Wlpnta6zEMFpQM9esR60tDIhQlmWCTHRBL8PG V7AgqKuJf/Big70d/jR5ArzCR1lmvCx0rjfnWQ/hHKoIwFNPhTNnVNXA4AtCcZkdsxGi /w79nqQH7MNG3Mazajhz6yiVOR/rdyOEGLU4Nem2KqZSlnpM/gJpeb4/h7shWeipGZnd umoDYOt2i7izg15XpHk7rVotK4juXXh1MjZqqHyZwsqety+gKQLmuou5r9h2UNhJHCMY JpHhr5SjAqEPj0Uv7eptN+vR/1PBS3BTxJay/Xiioul7Fb1BJhqiQdQr13U3sFAo2xyN YX/A== X-Gm-Message-State: APt69E3g03Ozfg1adXZVgO2FyoCOSiOFHAm9TGYGcc6kWeTP0z1FkNQy yc3STsSBjx89S2NvlrdI3NaqJlfGPp4= X-Google-Smtp-Source: ADUXVKIUKPpiumsM8cbLId1wA3g6H/zV7bHYi42XkDW9YfbeOIN8XxvCy+Oat7Ip09Xmrz7o99hk2Q== X-Received: by 2002:ac8:1c37:: with SMTP id a52-v6mr16500651qtk.411.1529437122469; Tue, 19 Jun 2018 12:38:42 -0700 (PDT) Received: from localhost.localdomain ([2804:431:f701:9a90:70ce:7c7a:343e:57]) by smtp.gmail.com with ESMTPSA id a62-v6sm419686qka.11.2018.06.19.12.38.39 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Tue, 19 Jun 2018 12:38:42 -0700 (PDT) From: Daniel Henrique Barboza To: qemu-devel@nongnu.org Date: Tue, 19 Jun 2018 16:38:05 -0300 Message-Id: <20180619193806.17419-6-danielhb413@gmail.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20180619193806.17419-1-danielhb413@gmail.com> References: <20180619193806.17419-1-danielhb413@gmail.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:400d:c0d::243 Subject: [Qemu-devel] [PATCH v1 5/6] qga: adding systemd hibernate/suspend/hybrid-sleep support X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Daniel Henrique Barboza , armbru@redhat.com, mdroth@linux.vnet.ibm.com Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP pmutils isn't being supported by newer OSes like Fedora 27 or Mint. This means that the only suspend option QGA offers for these guests are writing directly into the Linux sys state file. This also means that QGA also loses the ability to do hybrid suspend in those guests - this suspend mode is only available when using pmutils. Newer guests can use systemd facilities to do all the suspend times QGA supports. The mapping in comparison with pmutils is: - pm-hibernate -> systemctl hibernate - pm-suspend -> systemctl suspend - pm-suspend-hybrid -> systemctl hybrid-sleep To discover whether systemd supports these functions, we inspect the status of the services that implements them. With this patch, we can offer hybrid suspend again for newer guests that do not have pmutils support anymore. Signed-off-by: Daniel Henrique Barboza --- qga/commands-posix.c | 72 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/qga/commands-posix.c b/qga/commands-posix.c index d5e3805ce9..6a573de86d 100644 --- a/qga/commands-posix.c +++ b/qga/commands-posix.c @@ -1486,6 +1486,63 @@ out: return ret; } +static bool systemd_supports_mode(SuspendMode mode, Error **errp) +{ + Error *local_err = NULL; + const char *systemctl_args[3] = {"systemd-hibernate", "systemd-suspend", + "systemd-hybrid-sleep"}; + const char *cmd[4] = {"systemctl", "status", systemctl_args[mode], NULL}; + int status; + + status = run_process_child(cmd, &local_err); + + /* + * systemctl status uses LSB return codes so we can expect + * status > 0 and be ok. To assert if the guest has support + * for the selected suspend mode, status should be < 4. 4 is + * the code for unknown service status, the return value when + * the service does not exist. A common value is status = 3 + * (program is not running). + */ + if (status > 0 && status < 4) { + return true; + } + + if (local_err) { + error_propagate(errp, local_err); + } + + return false; +} + +static void systemd_suspend(SuspendMode mode, Error **errp) +{ + Error *local_err = NULL; + const char *systemctl_args[3] = {"hibernate", "suspend", "hybrid-sleep"}; + const char *cmd[3] = {"systemctl", systemctl_args[mode], NULL}; + int status; + + status = run_process_child(cmd, &local_err); + + if (status == 0) { + return; + } + + if (status == -1) { + error_setg(errp, "the helper program '%s' was not found", + systemctl_args[mode]); + return; + } + + if (local_err) { + error_propagate(errp, local_err); + } else { + error_setg(errp, "the helper program 'systemctl %s' returned an " + " unexpected exit status code (%d)", + systemctl_args[mode], status); + } +} + static bool pmutils_supports_mode(SuspendMode mode, Error **errp) { Error *local_err = NULL; @@ -1629,6 +1686,14 @@ static void bios_supports_mode(SuspendMode mode, Error **errp) Error *local_err = NULL; bool ret; + ret = systemd_supports_mode(mode, &local_err); + if (ret) { + return; + } + if (local_err) { + error_propagate(errp, local_err); + return; + } ret = pmutils_supports_mode(mode, &local_err); if (ret) { return; @@ -1654,6 +1719,13 @@ static void guest_suspend(SuspendMode mode, Error **errp) return; } + systemd_suspend(mode, &local_err); + if (!local_err) { + return; + } + + local_err = NULL; + pmutils_suspend(mode, &local_err); if (!local_err) { return;