From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 7868) id 041873858D20; Tue, 5 Mar 2024 17:50:08 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 041873858D20 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1709661009; bh=AjhhFCk9JDlhaERt7vlWqKwYNKKR1zUaseDTJ0W/30c=; h=From:To:Subject:Date:From; b=HIkab23btxkagBcwP3OmKv8T09qvKZeGmjsWiszaoo7yWH0bzuDvlDx1aRDFbA4gR /RDcra86L6bOWIm/52CYthle1EDt3lCQ18yjbcweUzYQ4GAZUDc6qHHHBXImD6/jBm nOpjRZs27EZw5Yb6yVW0ZZAaO7a54Zx+d42PI/88= Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Takashi Yano To: cygwin-cvs@sourceware.org Subject: [newlib-cygwin] Cygwin: pipe: Give up to use query_hdl for non-cygwin apps. X-Act-Checkin: newlib-cygwin X-Git-Author: Takashi Yano X-Git-Refname: refs/heads/master X-Git-Oldrev: b160b690b6ace93ee4225f14a9287549e37f4a71 X-Git-Newrev: 46bb999a8945f4d3243f560b5cbd71e18113fb01 Message-Id: <20240305175009.041873858D20@sourceware.org> Date: Tue, 5 Mar 2024 17:50:08 +0000 (GMT) List-Id: https://sourceware.org/git/gitweb.cgi?p=3Dnewlib-cygwin.git;h=3D46bb999a894= 5f4d3243f560b5cbd71e18113fb01 commit 46bb999a8945f4d3243f560b5cbd71e18113fb01 Author: Takashi Yano Date: Sun Mar 3 13:44:17 2024 +0900 Cygwin: pipe: Give up to use query_hdl for non-cygwin apps. =20 Non-cygwin app may call ReadFile() for empty pipe, which makes NtQueryObject() for ObjectNameInformation block in fhandler_pipe:: get_query_hdl_per_process. Therefore, do not to try to get query_hdl for non-cygwin apps. =20 Addresses: https://github.com/msys2/msys2-runtime/issues/202 =20 Fixes: b531d6b06eeb ("Cygwin: pipe: Introduce temporary query_hdl.") Reported-by: Alisa Sireneva, Johannes Schindelin Reviewed-by: Corinna Vinschen Signed-off-by: Takashi Yano Diff: --- winsup/cygwin/fhandler/pipe.cc | 57 ++++++++++----------------------------= ---- winsup/cygwin/release/3.5.2 | 4 +++ 2 files changed, 17 insertions(+), 44 deletions(-) diff --git a/winsup/cygwin/fhandler/pipe.cc b/winsup/cygwin/fhandler/pipe.cc index 1a97108b5..c877d89d7 100644 --- a/winsup/cygwin/fhandler/pipe.cc +++ b/winsup/cygwin/fhandler/pipe.cc @@ -1197,53 +1197,24 @@ HANDLE fhandler_pipe::get_query_hdl_per_process (WCHAR *name, OBJECT_NAME_INFORMATION *ntfn) { - NTSTATUS status; - ULONG len; - DWORD n_process =3D 256; - PSYSTEM_PROCESS_INFORMATION spi; - do - { /* Enumerate processes */ - DWORD nbytes =3D n_process * sizeof (SYSTEM_PROCESS_INFORMATION); - spi =3D (PSYSTEM_PROCESS_INFORMATION) HeapAlloc (GetProcessHeap (), - 0, nbytes); - if (!spi) - return NULL; - status =3D NtQuerySystemInformation (SystemProcessInformation, - spi, nbytes, &len); - if (NT_SUCCESS (status)) - break; - HeapFree (GetProcessHeap (), 0, spi); - n_process *=3D 2; - } - while (n_process < (1L<<20) && status =3D=3D STATUS_INFO_LENGTH_MISMATCH= ); - if (!NT_SUCCESS (status)) - return NULL; + winpids pids ((DWORD) 0); =20 - /* In most cases, it is faster to check the processes in reverse order. - To do this, store PIDs into an array. */ - DWORD *proc_pids =3D (DWORD *) HeapAlloc (GetProcessHeap (), 0, - n_process * sizeof (DWORD)); - if (!proc_pids) + /* In most cases, it is faster to check the processes in reverse order. = */ + for (LONG i =3D (LONG) pids.npids - 1; i >=3D 0; i--) { - HeapFree (GetProcessHeap (), 0, spi); - return NULL; - } - PSYSTEM_PROCESS_INFORMATION p =3D spi; - n_process =3D 0; - while (true) - { - proc_pids[n_process++] =3D (DWORD)(intptr_t) p->UniqueProcessId; - if (!p->NextEntryOffset) - break; - p =3D (PSYSTEM_PROCESS_INFORMATION) ((char *) p + p->NextEntryOffset= ); - } - HeapFree (GetProcessHeap (), 0, spi); + NTSTATUS status; + ULONG len; + + /* Non-cygwin app may call ReadFile() for empty pipe, which makes + NtQueryObject() for ObjectNameInformation block. Therefore, do + not try to get query_hdl for non-cygwin apps. */ + _pinfo *p =3D pids[i]; + if (!p || ISSTATE (p, PID_NOTCYGWIN)) + continue; =20 - for (LONG i =3D (LONG) n_process - 1; i >=3D 0; i--) - { HANDLE proc =3D OpenProcess (PROCESS_DUP_HANDLE | PROCESS_QUERY_INFORMATION, - 0, proc_pids[i]); + 0, p->dwProcessId); if (!proc) continue; =20 @@ -1307,7 +1278,6 @@ fhandler_pipe::get_query_hdl_per_process (WCHAR *name, query_hdl_proc =3D proc; query_hdl_value =3D (HANDLE)(intptr_t) phi->Handles[j].HandleValue; HeapFree (GetProcessHeap (), 0, phi); - HeapFree (GetProcessHeap (), 0, proc_pids); return h; } close_handle: @@ -1317,6 +1287,5 @@ close_handle: close_proc: CloseHandle (proc); } - HeapFree (GetProcessHeap (), 0, proc_pids); return NULL; } diff --git a/winsup/cygwin/release/3.5.2 b/winsup/cygwin/release/3.5.2 index fd3c768de..e6782c1c0 100644 --- a/winsup/cygwin/release/3.5.2 +++ b/winsup/cygwin/release/3.5.2 @@ -9,3 +9,7 @@ Fixes: - Fix a race issue between console open() and close() which is caused by state mismatch between con.owner and console attaching state. Addresses: https://cygwin.com/pipermail/cygwin/2024-March/255575.html + +- Fix a problem that select() call for write-side of a pipe possibly + hangs with non-cygwin reader. + Addresses: https://github.com/msys2/msys2-runtime/issues/202