From nobody Tue Nov 4 18:51:46 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1530651540868460.5143582029501; Tue, 3 Jul 2018 13:59:00 -0700 (PDT) Received: from localhost ([::1]:42612 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1faSO4-0003u9-4O for importer@patchew.org; Tue, 03 Jul 2018 16:59:00 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:41265) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1faSIz-0008QK-Kc for qemu-devel@nongnu.org; Tue, 03 Jul 2018 16:53:47 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1faSIu-0000xh-K6 for qemu-devel@nongnu.org; Tue, 03 Jul 2018 16:53:45 -0400 Received: from mx0a-001b2d01.pphosted.com ([148.163.156.1]:60146) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1faSIu-0000xD-55 for qemu-devel@nongnu.org; Tue, 03 Jul 2018 16:53:40 -0400 Received: from pps.filterd (m0098394.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.22/8.16.0.22) with SMTP id w63KnERk088367 for ; Tue, 3 Jul 2018 16:53:39 -0400 Received: from e33.co.us.ibm.com (e33.co.us.ibm.com [32.97.110.151]) by mx0a-001b2d01.pphosted.com with ESMTP id 2k0dpa6kec-1 (version=TLSv1.2 cipher=AES256-GCM-SHA384 bits=256 verify=NOT) for ; Tue, 03 Jul 2018 16:53:38 -0400 Received: from localhost by e33.co.us.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Tue, 3 Jul 2018 14:53:38 -0600 Received: from b03cxnp08025.gho.boulder.ibm.com (9.17.130.17) by e33.co.us.ibm.com (192.168.1.133) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; (version=TLSv1/SSLv3 cipher=AES256-GCM-SHA384 bits=256/256) Tue, 3 Jul 2018 14:53:35 -0600 Received: from b03ledav004.gho.boulder.ibm.com (b03ledav004.gho.boulder.ibm.com [9.17.130.235]) by b03cxnp08025.gho.boulder.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id w63KrYa321954874 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL); Tue, 3 Jul 2018 13:53:34 -0700 Received: from b03ledav004.gho.boulder.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 5CB547805C; Tue, 3 Jul 2018 14:53:34 -0600 (MDT) Received: from b03ledav004.gho.boulder.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 4087D7805E; Tue, 3 Jul 2018 14:53:34 -0600 (MDT) Received: from localhost (unknown [9.53.92.156]) by b03ledav004.gho.boulder.ibm.com (Postfix) with ESMTP; Tue, 3 Jul 2018 14:53:34 -0600 (MDT) From: Michael Roth To: qemu-devel@nongnu.org Date: Tue, 3 Jul 2018 15:52:17 -0500 X-Mailer: git-send-email 2.11.0 In-Reply-To: <20180703205221.24788-1-mdroth@linux.vnet.ibm.com> References: <20180703205221.24788-1-mdroth@linux.vnet.ibm.com> X-TM-AS-GCONF: 00 x-cbid: 18070320-0036-0000-0000-00000A0A947A X-IBM-SpamModules-Scores: X-IBM-SpamModules-Versions: BY=3.00009303; HX=3.00000241; KW=3.00000007; PH=3.00000004; SC=3.00000266; SDB=6.01056103; UDB=6.00541730; IPR=6.00834025; MB=3.00021980; MTD=3.00000008; XFM=3.00000015; UTC=2018-07-03 20:53:36 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 18070320-0037-0000-0000-000047F959D9 Message-Id: <20180703205221.24788-11-mdroth@linux.vnet.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:, , definitions=2018-07-03_09:, , signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 malwarescore=0 suspectscore=3 phishscore=0 bulkscore=0 spamscore=0 clxscore=1015 lowpriorityscore=0 mlxscore=0 impostorscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1806210000 definitions=main-1807030235 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [generic] [fuzzy] X-Received-From: 148.163.156.1 Subject: [Qemu-devel] [PULL 10/14] 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: peter.maydell@linaro.org, Daniel Henrique Barboza Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Daniel Henrique Barboza 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 Signed-off-by: Michael Roth --- 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 bd2c3514d8..a8299ecc77 100644 --- a/qga/commands-posix.c +++ b/qga/commands-posix.c @@ -1478,75 +1478,43 @@ qmp_guest_fstrim(bool has_minimum, int64_t minimum,= Error **errp) #define SUSPEND_MODE_RAM 2 #define SUSPEND_MODE_HYBRID 3 =20 -static void bios_supports_mode(int suspend_mode, Error **errp) +static bool pmutils_supports_mode(int suspend_mode, Error **errp) { Error *local_err =3D NULL; - const char *pmutils_arg, *sysfile_str; + const char *pmutils_arg; const char *pmutils_bin =3D "pm-is-supported"; char *pmutils_path; pid_t pid; int status; + bool ret =3D false; =20 switch (suspend_mode) { =20 case SUSPEND_MODE_DISK: pmutils_arg =3D "--hibernate"; - sysfile_str =3D "disk"; break; case SUSPEND_MODE_RAM: pmutils_arg =3D "--suspend"; - sysfile_str =3D "mem"; break; case SUSPEND_MODE_HYBRID: pmutils_arg =3D "--suspend-hybrid"; - sysfile_str =3D NULL; break; default: - error_setg(errp, "guest suspend mode not supported"); - return; + return ret; } =20 pmutils_path =3D g_find_program_in_path(pmutils_bin); + if (!pmutils_path) { + return ret; + } =20 pid =3D 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 =3D open(LINUX_SYS_STATE_FILE, O_RDONLY); - if (fd < 0) { - _exit(SUSPEND_NOT_SUPPORTED); - } - - ret =3D read(fd, buf, sizeof(buf)-1); - if (ret <=3D 0) { - _exit(SUSPEND_NOT_SUPPORTED); - } - buf[ret] =3D '\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"); @@ -1559,17 +1527,11 @@ static void bios_supports_mode(int suspend_mode, Er= ror **errp) goto out; } =20 - if (!WIFEXITED(status)) { - error_setg(errp, "child process has terminated abnormally"); - goto out; - } - switch (WEXITSTATUS(status)) { case SUSPEND_SUPPORTED: + ret =3D true; goto out; case SUSPEND_NOT_SUPPORTED: - error_setg(errp, - "the requested suspend mode is not supported by the gue= st"); goto out; default: error_setg(errp, @@ -1580,6 +1542,64 @@ static void bios_supports_mode(int suspend_mode, Err= or **errp) =20 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 =3D "disk"; + break; + case SUSPEND_MODE_RAM: + sysfile_str =3D "mem"; + break; + default: + return false; + } + + fd =3D open(LINUX_SYS_STATE_FILE, O_RDONLY); + if (fd < 0) { + return false; + } + + ret =3D read(fd, buf, sizeof(buf) - 1); + if (ret <=3D 0) { + return false; + } + buf[ret] =3D '\0'; + + if (strstr(buf, sysfile_str)) { + return true; + } + return false; +} + +static void bios_supports_mode(int suspend_mode, Error **errp) +{ + Error *local_err =3D NULL; + bool ret; + + ret =3D pmutils_supports_mode(suspend_mode, &local_err); + if (ret) { + return; + } + if (local_err) { + error_propagate(errp, local_err); + return; + } + ret =3D linux_sys_state_supports_mode(suspend_mode, errp); + if (!ret) { + error_setg(errp, + "the requested suspend mode is not supported by the gue= st"); + return; + } } =20 static void guest_suspend(int suspend_mode, Error **errp) --=20 2.11.0