[newlib-cygwin] Cygwin: ps: simplify code

Corinna Vinschen corinna@sourceware.org
Tue Mar 12 20:21:00 GMT 2019


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

commit 1def2148d25fec8a150e5eac873d8ef4a0c71a50
Author: Corinna Vinschen <corinna@vinschen.de>
Date:   Tue Mar 12 21:21:13 2019 +0100

    Cygwin: ps: simplify code
    
    Always use NtQuerySystemInformation(SystemProcessIdInformation).
    This drops two code paths calling NtQueryInformationProcess or
    GetModuleFileNameExW and only requires to open the process
    to fetch system time info.
    
    Signed-off-by: Corinna Vinschen <corinna@vinschen.de>

Diff:
---
 winsup/utils/ps.cc | 65 +++++++++++++++---------------------------------------
 1 file changed, 18 insertions(+), 47 deletions(-)

diff --git a/winsup/utils/ps.cc b/winsup/utils/ps.cc
index c655d74..4fce3e0 100644
--- a/winsup/utils/ps.cc
+++ b/winsup/utils/ps.cc
@@ -155,7 +155,6 @@ main (int argc, char *argv[])
   int aflag, lflag, fflag, sflag, proc_id;
   uid_t uid;
   bool found_proc_id = true;
-  DWORD proc_access = PROCESS_QUERY_LIMITED_INFORMATION;
   cygwin_getinfo_types query = CW_GETPINFO;
   const char *dtitle = "    PID  TTY        STIME COMMAND\n";
   const char *dfmt   = "%7d%4s%10s %s\n";
@@ -257,12 +256,6 @@ main (int argc, char *argv[])
 	}
 
       drive_map = (void *) cygwin_internal (CW_ALLOC_DRIVE_MAP);
-      /* Check old Cygwin version. */
-      if (drive_map == (void *) -1)
-	drive_map = NULL;
-      /* Allow fallback to GetModuleFileNameEx. */
-      if (!drive_map)
-	proc_access = PROCESS_QUERY_INFORMATION | PROCESS_VM_READ;
     }
 
   for (int pid = 0;
@@ -291,8 +284,6 @@ main (int argc, char *argv[])
       else if (p->process_state & PID_TTYOU)
 	status = 'O';
 
-      /* Maximum possible path length under NT.  There's no official define
-	 for that value. */
       if (p->ppid)
 	{
 	  char *s;
@@ -309,49 +300,27 @@ main (int argc, char *argv[])
 	  HANDLE h;
 	  NTSTATUS status;
 	  wchar_t *win32path = NULL;
+	  FILETIME ct, et, kt, ut;
 
-	  h = OpenProcess (proc_access, FALSE, p->dwProcessId);
-	  if (!h)
-	    {
-	      ucbuf.spii.ProcessId = (PVOID) (ULONG_PTR) p->dwProcessId;
-	      ucbuf.spii.ImageName.Length = 0;
-	      ucbuf.spii.ImageName.MaximumLength = NT_MAX_PATH * sizeof (WCHAR);
-	      ucbuf.spii.ImageName.Buffer = ucbuf.buf;
-	      status = NtQuerySystemInformation (SystemProcessIdInformation,
-						 &ucbuf.spii, sizeof ucbuf.spii, NULL);
-	      if (NT_SUCCESS (status))
-		{
-		  if (ucbuf.spii.ImageName.Length)
-		    ucbuf.spii.ImageName.Buffer[ucbuf.spii.ImageName.Length
-					  / sizeof (WCHAR)] = L'\0';
-		  win32path = ucbuf.spii.ImageName.Buffer;
-		}
-	    }
-	  /* We use NtQueryInformationProcess in the first place, because
-	     GetModuleFileNameEx does not work under WOW64 when trying
-	     to fetch module names of 64 bit processes. */
-	  else if (!(proc_access & PROCESS_VM_READ))
+	  ucbuf.spii.ProcessId = (PVOID) (ULONG_PTR) p->dwProcessId;
+	  ucbuf.spii.ImageName.Length = 0;
+	  ucbuf.spii.ImageName.MaximumLength = NT_MAX_PATH * sizeof (WCHAR);
+	  ucbuf.spii.ImageName.Buffer = ucbuf.buf;
+	  status = NtQuerySystemInformation (SystemProcessIdInformation,
+					     &ucbuf.spii, sizeof ucbuf.spii,
+					     NULL);
+	  if (NT_SUCCESS (status))
 	    {
-	      status = NtQueryInformationProcess (h, ProcessImageFileName,
-						  &ucbuf.spii.ImageName,
-						  sizeof ucbuf - sizeof (PVOID),
-						  NULL);
-	      if (NT_SUCCESS (status))
-		{
-		  if (ucbuf.spii.ImageName.Length)
-		    ucbuf.spii.ImageName.Buffer[ucbuf.spii.ImageName.Length / sizeof (WCHAR)] = L'\0';
-		  win32path = ucbuf.spii.ImageName.Buffer;
-		}
+	      if (ucbuf.spii.ImageName.Length)
+		ucbuf.spii.ImageName.Buffer[ucbuf.spii.ImageName.Length
+				      / sizeof (WCHAR)] = L'\0';
+	      win32path = ucbuf.spii.ImageName.Buffer;
 	    }
-	  else if (GetModuleFileNameExW (h, NULL, ucbuf.buf,
-					 NT_MAX_PATH + 1))
-	    win32path = ucbuf.buf;
 	  if (win32path)
 	    {
 	      /* Call CW_MAP_DRIVE_MAP to convert native NT device paths to
-	         an ordinary Win32 path.  The returned pointer is a pointer
-		 into the incoming buffer given as third argument.  It's
-		 expected to be big enough. */
+	         an ordinary Win32 path.  The returned pointer points into
+		 the incoming buffer given as third argument. */
 	      if (win32path[0] == L'\\')
 		win32path = (wchar_t *) cygwin_internal (CW_MAP_DRIVE_MAP,
 							 drive_map, win32path);
@@ -359,7 +328,9 @@ main (int argc, char *argv[])
 	    }
 	  else
 	    strcpy (pname, p->dwProcessId == 4 ? "System" : "*** unknown ***");
-	  FILETIME ct, et, kt, ut;
+
+	  h = OpenProcess (PROCESS_QUERY_LIMITED_INFORMATION, FALSE,
+			   p->dwProcessId);
 	  if (h)
 	    {
 	      if (GetProcessTimes (h, &ct, &et, &kt, &ut))



More information about the Cygwin-cvs mailing list