This commit is contained in:
Roger Riggs 2015-06-04 03:38:56 +02:00
commit c3d9e6c08d
3 changed files with 42 additions and 51 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

@ -36,6 +36,7 @@
*/
import java.lang.ProcessBuilder.Redirect;
import java.lang.ProcessHandle;
import static java.lang.ProcessBuilder.Redirect.*;
import java.io.*;
@ -47,7 +48,6 @@ import java.util.*;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.security.*;
import sun.misc.Unsafe;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import static java.lang.System.getenv;
@ -309,6 +309,8 @@ public class Basic {
String action = args[0];
if (action.equals("sleep")) {
Thread.sleep(10 * 60 * 1000L);
} else if (action.equals("pid")) {
System.out.println(ProcessHandle.current().getPid());
} else if (action.equals("testIO")) {
String expected = "standard input";
char[] buf = new char[expected.length()+1];
@ -1139,40 +1141,20 @@ public class Basic {
}
static void checkProcessPid() {
long actualPid = 0;
long expectedPid = -1;
if (Windows.is()) {
String[] argsTasklist = {"tasklist.exe", "/NH", "/FI", "\"IMAGENAME eq tasklist.exe\""};
ProcessBuilder pb = new ProcessBuilder(argsTasklist);
try {
Process proc = pb.start();
expectedPid = proc.getPid();
String output = commandOutput(proc);
String[] splits = output.split("\\s+");
actualPid = Integer.valueOf(splits[2]);
} catch (Throwable t) {
unexpected(t);
}
} else if (Unix.is() || MacOSX.is()) {
String[] shArgs = {"sh", "-c", "echo $$"};
ProcessBuilder pb = new ProcessBuilder(shArgs);
try {
Process proc = pb.start();
expectedPid = proc.getPid();
String output = commandOutput(proc);
String[] splits = output.split("\\s+");
actualPid = Integer.valueOf(splits[0]);
} catch (Throwable t) {
unexpected(t);
}
} else {
fail("No test for checkProcessPid on platform: " + System.getProperty("os.name"));
return;
ProcessBuilder pb = new ProcessBuilder();
List<String> list = new ArrayList<String>(javaChildArgs);
list.add("pid");
pb.command(list);
try {
Process p = pb.start();
String s = commandOutput(p);
long actualPid = Long.valueOf(s.trim());
long expectedPid = p.getPid();
equal(actualPid, expectedPid);
} catch (Throwable t) {
unexpected(t);
}
equal(actualPid, expectedPid);
// Test the default implementation of Process.getPid
DelegatingProcess p = new DelegatingProcess(null);

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);
}
}