From nobody Tue Feb 10 19:15:40 2026 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; dkim=fail; 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; dmarc=fail(p=none dis=none) header.from=gmail.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1529576828013616.4963498597044; Thu, 21 Jun 2018 03:27:08 -0700 (PDT) Received: from localhost ([::1]:54335 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fVwnz-00067v-6q for importer@patchew.org; Thu, 21 Jun 2018 06:27:07 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:53286) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fVwjM-0002pe-GK for qemu-devel@nongnu.org; Thu, 21 Jun 2018 06:22:22 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fVwjK-0001fp-TH for qemu-devel@nongnu.org; Thu, 21 Jun 2018 06:22:20 -0400 Received: from mail-qt0-x241.google.com ([2607:f8b0:400d:c0d::241]:40104) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1fVwjK-0001fc-Mz for qemu-devel@nongnu.org; Thu, 21 Jun 2018 06:22:18 -0400 Received: by mail-qt0-x241.google.com with SMTP id q6-v6so2308372qtn.7 for ; Thu, 21 Jun 2018 03:22:18 -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.15 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 21 Jun 2018 03:22:17 -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=aafRhRjwdkKj2KqX7ZH5M7Z4C7dJehNJifEC6gJu2Vo=; b=t6IfU//zt2wSTAGyOsZxo2n/Jor5WHCYgwvP1QJJV8EQX/5qzycB5nDmUVrxhdh7kf wkfZfPbTU826ULfjPEDlAv3PqYhNIbgy0iIdcS/1qgb+umCSk+nPipRtY21V+S5rzBjF wNgk4K7t04cGIxK4pQEQcvX7yaSfjoXfaav7Q6diCCYZuCf0ys3H6GJwuHbMQCBRX1Qz 5kdOPtDmT/fBoCnbeeJhNbilzKESaao8wB6EyZpzvpczkC+HMbNEmQaidW/AbV9S1obk qmzml6vTTHQ6YJy4nL9d2E+obPIV0Xx70bN69STj90uh28D3o+m1kN5JLfPFjCd9A2zX QU1Q== 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=aafRhRjwdkKj2KqX7ZH5M7Z4C7dJehNJifEC6gJu2Vo=; b=EKpWW4aJN26WG3C2mZiGd0YkpukbrkoYH9+WaMgPXWruO9ZO7/z97Jr18lqWfV8oFZ CCrRMrUE1qwhbiNVChW8xhSltgyLeY5aL2te7uzFLfLsLeSu1ooeMNExX2Hnc1Otvl/i +Npo7r4QiCdNbOGrG/r26YSDpaTKhA8HEU4ZOUVZyReYPeARnSEBHt9/i5xf1jITYIdP 7e+YBr6rD2N9DAsCsSKg+V6/wExEz+oEnzk3YfT9RyVwXAQcSzrHlMK5N8xVKsiJOJ/H bCRPdv87mIJXhXIJeQTTxhrRzhWjympHMaGtgyD59kI4a+a7vwTSR+WJU1sm/oTFT29n 75yA== X-Gm-Message-State: APt69E0JFhrgTZwFlb2Y6fPV8LiK2QO9oD8e79PgiUJ+A/sJ+kIlPgiZ ueJYP3Z0X63LKn3rg1ETHr6lpCRFm/Y= X-Google-Smtp-Source: ADUXVKKCoqAkhkZRh1FqGM045CQGtx7eh7C5u85vg3AXJo5cvKmgGpBxN2QKab2Rn8lmrUpecHyk4w== X-Received: by 2002:aed:3a29:: with SMTP id n38-v6mr22386597qte.108.1529576537837; Thu, 21 Jun 2018 03:22:17 -0700 (PDT) From: Daniel Henrique Barboza To: qemu-devel@nongnu.org Date: Thu, 21 Jun 2018 07:21:51 -0300 Message-Id: <20180621102153.28443-5-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::241 Subject: [Qemu-devel] [PATCH v2 4/6] qga: removing switch statements, adding run_process_child 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+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" This is a cleanup of the resulting code after detaching pmutils and Linux sys state file logic: - remove the SUSPEND_MODE_* macros and use an enumeration instead. At the same time, drop the switch statements at the start of each function and use the enumeration index to get the right binary/argument; - create a new function called run_process_child(). This function uses g_spawn_sync() to execute a shell command, returning the exit code. This is a common operation in the pmutils functions and will be used in the systemd implementation as well, so this function will avoid code repetition. There are more places inside commands-posix.c where this new run_process_child function can also be used, but one step at a time. Signed-off-by: Daniel Henrique Barboza --- qga/commands-posix.c | 211 +++++++++++++++++-------------------------- 1 file changed, 84 insertions(+), 127 deletions(-) diff --git a/qga/commands-posix.c b/qga/commands-posix.c index 4b6fbd9b80..925544ae6d 100644 --- a/qga/commands-posix.c +++ b/qga/commands-posix.c @@ -1438,152 +1438,117 @@ qmp_guest_fstrim(bool has_minimum, int64_t minimu= m, Error **errp) #define LINUX_SYS_STATE_FILE "/sys/power/state" #define SUSPEND_SUPPORTED 0 #define SUSPEND_NOT_SUPPORTED 1 -#define SUSPEND_MODE_DISK 1 -#define SUSPEND_MODE_RAM 2 -#define SUSPEND_MODE_HYBRID 3 =20 -static bool pmutils_supports_mode(int suspend_mode, Error **errp) +typedef enum { + SUSPEND_MODE_DISK =3D 0, + SUSPEND_MODE_RAM =3D 1, + SUSPEND_MODE_HYBRID =3D 2, +} SuspendMode; + +/* + * Executes a command in a child process using g_spawn_sync, + * returning an int >=3D 0 representing the exit status of the + * process. + * + * If the program wasn't found in path, returns -1. + * + * If a problem happened when creating the child process, + * returns -1 and errp is set. + */ +static int run_process_child(const char *command[], Error **errp) { - Error *local_err =3D NULL; - const char *pmutils_arg; - const char *pmutils_bin =3D "pm-is-supported"; - char *pmutils_path; - pid_t pid; - int status; - bool ret =3D false; + int exit_status, spawn_flag; + GError *g_err =3D NULL; + bool success; =20 - switch (suspend_mode) { + spawn_flag =3D G_SPAWN_SEARCH_PATH | G_SPAWN_STDOUT_TO_DEV_NULL | + G_SPAWN_STDERR_TO_DEV_NULL; =20 - case SUSPEND_MODE_DISK: - pmutils_arg =3D "--hibernate"; - break; - case SUSPEND_MODE_RAM: - pmutils_arg =3D "--suspend"; - break; - case SUSPEND_MODE_HYBRID: - pmutils_arg =3D "--suspend-hybrid"; - break; - default: - return ret; + success =3D g_spawn_sync(NULL, (char **)command, environ, spawn_flag, + NULL, NULL, NULL, NULL, + &exit_status, &g_err); + + if (success) { + return WEXITSTATUS(exit_status); } =20 - pmutils_path =3D g_find_program_in_path(pmutils_bin); - if (!pmutils_path) { - return ret; + if (g_err && (g_err->code !=3D G_SPAWN_ERROR_NOENT)) { + error_setg(errp, "failed to create child process, error '%s'", + g_err->message); } =20 - pid =3D fork(); - if (!pid) { - setsid(); - execle(pmutils_path, pmutils_bin, pmutils_arg, NULL, environ); - /* - * If we get here execle() has failed. - */ - _exit(SUSPEND_NOT_SUPPORTED); - } else if (pid < 0) { - error_setg_errno(errp, errno, "failed to create child process"); - goto out; + g_error_free(g_err); + return -1; +} + +static bool pmutils_supports_mode(SuspendMode mode, Error **errp) +{ + Error *local_err =3D NULL; + const char *pmutils_args[3] =3D {"--hibernate", "--suspend", + "--suspend-hybrid"}; + const char *cmd[3] =3D {"pm-is-supported", pmutils_args[mode], NULL}; + int status; + + status =3D run_process_child(cmd, &local_err); + + if (status =3D=3D SUSPEND_SUPPORTED) { + return true; } =20 - ga_wait_child(pid, &status, &local_err); - if (local_err) { - error_propagate(errp, local_err); - goto out; + if ((status =3D=3D -1) && !local_err) { + return false; } =20 - switch (WEXITSTATUS(status)) { - case SUSPEND_SUPPORTED: - ret =3D true; - goto out; - case SUSPEND_NOT_SUPPORTED: - goto out; - default: + if (local_err) { + error_propagate(errp, local_err); + } else { error_setg(errp, - "the helper program '%s' returned an unexpected exit st= atus" - " code (%d)", pmutils_path, WEXITSTATUS(status)); - goto out; + "the helper program '%s' returned an unexpected exit" + " status code (%d)", "pm-is-supported", status); } =20 -out: - g_free(pmutils_path); - return ret; + return false; } =20 -static void pmutils_suspend(int suspend_mode, Error **errp) +static void pmutils_suspend(SuspendMode mode, Error **errp) { Error *local_err =3D NULL; - const char *pmutils_bin; - char *pmutils_path; - pid_t pid; + const char *pmutils_binaries[3] =3D {"pm-hibernate", "pm-suspend", + "pm-suspend-hybrid"}; + const char *cmd[2] =3D {pmutils_binaries[mode], NULL}; int status; =20 - 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; - } + status =3D run_process_child(cmd, &local_err); =20 - 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); + if (status =3D=3D 0) { return; } =20 - 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; + if ((status =3D=3D -1) && !local_err) { + error_setg(errp, "the helper program '%s' was not found", + pmutils_binaries[mode]); + return; } =20 - ga_wait_child(pid, &status, &local_err); if (local_err) { error_propagate(errp, local_err); - goto out; - } - - if (WEXITSTATUS(status)) { + } else { error_setg(errp, - "the helper program '%s' returned an unexpected exit st= atus" - " code (%d)", pmutils_path, WEXITSTATUS(status)); + "the helper program '%s' returned an unexpected exit" + " status code (%d)", pmutils_binaries[mode], status); } - -out: - g_free(pmutils_path); } =20 -static bool linux_sys_state_supports_mode(int suspend_mode, Error **errp) +static bool linux_sys_state_supports_mode(SuspendMode mode, Error **errp) { - const char *sysfile_str; + const char *sysfile_strs[3] =3D {"disk", "mem", NULL}; + const char *sysfile_str =3D sysfile_strs[mode]; char buf[32]; /* hopefully big enough */ int fd; ssize_t ret; =20 - switch (suspend_mode) { - - case SUSPEND_MODE_DISK: - sysfile_str =3D "disk"; - break; - case SUSPEND_MODE_RAM: - sysfile_str =3D "mem"; - break; - default: + if (!sysfile_str) { + error_setg(errp, "unknown guest suspend mode"); return false; } =20 @@ -1604,22 +1569,15 @@ static bool linux_sys_state_supports_mode(int suspe= nd_mode, Error **errp) return false; } =20 -static void linux_sys_state_suspend(int suspend_mode, Error **errp) +static void linux_sys_state_suspend(SuspendMode mode, Error **errp) { Error *local_err =3D NULL; - const char *sysfile_str; + const char *sysfile_strs[3] =3D {"disk", "mem", NULL}; + const char *sysfile_str =3D sysfile_strs[mode]; pid_t pid; int status; =20 - switch (suspend_mode) { - - case SUSPEND_MODE_DISK: - sysfile_str =3D "disk"; - break; - case SUSPEND_MODE_RAM: - sysfile_str =3D "mem"; - break; - default: + if (!sysfile_str) { error_setg(errp, "unknown guest suspend mode"); return; } @@ -1661,12 +1619,12 @@ static void linux_sys_state_suspend(int suspend_mod= e, Error **errp) =20 } =20 -static void bios_supports_mode(int suspend_mode, Error **errp) +static void bios_supports_mode(SuspendMode mode, Error **errp) { Error *local_err =3D NULL; bool ret; =20 - ret =3D pmutils_supports_mode(suspend_mode, &local_err); + ret =3D pmutils_supports_mode(mode, &local_err); if (ret) { return; } @@ -1674,32 +1632,31 @@ static void bios_supports_mode(int suspend_mode, Er= ror **errp) error_propagate(errp, local_err); return; } - ret =3D linux_sys_state_supports_mode(suspend_mode, errp); + ret =3D linux_sys_state_supports_mode(mode, &local_err); 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) +static void guest_suspend(SuspendMode mode, Error **errp) { Error *local_err =3D NULL; =20 - bios_supports_mode(suspend_mode, &local_err); + bios_supports_mode(mode, &local_err); if (local_err) { error_propagate(errp, local_err); return; } =20 - pmutils_suspend(suspend_mode, &local_err); + pmutils_suspend(mode, &local_err); if (!local_err) { return; } =20 error_free(local_err); =20 - linux_sys_state_suspend(suspend_mode, &local_err); + linux_sys_state_suspend(mode, &local_err); if (local_err) { error_propagate(errp, local_err); } --=20 2.17.1