From nobody Fri May 17 07:47:02 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=1710505807; cv=none; d=zohomail.com; s=zohoarc; b=JPl6d0AvJ/0lo3XI+nHmztlTy7BHxZPKxH2ZZYcbJUnObuQlrpPWKHaohJdXfq24/PJpooBmk3NMKvxVZC9Q8mK6ZCiFC4/b7feMtmIsV5TTOTzV9Hp6XM7ui72h245luTiK6j1nhdNF48Q7M51XEnwPtZ1EYVykG6sF5YgMl1s= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1710505807; 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=z+2upVLm5CJLKbcQAz4YdhJ9C1wGGiDlSEOSE/EyGsY=; b=cvTUkw2zJd3lubrOCljj4MPxYCTGL4c6TNK/TrV5ssL+1jRms0vUw1nnu4gwB3GH0FKOuwHtJPBiVxlA1SusFg75V5bdPLP6pmn+SeeuArRSO02wrkz+P03iVbrrPaOrnHtjYCTj9+rrBlVyZv3JVFZe0rOQ1OJSje6M5K23OAY= 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 1710505807054229.75688487911145; Fri, 15 Mar 2024 05:30:07 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rl6hC-0002D8-MF; Fri, 15 Mar 2024 08:29:58 -0400 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 1rl6h9-0002CL-10 for qemu-devel@nongnu.org; Fri, 15 Mar 2024 08:29:55 -0400 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 1rl6h6-0006OR-Hb for qemu-devel@nongnu.org; Fri, 15 Mar 2024 08:29:54 -0400 Received: from [130.117.225.1] (helo=dev005.ch-qa.vzint.dev) by relay.virtuozzo.com with esmtp (Exim 4.96) (envelope-from ) id 1rl6f4-005Caf-39; Fri, 15 Mar 2024 13:29:46 +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=z+2upVLm5CJLKbcQAz4YdhJ9C1wGGiDlSEOSE/EyGsY=; b=nsINTdb8tBVP DgMlRXjEz2Umdf6eJMjrIGj3YLhtyxURzFMaZYZUg0vz4SvLdXt3v49+xk6lUIwDOWNjpEgY7tMrn IbEZ0Mit731NyOG3M/x+1H9/kKmi7OWfH2W3DtY6j3/NDGsn5lQv3jE9HKZVLUhWulXNfe5h5X9my TxJ7XHb5/gCdx3KziWy0NJrxy7JxKmYDrRQw0aKdDIqIcd5vzZr0Aoc240tb/F0U2C+Xo/GVVbOYN aiG8LvvrmfoiURwdl7u59v25oG0V1K2FXggBMLy1etY2oQgPybM77jvhzYkZBwzDEeG8uJV17g25T JYPn14efF1cRd1vsmqrnDA==; From: Andrey Drobyshev To: qemu-devel@nongnu.org Cc: berrange@redhat.com, michael.roth@amd.com, kkostiuk@redhat.com, marcandre.lureau@redhat.com, philmd@linaro.org, andrey.drobyshev@virtuozzo.com, den@virtuozzo.com Subject: [PATCH v3 1/7] qga: guest-get-fsinfo: add optional 'total-bytes-root' field Date: Fri, 15 Mar 2024 14:29:40 +0200 Message-Id: <20240315122946.39168-2-andrey.drobyshev@virtuozzo.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20240315122946.39168-1-andrey.drobyshev@virtuozzo.com> References: <20240315122946.39168-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: 1710505808866100001 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) as returned by statvfs(3). While on Windows guests that's all we can get with GetDiskFreeSpaceExA(), on POSIX guests we might also be interested in total file system size, as it's visible for root user. Let's add an optional field 'total-bytes-root' to GuestFilesystemInfo struct, which'd only be reported on POSIX and represent f_blocks value as returned by statvfs(3). While here, let's document better where those values come from in both POSIX and Windows. Signed-off-by: Andrey Drobyshev --- qga/commands-posix.c | 2 ++ qga/commands-win32.c | 1 + qga/qapi-schema.json | 12 +++++++++++- 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/qga/commands-posix.c b/qga/commands-posix.c index 26008db497..8207c4c47e 100644 --- a/qga/commands-posix.c +++ b/qga/commands-posix.c @@ -1569,8 +1569,10 @@ static GuestFilesystemInfo *build_guest_fsinfo(struc= t FsMount *mount, nonroot_total =3D used + buf.f_bavail; fs->used_bytes =3D used * fr_size; fs->total_bytes =3D nonroot_total * fr_size; + fs->total_bytes_root =3D buf.f_blocks * fr_size; =20 fs->has_total_bytes =3D true; + fs->has_total_bytes_root =3D true; fs->has_used_bytes =3D true; } =20 diff --git a/qga/commands-win32.c b/qga/commands-win32.c index a1015757d8..9e820aad8d 100644 --- a/qga/commands-win32.c +++ b/qga/commands-win32.c @@ -1143,6 +1143,7 @@ static GuestFilesystemInfo *build_guest_fsinfo(char *= guid, Error **errp) fs =3D g_malloc(sizeof(*fs)); fs->name =3D g_strdup(guid); fs->has_total_bytes =3D false; + fs->has_total_bytes_root =3D false; fs->has_used_bytes =3D false; if (len =3D=3D 0) { fs->mountpoint =3D g_strdup("System Reserved"); diff --git a/qga/qapi-schema.json b/qga/qapi-schema.json index b8efe31897..093a5ab602 100644 --- a/qga/qapi-schema.json +++ b/qga/qapi-schema.json @@ -1031,8 +1031,18 @@ # @type: file system type string # # @used-bytes: file system used bytes (since 3.0) +# * POSIX: (f_blocks - f_bfree) * f_frsize, as returned by statvfs(3) +# * Windows: (TotalNumberOfBytes - TotalNumberOfFreeBytes), as returned +# by GetDiskFreeSpaceEx() # # @total-bytes: non-root file system total bytes (since 3.0) +# * POSIX: (f_blocks - f_bfree + f_bavail) * f_frsize, as returned by +# statvfs(3) +# * Windows: TotalNumberOfBytes, as returned by GetDiskFreeSpaceEx() +# +# @total-bytes-root: total file system size in bytes (as visible for a +# priviledged user) (since 8.3) +# * POSIX only: (f_blocks * f_frsize), returned by statvfs(3) # # @disk: an array of disk hardware information that the volume lies # on, which may be empty if the disk type is not supported @@ -1042,7 +1052,7 @@ { 'struct': 'GuestFilesystemInfo', 'data': {'name': 'str', 'mountpoint': 'str', 'type': 'str', '*used-bytes': 'uint64', '*total-bytes': 'uint64', - 'disk': ['GuestDiskAddress']} } + '*total-bytes-root': 'uint64', 'disk': ['GuestDiskAddress']} } =20 ## # @guest-get-fsinfo: --=20 2.39.3 From nobody Fri May 17 07:47:02 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=1710505813; cv=none; d=zohomail.com; s=zohoarc; b=IW0zCcaPYd9q7lN7LVtyFpFdXdMXctQW6WSS+4Cj0dHU2/mNbC1Acr1bXRm8rv0LcqnZcUqV/liQrRZzczHe704n7Gwl/M2BIM/HV4+A00D2OBA7yhWJDieBuIi9mcvAqo4p8n0FbWQPE+NJKyHe5o2tS1BFGmsJbchimOA0k/w= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1710505813; 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=LI4Zjt2kmsotdBVKwkUebV826YtkKLl6ksUtmYYkisA=; b=Ad3Q/Iw8E7xD2ZPDUl+r+QQjDwDUDJ6znaTSnmAi9wt5Jmg/EgfiuuvFnescMH2K4J9hOJnqBo5bUAHXWBr5wF3rHxUx1OdoePoEeAMhHAzTLgG4rBJNnvTRfSueRrD1k35KATclxCEV2TaqFRRHZJO971ehRxXjMUeM3oGTF1A= 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 1710505813603626.8423956619484; Fri, 15 Mar 2024 05:30:13 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rl6hG-0002DM-Su; Fri, 15 Mar 2024 08:30:02 -0400 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 1rl6h9-0002CN-Vv for qemu-devel@nongnu.org; Fri, 15 Mar 2024 08:29:56 -0400 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 1rl6h6-0006OT-Hn for qemu-devel@nongnu.org; Fri, 15 Mar 2024 08:29:54 -0400 Received: from [130.117.225.1] (helo=dev005.ch-qa.vzint.dev) by relay.virtuozzo.com with esmtp (Exim 4.96) (envelope-from ) id 1rl6f5-005Caf-06; Fri, 15 Mar 2024 13:29:46 +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=LI4Zjt2kmsotdBVKwkUebV826YtkKLl6ksUtmYYkisA=; b=h2EHbiHxZbOW 926K4H1p/nZouq1o0yST2h3iNqfjYxOOpL1EXnpBaQEbdP+b17f0Z+3fVJq2l+59/FNunvw9cQLkf llpwmDreF6h5zI7108p2lS65wsb2Ax6bDtW87dBL9N9mHjt47++MIgjePlda5+4ijBwzro++y33U8 tDx6vOBg/1XX36zRzA6CmpMYUEIlAGVjftMTKJqpP3o4a4pT645vitwC1TUg7Pl/NiqvgTESIuuON XeTwZKJNXM7pyuRx8AFp6FcQmbpbt+JIWSx0FncaoM6cfMjp21M/7iRYJ38Gdj5iVny5V8t0cbu9I Dxt6lTS/alEWjsLIwZqHaQ==; From: Andrey Drobyshev To: qemu-devel@nongnu.org Cc: berrange@redhat.com, michael.roth@amd.com, kkostiuk@redhat.com, marcandre.lureau@redhat.com, philmd@linaro.org, andrey.drobyshev@virtuozzo.com, den@virtuozzo.com Subject: [PATCH v3 2/7] qga: introduce ga_run_command() helper for guest cmd execution Date: Fri, 15 Mar 2024 14:29:41 +0200 Message-Id: <20240315122946.39168-3-andrey.drobyshev@virtuozzo.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20240315122946.39168-1-andrey.drobyshev@virtuozzo.com> References: <20240315122946.39168-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: 1710505814643100001 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 Reviewed-by: Daniel P. Berrang=C3=A9 --- qga/commands-posix.c | 150 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 150 insertions(+) diff --git a/qga/commands-posix.c b/qga/commands-posix.c index 8207c4c47e..ad05630086 100644 --- a/qga/commands-posix.c +++ b/qga/commands-posix.c @@ -76,6 +76,156 @@ static void ga_wait_child(pid_t pid, int *status, Error= **errp) g_assert(rpid =3D=3D pid); } =20 +static ssize_t ga_pipe_read_str(int fd[2], char **str) +{ + ssize_t n, len =3D 0; + 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 { + len =3D -errno; + break; + } + } + *str =3D g_realloc(*str, len + n + 1); + memcpy(*str + len, buf, n); + len +=3D n; + *str[len] =3D '\0'; + } + close(fd[0]); + fd[0] =3D -1; + + return len; +} + +/* + * 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; + ssize_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; + } + + len =3D ga_pipe_read_str(outfd, &str); + if (len < 0) { + error_setg_errno(errp, -len, "%s: cannot read from stdout/stderr p= ipe", + action); + goto out; + } + + 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 Fri May 17 07:47:02 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=1710505890; cv=none; d=zohomail.com; s=zohoarc; b=I3qcMukjRgJdaNEQNT1XtUiGTd/cFL0FQ5fViJpfBzW4pfhA9NzKwbe0REiWHK8DBOwzbID6VE/VB3Tg6/0r0nt5N5CK6v3yZOSs5ltwR7HMH3L0HVo2SxN5HnYAZp+MXrW7LBKiwx5xO9K10um/m36xe1WVoshaSDoi8i5tKTc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1710505890; 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=+RaGuaEMaX9te6C/lVARGVygYN22wpJAkkpqQaFesuw=; b=Cyo1VcXnHBDybtn+h3qVAQoBbjZGECmzZUTBXHca1+mrpwBXSX3s0PCR8d/i/1lnfrniGp4hb7c2U1XS6mdEKZJwkyeR5Hp0crOcoCGt3aCW5NR/My7drO2wNPvnGiawyMHH2B8I2r7qWb4oK7dt0ZQFKrDuzQN6g8MelNj3oLk= 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 1710505890376137.38983708417106; Fri, 15 Mar 2024 05:31:30 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rl6hk-0002ks-G5; Fri, 15 Mar 2024 08:30:32 -0400 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 1rl6hI-0002Gk-PK for qemu-devel@nongnu.org; Fri, 15 Mar 2024 08:30:06 -0400 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 1rl6h6-0006OP-UF for qemu-devel@nongnu.org; Fri, 15 Mar 2024 08:30:03 -0400 Received: from [130.117.225.1] (helo=dev005.ch-qa.vzint.dev) by relay.virtuozzo.com with esmtp (Exim 4.96) (envelope-from ) id 1rl6f5-005Caf-0H; Fri, 15 Mar 2024 13:29:46 +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=+RaGuaEMaX9te6C/lVARGVygYN22wpJAkkpqQaFesuw=; b=UaW7n5x2vUa4 qnfi4kWz1jp79iAeNTaiB6ps7j8ebeefjCGXt8wh/GzQFcmaeABS1Hi86QMspdMV1UobDB+NoRnxd vmr5DEIKXKWVO3008HQroXYGdKEV1AR6ohuNHM/DMvDqH32ynKLzO0I6bx6vQL1mUylkBTxL1DAd2 VUibrzK8xiaZNRKalfemk4IkwzjIfYq/r77vhwyPiN2Y+OVtXGl5Y9QwoE3IVT0VxgRmkRHjEdHcU Ns76GDHeuqEuoauMfWb5MvjI7HjYpLV8wRCUxhr2j82QpKs/JecoUok4hrTFJwcey9uDrrm1ePARi aNz+OmolIVysVae00HVDQw==; From: Andrey Drobyshev To: qemu-devel@nongnu.org Cc: berrange@redhat.com, michael.roth@amd.com, kkostiuk@redhat.com, marcandre.lureau@redhat.com, philmd@linaro.org, andrey.drobyshev@virtuozzo.com, den@virtuozzo.com Subject: [PATCH v3 3/7] qga/commands-posix: qmp_guest_shutdown: use ga_run_command helper Date: Fri, 15 Mar 2024 14:29:42 +0200 Message-Id: <20240315122946.39168-4-andrey.drobyshev@virtuozzo.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20240315122946.39168-1-andrey.drobyshev@virtuozzo.com> References: <20240315122946.39168-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: 1710505892889100003 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 Reviewed-by: Daniel P. Berrang=C3=A9 --- 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 ad05630086..d4025e0c1e 100644 --- a/qga/commands-posix.c +++ b/qga/commands-posix.c @@ -108,7 +108,6 @@ static ssize_t ga_pipe_read_str(int fd[2], char **str) * 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) { @@ -230,8 +229,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"; @@ -260,46 +257,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 Fri May 17 07:47:02 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=1710505834; cv=none; d=zohomail.com; s=zohoarc; b=W/GWglw4OrlgE+DSUcdyMXg1AY8+UO36xe/smr3mr+IYZHnEIoQhSy8fW656ZC6zkYSsylyIvrfbLLqT9LTL7EGAXunAt/mquPeMMkcRJMyvKPnuIiirr7CXkiX4U9y5GaioMkrActqcnRMcAnsSCGLIBw9yV3+2eVf7DZ02CDY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1710505834; 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=t7XlrqutVCUHR7SEHqbK4gTTxIOu1rtpDU+8vOsVCKg=; b=gMVKMMYjnOQQK5r5tWXRh78eGDSE6jHWB2Ln8qBEzdTwIvoaBbwqwqIEFCquiSRYgUgWo3mHws8VV0BRkt+dOtf16GWgeXUZVc18TRlFocfKP4y8GBZIPYdpuONaN/one6S/dSIw8jUA1c/9UNxcb/RH7VffQeSduWuovT4ehiE= 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 1710505834912734.9864526689163; Fri, 15 Mar 2024 05:30:34 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rl6hZ-0002J8-BS; Fri, 15 Mar 2024 08:30:23 -0400 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 1rl6hE-0002Dc-OL for qemu-devel@nongnu.org; Fri, 15 Mar 2024 08:30:02 -0400 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 1rl6h6-0006OU-UE for qemu-devel@nongnu.org; Fri, 15 Mar 2024 08:29:58 -0400 Received: from [130.117.225.1] (helo=dev005.ch-qa.vzint.dev) by relay.virtuozzo.com with esmtp (Exim 4.96) (envelope-from ) id 1rl6f5-005Caf-0S; Fri, 15 Mar 2024 13:29:46 +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=t7XlrqutVCUHR7SEHqbK4gTTxIOu1rtpDU+8vOsVCKg=; b=BFDlUavUltkB t7k6TF7NjogYRJIsCxRJ3ZNKAYsgrRz+zFG9WGJCHaYnk0JB1vhbF8+94pKcOrhbsxg2zpevESGdM +sOm1tmGCjMt1wEiePKWs5tIgaNzoQHUkVB7/xsYfOoRHhMZ3bGLS2g8kAEYvZAOEE9FvjR3iSx8C XqggWWznrinCKZ8DV7X5A7tpz3otyGedxzJIODkTBUUOKzpp0O+RTCMOGHPvV2JslnNhhdnw5plO0 lh1xgVt9fRrTtm+AVQkGDYUxoAlUIPnpJkKKdPxbUkzHrc4ZNiCIxbxBFo5wx3aNXl5GaTEWeuGmX 73ZlN99IJK3Ct5EIluzr9Q==; From: Andrey Drobyshev To: qemu-devel@nongnu.org Cc: berrange@redhat.com, michael.roth@amd.com, kkostiuk@redhat.com, marcandre.lureau@redhat.com, philmd@linaro.org, andrey.drobyshev@virtuozzo.com, den@virtuozzo.com Subject: [PATCH v3 4/7] qga/commands-posix: qmp_guest_set_time: use ga_run_command helper Date: Fri, 15 Mar 2024 14:29:43 +0200 Message-Id: <20240315122946.39168-5-andrey.drobyshev@virtuozzo.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20240315122946.39168-1-andrey.drobyshev@virtuozzo.com> References: <20240315122946.39168-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: 1710505836699100003 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 Reviewed-by: Daniel P. Berrang=C3=A9 --- 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 d4025e0c1e..94b652d54e 100644 --- a/qga/commands-posix.c +++ b/qga/commands-posix.c @@ -279,21 +279,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) { @@ -324,37 +312,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 Fri May 17 07:47:02 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=1710505915; cv=none; d=zohomail.com; s=zohoarc; b=jsdcFfQGCoWRiSUA4CwMX7bgV3Pqzroir/M6OfCWvnFdjs1l1CPnrxf0WfPaIG4rWXREMw5W6hBPkopomHrpRGyNn+0Xl/+0Bxg+1EETuFEpvfY57vU9HvDvwz+yeXjkJgzVi5KdRe2E9Zk277WwQG4N3Kap60Fr868NRBYja9c= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1710505915; 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=rzhBDkOhn7PA/qCuliUHOgVG1zAo7JXrGfpije2w0fg=; b=kjgO1YEjJaJ6wqQ2qpiqOBiaWtFm/PJljK8v2R67rWWb5bqNB0uak2yexJtEssP6tJQz+MSxVy/+sCsn41MmahjkAVBboDIu3g3s/VIhWgN1QbanhMmPKk8nGxa05WKQdgBZHucMhEq8iyd5gcm5yWopmZkyi1koHZSgrtimLv4= 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 171050591569036.71706067566572; Fri, 15 Mar 2024 05:31:55 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rl6hm-0002oE-FD; Fri, 15 Mar 2024 08:30:34 -0400 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 1rl6hI-0002Gj-P7 for qemu-devel@nongnu.org; Fri, 15 Mar 2024 08:30:06 -0400 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 1rl6h8-0006OQ-Na for qemu-devel@nongnu.org; Fri, 15 Mar 2024 08:30:03 -0400 Received: from [130.117.225.1] (helo=dev005.ch-qa.vzint.dev) by relay.virtuozzo.com with esmtp (Exim 4.96) (envelope-from ) id 1rl6f5-005Caf-0d; Fri, 15 Mar 2024 13:29:46 +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=rzhBDkOhn7PA/qCuliUHOgVG1zAo7JXrGfpije2w0fg=; b=XzD7HIs0EOHQ BLN5Dg6Zzc3fkcrHqp3/KzcxrXl9w2cj03Y4R0fzUipYGMNuXHnOSVcJN+mTTXa08nW0fyBmQDAQP 6Zim8fIT3RSph5uToHwH/n7RCbykfFg2jwDKpTZdoFR/SK57/BjcOIZmxcFsrbjEgJY+zc5uomc7/ QBNHAemBEXGEI01DbuE0L4yupDeV+mHiNqTtRihKejtVClZC6kz9ivaIl/UobLas3cro5zbkoBj6A bPfZu76TZ439acMKSIqg4aeLhAml0zAk1YVtBuZTfGNK/SEoLBm6H8J7IbSM//lKZVdojyn99iYpk BDOlfuwDYJeN3iIg+mCLbA==; From: Andrey Drobyshev To: qemu-devel@nongnu.org Cc: berrange@redhat.com, michael.roth@amd.com, kkostiuk@redhat.com, marcandre.lureau@redhat.com, philmd@linaro.org, andrey.drobyshev@virtuozzo.com, den@virtuozzo.com Subject: [PATCH v3 5/7] qga/commands-posix: execute_fsfreeze_hook: use ga_run_command helper Date: Fri, 15 Mar 2024 14:29:44 +0200 Message-Id: <20240315122946.39168-6-andrey.drobyshev@virtuozzo.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20240315122946.39168-1-andrey.drobyshev@virtuozzo.com> References: <20240315122946.39168-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: 1710505916979100003 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 Reviewed-by: Daniel P. Berrang=C3=A9 --- 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 94b652d54e..610d225d30 100644 --- a/qga/commands-posix.c +++ b/qga/commands-posix.c @@ -736,8 +736,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; @@ -746,42 +744,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 Fri May 17 07:47:02 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=1710505847; cv=none; d=zohomail.com; s=zohoarc; b=UYUDL7TqloBve/IXGjgHHU8fptTf6cx55TmBS2IICbYgOx0l0ecdFqNHdryY5S/I0+Cf4a9FYn/8WItBmOQYzKZAe5jNY6ommVxN1/GXS1/uhGHODbPDUQsxC+VzoGlknVtZY322NccYPdWVCo1qMK3DaEqniP5/3bnV1PkHsU8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1710505847; h=Content-Type: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=eKJQrKU9aZiOLAU3zCGqFcD6ur956gr0FfORIR7FeCE=; b=ilx1VpKFkJJ5LKatBzvZznw7Ca3/xgY9WIBtf11RnA3eXM0ORNk5YatOUeJ8uxhs29tP6/GwLhzXCoiZWb5k0iOpNso0txFsASFeI51MXFsJ8sPtdH95NkKz4jiOAsz2SNs1DomI7UOQXu5M0PfAm2Dgsw2YZTE25NVc8djAS2I= 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 1710505847418222.48556117626663; Fri, 15 Mar 2024 05:30:47 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rl6hf-0002SM-5D; Fri, 15 Mar 2024 08:30:29 -0400 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 1rl6hC-0002DB-PQ for qemu-devel@nongnu.org; Fri, 15 Mar 2024 08:29:58 -0400 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 1rl6h8-0006OV-Nb for qemu-devel@nongnu.org; Fri, 15 Mar 2024 08:29:57 -0400 Received: from [130.117.225.1] (helo=dev005.ch-qa.vzint.dev) by relay.virtuozzo.com with esmtp (Exim 4.96) (envelope-from ) id 1rl6f5-005Caf-0o; Fri, 15 Mar 2024 13:29:46 +0100 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=virtuozzo.com; s=relay; h=Content-Type:MIME-Version:Message-Id:Date:Subject :From; bh=eKJQrKU9aZiOLAU3zCGqFcD6ur956gr0FfORIR7FeCE=; b=L+WpVR5ELtDahdX/XZ0 4mNduzpPHoVPGFV6w/jlm2CFRUH/WvKabRm2ae9E7K0EeeP5kD1dd4P6T+vMRqV53BAjdnl+p66mN euZwGPMZx2upizrmi0YYT41685AP0tt+F2liOofQLyscC1wTOHAwcnCAon3EAfRIMwKjIF5YFiQbM 1jUarIVVFlR4DBj7vSLeiBUszSXu8P/e2nBDj1llSVfnZ/ny0wdRaRD9iJh1dN58AAPu/SlTLBUwr BCuLKnzp7QZSv2PwVgNca84HLVcPklDajV2tfmEN42sdYNIBrJ6tB17Ay5PWSqlyoy1p/2cFjqzQw lOUxrW4rFbmR6AA==; From: Andrey Drobyshev To: qemu-devel@nongnu.org Cc: berrange@redhat.com, michael.roth@amd.com, kkostiuk@redhat.com, marcandre.lureau@redhat.com, philmd@linaro.org, andrey.drobyshev@virtuozzo.com, den@virtuozzo.com Subject: [PATCH v3 6/7] qga/commands-posix: don't do fork()/exec() when suspending via sysfs Date: Fri, 15 Mar 2024 14:29:45 +0200 Message-Id: <20240315122946.39168-7-andrey.drobyshev@virtuozzo.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20240315122946.39168-1-andrey.drobyshev@virtuozzo.com> References: <20240315122946.39168-1-andrey.drobyshev@virtuozzo.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" 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: 1710505848760100003 Since commit 246d76eba ("qga: guest_suspend: decoupling pm-utils and sys logic") pm-utils logic is running in a separate child from the sysfs logic. Now when suspending via sysfs we don't really need to do that in a separate process as we only need to perform one write to /sys/power/state. Let's just use g_file_set_contents() to simplify things here. Suggested-by: Daniel P. Berrang=C3=A9 Signed-off-by: Andrey Drobyshev Reviewed-by: Daniel P. Berrang=C3=A9 --- qga/commands-posix.c | 41 +++++------------------------------------ 1 file changed, 5 insertions(+), 36 deletions(-) diff --git a/qga/commands-posix.c b/qga/commands-posix.c index 610d225d30..e0ea377f65 100644 --- a/qga/commands-posix.c +++ b/qga/commands-posix.c @@ -1928,52 +1928,21 @@ static bool linux_sys_state_supports_mode(SuspendMo= de mode, Error **errp) =20 static void linux_sys_state_suspend(SuspendMode mode, Error **errp) { - Error *local_err =3D NULL; + GError *local_gerr =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; - } - - ga_wait_child(pid, &status, &local_err); - if (local_err) { - error_propagate(errp, local_err); + if (!g_file_set_contents(LINUX_SYS_STATE_FILE, sysfile_str, + -1, &local_gerr)) { + error_setg(errp, "suspend: cannot write to '%s': %s", + LINUX_SYS_STATE_FILE, local_gerr->message); 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 Fri May 17 07:47:02 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=1710505865; cv=none; d=zohomail.com; s=zohoarc; b=LY3Seyw5sY+xplpcToZOq0p/G0D4Sb3Smafyp9MD6VFgPzNUv0riBckcvpG4ry5T3Ob7paTZmXJt/un1PbVIf+h78vcvg/xCm6KNKqn6gE8Ne/I18KHJWAl6qM5xbK9XLPJ7laHZA9Tvwkkh8O0E5dGpXwgeo/P/rHmvGfyKL7w= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1710505865; 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=atrtJ9o9Y5R8NzRgsf6pRIlcgshMhbCbGyUrqmxx9xQ=; b=eUHtDmRl5zF2muK9oAP4LVog7nD/jGHIf/noKbzlTC7jtq5xTr+sZVa8fKrBh1gU7MOiv+CwGvv8gAOP8EP2PqsC33HyIIs26ZODES1WXhuASKd2RPIX+3OsqHsHPgaLQvjQ/pDYR4x7mZX6rH1jAW2Of8GwaFYuPYRE5TTFB+g= 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 1710505865851251.69981942392405; Fri, 15 Mar 2024 05:31:05 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rl6hp-0002vh-QG; Fri, 15 Mar 2024 08:30:38 -0400 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 1rl6hN-0002JG-Ou for qemu-devel@nongnu.org; Fri, 15 Mar 2024 08:30:13 -0400 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 1rl6h8-0006OW-OF for qemu-devel@nongnu.org; Fri, 15 Mar 2024 08:30:09 -0400 Received: from [130.117.225.1] (helo=dev005.ch-qa.vzint.dev) by relay.virtuozzo.com with esmtp (Exim 4.96) (envelope-from ) id 1rl6f5-005Caf-0z; Fri, 15 Mar 2024 13:29:46 +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=atrtJ9o9Y5R8NzRgsf6pRIlcgshMhbCbGyUrqmxx9xQ=; b=LCeQ/plhqWHb 0GBn8yDCgGCXtfxYxiTJ/w+kN8LXzeSA3eKNPYBPBmAeYym/1DUvvSP12sqZ/3RD2/zw/k4aIVgpY CEo8hGYW8l9/7Os40PgRvoPAjuMjJi75gshN5G4zcMmge7ulkOCo9YKm7s3IF3iPzVJLQNcZBoysn BFSo0CiDNk8OvX40QU57kL/OT3OLZIKds7tOmHjPGaZxRudpovHBZiIHivU1py1X0IRGoQuTE2iXc G7z10AUfdz/oa7RtWoq0fvIs0sTUlHehiZ3qvhv5e+m/E9I/xCbVi7CXIrcQ6GJuuCmnLoPH6a9kP kFmTChQtLcLYRYM3Bthapg==; From: Andrey Drobyshev To: qemu-devel@nongnu.org Cc: berrange@redhat.com, michael.roth@amd.com, kkostiuk@redhat.com, marcandre.lureau@redhat.com, philmd@linaro.org, andrey.drobyshev@virtuozzo.com, den@virtuozzo.com Subject: [PATCH v3 7/7] qga/commands-posix: qmp_guest_set_user_password: use ga_run_command helper Date: Fri, 15 Mar 2024 14:29:46 +0200 Message-Id: <20240315122946.39168-8-andrey.drobyshev@virtuozzo.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20240315122946.39168-1-andrey.drobyshev@virtuozzo.com> References: <20240315122946.39168-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: 1710505866816100001 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 Reviewed-by: Daniel P. Berrang=C3=A9 --- 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 e0ea377f65..ffea69c3f0 100644 --- a/qga/commands-posix.c +++ b/qga/commands-posix.c @@ -2151,14 +2151,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) { @@ -2169,95 +2163,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