./src/solaris/classes/java/lang/UNIXProcess.java.bsd

Print this page
rev 5754 : [mq]: getPid-patch

@@ -32,13 +32,18 @@
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
+import java.nio.charset.Charset;
+import java.nio.file.Files;
+import java.nio.file.LinkOption;
+import java.nio.file.Path;
+import java.nio.file.Paths;
 import java.util.Arrays;
-import java.util.concurrent.Executors;
 import java.util.concurrent.Executor;
+import java.util.concurrent.Executors;
 import java.util.concurrent.ThreadFactory;
 import java.util.concurrent.TimeUnit;
 import java.security.AccessController;
 import static java.security.AccessController.doPrivileged;
 import java.security.PrivilegedAction;

@@ -268,10 +273,37 @@
     @Override
     public synchronized boolean isAlive() {
         return !hasExited;
     }
 
+    @Override
+    public int getPid() {
+        if (!isAlive()) {
+            return -1;
+        } else {
+            return pid;
+        }
+    }
+
+    @Override
+    public String getProcessName(int pid) {
+        // look at /proc/<pid>/comm
+        if (pid < 0) {
+            throw new IllegalArgumentException( "pid == " + pid );
+        }
+        Path p = Paths.get( "/proc/" + pid + "/comm" );
+        if (!Files.exists(p, LinkOption.NOFOLLOW_LINKS )) {
+            return null;
+        }
+    
+        try {
+            return Files.readAllLines( p, Charset.defaultCharset() ).get(0);
+        } catch (IOException ioex) {
+            throw new RuntimeException( ioex );
+        }
+    }
+
     /* This routine initializes JNI field offsets for the class */
     private static native void initIDs();
 
     static {
         initIDs();

@@ -347,6 +379,8 @@
                 }
                 this.out = ProcessBuilder.NullOutputStream.INSTANCE;
             }
         }
     }
+
+
 }