From 998027fe7a7a56af9507341266c9fcadee66beb9 Mon Sep 17 00:00:00 2001 From: Takashi Yano Date: Tue, 5 Mar 2024 23:34:21 +0900 Subject: [PATCH] Cygwin: pipe: Simplify chhecking procedure of query_hdl. This patch eliminates verbose NtQueryObject() calls in the procedure to get query_hdl by storing pipe name into fhandler_base::pc when the pipe is created. fhandler_pipe::temporary_query_hdl() uses the storedpipe name rather than the name retrieved by NtQueryObject(). Suggested-by: Corinna Vinschen Signed-off-by: Takashi Yano --- winsup/cygwin/fhandler/pipe.cc | 39 ++++++++++++------------- winsup/cygwin/local_includes/fhandler.h | 3 -- 2 files changed, 19 insertions(+), 23 deletions(-) diff --git a/winsup/cygwin/fhandler/pipe.cc b/winsup/cygwin/fhandler/pipe.cc index c877d89d7..0611dd1c3 100644 --- a/winsup/cygwin/fhandler/pipe.cc +++ b/winsup/cygwin/fhandler/pipe.cc @@ -93,6 +93,19 @@ fhandler_pipe::init (HANDLE f, DWORD a, mode_t mode, int64_t uniq_id) even with FILE_SYNCHRONOUS_IO_NONALERT. */ set_pipe_non_blocking (get_device () == FH_PIPER ? true : is_nonblocking ()); + + /* Store pipe name to path_conv pc for query_hdl check */ + if (get_dev () == FH_PIPEW) + { + ULONG len; + tmp_pathbuf tp; + OBJECT_NAME_INFORMATION *ntfn = (OBJECT_NAME_INFORMATION *) tp.w_get (); + NTSTATUS status = NtQueryObject (f, ObjectNameInformation, ntfn, + 65536, &len); + if (NT_SUCCESS (status) && ntfn->Name.Buffer) + pc.set_nt_native_path (&ntfn->Name); + } + return 1; } @@ -1149,6 +1162,9 @@ fhandler_pipe::temporary_query_hdl () tmp_pathbuf tp; OBJECT_NAME_INFORMATION *ntfn = (OBJECT_NAME_INFORMATION *) tp.w_get (); + UNICODE_STRING *name = pc.get_nt_native_path (NULL); + name->Buffer[name->Length / sizeof (WCHAR)] = L'\0'; + /* Try process handle opened and pipe handle value cached first in order to reduce overhead. */ if (query_hdl_proc && query_hdl_value) @@ -1161,14 +1177,7 @@ fhandler_pipe::temporary_query_hdl () status = NtQueryObject (h, ObjectNameInformation, ntfn, 65536, &len); if (!NT_SUCCESS (status) || !ntfn->Name.Buffer) goto hdl_err; - ntfn->Name.Buffer[ntfn->Name.Length / sizeof (WCHAR)] = L'\0'; - uint64_t key; - DWORD pid; - LONG id; - if (swscanf (ntfn->Name.Buffer, - L"\\Device\\NamedPipe\\%llx-%u-pipe-nt-0x%x", - &key, &pid, &id) == 3 && - key == pipename_key && pid == pipename_pid && id == pipename_id) + if (RtlEqualUnicodeString (name, &ntfn->Name, FALSE)) return h; hdl_err: CloseHandle (h); @@ -1178,19 +1187,9 @@ cache_err: query_hdl_value = NULL; } - status = NtQueryObject (get_handle (), ObjectNameInformation, ntfn, - 65536, &len); - if (!NT_SUCCESS (status) || !ntfn->Name.Buffer) + if (name->Length == 0 || name->Buffer == NULL) return NULL; /* Non cygwin pipe? */ - WCHAR name[MAX_PATH]; - int namelen = min (ntfn->Name.Length / sizeof (WCHAR), MAX_PATH-1); - memcpy (name, ntfn->Name.Buffer, namelen * sizeof (WCHAR)); - name[namelen] = L'\0'; - if (swscanf (name, L"\\Device\\NamedPipe\\%llx-%u-pipe-nt-0x%x", - &pipename_key, &pipename_pid, &pipename_id) != 3) - return NULL; /* Non cygwin pipe? */ - - return get_query_hdl_per_process (name, ntfn); /* Since Win8 */ + return get_query_hdl_per_process (name->Buffer, ntfn); /* Since Win8 */ } HANDLE diff --git a/winsup/cygwin/local_includes/fhandler.h b/winsup/cygwin/local_includes/fhandler.h index 6ddf37370..028eb49d0 100644 --- a/winsup/cygwin/local_includes/fhandler.h +++ b/winsup/cygwin/local_includes/fhandler.h @@ -1216,9 +1216,6 @@ private: HANDLE query_hdl_proc; HANDLE query_hdl_value; HANDLE query_hdl_close_req_evt; - uint64_t pipename_key; - DWORD pipename_pid; - LONG pipename_id; void release_select_sem (const char *); HANDLE get_query_hdl_per_process (WCHAR *, OBJECT_NAME_INFORMATION *); public: -- 2.43.0