From patchwork Thu Jun 21 10:21:49 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: 10479589 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 570FB604D3 for ; Thu, 21 Jun 2018 10:23:41 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 45E6427EED for ; Thu, 21 Jun 2018 10:23:41 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 3A1D727F97; Thu, 21 Jun 2018 10:23:41 +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 A496527EED for ; Thu, 21 Jun 2018 10:23:40 +0000 (UTC) Received: from localhost ([::1]:54317 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fVwkd-0003eo-Sy for patchwork-qemu-devel@patchwork.kernel.org; Thu, 21 Jun 2018 06:23:39 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:53258) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fVwjH-0002lm-SB for qemu-devel@nongnu.org; Thu, 21 Jun 2018 06:22:17 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fVwjG-0001ec-NT for qemu-devel@nongnu.org; Thu, 21 Jun 2018 06:22:15 -0400 Received: from mail-qt0-x243.google.com ([2607:f8b0:400d:c0d::243]:38023) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1fVwjG-0001eS-IK for qemu-devel@nongnu.org; Thu, 21 Jun 2018 06:22:14 -0400 Received: by mail-qt0-x243.google.com with SMTP id e11-v6so1730310qtp.5 for ; Thu, 21 Jun 2018 03:22:14 -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=iVZrztSVcELAhqbdHKFYyi+XFU11JhBnjsiQg2pn02g=; b=Jlhj9qzkJtOyjCzz/Z1mhaN7pZv5BLkcgCoOl9I2cNz9+WbjBtk08WDAVBp8EpFAwY +pVK82XiFgFmNoDcI9p+2uIX8K6G2vKuF45SHLC09WhxByEpqg46qrRxBO03i4gAn0Wi Z14r2o9o1pe6bx28m8Sr3ykd1+iv/cqTy34OpjznDri3KVx9Az2Dm7zRm7KQY6RGwbBE X7+NUvzzeiC9mbNJIwHj55YSojwIrSOruv8YiYz5tFh1EOD1I+ia4G1JK8MTsyudMmWz O6Y97eplwYYgDZ3dwweaGwfsEw+sjKdzU6iZaxkcuKjuyRQmcmwtNTl3zxtvfI86wBF4 dPaw== 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=iVZrztSVcELAhqbdHKFYyi+XFU11JhBnjsiQg2pn02g=; b=pQUt2dISwqH7fP3wo6tczlV9KGA9m+5wJnK+uKIbJi9qUXYv/WlZFfic9WatS1ByZL Jx5P2PG9PI/OoUHHxJ3WSZtQUkIYYzS/1UZwAO6tPe6PsSSkIE0DozaNriZdDKHqPLp/ XKUbrguZUapcp4EWiIjyxCJIjvkZsL3itT+PMxs6drxkRVbfUDZU2iZRQtY8sQFmc0Pt cdBRf3MP3HuDWN1CMje7EJfY1RW+cMpEo5CutwOgsc1xvjh8cDyf6LoYOhCNmcNYpPDJ syS8GkW9ctW4xb3JKZXvLaWbiRzRU1Z+rL1zqdFUM1flntt3HMyZj8YK6V06r5guJj8j BIxg== X-Gm-Message-State: APt69E3YzZActuTM1lm/kJhN5IdJb7CGyEP9HcVmLHRiNwGl92dFPfhw wZC8PTJXJtdsc+wQ51gx7yIs4KaqX2E= X-Google-Smtp-Source: ADUXVKKHY3fPLC05HBxQHq7NsPdB3DsU/gX22kU8BxhtiUHs+5WPfdmDmSPQ1FIeVGH4dOmAp4r2Dw== X-Received: by 2002:ac8:5155:: with SMTP id h21-v6mr21841485qtn.311.1529576533998; Thu, 21 Jun 2018 03:22:13 -0700 (PDT) Received: from localhost.localdomain ([2804:431:f701:8296:fe0b:2550:5aa8:2171]) by smtp.gmail.com with ESMTPSA id d42-v6sm2256974qtd.88.2018.06.21.03.22.12 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 21 Jun 2018 03:22:13 -0700 (PDT) From: Daniel Henrique Barboza To: qemu-devel@nongnu.org Date: Thu, 21 Jun 2018 07:21:49 -0300 Message-Id: <20180621102153.28443-3-danielhb413@gmail.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20180621102153.28443-1-danielhb413@gmail.com> References: <20180621102153.28443-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 v2 2/6] qga: bios_supports_mode: decoupling pm-utils and sys logic 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 , marcandre.lureau@gmail.com, mdroth@linux.vnet.ibm.com, armbru@redhat.com Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP In bios_supports_mode there is a verification to assert if the chosen suspend mode is supported by the pmutils tools and, if not, we see if the Linux sys state files supports it. This verification is done in the same function, one after the other, and it works for now. But, when adding a new suspend mechanism that will not necessarily follow the same return 0 or 1 logic of pmutils, this code will be hard to deal with. This patch decouple the two existing logics into their own functions, pmutils_supports_mode and linux_sys_state_supports_mode, which in turn are used inside bios_support_mode. The existing logic is kept but now it's easier to extend it. Signed-off-by: Daniel Henrique Barboza --- qga/commands-posix.c | 116 +++++++++++++++++++++++++------------------ 1 file changed, 68 insertions(+), 48 deletions(-) diff --git a/qga/commands-posix.c b/qga/commands-posix.c index 63c49791a4..89ffd8dc88 100644 --- a/qga/commands-posix.c +++ b/qga/commands-posix.c @@ -1442,75 +1442,43 @@ qmp_guest_fstrim(bool has_minimum, int64_t minimum, Error **errp) #define SUSPEND_MODE_RAM 2 #define SUSPEND_MODE_HYBRID 3 -static void bios_supports_mode(int suspend_mode, Error **errp) +static bool pmutils_supports_mode(int suspend_mode, Error **errp) { Error *local_err = NULL; - const char *pmutils_arg, *sysfile_str; + const char *pmutils_arg; const char *pmutils_bin = "pm-is-supported"; char *pmutils_path; pid_t pid; int status; + bool ret = false; switch (suspend_mode) { case SUSPEND_MODE_DISK: pmutils_arg = "--hibernate"; - sysfile_str = "disk"; break; case SUSPEND_MODE_RAM: pmutils_arg = "--suspend"; - sysfile_str = "mem"; break; case SUSPEND_MODE_HYBRID: pmutils_arg = "--suspend-hybrid"; - sysfile_str = NULL; break; default: - error_setg(errp, "guest suspend mode not supported"); - return; + return ret; } pmutils_path = g_find_program_in_path(pmutils_bin); + if (!pmutils_path) { + return ret; + } pid = fork(); if (!pid) { - char buf[32]; /* hopefully big enough */ - ssize_t ret; - int fd; - setsid(); - reopen_fd_to_null(0); - reopen_fd_to_null(1); - reopen_fd_to_null(2); - - if (pmutils_path) { - execle(pmutils_path, pmutils_bin, pmutils_arg, NULL, environ); - } - + execle(pmutils_path, pmutils_bin, pmutils_arg, NULL, environ); /* - * If we get here either pm-utils is not installed or execle() has - * failed. Let's try the manual method if the caller wants it. + * If we get here execle() has failed. */ - - if (!sysfile_str) { - _exit(SUSPEND_NOT_SUPPORTED); - } - - fd = open(LINUX_SYS_STATE_FILE, O_RDONLY); - if (fd < 0) { - _exit(SUSPEND_NOT_SUPPORTED); - } - - ret = read(fd, buf, sizeof(buf)-1); - if (ret <= 0) { - _exit(SUSPEND_NOT_SUPPORTED); - } - buf[ret] = '\0'; - - if (strstr(buf, sysfile_str)) { - _exit(SUSPEND_SUPPORTED); - } - _exit(SUSPEND_NOT_SUPPORTED); } else if (pid < 0) { error_setg_errno(errp, errno, "failed to create child process"); @@ -1523,17 +1491,11 @@ static void bios_supports_mode(int suspend_mode, Error **errp) goto out; } - if (!WIFEXITED(status)) { - error_setg(errp, "child process has terminated abnormally"); - goto out; - } - switch (WEXITSTATUS(status)) { case SUSPEND_SUPPORTED: + ret = true; goto out; case SUSPEND_NOT_SUPPORTED: - error_setg(errp, - "the requested suspend mode is not supported by the guest"); goto out; default: error_setg(errp, @@ -1544,6 +1506,64 @@ static void bios_supports_mode(int suspend_mode, Error **errp) out: g_free(pmutils_path); + return ret; +} + +static bool linux_sys_state_supports_mode(int suspend_mode, Error **errp) +{ + const char *sysfile_str; + char buf[32]; /* hopefully big enough */ + int fd; + ssize_t ret; + + switch (suspend_mode) { + + case SUSPEND_MODE_DISK: + sysfile_str = "disk"; + break; + case SUSPEND_MODE_RAM: + sysfile_str = "mem"; + break; + default: + return false; + } + + fd = open(LINUX_SYS_STATE_FILE, O_RDONLY); + if (fd < 0) { + return false; + } + + ret = read(fd, buf, sizeof(buf) - 1); + if (ret <= 0) { + return false; + } + buf[ret] = '\0'; + + if (strstr(buf, sysfile_str)) { + return true; + } + return false; +} + +static void bios_supports_mode(int suspend_mode, Error **errp) +{ + Error *local_err = NULL; + bool ret; + + ret = pmutils_supports_mode(suspend_mode, &local_err); + if (ret) { + return; + } + if (local_err) { + error_propagate(errp, local_err); + return; + } + ret = linux_sys_state_supports_mode(suspend_mode, errp); + if (!ret) { + error_setg(errp, + "the requested suspend mode is not supported by the guest"); + return; + } } static void guest_suspend(int suspend_mode, Error **errp)