1 /* 2 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 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.IOException; 29 import java.io.FileInputStream; 30 import java.io.FileOutputStream; 31 import java.lang.ProcessBuilder.Redirect; 32 import java.lang.ProcessBuilder.Redirect; 33 34 /** 35 * This class is for the exclusive use of ProcessBuilder.start() to 36 * create new processes. 37 * 38 * @author Martin Buchholz 39 * @since 1.5 40 */ 41 final class ProcessImpl { 42 // Android-changed: Use FileDescriptor.getInt$() instead of fdAccess.get(...). 43 // private static final sun.misc.JavaIOFileDescriptorAccess fdAccess 44 // = sun.misc.SharedSecrets.getJavaIOFileDescriptorAccess(); 45 ProcessImpl()46 private ProcessImpl() {} // Not instantiable 47 toCString(String s)48 private static byte[] toCString(String s) { 49 if (s == null) 50 return null; 51 byte[] bytes = s.getBytes(); 52 byte[] result = new byte[bytes.length + 1]; 53 System.arraycopy(bytes, 0, 54 result, 0, 55 bytes.length); 56 result[result.length-1] = (byte)0; 57 return result; 58 } 59 60 // Only for use by ProcessBuilder.start() start(String[] cmdarray, java.util.Map<String,String> environment, String dir, ProcessBuilder.Redirect[] redirects, boolean redirectErrorStream)61 static Process start(String[] cmdarray, 62 java.util.Map<String,String> environment, 63 String dir, 64 ProcessBuilder.Redirect[] redirects, 65 boolean redirectErrorStream) 66 throws IOException 67 { 68 assert cmdarray != null && cmdarray.length > 0; 69 70 // Convert arguments to a contiguous block; it's easier to do 71 // memory management in Java than in C. 72 byte[][] args = new byte[cmdarray.length-1][]; 73 int size = args.length; // For added NUL bytes 74 for (int i = 0; i < args.length; i++) { 75 args[i] = cmdarray[i+1].getBytes(); 76 size += args[i].length; 77 } 78 byte[] argBlock = new byte[size]; 79 int i = 0; 80 for (byte[] arg : args) { 81 System.arraycopy(arg, 0, argBlock, i, arg.length); 82 i += arg.length + 1; 83 // No need to write NUL bytes explicitly 84 } 85 86 int[] envc = new int[1]; 87 byte[] envBlock = ProcessEnvironment.toEnvironmentBlock(environment, envc); 88 89 int[] std_fds; 90 91 FileInputStream f0 = null; 92 FileOutputStream f1 = null; 93 FileOutputStream f2 = null; 94 95 try { 96 if (redirects == null) { 97 std_fds = new int[] { -1, -1, -1 }; 98 } else { 99 std_fds = new int[3]; 100 101 if (redirects[0] == Redirect.PIPE) 102 std_fds[0] = -1; 103 else if (redirects[0] == Redirect.INHERIT) 104 std_fds[0] = 0; 105 else { 106 f0 = new FileInputStream(redirects[0].file()); 107 // Android-changed: Use FileDescriptor.getInt$() instead of fdAccess.get(...). 108 // std_fds[0] = fdAccess.get(f0.getFD()); 109 std_fds[0] = f0.getFD().getInt$(); 110 } 111 112 if (redirects[1] == Redirect.PIPE) 113 std_fds[1] = -1; 114 else if (redirects[1] == Redirect.INHERIT) 115 std_fds[1] = 1; 116 else { 117 f1 = new FileOutputStream(redirects[1].file(), 118 redirects[1].append()); 119 // Android-changed: Use FileDescriptor.getInt$() instead of fdAccess.get(...). 120 // std_fds[1] = fdAccess.get(f1.getFD()); 121 std_fds[1] = f1.getFD().getInt$(); 122 } 123 124 if (redirects[2] == Redirect.PIPE) 125 std_fds[2] = -1; 126 else if (redirects[2] == Redirect.INHERIT) 127 std_fds[2] = 2; 128 else { 129 f2 = new FileOutputStream(redirects[2].file(), 130 redirects[2].append()); 131 // Android-changed: Use FileDescriptor.getInt$() instead of fdAccess.get(...). 132 // std_fds[2] = fdAccess.get(f2.getFD()); 133 std_fds[2] = f2.getFD().getInt$(); 134 } 135 } 136 137 return new UNIXProcess 138 (toCString(cmdarray[0]), 139 argBlock, args.length, 140 envBlock, envc[0], 141 toCString(dir), 142 std_fds, 143 redirectErrorStream); 144 } finally { 145 // In theory, close() can throw IOException 146 // (although it is rather unlikely to happen here) 147 try { if (f0 != null) f0.close(); } 148 finally { 149 try { if (f1 != null) f1.close(); } 150 finally { if (f2 != null) f2.close(); } 151 } 152 } 153 } 154 } 155