public inbox for cygwin-cvs@sourceware.org
help / color / mirror / Atom feed
* [newlib-cygwin] Cygwin: pipe: Simplify chhecking procedure of query_hdl.
@ 2024-03-06 13:06 Takashi Yano
  0 siblings, 0 replies; only message in thread
From: Takashi Yano @ 2024-03-06 13:06 UTC (permalink / raw)
  To: cygwin-cvs

https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;h=c2310061c66a100653fe10762859b8adb6a3d357

commit c2310061c66a100653fe10762859b8adb6a3d357
Author: Takashi Yano <takashi.yano@nifty.ne.jp>
Date:   Tue Mar 5 23:34:21 2024 +0900

    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 <corinna@vinschen.de>
    Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp>

Diff:
---
 winsup/cygwin/fhandler/pipe.cc          | 47 ++++++++++++++++-----------------
 winsup/cygwin/local_includes/fhandler.h |  5 +---
 2 files changed, 24 insertions(+), 28 deletions(-)

diff --git a/winsup/cygwin/fhandler/pipe.cc b/winsup/cygwin/fhandler/pipe.cc
index c877d89d7..29d3b41d9 100644
--- a/winsup/cygwin/fhandler/pipe.cc
+++ b/winsup/cygwin/fhandler/pipe.cc
@@ -93,6 +93,21 @@ 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)
+    {
+      UNICODE_STRING name;
+      WCHAR pipename_buf[MAX_PATH];
+      __small_swprintf (pipename_buf, L"%S%S-%u-pipe-nt-%p",
+			&ro_u_npfs, &cygheap->installation_key,
+			GetCurrentProcessId (), unique_id >> 32);
+      name.Length = wcslen (pipename_buf) * sizeof (WCHAR);
+      name.MaximumLength = sizeof (pipename_buf);
+      name.Buffer = pipename_buf;
+      pc.set_nt_native_path (&name);
+    }
+
   return 1;
 }
 
@@ -1149,6 +1164,8 @@ 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);
+
   /* Try process handle opened and pipe handle value cached first
      in order to reduce overhead. */
   if (query_hdl_proc && query_hdl_value)
@@ -1161,14 +1178,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,24 +1188,13 @@ cache_err:
       query_hdl_value = NULL;
     }
 
-  status = NtQueryObject (get_handle (), ObjectNameInformation, ntfn,
-			  65536, &len);
-  if (!NT_SUCCESS (status) || !ntfn->Name.Buffer)
-    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)
+  if (name->Length == 0 || name->Buffer == NULL)
     return NULL; /* Non cygwin pipe? */
-
-  return get_query_hdl_per_process (name, ntfn); /* Since Win8 */
+  return get_query_hdl_per_process (ntfn); /* Since Win8 */
 }
 
 HANDLE
-fhandler_pipe::get_query_hdl_per_process (WCHAR *name,
-					  OBJECT_NAME_INFORMATION *ntfn)
+fhandler_pipe::get_query_hdl_per_process (OBJECT_NAME_INFORMATION *ntfn)
 {
   winpids pids ((DWORD) 0);
 
@@ -1272,8 +1271,8 @@ fhandler_pipe::get_query_hdl_per_process (WCHAR *name,
 				  ntfn, 65536, &len);
 	  if (!NT_SUCCESS (status) || !ntfn->Name.Buffer)
 	    goto close_handle;
-	  ntfn->Name.Buffer[ntfn->Name.Length / sizeof (WCHAR)] = L'\0';
-	  if (wcscmp (name, ntfn->Name.Buffer) == 0)
+	  if (RtlEqualUnicodeString (pc.get_nt_native_path (),
+				     &ntfn->Name, FALSE))
 	    {
 	      query_hdl_proc = proc;
 	      query_hdl_value = (HANDLE)(intptr_t) phi->Handles[j].HandleValue;
diff --git a/winsup/cygwin/local_includes/fhandler.h b/winsup/cygwin/local_includes/fhandler.h
index 6ddf37370..8729eb276 100644
--- a/winsup/cygwin/local_includes/fhandler.h
+++ b/winsup/cygwin/local_includes/fhandler.h
@@ -1216,11 +1216,8 @@ 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 *);
+  HANDLE get_query_hdl_per_process (OBJECT_NAME_INFORMATION *);
 public:
   fhandler_pipe ();

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2024-03-06 13:06 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-03-06 13:06 [newlib-cygwin] Cygwin: pipe: Simplify chhecking procedure of query_hdl Takashi Yano

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).