jdk/src/windows/classes/java/lang/ProcessImpl.java
author ohair
Tue, 28 Dec 2010 15:53:50 -0800
changeset 7668 d4a77089c587
parent 7515 43202796198e
child 10903 dfb253adb28f
permissions -rw-r--r--
6962318: Update copyright year Reviewed-by: xdono
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7668
d4a77089c587 6962318: Update copyright year
ohair
parents: 7515
diff changeset
     2
 * Copyright (c) 1995, 2010, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.lang;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    28
import java.io.IOException;
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    29
import java.io.File;
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    30
import java.io.InputStream;
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    31
import java.io.OutputStream;
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    32
import java.io.FileInputStream;
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    33
import java.io.FileOutputStream;
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    34
import java.io.FileDescriptor;
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    35
import java.io.BufferedInputStream;
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    36
import java.io.BufferedOutputStream;
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    37
import java.lang.ProcessBuilder.Redirect;
7515
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5787
diff changeset
    38
import java.security.AccessController;
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5787
diff changeset
    39
import java.security.PrivilegedAction;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
/* This class is for the exclusive use of ProcessBuilder.start() to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * create new processes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * @author Martin Buchholz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * @since   1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
final class ProcessImpl extends Process {
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    49
    private static final sun.misc.JavaIOFileDescriptorAccess fdAccess
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    50
        = sun.misc.SharedSecrets.getJavaIOFileDescriptorAccess();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
7515
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5787
diff changeset
    52
    /**
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5787
diff changeset
    53
     * Open a file for writing. If {@code append} is {@code true} then the file
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5787
diff changeset
    54
     * is opened for atomic append directly and a FileOutputStream constructed
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5787
diff changeset
    55
     * with the resulting handle. This is because a FileOutputStream created
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5787
diff changeset
    56
     * to append to a file does not open the file in a manner that guarantees
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5787
diff changeset
    57
     * that writes by the child process will be atomic.
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5787
diff changeset
    58
     */
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5787
diff changeset
    59
    private static FileOutputStream newFileOutputStream(File f, boolean append)
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5787
diff changeset
    60
        throws IOException
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5787
diff changeset
    61
    {
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5787
diff changeset
    62
        if (append) {
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5787
diff changeset
    63
            SecurityManager sm = System.getSecurityManager();
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5787
diff changeset
    64
            if (sm != null)
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5787
diff changeset
    65
                sm.checkWrite(f.getPath());
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5787
diff changeset
    66
            long handle = openForAtomicAppend(f.getPath());
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5787
diff changeset
    67
            final FileDescriptor fd = new FileDescriptor();
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5787
diff changeset
    68
            fdAccess.setHandle(fd, handle);
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5787
diff changeset
    69
            return AccessController.doPrivileged(
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5787
diff changeset
    70
                new PrivilegedAction<FileOutputStream>() {
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5787
diff changeset
    71
                    public FileOutputStream run() {
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5787
diff changeset
    72
                        return new FileOutputStream(fd);
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5787
diff changeset
    73
                    }
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5787
diff changeset
    74
                }
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5787
diff changeset
    75
            );
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5787
diff changeset
    76
        } else {
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5787
diff changeset
    77
            return new FileOutputStream(f);
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5787
diff changeset
    78
        }
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5787
diff changeset
    79
    }
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5787
diff changeset
    80
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    // System-dependent portion of ProcessBuilder.start()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    static Process start(String cmdarray[],
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                         java.util.Map<String,String> environment,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                         String dir,
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    85
                         ProcessBuilder.Redirect[] redirects,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                         boolean redirectErrorStream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        String envblock = ProcessEnvironment.toEnvironmentBlock(environment);
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    90
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    91
        FileInputStream  f0 = null;
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    92
        FileOutputStream f1 = null;
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    93
        FileOutputStream f2 = null;
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    94
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    95
        try {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    96
            long[] stdHandles;
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    97
            if (redirects == null) {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    98
                stdHandles = new long[] { -1L, -1L, -1L };
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    99
            } else {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   100
                stdHandles = new long[3];
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   101
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   102
                if (redirects[0] == Redirect.PIPE)
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   103
                    stdHandles[0] = -1L;
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   104
                else if (redirects[0] == Redirect.INHERIT)
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   105
                    stdHandles[0] = fdAccess.getHandle(FileDescriptor.in);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   106
                else {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   107
                    f0 = new FileInputStream(redirects[0].file());
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   108
                    stdHandles[0] = fdAccess.getHandle(f0.getFD());
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   109
                }
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   110
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   111
                if (redirects[1] == Redirect.PIPE)
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   112
                    stdHandles[1] = -1L;
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   113
                else if (redirects[1] == Redirect.INHERIT)
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   114
                    stdHandles[1] = fdAccess.getHandle(FileDescriptor.out);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   115
                else {
7515
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5787
diff changeset
   116
                    f1 = newFileOutputStream(redirects[1].file(),
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5787
diff changeset
   117
                                             redirects[1].append());
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   118
                    stdHandles[1] = fdAccess.getHandle(f1.getFD());
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   119
                }
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   120
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   121
                if (redirects[2] == Redirect.PIPE)
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   122
                    stdHandles[2] = -1L;
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   123
                else if (redirects[2] == Redirect.INHERIT)
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   124
                    stdHandles[2] = fdAccess.getHandle(FileDescriptor.err);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   125
                else {
7515
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5787
diff changeset
   126
                    f2 = newFileOutputStream(redirects[2].file(),
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5787
diff changeset
   127
                                             redirects[2].append());
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   128
                    stdHandles[2] = fdAccess.getHandle(f2.getFD());
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   129
                }
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   130
            }
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   131
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   132
            return new ProcessImpl(cmdarray, envblock, dir,
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   133
                                   stdHandles, redirectErrorStream);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   134
        } finally {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   135
            // In theory, close() can throw IOException
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   136
            // (although it is rather unlikely to happen here)
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   137
            try { if (f0 != null) f0.close(); }
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   138
            finally {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   139
                try { if (f1 != null) f1.close(); }
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   140
                finally { if (f2 != null) f2.close(); }
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   141
            }
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   142
        }
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   143
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    private long handle = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    private OutputStream stdin_stream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    private InputStream stdout_stream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    private InputStream stderr_stream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   151
    private ProcessImpl(final String cmd[],
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   152
                        final String envblock,
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   153
                        final String path,
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   154
                        final long[] stdHandles,
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   155
                        final boolean redirectErrorStream)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        // Win32 CreateProcess requires cmd[0] to be normalized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        cmd[0] = new File(cmd[0]).getPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        StringBuilder cmdbuf = new StringBuilder(80);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        for (int i = 0; i < cmd.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            if (i > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                cmdbuf.append(' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            String s = cmd[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            if (s.indexOf(' ') >= 0 || s.indexOf('\t') >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                if (s.charAt(0) != '"') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                    cmdbuf.append('"');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                    cmdbuf.append(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                    if (s.endsWith("\\")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                        cmdbuf.append("\\");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                    cmdbuf.append('"');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                } else if (s.endsWith("\"")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                    /* The argument has already been quoted. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                    cmdbuf.append(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                    /* Unmatched quote for the argument. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                    throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                cmdbuf.append(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        String cmdstr = cmdbuf.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   188
        handle = create(cmdstr, envblock, path,
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   189
                        stdHandles, redirectErrorStream);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        java.security.AccessController.doPrivileged(
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   192
        new java.security.PrivilegedAction<Void>() {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   193
        public Void run() {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   194
            if (stdHandles[0] == -1L)
5787
a0af7b8e80ed 6960898: Regression due to src/share/classes/java/lang/ProcessBuilder.java changes
martin
parents: 5506
diff changeset
   195
                stdin_stream = ProcessBuilder.NullOutputStream.INSTANCE;
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   196
            else {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   197
                FileDescriptor stdin_fd = new FileDescriptor();
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   198
                fdAccess.setHandle(stdin_fd, stdHandles[0]);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   199
                stdin_stream = new BufferedOutputStream(
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   200
                    new FileOutputStream(stdin_fd));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            }
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   202
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   203
            if (stdHandles[1] == -1L)
5787
a0af7b8e80ed 6960898: Regression due to src/share/classes/java/lang/ProcessBuilder.java changes
martin
parents: 5506
diff changeset
   204
                stdout_stream = ProcessBuilder.NullInputStream.INSTANCE;
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   205
            else {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   206
                FileDescriptor stdout_fd = new FileDescriptor();
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   207
                fdAccess.setHandle(stdout_fd, stdHandles[1]);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   208
                stdout_stream = new BufferedInputStream(
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   209
                    new FileInputStream(stdout_fd));
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   210
            }
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   211
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   212
            if (stdHandles[2] == -1L)
5787
a0af7b8e80ed 6960898: Regression due to src/share/classes/java/lang/ProcessBuilder.java changes
martin
parents: 5506
diff changeset
   213
                stderr_stream = ProcessBuilder.NullInputStream.INSTANCE;
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   214
            else {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   215
                FileDescriptor stderr_fd = new FileDescriptor();
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   216
                fdAccess.setHandle(stderr_fd, stdHandles[2]);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   217
                stderr_stream = new FileInputStream(stderr_fd);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   218
            }
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   219
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   220
            return null; }});
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    public OutputStream getOutputStream() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        return stdin_stream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    public InputStream getInputStream() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        return stdout_stream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    public InputStream getErrorStream() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        return stderr_stream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    public void finalize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        closeHandle(handle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    private static final int STILL_ACTIVE = getStillActive();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    private static native int getStillActive();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    public int exitValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        int exitCode = getExitCodeProcess(handle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        if (exitCode == STILL_ACTIVE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            throw new IllegalThreadStateException("process has not exited");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        return exitCode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    private static native int getExitCodeProcess(long handle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    public int waitFor() throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        waitForInterruptibly(handle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        if (Thread.interrupted())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            throw new InterruptedException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        return exitValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    private static native void waitForInterruptibly(long handle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    public void destroy() { terminateProcess(handle); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    private static native void terminateProcess(long handle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   261
    /**
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   262
     * Create a process using the win32 function CreateProcess.
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   263
     *
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   264
     * @param cmdstr the Windows commandline
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   265
     * @param envblock NUL-separated, double-NUL-terminated list of
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   266
     *        environment strings in VAR=VALUE form
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   267
     * @param dir the working directory of the process, or null if
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   268
     *        inheriting the current directory from the parent process
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   269
     * @param stdHandles array of windows HANDLEs.  Indexes 0, 1, and
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   270
     *        2 correspond to standard input, standard output and
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   271
     *        standard error, respectively.  On input, a value of -1
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   272
     *        means to create a pipe to connect child and parent
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   273
     *        processes.  On output, a value which is not -1 is the
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   274
     *        parent pipe handle corresponding to the pipe which has
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   275
     *        been created.  An element of this array is -1 on input
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   276
     *        if and only if it is <em>not</em> -1 on output.
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   277
     * @param redirectErrorStream redirectErrorStream attribute
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   278
     * @return the native subprocess HANDLE returned by CreateProcess
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   279
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    private static native long create(String cmdstr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                                      String envblock,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                                      String dir,
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   283
                                      long[] stdHandles,
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   284
                                      boolean redirectErrorStream)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
7515
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5787
diff changeset
   287
    /**
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5787
diff changeset
   288
     * Opens a file for atomic append. The file is created if it doesn't
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5787
diff changeset
   289
     * already exist.
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5787
diff changeset
   290
     *
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5787
diff changeset
   291
     * @param file the file to open or create
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5787
diff changeset
   292
     * @return the native HANDLE
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5787
diff changeset
   293
     */
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5787
diff changeset
   294
    private static native long openForAtomicAppend(String path)
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5787
diff changeset
   295
        throws IOException;
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5787
diff changeset
   296
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    private static native boolean closeHandle(long handle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
}