8081567: java/lang/ProcessHandle/InfoTest.java failed Cannot run program "whoami"
authorrriggs
Wed, 03 Jun 2015 18:17:04 -0400
changeset 30955 9f660470002c
parent 30914 870f7227984a
child 30956 6f95bc555a6f
8081567: java/lang/ProcessHandle/InfoTest.java failed Cannot run program "whoami" Summary: Replace use of whoami with checking the user against a file created Reviewed-by: igerasim
jdk/src/java.base/windows/native/libjava/ProcessHandleImpl_win.c
jdk/test/java/lang/ProcessHandle/InfoTest.java
--- a/jdk/src/java.base/windows/native/libjava/ProcessHandleImpl_win.c	Wed Jun 03 14:35:17 2015 -0700
+++ b/jdk/src/java.base/windows/native/libjava/ProcessHandleImpl_win.c	Wed Jun 03 18:17:04 2015 -0400
@@ -32,10 +32,11 @@
 
 #include <windows.h>
 #include <tlhelp32.h>
+#include <sddl.h>
 
 static void getStatInfo(JNIEnv *env, HANDLE handle, jobject jinfo);
 static void getCmdlineInfo(JNIEnv *env, HANDLE handle, jobject jinfo);
-static void procToUser( JNIEnv *env, HANDLE handle, jobject jinfo);
+static void procToUser(JNIEnv *env, HANDLE handle, jobject jinfo);
 
 /**************************************************************
  * Implementation of ProcessHandleImpl_Info native methods.
@@ -387,15 +388,15 @@
     }
 }
 
-static void procToUser( JNIEnv *env, HANDLE handle, jobject jinfo) {
+static void procToUser(JNIEnv *env, HANDLE handle, jobject jinfo) {
 #define TOKEN_LEN 256
     DWORD token_len = TOKEN_LEN;
     char token_buf[TOKEN_LEN];
     TOKEN_USER *token_user = (TOKEN_USER*)token_buf;
     HANDLE tokenHandle;
-    WCHAR domain[255];
-    WCHAR name[255];
-    DWORD domainLen = sizeof(domain);
+    WCHAR domain[255 + 1 + 255 + 1];    // large enough to concat with '/' and name
+    WCHAR name[255 + 1];
+    DWORD domainLen = sizeof(domain) - sizeof(name);
     DWORD nameLen = sizeof(name);
     SID_NAME_USE use;
     jstring s;
@@ -416,11 +417,18 @@
 
     if (LookupAccountSidW(NULL, token_user->User.Sid, &name[0], &nameLen,
                           &domain[0], &domainLen, &use) == 0) {
-        // Name not available
-        return;
+        // Name not available, convert to a String
+        LPWSTR str;
+        if (ConvertSidToStringSidW(token_user->User.Sid, &str) == 0) {
+            return;
+        }
+        s = (*env)->NewString(env, (const jchar *)str, (jsize)wcslen(str));
+        LocalFree(str);
+    } else {
+        wcscat(domain, L"\\");
+        wcscat(domain, name);
+        s = (*env)->NewString(env, (const jchar *)domain, (jsize)wcslen(domain));
     }
-
-    s = (*env)->NewString(env, (const jchar *)name, (jsize)wcslen(name));
     CHECK_NULL(s);
     (*env)->SetObjectField(env, jinfo, ProcessHandleImpl_Info_userID, s);
 }
--- a/jdk/test/java/lang/ProcessHandle/InfoTest.java	Wed Jun 03 14:35:17 2015 -0700
+++ b/jdk/test/java/lang/ProcessHandle/InfoTest.java	Wed Jun 03 18:17:04 2015 -0400
@@ -24,10 +24,12 @@
 import java.io.File;
 import java.io.BufferedReader;
 import java.io.IOException;
+import java.io.UncheckedIOException;
 import java.lang.ProcessBuilder;
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.Paths;
+import java.nio.file.attribute.UserPrincipal;
 import java.time.Duration;
 import java.time.Instant;
 import java.time.temporal.ChronoUnit;
@@ -58,17 +60,16 @@
     static String whoami;
 
     static {
-        ProcessBuilder pb = new ProcessBuilder("whoami");
-        String fullName;
         try {
-            fullName = new Scanner(pb.start().getInputStream()).nextLine();
-            StringTokenizer st = new StringTokenizer(fullName, "\\");
-            while (st.hasMoreTokens()) {
-                whoami = st.nextToken();
-            }
-            System.out.printf("whoami: %s, user.name: %s%n", whoami, System.getProperty("user.name"));
+            // Create a file and take the username from the file
+            Path p = Paths.get("OwnerName.tmp");
+            Files.createFile(p);
+            UserPrincipal owner = Files.getOwner(p);
+            whoami = owner.getName();
+            Files.delete(p);
         } catch (IOException ex) {
-            throw new RuntimeException(ex);
+            ex.printStackTrace();
+            throw new UncheckedIOException("tmp file", ex);
         }
     }