jdk/src/windows/native/java/lang/ProcessImpl_md.c
author rriggs
Tue, 27 May 2014 16:05:04 -0400
changeset 24577 b3bf9c82a050
parent 23557 fdb248aa4e92
permissions -rw-r--r--
8003488: (process) Provide Process.getPid() Summary: Add Process.getPid Reviewed-by: alanb, martin, igerasim
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 22962
diff changeset
     2
 * Copyright (c) 1997, 2013, 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
19032
e31afe87fb92 8016579: (process) IOException thrown by ProcessBuilder.start() method is incorrectly encoded
uta
parents: 16016
diff changeset
    43
/* We have THREE locales in action:
e31afe87fb92 8016579: (process) IOException thrown by ProcessBuilder.start() method is incorrectly encoded
uta
parents: 16016
diff changeset
    44
 * 1. Thread default locale - dictates UNICODE-to-8bit conversion
e31afe87fb92 8016579: (process) IOException thrown by ProcessBuilder.start() method is incorrectly encoded
uta
parents: 16016
diff changeset
    45
 * 2. System locale that defines the message localization
e31afe87fb92 8016579: (process) IOException thrown by ProcessBuilder.start() method is incorrectly encoded
uta
parents: 16016
diff changeset
    46
 * 3. The file name locale
e31afe87fb92 8016579: (process) IOException thrown by ProcessBuilder.start() method is incorrectly encoded
uta
parents: 16016
diff changeset
    47
 * Each locale could be an extended locale, that means that text cannot be
e31afe87fb92 8016579: (process) IOException thrown by ProcessBuilder.start() method is incorrectly encoded
uta
parents: 16016
diff changeset
    48
 * mapped to 8bit sequence without multibyte encoding.
e31afe87fb92 8016579: (process) IOException thrown by ProcessBuilder.start() method is incorrectly encoded
uta
parents: 16016
diff changeset
    49
 * VM is ready for that, if text is UTF-8.
e31afe87fb92 8016579: (process) IOException thrown by ProcessBuilder.start() method is incorrectly encoded
uta
parents: 16016
diff changeset
    50
 * Here we make the work right from the beginning.
e31afe87fb92 8016579: (process) IOException thrown by ProcessBuilder.start() method is incorrectly encoded
uta
parents: 16016
diff changeset
    51
 */
e31afe87fb92 8016579: (process) IOException thrown by ProcessBuilder.start() method is incorrectly encoded
uta
parents: 16016
diff changeset
    52
size_t os_error_message(int errnum, WCHAR* utf16_OSErrorMsg, size_t maxMsgLength) {
e31afe87fb92 8016579: (process) IOException thrown by ProcessBuilder.start() method is incorrectly encoded
uta
parents: 16016
diff changeset
    53
    size_t n = (size_t)FormatMessageW(
e31afe87fb92 8016579: (process) IOException thrown by ProcessBuilder.start() method is incorrectly encoded
uta
parents: 16016
diff changeset
    54
            FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
e31afe87fb92 8016579: (process) IOException thrown by ProcessBuilder.start() method is incorrectly encoded
uta
parents: 16016
diff changeset
    55
            NULL,
e31afe87fb92 8016579: (process) IOException thrown by ProcessBuilder.start() method is incorrectly encoded
uta
parents: 16016
diff changeset
    56
            (DWORD)errnum,
e31afe87fb92 8016579: (process) IOException thrown by ProcessBuilder.start() method is incorrectly encoded
uta
parents: 16016
diff changeset
    57
            0,
e31afe87fb92 8016579: (process) IOException thrown by ProcessBuilder.start() method is incorrectly encoded
uta
parents: 16016
diff changeset
    58
            utf16_OSErrorMsg,
e31afe87fb92 8016579: (process) IOException thrown by ProcessBuilder.start() method is incorrectly encoded
uta
parents: 16016
diff changeset
    59
            (DWORD)maxMsgLength,
e31afe87fb92 8016579: (process) IOException thrown by ProcessBuilder.start() method is incorrectly encoded
uta
parents: 16016
diff changeset
    60
            NULL);
e31afe87fb92 8016579: (process) IOException thrown by ProcessBuilder.start() method is incorrectly encoded
uta
parents: 16016
diff changeset
    61
    if (n > 3) {
e31afe87fb92 8016579: (process) IOException thrown by ProcessBuilder.start() method is incorrectly encoded
uta
parents: 16016
diff changeset
    62
        // Drop final '.', CR, LF
e31afe87fb92 8016579: (process) IOException thrown by ProcessBuilder.start() method is incorrectly encoded
uta
parents: 16016
diff changeset
    63
        if (utf16_OSErrorMsg[n - 1] == L'\n') --n;
e31afe87fb92 8016579: (process) IOException thrown by ProcessBuilder.start() method is incorrectly encoded
uta
parents: 16016
diff changeset
    64
        if (utf16_OSErrorMsg[n - 1] == L'\r') --n;
e31afe87fb92 8016579: (process) IOException thrown by ProcessBuilder.start() method is incorrectly encoded
uta
parents: 16016
diff changeset
    65
        if (utf16_OSErrorMsg[n - 1] == L'.') --n;
e31afe87fb92 8016579: (process) IOException thrown by ProcessBuilder.start() method is incorrectly encoded
uta
parents: 16016
diff changeset
    66
        utf16_OSErrorMsg[n] = L'\0';
e31afe87fb92 8016579: (process) IOException thrown by ProcessBuilder.start() method is incorrectly encoded
uta
parents: 16016
diff changeset
    67
    }
e31afe87fb92 8016579: (process) IOException thrown by ProcessBuilder.start() method is incorrectly encoded
uta
parents: 16016
diff changeset
    68
    return n;
e31afe87fb92 8016579: (process) IOException thrown by ProcessBuilder.start() method is incorrectly encoded
uta
parents: 16016
diff changeset
    69
}
e31afe87fb92 8016579: (process) IOException thrown by ProcessBuilder.start() method is incorrectly encoded
uta
parents: 16016
diff changeset
    70
e31afe87fb92 8016579: (process) IOException thrown by ProcessBuilder.start() method is incorrectly encoded
uta
parents: 16016
diff changeset
    71
#define MESSAGE_LENGTH (256 + 100)
e31afe87fb92 8016579: (process) IOException thrown by ProcessBuilder.start() method is incorrectly encoded
uta
parents: 16016
diff changeset
    72
#define ARRAY_SIZE(x) (sizeof(x)/sizeof(*x))
e31afe87fb92 8016579: (process) IOException thrown by ProcessBuilder.start() method is incorrectly encoded
uta
parents: 16016
diff changeset
    73
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
static void
19032
e31afe87fb92 8016579: (process) IOException thrown by ProcessBuilder.start() method is incorrectly encoded
uta
parents: 16016
diff changeset
    75
win32Error(JNIEnv *env, const WCHAR *functionName)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
{
19032
e31afe87fb92 8016579: (process) IOException thrown by ProcessBuilder.start() method is incorrectly encoded
uta
parents: 16016
diff changeset
    77
    WCHAR utf16_OSErrorMsg[MESSAGE_LENGTH - 100];
e31afe87fb92 8016579: (process) IOException thrown by ProcessBuilder.start() method is incorrectly encoded
uta
parents: 16016
diff changeset
    78
    WCHAR utf16_javaMessage[MESSAGE_LENGTH];
e31afe87fb92 8016579: (process) IOException thrown by ProcessBuilder.start() method is incorrectly encoded
uta
parents: 16016
diff changeset
    79
    /*Good suggestion about 2-bytes-per-symbol in localized error reports*/
e31afe87fb92 8016579: (process) IOException thrown by ProcessBuilder.start() method is incorrectly encoded
uta
parents: 16016
diff changeset
    80
    char  utf8_javaMessage[MESSAGE_LENGTH*2];
e31afe87fb92 8016579: (process) IOException thrown by ProcessBuilder.start() method is incorrectly encoded
uta
parents: 16016
diff changeset
    81
    const int errnum = (int)GetLastError();
22962
309eaab8c853 8030010: cleanup native code warnings
mchung
parents: 20191
diff changeset
    82
    size_t n = os_error_message(errnum, utf16_OSErrorMsg, ARRAY_SIZE(utf16_OSErrorMsg));
19032
e31afe87fb92 8016579: (process) IOException thrown by ProcessBuilder.start() method is incorrectly encoded
uta
parents: 16016
diff changeset
    83
    n = (n > 0)
e31afe87fb92 8016579: (process) IOException thrown by ProcessBuilder.start() method is incorrectly encoded
uta
parents: 16016
diff changeset
    84
        ? swprintf(utf16_javaMessage, MESSAGE_LENGTH, L"%s error=%d, %s", functionName, errnum, utf16_OSErrorMsg)
e31afe87fb92 8016579: (process) IOException thrown by ProcessBuilder.start() method is incorrectly encoded
uta
parents: 16016
diff changeset
    85
        : swprintf(utf16_javaMessage, MESSAGE_LENGTH, L"%s failed, error=%d", functionName, errnum);
e31afe87fb92 8016579: (process) IOException thrown by ProcessBuilder.start() method is incorrectly encoded
uta
parents: 16016
diff changeset
    86
e31afe87fb92 8016579: (process) IOException thrown by ProcessBuilder.start() method is incorrectly encoded
uta
parents: 16016
diff changeset
    87
    if (n > 0) /*terminate '\0' is not a part of conversion procedure*/
e31afe87fb92 8016579: (process) IOException thrown by ProcessBuilder.start() method is incorrectly encoded
uta
parents: 16016
diff changeset
    88
        n = WideCharToMultiByte(
e31afe87fb92 8016579: (process) IOException thrown by ProcessBuilder.start() method is incorrectly encoded
uta
parents: 16016
diff changeset
    89
            CP_UTF8,
e31afe87fb92 8016579: (process) IOException thrown by ProcessBuilder.start() method is incorrectly encoded
uta
parents: 16016
diff changeset
    90
            0,
e31afe87fb92 8016579: (process) IOException thrown by ProcessBuilder.start() method is incorrectly encoded
uta
parents: 16016
diff changeset
    91
            utf16_javaMessage,
e31afe87fb92 8016579: (process) IOException thrown by ProcessBuilder.start() method is incorrectly encoded
uta
parents: 16016
diff changeset
    92
            n, /*by creation n <= MESSAGE_LENGTH*/
e31afe87fb92 8016579: (process) IOException thrown by ProcessBuilder.start() method is incorrectly encoded
uta
parents: 16016
diff changeset
    93
            utf8_javaMessage,
e31afe87fb92 8016579: (process) IOException thrown by ProcessBuilder.start() method is incorrectly encoded
uta
parents: 16016
diff changeset
    94
            MESSAGE_LENGTH*2,
e31afe87fb92 8016579: (process) IOException thrown by ProcessBuilder.start() method is incorrectly encoded
uta
parents: 16016
diff changeset
    95
            NULL,
e31afe87fb92 8016579: (process) IOException thrown by ProcessBuilder.start() method is incorrectly encoded
uta
parents: 16016
diff changeset
    96
            NULL);
e31afe87fb92 8016579: (process) IOException thrown by ProcessBuilder.start() method is incorrectly encoded
uta
parents: 16016
diff changeset
    97
e31afe87fb92 8016579: (process) IOException thrown by ProcessBuilder.start() method is incorrectly encoded
uta
parents: 16016
diff changeset
    98
    /*no way to die*/
e31afe87fb92 8016579: (process) IOException thrown by ProcessBuilder.start() method is incorrectly encoded
uta
parents: 16016
diff changeset
    99
    {
e31afe87fb92 8016579: (process) IOException thrown by ProcessBuilder.start() method is incorrectly encoded
uta
parents: 16016
diff changeset
   100
        const char *errorMessage = "Secondary error while OS message extraction";
e31afe87fb92 8016579: (process) IOException thrown by ProcessBuilder.start() method is incorrectly encoded
uta
parents: 16016
diff changeset
   101
        if (n > 0) {
e31afe87fb92 8016579: (process) IOException thrown by ProcessBuilder.start() method is incorrectly encoded
uta
parents: 16016
diff changeset
   102
            utf8_javaMessage[min(MESSAGE_LENGTH*2 - 1, n)] = '\0';
e31afe87fb92 8016579: (process) IOException thrown by ProcessBuilder.start() method is incorrectly encoded
uta
parents: 16016
diff changeset
   103
            errorMessage = utf8_javaMessage;
e31afe87fb92 8016579: (process) IOException thrown by ProcessBuilder.start() method is incorrectly encoded
uta
parents: 16016
diff changeset
   104
        }
e31afe87fb92 8016579: (process) IOException thrown by ProcessBuilder.start() method is incorrectly encoded
uta
parents: 16016
diff changeset
   105
        JNU_ThrowIOException(env, errorMessage);
e31afe87fb92 8016579: (process) IOException thrown by ProcessBuilder.start() method is incorrectly encoded
uta
parents: 16016
diff changeset
   106
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
closeSafely(HANDLE handle)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
{
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 43
diff changeset
   112
    if (handle != INVALID_HANDLE_VALUE)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        CloseHandle(handle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
19372
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   116
static BOOL hasInheritFlag(HANDLE handle)
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   117
{
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   118
    DWORD mask;
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   119
    if (GetHandleInformation(handle, &mask)) {
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   120
        return mask & HANDLE_FLAG_INHERIT;
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   121
    }
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   122
    return FALSE;
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   123
}
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   124
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   125
#define HANDLE_STORAGE_SIZE 6
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   126
#define OFFSET_READ  0
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   127
#define OFFSET_WRITE 1
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   128
//long signed version of INVALID_HANDLE_VALUE
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   129
#define JAVA_INVALID_HANDLE_VALUE ((jlong) -1)
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   130
#define OPPOSITE_END(offset) (offset==OFFSET_READ ? OFFSET_WRITE : OFFSET_READ)
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   131
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   132
/* Pipe holder structure */
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   133
typedef struct _STDHOLDER {
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   134
    HANDLE  pipe[2];
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   135
    int     offset;
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   136
} STDHOLDER;
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   137
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   138
/* Responsible for correct initialization of the [pHolder] structure
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   139
   (that is used for handles recycling) if needs,
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   140
   and appropriate setup of IOE handle [phStd] for child process based
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   141
   on created pipe or Java handle. */
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   142
static BOOL initHolder(
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   143
    JNIEnv *env,
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   144
    jlong *pjhandles,   /* IN OUT - the handle form Java,
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   145
                                    that can be a file, console or undefined */
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   146
    STDHOLDER *pHolder, /* OUT    - initialized structure that holds pipe
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   147
                                    handles */
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   148
    HANDLE *phStd       /* OUT    - initialized handle for child process */
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   149
) {
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   150
    /* Here we test the value from Java against invalid
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   151
       handle value. We are not using INVALID_HANDLE_VALUE macro
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   152
       due to double signed/unsigned and 32/64bit ambiguity.
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   153
       Otherwise it will be easy to get the wrong
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   154
       value   0x00000000FFFFFFFF
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   155
       instead 0xFFFFFFFFFFFFFFFF. */
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   156
    if (*pjhandles != JAVA_INVALID_HANDLE_VALUE) {
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   157
        /* Java file or console redirection */
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   158
        *phStd = (HANDLE) *pjhandles;
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   159
        /* Here we set the related Java stream (Process.getXXXXStream())
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   160
           to [ProcessBuilder.NullXXXXStream.INSTANCE] value.
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   161
           The initial Java handle [*pjhandles] will be closed in
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   162
           ANY case. It is not a handle leak. */
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   163
        *pjhandles = JAVA_INVALID_HANDLE_VALUE;
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   164
    } else {
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   165
        /* Creation of parent-child pipe */
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   166
        if (!CreatePipe(
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   167
            &pHolder->pipe[OFFSET_READ],
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   168
            &pHolder->pipe[OFFSET_WRITE],
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   169
            NULL, /* we would like to inherit
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   170
                     default process access,
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   171
                     instead of 'Everybody' access */
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   172
            PIPE_SIZE))
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   173
        {
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   174
            win32Error(env, L"CreatePipe");
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   175
            return FALSE;
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   176
        } else {
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   177
            /* [thisProcessEnd] has no the inherit flag because
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   178
               the [lpPipeAttributes] param of [CreatePipe]
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   179
               had the NULL value. */
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   180
            HANDLE thisProcessEnd = pHolder->pipe[OPPOSITE_END(pHolder->offset)];
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   181
            *phStd = pHolder->pipe[pHolder->offset];
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   182
            *pjhandles = (jlong) thisProcessEnd;
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   183
        }
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   184
    }
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   185
    /* Pipe handle will be closed in the [releaseHolder] call,
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   186
       file handle will be closed in Java.
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   187
       The long-live handle need to restore the inherit flag,
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   188
       we do it later in the [prepareIOEHandleState] call. */
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   189
    SetHandleInformation(
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   190
        *phStd,
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   191
        HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT);
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   192
    return TRUE;
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   193
}
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   194
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   195
/* Smart recycling of pipe handles in [pHolder]. For the failed
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   196
   create process attempts, both ends of pipe need to be released.
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   197
   The [complete] has the [TRUE] value in the failed attempt. */
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   198
static void releaseHolder(BOOL complete, STDHOLDER *pHolder) {
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   199
    closeSafely(pHolder->pipe[pHolder->offset]);
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   200
    if (complete) {
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   201
        /* Error occur, close this process pipe end */
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   202
        closeSafely(pHolder->pipe[OPPOSITE_END(pHolder->offset)]);
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   203
    }
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   204
}
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   205
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   206
/* Stores and drops the inherit flag of handles that should not
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   207
   be shared with the child process by default, but can hold the
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   208
   inherit flag due to MS process birth specific. */
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   209
static void prepareIOEHandleState(
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   210
    HANDLE *stdIOE,
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   211
    BOOL *inherit)
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   212
{
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   213
    int i;
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   214
    for (i = 0; i < HANDLE_STORAGE_SIZE; ++i) {
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   215
        HANDLE hstd = stdIOE[i];
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   216
        if (INVALID_HANDLE_VALUE != hstd && hasInheritFlag(hstd)) {
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   217
            /* FALSE by default */
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   218
            inherit[i] = TRUE;
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   219
            /* Java does not need implicit inheritance for IOE handles,
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   220
               so we drop inherit flag that probably was installed by
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   221
               previous CreateProcess call that launched current process.
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   222
               We will return the handle state back after CreateProcess call.
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   223
               By clearing inherit flag we prevent "greedy grandchild" birth.
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   224
               The explicit inheritance for child process IOE handles is
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   225
               implemented in the [initHolder] call. */
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   226
            SetHandleInformation(hstd, HANDLE_FLAG_INHERIT, 0);
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   227
        }
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   228
    }
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   229
}
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   230
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   231
/* Restores the inheritance flag of handles from stored values. */
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   232
static void restoreIOEHandleState(
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   233
    const HANDLE *stdIOE,
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   234
    const BOOL *inherit)
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   235
{
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   236
    /* The set of current process standard IOE handles and
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   237
       the set of child process IOE handles can intersect.
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   238
       To restore the inherit flag right, we use backward
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   239
       array iteration. */
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   240
    int i;
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   241
    for (i = HANDLE_STORAGE_SIZE - 1; i >= 0; --i)
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   242
        if (INVALID_HANDLE_VALUE != stdIOE[i]) {
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   243
           /* Restore inherit flag for any case.
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   244
              The handle can be changed by explicit inheritance.*/
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   245
            SetHandleInformation(stdIOE[i],
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   246
                HANDLE_FLAG_INHERIT,
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   247
                inherit[i] ? HANDLE_FLAG_INHERIT : 0);
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   248
        }
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   249
}
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   250
24577
b3bf9c82a050 8003488: (process) Provide Process.getPid()
rriggs
parents: 23557
diff changeset
   251
/*
b3bf9c82a050 8003488: (process) Provide Process.getPid()
rriggs
parents: 23557
diff changeset
   252
 * Class:     java_lang_ProcessImpl
b3bf9c82a050 8003488: (process) Provide Process.getPid()
rriggs
parents: 23557
diff changeset
   253
 * Method:    getProcessId0
b3bf9c82a050 8003488: (process) Provide Process.getPid()
rriggs
parents: 23557
diff changeset
   254
 * Signature: (J)I
b3bf9c82a050 8003488: (process) Provide Process.getPid()
rriggs
parents: 23557
diff changeset
   255
 */
b3bf9c82a050 8003488: (process) Provide Process.getPid()
rriggs
parents: 23557
diff changeset
   256
JNIEXPORT jint JNICALL Java_java_lang_ProcessImpl_getProcessId0
b3bf9c82a050 8003488: (process) Provide Process.getPid()
rriggs
parents: 23557
diff changeset
   257
  (JNIEnv *env, jclass clazz, jlong handle) {
b3bf9c82a050 8003488: (process) Provide Process.getPid()
rriggs
parents: 23557
diff changeset
   258
    DWORD pid = GetProcessId((HANDLE) jlong_to_ptr(handle));
b3bf9c82a050 8003488: (process) Provide Process.getPid()
rriggs
parents: 23557
diff changeset
   259
    return (jint)pid;
b3bf9c82a050 8003488: (process) Provide Process.getPid()
rriggs
parents: 23557
diff changeset
   260
}
b3bf9c82a050 8003488: (process) Provide Process.getPid()
rriggs
parents: 23557
diff changeset
   261
19372
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   262
/* Please, read about the MS inheritance problem
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   263
   http://support.microsoft.com/kb/315939
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   264
   and critical section/synchronized block solution. */
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   265
static jlong processCreate(
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   266
    JNIEnv *env,
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   267
    const jchar *pcmd,
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   268
    const jchar *penvBlock,
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   269
    const jchar *pdir,
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   270
    jlong *handles,
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   271
    jboolean redirectErrorStream)
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   272
{
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   273
    jlong ret = 0L;
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   274
    STARTUPINFOW si = {sizeof(si)};
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   275
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   276
    /* Handles for which the inheritance flag must be restored. */
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   277
    HANDLE stdIOE[HANDLE_STORAGE_SIZE] = {
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   278
        /* Current process standard IOE handles: JDK-7147084 */
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   279
        INVALID_HANDLE_VALUE, INVALID_HANDLE_VALUE, INVALID_HANDLE_VALUE,
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   280
        /* Child process IOE handles: JDK-6921885 */
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   281
        (HANDLE)handles[0], (HANDLE)handles[1], (HANDLE)handles[2]};
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   282
    BOOL inherit[HANDLE_STORAGE_SIZE] = {
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   283
        FALSE, FALSE, FALSE,
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   284
        FALSE, FALSE, FALSE};
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   285
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   286
    {
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   287
        /* Extraction of current process standard IOE handles */
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   288
        DWORD idsIOE[3] = {STD_INPUT_HANDLE, STD_OUTPUT_HANDLE, STD_ERROR_HANDLE};
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   289
        int i;
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   290
        for (i = 0; i < 3; ++i)
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   291
            /* Should not be closed by CloseHandle! */
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   292
            stdIOE[i] = GetStdHandle(idsIOE[i]);
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   293
    }
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   294
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   295
    prepareIOEHandleState(stdIOE, inherit);
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   296
    {
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   297
        /* Input */
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   298
        STDHOLDER holderIn = {{INVALID_HANDLE_VALUE, INVALID_HANDLE_VALUE}, OFFSET_READ};
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   299
        if (initHolder(env, &handles[0], &holderIn, &si.hStdInput)) {
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   300
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   301
            /* Output */
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   302
            STDHOLDER holderOut = {{INVALID_HANDLE_VALUE, INVALID_HANDLE_VALUE}, OFFSET_WRITE};
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   303
            if (initHolder(env, &handles[1], &holderOut, &si.hStdOutput)) {
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   304
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   305
                /* Error */
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   306
                STDHOLDER holderErr = {{INVALID_HANDLE_VALUE, INVALID_HANDLE_VALUE}, OFFSET_WRITE};
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   307
                BOOL success;
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   308
                if (redirectErrorStream) {
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   309
                    si.hStdError = si.hStdOutput;
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   310
                    /* Here we set the error stream to [ProcessBuilder.NullInputStream.INSTANCE]
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   311
                       value. That is in accordance with Java Doc for the redirection case.
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   312
                       The Java file for the [ handles[2] ] will be closed in ANY case. It is not
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   313
                       a handle leak. */
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   314
                    handles[2] = JAVA_INVALID_HANDLE_VALUE;
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   315
                    success = TRUE;
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   316
                } else {
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   317
                    success = initHolder(env, &handles[2], &holderErr, &si.hStdError);
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   318
                }
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   319
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   320
                if (success) {
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   321
                    PROCESS_INFORMATION pi;
20191
f0e23e7272d6 8023130: (process) ProcessBuilder#inheritIO does not work on Windows
alanb
parents: 19372
diff changeset
   322
                    DWORD processFlag = CREATE_UNICODE_ENVIRONMENT;
f0e23e7272d6 8023130: (process) ProcessBuilder#inheritIO does not work on Windows
alanb
parents: 19372
diff changeset
   323
f0e23e7272d6 8023130: (process) ProcessBuilder#inheritIO does not work on Windows
alanb
parents: 19372
diff changeset
   324
                    /* Suppress popping-up of a console window for non-console applications */
f0e23e7272d6 8023130: (process) ProcessBuilder#inheritIO does not work on Windows
alanb
parents: 19372
diff changeset
   325
                    if (GetConsoleWindow() == NULL)
f0e23e7272d6 8023130: (process) ProcessBuilder#inheritIO does not work on Windows
alanb
parents: 19372
diff changeset
   326
                        processFlag |= CREATE_NO_WINDOW;
19372
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   327
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   328
                    si.dwFlags = STARTF_USESTDHANDLES;
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   329
                    if (!CreateProcessW(
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   330
                        NULL,             /* executable name */
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   331
                        (LPWSTR)pcmd,     /* command line */
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   332
                        NULL,             /* process security attribute */
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   333
                        NULL,             /* thread security attribute */
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   334
                        TRUE,             /* inherits system handles */
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   335
                        processFlag,      /* selected based on exe type */
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   336
                        (LPVOID)penvBlock,/* environment block */
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   337
                        (LPCWSTR)pdir,    /* change to the new current directory */
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   338
                        &si,              /* (in)  startup information */
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   339
                        &pi))             /* (out) process information */
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   340
                    {
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   341
                        win32Error(env, L"CreateProcess");
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   342
                    } else {
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   343
                        closeSafely(pi.hThread);
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   344
                        ret = (jlong)pi.hProcess;
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   345
                    }
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   346
                }
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   347
                releaseHolder(ret == 0, &holderErr);
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   348
                releaseHolder(ret == 0, &holderOut);
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   349
            }
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   350
            releaseHolder(ret == 0, &holderIn);
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   351
        }
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   352
    }
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   353
    restoreIOEHandleState(stdIOE, inherit);
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   354
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   355
    return ret;
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   356
}
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   357
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
JNIEXPORT jlong JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
Java_java_lang_ProcessImpl_create(JNIEnv *env, jclass ignored,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                                  jstring cmd,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                                  jstring envBlock,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                                  jstring dir,
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 43
diff changeset
   363
                                  jlongArray stdHandles,
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 43
diff changeset
   364
                                  jboolean redirectErrorStream)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    jlong ret = 0;
19372
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   367
    if (cmd != NULL && stdHandles != NULL) {
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   368
        const jchar *pcmd = (*env)->GetStringChars(env, cmd, NULL);
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   369
        if (pcmd != NULL) {
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   370
            const jchar *penvBlock = (envBlock != NULL)
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   371
                ? (*env)->GetStringChars(env, envBlock, NULL)
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   372
                : NULL;
23557
fdb248aa4e92 8036609: Check jdk/src/windows/native/java/lang/ProcessImpl_md.c for JNI pending exceptions
msheppar
parents: 23010
diff changeset
   373
            if (!(*env)->ExceptionCheck(env)) {
fdb248aa4e92 8036609: Check jdk/src/windows/native/java/lang/ProcessImpl_md.c for JNI pending exceptions
msheppar
parents: 23010
diff changeset
   374
                const jchar *pdir = (dir != NULL)
fdb248aa4e92 8036609: Check jdk/src/windows/native/java/lang/ProcessImpl_md.c for JNI pending exceptions
msheppar
parents: 23010
diff changeset
   375
                    ? (*env)->GetStringChars(env, dir, NULL)
fdb248aa4e92 8036609: Check jdk/src/windows/native/java/lang/ProcessImpl_md.c for JNI pending exceptions
msheppar
parents: 23010
diff changeset
   376
                    : NULL;
fdb248aa4e92 8036609: Check jdk/src/windows/native/java/lang/ProcessImpl_md.c for JNI pending exceptions
msheppar
parents: 23010
diff changeset
   377
                if (!(*env)->ExceptionCheck(env)) {
fdb248aa4e92 8036609: Check jdk/src/windows/native/java/lang/ProcessImpl_md.c for JNI pending exceptions
msheppar
parents: 23010
diff changeset
   378
                    jlong *handles = (*env)->GetLongArrayElements(env, stdHandles, NULL);
fdb248aa4e92 8036609: Check jdk/src/windows/native/java/lang/ProcessImpl_md.c for JNI pending exceptions
msheppar
parents: 23010
diff changeset
   379
                    if (handles != NULL) {
fdb248aa4e92 8036609: Check jdk/src/windows/native/java/lang/ProcessImpl_md.c for JNI pending exceptions
msheppar
parents: 23010
diff changeset
   380
                        ret = processCreate(
fdb248aa4e92 8036609: Check jdk/src/windows/native/java/lang/ProcessImpl_md.c for JNI pending exceptions
msheppar
parents: 23010
diff changeset
   381
                            env,
fdb248aa4e92 8036609: Check jdk/src/windows/native/java/lang/ProcessImpl_md.c for JNI pending exceptions
msheppar
parents: 23010
diff changeset
   382
                            pcmd,
fdb248aa4e92 8036609: Check jdk/src/windows/native/java/lang/ProcessImpl_md.c for JNI pending exceptions
msheppar
parents: 23010
diff changeset
   383
                            penvBlock,
fdb248aa4e92 8036609: Check jdk/src/windows/native/java/lang/ProcessImpl_md.c for JNI pending exceptions
msheppar
parents: 23010
diff changeset
   384
                            pdir,
fdb248aa4e92 8036609: Check jdk/src/windows/native/java/lang/ProcessImpl_md.c for JNI pending exceptions
msheppar
parents: 23010
diff changeset
   385
                            handles,
fdb248aa4e92 8036609: Check jdk/src/windows/native/java/lang/ProcessImpl_md.c for JNI pending exceptions
msheppar
parents: 23010
diff changeset
   386
                            redirectErrorStream);
fdb248aa4e92 8036609: Check jdk/src/windows/native/java/lang/ProcessImpl_md.c for JNI pending exceptions
msheppar
parents: 23010
diff changeset
   387
                        (*env)->ReleaseLongArrayElements(env, stdHandles, handles, 0);
fdb248aa4e92 8036609: Check jdk/src/windows/native/java/lang/ProcessImpl_md.c for JNI pending exceptions
msheppar
parents: 23010
diff changeset
   388
                    }
fdb248aa4e92 8036609: Check jdk/src/windows/native/java/lang/ProcessImpl_md.c for JNI pending exceptions
msheppar
parents: 23010
diff changeset
   389
                    if (pdir != NULL)
fdb248aa4e92 8036609: Check jdk/src/windows/native/java/lang/ProcessImpl_md.c for JNI pending exceptions
msheppar
parents: 23010
diff changeset
   390
                        (*env)->ReleaseStringChars(env, dir, pdir);
fdb248aa4e92 8036609: Check jdk/src/windows/native/java/lang/ProcessImpl_md.c for JNI pending exceptions
msheppar
parents: 23010
diff changeset
   391
                }
fdb248aa4e92 8036609: Check jdk/src/windows/native/java/lang/ProcessImpl_md.c for JNI pending exceptions
msheppar
parents: 23010
diff changeset
   392
                if (penvBlock != NULL)
fdb248aa4e92 8036609: Check jdk/src/windows/native/java/lang/ProcessImpl_md.c for JNI pending exceptions
msheppar
parents: 23010
diff changeset
   393
                    (*env)->ReleaseStringChars(env, envBlock, penvBlock);
19372
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   394
            }
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents: 19032
diff changeset
   395
            (*env)->ReleaseStringChars(env, cmd, pcmd);
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 43
diff changeset
   396
        }
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 43
diff changeset
   397
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
JNIEXPORT jint JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
Java_java_lang_ProcessImpl_getExitCodeProcess(JNIEnv *env, jclass ignored, jlong handle)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    DWORD exit_code;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    if (GetExitCodeProcess((HANDLE) handle, &exit_code) == 0)
19032
e31afe87fb92 8016579: (process) IOException thrown by ProcessBuilder.start() method is incorrectly encoded
uta
parents: 16016
diff changeset
   406
        win32Error(env, L"GetExitCodeProcess");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    return exit_code;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
JNIEXPORT jint JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
Java_java_lang_ProcessImpl_getStillActive(JNIEnv *env, jclass ignored)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    return STILL_ACTIVE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
Java_java_lang_ProcessImpl_waitForInterruptibly(JNIEnv *env, jclass ignored, jlong handle)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    HANDLE events[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    events[0] = (HANDLE) handle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    events[1] = JVM_GetThreadInterruptEvent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    if (WaitForMultipleObjects(sizeof(events)/sizeof(events[0]), events,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                               FALSE,    /* Wait for ANY event */
13149
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 7668
diff changeset
   425
                               INFINITE)  /* Wait forever */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        == WAIT_FAILED)
19032
e31afe87fb92 8016579: (process) IOException thrown by ProcessBuilder.start() method is incorrectly encoded
uta
parents: 16016
diff changeset
   427
        win32Error(env, L"WaitForMultipleObjects");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
JNIEXPORT void JNICALL
13149
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 7668
diff changeset
   431
Java_java_lang_ProcessImpl_waitForTimeoutInterruptibly(JNIEnv *env,
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 7668
diff changeset
   432
                                                       jclass ignored,
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 7668
diff changeset
   433
                                                       jlong handle,
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 7668
diff changeset
   434
                                                       jlong timeout)
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 7668
diff changeset
   435
{
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 7668
diff changeset
   436
    HANDLE events[2];
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 7668
diff changeset
   437
    DWORD dwTimeout = (DWORD)timeout;
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 7668
diff changeset
   438
    DWORD result;
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 7668
diff changeset
   439
    events[0] = (HANDLE) handle;
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 7668
diff changeset
   440
    events[1] = JVM_GetThreadInterruptEvent();
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 7668
diff changeset
   441
    result = WaitForMultipleObjects(sizeof(events)/sizeof(events[0]), events,
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 7668
diff changeset
   442
                                    FALSE,    /* Wait for ANY event */
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 7668
diff changeset
   443
                                    dwTimeout);  /* Wait for dwTimeout */
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 7668
diff changeset
   444
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 7668
diff changeset
   445
    if (result == WAIT_FAILED)
19032
e31afe87fb92 8016579: (process) IOException thrown by ProcessBuilder.start() method is incorrectly encoded
uta
parents: 16016
diff changeset
   446
        win32Error(env, L"WaitForMultipleObjects");
13149
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 7668
diff changeset
   447
}
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 7668
diff changeset
   448
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 7668
diff changeset
   449
JNIEXPORT void JNICALL
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
Java_java_lang_ProcessImpl_terminateProcess(JNIEnv *env, jclass ignored, jlong handle)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
    TerminateProcess((HANDLE) handle, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
JNIEXPORT jboolean JNICALL
13149
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 7668
diff changeset
   456
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
   457
{
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 7668
diff changeset
   458
    DWORD dwExitStatus;
19032
e31afe87fb92 8016579: (process) IOException thrown by ProcessBuilder.start() method is incorrectly encoded
uta
parents: 16016
diff changeset
   459
    GetExitCodeProcess((HANDLE) handle, &dwExitStatus);
13149
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 7668
diff changeset
   460
    return dwExitStatus == STILL_ACTIVE;
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 7668
diff changeset
   461
}
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 7668
diff changeset
   462
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents: 7668
diff changeset
   463
JNIEXPORT jboolean JNICALL
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
Java_java_lang_ProcessImpl_closeHandle(JNIEnv *env, jclass ignored, jlong handle)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
{
23557
fdb248aa4e92 8036609: Check jdk/src/windows/native/java/lang/ProcessImpl_md.c for JNI pending exceptions
msheppar
parents: 23010
diff changeset
   466
    return (jboolean) CloseHandle((HANDLE) handle);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
}
7515
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   468
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   469
/**
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   470
 * 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
   471
 * 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
   472
 */
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   473
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
   474
    WCHAR *pathbuf = NULL;
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   475
    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
   476
    if (chars != NULL) {
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   477
        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
   478
        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
   479
        if (pathbuf == NULL) {
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   480
            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
   481
        } else {
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   482
            wcscpy(pathbuf, chars);
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   483
        }
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   484
        (*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
   485
    }
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   486
    return pathbuf;
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   487
}
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   488
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   489
JNIEXPORT jlong JNICALL
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   490
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
   491
{
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   492
    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
   493
    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
   494
    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
   495
    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
   496
    HANDLE h;
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   497
    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
   498
    if (pathbuf == NULL) {
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   499
        /* Exception already pending */
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   500
        return -1;
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   501
    }
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   502
    h = CreateFileW(
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   503
        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
   504
        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
   505
        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
   506
        NULL,               /* Security attributes */
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   507
        disposition,        /* creation disposition */
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   508
        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
   509
        NULL);
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   510
    free(pathbuf);
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   511
    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
   512
        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
   513
    }
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   514
    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
   515
}