Skip to content

Commit

Permalink
Issue #51. Polishing obtaining of a user name for a given PID
Browse files Browse the repository at this point in the history
  • Loading branch information
kocherovms committed Apr 24, 2016
1 parent 80c92cc commit c20c11d
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 16 deletions.
23 changes: 14 additions & 9 deletions scripts/metracer.cmd
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@ECHO OFF
::@ECHO OFF
SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION
SET myself=%~dpnx0
Expand Down Expand Up @@ -44,7 +44,7 @@ IF DEFINED is_help_requested (

:: For all other options we need tools.jar, resolve it
IF EXIST "%java_exe_folder%..\lib\tools.jar" (
SET tools_jar="%java_exe_folder%..\lib\tools.jar"
SET tools_jar=%java_exe_folder%..\lib\tools.jar
) ELSE (
ECHO Failed to resolve tools.jar from a JDK. Please, make sure that JDK is installed and JAVA_HOME environment variable is properly set
EXIT /B 1
Expand All @@ -53,15 +53,20 @@ IF EXIST "%java_exe_folder%..\lib\tools.jar" (

IF DEFINED is_list_requested (
:: For JVM listing no impersonation is required
"%java_exe%" -Xbootclasspath/a:%tools_jar% -jar "%myself%" %*
"%java_exe%" -Xbootclasspath/a:"%tools_jar%" -jar "%myself%" %*
EXIT /B !ERRORLEVEL!
)

:: Following could be used to find user of a target JVM
:: FOR /F delims^=^"^ tokens^=3^,13 %%P IN ('tasklist /FO CSV /V /NH') DO (
:: @ECHO %p %q
::)
:: Following could be used to resolve whoami: %USERDOMAIN%\%USERNAME%
FOR /F %%I IN ('CALL "%java_exe%" -cp "%myself%" com.develorium.metracer.WinMain %*') DO SET user_name=%%I
ECHO user_name=%user_name%

"%java_exe%" -Xbootclasspath/a:%tools_jar% -jar "%myself%" %*
IF DEFINED user_name (
SET current_user_name=%USERDOMAIN%\%USERNAME%
IF NOT "!current_user_name!"=="%user_name%" (
runas /user:"%user_name%" "\"%java_exe%\" -Xbootclasspath/a:\"%tools_jar%\" -jar \"%myself%\" %*"
EXIT /B !ERRORLEVEL!
)
)

"%java_exe%" -Xbootclasspath/a:"%tools_jar%" -jar "%myself%" %*
EXIT /B !ERRORLEVEL!
18 changes: 16 additions & 2 deletions src/main/java/com/develorium/metracer/WinMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,31 @@
public class WinMain {
public static void main(String[] theArguments) {
try {
for(String arg : theArguments)
System.err.println(arg);

String userName = resolveUserName(theArguments);

if(userName != null)
System.out.println(userName);
} catch(Config.BadConfig e) {
System.err.println(e.getMessage());
e.printStackTrace(System.err);
System.exit(1);
} catch(IOException e) {
System.err.println(e.getMessage());
e.printStackTrace(System.err);
System.exit(2);
} catch(Throwable e) {
System.err.println(e.getMessage());
e.printStackTrace(System.err);
System.exit(3);
}
}

static String resolveUserName(String[] theArguments) throws Config.BadConfig, IOException {
Config config = new Config(theArguments);
System.err.println("zzz PID = " + config.pid);
CSVParser parser = getTaskList();
return resolveUserNameOfPid(parser, "" + config.pid);
}
Expand All @@ -57,14 +67,18 @@ static CSVParser getTaskList() throws IOException {
}

static String resolveUserNameOfPid(CSVParser theParser, String thePid) {
String rv = null;
for(CSVRecord record : theParser) {
String recordPid = record.get(1);
System.err.println(record.get(0) + " " + record.get(1));

if(recordPid.equals(thePid)) {
return record.get(6);
System.err.println("AAAAAA");
//return record.get(6);
rv = record.get(6);
}
}

return null;
return rv;
}
}
15 changes: 10 additions & 5 deletions src/test/java/com/develorium/metracer/WinMainTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@
import org.apache.commons.csv.*;

public class WinMainTest {
@Before
public void runOnWindowsOnly() {
String osName = System.getProperty("os.name");
org.junit.Assume.assumeTrue(osName.startsWith("Windows"));
}
@Test
public void testResolveUserNameOfPid() throws IOException {
String[] samples = {
Expand All @@ -36,6 +31,7 @@ public void testResolveUserNameOfPid() throws IOException {
"\"explorer.exe\",\"1584\",\"Console\",\"0\",\"18 432 КБ\",\"Работает\",\"VIRTUALPC\\вася\",\"0:01:43\",\"Н/Д\"", "1584", "VIRTUALPC\\вася",
"\"some\", \"strange\", \"csv\"", "100", null,
"\"csrss.exe\",\"364\",\"Services\",\"0\",\"3,480 K\",\"Unknown\",\"NT AUTHORITY\\SYSTEM\",\"0:00:22\",\"N/A\"", "zzzz", null,
"\"cmd.exe\",\"1232\",\"Console\",\"0\",\"1 472 КБ\",\"Работает\",\"VIRTUALPC\\вася\",\"0:00:00\",\"C:\\WINDOWS\\system32\\cmd.exe - java -cp target\\test-classes \"\"com.develorium.metracertest.Main\"\"\"", "1232", "VIRTUALPC\\вася"
};

for(int i = 0; i < samples.length; i += 3) {
Expand All @@ -50,10 +46,19 @@ public void testResolveUserNameOfPid() throws IOException {
}
@Test
public void testResolveUserName() throws Config.BadConfig, IOException {
if(!isWindows()) {
System.out.println("Skipping test testResolveUserName - must be executed under Windows only");
return;
}
// Check 0 ("System Idle Process") - hope it always exists
String userName = WinMain.resolveUserName(new String[] { "0" });
System.out.println(String.format("Resolved user name of PID 0: %s", userName));
Assert.assertTrue(userName != null);
Assert.assertTrue(userName.length() > 0);
}

private static boolean isWindows() {
String osName = System.getProperty("os.name");
return osName.startsWith("Windows");
}
}

0 comments on commit c20c11d

Please sign in to comment.