From nobody Tue Nov 26 09:32:09 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=quarantine dis=none) header.from=virtuozzo.com ARC-Seal: i=1; a=rsa-sha256; t=1708967061; cv=none; d=zohomail.com; s=zohoarc; b=gbUNukhQP8QZ3y/FqT7RPsxVSJQVnSPgf9sbGqJGC5a3R+z1fXNkHi1QHsynZen7R9R0g95bhOS+52uGEbAYNuAUs+EPdWabPryfmNc0ASj25iqDTnWs2rjdMUV0DtnbJYU5H3wkCOJ2644Wzr+f/nq9mLS9joZNYx1dcnVdkUY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1708967061; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=kV26etjdgrTttBHqNO1xlpqvRXHOfMKajDrhdh/+Zqo=; b=eW+CWb2M1Fe7UJ3jUF5G1EaPwET1yKpq7CQr9Z1KK3Etk2+jL16fg96W9qWErBsplGs5HgQWGjNTdpoGscleLQ0QBLjj0WTliI7Rlc+vFvhIR0G/KSccNDcuged1nXlFgHqExLo5kBHRSuwrMMK0DExfYHzaSjlKJV871vEUNBE= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1708967061408762.3701434757031; Mon, 26 Feb 2024 09:04:21 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1reeHo-0005ZJ-6g; Mon, 26 Feb 2024 11:57:04 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1reeHl-0005XN-7C for qemu-devel@nongnu.org; Mon, 26 Feb 2024 11:57:01 -0500 Received: from relay.virtuozzo.com ([130.117.225.111]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1reeHX-0007aO-4T for qemu-devel@nongnu.org; Mon, 26 Feb 2024 11:56:53 -0500 Received: from [130.117.225.1] (helo=dev005.ch-qa.vzint.dev) by relay.virtuozzo.com with esmtp (Exim 4.96) (envelope-from ) id 1reeG3-001AD7-02; Mon, 26 Feb 2024 17:56:34 +0100 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=virtuozzo.com; s=relay; h=MIME-Version:Message-Id:Date:Subject:From: Content-Type; bh=kV26etjdgrTttBHqNO1xlpqvRXHOfMKajDrhdh/+Zqo=; b=ijy1WUQl4gy5 15u8ifI9ORKKzhR+7noCc41HWvc7WQUipIoFtoNkaDQp+ck3KFKT8Y/UayQkaluF+CEi07yvtnrLG upOA3l81RBtlj7/5vjjvi/s+rGUj53ZOTco4EQI3Dhl1kGup0mnlWev94HtwdK7W4+zTu6QDGNzZD fkbf/vNu+axs+c/csFxD8ZcElFHR8j7iC+XSUfYTqdFJksPixOsIZQT3gUpBq+q3ZQyM3U1yb8oEz JKgTk2VmuwpHPZ0PQiHZecL30T0OoPB5nw2m62mOVCmXVk7QX4Yb4E7DrivpYa9OuNy89RhPZus9M dMZXwiO7YalqOFj+i9DyBg==; From: Andrey Drobyshev To: qemu-devel@nongnu.org Cc: michael.roth@amd.com, kkostiuk@redhat.com, marcandre.lureau@redhat.com, andrey.drobyshev@virtuozzo.com, den@virtuozzo.com Subject: [PATCH 1/7] qga/commands-posix: return fsinfo values directly as reported by statvfs Date: Mon, 26 Feb 2024 18:56:36 +0200 Message-Id: <20240226165642.807350-2-andrey.drobyshev@virtuozzo.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20240226165642.807350-1-andrey.drobyshev@virtuozzo.com> References: <20240226165642.807350-1-andrey.drobyshev@virtuozzo.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=130.117.225.111; envelope-from=andrey.drobyshev@virtuozzo.com; helo=relay.virtuozzo.com X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @virtuozzo.com) X-ZM-MESSAGEID: 1708967062938100003 Content-Type: text/plain; charset="utf-8" Since the commit 25b5ff1a86 ("qga: add mountpoint usage info to GuestFilesystemInfo") we have 2 values reported in guest-get-fsinfo: used =3D (f_blocks - f_bfree), total =3D (f_blocks - f_bfree + f_bavail). These calculations might be obscure for the end user and require one to actually get into QGA source to understand how they're obtained. Let's just report the values f_blocks, f_bfree, f_bavail (in bytes) from statvfs() as they are, letting the user decide how to process them further. Originally-by: Yuri Pudgorodskiy Signed-off-by: Andrey Drobyshev --- qga/commands-posix.c | 16 +++++++--------- qga/qapi-schema.json | 11 +++++++---- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/qga/commands-posix.c b/qga/commands-posix.c index 26008db497..752ef509d0 100644 --- a/qga/commands-posix.c +++ b/qga/commands-posix.c @@ -1554,8 +1554,7 @@ static GuestFilesystemInfo *build_guest_fsinfo(struct= FsMount *mount, Error **errp) { GuestFilesystemInfo *fs =3D g_malloc0(sizeof(*fs)); - struct statvfs buf; - unsigned long used, nonroot_total, fr_size; + struct statvfs st; char *devpath =3D g_strdup_printf("/sys/dev/block/%u:%u", mount->devmajor, mount->devminor); =20 @@ -1563,15 +1562,14 @@ static GuestFilesystemInfo *build_guest_fsinfo(stru= ct FsMount *mount, fs->type =3D g_strdup(mount->devtype); build_guest_fsinfo_for_device(devpath, fs, errp); =20 - if (statvfs(fs->mountpoint, &buf) =3D=3D 0) { - fr_size =3D buf.f_frsize; - used =3D buf.f_blocks - buf.f_bfree; - nonroot_total =3D used + buf.f_bavail; - fs->used_bytes =3D used * fr_size; - fs->total_bytes =3D nonroot_total * fr_size; + if (statvfs(fs->mountpoint, &st) =3D=3D 0) { + fs->total_bytes =3D st.f_blocks * st.f_frsize; + fs->free_bytes =3D st.f_bfree * st.f_frsize; + fs->avail_bytes =3D st.f_bavail * st.f_frsize; =20 fs->has_total_bytes =3D true; - fs->has_used_bytes =3D true; + fs->has_free_bytes =3D true; + fs->has_avail_bytes =3D true; } =20 g_free(devpath); diff --git a/qga/qapi-schema.json b/qga/qapi-schema.json index b8efe31897..1cce3c1df5 100644 --- a/qga/qapi-schema.json +++ b/qga/qapi-schema.json @@ -1030,9 +1030,12 @@ # # @type: file system type string # -# @used-bytes: file system used bytes (since 3.0) +# @total-bytes: total file system size in bytes (since 8.3) # -# @total-bytes: non-root file system total bytes (since 3.0) +# @free-bytes: amount of free space in file system in bytes (since 8.3) +# +# @avail-bytes: amount of free space in file system for unprivileged +# users in bytes (since 8.3) # # @disk: an array of disk hardware information that the volume lies # on, which may be empty if the disk type is not supported @@ -1041,8 +1044,8 @@ ## { 'struct': 'GuestFilesystemInfo', 'data': {'name': 'str', 'mountpoint': 'str', 'type': 'str', - '*used-bytes': 'uint64', '*total-bytes': 'uint64', - 'disk': ['GuestDiskAddress']} } + '*total-bytes': 'uint64', '*free-bytes': 'uint64', + '*avail-bytes': 'uint64', 'disk': ['GuestDiskAddress']} } =20 ## # @guest-get-fsinfo: --=20 2.39.3 From nobody Tue Nov 26 09:32:09 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=quarantine dis=none) header.from=virtuozzo.com ARC-Seal: i=1; a=rsa-sha256; t=1708966759; cv=none; d=zohomail.com; s=zohoarc; b=V+I9aWvosoPasRQRl791wzihVy62P/cwxtpSjDfmTyWtB2FvswcxFalacEz4iZ9+yWQV+y1jnQrbcaiHVBBBpXX5cOwxozRNBghXhG8Ihz1i47Y9wjLU/zU6mx4TM5dSMVlygJPPbxY0bFTJEw5QMLqOGaGC5LSH2XuRcXv1RiI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1708966759; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=fW7rtp0/UXuP9tIVplL8g9pjh+RyEaa4l4hEMmcNHFA=; b=dnlG2edlxZ3to74SzJkHueOL7bbrYTKLBfRWIciDBN+ys4K9gnzV4siF2R+OYhEzstRJRhDcN2pA2zPPJruAjDVC31P6KmX8O/HTeTzFo2KNaGbFpUSXlg4aCo8gfR/GfVfoP6baCk5PjtVqs/pppfxD7INOEj8T73GaTbdI2W8= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1708966759731786.0977014186124; Mon, 26 Feb 2024 08:59:19 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1reeHp-0005Zk-7U; Mon, 26 Feb 2024 11:57:05 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1reeHl-0005XP-84 for qemu-devel@nongnu.org; Mon, 26 Feb 2024 11:57:01 -0500 Received: from relay.virtuozzo.com ([130.117.225.111]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1reeHX-0007aK-4k for qemu-devel@nongnu.org; Mon, 26 Feb 2024 11:57:00 -0500 Received: from [130.117.225.1] (helo=dev005.ch-qa.vzint.dev) by relay.virtuozzo.com with esmtp (Exim 4.96) (envelope-from ) id 1reeG3-001AD7-0C; Mon, 26 Feb 2024 17:56:34 +0100 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=virtuozzo.com; s=relay; h=MIME-Version:Message-Id:Date:Subject:From: Content-Type; bh=fW7rtp0/UXuP9tIVplL8g9pjh+RyEaa4l4hEMmcNHFA=; b=nkY4k8MS0xF8 r4MB6ZHqkaB9xTq8bPvnwlDWesqsfW2ddsyVyZ3aQ4DyQuzL+MGj0qyIsQD9FytrKx+aeYqCEmXoz 6DMwxBwzdAapfcJbnBo/Ct7od0MTyBn/mHirqg/bzrvXEarfT4u4RiUxEV1J8c3qRroACuDM4+E0y eqdJpvipc4dQZDWJUahje0Z07HFIsaMB6MQZWB3NQTdkZw21fyhp547R9gz+U+OKAfHPLpyOhfZ88 PIKNjoG5RITg5IHVFnfqpaEkjQuErHv9k7VE4iSaULjVtXMVhsoBZijpxp2w62WZFROVbkWrGDTkJ HCSRwIOzK+HzXaFZeOcsUw==; From: Andrey Drobyshev To: qemu-devel@nongnu.org Cc: michael.roth@amd.com, kkostiuk@redhat.com, marcandre.lureau@redhat.com, andrey.drobyshev@virtuozzo.com, den@virtuozzo.com Subject: [PATCH 2/7] qga: introduce ga_run_command() helper for guest cmd execution Date: Mon, 26 Feb 2024 18:56:37 +0200 Message-Id: <20240226165642.807350-3-andrey.drobyshev@virtuozzo.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20240226165642.807350-1-andrey.drobyshev@virtuozzo.com> References: <20240226165642.807350-1-andrey.drobyshev@virtuozzo.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=130.117.225.111; envelope-from=andrey.drobyshev@virtuozzo.com; helo=relay.virtuozzo.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @virtuozzo.com) X-ZM-MESSAGEID: 1708966761454100003 Content-Type: text/plain; charset="utf-8" When executing guest commands in *nix environment, we repeat the same fork/exec pattern multiple times. Let's just separate it into a single helper which would also be able to feed input data into the launched process' stdin. This way we can avoid code duplication. To keep the history more bisectable, let's replace qmp commands implementations one by one. Also add G_GNUC_UNUSED attribute to the helper and remove it in the next commit. Originally-by: Yuri Pudgorodskiy Signed-off-by: Andrey Drobyshev --- qga/commands-posix.c | 140 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 140 insertions(+) diff --git a/qga/commands-posix.c b/qga/commands-posix.c index 752ef509d0..510b902b1a 100644 --- a/qga/commands-posix.c +++ b/qga/commands-posix.c @@ -76,6 +76,146 @@ static void ga_wait_child(pid_t pid, int *status, Error= **errp) g_assert(rpid =3D=3D pid); } =20 +static void ga_pipe_read_str(int fd[2], char **str, size_t *len) +{ + ssize_t n; + char buf[1024]; + close(fd[1]); + fd[1] =3D -1; + while ((n =3D read(fd[0], buf, sizeof(buf))) !=3D 0) { + if (n < 0) { + if (errno =3D=3D EINTR) { + continue; + } else { + break; + } + } + *str =3D g_realloc(*str, *len + n); + memcpy(*str + *len, buf, n); + *len +=3D n; + } + close(fd[0]); + fd[0] =3D -1; +} + +/* + * Helper to run command with input/output redirection, + * sending string to stdin and taking error message from + * stdout/err. + */ +G_GNUC_UNUSED +static int ga_run_command(const char *argv[], const char *in_str, + const char *action, Error **errp) +{ + pid_t pid; + int status; + int retcode =3D -1; + int infd[2] =3D { -1, -1 }; + int outfd[2] =3D { -1, -1 }; + char *str =3D NULL; + size_t len =3D 0; + + if ((in_str && !g_unix_open_pipe(infd, FD_CLOEXEC, NULL)) || + !g_unix_open_pipe(outfd, FD_CLOEXEC, NULL)) { + error_setg(errp, "cannot create pipe FDs"); + goto out; + } + + pid =3D fork(); + if (pid =3D=3D 0) { + char *cherr =3D NULL; + + setsid(); + + if (in_str) { + /* Redirect stdin to infd. */ + close(infd[1]); + dup2(infd[0], 0); + close(infd[0]); + } else { + reopen_fd_to_null(0); + } + + /* Redirect stdout/stderr to outfd. */ + close(outfd[0]); + dup2(outfd[1], 1); + dup2(outfd[1], 2); + close(outfd[1]); + + execvp(argv[0], (char *const *)argv); + + /* Write the cause of failed exec to pipe for the parent to read i= t. */ + cherr =3D g_strdup_printf("failed to exec '%s'", argv[0]); + perror(cherr); + g_free(cherr); + _exit(EXIT_FAILURE); + } else if (pid < 0) { + error_setg_errno(errp, errno, "failed to create child process"); + goto out; + } + + if (in_str) { + close(infd[0]); + infd[0] =3D -1; + if (qemu_write_full(infd[1], in_str, strlen(in_str)) !=3D + strlen(in_str)) { + error_setg_errno(errp, errno, "%s: cannot write to stdin pipe", + action); + goto out; + } + close(infd[1]); + infd[1] =3D -1; + } + + ga_pipe_read_str(outfd, &str, &len); + + ga_wait_child(pid, &status, errp); + if (*errp) { + goto out; + } + + if (!WIFEXITED(status)) { + if (len) { + error_setg(errp, "child process has terminated abnormally: %s", + str); + } else { + error_setg(errp, "child process has terminated abnormally"); + } + goto out; + } + + retcode =3D WEXITSTATUS(status); + + if (WEXITSTATUS(status)) { + if (len) { + error_setg(errp, "child process has failed to %s: %s", + action, str); + } else { + error_setg(errp, "child process has failed to %s: exit status = %d", + action, WEXITSTATUS(status)); + } + goto out; + } + +out: + g_free(str); + + if (infd[0] !=3D -1) { + close(infd[0]); + } + if (infd[1] !=3D -1) { + close(infd[1]); + } + if (outfd[0] !=3D -1) { + close(outfd[0]); + } + if (outfd[1] !=3D -1) { + close(outfd[1]); + } + + return retcode; +} + void qmp_guest_shutdown(const char *mode, Error **errp) { const char *shutdown_flag; --=20 2.39.3 From nobody Tue Nov 26 09:32:09 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=quarantine dis=none) header.from=virtuozzo.com ARC-Seal: i=1; a=rsa-sha256; t=1708966963; cv=none; d=zohomail.com; s=zohoarc; b=HOXGDuZpvHohi9W5MaWSsLlQOOEPqssrPTtq7bNs6o9p0aR7omU0DZfequdYaHuGTMBJD7Q4aGO2N+bzk+XPur7UHvq8eW3yWxt3KwxqtQhIgBjr4xEcfauEVejmGeqwhlolJ1SzAmOfN3e/IijAgmOB5AcCqXdCKJ6Uih2i/f8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1708966963; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=GQCJppOxjtPPvX5ALSUce3CAX9D6q9BlO733MTw3eZ8=; b=ZkJCj3GHERcJyLUI+U1412aHNTli6zRhCn/b5aTb69KIDZpRB8C88DON3TW//hwEGtVszw2ZTH/CdB6+1X7AMaFMlncHpqUkXqMTFTDlt5L/dPsY/TrK8YQdccDgDcvPvFtj50shX/y8bP/9Hq7vMH8n+biEGvcxUW0owMtjyyY= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1708966963809147.86873759852404; Mon, 26 Feb 2024 09:02:43 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1reeHo-0005ZL-9h; Mon, 26 Feb 2024 11:57:04 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1reeHl-0005XL-3j for qemu-devel@nongnu.org; Mon, 26 Feb 2024 11:57:01 -0500 Received: from relay.virtuozzo.com ([130.117.225.111]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1reeHX-0007aQ-6I for qemu-devel@nongnu.org; Mon, 26 Feb 2024 11:56:53 -0500 Received: from [130.117.225.1] (helo=dev005.ch-qa.vzint.dev) by relay.virtuozzo.com with esmtp (Exim 4.96) (envelope-from ) id 1reeG3-001AD7-0N; Mon, 26 Feb 2024 17:56:34 +0100 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=virtuozzo.com; s=relay; h=MIME-Version:Message-Id:Date:Subject:From: Content-Type; bh=GQCJppOxjtPPvX5ALSUce3CAX9D6q9BlO733MTw3eZ8=; b=EhZFv336uK5p hbJeDqDNsKsfEfsqz1u0vKebwC1u7R1Rl0f793xxH79+aaA83rca/TTueSxd71Se66STdSZ48NBhc dChNCdLnGexCJjboIO8RPnPN1JoQY4ywwNfglCwWu1ab1X7UrZMZgnKxna9sVusAFzazduvKoXKIK cXrd/eyw8dLPtbxVOJdftkw/eg7eKFdk5A9S86S2yihQz0TauVKly1YM1fqxNj+0Z+NbxyawgSwEY hg8tqw01m3XJgZUioQfeltopM+geOowCKMuhQBKOBf1Riv2z2379O65lNq5k1/4GXC0VKmBokmVnc lVGLo6OlU5bt5ojv+p6CDg==; From: Andrey Drobyshev To: qemu-devel@nongnu.org Cc: michael.roth@amd.com, kkostiuk@redhat.com, marcandre.lureau@redhat.com, andrey.drobyshev@virtuozzo.com, den@virtuozzo.com Subject: [PATCH 3/7] qga/commands-posix: qmp_guest_shutdown: use ga_run_command helper Date: Mon, 26 Feb 2024 18:56:38 +0200 Message-Id: <20240226165642.807350-4-andrey.drobyshev@virtuozzo.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20240226165642.807350-1-andrey.drobyshev@virtuozzo.com> References: <20240226165642.807350-1-andrey.drobyshev@virtuozzo.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=130.117.225.111; envelope-from=andrey.drobyshev@virtuozzo.com; helo=relay.virtuozzo.com X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @virtuozzo.com) X-ZM-MESSAGEID: 1708966964477100001 Content-Type: text/plain; charset="utf-8" Also remove the G_GNUC_UNUSED attribute added in the previous commit from the helper. Signed-off-by: Andrey Drobyshev --- qga/commands-posix.c | 39 ++++++--------------------------------- 1 file changed, 6 insertions(+), 33 deletions(-) diff --git a/qga/commands-posix.c b/qga/commands-posix.c index 510b902b1a..4b64716bab 100644 --- a/qga/commands-posix.c +++ b/qga/commands-posix.c @@ -103,7 +103,6 @@ static void ga_pipe_read_str(int fd[2], char **str, siz= e_t *len) * sending string to stdin and taking error message from * stdout/err. */ -G_GNUC_UNUSED static int ga_run_command(const char *argv[], const char *in_str, const char *action, Error **errp) { @@ -220,8 +219,6 @@ void qmp_guest_shutdown(const char *mode, Error **errp) { const char *shutdown_flag; Error *local_err =3D NULL; - pid_t pid; - int status; =20 #ifdef CONFIG_SOLARIS const char *powerdown_flag =3D "-i5"; @@ -250,46 +247,22 @@ void qmp_guest_shutdown(const char *mode, Error **err= p) return; } =20 - pid =3D fork(); - if (pid =3D=3D 0) { - /* child, start the shutdown */ - setsid(); - reopen_fd_to_null(0); - reopen_fd_to_null(1); - reopen_fd_to_null(2); - + const char *argv[] =3D {"/sbin/shutdown", #ifdef CONFIG_SOLARIS - execl("/sbin/shutdown", "shutdown", shutdown_flag, "-g0", "-y", - "hypervisor initiated shutdown", (char *)NULL); + shutdown_flag, "-g0", "-y", #elif defined(CONFIG_BSD) - execl("/sbin/shutdown", "shutdown", shutdown_flag, "+0", - "hypervisor initiated shutdown", (char *)NULL); + shutdown_flag, "+0", #else - execl("/sbin/shutdown", "shutdown", "-h", shutdown_flag, "+0", - "hypervisor initiated shutdown", (char *)NULL); + "-h", shutdown_flag, "+0", #endif - _exit(EXIT_FAILURE); - } else if (pid < 0) { - error_setg_errno(errp, errno, "failed to create child process"); - return; - } + "hypervisor initiated shutdown", (char *) NULL}; =20 - ga_wait_child(pid, &status, &local_err); + ga_run_command(argv, NULL, "shutdown", &local_err); if (local_err) { error_propagate(errp, local_err); return; } =20 - if (!WIFEXITED(status)) { - error_setg(errp, "child process has terminated abnormally"); - return; - } - - if (WEXITSTATUS(status)) { - error_setg(errp, "child process has failed to shutdown"); - return; - } - /* succeeded */ } =20 --=20 2.39.3 From nobody Tue Nov 26 09:32:09 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=quarantine dis=none) header.from=virtuozzo.com ARC-Seal: i=1; a=rsa-sha256; t=1708966698; cv=none; d=zohomail.com; s=zohoarc; b=hj5WDNFUBxAwcFGEAkeKN0UylhjKgT10GHgYPpANYH3LjOHGdbDnzSgFDoM6gzWzcfAikZb21Oi5vfEsQ7LnuJ12/KF1uyx4GXcdUV2tQByiUhLzqcLohjqIuQ9TaYqHl+EFNdI5GODnvz0QA73vkIP1GnpLZkIq44Ci3rT9+CQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1708966698; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=htlckGLDs2sIL4sjOt+I4d40bYlCVHcQtX3nL55eumk=; b=OPOQbR44ntcbpMBX34OvaPAkCJqs0t7ocKPAopDgBVZ0j15NfLWnWNVKi3uICuIwY03nv5wROOEyErHWFepRrUMlUPyp4Wgoi2uAUIrNgoXbVZDHkIqagd9c9ZfBUq1fyUYVSkGMj6cvBMU6ysrsKvoODn576UJNq9qbsMUuTGg= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 170896669851312.060168906251533; Mon, 26 Feb 2024 08:58:18 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1reeHp-0005Zj-9E; Mon, 26 Feb 2024 11:57:05 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1reeHl-0005Xd-Fc for qemu-devel@nongnu.org; Mon, 26 Feb 2024 11:57:01 -0500 Received: from relay.virtuozzo.com ([130.117.225.111]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1reeHX-0007aP-8M for qemu-devel@nongnu.org; Mon, 26 Feb 2024 11:57:00 -0500 Received: from [130.117.225.1] (helo=dev005.ch-qa.vzint.dev) by relay.virtuozzo.com with esmtp (Exim 4.96) (envelope-from ) id 1reeG3-001AD7-0Y; Mon, 26 Feb 2024 17:56:34 +0100 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=virtuozzo.com; s=relay; h=MIME-Version:Message-Id:Date:Subject:From: Content-Type; bh=htlckGLDs2sIL4sjOt+I4d40bYlCVHcQtX3nL55eumk=; b=o8o/bRZKeUkd UPP22Nxu20IfrlQVJ50OQTNVDcXCeeZiJRYbjAhIcgckHR+MskVOG1eKSwwtkDaRWasubGIqUeHMp itxomRDQ3GObj6gqFst1t8KIAxpVt4whknVn47OZmT8YrFD+X1C/3XHi82S8n9MIiF5MXW4YHXboA rwnUuRM/GwspNF/wiC1JBZZ622Q29J4PJsMGyl6IS+egvdsrtliypx5t/YTpQWzxnPJ+d0EHJTqOp lykEWYF/WioM1nYVdtD4QC2CDTo9nRkaGxwkgYdSQ9XImSB+VLmhhZswrpU4kWhm0TxTebWxo9J1U iqnAu3tiKk/awqZ3CMZ3jQ==; From: Andrey Drobyshev To: qemu-devel@nongnu.org Cc: michael.roth@amd.com, kkostiuk@redhat.com, marcandre.lureau@redhat.com, andrey.drobyshev@virtuozzo.com, den@virtuozzo.com Subject: [PATCH 4/7] qga/commands-posix: qmp_guest_set_time: use ga_run_command helper Date: Mon, 26 Feb 2024 18:56:39 +0200 Message-Id: <20240226165642.807350-5-andrey.drobyshev@virtuozzo.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20240226165642.807350-1-andrey.drobyshev@virtuozzo.com> References: <20240226165642.807350-1-andrey.drobyshev@virtuozzo.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=130.117.225.111; envelope-from=andrey.drobyshev@virtuozzo.com; helo=relay.virtuozzo.com X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @virtuozzo.com) X-ZM-MESSAGEID: 1708966699210100003 Content-Type: text/plain; charset="utf-8" There's no need to check for the existence of "/sbin/hwclock", the exec() call will do that for us. Signed-off-by: Andrey Drobyshev --- qga/commands-posix.c | 43 +++---------------------------------------- 1 file changed, 3 insertions(+), 40 deletions(-) diff --git a/qga/commands-posix.c b/qga/commands-posix.c index 4b64716bab..b704006c98 100644 --- a/qga/commands-posix.c +++ b/qga/commands-posix.c @@ -269,21 +269,9 @@ void qmp_guest_shutdown(const char *mode, Error **errp) void qmp_guest_set_time(bool has_time, int64_t time_ns, Error **errp) { int ret; - int status; - pid_t pid; Error *local_err =3D NULL; struct timeval tv; - static const char hwclock_path[] =3D "/sbin/hwclock"; - static int hwclock_available =3D -1; - - if (hwclock_available < 0) { - hwclock_available =3D (access(hwclock_path, X_OK) =3D=3D 0); - } - - if (!hwclock_available) { - error_setg(errp, QERR_UNSUPPORTED); - return; - } + const char *argv[] =3D {"/sbin/hwclock", has_time ? "-w" : "-s", NULL}; =20 /* If user has passed a time, validate and set it. */ if (has_time) { @@ -314,37 +302,12 @@ void qmp_guest_set_time(bool has_time, int64_t time_n= s, Error **errp) * just need to synchronize the hardware clock. However, if no time was * passed, user is requesting the opposite: set the system time from t= he * hardware clock (RTC). */ - pid =3D fork(); - if (pid =3D=3D 0) { - setsid(); - reopen_fd_to_null(0); - reopen_fd_to_null(1); - reopen_fd_to_null(2); - - /* Use '/sbin/hwclock -w' to set RTC from the system time, - * or '/sbin/hwclock -s' to set the system time from RTC. */ - execl(hwclock_path, "hwclock", has_time ? "-w" : "-s", NULL); - _exit(EXIT_FAILURE); - } else if (pid < 0) { - error_setg_errno(errp, errno, "failed to create child process"); - return; - } - - ga_wait_child(pid, &status, &local_err); + ga_run_command(argv, NULL, "set hardware clock to system time", + &local_err); if (local_err) { error_propagate(errp, local_err); return; } - - if (!WIFEXITED(status)) { - error_setg(errp, "child process has terminated abnormally"); - return; - } - - if (WEXITSTATUS(status)) { - error_setg(errp, "hwclock failed to set hardware clock to system t= ime"); - return; - } } =20 typedef enum { --=20 2.39.3 From nobody Tue Nov 26 09:32:09 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=quarantine dis=none) header.from=virtuozzo.com ARC-Seal: i=1; a=rsa-sha256; t=1708966675; cv=none; d=zohomail.com; s=zohoarc; b=JpYhlxaLXizNYKXVCPqlRNavY2cBBEO9vEVSGuM4obVcj+PuJ+2lcRU60ZZ4PZGVnb9E0uzqdDQmxuLU2jezERdZjpS1xHACU5kWCFtemgZO5RfIeq9v9OWBTqoxHaoNrcSZNCEcZsGo/5NPEHKXZD1ETuRAA4YWYrnEXUGQIlM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1708966675; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=kp8z+p1ZvDrme73dRSAIidGhStbQaeG0VLfyNPijbvw=; b=aUy26HvffFd/XVaZLu4sn9e9/w/Et1Bp7KBjJyQTIdUzKnIaKChmnEyojMjkQRIbla3wEcaqVZ1A21ZFGcB0bpgav4Zw3W9xahJ6nIAnWijJ45U4ArFkSPXBbYMX364At+m2y0twaUy4ZTlqyMyj0M+aFt4LEyoTO5xBX2Y6M+A= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1708966675091916.7017295643918; Mon, 26 Feb 2024 08:57:55 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1reeHm-0005YE-Bc; Mon, 26 Feb 2024 11:57:02 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1reeHd-0005W7-96 for qemu-devel@nongnu.org; Mon, 26 Feb 2024 11:56:54 -0500 Received: from relay.virtuozzo.com ([130.117.225.111]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1reeHX-0007aM-41 for qemu-devel@nongnu.org; Mon, 26 Feb 2024 11:56:53 -0500 Received: from [130.117.225.1] (helo=dev005.ch-qa.vzint.dev) by relay.virtuozzo.com with esmtp (Exim 4.96) (envelope-from ) id 1reeG3-001AD7-0j; Mon, 26 Feb 2024 17:56:34 +0100 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=virtuozzo.com; s=relay; h=MIME-Version:Message-Id:Date:Subject:From: Content-Type; bh=kp8z+p1ZvDrme73dRSAIidGhStbQaeG0VLfyNPijbvw=; b=pilkOaV72yBY LUM1hdb/9jw54xNLi7M9lMvWtJU+UfnvmJ4lePBUOl8f9kOm+VlgXLhDIpjrtL8XYop0QegLmf+td kaHthwzNsNFKFvxXE3myYHTVziNX869bAbDfLPPKqH2sNikNA0YtDVLSPMfLceqIGtxHlqs3MmAwx dQFpRbj7d31fXVEQh9AHWoNKE99Z0CAoq6OIq1DiFgUnoYcuam+nA86VEDTnOQoqJfoFnDZoW8Cti /XXFW/c5WAYjd92njTlaLKXEVlK4gJe9DSRzmkOC1cz+aeJYeAzmZeazZsicKofI0KF4TMvU7XVAM J3NusOtARA20lk/jlOr+UA==; From: Andrey Drobyshev To: qemu-devel@nongnu.org Cc: michael.roth@amd.com, kkostiuk@redhat.com, marcandre.lureau@redhat.com, andrey.drobyshev@virtuozzo.com, den@virtuozzo.com Subject: [PATCH 5/7] qga/commands-posix: execute_fsfreeze_hook: use ga_run_command helper Date: Mon, 26 Feb 2024 18:56:40 +0200 Message-Id: <20240226165642.807350-6-andrey.drobyshev@virtuozzo.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20240226165642.807350-1-andrey.drobyshev@virtuozzo.com> References: <20240226165642.807350-1-andrey.drobyshev@virtuozzo.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=130.117.225.111; envelope-from=andrey.drobyshev@virtuozzo.com; helo=relay.virtuozzo.com X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @virtuozzo.com) X-ZM-MESSAGEID: 1708966677181100003 Content-Type: text/plain; charset="utf-8" There's no need to check for the existence of the hook executable, as the exec() call will do that for us. Signed-off-by: Andrey Drobyshev --- qga/commands-posix.c | 35 +++-------------------------------- 1 file changed, 3 insertions(+), 32 deletions(-) diff --git a/qga/commands-posix.c b/qga/commands-posix.c index b704006c98..6baaabc29e 100644 --- a/qga/commands-posix.c +++ b/qga/commands-posix.c @@ -726,8 +726,6 @@ static const char *fsfreeze_hook_arg_string[] =3D { =20 static void execute_fsfreeze_hook(FsfreezeHookArg arg, Error **errp) { - int status; - pid_t pid; const char *hook; const char *arg_str =3D fsfreeze_hook_arg_string[arg]; Error *local_err =3D NULL; @@ -736,42 +734,15 @@ static void execute_fsfreeze_hook(FsfreezeHookArg arg= , Error **errp) if (!hook) { return; } - if (access(hook, X_OK) !=3D 0) { - error_setg_errno(errp, errno, "can't access fsfreeze hook '%s'", h= ook); - return; - } =20 - slog("executing fsfreeze hook with arg '%s'", arg_str); - pid =3D fork(); - if (pid =3D=3D 0) { - setsid(); - reopen_fd_to_null(0); - reopen_fd_to_null(1); - reopen_fd_to_null(2); - - execl(hook, hook, arg_str, NULL); - _exit(EXIT_FAILURE); - } else if (pid < 0) { - error_setg_errno(errp, errno, "failed to create child process"); - return; - } + const char *argv[] =3D {hook, arg_str, NULL}; =20 - ga_wait_child(pid, &status, &local_err); + slog("executing fsfreeze hook with arg '%s'", arg_str); + ga_run_command(argv, NULL, "execute fsfreeze hook", &local_err); if (local_err) { error_propagate(errp, local_err); return; } - - if (!WIFEXITED(status)) { - error_setg(errp, "fsfreeze hook has terminated abnormally"); - return; - } - - status =3D WEXITSTATUS(status); - if (status) { - error_setg(errp, "fsfreeze hook has failed with status %d", status= ); - return; - } } =20 /* --=20 2.39.3 From nobody Tue Nov 26 09:32:09 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=quarantine dis=none) header.from=virtuozzo.com ARC-Seal: i=1; a=rsa-sha256; t=1708967159; cv=none; d=zohomail.com; s=zohoarc; b=DxrFINaMY48B7wCaMv5zdIYh/SPeQbzXE0b8ukYwmLfntwtDwi8fQcuuekypqihv5siZRapNaEx770JSLnWPYB6IbLQHx6L8VmG1Bsp0ROs59GD1+yqPwuNsn0rRxBhjn4BbmbUlKYfuLVRRx6aaN3nYh3YnWzvTthR2aelEjoc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1708967159; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=r5CvpnosciMvnPjJjGTldJRMDdV43tlw4uH7/lKch6E=; b=JgQ0PfwOdipEy59tITHz1c0hTiP+pZQvLwSI8JdnrQgsvYqoL1xVCAJKCc7kB4jEdFreDfEZhyAcNMJuLpVLGwW5NcAOQK3oux/HHUmFORXYTbH49fMDOvpu9xPRji7cr3PNUlAYYzfe1VL4OUu+rr1zAP3O5i554fS+j1TCMvk= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1708967159650464.45515888557475; Mon, 26 Feb 2024 09:05:59 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1reeI0-0005ec-1S; Mon, 26 Feb 2024 11:57:16 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1reeHl-0005XZ-Ck for qemu-devel@nongnu.org; Mon, 26 Feb 2024 11:57:01 -0500 Received: from relay.virtuozzo.com ([130.117.225.111]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1reeHX-0007aL-LW for qemu-devel@nongnu.org; Mon, 26 Feb 2024 11:57:00 -0500 Received: from [130.117.225.1] (helo=dev005.ch-qa.vzint.dev) by relay.virtuozzo.com with esmtp (Exim 4.96) (envelope-from ) id 1reeG3-001AD7-0t; Mon, 26 Feb 2024 17:56:34 +0100 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=virtuozzo.com; s=relay; h=MIME-Version:Message-Id:Date:Subject:From: Content-Type; bh=r5CvpnosciMvnPjJjGTldJRMDdV43tlw4uH7/lKch6E=; b=dtlyLU6FeP4B qx8qFI0H+B8T5/zLRuIcSt0qlRAoJwlJYYoBxHu3JBoY0X3B1Mccm8RDfKO7mp7gNwcX4h1NmrxB6 gf03183uaQDFbd5cPqhoKHyRErVtaEq0AGe1ztQhnp6Nti1YF/S/bj7BovzdUn+ffQ+3E18a4MXZw L8RrgwL2XF/gzRvXRMUPQZQ0vzH0llTL0sbu9v9JhbUtHRtuuTlml+lyLzrs51er908dSDCsDVghd S6MkXce1pa+0CewqIw5ZCMPgjkHkmhXFCU68mzr0LqNOLInvQZq0NJY3uDMtQpf45GnAa8L0mz2ha jxVEo8b6tRc1vZWSsBUkxA==; From: Andrey Drobyshev To: qemu-devel@nongnu.org Cc: michael.roth@amd.com, kkostiuk@redhat.com, marcandre.lureau@redhat.com, andrey.drobyshev@virtuozzo.com, den@virtuozzo.com Subject: [PATCH 6/7] qga/commands-posix: use ga_run_command helper when suspending via sysfs Date: Mon, 26 Feb 2024 18:56:41 +0200 Message-Id: <20240226165642.807350-7-andrey.drobyshev@virtuozzo.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20240226165642.807350-1-andrey.drobyshev@virtuozzo.com> References: <20240226165642.807350-1-andrey.drobyshev@virtuozzo.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=130.117.225.111; envelope-from=andrey.drobyshev@virtuozzo.com; helo=relay.virtuozzo.com X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @virtuozzo.com) X-ZM-MESSAGEID: 1708967161392100003 Content-Type: text/plain; charset="utf-8" We replace the direct call to open() with a "sh -c 'echo ...'" call, so that it becomes an executable command. Signed-off-by: Andrey Drobyshev --- qga/commands-posix.c | 36 ++++-------------------------------- 1 file changed, 4 insertions(+), 32 deletions(-) diff --git a/qga/commands-posix.c b/qga/commands-posix.c index 6baaabc29e..599f59a323 100644 --- a/qga/commands-posix.c +++ b/qga/commands-posix.c @@ -1917,49 +1917,21 @@ static void linux_sys_state_suspend(SuspendMode mod= e, Error **errp) Error *local_err =3D NULL; const char *sysfile_strs[3] =3D {"disk", "mem", NULL}; const char *sysfile_str =3D sysfile_strs[mode]; - pid_t pid; - int status; =20 if (!sysfile_str) { error_setg(errp, "unknown guest suspend mode"); return; } =20 - pid =3D fork(); - if (!pid) { - /* child */ - int fd; - - setsid(); - reopen_fd_to_null(0); - reopen_fd_to_null(1); - reopen_fd_to_null(2); - - fd =3D open(LINUX_SYS_STATE_FILE, O_WRONLY); - if (fd < 0) { - _exit(EXIT_FAILURE); - } - - if (write(fd, sysfile_str, strlen(sysfile_str)) < 0) { - _exit(EXIT_FAILURE); - } - - _exit(EXIT_SUCCESS); - } else if (pid < 0) { - error_setg_errno(errp, errno, "failed to create child process"); - return; - } + g_autofree char *echo_cmd =3D g_strdup_printf( + "echo %s > " LINUX_SYS_STATE_FILE, sysfile_str); + const char *argv[] =3D {"sh", "-c", echo_cmd, NULL}; =20 - ga_wait_child(pid, &status, &local_err); + ga_run_command(argv, NULL, "suspend", &local_err); if (local_err) { error_propagate(errp, local_err); return; } - - if (WEXITSTATUS(status)) { - error_setg(errp, "child process has failed to suspend"); - } - } =20 static void guest_suspend(SuspendMode mode, Error **errp) --=20 2.39.3 From nobody Tue Nov 26 09:32:09 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=quarantine dis=none) header.from=virtuozzo.com ARC-Seal: i=1; a=rsa-sha256; t=1708967014; cv=none; d=zohomail.com; s=zohoarc; b=evwOJe9BlOr9Disd6nTO/9Gk6/8ZVkQfBq7CSgFFB8qR6xbalqzQJXUcXQe4V1LGhCbRODAq3BO5c75xFt2duQ+VZWBCaI3VnNDWfqoF5HNUJg6U81pzSUHIiNcgzazAnfqU0bvRx+APH/8ox95GRcc01UbbhIqSIMGgi3ua098= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1708967014; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=H0hbpkn404mwmdH7XPrx5U+ELdeb94tjp4nhB1Ff+A8=; b=B4L7my2+H3c2CN7bpxr5IFSCUO8erypnoGu7CGQfTAxHI+tknhOVXeR5p5qMPerVrNKI46+0QI6Rq9Vi180ilusC6o5jCAR/bo64be+2VY+eexwNWPueqmlYx9/1aVxdRkam4TD/241W6pkmhaOd3X7vUyA8qiyktkzK2doHu4Y= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1708967014396508.5272465505992; Mon, 26 Feb 2024 09:03:34 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1reeHu-0005ba-RZ; Mon, 26 Feb 2024 11:57:11 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1reeHl-0005XU-BI for qemu-devel@nongnu.org; Mon, 26 Feb 2024 11:57:01 -0500 Received: from relay.virtuozzo.com ([130.117.225.111]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1reeHX-0007aR-7l for qemu-devel@nongnu.org; Mon, 26 Feb 2024 11:56:53 -0500 Received: from [130.117.225.1] (helo=dev005.ch-qa.vzint.dev) by relay.virtuozzo.com with esmtp (Exim 4.96) (envelope-from ) id 1reeG3-001AD7-0w; Mon, 26 Feb 2024 17:56:34 +0100 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=virtuozzo.com; s=relay; h=MIME-Version:Message-Id:Date:Subject:From: Content-Type; bh=H0hbpkn404mwmdH7XPrx5U+ELdeb94tjp4nhB1Ff+A8=; b=wba8/XTv1azl TIO+8csWhO3ezT84yk5UNqi87YhlGxvNl34y2F4omTqd9+n2Sno4w36816XhSG3SCL3YwJWqFOPrN bxACcR8xXO/tL2FYgEbGkTpsSit5YCy0+ad/lM4kdlHOJCuMDSfMLaiAlmb/7JDseDZQmiztbFXza /VeC+OLYIFYt+l5qq577xdyGwJ3CK/OoRSEm08gbAT9ZKZ+5KyR7toKfFA9NODvSHxO8s7SunY+iC 2MkKHNIIE6ELVDrwrR3FSZIk6mzurCcqMfGkN5GATz/vSAHwhGc/5BB8dawxPI0gpMZrhG2PmVlBX CQlQGBHHKcqRpjGAPArGQA==; From: Andrey Drobyshev To: qemu-devel@nongnu.org Cc: michael.roth@amd.com, kkostiuk@redhat.com, marcandre.lureau@redhat.com, andrey.drobyshev@virtuozzo.com, den@virtuozzo.com Subject: [PATCH 7/7] qga/commands-posix: qmp_guest_set_user_password: use ga_run_command helper Date: Mon, 26 Feb 2024 18:56:42 +0200 Message-Id: <20240226165642.807350-8-andrey.drobyshev@virtuozzo.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20240226165642.807350-1-andrey.drobyshev@virtuozzo.com> References: <20240226165642.807350-1-andrey.drobyshev@virtuozzo.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=130.117.225.111; envelope-from=andrey.drobyshev@virtuozzo.com; helo=relay.virtuozzo.com X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @virtuozzo.com) X-ZM-MESSAGEID: 1708967014852100001 Content-Type: text/plain; charset="utf-8" There's no need to check for the existence of the "chpasswd", "pw" executables, as the exec() call will do that for us. Signed-off-by: Andrey Drobyshev --- qga/commands-posix.c | 96 ++++++-------------------------------------- 1 file changed, 13 insertions(+), 83 deletions(-) diff --git a/qga/commands-posix.c b/qga/commands-posix.c index 599f59a323..7f39636417 100644 --- a/qga/commands-posix.c +++ b/qga/commands-posix.c @@ -2140,14 +2140,8 @@ void qmp_guest_set_user_password(const char *usernam= e, Error **errp) { Error *local_err =3D NULL; - char *passwd_path =3D NULL; - pid_t pid; - int status; - int datafd[2] =3D { -1, -1 }; - char *rawpasswddata =3D NULL; + g_autofree char *rawpasswddata =3D NULL; size_t rawpasswdlen; - char *chpasswddata =3D NULL; - size_t chpasswdlen; =20 rawpasswddata =3D (char *)qbase64_decode(password, -1, &rawpasswdlen, = errp); if (!rawpasswddata) { @@ -2158,95 +2152,31 @@ void qmp_guest_set_user_password(const char *userna= me, =20 if (strchr(rawpasswddata, '\n')) { error_setg(errp, "forbidden characters in raw password"); - goto out; + return; } =20 if (strchr(username, '\n') || strchr(username, ':')) { error_setg(errp, "forbidden characters in username"); - goto out; + return; } =20 #ifdef __FreeBSD__ - chpasswddata =3D g_strdup(rawpasswddata); - passwd_path =3D g_find_program_in_path("pw"); + g_autofree char *chpasswdata =3D g_strdup(rawpasswddata); + const char *crypt_flag =3D (crypted) ? "-H" : "-h"; + const char *argv[] =3D {"pw", "usermod", "-n", username, + crypt_flag, "0", NULL}; #else - chpasswddata =3D g_strdup_printf("%s:%s\n", username, rawpasswddata); - passwd_path =3D g_find_program_in_path("chpasswd"); + g_autofree char *chpasswddata =3D g_strdup_printf("%s:%s\n", username, + rawpasswddata); + const char *crypt_flag =3D (crypted) ? "-e" : NULL; + const char *argv[] =3D {"chpasswd", crypt_flag, NULL}; #endif =20 - chpasswdlen =3D strlen(chpasswddata); - - if (!passwd_path) { - error_setg(errp, "cannot find 'passwd' program in PATH"); - goto out; - } - - if (!g_unix_open_pipe(datafd, FD_CLOEXEC, NULL)) { - error_setg(errp, "cannot create pipe FDs"); - goto out; - } - - pid =3D fork(); - if (pid =3D=3D 0) { - close(datafd[1]); - /* child */ - setsid(); - dup2(datafd[0], 0); - reopen_fd_to_null(1); - reopen_fd_to_null(2); - -#ifdef __FreeBSD__ - const char *h_arg; - h_arg =3D (crypted) ? "-H" : "-h"; - execl(passwd_path, "pw", "usermod", "-n", username, h_arg, "0", NU= LL); -#else - if (crypted) { - execl(passwd_path, "chpasswd", "-e", NULL); - } else { - execl(passwd_path, "chpasswd", NULL); - } -#endif - _exit(EXIT_FAILURE); - } else if (pid < 0) { - error_setg_errno(errp, errno, "failed to create child process"); - goto out; - } - close(datafd[0]); - datafd[0] =3D -1; - - if (qemu_write_full(datafd[1], chpasswddata, chpasswdlen) !=3D chpassw= dlen) { - error_setg_errno(errp, errno, "cannot write new account password"); - goto out; - } - close(datafd[1]); - datafd[1] =3D -1; - - ga_wait_child(pid, &status, &local_err); + ga_run_command(argv, chpasswddata, "set user password", &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; - } - - if (WEXITSTATUS(status)) { - error_setg(errp, "child process has failed to set user password"); - goto out; - } - -out: - g_free(chpasswddata); - g_free(rawpasswddata); - g_free(passwd_path); - if (datafd[0] !=3D -1) { - close(datafd[0]); - } - if (datafd[1] !=3D -1) { - close(datafd[1]); + return; } } #else /* __linux__ || __FreeBSD__ */ --=20 2.39.3