Skip to content

Commit 36fa5e8

Browse files
committed
win,unix: add uv_pipe_open_ex
uv_pipe_open() interprets its argument as a CRT file descriptor on Windows, so a raw HANDLE without one (e.g. an inherited anonymous pipe) fails. uv_pipe_open_ex() accepts the native OS handle directly. On Unix it is equivalent to uv_pipe_open(). Refs: nodejs/node#57288 Signed-off-by: PickBas <[email protected]>
1 parent ea493f1 commit 36fa5e8

8 files changed

Lines changed: 171 additions & 4 deletions

File tree

‎CMakeLists.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,7 @@ if(LIBUV_BUILD_TESTS)
602602
test/test-pipe-connect-multiple.c
603603
test/test-pipe-connect-prepare.c
604604
test/test-pipe-getsockname.c
605+
test/test-pipe-open-ex.c
605606
test/test-pipe-pending-instances.c
606607
test/test-pipe-sendmsg.c
607608
test/test-pipe-server-close.c

‎Makefile.am‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ test_run_tests_SOURCES = test/blackhole-server.c \
226226
test/test-pipe-connect-multiple.c \
227227
test/test-pipe-connect-prepare.c \
228228
test/test-pipe-getsockname.c \
229+
test/test-pipe-open-ex.c \
229230
test/test-pipe-pending-instances.c \
230231
test/test-pipe-sendmsg.c \
231232
test/test-pipe-server-close.c \

‎docs/src/pipe.rst‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,25 @@ API
5151
The passed file descriptor or HANDLE is not checked for its type, but
5252
it's required that it represents a valid pipe.
5353
54+
.. c:function:: int uv_pipe_open_ex(uv_pipe_t* handle, uv_os_fd_t os_handle)
55+
56+
Open an existing OS handle (a ``HANDLE`` on Windows, a file descriptor on
57+
Unix) as a pipe.
58+
59+
Unlike :c:func:`uv_pipe_open`, which interprets its argument as a CRT file
60+
descriptor on Windows, this accepts the native OS handle directly. Use it
61+
when you hold a raw ``HANDLE`` that has no associated CRT file descriptor,
62+
such as an inherited anonymous pipe handle.
63+
64+
The handle is owned by the pipe after a successful call and is closed when
65+
the pipe is closed with :c:func:`uv_close`.
66+
67+
.. versionadded:: 1.53.0
68+
69+
.. note::
70+
The passed handle is not checked for its type, but it's required that
71+
it represents a valid pipe.
72+
5473
.. c:function:: int uv_pipe_bind(uv_pipe_t* handle, const char* name)
5574
5675
Bind the pipe to a file path (Unix) or a name (Windows).

‎include/uv.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -875,6 +875,7 @@ struct uv_pipe_s {
875875

876876
UV_EXTERN int uv_pipe_init(uv_loop_t*, uv_pipe_t* handle, int ipc);
877877
UV_EXTERN int uv_pipe_open(uv_pipe_t*, uv_file file);
878+
UV_EXTERN int uv_pipe_open_ex(uv_pipe_t* handle, uv_os_fd_t os_handle);
878879
UV_EXTERN int uv_pipe_bind(uv_pipe_t* handle, const char* name);
879880
UV_EXTERN int uv_pipe_bind2(uv_pipe_t* handle,
880881
const char* name,

‎src/unix/pipe.c‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,14 @@ int uv_pipe_open(uv_pipe_t* handle, uv_file fd) {
240240
}
241241

242242

243+
int uv_pipe_open_ex(uv_pipe_t* handle, uv_os_fd_t os_handle) {
244+
/* On Unix uv_os_fd_t is a file descriptor, so this is identical to
245+
* uv_pipe_open(). The separate entry point exists for Windows, where the
246+
* native handle and the CRT file descriptor are distinct namespaces. */
247+
return uv_pipe_open(handle, os_handle);
248+
}
249+
250+
243251
void uv_pipe_connect(uv_connect_t* req,
244252
uv_pipe_t* handle,
245253
const char* name,

‎src/win/pipe.c‎

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2439,12 +2439,14 @@ static void eof_timer_close_cb(uv_handle_t* handle) {
24392439
}
24402440

24412441

2442-
int uv_pipe_open(uv_pipe_t* pipe, uv_file file) {
2443-
HANDLE os_handle = uv__get_osfhandle(file);
2442+
static int uv__pipe_open_handle(uv_pipe_t* pipe,
2443+
HANDLE os_handle,
2444+
uv_file file) {
24442445
NTSTATUS nt_status;
24452446
IO_STATUS_BLOCK io_status;
24462447
FILE_ACCESS_INFORMATION access;
24472448
DWORD duplex_flags = 0;
2449+
int duplicated = 0;
24482450
int err;
24492451

24502452
if (os_handle == INVALID_HANDLE_VALUE)
@@ -2461,8 +2463,10 @@ int uv_pipe_open(uv_pipe_t* pipe, uv_file file) {
24612463
* We could also opt to use the original OS handle and just never close it,
24622464
* but then there would be no reliable way to cancel pending read operations
24632465
* upon close.
2466+
* A file value of -1 means there is no associated fd (e.g. when the handle
2467+
* was passed directly via uv_pipe_open_ex), so skip the duplication.
24642468
*/
2465-
if (file <= 2) {
2469+
if (file >= 0 && file <= 2) {
24662470
if (!DuplicateHandle(INVALID_HANDLE_VALUE,
24672471
os_handle,
24682472
INVALID_HANDLE_VALUE,
@@ -2473,6 +2477,7 @@ int uv_pipe_open(uv_pipe_t* pipe, uv_file file) {
24732477
return uv_translate_sys_error(GetLastError());
24742478
assert(os_handle != INVALID_HANDLE_VALUE);
24752479
file = -1;
2480+
duplicated = 1;
24762481
}
24772482

24782483
/* Determine what kind of permissions we have on this handle.
@@ -2505,7 +2510,10 @@ int uv_pipe_open(uv_pipe_t* pipe, uv_file file) {
25052510
file,
25062511
duplex_flags);
25072512
if (err) {
2508-
if (file == -1)
2513+
/* Only close the handle if we duplicated it ourselves; a handle passed
2514+
* in by the caller (uv_pipe_open_ex) remains owned by the caller on
2515+
* failure. */
2516+
if (duplicated)
25092517
CloseHandle(os_handle);
25102518
return err;
25112519
}
@@ -2522,6 +2530,18 @@ int uv_pipe_open(uv_pipe_t* pipe, uv_file file) {
25222530
}
25232531

25242532

2533+
int uv_pipe_open(uv_pipe_t* pipe, uv_file file) {
2534+
return uv__pipe_open_handle(pipe, uv__get_osfhandle(file), file);
2535+
}
2536+
2537+
2538+
int uv_pipe_open_ex(uv_pipe_t* pipe, uv_os_fd_t os_handle) {
2539+
/* Pass file == -1: there is no CRT fd backing this handle, so libuv takes
2540+
* ownership of os_handle and closes it directly on uv_close(). */
2541+
return uv__pipe_open_handle(pipe, os_handle, -1);
2542+
}
2543+
2544+
25252545
static int uv__pipe_getname(const uv_pipe_t* handle, char* buffer, size_t* size) {
25262546
NTSTATUS nt_status;
25272547
IO_STATUS_BLOCK io_status;

‎test/test-list.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,7 @@ TEST_DECLARE (pipe_ref4)
284284
TEST_DECLARE (pipe_close_stdout_read_stdin)
285285
#endif
286286
TEST_DECLARE (pipe_set_non_blocking)
287+
TEST_DECLARE (pipe_open_ex)
287288
TEST_DECLARE (pipe_set_chmod)
288289
TEST_DECLARE (process_ref)
289290
TEST_DECLARE (process_priority)
@@ -647,6 +648,7 @@ TASK_LIST_START
647648
#endif
648649
/* Seems to be either about 0.5s or 5s, depending on the OS. */
649650
TEST_ENTRY_CUSTOM (pipe_set_non_blocking, 0, 0, 20000)
651+
TEST_ENTRY (pipe_open_ex)
650652
TEST_ENTRY (pipe_set_chmod)
651653
TEST_ENTRY (tty)
652654
#ifdef _WIN32

‎test/test-pipe-open-ex.c‎

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/* Copyright libuv project contributors. All rights reserved.
2+
*
3+
* Permission to use, copy, modify, and/or distribute this software for any
4+
* purpose with or without fee is hereby granted, provided that the above
5+
* copyright notice and this permission notice appear in all copies.
6+
*
7+
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8+
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9+
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
10+
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11+
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
12+
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
13+
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
14+
*/
15+
16+
#include "uv.h"
17+
#include "task.h"
18+
19+
#include <string.h> /* memcmp, memset */
20+
#ifndef _WIN32
21+
#include <unistd.h> /* close */
22+
#endif
23+
24+
/* Verify that uv_pipe_open_ex() accepts a native OS handle directly.
25+
*
26+
* On Windows uv_pipe_open() treats its argument as a CRT file descriptor and
27+
* converts it to a HANDLE via _get_osfhandle(). A raw HANDLE (e.g. an
28+
* inherited anonymous pipe) is in a different namespace and fails with EBADF.
29+
* uv_pipe_open_ex() takes the HANDLE as-is. On Unix uv_os_fd_t is the file
30+
* descriptor itself, so the call is equivalent to uv_pipe_open().
31+
*/
32+
33+
#ifdef _WIN32
34+
static void write_cb(uv_write_t* req, int status) {
35+
ASSERT_OK(status);
36+
req->handle = NULL; /* signal completion of write_cb */
37+
}
38+
#endif
39+
40+
41+
TEST_IMPL(pipe_open_ex) {
42+
uv_pipe_t pipe_handle;
43+
uv_os_fd_t os_handle;
44+
uv_buf_t buf;
45+
char data[] = "test data";
46+
char readbuf[64];
47+
#ifdef _WIN32
48+
HANDLE read_handle;
49+
DWORD bytes_read;
50+
uv_write_t write_req;
51+
#else
52+
uv_fs_t req;
53+
uv_file fd[2];
54+
int n;
55+
#endif
56+
57+
ASSERT_OK(uv_pipe_init(uv_default_loop(), &pipe_handle, 0));
58+
59+
#ifdef _WIN32
60+
/* CreatePipe() returns raw anonymous pipe HANDLEs that have no associated
61+
* CRT file descriptor - the exact scenario uv_pipe_open_ex() exists for.
62+
* The handles are synchronous, which also exercises the non-overlapped
63+
* pipe code path. */
64+
ASSERT(CreatePipe(&read_handle, &os_handle, NULL, 0));
65+
#else
66+
ASSERT_OK(uv_pipe(fd, 0, 0));
67+
os_handle = fd[1];
68+
#endif
69+
70+
/* Open the write end directly from its native OS handle. The handle is
71+
* owned by pipe_handle after this call. */
72+
ASSERT_OK(uv_pipe_open_ex(&pipe_handle, os_handle));
73+
ASSERT_OK(uv_stream_set_blocking((uv_stream_t*) &pipe_handle, 1));
74+
75+
buf = uv_buf_init(data, sizeof(data));
76+
#ifdef _WIN32
77+
/* uv_try_write() is not implemented for pipes on Windows. */
78+
ASSERT_OK(uv_write(&write_req,
79+
(uv_stream_t*) &pipe_handle,
80+
&buf,
81+
1,
82+
write_cb));
83+
ASSERT_NOT_NULL(write_req.handle);
84+
ASSERT_OK(uv_run(uv_default_loop(), UV_RUN_ONCE));
85+
ASSERT_NULL(write_req.handle); /* signaled completion of write_cb */
86+
#else
87+
n = uv_try_write((uv_stream_t*) &pipe_handle, &buf, 1);
88+
ASSERT_EQ(n, (int) sizeof(data));
89+
#endif
90+
91+
/* Read the data back from the other end and verify it round-trips. */
92+
memset(readbuf, 0, sizeof(readbuf));
93+
#ifdef _WIN32
94+
ASSERT(ReadFile(read_handle, readbuf, sizeof(readbuf), &bytes_read, NULL));
95+
ASSERT_EQ(bytes_read, (DWORD) sizeof(data));
96+
#else
97+
buf = uv_buf_init(readbuf, sizeof(readbuf));
98+
n = uv_fs_read(NULL, &req, fd[0], &buf, 1, -1, NULL);
99+
ASSERT_EQ(n, (int) sizeof(data));
100+
uv_fs_req_cleanup(&req);
101+
#endif
102+
ASSERT_OK(memcmp(readbuf, data, sizeof(data)));
103+
104+
uv_close((uv_handle_t*) &pipe_handle, NULL);
105+
ASSERT_OK(uv_run(uv_default_loop(), UV_RUN_DEFAULT));
106+
107+
#ifdef _WIN32
108+
ASSERT(CloseHandle(read_handle));
109+
#else
110+
ASSERT_OK(close(fd[0]));
111+
#endif
112+
113+
MAKE_VALGRIND_HAPPY(uv_default_loop());
114+
return 0;
115+
}

0 commit comments

Comments
 (0)