89 } else {
90 return new FileOutputStream(f);
91 }
92 }
93
94 // System-dependent portion of ProcessBuilder.start()
95 static Process start(String cmdarray[],
96 java.util.Map<String,String> environment,
97 String dir,
98 ProcessBuilder.Redirect[] redirects,
99 boolean redirectErrorStream)
100 throws IOException
101 {
102 String envblock = ProcessEnvironment.toEnvironmentBlock(environment);
103
104 FileInputStream f0 = null;
105 FileOutputStream f1 = null;
106 FileOutputStream f2 = null;
107
108 try {
109 long[] stdHandles;
110 if (redirects == null) {
111 stdHandles = new long[] { -1L, -1L, -1L };
112 } else {
113 stdHandles = new long[3];
114
115 if (redirects[0] == Redirect.PIPE) {
116 stdHandles[0] = -1L;
117 } else if (redirects[0] == Redirect.INHERIT) {
118 stdHandles[0] = fdAccess.getHandle(FileDescriptor.in);
119 } else if (redirects[0] instanceof ProcessBuilder.RedirectPipeImpl) {
120 stdHandles[0] = fdAccess.getHandle(((ProcessBuilder.RedirectPipeImpl) redirects[0]).getFd());
121 } else {
122 f0 = new FileInputStream(redirects[0].file());
123 stdHandles[0] = fdAccess.getHandle(f0.getFD());
124 }
125
126 if (redirects[1] == Redirect.PIPE) {
127 stdHandles[1] = -1L;
128 } else if (redirects[1] == Redirect.INHERIT) {
129 stdHandles[1] = fdAccess.getHandle(FileDescriptor.out);
130 } else if (redirects[1] instanceof ProcessBuilder.RedirectPipeImpl) {
131 stdHandles[1] = fdAccess.getHandle(((ProcessBuilder.RedirectPipeImpl) redirects[1]).getFd());
132 } else {
133 f1 = newFileOutputStream(redirects[1].file(),
134 redirects[1].append());
135 stdHandles[1] = fdAccess.getHandle(f1.getFD());
136 }
137
138 if (redirects[2] == Redirect.PIPE) {
139 stdHandles[2] = -1L;
140 } else if (redirects[2] == Redirect.INHERIT) {
141 stdHandles[2] = fdAccess.getHandle(FileDescriptor.err);
142 } else if (redirects[2] instanceof ProcessBuilder.RedirectPipeImpl) {
143 stdHandles[2] = fdAccess.getHandle(((ProcessBuilder.RedirectPipeImpl) redirects[2]).getFd());
144 } else {
145 f2 = newFileOutputStream(redirects[2].file(),
146 redirects[2].append());
147 stdHandles[2] = fdAccess.getHandle(f2.getFD());
148 }
149 }
150
151 Process p = new ProcessImpl(cmdarray, envblock, dir,
152 stdHandles, redirectErrorStream);
153 if (redirects != null) {
154 // Copy the handles's if they are to be redirected to another process
155 if (stdHandles[0] >= 0
156 && redirects[0] instanceof ProcessBuilder.RedirectPipeImpl) {
157 fdAccess.setHandle(((ProcessBuilder.RedirectPipeImpl) redirects[0]).getFd(),
158 stdHandles[0]);
159 }
160 if (stdHandles[1] >= 0
161 && redirects[1] instanceof ProcessBuilder.RedirectPipeImpl) {
162 fdAccess.setHandle(((ProcessBuilder.RedirectPipeImpl) redirects[1]).getFd(),
163 stdHandles[1]);
164 }
165 if (stdHandles[2] >= 0
166 && redirects[2] instanceof ProcessBuilder.RedirectPipeImpl) {
167 fdAccess.setHandle(((ProcessBuilder.RedirectPipeImpl) redirects[2]).getFd(),
168 stdHandles[2]);
169 }
170 }
171 return p;
172 } finally {
332 String upPath = executablePath.toUpperCase();
333 return (upPath.endsWith(".CMD") || upPath.endsWith(".BAT"));
334 }
335
336 private String quoteString(String arg) {
337 StringBuilder argbuf = new StringBuilder(arg.length() + 2);
338 return argbuf.append('"').append(arg).append('"').toString();
339 }
340
341
342 private final long handle;
343 private final ProcessHandle processHandle;
344 private OutputStream stdin_stream;
345 private InputStream stdout_stream;
346 private InputStream stderr_stream;
347
348 private ProcessImpl(String cmd[],
349 final String envblock,
350 final String path,
351 final long[] stdHandles,
352 final boolean redirectErrorStream)
353 throws IOException
354 {
355 String cmdstr;
356 SecurityManager security = System.getSecurityManager();
357 boolean allowAmbiguousCommands = false;
358 if (security == null) {
359 allowAmbiguousCommands = true;
360 String value = System.getProperty("jdk.lang.Process.allowAmbiguousCommands");
361 if (value != null)
362 allowAmbiguousCommands = !"false".equalsIgnoreCase(value);
363 }
364 if (allowAmbiguousCommands) {
365 // Legacy mode.
366
367 // Normalize path if possible.
368 String executablePath = new File(cmd[0]).getPath();
369
370 // No worry about internal, unpaired ["], and redirection/piping.
371 if (needsEscaping(VERIFICATION_LEGACY, executablePath) )
420 handle = create(cmdstr, envblock, path,
421 stdHandles, redirectErrorStream);
422 // Register a cleaning function to close the handle
423 final long local_handle = handle; // local to prevent capture of this
424 CleanerFactory.cleaner().register(this, () -> closeHandle(local_handle));
425
426 processHandle = ProcessHandleImpl.getInternal(getProcessId0(handle));
427
428 java.security.AccessController.doPrivileged(
429 new java.security.PrivilegedAction<Void>() {
430 public Void run() {
431 if (stdHandles[0] == -1L)
432 stdin_stream = ProcessBuilder.NullOutputStream.INSTANCE;
433 else {
434 FileDescriptor stdin_fd = new FileDescriptor();
435 fdAccess.setHandle(stdin_fd, stdHandles[0]);
436 stdin_stream = new BufferedOutputStream(
437 new FileOutputStream(stdin_fd));
438 }
439
440 if (stdHandles[1] == -1L)
441 stdout_stream = ProcessBuilder.NullInputStream.INSTANCE;
442 else {
443 FileDescriptor stdout_fd = new FileDescriptor();
444 fdAccess.setHandle(stdout_fd, stdHandles[1]);
445 stdout_stream = new BufferedInputStream(
446 new PipeInputStream(stdout_fd));
447 }
448
449 if (stdHandles[2] == -1L)
450 stderr_stream = ProcessBuilder.NullInputStream.INSTANCE;
451 else {
452 FileDescriptor stderr_fd = new FileDescriptor();
453 fdAccess.setHandle(stderr_fd, stdHandles[2]);
454 stderr_stream = new PipeInputStream(stderr_fd);
455 }
456
457 return null; }});
458 }
459
460 public OutputStream getOutputStream() {
|
89 } else {
90 return new FileOutputStream(f);
91 }
92 }
93
94 // System-dependent portion of ProcessBuilder.start()
95 static Process start(String cmdarray[],
96 java.util.Map<String,String> environment,
97 String dir,
98 ProcessBuilder.Redirect[] redirects,
99 boolean redirectErrorStream)
100 throws IOException
101 {
102 String envblock = ProcessEnvironment.toEnvironmentBlock(environment);
103
104 FileInputStream f0 = null;
105 FileOutputStream f1 = null;
106 FileOutputStream f2 = null;
107
108 try {
109 boolean forceNullOutputStream = false;
110 long[] stdHandles;
111 if (redirects == null) {
112 stdHandles = new long[] { -1L, -1L, -1L };
113 } else {
114 stdHandles = new long[3];
115
116 if (redirects[0] == Redirect.PIPE) {
117 stdHandles[0] = -1L;
118 } else if (redirects[0] == Redirect.INHERIT) {
119 stdHandles[0] = fdAccess.getHandle(FileDescriptor.in);
120 } else if (redirects[0] instanceof ProcessBuilder.RedirectPipeImpl) {
121 stdHandles[0] = fdAccess.getHandle(((ProcessBuilder.RedirectPipeImpl) redirects[0]).getFd());
122 } else {
123 f0 = new FileInputStream(redirects[0].file());
124 stdHandles[0] = fdAccess.getHandle(f0.getFD());
125 }
126
127 if (redirects[1] == Redirect.PIPE) {
128 stdHandles[1] = -1L;
129 } else if (redirects[1] == Redirect.INHERIT) {
130 stdHandles[1] = fdAccess.getHandle(FileDescriptor.out);
131 } else if (redirects[1] instanceof ProcessBuilder.RedirectPipeImpl) {
132 stdHandles[1] = fdAccess.getHandle(((ProcessBuilder.RedirectPipeImpl) redirects[1]).getFd());
133 // Force getInputStream to return a null stream,
134 // the handle is directly assigned to the next process.
135 forceNullOutputStream = true;
136 } else {
137 f1 = newFileOutputStream(redirects[1].file(),
138 redirects[1].append());
139 stdHandles[1] = fdAccess.getHandle(f1.getFD());
140 }
141
142 if (redirects[2] == Redirect.PIPE) {
143 stdHandles[2] = -1L;
144 } else if (redirects[2] == Redirect.INHERIT) {
145 stdHandles[2] = fdAccess.getHandle(FileDescriptor.err);
146 } else if (redirects[2] instanceof ProcessBuilder.RedirectPipeImpl) {
147 stdHandles[2] = fdAccess.getHandle(((ProcessBuilder.RedirectPipeImpl) redirects[2]).getFd());
148 } else {
149 f2 = newFileOutputStream(redirects[2].file(),
150 redirects[2].append());
151 stdHandles[2] = fdAccess.getHandle(f2.getFD());
152 }
153 }
154
155 Process p = new ProcessImpl(cmdarray, envblock, dir,
156 stdHandles, forceNullOutputStream, redirectErrorStream);
157 if (redirects != null) {
158 // Copy the handles's if they are to be redirected to another process
159 if (stdHandles[0] >= 0
160 && redirects[0] instanceof ProcessBuilder.RedirectPipeImpl) {
161 fdAccess.setHandle(((ProcessBuilder.RedirectPipeImpl) redirects[0]).getFd(),
162 stdHandles[0]);
163 }
164 if (stdHandles[1] >= 0
165 && redirects[1] instanceof ProcessBuilder.RedirectPipeImpl) {
166 fdAccess.setHandle(((ProcessBuilder.RedirectPipeImpl) redirects[1]).getFd(),
167 stdHandles[1]);
168 }
169 if (stdHandles[2] >= 0
170 && redirects[2] instanceof ProcessBuilder.RedirectPipeImpl) {
171 fdAccess.setHandle(((ProcessBuilder.RedirectPipeImpl) redirects[2]).getFd(),
172 stdHandles[2]);
173 }
174 }
175 return p;
176 } finally {
336 String upPath = executablePath.toUpperCase();
337 return (upPath.endsWith(".CMD") || upPath.endsWith(".BAT"));
338 }
339
340 private String quoteString(String arg) {
341 StringBuilder argbuf = new StringBuilder(arg.length() + 2);
342 return argbuf.append('"').append(arg).append('"').toString();
343 }
344
345
346 private final long handle;
347 private final ProcessHandle processHandle;
348 private OutputStream stdin_stream;
349 private InputStream stdout_stream;
350 private InputStream stderr_stream;
351
352 private ProcessImpl(String cmd[],
353 final String envblock,
354 final String path,
355 final long[] stdHandles,
356 boolean forceNullOutputStream,
357 final boolean redirectErrorStream)
358 throws IOException
359 {
360 String cmdstr;
361 SecurityManager security = System.getSecurityManager();
362 boolean allowAmbiguousCommands = false;
363 if (security == null) {
364 allowAmbiguousCommands = true;
365 String value = System.getProperty("jdk.lang.Process.allowAmbiguousCommands");
366 if (value != null)
367 allowAmbiguousCommands = !"false".equalsIgnoreCase(value);
368 }
369 if (allowAmbiguousCommands) {
370 // Legacy mode.
371
372 // Normalize path if possible.
373 String executablePath = new File(cmd[0]).getPath();
374
375 // No worry about internal, unpaired ["], and redirection/piping.
376 if (needsEscaping(VERIFICATION_LEGACY, executablePath) )
425 handle = create(cmdstr, envblock, path,
426 stdHandles, redirectErrorStream);
427 // Register a cleaning function to close the handle
428 final long local_handle = handle; // local to prevent capture of this
429 CleanerFactory.cleaner().register(this, () -> closeHandle(local_handle));
430
431 processHandle = ProcessHandleImpl.getInternal(getProcessId0(handle));
432
433 java.security.AccessController.doPrivileged(
434 new java.security.PrivilegedAction<Void>() {
435 public Void run() {
436 if (stdHandles[0] == -1L)
437 stdin_stream = ProcessBuilder.NullOutputStream.INSTANCE;
438 else {
439 FileDescriptor stdin_fd = new FileDescriptor();
440 fdAccess.setHandle(stdin_fd, stdHandles[0]);
441 stdin_stream = new BufferedOutputStream(
442 new FileOutputStream(stdin_fd));
443 }
444
445 if (stdHandles[1] == -1L || forceNullOutputStream)
446 stdout_stream = ProcessBuilder.NullInputStream.INSTANCE;
447 else {
448 FileDescriptor stdout_fd = new FileDescriptor();
449 fdAccess.setHandle(stdout_fd, stdHandles[1]);
450 stdout_stream = new BufferedInputStream(
451 new PipeInputStream(stdout_fd));
452 }
453
454 if (stdHandles[2] == -1L)
455 stderr_stream = ProcessBuilder.NullInputStream.INSTANCE;
456 else {
457 FileDescriptor stderr_fd = new FileDescriptor();
458 fdAccess.setHandle(stderr_fd, stdHandles[2]);
459 stderr_stream = new PipeInputStream(stderr_fd);
460 }
461
462 return null; }});
463 }
464
465 public OutputStream getOutputStream() {
|