From 357d28a256bfe11d9a4a9b2cf4f28d97b42ba68d Mon Sep 17 00:00:00 2001 From: number201724 Date: Fri, 21 Oct 2022 21:14:48 +0800 Subject: [PATCH] win,pipe: improve method of obtaining pid for ipc (#3765) In the old version of uv_pipe_open, the parent process ID is used always. If the open pipe is used in the same process, the parent process will be obtained incorrectly. Now we first get the client pid and compare it with its own pid. If it is the same, then get the server pid. If the two are the same, then the pipe is from the same process. Fixes: https://github.com/libuv/libuv/issues/3766 --- src/win/pipe.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/win/pipe.c b/src/win/pipe.c index 1babe05c..b9fa986e 100644 --- a/src/win/pipe.c +++ b/src/win/pipe.c @@ -1639,9 +1639,13 @@ static DWORD uv__pipe_get_ipc_remote_pid(uv_pipe_t* handle) { /* If the both ends of the IPC pipe are owned by the same process, * the remote end pid may not yet be set. If so, do it here. * TODO: this is weird; it'd probably better to use a handshake. */ - if (*pid == 0) - *pid = GetCurrentProcessId(); - + if (*pid == 0) { + GetNamedPipeClientProcessId(handle->handle, pid); + if (*pid == GetCurrentProcessId()) { + GetNamedPipeServerProcessId(handle->handle, pid); + } + } + return *pid; } @@ -2342,7 +2346,10 @@ int uv_pipe_open(uv_pipe_t* pipe, uv_file file) { if (pipe->ipc) { assert(!(pipe->flags & UV_HANDLE_NON_OVERLAPPED_PIPE)); - pipe->pipe.conn.ipc_remote_pid = uv_os_getppid(); + GetNamedPipeClientProcessId(os_handle, &pipe->pipe.conn.ipc_remote_pid); + if (pipe->pipe.conn.ipc_remote_pid == GetCurrentProcessId()) { + GetNamedPipeServerProcessId(os_handle, &pipe->pipe.conn.ipc_remote_pid); + } assert(pipe->pipe.conn.ipc_remote_pid != (DWORD)(uv_pid_t) -1); } return 0;