Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/solaris/classes/java/lang/ProcessImpl.java
32287 views
/*1* Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.lang;2627import java.io.IOException;28import java.io.FileInputStream;29import java.io.FileOutputStream;30import java.lang.ProcessBuilder.Redirect;31import java.lang.ProcessBuilder.Redirect;3233/**34* This class is for the exclusive use of ProcessBuilder.start() to35* create new processes.36*37* @author Martin Buchholz38* @since 1.539*/40final class ProcessImpl {41private static final sun.misc.JavaIOFileDescriptorAccess fdAccess42= sun.misc.SharedSecrets.getJavaIOFileDescriptorAccess();4344private ProcessImpl() {} // Not instantiable4546private static byte[] toCString(String s) {47if (s == null)48return null;49byte[] bytes = s.getBytes();50byte[] result = new byte[bytes.length + 1];51System.arraycopy(bytes, 0,52result, 0,53bytes.length);54result[result.length-1] = (byte)0;55return result;56}5758// Only for use by ProcessBuilder.start()59static Process start(String[] cmdarray,60java.util.Map<String,String> environment,61String dir,62ProcessBuilder.Redirect[] redirects,63boolean redirectErrorStream)64throws IOException65{66assert cmdarray != null && cmdarray.length > 0;6768// Convert arguments to a contiguous block; it's easier to do69// memory management in Java than in C.70byte[][] args = new byte[cmdarray.length-1][];71int size = args.length; // For added NUL bytes72for (int i = 0; i < args.length; i++) {73args[i] = cmdarray[i+1].getBytes();74size += args[i].length;75}76byte[] argBlock = new byte[size];77int i = 0;78for (byte[] arg : args) {79System.arraycopy(arg, 0, argBlock, i, arg.length);80i += arg.length + 1;81// No need to write NUL bytes explicitly82}8384int[] envc = new int[1];85byte[] envBlock = ProcessEnvironment.toEnvironmentBlock(environment, envc);8687int[] std_fds;8889FileInputStream f0 = null;90FileOutputStream f1 = null;91FileOutputStream f2 = null;9293try {94if (redirects == null) {95std_fds = new int[] { -1, -1, -1 };96} else {97std_fds = new int[3];9899if (redirects[0] == Redirect.PIPE)100std_fds[0] = -1;101else if (redirects[0] == Redirect.INHERIT)102std_fds[0] = 0;103else {104f0 = new FileInputStream(redirects[0].file());105std_fds[0] = fdAccess.get(f0.getFD());106}107108if (redirects[1] == Redirect.PIPE)109std_fds[1] = -1;110else if (redirects[1] == Redirect.INHERIT)111std_fds[1] = 1;112else {113f1 = new FileOutputStream(redirects[1].file(),114redirects[1].append());115std_fds[1] = fdAccess.get(f1.getFD());116}117118if (redirects[2] == Redirect.PIPE)119std_fds[2] = -1;120else if (redirects[2] == Redirect.INHERIT)121std_fds[2] = 2;122else {123f2 = new FileOutputStream(redirects[2].file(),124redirects[2].append());125std_fds[2] = fdAccess.get(f2.getFD());126}127}128129return new UNIXProcess130(toCString(cmdarray[0]),131argBlock, args.length,132envBlock, envc[0],133toCString(dir),134std_fds,135redirectErrorStream);136} finally {137// In theory, close() can throw IOException138// (although it is rather unlikely to happen here)139try { if (f0 != null) f0.close(); }140finally {141try { if (f1 != null) f1.close(); }142finally { if (f2 != null) f2.close(); }143}144}145}146}147148149