Thread (41 messages) 41 messages, 4 authors, 2022-08-20

Re: [PATCH v2] pipe_command(): mark stdin descriptor as non-blocking

From: René Scharfe <hidden>
Date: 2022-08-07 10:16:01
Subsystem: the rest · Maintainer: Linus Torvalds

Am 05.08.2022 um 17:36 schrieb Jeff King:
On Wed, Aug 03, 2022 at 11:56:13PM +0200, René Scharfe wrote:
quoted
quoted
 int enable_nonblock(int fd)
 {
+	DWORD mode;
+	HANDLE handle = winansi_get_osfhandle(fd);
+	if (!handle)
+		return -1;
+	if (!GetNamedPipeHandleState(handle, &mode, NULL, NULL, NULL, NULL, 0))
+		return -1;
+	if (mode & PIPE_NOWAIT)
+		return 0;
+	mode |= PIPE_NOWAIT;
+	if (!SetNamedPipeHandleState(handle, &mode, NULL, NULL))
+		return -1;
 	return 0;
 }
This looks plausibly correct to me. ;) We might want to change the name
of the compat layer to enable_pipe_nonblock(), since one assumes from
the function names this only works for pipes.
Or how about this?  Squashable.  Needs testing.
--- >8 ---
Subject: [PATCH] nonblock: support Windows

Implement enable_nonblock() using the Windows API.  Supports only named
and anonymous pipes for now, which suffices for the current caller.

Signed-off-by: René Scharfe <redacted>
---
 compat/nonblock.c | 31 +++++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)
diff --git a/compat/nonblock.c b/compat/nonblock.c
index 897c099010..78923cd2c3 100644
--- a/compat/nonblock.c
+++ b/compat/nonblock.c
@@ -14,9 +14,40 @@ int enable_nonblock(int fd)

 #else

+#ifdef GIT_WINDOWS_NATIVE
+
+#include "win32.h"
+
+int enable_nonblock(int fd)
+{
+	HANDLE h = (HANDLE)_get_osfhandle(fd);
+	DWORD mode;
+	DWORD type = GetFileType(h);
+	if (type == FILE_TYPE_UNKNOWN && GetLastError() != NO_ERROR) {
+		errno = EBADF;
+		return -1;
+	}
+	if (type != FILE_TYPE_PIPE)
+		BUG("unsupported file type: %lu", type);
+	if (!GetNamedPipeHandleState(h, &mode, NULL, NULL, NULL, NULL, 0)) {
+		errno = err_win_to_posix(GetLastError());
+		return -1;
+	}
+	mode |= PIPE_NOWAIT;
+	if (!SetNamedPipeHandleState(h, &mode, NULL, NULL)) {
+		errno = err_win_to_posix(GetLastError());
+		return -1;
+	}
+	return 0;
+}
+
+#else
+
 int enable_nonblock(int fd)
 {
 	return 0;
 }

 #endif
+
+#endif
--
2.37.1.windows.1
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help