From patchwork Fri Aug 25 19:29:40 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Denis V. Lunev" X-Patchwork-Id: 13366231 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 19F6FC3DA66 for ; Fri, 25 Aug 2023 19:30:15 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qZcV9-0007Vl-3l; Fri, 25 Aug 2023 15:29:47 -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 1qZcV7-0007V8-OF; Fri, 25 Aug 2023 15:29:45 -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 1qZcV5-0000aY-GE; Fri, 25 Aug 2023 15:29:45 -0400 Received: from ch-vpn.virtuozzo.com ([130.117.225.6] helo=iris.sw.ru) by relay.virtuozzo.com with esmtp (Exim 4.96) (envelope-from ) id 1qZcSK-00FdMR-00; Fri, 25 Aug 2023 21:29:38 +0200 From: "Denis V. Lunev" To: qemu-devel@nongnu.org Cc: qemu-stable@nongnu.org, qemu-block@nongnu.org, "Denis V. Lunev" , Kevin Wolf , Eric Blake , Vladimir Sementsov-Ogievskiy , Hanna Reitz Subject: [PATCH vOther 1/1] qemu-nbd: Restore "qemu-nbd -v --fork" output Date: Fri, 25 Aug 2023 21:29:40 +0200 Message-Id: <20230825192940.35364-1-den@openvz.org> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Received-SPF: pass client-ip=130.117.225.111; envelope-from=den@openvz.org; helo=relay.virtuozzo.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 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: , Reply-To: 20230824200311.636589-2-eblake@redhat.com Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Closing stderr earlier is good for daemonized qemu-nbd under ssh earlier, but breaks the case where -v is being used to track what is happening in the server, as in iotest 233. When we know we are verbose, we should preserve original stderr and restore it once the setup stage is done. This commit restores the original behavior with -v option. In this case original output inside the test is kept intact. Reported-by: Kevin Wolf Signed-off-by: Denis V. Lunev CC: Eric Blake CC: Vladimir Sementsov-Ogievskiy CC: Hanna Reitz Fixes: 5c56dd27a2 ("qemu-nbd: fix regression with qemu-nbd --fork run over ssh") --- After lengthy thoughts there is a different proposal to fix the introduced regression. Under this approach we could keep original test output. This looks important to me. Eric, do you have any opinion? Thank you in advance, Den qemu-nbd.c | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/qemu-nbd.c b/qemu-nbd.c index aaccaa3318..8322bd5b5b 100644 --- a/qemu-nbd.c +++ b/qemu-nbd.c @@ -275,6 +275,7 @@ struct NbdClientOpts { char *device; bool fork_process; bool verbose; + int stderr; }; static void *nbd_client_thread(void *arg) @@ -323,11 +324,14 @@ static void *nbd_client_thread(void *arg) opts->device, srcpath); } else { /* Close stderr so that the qemu-nbd process exits. */ - if (dup2(STDOUT_FILENO, STDERR_FILENO) < 0) { + if (dup2(opts->stderr, STDERR_FILENO) < 0) { error_report("Could not set stderr to /dev/null: %s", strerror(errno)); exit(EXIT_FAILURE); } + if (opts->stderr != STDOUT_FILENO) { + close(opts->stderr); + } } if (nbd_client(fd) < 0) { @@ -590,7 +594,9 @@ int main(int argc, char **argv) const char *selinux_label = NULL; BlockExportOptions *export_opts; #if HAVE_NBD_DEVICE - struct NbdClientOpts opts; + struct NbdClientOpts opts = { + .stderr = STDOUT_FILENO, + }; #endif #ifdef CONFIG_POSIX @@ -944,6 +950,15 @@ int main(int argc, char **argv) close(stderr_fd[0]); + /* Remember parent's stderr if we will be restoring it. */ + if (verbose /* fork_process is set */) { + opts.stderr = dup(STDERR_FILENO); + if (opts.stderr < 0) { + error_report("Could not dup stdedd: %s", strerror(errno)); + exit(EXIT_FAILURE); + } + } + ret = qemu_daemon(1, 0); saved_errno = errno; /* dup2 will overwrite error below */ @@ -1180,11 +1195,14 @@ int main(int argc, char **argv) } if (fork_process) { - if (dup2(STDOUT_FILENO, STDERR_FILENO) < 0) { + if (dup2(opts.stderr, STDERR_FILENO) < 0) { error_report("Could not set stderr to /dev/null: %s", strerror(errno)); exit(EXIT_FAILURE); } + if (opts.stderr != STDOUT_FILENO) { + close(opts.stderr); + } } state = RUNNING;