From patchwork Mon Sep 10 17:05:21 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Arver via GitGitGadget X-Patchwork-Id: 10594543 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 170B614BD for ; Mon, 10 Sep 2018 17:05:26 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 0074128E96 for ; Mon, 10 Sep 2018 17:05:26 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id E8CA228FCA; Mon, 10 Sep 2018 17:05:25 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6911B28D9E for ; Mon, 10 Sep 2018 17:05:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728806AbeIJWAZ (ORCPT ); Mon, 10 Sep 2018 18:00:25 -0400 Received: from mail-pl1-f172.google.com ([209.85.214.172]:37295 "EHLO mail-pl1-f172.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726603AbeIJWAZ (ORCPT ); Mon, 10 Sep 2018 18:00:25 -0400 Received: by mail-pl1-f172.google.com with SMTP id f1-v6so10021369plt.4 for ; Mon, 10 Sep 2018 10:05:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:message-id:in-reply-to:references:from:subject:fcc :content-transfer-encoding:mime-version:to:cc; bh=8DeR8mfKxdGNHuVI4yZjPCpc4dM/U91ZPGF8eizlR4A=; b=njsDeEAdFQR6sMoTkO+BAOGoekbNJ5E3BrBxLWSikqkc+HqOrkizvedl12zlobs/k/ UILQjL4L1UYMjummrfWZRh5BYnW4sZCP6XLRZsAb6396yOQGSxF3+jW8aB04tZQS91mg xEQBGDWtXaARavm2EmxG1/j2M17LXcNlPGy8cugLfBCmZKvL2Hgu0kUjpjFDLctgKAkg zd+0Y/q01GSvuOnRqmkMvwR9FvHRlLOOIhunkzyOdUCSFSCn+ztuEra3ca2vMuwxx7uf 0DIM4NXAufyYBlCYZwiRdK7dsNz4k63C51a68rrvQMMD6HE5rm53h5f6eNW5ewkI0Fqy RaNw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:message-id:in-reply-to:references:from :subject:fcc:content-transfer-encoding:mime-version:to:cc; bh=8DeR8mfKxdGNHuVI4yZjPCpc4dM/U91ZPGF8eizlR4A=; b=SkNUcc+9/XqiHNnGUb9sdet2zsq8TegZKB2g04m+Y4DwIDhGRyozh0qa8Y7bdvjMkv e6jL6D9rS/NrDI993DLBvksOEl1cdfMWqvMwnXbyS/tcGrwZn4bt4X+4MX7Hw7sCuIH3 GopCJ0i5COVCQriTrhMxrz+WVIWfiEIiywrGLQhsDSuQ8b/G0pvKi+WwFYAfmL0LiSf3 BuU6Ey89KBsx3aPMYpudjeg2qmF/7scvei3ygSawya0FHRhp0+iqZwPGnZQFegLzNDul kFx55TjkM2Vdsv8GzOGhbCnArHuToZK8UV6ex4jCTdpLVfLuiyNdbwh9SCwkPASGc2VJ XnHA== X-Gm-Message-State: APzg51AUn36klr4gs5oFrJi+vMrhQrxAMt3fANOX/hoL26hH2ndiZzkV HDwsWXmKdPUC1rj+vcWV4vmjuOgJ X-Google-Smtp-Source: ANB0VdZnbgCQ8hEB3CsTxDVXR710LQHNPfNA94HDMSfNMcCeQFPyus/lKQnicGCdFatOsdVrTKSMXw== X-Received: by 2002:a17:902:a715:: with SMTP id w21-v6mr23008998plq.61.1536599122341; Mon, 10 Sep 2018 10:05:22 -0700 (PDT) Received: from [127.0.0.1] ([40.112.142.204]) by smtp.gmail.com with ESMTPSA id a192-v6sm27101264pfa.124.2018.09.10.10.05.21 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 10 Sep 2018 10:05:21 -0700 (PDT) Date: Mon, 10 Sep 2018 10:05:21 -0700 (PDT) X-Google-Original-Date: Mon, 10 Sep 2018 17:05:16 GMT Message-Id: In-Reply-To: References: From: "Jeff Hostetler via GitGitGadget" Subject: [PATCH v2 1/2] t0051: test GIT_TRACE to a windows named pipe Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: jeffhost@microsoft.com, Junio C Hamano , Jeff Hostetler Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Jeff Hostetler Create a test-tool helper to create the server side of a windows named pipe, wait for a client connection, and copy data written to the pipe to stdout. Create t0051 test to route GIT_TRACE output of a command to a named pipe using the above test-tool helper. Signed-off-by: Jeff Hostetler --- Makefile | 1 + t/helper/test-tool.c | 3 ++ t/helper/test-tool.h | 3 ++ t/helper/test-windows-named-pipe.c | 72 ++++++++++++++++++++++++++++++ t/t0051-windows-named-pipe.sh | 17 +++++++ 5 files changed, 96 insertions(+) create mode 100644 t/helper/test-windows-named-pipe.c create mode 100755 t/t0051-windows-named-pipe.sh diff --git a/Makefile b/Makefile index e4b503d259..b1b934d295 100644 --- a/Makefile +++ b/Makefile @@ -731,6 +731,7 @@ TEST_BUILTINS_OBJS += test-submodule-config.o TEST_BUILTINS_OBJS += test-subprocess.o TEST_BUILTINS_OBJS += test-urlmatch-normalization.o TEST_BUILTINS_OBJS += test-wildmatch.o +TEST_BUILTINS_OBJS += test-windows-named-pipe.o TEST_BUILTINS_OBJS += test-write-cache.o TEST_PROGRAMS_NEED_X += test-dump-fsmonitor diff --git a/t/helper/test-tool.c b/t/helper/test-tool.c index 805a45de9c..7a9764cd5c 100644 --- a/t/helper/test-tool.c +++ b/t/helper/test-tool.c @@ -41,6 +41,9 @@ static struct test_cmd cmds[] = { { "subprocess", cmd__subprocess }, { "urlmatch-normalization", cmd__urlmatch_normalization }, { "wildmatch", cmd__wildmatch }, +#ifdef GIT_WINDOWS_NATIVE + { "windows-named-pipe", cmd__windows_named_pipe }, +#endif { "write-cache", cmd__write_cache }, }; diff --git a/t/helper/test-tool.h b/t/helper/test-tool.h index 7116ddfb94..01c34fe5e7 100644 --- a/t/helper/test-tool.h +++ b/t/helper/test-tool.h @@ -35,6 +35,9 @@ int cmd__submodule_config(int argc, const char **argv); int cmd__subprocess(int argc, const char **argv); int cmd__urlmatch_normalization(int argc, const char **argv); int cmd__wildmatch(int argc, const char **argv); +#ifdef GIT_WINDOWS_NATIVE +int cmd__windows_named_pipe(int argc, const char **argv); +#endif int cmd__write_cache(int argc, const char **argv); #endif diff --git a/t/helper/test-windows-named-pipe.c b/t/helper/test-windows-named-pipe.c new file mode 100644 index 0000000000..b4b752b01a --- /dev/null +++ b/t/helper/test-windows-named-pipe.c @@ -0,0 +1,72 @@ +#include "test-tool.h" +#include "git-compat-util.h" +#include "strbuf.h" + +#ifdef GIT_WINDOWS_NATIVE +static const char *usage_string = ""; + +#define TEST_BUFSIZE (4096) + +int cmd__windows_named_pipe(int argc, const char **argv) +{ + const char *filename; + struct strbuf pathname = STRBUF_INIT; + int err; + HANDLE h; + BOOL connected; + char buf[TEST_BUFSIZE + 1]; + + if (argc < 2) + goto print_usage; + filename = argv[1]; + if (strchr(filename, '/') || strchr(filename, '\\')) + goto print_usage; + strbuf_addf(&pathname, "//./pipe/%s", filename); + + /* + * Create a single instance of the server side of the named pipe. + * This will allow exactly one client instance to connect to it. + */ + h = CreateNamedPipeA( + pathname.buf, + PIPE_ACCESS_INBOUND | FILE_FLAG_FIRST_PIPE_INSTANCE, + PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT, + PIPE_UNLIMITED_INSTANCES, + TEST_BUFSIZE, TEST_BUFSIZE, 0, NULL); + if (h == INVALID_HANDLE_VALUE) { + err = err_win_to_posix(GetLastError()); + fprintf(stderr, "CreateNamedPipe failed: %s\n", + strerror(err)); + return err; + } + + connected = ConnectNamedPipe(h, NULL) + ? TRUE + : (GetLastError() == ERROR_PIPE_CONNECTED); + if (!connected) { + err = err_win_to_posix(GetLastError()); + fprintf(stderr, "ConnectNamedPipe failed: %s\n", + strerror(err)); + CloseHandle(h); + return err; + } + + while (1) { + DWORD nbr; + BOOL success = ReadFile(h, buf, TEST_BUFSIZE, &nbr, NULL); + if (!success || nbr == 0) + break; + buf[nbr] = 0; + + write(1, buf, nbr); + } + + DisconnectNamedPipe(h); + CloseHandle(h); + return 0; + +print_usage: + fprintf(stderr, "usage: %s %s\n", argv[0], usage_string); + return 1; +} +#endif diff --git a/t/t0051-windows-named-pipe.sh b/t/t0051-windows-named-pipe.sh new file mode 100755 index 0000000000..e3c36341a0 --- /dev/null +++ b/t/t0051-windows-named-pipe.sh @@ -0,0 +1,17 @@ +#!/bin/sh + +test_description='Windows named pipes' + +. ./test-lib.sh + +test_expect_failure MINGW 'o_append write to named pipe' ' + GIT_TRACE="$(pwd)/expect" git status >/dev/null 2>&1 && + { test-tool windows-named-pipe t0051 >actual 2>&1 & } && + pid=$! && + sleep 1 && + GIT_TRACE=//./pipe/t0051 git status >/dev/null 2>warning && + wait $pid && + test_cmp expect actual +' + +test_done From patchwork Mon Sep 10 17:05:22 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Arver via GitGitGadget X-Patchwork-Id: 10594545 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 8C36115E2 for ; Mon, 10 Sep 2018 17:05:26 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 75A9928E96 for ; Mon, 10 Sep 2018 17:05:26 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 69D2728D9E; Mon, 10 Sep 2018 17:05:26 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id F16FD28D9E for ; Mon, 10 Sep 2018 17:05:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728856AbeIJWA0 (ORCPT ); Mon, 10 Sep 2018 18:00:26 -0400 Received: from mail-pl1-f196.google.com ([209.85.214.196]:46775 "EHLO mail-pl1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726603AbeIJWAZ (ORCPT ); Mon, 10 Sep 2018 18:00:25 -0400 Received: by mail-pl1-f196.google.com with SMTP id t19-v6so10019476ply.13 for ; Mon, 10 Sep 2018 10:05:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:message-id:in-reply-to:references:from:subject:fcc :content-transfer-encoding:mime-version:to:cc; bh=uaaCPyujHx/G847/ERRvieUkKT3T50xow363oBxFmpc=; b=eC20yPSL2jcvyVyTkm9Hyk3kQnIzacRAWd0yRMlr+OoVjOfngxXkX7GZPBNntY2brj glTMpTaBFOlwK9VMf1rzm3layBJoGEwvvdDybOtLTmOAZengcMh5ciyXYyOg+1ZKLyVo 2Y1Mg23qL+Gjl3ex0FU3+r+3UVGLtu+9o+I/3swRI/adXFL8Dnv8EHpnYEALFY6dFZjM iWsEbvjrBCwukoTWHt5kTONdaQ4k7Gk574YuRmzAEgdFd43XXuHWEG6Io9OpXVM01b0X AWgCTEpP1r8kK0KpHr/1g0ca21tHHapgkqnxZ20xlgXXd7LuZn7NQpPWn2TN4pnsifZZ uVmw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:message-id:in-reply-to:references:from :subject:fcc:content-transfer-encoding:mime-version:to:cc; bh=uaaCPyujHx/G847/ERRvieUkKT3T50xow363oBxFmpc=; b=cGk6vMRJA0WNS5jLnaX4JN7dmv/ubDitzys3ii5dF43O+ufYtDYJG3dOdGXrfXQUD+ MSHB1lpoO7SpgIRr3XFy5GdnFkQVhfE4N/XZcLj/cbQYuppnGoAecN6QEXPuxASr2RrA Kefn4a5Ah2wuszFPko0Sz1r+0NBZw0QIiEQuO0y6gUzfExCgIv6M0vMAG1pNfVWMiNKP CNbXqm1QSY5vFchiLPuYzPXrXNTEG99YrO6t6WdMZ8mFl7dzRUMxeQ2xKpvaairMt5Gu pKj+9nIzy1QQY4TnylAGC6s9fjVz3Ahs5un5AiCQuRZyJ5TmEzCY0z+9nvomu/esCZkd JW0g== X-Gm-Message-State: APzg51CcOd1Pm3B5Xj60URzb2FLE4rOV8LGbcIx3BZiT2wAa3Ig5sDay DraL/4oJbS+Wb6sxDYICE7bOpS80 X-Google-Smtp-Source: ANB0VdZF8P+RY3xzK3qmxpgN0m58cYqLmeyktnF4uDPYdTOvIdsgjyj1oTquO4O+g5H/2orj0aiyvA== X-Received: by 2002:a17:902:aa83:: with SMTP id d3-v6mr22760973plr.242.1536599123718; Mon, 10 Sep 2018 10:05:23 -0700 (PDT) Received: from [127.0.0.1] ([40.112.142.204]) by smtp.gmail.com with ESMTPSA id h85-v6sm31439536pfk.71.2018.09.10.10.05.22 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 10 Sep 2018 10:05:22 -0700 (PDT) Date: Mon, 10 Sep 2018 10:05:22 -0700 (PDT) X-Google-Original-Date: Mon, 10 Sep 2018 17:05:17 GMT Message-Id: In-Reply-To: References: From: "Jeff Hostetler via GitGitGadget" Subject: [PATCH v2 2/2] mingw: fix mingw_open_append to work with named pipes Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: jeffhost@microsoft.com, Junio C Hamano , Jeff Hostetler Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Jeff Hostetler Signed-off-by: Jeff Hostetler --- compat/mingw.c | 38 ++++++++++++++++++++++++++++++++--- t/t0051-windows-named-pipe.sh | 2 +- 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/compat/mingw.c b/compat/mingw.c index 858ca14a57..f87376b26a 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -341,6 +341,19 @@ int mingw_mkdir(const char *path, int mode) return ret; } +/* + * Calling CreateFile() using FILE_APPEND_DATA and without FILE_WRITE_DATA + * is documented in [1] as opening a writable file handle in append mode. + * (It is believed that) this is atomic since it is maintained by the + * kernel unlike the O_APPEND flag which is racily maintained by the CRT. + * + * [1] https://docs.microsoft.com/en-us/windows/desktop/fileio/file-access-rights-constants + * + * This trick does not appear to work for named pipes. Instead it creates + * a named pipe client handle that cannot be written to. Callers should + * just use the regular _wopen() for them. (And since client handle gets + * bound to a unique server handle, it isn't really an issue.) + */ static int mingw_open_append(wchar_t const *wfilename, int oflags, ...) { HANDLE handle; @@ -360,10 +373,12 @@ static int mingw_open_append(wchar_t const *wfilename, int oflags, ...) NULL, create, FILE_ATTRIBUTE_NORMAL, NULL); if (handle == INVALID_HANDLE_VALUE) return errno = err_win_to_posix(GetLastError()), -1; + /* * No O_APPEND here, because the CRT uses it only to reset the - * file pointer to EOF on write(); but that is not necessary - * for a file created with FILE_APPEND_DATA. + * file pointer to EOF before each write(); but that is not + * necessary (and may lead to races) for a file created with + * FILE_APPEND_DATA. */ fd = _open_osfhandle((intptr_t)handle, O_BINARY); if (fd < 0) @@ -371,6 +386,23 @@ static int mingw_open_append(wchar_t const *wfilename, int oflags, ...) return fd; } +#define IS_SBS(ch) (((ch) == '/') || ((ch) == '\\')) +/* + * Does the pathname map to the local named pipe filesystem? + * That is, does it have a "//./pipe/" prefix? + */ +static int mingw_is_local_named_pipe_path(const char *filename) +{ + return (IS_SBS(filename[0]) && + IS_SBS(filename[1]) && + filename[2] == '.' && + IS_SBS(filename[3]) && + !strncasecmp(filename+4, "pipe", 4) && + IS_SBS(filename[8]) && + filename[9]); +} +#undef IS_SBS + int mingw_open (const char *filename, int oflags, ...) { typedef int (*open_fn_t)(wchar_t const *wfilename, int oflags, ...); @@ -387,7 +419,7 @@ int mingw_open (const char *filename, int oflags, ...) if (filename && !strcmp(filename, "/dev/null")) filename = "nul"; - if (oflags & O_APPEND) + if ((oflags & O_APPEND) && !mingw_is_local_named_pipe_path(filename)) open_fn = mingw_open_append; else open_fn = _wopen; diff --git a/t/t0051-windows-named-pipe.sh b/t/t0051-windows-named-pipe.sh index e3c36341a0..10ac92d225 100755 --- a/t/t0051-windows-named-pipe.sh +++ b/t/t0051-windows-named-pipe.sh @@ -4,7 +4,7 @@ test_description='Windows named pipes' . ./test-lib.sh -test_expect_failure MINGW 'o_append write to named pipe' ' +test_expect_success MINGW 'o_append write to named pipe' ' GIT_TRACE="$(pwd)/expect" git status >/dev/null 2>&1 && { test-tool windows-named-pipe t0051 >actual 2>&1 & } && pid=$! &&