changeset 13061:a92a6bd00459

8132394: (process) ProcessBuilder support for a pipeline of processes Reviewed-by: psandoz, alanb
author rriggs
date Fri, 13 Nov 2015 15:48:59 -0500
parents 2891d078c753
children d22e834f6bfb
files src/java.base/share/classes/java/lang/ProcessBuilder.java src/java.base/unix/classes/java/lang/ProcessImpl.java src/java.base/windows/classes/java/lang/ProcessImpl.java test/java/lang/ProcessBuilder/PipelineTest.java
diffstat 4 files changed, 596 insertions(+), 25 deletions(-) [+]
line wrap: on
line diff
--- a/src/java.base/share/classes/java/lang/ProcessBuilder.java	Fri Nov 13 12:00:23 2015 -0500
+++ b/src/java.base/share/classes/java/lang/ProcessBuilder.java	Fri Nov 13 15:48:59 2015 -0500
@@ -26,9 +26,11 @@
 package java.lang;
 
 import java.io.File;
+import java.io.FileDescriptor;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
+import java.nio.channels.Pipe;
 import java.util.Arrays;
 import java.util.ArrayList;
 import java.util.List;
@@ -43,6 +45,11 @@
  * {@link Process} instance with those attributes.  The {@link
  * #start()} method can be invoked repeatedly from the same instance
  * to create new subprocesses with identical or related attributes.
+ * <p>
+ * The {@link #startPipeline startPipeline} method can be invoked to create
+ * a pipeline of new processes that send the output of each process
+ * directly to the next process.  Each process has the attributes of
+ * its respective ProcessBuilder.
  *
  * <p>Each process builder manages these process attributes:
  *
@@ -696,11 +703,37 @@
         private Redirect() {}
     }
 
+    /**
+     * Private implementation subclass of Redirect that holds a FileDescriptor for the
+     * output of a previously started Process.
+     * The FileDescriptor is used as the standard input of the next Process
+     * to be started.
+     */
+    static class RedirectPipeImpl extends Redirect {
+        final FileDescriptor fd;
+
+        RedirectPipeImpl() {
+            this.fd = new FileDescriptor();
+        }
+        @Override
+        public Type type() { return Type.PIPE; }
+
+        @Override
+        public String toString() { return type().toString();}
+
+        FileDescriptor getFd() { return fd; }
+    }
+
+    /**
+     * Return the array of redirects, creating the default as needed.
+     * @return the array of redirects
+     */
     private Redirect[] redirects() {
-        if (redirects == null)
+        if (redirects == null) {
             redirects = new Redirect[] {
-                Redirect.PIPE, Redirect.PIPE, Redirect.PIPE
+                    Redirect.PIPE, Redirect.PIPE, Redirect.PIPE
             };
+        }
         return redirects;
     }
 
@@ -1039,6 +1072,18 @@
      * @see Runtime#exec(String[], String[], java.io.File)
      */
     public Process start() throws IOException {
+        return start(redirects);
+    }
+
+    /**
+     * Start a new Process using an explicit array of redirects.
+     * See {@link #start} for details of starting each Process.
+     *
+     * @param redirect array of redirects for stdin, stdout, stderr
+     * @return the new Process
+     * @throws IOException if an I/O error occurs
+     */
+    private Process start(Redirect[] redirects) throws IOException {
         // Must convert to array first -- a malicious user-supplied
         // list might try to circumvent the security check.
         String[] cmdarray = command.toArray(new String[command.size()]);
@@ -1089,4 +1134,171 @@
                 cause);
         }
     }
+
+    /**
+     * Starts a Process for each ProcessBuilder, creating a pipeline of
+     * processes linked by their standard output and standard input streams.
+     * The attributes of each ProcessBuilder are used to start the respective
+     * process except that as each process is started, its standard output
+     * is directed to the standard input of the next.  The redirects for standard
+     * input of the first process and standard output of the last process are
+     * initialized using the redirect settings of the respective ProcessBuilder.
+     * All other {@code ProcessBuilder} redirects should be
+     * {@link Redirect#PIPE Redirect.PIPE}.
+     * <p>
+     * All input and output streams between the intermediate processes are
+     * not accessible.
+     * The {@link Process#getOutputStream standard input} of all processes
+     * except the first process are <i>null output streams</i>
+     * The {@link Process#getInputStream standard output} of all processes
+     * except the last process are <i>null input streams</i>.
+     * <p>
+     * The {@link #redirectErrorStream} of each ProcessBuilder applies to the
+     * respective process.  If set to {@code true}, the error stream is written
+     * to the same stream as standard output.
+     * <p>
+     * If starting any of the processes throws an Exception, all processes
+     * are forcibly destroyed.
+     * <p>
+     * The {@code startPipeline} method performs the same checks on
+     * each ProcessBuilder as does the {@link #start} method. The new process
+     * will invoke the command and arguments given by {@link #command()},
+     * in a working directory as given by {@link #directory()},
+     * with a process environment as given by {@link #environment()}.
+     * <p>
+     * This method checks that the command is a valid operating
+     * system command.  Which commands are valid is system-dependent,
+     * but at the very least the command must be a non-empty list of
+     * non-null strings.
+     * <p>
+     * A minimal set of system dependent environment variables may
+     * be required to start a process on some operating systems.
+     * As a result, the subprocess may inherit additional environment variable
+     * settings beyond those in the process builder's {@link #environment()}.
+     * <p>
+     * If there is a security manager, its
+     * {@link SecurityManager#checkExec checkExec}
+     * method is called with the first component of this object's
+     * {@code command} array as its argument. This may result in
+     * a {@link SecurityException} being thrown.
+     * <p>
+     * Starting an operating system process is highly system-dependent.
+     * Among the many things that can go wrong are:
+     * <ul>
+     * <li>The operating system program file was not found.
+     * <li>Access to the program file was denied.
+     * <li>The working directory does not exist.
+     * <li>Invalid character in command argument, such as NUL.
+     * </ul>
+     * <p>
+     * In such cases an exception will be thrown.  The exact nature
+     * of the exception is system-dependent, but it will always be a
+     * subclass of {@link IOException}.
+     * <p>
+     * If the operating system does not support the creation of
+     * processes, an {@link UnsupportedOperationException} will be thrown.
+     * <p>
+     * Subsequent modifications to this process builder will not
+     * affect the returned {@link Process}.
+     * @apiNote
+     * For example to count the unique imports for all the files in a file hierarchy
+     * on a Unix compatible platform:
+     * <pre>{@code
+     * String directory = "/home/duke/src";
+     * ProcessBuilder[] builders = {
+     *              new ProcessBuilder("find", directory, "-type", "f"),
+                    new ProcessBuilder("xargs", "grep", "-h", "^import "),
+                    new ProcessBuilder("awk", "{print $2;}"),
+                    new ProcessBuilder("sort", "-u")};
+     * List<Process> processes = ProcessBuilder.startPipeline(
+     *         Arrays.asList(builders));
+     * Process last = processes.get(processes.size()-1);
+     * try (InputStream is = last.getInputStream();
+     *         Reader isr = new InputStreamReader(is);
+     *         BufferedReader r = new BufferedReader(isr)) {
+     *     long count = r.lines().count();
+     * }
+     * }</pre>
+     *
+     * @param builders a List of ProcessBuilders
+     * @return a {@code List<Process>}es started from the corresponding
+     *         ProcessBuilder
+     * @throws IllegalArgumentException any of the redirects except the
+     *          standard input of the first builder and the standard output of
+     *          the last builder are not {@link Redirect#PIPE}.
+     * @throws NullPointerException
+     *         if an element of the command list is null or
+     *         if an element of the ProcessBuilder list is null or
+     *         the builders argument is null
+     * @throws IndexOutOfBoundsException
+     *         if the command is an empty list (has size {@code 0})
+     * @throws SecurityException
+     *         if a security manager exists and
+     *         <ul>
+     *         <li>its
+     *         {@link SecurityManager#checkExec checkExec}
+     *         method doesn't allow creation of the subprocess, or
+     *         <li>the standard input to the subprocess was
+     *         {@linkplain #redirectInput redirected from a file}
+     *         and the security manager's
+     *         {@link SecurityManager#checkRead(String) checkRead} method
+     *         denies read access to the file, or
+     *         <li>the standard output or standard error of the
+     *         subprocess was
+     *         {@linkplain #redirectOutput redirected to a file}
+     *         and the security manager's
+     *         {@link SecurityManager#checkWrite(String) checkWrite} method
+     *         denies write access to the file
+     *         </ul>
+     *
+     * @throws  UnsupportedOperationException
+     *          If the operating system does not support the creation of processes
+     *
+     * @throws IOException if an I/O error occurs
+     */
+    public static List<Process> startPipeline(List<ProcessBuilder> builders) throws IOException {
+        // Accumulate and check the builders
+        final int numBuilders = builders.size();
+        List<Process> processes = new ArrayList<>(numBuilders);
+        try {
+            Redirect prevOutput = null;
+            for (int index = 0; index < builders.size(); index++) {
+                ProcessBuilder builder = builders.get(index);
+                Redirect[] redirects = builder.redirects();
+                if (index > 0) {
+                    // check the current Builder to see if it can take input from the previous
+                    if (builder.redirectInput() != Redirect.PIPE) {
+                        throw new IllegalArgumentException("builder redirectInput()" +
+                                " must be PIPE except for the first builder: "
+                                + builder.redirectInput());
+                    }
+                    redirects[0] = prevOutput;
+                }
+                if (index < numBuilders - 1) {
+                    // check all but the last stage has output = PIPE
+                    if (builder.redirectOutput() != Redirect.PIPE) {
+                        throw new IllegalArgumentException("builder redirectOutput()" +
+                                " must be PIPE except for the last builder: "
+                                + builder.redirectOutput());
+                    }
+                    redirects[1] = new RedirectPipeImpl();  // placeholder for new output
+                }
+                processes.add(builder.start(redirects));
+                prevOutput = redirects[1];
+            }
+        } catch (Exception ex) {
+            // Cleanup processes already started
+            processes.forEach(Process::destroyForcibly);
+            processes.forEach(p -> {
+                try {
+                    p.waitFor();        // Wait for it to exit
+                } catch (InterruptedException ie) {
+                    // If interrupted; continue with next Process
+                    Thread.currentThread().interrupt();
+                }
+            });
+            throw ex;
+        }
+        return processes;
+    }
 }
--- a/src/java.base/unix/classes/java/lang/ProcessImpl.java	Fri Nov 13 12:00:23 2015 -0500
+++ b/src/java.base/unix/classes/java/lang/ProcessImpl.java	Fri Nov 13 15:48:59 2015 -0500
@@ -224,48 +224,75 @@
         FileOutputStream f2 = null;
 
         try {
+            boolean forceNullOutputStream = false;
             if (redirects == null) {
                 std_fds = new int[] { -1, -1, -1 };
             } else {
                 std_fds = new int[3];
 
-                if (redirects[0] == Redirect.PIPE)
+                if (redirects[0] == Redirect.PIPE) {
                     std_fds[0] = -1;
-                else if (redirects[0] == Redirect.INHERIT)
+                } else if (redirects[0] == Redirect.INHERIT) {
                     std_fds[0] = 0;
-                else {
+                } else if (redirects[0] instanceof ProcessBuilder.RedirectPipeImpl) {
+                    std_fds[0] = fdAccess.get(((ProcessBuilder.RedirectPipeImpl) redirects[0]).getFd());
+                } else {
                     f0 = new FileInputStream(redirects[0].file());
                     std_fds[0] = fdAccess.get(f0.getFD());
                 }
 
-                if (redirects[1] == Redirect.PIPE)
+                if (redirects[1] == Redirect.PIPE) {
                     std_fds[1] = -1;
-                else if (redirects[1] == Redirect.INHERIT)
+                } else if (redirects[1] == Redirect.INHERIT) {
                     std_fds[1] = 1;
-                else {
+                } else if (redirects[1] instanceof ProcessBuilder.RedirectPipeImpl) {
+                    std_fds[1] = fdAccess.get(((ProcessBuilder.RedirectPipeImpl) redirects[1]).getFd());
+                    // Force getInputStream to return a null stream,
+                    // the fd is directly assigned to the next process.
+                    forceNullOutputStream = true;
+                } else {
                     f1 = new FileOutputStream(redirects[1].file(),
                             redirects[1].append());
                     std_fds[1] = fdAccess.get(f1.getFD());
                 }
 
-                if (redirects[2] == Redirect.PIPE)
+                if (redirects[2] == Redirect.PIPE) {
                     std_fds[2] = -1;
-                else if (redirects[2] == Redirect.INHERIT)
+                } else if (redirects[2] == Redirect.INHERIT) {
                     std_fds[2] = 2;
-                else {
+                } else if (redirects[2] instanceof ProcessBuilder.RedirectPipeImpl) {
+                    std_fds[2] = fdAccess.get(((ProcessBuilder.RedirectPipeImpl) redirects[2]).getFd());
+                } else {
                     f2 = new FileOutputStream(redirects[2].file(),
                             redirects[2].append());
                     std_fds[2] = fdAccess.get(f2.getFD());
                 }
             }
 
-            return new ProcessImpl
+            Process p = new ProcessImpl
                     (toCString(cmdarray[0]),
                             argBlock, args.length,
                             envBlock, envc[0],
                             toCString(dir),
                             std_fds,
+                            forceNullOutputStream,
                             redirectErrorStream);
+            if (redirects != null) {
+                // Copy the fd's if they are to be redirected to another process
+                if (std_fds[0] >= 0 &&
+                        redirects[0] instanceof ProcessBuilder.RedirectPipeImpl) {
+                    fdAccess.set(((ProcessBuilder.RedirectPipeImpl) redirects[0]).getFd(), std_fds[0]);
+                }
+                if (std_fds[1] >= 0 &&
+                        redirects[1] instanceof ProcessBuilder.RedirectPipeImpl) {
+                    fdAccess.set(((ProcessBuilder.RedirectPipeImpl) redirects[1]).getFd(), std_fds[1]);
+                }
+                if (std_fds[2] >= 0 &&
+                        redirects[2] instanceof ProcessBuilder.RedirectPipeImpl) {
+                    fdAccess.set(((ProcessBuilder.RedirectPipeImpl) redirects[2]).getFd(), std_fds[2]);
+                }
+            }
+            return p;
         } finally {
             // In theory, close() can throw IOException
             // (although it is rather unlikely to happen here)
@@ -311,6 +338,7 @@
                 final byte[] envBlock, final int envc,
                 final byte[] dir,
                 final int[] fds,
+                final boolean forceNullOutputStream,
                 final boolean redirectErrorStream)
             throws IOException {
 
@@ -326,7 +354,7 @@
 
         try {
             doPrivileged((PrivilegedExceptionAction<Void>) () -> {
-                initStreams(fds);
+                initStreams(fds, forceNullOutputStream);
                 return null;
             });
         } catch (PrivilegedActionException ex) {
@@ -340,7 +368,14 @@
         return fileDescriptor;
     }
 
-    void initStreams(int[] fds) throws IOException {
+    /**
+     * Initialize the streams from the file descriptors.
+     * @param fds array of stdin, stdout, stderr fds
+     * @param forceNullOutputStream true if the stdout is being directed to
+     *        a subsequent process. The stdout stream should be a null output stream .
+     * @throws IOException
+     */
+    void initStreams(int[] fds, boolean forceNullOutputStream) throws IOException {
         switch (platform) {
             case LINUX:
             case BSD:
@@ -348,7 +383,7 @@
                         ProcessBuilder.NullOutputStream.INSTANCE :
                         new ProcessPipeOutputStream(fds[0]);
 
-                stdout = (fds[1] == -1) ?
+                stdout = (fds[1] == -1 || forceNullOutputStream) ?
                          ProcessBuilder.NullInputStream.INSTANCE :
                          new ProcessPipeInputStream(fds[1]);
 
--- a/src/java.base/windows/classes/java/lang/ProcessImpl.java	Fri Nov 13 12:00:23 2015 -0500
+++ b/src/java.base/windows/classes/java/lang/ProcessImpl.java	Fri Nov 13 15:48:59 2015 -0500
@@ -110,38 +110,63 @@
             } else {
                 stdHandles = new long[3];
 
-                if (redirects[0] == Redirect.PIPE)
+                if (redirects[0] == Redirect.PIPE) {
                     stdHandles[0] = -1L;
-                else if (redirects[0] == Redirect.INHERIT)
+                } else if (redirects[0] == Redirect.INHERIT) {
                     stdHandles[0] = fdAccess.getHandle(FileDescriptor.in);
-                else {
+                } else if (redirects[0] instanceof ProcessBuilder.RedirectPipeImpl) {
+                    stdHandles[0] = fdAccess.getHandle(((ProcessBuilder.RedirectPipeImpl) redirects[0]).getFd());
+                } else {
                     f0 = new FileInputStream(redirects[0].file());
                     stdHandles[0] = fdAccess.getHandle(f0.getFD());
                 }
 
-                if (redirects[1] == Redirect.PIPE)
+                if (redirects[1] == Redirect.PIPE) {
                     stdHandles[1] = -1L;
-                else if (redirects[1] == Redirect.INHERIT)
+                } else if (redirects[1] == Redirect.INHERIT) {
                     stdHandles[1] = fdAccess.getHandle(FileDescriptor.out);
-                else {
+                } else if (redirects[1] instanceof ProcessBuilder.RedirectPipeImpl) {
+                    stdHandles[1] = fdAccess.getHandle(((ProcessBuilder.RedirectPipeImpl) redirects[1]).getFd());
+                } else {
                     f1 = newFileOutputStream(redirects[1].file(),
                                              redirects[1].append());
                     stdHandles[1] = fdAccess.getHandle(f1.getFD());
                 }
 
-                if (redirects[2] == Redirect.PIPE)
+                if (redirects[2] == Redirect.PIPE) {
                     stdHandles[2] = -1L;
-                else if (redirects[2] == Redirect.INHERIT)
+                } else if (redirects[2] == Redirect.INHERIT) {
                     stdHandles[2] = fdAccess.getHandle(FileDescriptor.err);
-                else {
+                } else if (redirects[2] instanceof ProcessBuilder.RedirectPipeImpl) {
+                    stdHandles[2] = fdAccess.getHandle(((ProcessBuilder.RedirectPipeImpl) redirects[2]).getFd());
+                } else {
                     f2 = newFileOutputStream(redirects[2].file(),
                                              redirects[2].append());
                     stdHandles[2] = fdAccess.getHandle(f2.getFD());
                 }
             }
 
-            return new ProcessImpl(cmdarray, envblock, dir,
+            Process p = new ProcessImpl(cmdarray, envblock, dir,
                                    stdHandles, redirectErrorStream);
+            if (redirects != null) {
+                // Copy the handles's if they are to be redirected to another process
+                if (stdHandles[0] >= 0
+                        && redirects[0] instanceof ProcessBuilder.RedirectPipeImpl) {
+                    fdAccess.setHandle(((ProcessBuilder.RedirectPipeImpl) redirects[0]).getFd(),
+                            stdHandles[0]);
+                }
+                if (stdHandles[1] >= 0
+                        && redirects[1] instanceof ProcessBuilder.RedirectPipeImpl) {
+                    fdAccess.setHandle(((ProcessBuilder.RedirectPipeImpl) redirects[1]).getFd(),
+                            stdHandles[1]);
+                }
+                if (stdHandles[2] >= 0
+                        && redirects[2] instanceof ProcessBuilder.RedirectPipeImpl) {
+                    fdAccess.setHandle(((ProcessBuilder.RedirectPipeImpl) redirects[2]).getFd(),
+                            stdHandles[2]);
+                }
+            }
+            return p;
         } finally {
             // In theory, close() can throw IOException
             // (although it is rather unlikely to happen here)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/java/lang/ProcessBuilder/PipelineTest.java	Fri Nov 13 15:48:59 2015 -0500
@@ -0,0 +1,299 @@
+/*
+ * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ *
+ */
+
+import java.io.File;
+import java.io.FileReader;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.Reader;
+import java.io.Writer;
+import java.util.Arrays;
+import java.util.List;
+
+/*
+ * @test PipelineTest
+ */
+
+public class PipelineTest {
+
+    private static void realMain(String[] args) throws Throwable {
+        t1_simplePipeline();
+        t2_translatePipeline();
+        t3_redirectErrorStream();
+        t4_failStartPipeline();
+    }
+
+    /**
+     * Return a list of the varargs arguments.
+     * @param args elements to include in the list
+     * @param <T> the type of the elements
+     * @return a {@code List<T>} of the arguments
+     */
+    @SafeVarargs
+    @SuppressWarnings("varargs")
+    static <T> List<T> asList(T... args) {
+        return Arrays.asList(args);
+    }
+
+    /**
+     * T1 - simple copy between two processes
+     */
+    static void t1_simplePipeline() {
+        try {
+            String s1 = "Now is the time to check!";
+            verify(s1, s1,
+                    asList(new ProcessBuilder("cat")));
+            verify(s1, s1,
+                    asList(new ProcessBuilder("cat"),
+                            new ProcessBuilder("cat")));
+            verify(s1, s1,
+                    asList(new ProcessBuilder("cat"),
+                            new ProcessBuilder("cat"),
+                            new ProcessBuilder("cat")));
+        } catch (Throwable t) {
+            unexpected(t);
+        }
+    }
+
+    /**
+     * Pipeline that modifies the content.
+     */
+    static void t2_translatePipeline() {
+        try {
+            String s2 = "Now is the time to check!";
+            String r2 = s2.replace('e', 'E').replace('o', 'O');
+            verify(s2, r2,
+                    asList(new ProcessBuilder("tr", "e", "E"),
+                            new ProcessBuilder("tr", "o", "O")));
+        } catch (Throwable t) {
+            unexpected(t);
+        }
+    }
+
+    /**
+     * Test that redirectErrorStream sends standard error of the first process
+     * to the standard output. The standard error of the first process should be empty.
+     * The standard output of the 2nd should contain the error message including the bad file name.
+     */
+    static void t3_redirectErrorStream() {
+        try {
+            File p1err = new File("p1-test.err");
+            File p2out = new File("p2-test.out");
+
+            List<Process> processes = ProcessBuilder.startPipeline(
+                    asList(new ProcessBuilder("cat", "NON-EXISTENT-FILE")
+                                    .redirectErrorStream(true)
+                                    .redirectError(p1err),
+                            new ProcessBuilder("cat").redirectOutput(p2out)));
+            waitForAll(processes);
+
+            check("".equals(fileContents(p1err)), "The first process standard error should be empty");
+            String p2contents = fileContents(p2out);
+            check(p2contents.contains("NON-EXISTENT-FILE"),
+                    "The error from the first process should be in the output of the second: " + p2contents);
+        } catch (Throwable t) {
+            unexpected(t);
+        }
+    }
+
+    /**
+     * Test that no processes are left after a failed startPipeline.
+     * Test illegal combinations of redirects.
+     */
+    static void t4_failStartPipeline() {
+        File p1err = new File("p1-test.err");
+        File p2out = new File("p2-test.out");
+
+        THROWS(IllegalArgumentException.class,
+                () -> {
+                    // Test that output redirect != PIPE throws IAE
+                    List<Process> processes = ProcessBuilder.startPipeline(
+                            asList(new ProcessBuilder("cat", "NON-EXISTENT-FILE1")
+                                            .redirectOutput(p1err),
+                                    new ProcessBuilder("cat")));
+                },
+                () -> {
+                    // Test that input redirect != PIPE throws IAE
+                    List<Process> processes = ProcessBuilder.startPipeline(
+                            asList(new ProcessBuilder("cat", "NON-EXISTENT-FILE2"),
+                                    new ProcessBuilder("cat").redirectInput(p2out)));
+                }
+        );
+
+        THROWS(NullPointerException.class,
+                () -> {
+                    List<Process> processes = ProcessBuilder.startPipeline(
+                            asList(new ProcessBuilder("cat", "a"), null));
+                },
+                () -> {
+                    List<Process> processes = ProcessBuilder.startPipeline(
+                            asList(null, new ProcessBuilder("cat", "b")));
+                }
+        );
+
+        THROWS(IOException.class,
+                () -> {
+                    List<Process> processes = ProcessBuilder.startPipeline(
+                            asList(new ProcessBuilder("cat", "c"),
+                                    new ProcessBuilder("NON-EXISTENT-COMMAND")));
+                });
+
+        // Check no subprocess are left behind
+        ProcessHandle.current().children().forEach(PipelineTest::print);
+        ProcessHandle.current().children()
+                .filter(p -> p.info().command().orElse("").contains("cat"))
+                .forEach(p -> fail("process should have been destroyed: " + p));
+    }
+
+    static void verify(String input, String expected, List<ProcessBuilder> builders) throws IOException {
+        File infile = new File("test.in");
+        File outfile = new File("test.out");
+        setFileContents(infile, expected);
+        for (int i = 0; i < builders.size(); i++) {
+            ProcessBuilder b = builders.get(i);
+            if (i == 0) {
+                b.redirectInput(infile);
+            }
+            if (i == builders.size() - 1) {
+                b.redirectOutput(outfile);
+            }
+        }
+        List<Process> processes = ProcessBuilder.startPipeline(builders);
+        verifyProcesses(processes);
+        waitForAll(processes);
+        String result = fileContents(outfile);
+        System.out.printf(" in: %s%nout: %s%n", input, expected);
+        check(result.equals(expected), "result not as expected");
+    }
+
+    /**
+     * Wait for each of the processes to be done.
+     *
+     * @param processes the list  of processes to check
+     */
+    static void waitForAll(List<Process> processes) {
+        processes.forEach(p -> {
+            try {
+                int status = p.waitFor();
+            } catch (InterruptedException ie) {
+                unexpected(ie);
+            }
+        });
+    }
+
+    static void print(ProcessBuilder pb) {
+        if (pb != null) {
+            System.out.printf(" pb: %s%n", pb);
+            System.out.printf("    cmd: %s%n", pb.command());
+        }
+    }
+
+    static void print(ProcessHandle p) {
+        System.out.printf("process: pid: %d, info: %s%n",
+                p.getPid(), p.info());
+    }
+
+    // Check various aspects of the processes
+    static void verifyProcesses(List<Process> processes) {
+        for (int i = 0; i < processes.size(); i++) {
+            Process p = processes.get(i);
+            if (i != 0) {
+                verifyNullStream(p.getOutputStream(), "getOutputStream");
+            }
+            if (i == processes.size() - 1) {
+                verifyNullStream(p.getInputStream(), "getInputStream");
+                verifyNullStream(p.getErrorStream(), "getErrorStream");
+            }
+        }
+    }
+
+    static void verifyNullStream(OutputStream s, String msg) {
+        try {
+            s.write(0xff);
+            fail("Stream should have been a NullStream" + msg);
+        } catch (IOException ie) {
+            // expected
+        }
+    }
+
+    static void verifyNullStream(InputStream s, String msg) {
+        try {
+            int len = s.read();
+            check(len == -1, "Stream should have been a NullStream" + msg);
+        } catch (IOException ie) {
+            // expected
+        }
+    }
+
+    static void setFileContents(File file, String contents) {
+        try {
+            Writer w = new FileWriter(file);
+            w.write(contents);
+            w.close();
+        } catch (Throwable t) { unexpected(t); }
+    }
+
+    static String fileContents(File file) {
+        try {
+            Reader r = new FileReader(file);
+            StringBuilder sb = new StringBuilder();
+            char[] buffer = new char[1024];
+            int n;
+            while ((n = r.read(buffer)) != -1)
+                sb.append(buffer,0,n);
+            r.close();
+            return new String(sb);
+        } catch (Throwable t) { unexpected(t); return ""; }
+    }
+
+    //--------------------- Infrastructure ---------------------------
+    static volatile int passed = 0, failed = 0;
+    static void pass() {passed++;}
+    static void fail() {failed++; Thread.dumpStack();}
+    static void fail(String msg) {System.err.println(msg); fail();}
+    static void unexpected(Throwable t) {failed++; t.printStackTrace();}
+    static void check(boolean cond) {if (cond) pass(); else fail();}
+    static void check(boolean cond, String m) {if (cond) pass(); else fail(m);}
+    static void equal(Object x, Object y) {
+        if (x == null ? y == null : x.equals(y)) pass();
+        else fail(">'" + x + "'<" + " not equal to " + "'" + y + "'");
+    }
+
+    public static void main(String[] args) throws Throwable {
+        try {realMain(args);} catch (Throwable t) {unexpected(t);}
+        System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
+        if (failed > 0) throw new AssertionError("Some tests failed");
+    }
+    interface Fun {void f() throws Throwable;}
+    static void THROWS(Class<? extends Throwable> k, Fun... fs) {
+        for (Fun f : fs)
+            try { f.f(); fail("Expected " + k.getName() + " not thrown"); }
+            catch (Throwable t) {
+                if (k.isAssignableFrom(t.getClass())) pass();
+                else unexpected(t);}
+    }
+
+}