public inbox for cygwin-cvs@sourceware.org
help / color / mirror / Atom feed
From: Corinna Vinschen <corinna@sourceware.org>
To: cygwin-cvs@sourceware.org
Subject: [newlib-cygwin/main] Cygwin: open_shared: don't reuse shared_locations parameter as output
Date: Tue, 17 Jan 2023 19:15:06 +0000 (GMT)	[thread overview]
Message-ID: <20230117191506.5DBAB3858410@sourceware.org> (raw)

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

commit 93508e5bb841138911ed3dee3c92cc18be43d9ca
Author:     Corinna Vinschen <corinna@vinschen.de>
AuthorDate: Mon Jan 16 22:25:42 2023 +0100
Commit:     Corinna Vinschen <corinna@vinschen.de>
CommitDate: Tue Jan 17 16:32:14 2023 +0100

    Cygwin: open_shared: don't reuse shared_locations parameter as output
    
    For ages, open_shared uses the shared_locations parameter as
    output to indicate if the mapping for a shared region has been
    created or just opened.  Split this into two parameters.  Use
    the shared_locations parameter as input only, return the creation
    state of the mapping in a bool reference parameter.
    
    Signed-off-by: Corinna Vinschen <corinna@vinschen.de>

Diff:
---
 winsup/cygwin/fhandler/console.cc          |  7 +++---
 winsup/cygwin/local_includes/shared_info.h |  8 +++---
 winsup/cygwin/mm/shared.cc                 | 39 ++++++++++++++++--------------
 winsup/cygwin/pinfo.cc                     |  6 ++---
 4 files changed, 31 insertions(+), 29 deletions(-)

diff --git a/winsup/cygwin/fhandler/console.cc b/winsup/cygwin/fhandler/console.cc
index ee392fda25dc..68ab43d81ea1 100644
--- a/winsup/cygwin/fhandler/console.cc
+++ b/winsup/cygwin/fhandler/console.cc
@@ -211,15 +211,14 @@ beep ()
 }
 
 fhandler_console::console_state *
-fhandler_console::open_shared_console (HWND hw, HANDLE& h, bool& create)
+fhandler_console::open_shared_console (HWND hw, HANDLE& h, bool& created)
 {
   wchar_t namebuf[(sizeof "XXXXXXXXXXXXXXXXXX-consNNNNNNNNNN")];
   __small_swprintf (namebuf, L"%S-cons%p", &cygheap->installation_key, hw);
 
-  shared_locations m = create ? SH_SHARED_CONSOLE : SH_JUSTOPEN;
+  shared_locations m = created ? SH_SHARED_CONSOLE : SH_JUSTOPEN;
   console_state *res = (console_state *)
-    open_shared (namebuf, 0, h, sizeof (console_state), &m);
-  create = m != SH_JUSTOPEN;
+    open_shared (namebuf, 0, h, sizeof (console_state), m, created);
   return res;
 }
 
diff --git a/winsup/cygwin/local_includes/shared_info.h b/winsup/cygwin/local_includes/shared_info.h
index 6c53ec0b8b0f..cbe55a27887b 100644
--- a/winsup/cygwin/local_includes/shared_info.h
+++ b/winsup/cygwin/local_includes/shared_info.h
@@ -89,9 +89,9 @@ HANDLE get_session_parent_dir ();
 char *shared_name (char *, const char *, int);
 WCHAR *shared_name (WCHAR *, const WCHAR *, int);
 void *open_shared (const WCHAR *, int, HANDLE&, DWORD,
-			     shared_locations, PSECURITY_ATTRIBUTES = &sec_all,
-			     DWORD = FILE_MAP_READ | FILE_MAP_WRITE);
+		   shared_locations, PSECURITY_ATTRIBUTES = &sec_all,
+		   DWORD = FILE_MAP_READ | FILE_MAP_WRITE);
 void *open_shared (const WCHAR *, int, HANDLE&, DWORD,
-			     shared_locations *, PSECURITY_ATTRIBUTES = &sec_all,
-			     DWORD = FILE_MAP_READ | FILE_MAP_WRITE);
+		   shared_locations, bool &, PSECURITY_ATTRIBUTES = &sec_all,
+		   DWORD = FILE_MAP_READ | FILE_MAP_WRITE);
 extern void user_shared_create (bool reinit);
diff --git a/winsup/cygwin/mm/shared.cc b/winsup/cygwin/mm/shared.cc
index 893b20d289b4..351d314af01b 100644
--- a/winsup/cygwin/mm/shared.cc
+++ b/winsup/cygwin/mm/shared.cc
@@ -127,54 +127,57 @@ void *
 open_shared (const WCHAR *name, int n, HANDLE& shared_h, DWORD size,
 	     shared_locations m, PSECURITY_ATTRIBUTES psa, DWORD access)
 {
-  return open_shared (name, n, shared_h, size, &m, psa, access);
+  bool created_dummy;
+  return open_shared (name, n, shared_h, size, m, created_dummy, psa, access);
 }
 
 void *
 open_shared (const WCHAR *name, int n, HANDLE& shared_h, DWORD size,
-	     shared_locations *m, PSECURITY_ATTRIBUTES psa, DWORD access)
+	     shared_locations m, bool &created, PSECURITY_ATTRIBUTES psa,
+	     DWORD access)
 {
+  WCHAR map_buf[MAX_PATH];
+  WCHAR *mapname = NULL;
   void *shared;
-
   void *addr;
-  if (*m == SH_JUSTCREATE || *m == SH_JUSTOPEN)
+
+  if (m == SH_JUSTCREATE || m == SH_JUSTOPEN)
     addr = NULL;
   else
-    addr = (void *) region_address[*m];
+    addr = (void *) region_address[m];
 
-  WCHAR map_buf[MAX_PATH];
-  WCHAR *mapname = NULL;
-
-  if (shared_h)
-    *m = SH_JUSTOPEN;
-  else
+  created = false;
+  if (!shared_h)
     {
       if (name)
 	mapname = shared_name (map_buf, name, n);
-      if (*m == SH_JUSTOPEN)
-	shared_h = OpenFileMappingW (access, FALSE, mapname);
+      if (m == SH_JUSTOPEN)
+	shared_h = OpenFileMappingW (FILE_MAP_READ | FILE_MAP_WRITE, FALSE,
+				     mapname);
       else
 	{
+	  created = true;
 	  shared_h = CreateFileMappingW (INVALID_HANDLE_VALUE, psa,
 					PAGE_READWRITE, 0, size, mapname);
 	  if (GetLastError () == ERROR_ALREADY_EXISTS)
-	    *m = SH_JUSTOPEN;
+	    created = false;
 	}
       if (shared_h)
 	/* ok! */;
-      else if (*m != SH_JUSTOPEN)
+      else if (m != SH_JUSTOPEN)
 	api_fatal ("CreateFileMapping %W, %E.  Terminating.", mapname);
       else
 	return NULL;
     }
 
-  shared = (shared_info *) MapViewOfFileEx (shared_h, access, 0, 0, 0, addr);
+  shared = MapViewOfFileEx (shared_h, FILE_MAP_READ | FILE_MAP_WRITE,
+			    0, 0, 0, addr);
 
   if (!shared)
     api_fatal ("MapViewOfFileEx '%W'(%p), %E.  Terminating.", mapname, shared_h);
 
-  debug_printf ("name %W, n %d, shared %p (wanted %p), h %p, *m %d",
-		mapname, n, shared, addr, shared_h, *m);
+  debug_printf ("name %W, n %d, shared %p (wanted %p), h %p, m %d",
+		mapname, n, shared, addr, shared_h, m);
 
   return shared;
 }
diff --git a/winsup/cygwin/pinfo.cc b/winsup/cygwin/pinfo.cc
index fc618b9b8dd1..c05cf2662cd7 100644
--- a/winsup/cygwin/pinfo.cc
+++ b/winsup/cygwin/pinfo.cc
@@ -387,8 +387,10 @@ pinfo::init (pid_t n, DWORD flag, HANDLE h0)
 
   for (int i = 0; i < 20; i++)
     {
+      bool created;
+
       procinfo = (_pinfo *) open_shared (L"cygpid", n, h0, sizeof (_pinfo),
-					 &shloc, sec_attribs, access);
+					 shloc, created, sec_attribs, access);
       if (!h0)
 	{
 	  if (createit)
@@ -409,8 +411,6 @@ pinfo::init (pid_t n, DWORD flag, HANDLE h0)
 	  continue;
 	}
 
-      bool created = shloc != SH_JUSTOPEN;
-
       /* Just fetching info for ps or /proc, don't do anything rash. */
       if (!created && !(flag & PID_NEW) && !procinfo->ppid
 	  && (flag & PID_PROCINFO))

                 reply	other threads:[~2023-01-17 19:15 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230117191506.5DBAB3858410@sourceware.org \
    --to=corinna@sourceware.org \
    --cc=cygwin-cvs@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).