From nobody Tue Nov 4 18:51:44 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 1530651864195335.4083110715135; Tue, 3 Jul 2018 14:04:24 -0700 (PDT) Received: from localhost ([::1]:42642 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1faSTF-0007r8-77 for importer@patchew.org; Tue, 03 Jul 2018 17:04:21 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:41257) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1faSIz-0008QH-IC for qemu-devel@nongnu.org; Tue, 03 Jul 2018 16:53:48 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1faSIu-0000xo-Ma for qemu-devel@nongnu.org; Tue, 03 Jul 2018 16:53:45 -0400 Received: from mx0b-001b2d01.pphosted.com ([148.163.158.5]:45562 helo=mx0a-001b2d01.pphosted.com) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1faSIu-0000xH-FZ for qemu-devel@nongnu.org; Tue, 03 Jul 2018 16:53:40 -0400 Received: from pps.filterd (m0098421.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.22/8.16.0.22) with SMTP id w63KnBfL041238 for ; Tue, 3 Jul 2018 16:53:39 -0400 Received: from e34.co.us.ibm.com (e34.co.us.ibm.com [32.97.110.152]) by mx0a-001b2d01.pphosted.com with ESMTP id 2k0dp9pbym-1 (version=TLSv1.2 cipher=AES256-GCM-SHA384 bits=256 verify=NOT) for ; Tue, 03 Jul 2018 16:53:39 -0400 Received: from localhost by e34.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 e34.co.us.ibm.com (192.168.1.134) 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:36 -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 w63KrZB418743680 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL); Tue, 3 Jul 2018 13:53:35 -0700 Received: from b03ledav004.gho.boulder.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 5430C7805C; Tue, 3 Jul 2018 14:53:35 -0600 (MDT) Received: from b03ledav004.gho.boulder.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 3B96A7805F; Tue, 3 Jul 2018 14:53:35 -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:35 -0600 (MDT) From: Michael Roth To: qemu-devel@nongnu.org Date: Tue, 3 Jul 2018 15:52:18 -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-0016-0000-0000-000009038D41 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.00541729; IPR=6.00834025; MB=3.00021980; MTD=3.00000008; XFM=3.00000015; UTC=2018-07-03 20:53:37 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 18070320-0017-0000-0000-00003F813A7E Message-Id: <20180703205221.24788-12-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.158.5 Subject: [Qemu-devel] [PULL 11/14] qga: guest_suspend: 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 Following the same logic of the previous patch, let's also decouple the suspend logic from guest_suspend into specialized functions, one for each strategy we support at this moment. Signed-off-by: Daniel Henrique Barboza Signed-off-by: Michael Roth --- qga/commands-posix.c | 170 ++++++++++++++++++++++++++++++++---------------= ---- 1 file changed, 108 insertions(+), 62 deletions(-) diff --git a/qga/commands-posix.c b/qga/commands-posix.c index a8299ecc77..9ebd77b894 100644 --- a/qga/commands-posix.c +++ b/qga/commands-posix.c @@ -1545,6 +1545,65 @@ out: return ret; } =20 +static void pmutils_suspend(int suspend_mode, Error **errp) +{ + Error *local_err =3D NULL; + const char *pmutils_bin; + char *pmutils_path; + pid_t pid; + int status; + + switch (suspend_mode) { + + case SUSPEND_MODE_DISK: + pmutils_bin =3D "pm-hibernate"; + break; + case SUSPEND_MODE_RAM: + pmutils_bin =3D "pm-suspend"; + break; + case SUSPEND_MODE_HYBRID: + pmutils_bin =3D "pm-suspend-hybrid"; + break; + default: + error_setg(errp, "unknown guest suspend mode"); + return; + } + + pmutils_path =3D g_find_program_in_path(pmutils_bin); + if (!pmutils_path) { + error_setg(errp, "the helper program '%s' was not found", pmutils_= bin); + return; + } + + pid =3D fork(); + if (!pid) { + setsid(); + execle(pmutils_path, pmutils_bin, NULL, environ); + /* + * If we get here execle() has failed. + */ + _exit(EXIT_FAILURE); + } else if (pid < 0) { + error_setg_errno(errp, errno, "failed to create child process"); + goto out; + } + + ga_wait_child(pid, &status, &local_err); + if (local_err) { + error_propagate(errp, local_err); + goto out; + } + + if (WEXITSTATUS(status)) { + error_setg(errp, + "the helper program '%s' returned an unexpected exit st= atus" + " code (%d)", pmutils_path, WEXITSTATUS(status)); + } + +out: + g_free(pmutils_path); +} + static bool linux_sys_state_supports_mode(int suspend_mode, Error **errp) { const char *sysfile_str; @@ -1581,64 +1640,28 @@ static bool linux_sys_state_supports_mode(int suspe= nd_mode, Error **errp) return false; } =20 -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; - } -} - -static void guest_suspend(int suspend_mode, Error **errp) +static void linux_sys_state_suspend(int suspend_mode, Error **errp) { Error *local_err =3D NULL; - const char *pmutils_bin, *sysfile_str; - char *pmutils_path; + const char *sysfile_str; pid_t pid; int status; =20 - bios_supports_mode(suspend_mode, &local_err); - if (local_err) { - error_propagate(errp, local_err); - return; - } - switch (suspend_mode) { =20 case SUSPEND_MODE_DISK: - pmutils_bin =3D "pm-hibernate"; sysfile_str =3D "disk"; break; case SUSPEND_MODE_RAM: - pmutils_bin =3D "pm-suspend"; sysfile_str =3D "mem"; break; - case SUSPEND_MODE_HYBRID: - pmutils_bin =3D "pm-suspend-hybrid"; - sysfile_str =3D NULL; - break; default: error_setg(errp, "unknown guest suspend mode"); return; } =20 - pmutils_path =3D g_find_program_in_path(pmutils_bin); - pid =3D fork(); - if (pid =3D=3D 0) { + if (!pid) { /* child */ int fd; =20 @@ -1647,19 +1670,6 @@ static void guest_suspend(int suspend_mode, Error **= errp) reopen_fd_to_null(1); reopen_fd_to_null(2); =20 - if (pmutils_path) { - execle(pmutils_path, pmutils_bin, 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 (!sysfile_str) { - _exit(EXIT_FAILURE); - } - fd =3D open(LINUX_SYS_STATE_FILE, O_WRONLY); if (fd < 0) { _exit(EXIT_FAILURE); @@ -1672,27 +1682,63 @@ static void guest_suspend(int suspend_mode, Error *= *errp) _exit(EXIT_SUCCESS); } else if (pid < 0) { error_setg_errno(errp, errno, "failed to create child process"); - goto out; + return; } =20 ga_wait_child(pid, &status, &local_err); if (local_err) { error_propagate(errp, local_err); - goto out; - } - - if (!WIFEXITED(status)) { - error_setg(errp, "child process has terminated abnormally"); - goto out; + return; } =20 if (WEXITSTATUS(status)) { error_setg(errp, "child process has failed to suspend"); - goto out; } =20 -out: - g_free(pmutils_path); +} + +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; + } +} + +static void guest_suspend(int suspend_mode, Error **errp) +{ + Error *local_err =3D NULL; + + bios_supports_mode(suspend_mode, &local_err); + if (local_err) { + error_propagate(errp, local_err); + return; + } + + pmutils_suspend(suspend_mode, &local_err); + if (!local_err) { + return; + } + + error_free(local_err); + + linux_sys_state_suspend(suspend_mode, &local_err); + if (local_err) { + error_propagate(errp, local_err); + } } =20 void qmp_guest_suspend_disk(Error **errp) --=20 2.11.0