8081567: java/lang/ProcessHandle/InfoTest.java failed Cannot run program "whoami"

Replace use of whoami with checking the user against a file created

Reviewed-by: igerasim
This commit is contained in:
Roger Riggs 2015-06-03 18:17:04 -04:00
parent e76e828a34
commit b440bfb8eb
2 changed files with 27 additions and 18 deletions

View File

@ -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 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) {
#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 @@ static void procToUser( JNIEnv *env, HANDLE handle, jobject jinfo) {
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);
}

View File

@ -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 @@ public class InfoTest {
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);
}
}