< prev index next >

src/java.base/windows/classes/java/lang/ProcessImpl.java

Print this page

498         }
499 
500         handle = create(cmdstr, envblock, path,
501                         stdHandles, redirectErrorStream);
502         // Register a cleaning function to close the handle
503         final long local_handle = handle;    // local to prevent capture of this
504         CleanerFactory.cleaner().register(this, () -> closeHandle(local_handle));
505 
506         processHandle = ProcessHandleImpl.getInternal(getProcessId0(handle));
507 
508         java.security.AccessController.doPrivileged(
509         new java.security.PrivilegedAction<Void>() {
510         public Void run() {
511             if (stdHandles[0] == -1L)
512                 stdin_stream = ProcessBuilder.NullOutputStream.INSTANCE;
513             else {
514                 FileDescriptor stdin_fd = new FileDescriptor();
515                 fdAccess.setHandle(stdin_fd, stdHandles[0]);
516                 fdAccess.registerCleanup(stdin_fd);
517                 stdin_stream = new BufferedOutputStream(
518                     new FileOutputStream(stdin_fd));
519             }
520 
521             if (stdHandles[1] == -1L || forceNullOutputStream)
522                 stdout_stream = ProcessBuilder.NullInputStream.INSTANCE;
523             else {
524                 FileDescriptor stdout_fd = new FileDescriptor();
525                 fdAccess.setHandle(stdout_fd, stdHandles[1]);
526                 fdAccess.registerCleanup(stdout_fd);
527                 stdout_stream = new BufferedInputStream(
528                     new PipeInputStream(stdout_fd));
529             }
530 
531             if (stdHandles[2] == -1L)
532                 stderr_stream = ProcessBuilder.NullInputStream.INSTANCE;
533             else {
534                 FileDescriptor stderr_fd = new FileDescriptor();
535                 fdAccess.setHandle(stderr_fd, stdHandles[2]);
536                 fdAccess.registerCleanup(stderr_fd);
537                 stderr_stream = new PipeInputStream(stderr_fd);
538             }

547     public InputStream getInputStream() {
548         return stdout_stream;
549     }
550 
551     public InputStream getErrorStream() {
552         return stderr_stream;
553     }
554 
555     private static final int STILL_ACTIVE = getStillActive();
556     private static native int getStillActive();
557 
558     public int exitValue() {
559         int exitCode = getExitCodeProcess(handle);
560         if (exitCode == STILL_ACTIVE)
561             throw new IllegalThreadStateException("process has not exited");
562         return exitCode;
563     }
564     private static native int getExitCodeProcess(long handle);
565 
566     public int waitFor() throws InterruptedException {
567         long comp = Blocker.begin();
568         try {
569             waitForInterruptibly(handle);
570         } finally {
571             Blocker.end(comp);
572         }
573         if (Thread.interrupted())
574             throw new InterruptedException();
575         return getExitCodeProcess(handle);
576     }
577 
578     private static native void waitForInterruptibly(long handle);
579 
580     @Override
581     public boolean waitFor(long timeout, TimeUnit unit)
582         throws InterruptedException
583     {
584         long remainingNanos = unit.toNanos(timeout);    // throw NPE before other conditions
585         if (getExitCodeProcess(handle) != STILL_ACTIVE) return true;
586         if (timeout <= 0) return false;
587 
588         long deadline = System.nanoTime() + remainingNanos;
589         do {
590             // Round up to next millisecond
591             long msTimeout = TimeUnit.NANOSECONDS.toMillis(remainingNanos + 999_999L);
592             if (msTimeout < 0) {
593                 // if wraps around then wait a long while
594                 msTimeout = Integer.MAX_VALUE;
595             }
596             long comp = Blocker.begin();
597             try {
598                 waitForTimeoutInterruptibly(handle, msTimeout);
599             } finally {
600                 Blocker.end(comp);
601             }
602             if (Thread.interrupted())
603                 throw new InterruptedException();
604             if (getExitCodeProcess(handle) != STILL_ACTIVE) {
605                 return true;
606             }
607             remainingNanos = deadline - System.nanoTime();
608         } while (remainingNanos > 0);
609 
610         return (getExitCodeProcess(handle) != STILL_ACTIVE);
611     }
612 
613     private static native void waitForTimeoutInterruptibly(
614         long handle, long timeoutMillis);
615 
616     @Override
617     public void destroy() {
618         terminateProcess(handle);
619     }
620 

498         }
499 
500         handle = create(cmdstr, envblock, path,
501                         stdHandles, redirectErrorStream);
502         // Register a cleaning function to close the handle
503         final long local_handle = handle;    // local to prevent capture of this
504         CleanerFactory.cleaner().register(this, () -> closeHandle(local_handle));
505 
506         processHandle = ProcessHandleImpl.getInternal(getProcessId0(handle));
507 
508         java.security.AccessController.doPrivileged(
509         new java.security.PrivilegedAction<Void>() {
510         public Void run() {
511             if (stdHandles[0] == -1L)
512                 stdin_stream = ProcessBuilder.NullOutputStream.INSTANCE;
513             else {
514                 FileDescriptor stdin_fd = new FileDescriptor();
515                 fdAccess.setHandle(stdin_fd, stdHandles[0]);
516                 fdAccess.registerCleanup(stdin_fd);
517                 stdin_stream = new BufferedOutputStream(
518                     new PipeOutputStream(stdin_fd));
519             }
520 
521             if (stdHandles[1] == -1L || forceNullOutputStream)
522                 stdout_stream = ProcessBuilder.NullInputStream.INSTANCE;
523             else {
524                 FileDescriptor stdout_fd = new FileDescriptor();
525                 fdAccess.setHandle(stdout_fd, stdHandles[1]);
526                 fdAccess.registerCleanup(stdout_fd);
527                 stdout_stream = new BufferedInputStream(
528                     new PipeInputStream(stdout_fd));
529             }
530 
531             if (stdHandles[2] == -1L)
532                 stderr_stream = ProcessBuilder.NullInputStream.INSTANCE;
533             else {
534                 FileDescriptor stderr_fd = new FileDescriptor();
535                 fdAccess.setHandle(stderr_fd, stdHandles[2]);
536                 fdAccess.registerCleanup(stderr_fd);
537                 stderr_stream = new PipeInputStream(stderr_fd);
538             }

547     public InputStream getInputStream() {
548         return stdout_stream;
549     }
550 
551     public InputStream getErrorStream() {
552         return stderr_stream;
553     }
554 
555     private static final int STILL_ACTIVE = getStillActive();
556     private static native int getStillActive();
557 
558     public int exitValue() {
559         int exitCode = getExitCodeProcess(handle);
560         if (exitCode == STILL_ACTIVE)
561             throw new IllegalThreadStateException("process has not exited");
562         return exitCode;
563     }
564     private static native int getExitCodeProcess(long handle);
565 
566     public int waitFor() throws InterruptedException {
567         boolean attempted = Blocker.begin();
568         try {
569             waitForInterruptibly(handle);
570         } finally {
571             Blocker.end(attempted);
572         }
573         if (Thread.interrupted())
574             throw new InterruptedException();
575         return getExitCodeProcess(handle);
576     }
577 
578     private static native void waitForInterruptibly(long handle);
579 
580     @Override
581     public boolean waitFor(long timeout, TimeUnit unit)
582         throws InterruptedException
583     {
584         long remainingNanos = unit.toNanos(timeout);    // throw NPE before other conditions
585         if (getExitCodeProcess(handle) != STILL_ACTIVE) return true;
586         if (timeout <= 0) return false;
587 
588         long deadline = System.nanoTime() + remainingNanos;
589         do {
590             // Round up to next millisecond
591             long msTimeout = TimeUnit.NANOSECONDS.toMillis(remainingNanos + 999_999L);
592             if (msTimeout < 0) {
593                 // if wraps around then wait a long while
594                 msTimeout = Integer.MAX_VALUE;
595             }
596             boolean attempted = Blocker.begin();
597             try {
598                 waitForTimeoutInterruptibly(handle, msTimeout);
599             } finally {
600                 Blocker.end(attempted);
601             }
602             if (Thread.interrupted())
603                 throw new InterruptedException();
604             if (getExitCodeProcess(handle) != STILL_ACTIVE) {
605                 return true;
606             }
607             remainingNanos = deadline - System.nanoTime();
608         } while (remainingNanos > 0);
609 
610         return (getExitCodeProcess(handle) != STILL_ACTIVE);
611     }
612 
613     private static native void waitForTimeoutInterruptibly(
614         long handle, long timeoutMillis);
615 
616     @Override
617     public void destroy() {
618         terminateProcess(handle);
619     }
620 
< prev index next >