From patchwork Thu Mar 8 18:09:07 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alin-Gabriel Serdean X-Patchwork-Id: 883247 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=openvswitch.org (client-ip=140.211.169.12; helo=mail.linuxfoundation.org; envelope-from=ovs-dev-bounces@openvswitch.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=ovn.org Received: from mail.linuxfoundation.org (mail.linuxfoundation.org [140.211.169.12]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3zxz6m6dkRz9slK for ; Fri, 9 Mar 2018 05:09:20 +1100 (AEDT) Received: from mail.linux-foundation.org (localhost [127.0.0.1]) by mail.linuxfoundation.org (Postfix) with ESMTP id 7BBDDF1D; Thu, 8 Mar 2018 18:09:19 +0000 (UTC) X-Original-To: dev@openvswitch.org Delivered-To: ovs-dev@mail.linuxfoundation.org Received: from smtp1.linuxfoundation.org (smtp1.linux-foundation.org [172.17.192.35]) by mail.linuxfoundation.org (Postfix) with ESMTPS id 12356EEA for ; Thu, 8 Mar 2018 18:09:19 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from relay4-d.mail.gandi.net (relay4-d.mail.gandi.net [217.70.183.196]) by smtp1.linuxfoundation.org (Postfix) with ESMTPS id 64A475F2 for ; Thu, 8 Mar 2018 18:09:18 +0000 (UTC) X-Originating-IP: 85.186.139.13 Received: from localhost.localdomain (unknown [85.186.139.13]) (Authenticated sender: aserdean@ovn.org) by relay4-d.mail.gandi.net (Postfix) with ESMTPSA id 6F4A31720A9; Thu, 8 Mar 2018 19:09:16 +0100 (CET) From: Alin Gabriel Serdean To: dev@openvswitch.org Date: Thu, 8 Mar 2018 20:09:07 +0200 Message-Id: <20180308180907.8592-1-aserdean@ovn.org> X-Mailer: git-send-email 2.16.1.windows.1 X-Spam-Status: No, score=-2.6 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_LOW autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on smtp1.linux-foundation.org Cc: Alin Gabriel Serdean Subject: [ovs-dev] [PATCH] ovsdb-client: Set binary on FDs when doing backup/restore X-BeenThere: ovs-dev@openvswitch.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: ovs-dev-bounces@openvswitch.org Errors-To: ovs-dev-bounces@openvswitch.org Add some needed consitentcy on Windows for STD_IN/OUT file descriptors when doing backup and restore. Reported-at:https://mail.openvswitch.org/pipermail/ovs-dev/2018-January/343518.html Suggested-by: Ben Pfaff Signed-off-by: Alin Gabriel Serdean --- ovsdb/ovsdb-client.c | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/ovsdb/ovsdb-client.c b/ovsdb/ovsdb-client.c index 222bd6ca8..e1b056df8 100644 --- a/ovsdb/ovsdb-client.c +++ b/ovsdb/ovsdb-client.c @@ -18,6 +18,7 @@ #include #include +#include #include #include #include @@ -1475,11 +1476,30 @@ print_and_free_log_record(struct json *record) json_destroy(record); } +/* Wrapper to verify if the file descriptor `fd` refers to a terminal. + * On Windows also set binary mode on the file descriptor to avoid + * translation (i.e. CRLF line endings). */ +static bool +check_and_set_stdout(int fd) +{ + if (isatty(fd)) { + return false; + } +#ifdef _WIN32 + fflush(stdout); + int result = _setmode(STDOUT_FILENO, O_BINARY); + if (result == -1) { + ovs_fatal(0, "could not _setmode O_BINARY on STDOUT_FILENO"); + } +#endif + return true; +} + static void do_backup(struct jsonrpc *rpc, const char *database, int argc OVS_UNUSED, char *argv[] OVS_UNUSED) { - if (isatty(STDOUT_FILENO)) { + if (!check_and_set_stdout(STDOUT_FILENO)) { ovs_fatal(0, "not writing backup to a terminal; " "please redirect stdout to a file"); } @@ -1595,7 +1615,7 @@ static void do_restore(struct jsonrpc *rpc, const char *database, int argc OVS_UNUSED, char *argv[] OVS_UNUSED) { - if (isatty(STDIN_FILENO)) { + if (!check_and_set_stdout(STDIN_FILENO)) { ovs_fatal(0, "not reading backup from a terminal; " "please redirect stdin from a file"); }