Skip to content

Commit

Permalink
Cygwin: pipe: Give up to use query_hdl for non-cygwin apps.
Browse files Browse the repository at this point in the history
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.

Addresses: msys2/msys2-runtime#202

Backported-from: f6be372 (Cygwin: pipe: Give up to use query_hdl for non-cygwin apps., 2024-03-03)
Fixes: b531d6b ("Cygwin: pipe: Introduce temporary query_hdl.")
Reported-by: Alisa Sireneva, Johannes Schindelin <Johannes.Schindelin@gmx.de>
Reviewed-by: Corinna Vinschen <corinna@vinschen.de>
Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp>
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
  • Loading branch information
tyan0 authored and dscho committed Jul 9, 2024
1 parent d52d8f7 commit a4bd6e9
Showing 1 changed file with 13 additions and 44 deletions.
57 changes: 13 additions & 44 deletions winsup/cygwin/fhandler/pipe.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1207,53 +1207,24 @@ HANDLE
fhandler_pipe::get_query_hdl_per_process (WCHAR *name,
OBJECT_NAME_INFORMATION *ntfn)
{
NTSTATUS status;
ULONG len;
DWORD n_process = 256;
PSYSTEM_PROCESS_INFORMATION spi;
do
{ /* Enumerate processes */
DWORD nbytes = n_process * sizeof (SYSTEM_PROCESS_INFORMATION);
spi = (PSYSTEM_PROCESS_INFORMATION) HeapAlloc (GetProcessHeap (),
0, nbytes);
if (!spi)
return NULL;
status = NtQuerySystemInformation (SystemProcessInformation,
spi, nbytes, &len);
if (NT_SUCCESS (status))
break;
HeapFree (GetProcessHeap (), 0, spi);
n_process *= 2;
}
while (n_process < (1L<<20) && status == STATUS_INFO_LENGTH_MISMATCH);
if (!NT_SUCCESS (status))
return NULL;
winpids pids ((DWORD) 0);

/* In most cases, it is faster to check the processes in reverse order.
To do this, store PIDs into an array. */
DWORD *proc_pids = (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 = (LONG) pids.npids - 1; i >= 0; i--)
{
HeapFree (GetProcessHeap (), 0, spi);
return NULL;
}
PSYSTEM_PROCESS_INFORMATION p = spi;
n_process = 0;
while (true)
{
proc_pids[n_process++] = (DWORD)(intptr_t) p->UniqueProcessId;
if (!p->NextEntryOffset)
break;
p = (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 = pids[i];
if (!p || ISSTATE (p, PID_NOTCYGWIN))
continue;

for (LONG i = (LONG) n_process - 1; i >= 0; i--)
{
HANDLE proc = OpenProcess (PROCESS_DUP_HANDLE
| PROCESS_QUERY_INFORMATION,
0, proc_pids[i]);
0, p->dwProcessId);
if (!proc)
continue;

Expand Down Expand Up @@ -1317,7 +1288,6 @@ fhandler_pipe::get_query_hdl_per_process (WCHAR *name,
query_hdl_proc = proc;
query_hdl_value = (HANDLE)(intptr_t) phi->Handles[j].HandleValue;
HeapFree (GetProcessHeap (), 0, phi);
HeapFree (GetProcessHeap (), 0, proc_pids);
return h;
}
close_handle:
Expand All @@ -1327,7 +1297,6 @@ fhandler_pipe::get_query_hdl_per_process (WCHAR *name,
close_proc:
CloseHandle (proc);
}
HeapFree (GetProcessHeap (), 0, proc_pids);
return NULL;
}

Expand Down

0 comments on commit a4bd6e9

Please sign in to comment.