jdk/src/windows/native/java/lang/ProcessImpl_md.c
author uta
Wed, 20 Feb 2013 16:39:35 +0400
changeset 16016 70e2e77626ec
parent 14177 007c2f91d22b
child 19032 e31afe87fb92
permissions -rw-r--r--
8007454: (process) SetHandleInformation parameters DWORD (not BOOLEAN) Summary: the SetHandleInformation arguments list was fixed. Reviewed-by: alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
13149
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 7668
diff changeset
     2
 * Copyright (c) 1997, 2012, 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: 5168
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: 5168
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: 5168
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5168
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5168
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
#include <assert.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
#include "java_lang_ProcessImpl.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
#include "jni.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
#include "jvm.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
#include "jni_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
#include "io_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
#include <windows.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
#include <io.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
43
bfccfd41f0fc 6631966: (process) Raise Windows pipe buffer size an extra 24 bytes (win)
martin
parents: 2
diff changeset
    36
/* We try to make sure that we can read and write 4095 bytes (the
bfccfd41f0fc 6631966: (process) Raise Windows pipe buffer size an extra 24 bytes (win)
martin
parents: 2
diff changeset
    37
 * fixed limit on Linux) to the pipe on all operating systems without
bfccfd41f0fc 6631966: (process) Raise Windows pipe buffer size an extra 24 bytes (win)
martin
parents: 2
diff changeset
    38
 * deadlock.  Windows 2000 inexplicably appears to need an extra 24
bfccfd41f0fc 6631966: (process) Raise Windows pipe buffer size an extra 24 bytes (win)
martin
parents: 2
diff changeset
    39
 * bytes of slop to avoid deadlock.
bfccfd41f0fc 6631966: (process) Raise Windows pipe buffer size an extra 24 bytes (win)
martin
parents: 2
diff changeset
    40
 */
bfccfd41f0fc 6631966: (process) Raise Windows pipe buffer size an extra 24 bytes (win)
martin
parents: 2
diff changeset
    41
#define PIPE_SIZE (4096+24)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
win32Error(JNIEnv *env, const char *functionName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    static const char * const format = "%s error=%d, %s";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    static const char * const fallbackFormat = "%s failed, error=%d";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    char buf[256];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    char errmsg[sizeof(buf) + 100];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    const int errnum = GetLastError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    const int n = JVM_GetLastErrorString(buf, sizeof(buf));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    if (n > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        sprintf(errmsg, format, functionName, errnum, buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    else
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        sprintf(errmsg, fallbackFormat, functionName, errnum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    JNU_ThrowIOException(env, errmsg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
closeSafely(HANDLE handle)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
{
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 43
diff changeset
    62
    if (handle != INVALID_HANDLE_VALUE)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        CloseHandle(handle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
JNIEXPORT jlong JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
Java_java_lang_ProcessImpl_create(JNIEnv *env, jclass ignored,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                                  jstring cmd,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                                  jstring envBlock,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                                  jstring dir,
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 43
diff changeset
    71
                                  jlongArray stdHandles,
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 43
diff changeset
    72
                                  jboolean redirectErrorStream)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
{
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 43
diff changeset
    74
    HANDLE inRead   = INVALID_HANDLE_VALUE;
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 43
diff changeset
    75
    HANDLE inWrite  = INVALID_HANDLE_VALUE;
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 43
diff changeset
    76
    HANDLE outRead  = INVALID_HANDLE_VALUE;
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 43
diff changeset
    77
    HANDLE outWrite = INVALID_HANDLE_VALUE;
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 43
diff changeset
    78
    HANDLE errRead  = INVALID_HANDLE_VALUE;
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 43
diff changeset
    79
    HANDLE errWrite = INVALID_HANDLE_VALUE;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    SECURITY_ATTRIBUTES sa;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    PROCESS_INFORMATION pi;
5168
41e46b5d9b15 4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents: 715
diff changeset
    82
    STARTUPINFOW si;
41e46b5d9b15 4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents: 715
diff changeset
    83
    const jchar*  pcmd = NULL;
41e46b5d9b15 4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents: 715
diff changeset
    84
    const jchar*  pdir = NULL;
41e46b5d9b15 4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents: 715
diff changeset
    85
    const jchar*  penvBlock = NULL;
41e46b5d9b15 4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents: 715
diff changeset
    86
    jlong  *handles = NULL;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    jlong ret = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    DWORD processFlag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    assert(cmd != NULL);
5168
41e46b5d9b15 4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents: 715
diff changeset
    91
    pcmd = (*env)->GetStringChars(env, cmd, NULL);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    if (pcmd == NULL) goto Catch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    if (dir != 0) {
5168
41e46b5d9b15 4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents: 715
diff changeset
    95
        pdir = (*env)->GetStringChars(env, dir, NULL);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        if (pdir == NULL) goto Catch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    if (envBlock != NULL) {
5168
41e46b5d9b15 4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents: 715
diff changeset
    99
        penvBlock = ((*env)->GetStringChars(env, envBlock, NULL));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        if (penvBlock == NULL) goto Catch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    }
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 43
diff changeset
   102
    assert(stdHandles != NULL);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 43
diff changeset
   103
    handles = (*env)->GetLongArrayElements(env, stdHandles, NULL);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 43
diff changeset
   104
    if (handles == NULL) goto Catch;
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 43
diff changeset
   105
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    memset(&si, 0, sizeof(si));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    si.cb = sizeof(si);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    si.dwFlags = STARTF_USESTDHANDLES;
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 43
diff changeset
   109
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 43
diff changeset
   110
    sa.nLength = sizeof(sa);
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 43
diff changeset
   111
    sa.lpSecurityDescriptor = 0;
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 43
diff changeset
   112
    sa.bInheritHandle = TRUE;
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 43
diff changeset
   113
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 43
diff changeset
   114
    if (handles[0] != (jlong) -1) {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 43
diff changeset
   115
        si.hStdInput = (HANDLE) handles[0];
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 43
diff changeset
   116
        handles[0] = (jlong) -1;
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 43
diff changeset
   117
    } else {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 43
diff changeset
   118
        if (! CreatePipe(&inRead,  &inWrite,  &sa, PIPE_SIZE)) {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 43
diff changeset
   119
            win32Error(env, "CreatePipe");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 43
diff changeset
   120
            goto Catch;
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 43
diff changeset
   121
        }
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 43
diff changeset
   122
        si.hStdInput = inRead;
16016
70e2e77626ec 8007454: (process) SetHandleInformation parameters DWORD (not BOOLEAN)
uta
parents: 14177
diff changeset
   123
        SetHandleInformation(inWrite, HANDLE_FLAG_INHERIT, 0);
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 43
diff changeset
   124
        handles[0] = (jlong) inWrite;
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 43
diff changeset
   125
    }
16016
70e2e77626ec 8007454: (process) SetHandleInformation parameters DWORD (not BOOLEAN)
uta
parents: 14177
diff changeset
   126
    SetHandleInformation(si.hStdInput,
70e2e77626ec 8007454: (process) SetHandleInformation parameters DWORD (not BOOLEAN)
uta
parents: 14177
diff changeset
   127
        HANDLE_FLAG_INHERIT,
70e2e77626ec 8007454: (process) SetHandleInformation parameters DWORD (not BOOLEAN)
uta
parents: 14177
diff changeset
   128
        HANDLE_FLAG_INHERIT);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 43
diff changeset
   130
    if (handles[1] != (jlong) -1) {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 43
diff changeset
   131
        si.hStdOutput = (HANDLE) handles[1];
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 43
diff changeset
   132
        handles[1] = (jlong) -1;
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 43
diff changeset
   133
    } else {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 43
diff changeset
   134
        if (! CreatePipe(&outRead, &outWrite, &sa, PIPE_SIZE)) {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 43
diff changeset
   135
            win32Error(env, "CreatePipe");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 43
diff changeset
   136
            goto Catch;
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 43
diff changeset
   137
        }
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 43
diff changeset
   138
        si.hStdOutput = outWrite;
16016
70e2e77626ec 8007454: (process) SetHandleInformation parameters DWORD (not BOOLEAN)
uta
parents: 14177
diff changeset
   139
        SetHandleInformation(outRead, HANDLE_FLAG_INHERIT, 0);
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 43
diff changeset
   140
        handles[1] = (jlong) outRead;
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 43
diff changeset
   141
    }
16016
70e2e77626ec 8007454: (process) SetHandleInformation parameters DWORD (not BOOLEAN)
uta
parents: 14177
diff changeset
   142
    SetHandleInformation(si.hStdOutput,
70e2e77626ec 8007454: (process) SetHandleInformation parameters DWORD (not BOOLEAN)
uta
parents: 14177
diff changeset
   143
        HANDLE_FLAG_INHERIT,
70e2e77626ec 8007454: (process) SetHandleInformation parameters DWORD (not BOOLEAN)
uta
parents: 14177
diff changeset
   144
        HANDLE_FLAG_INHERIT);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 43
diff changeset
   146
    if (redirectErrorStream) {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 43
diff changeset
   147
        si.hStdError = si.hStdOutput;
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 43
diff changeset
   148
        handles[2] = (jlong) -1;
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 43
diff changeset
   149
    } else if (handles[2] != (jlong) -1) {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 43
diff changeset
   150
        si.hStdError = (HANDLE) handles[2];
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 43
diff changeset
   151
        handles[2] = (jlong) -1;
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 43
diff changeset
   152
    } else {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 43
diff changeset
   153
        if (! CreatePipe(&errRead, &errWrite, &sa, PIPE_SIZE)) {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 43
diff changeset
   154
            win32Error(env, "CreatePipe");
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 43
diff changeset
   155
            goto Catch;
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 43
diff changeset
   156
        }
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 43
diff changeset
   157
        si.hStdError = errWrite;
16016
70e2e77626ec 8007454: (process) SetHandleInformation parameters DWORD (not BOOLEAN)
uta
parents: 14177
diff changeset
   158
        SetHandleInformation(errRead, HANDLE_FLAG_INHERIT, 0);
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 43
diff changeset
   159
        handles[2] = (jlong) errRead;
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 43
diff changeset
   160
    }
16016
70e2e77626ec 8007454: (process) SetHandleInformation parameters DWORD (not BOOLEAN)
uta
parents: 14177
diff changeset
   161
    SetHandleInformation(si.hStdError,
70e2e77626ec 8007454: (process) SetHandleInformation parameters DWORD (not BOOLEAN)
uta
parents: 14177
diff changeset
   162
        HANDLE_FLAG_INHERIT,
70e2e77626ec 8007454: (process) SetHandleInformation parameters DWORD (not BOOLEAN)
uta
parents: 14177
diff changeset
   163
        HANDLE_FLAG_INHERIT);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
14177
007c2f91d22b 7186817: Remove Windows 95/98/ME Support
dxu
parents: 13149
diff changeset
   165
    processFlag = CREATE_NO_WINDOW | CREATE_UNICODE_ENVIRONMENT;
5168
41e46b5d9b15 4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents: 715
diff changeset
   166
    ret = CreateProcessW(0,                /* executable name */
41e46b5d9b15 4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents: 715
diff changeset
   167
                         (LPWSTR)pcmd,     /* command line */
41e46b5d9b15 4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents: 715
diff changeset
   168
                         0,                /* process security attribute */
41e46b5d9b15 4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents: 715
diff changeset
   169
                         0,                /* thread security attribute */
41e46b5d9b15 4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents: 715
diff changeset
   170
                         TRUE,             /* inherits system handles */
41e46b5d9b15 4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents: 715
diff changeset
   171
                         processFlag,      /* selected based on exe type */
41e46b5d9b15 4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents: 715
diff changeset
   172
                         (LPVOID)penvBlock,/* environment block */
41e46b5d9b15 4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents: 715
diff changeset
   173
                         (LPCWSTR)pdir,    /* change to the new current directory */
41e46b5d9b15 4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents: 715
diff changeset
   174
                         &si,              /* (in)  startup information */
41e46b5d9b15 4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents: 715
diff changeset
   175
                         &pi);             /* (out) process information */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    if (!ret) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        win32Error(env, "CreateProcess");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        goto Catch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    CloseHandle(pi.hThread);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    ret = (jlong)pi.hProcess;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
 Finally:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    /* Always clean up the child's side of the pipes */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    closeSafely(inRead);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    closeSafely(outWrite);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    closeSafely(errWrite);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    if (pcmd != NULL)
5168
41e46b5d9b15 4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents: 715
diff changeset
   191
        (*env)->ReleaseStringChars(env, cmd, pcmd);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    if (pdir != NULL)
5168
41e46b5d9b15 4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents: 715
diff changeset
   193
        (*env)->ReleaseStringChars(env, dir, pdir);
41e46b5d9b15 4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents: 715
diff changeset
   194
    if (penvBlock != NULL)
41e46b5d9b15 4947220: (process)Runtime.exec() cannot invoke applications with unicode parameters(win)
sherman
parents: 715
diff changeset
   195
        (*env)->ReleaseStringChars(env, envBlock, penvBlock);
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 43
diff changeset
   196
    if (handles != NULL)
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 43
diff changeset
   197
        (*env)->ReleaseLongArrayElements(env, stdHandles, handles, 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
 Catch:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    /* Clean up the parent's side of the pipes in case of failure only */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    closeSafely(inWrite);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    closeSafely(outRead);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    closeSafely(errRead);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    goto Finally;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
JNIEXPORT jint JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
Java_java_lang_ProcessImpl_getExitCodeProcess(JNIEnv *env, jclass ignored, jlong handle)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    DWORD exit_code;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    if (GetExitCodeProcess((HANDLE) handle, &exit_code) == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        win32Error(env, "GetExitCodeProcess");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    return exit_code;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
JNIEXPORT jint JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
Java_java_lang_ProcessImpl_getStillActive(JNIEnv *env, jclass ignored)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    return STILL_ACTIVE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
Java_java_lang_ProcessImpl_waitForInterruptibly(JNIEnv *env, jclass ignored, jlong handle)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    HANDLE events[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    events[0] = (HANDLE) handle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    events[1] = JVM_GetThreadInterruptEvent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    if (WaitForMultipleObjects(sizeof(events)/sizeof(events[0]), events,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                               FALSE,    /* Wait for ANY event */
13149
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 7668
diff changeset
   232
                               INFINITE)  /* Wait forever */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        == WAIT_FAILED)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        win32Error(env, "WaitForMultipleObjects");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
JNIEXPORT void JNICALL
13149
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 7668
diff changeset
   238
Java_java_lang_ProcessImpl_waitForTimeoutInterruptibly(JNIEnv *env,
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 7668
diff changeset
   239
                                                       jclass ignored,
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 7668
diff changeset
   240
                                                       jlong handle,
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 7668
diff changeset
   241
                                                       jlong timeout)
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 7668
diff changeset
   242
{
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 7668
diff changeset
   243
    HANDLE events[2];
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 7668
diff changeset
   244
    DWORD dwTimeout = (DWORD)timeout;
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 7668
diff changeset
   245
    DWORD result;
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 7668
diff changeset
   246
    events[0] = (HANDLE) handle;
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 7668
diff changeset
   247
    events[1] = JVM_GetThreadInterruptEvent();
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 7668
diff changeset
   248
    result = WaitForMultipleObjects(sizeof(events)/sizeof(events[0]), events,
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 7668
diff changeset
   249
                                    FALSE,    /* Wait for ANY event */
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 7668
diff changeset
   250
                                    dwTimeout);  /* Wait for dwTimeout */
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 7668
diff changeset
   251
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 7668
diff changeset
   252
    if (result == WAIT_FAILED)
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 7668
diff changeset
   253
        win32Error(env, "WaitForMultipleObjects");
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 7668
diff changeset
   254
}
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 7668
diff changeset
   255
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 7668
diff changeset
   256
JNIEXPORT void JNICALL
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
Java_java_lang_ProcessImpl_terminateProcess(JNIEnv *env, jclass ignored, jlong handle)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    TerminateProcess((HANDLE) handle, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
JNIEXPORT jboolean JNICALL
13149
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 7668
diff changeset
   263
Java_java_lang_ProcessImpl_isProcessAlive(JNIEnv *env, jclass ignored, jlong handle)
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 7668
diff changeset
   264
{
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 7668
diff changeset
   265
    DWORD dwExitStatus;
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 7668
diff changeset
   266
    GetExitCodeProcess(handle, &dwExitStatus);
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 7668
diff changeset
   267
    return dwExitStatus == STILL_ACTIVE;
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 7668
diff changeset
   268
}
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 7668
diff changeset
   269
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 7668
diff changeset
   270
JNIEXPORT jboolean JNICALL
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
Java_java_lang_ProcessImpl_closeHandle(JNIEnv *env, jclass ignored, jlong handle)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    return CloseHandle((HANDLE) handle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
}
7515
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   275
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   276
/**
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   277
 * Returns a copy of the Unicode characters of a string. Fow now this
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   278
 * function doesn't handle long path names and other issues.
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   279
 */
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   280
static WCHAR* getPath(JNIEnv *env, jstring ps) {
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   281
    WCHAR *pathbuf = NULL;
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   282
    const jchar *chars = (*(env))->GetStringChars(env, ps, NULL);
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   283
    if (chars != NULL) {
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   284
        size_t pathlen = wcslen(chars);
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   285
        pathbuf = (WCHAR*)malloc((pathlen + 6) * sizeof(WCHAR));
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   286
        if (pathbuf == NULL) {
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   287
            JNU_ThrowOutOfMemoryError(env, NULL);
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   288
        } else {
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   289
            wcscpy(pathbuf, chars);
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   290
        }
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   291
        (*env)->ReleaseStringChars(env, ps, chars);
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   292
    }
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   293
    return pathbuf;
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   294
}
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   295
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   296
JNIEXPORT jlong JNICALL
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   297
Java_java_lang_ProcessImpl_openForAtomicAppend(JNIEnv *env, jclass ignored, jstring path)
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   298
{
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   299
    const DWORD access = (FILE_GENERIC_WRITE & ~FILE_WRITE_DATA);
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   300
    const DWORD sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   301
    const DWORD disposition = OPEN_ALWAYS;
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   302
    const DWORD flagsAndAttributes = FILE_ATTRIBUTE_NORMAL;
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   303
    HANDLE h;
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   304
    WCHAR *pathbuf = getPath(env, path);
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   305
    if (pathbuf == NULL) {
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   306
        /* Exception already pending */
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   307
        return -1;
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   308
    }
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   309
    h = CreateFileW(
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   310
        pathbuf,            /* Wide char path name */
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   311
        access,             /* Read and/or write permission */
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   312
        sharing,            /* File sharing flags */
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   313
        NULL,               /* Security attributes */
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   314
        disposition,        /* creation disposition */
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   315
        flagsAndAttributes, /* flags and attributes */
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   316
        NULL);
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   317
    free(pathbuf);
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   318
    if (h == INVALID_HANDLE_VALUE) {
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   319
        JNU_ThrowIOExceptionWithLastError(env, "CreateFileW");
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   320
    }
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   321
    return ptr_to_jlong(h);
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   322
}