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

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


  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.lang;
  27 
  28 import java.io.BufferedInputStream;
  29 import java.io.BufferedOutputStream;
  30 import java.io.ByteArrayInputStream;
  31 import java.io.FileDescriptor;
  32 import java.io.FileInputStream;
  33 import java.io.FileOutputStream;
  34 import java.io.IOException;
  35 import java.io.InputStream;
  36 import java.io.OutputStream;





  37 import java.util.Arrays;
  38 import java.util.concurrent.Executors;
  39 import java.util.concurrent.Executor;
  40 import java.util.concurrent.ThreadFactory;
  41 import java.util.concurrent.TimeUnit;
  42 import java.security.AccessController;
  43 import static java.security.AccessController.doPrivileged;
  44 import java.security.PrivilegedAction;
  45 import java.security.PrivilegedActionException;
  46 import java.security.PrivilegedExceptionAction;
  47 
  48 /**
  49  * java.lang.Process subclass in the UNIX environment.
  50  *
  51  * @author Mario Wolczko and Ross Knippel.
  52  * @author Konstantin Kladko (ported to Linux)
  53  * @author Martin Buchholz
  54  */
  55 final class UNIXProcess extends Process {
  56     private static final sun.misc.JavaIOFileDescriptorAccess fdAccess


 251                 destroyProcess(pid, force);
 252         }
 253         try { stdin.close();  } catch (IOException ignored) {}
 254         try { stdout.close(); } catch (IOException ignored) {}
 255         try { stderr.close(); } catch (IOException ignored) {}
 256     }
 257 
 258     public void destroy() {
 259         destroy(false);
 260     }
 261 
 262     @Override
 263     public Process destroyForcibly() {
 264         destroy(true);
 265         return this;
 266     }
 267 
 268     @Override
 269     public synchronized boolean isAlive() {
 270         return !hasExited;



























 271     }
 272 
 273     /* This routine initializes JNI field offsets for the class */
 274     private static native void initIDs();
 275 
 276     static {
 277         initIDs();
 278     }
 279 
 280     /**
 281      * A buffered input stream for a subprocess pipe file descriptor
 282      * that allows the underlying file descriptor to be reclaimed when
 283      * the process exits, via the processExited hook.
 284      *
 285      * This is tricky because we do not want the user-level InputStream to be
 286      * closed until the user invokes close(), and we need to continue to be
 287      * able to read any buffered data lingering in the OS pipe buffer.
 288      */
 289     static class ProcessPipeInputStream extends BufferedInputStream {
 290         ProcessPipeInputStream(int fd) {




  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.lang;
  27 
  28 import java.io.BufferedInputStream;
  29 import java.io.BufferedOutputStream;
  30 import java.io.ByteArrayInputStream;
  31 import java.io.FileDescriptor;
  32 import java.io.FileInputStream;
  33 import java.io.FileOutputStream;
  34 import java.io.IOException;
  35 import java.io.InputStream;
  36 import java.io.OutputStream;
  37 import java.nio.charset.Charset;
  38 import java.nio.file.Files;
  39 import java.nio.file.LinkOption;
  40 import java.nio.file.Path;
  41 import java.nio.file.Paths;
  42 import java.util.Arrays;
  43 import java.util.concurrent.Executors;
  44 import java.util.concurrent.Executor;
  45 import java.util.concurrent.ThreadFactory;
  46 import java.util.concurrent.TimeUnit;
  47 import java.security.AccessController;
  48 import static java.security.AccessController.doPrivileged;
  49 import java.security.PrivilegedAction;
  50 import java.security.PrivilegedActionException;
  51 import java.security.PrivilegedExceptionAction;
  52 
  53 /**
  54  * java.lang.Process subclass in the UNIX environment.
  55  *
  56  * @author Mario Wolczko and Ross Knippel.
  57  * @author Konstantin Kladko (ported to Linux)
  58  * @author Martin Buchholz
  59  */
  60 final class UNIXProcess extends Process {
  61     private static final sun.misc.JavaIOFileDescriptorAccess fdAccess


 256                 destroyProcess(pid, force);
 257         }
 258         try { stdin.close();  } catch (IOException ignored) {}
 259         try { stdout.close(); } catch (IOException ignored) {}
 260         try { stderr.close(); } catch (IOException ignored) {}
 261     }
 262 
 263     public void destroy() {
 264         destroy(false);
 265     }
 266 
 267     @Override
 268     public Process destroyForcibly() {
 269         destroy(true);
 270         return this;
 271     }
 272 
 273     @Override
 274     public synchronized boolean isAlive() {
 275         return !hasExited;
 276     }
 277 
 278     @Override
 279     public int getPid() {
 280         if (!isAlive()) {
 281             return -1;
 282         } else {
 283             return pid;
 284         }
 285     }
 286 
 287     @Override
 288     public String getProcessName(int pid) {
 289         // look at /proc/<pid>/comm
 290         if (pid < 0) {
 291             throw new IllegalArgumentException( "pid == " + pid );
 292         }
 293         Path p = Paths.get( "/proc/" + pid + "/comm" );
 294         if (!Files.exists(p, LinkOption.NOFOLLOW_LINKS )) {
 295             return null;
 296         }
 297     
 298         try {
 299             return Files.readAllLines( p, Charset.defaultCharset() ).get(0);
 300         } catch (IOException ioex) {
 301             throw new RuntimeException( ioex );
 302         }
 303     }
 304 
 305     /* This routine initializes JNI field offsets for the class */
 306     private static native void initIDs();
 307 
 308     static {
 309         initIDs();
 310     }
 311 
 312     /**
 313      * A buffered input stream for a subprocess pipe file descriptor
 314      * that allows the underlying file descriptor to be reclaimed when
 315      * the process exits, via the processExited hook.
 316      *
 317      * This is tricky because we do not want the user-level InputStream to be
 318      * closed until the user invokes close(), and we need to continue to be
 319      * able to read any buffered data lingering in the OS pipe buffer.
 320      */
 321     static class ProcessPipeInputStream extends BufferedInputStream {
 322         ProcessPipeInputStream(int fd) {