1 /*
   2  * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 #include <assert.h>
  27 #include "java_lang_ProcessImpl.h"
  28 
  29 #include "jni.h"
  30 #include "jvm.h"
  31 #include "jni_util.h"
  32 #include "io_util.h"
  33 #include <windows.h>
  34 #include <io.h>
  35 
  36 /* We try to make sure that we can read and write 4095 bytes (the
  37  * fixed limit on Linux) to the pipe on all operating systems without
  38  * deadlock.  Windows 2000 inexplicably appears to need an extra 24
  39  * bytes of slop to avoid deadlock.
  40  */
  41 #define PIPE_SIZE (4096+24)
  42 
  43 /* We have THREE locales in action:
  44  * 1. Thread default locale - dictates UNICODE-to-8bit conversion
  45  * 2. System locale that defines the message localization
  46  * 3. The file name locale
  47  * Each locale could be an extended locale, that means that text cannot be
  48  * mapped to 8bit sequence without multibyte encoding.
  49  * VM is ready for that, if text is UTF-8.
  50  * Here we make the work right from the beginning.
  51  */
  52 size_t os_error_message(int errnum, WCHAR* utf16_OSErrorMsg, size_t maxMsgLength) {
  53     size_t n = (size_t)FormatMessageW(
  54             FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
  55             NULL,
  56             (DWORD)errnum,
  57             0,
  58             utf16_OSErrorMsg,
  59             (DWORD)maxMsgLength,
  60             NULL);
  61     if (n > 3) {
  62         // Drop final '.', CR, LF
  63         if (utf16_OSErrorMsg[n - 1] == L'\n') --n;
  64         if (utf16_OSErrorMsg[n - 1] == L'\r') --n;
  65         if (utf16_OSErrorMsg[n - 1] == L'.') --n;
  66         utf16_OSErrorMsg[n] = L'\0';
  67     }
  68     return n;
  69 }
  70 
  71 #define MESSAGE_LENGTH (256 + 100)
  72 #define ARRAY_SIZE(x) (sizeof(x)/sizeof(*x))
  73 
  74 static void
  75 win32Error(JNIEnv *env, const WCHAR *functionName)
  76 {
  77     WCHAR utf16_OSErrorMsg[MESSAGE_LENGTH - 100];
  78     WCHAR utf16_javaMessage[MESSAGE_LENGTH];
  79     /*Good suggestion about 2-bytes-per-symbol in localized error reports*/
  80     char  utf8_javaMessage[MESSAGE_LENGTH*2];
  81     const int errnum = (int)GetLastError();
  82     size_t n = os_error_message(errnum, utf16_OSErrorMsg, ARRAY_SIZE(utf16_OSErrorMsg));
  83     n = (n > 0)
  84         ? swprintf(utf16_javaMessage, MESSAGE_LENGTH, L"%s error=%d, %s", functionName, errnum, utf16_OSErrorMsg)
  85         : swprintf(utf16_javaMessage, MESSAGE_LENGTH, L"%s failed, error=%d", functionName, errnum);
  86 
  87     if (n > 0) /*terminate '\0' is not a part of conversion procedure*/
  88         n = WideCharToMultiByte(
  89             CP_UTF8,
  90             0,
  91             utf16_javaMessage,
  92             n, /*by creation n <= MESSAGE_LENGTH*/
  93             utf8_javaMessage,
  94             MESSAGE_LENGTH*2,
  95             NULL,
  96             NULL);
  97 
  98     /*no way to die*/
  99     {
 100         const char *errorMessage = "Secondary error while OS message extraction";
 101         if (n > 0) {
 102             utf8_javaMessage[min(MESSAGE_LENGTH*2 - 1, n)] = '\0';
 103             errorMessage = utf8_javaMessage;
 104         }
 105         JNU_ThrowIOException(env, errorMessage);
 106     }
 107 }
 108 
 109 static void
 110 closeSafely(HANDLE handle)
 111 {
 112     if (handle != INVALID_HANDLE_VALUE)
 113         CloseHandle(handle);
 114 }
 115 
 116 static BOOL hasInheritFlag(HANDLE handle)
 117 {
 118     DWORD mask;
 119     if (GetHandleInformation(handle, &mask)) {
 120         return mask & HANDLE_FLAG_INHERIT;
 121     }
 122     return FALSE;
 123 }
 124 
 125 #define HANDLE_STORAGE_SIZE 6
 126 #define OFFSET_READ  0
 127 #define OFFSET_WRITE 1
 128 //long signed version of INVALID_HANDLE_VALUE
 129 #define JAVA_INVALID_HANDLE_VALUE ((jlong) -1)
 130 #define OPPOSITE_END(offset) (offset==OFFSET_READ ? OFFSET_WRITE : OFFSET_READ)
 131 
 132 /* Pipe holder structure */
 133 typedef struct _STDHOLDER {
 134     HANDLE  pipe[2];
 135     int     offset;
 136 } STDHOLDER;
 137 
 138 /* Responsible for correct initialization of the [pHolder] structure
 139    (that is used for handles recycling) if needs,
 140    and appropriate setup of IOE handle [phStd] for child process based
 141    on created pipe or Java handle. */
 142 static BOOL initHolder(
 143     JNIEnv *env,
 144     jlong *pjhandles,   /* IN OUT - the handle form Java,
 145                                     that can be a file, console or undefined */
 146     STDHOLDER *pHolder, /* OUT    - initialized structure that holds pipe
 147                                     handles */
 148     HANDLE *phStd       /* OUT    - initialized handle for child process */
 149 ) {
 150     /* Here we test the value from Java against invalid
 151        handle value. We are not using INVALID_HANDLE_VALUE macro
 152        due to double signed/unsigned and 32/64bit ambiguity.
 153        Otherwise it will be easy to get the wrong
 154        value   0x00000000FFFFFFFF
 155        instead 0xFFFFFFFFFFFFFFFF. */
 156     if (*pjhandles != JAVA_INVALID_HANDLE_VALUE) {
 157         /* Java file or console redirection */
 158         *phStd = (HANDLE) *pjhandles;
 159         /* Here we set the related Java stream (Process.getXXXXStream())
 160            to [ProcessBuilder.NullXXXXStream.INSTANCE] value.
 161            The initial Java handle [*pjhandles] will be closed in
 162            ANY case. It is not a handle leak. */
 163         *pjhandles = JAVA_INVALID_HANDLE_VALUE;
 164     } else {
 165         /* Creation of parent-child pipe */
 166         if (!CreatePipe(
 167             &pHolder->pipe[OFFSET_READ],
 168             &pHolder->pipe[OFFSET_WRITE],
 169             NULL, /* we would like to inherit
 170                      default process access,
 171                      instead of 'Everybody' access */
 172             PIPE_SIZE))
 173         {
 174             win32Error(env, L"CreatePipe");
 175             return FALSE;
 176         } else {
 177             /* [thisProcessEnd] has no the inherit flag because
 178                the [lpPipeAttributes] param of [CreatePipe]
 179                had the NULL value. */
 180             HANDLE thisProcessEnd = pHolder->pipe[OPPOSITE_END(pHolder->offset)];
 181             *phStd = pHolder->pipe[pHolder->offset];
 182             *pjhandles = (jlong) thisProcessEnd;
 183         }
 184     }
 185     /* Pipe handle will be closed in the [releaseHolder] call,
 186        file handle will be closed in Java.
 187        The long-live handle need to restore the inherit flag,
 188        we do it later in the [prepareIOEHandleState] call. */
 189     SetHandleInformation(
 190         *phStd,
 191         HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT);
 192     return TRUE;
 193 }
 194 
 195 /* Smart recycling of pipe handles in [pHolder]. For the failed
 196    create process attempts, both ends of pipe need to be released.
 197    The [complete] has the [TRUE] value in the failed attempt. */
 198 static void releaseHolder(BOOL complete, STDHOLDER *pHolder) {
 199     closeSafely(pHolder->pipe[pHolder->offset]);
 200     if (complete) {
 201         /* Error occur, close this process pipe end */
 202         closeSafely(pHolder->pipe[OPPOSITE_END(pHolder->offset)]);
 203     }
 204 }
 205 
 206 /* Stores and drops the inherit flag of handles that should not
 207    be shared with the child process by default, but can hold the
 208    inherit flag due to MS process birth specific. */
 209 static void prepareIOEHandleState(
 210     HANDLE *stdIOE,
 211     BOOL *inherit)
 212 {
 213     int i;
 214     for (i = 0; i < HANDLE_STORAGE_SIZE; ++i) {
 215         HANDLE hstd = stdIOE[i];
 216         if (INVALID_HANDLE_VALUE != hstd && hasInheritFlag(hstd)) {
 217             /* FALSE by default */
 218             inherit[i] = TRUE;
 219             /* Java does not need implicit inheritance for IOE handles,
 220                so we drop inherit flag that probably was installed by
 221                previous CreateProcess call that launched current process.
 222                We will return the handle state back after CreateProcess call.
 223                By clearing inherit flag we prevent "greedy grandchild" birth.
 224                The explicit inheritance for child process IOE handles is
 225                implemented in the [initHolder] call. */
 226             SetHandleInformation(hstd, HANDLE_FLAG_INHERIT, 0);
 227         }
 228     }
 229 }
 230 
 231 /* Restores the inheritance flag of handles from stored values. */
 232 static void restoreIOEHandleState(
 233     const HANDLE *stdIOE,
 234     const BOOL *inherit)
 235 {
 236     /* The set of current process standard IOE handles and
 237        the set of child process IOE handles can intersect.
 238        To restore the inherit flag right, we use backward
 239        array iteration. */
 240     int i;
 241     for (i = HANDLE_STORAGE_SIZE - 1; i >= 0; --i)
 242         if (INVALID_HANDLE_VALUE != stdIOE[i]) {
 243            /* Restore inherit flag for any case.
 244               The handle can be changed by explicit inheritance.*/
 245             SetHandleInformation(stdIOE[i],
 246                 HANDLE_FLAG_INHERIT,
 247                 inherit[i] ? HANDLE_FLAG_INHERIT : 0);
 248         }
 249 }
 250 
 251 /*
 252  * Class:     java_lang_ProcessImpl
 253  * Method:    getProcessId0
 254  * Signature: (J)I
 255  */
 256 JNIEXPORT jint JNICALL Java_java_lang_ProcessImpl_getProcessId0
 257   (JNIEnv *env, jclass clazz, jlong handle) {
 258     DWORD pid = GetProcessId((HANDLE) jlong_to_ptr(handle));
 259     return (jint)pid;
 260 }
 261 
 262 /* Please, read about the MS inheritance problem
 263    http://support.microsoft.com/kb/315939
 264    and critical section/synchronized block solution. */
 265 static jlong processCreate(
 266     JNIEnv *env,
 267     const jchar *pcmd,
 268     const jchar *penvBlock,
 269     const jchar *pdir,
 270     jlong *handles,
 271     jboolean redirectErrorStream)
 272 {
 273     jlong ret = 0L;
 274     STARTUPINFOW si = {sizeof(si)};
 275 
 276     /* Handles for which the inheritance flag must be restored. */
 277     HANDLE stdIOE[HANDLE_STORAGE_SIZE] = {
 278         /* Current process standard IOE handles: JDK-7147084 */
 279         INVALID_HANDLE_VALUE, INVALID_HANDLE_VALUE, INVALID_HANDLE_VALUE,
 280         /* Child process IOE handles: JDK-6921885 */
 281         (HANDLE)handles[0], (HANDLE)handles[1], (HANDLE)handles[2]};
 282     BOOL inherit[HANDLE_STORAGE_SIZE] = {
 283         FALSE, FALSE, FALSE,
 284         FALSE, FALSE, FALSE};
 285 
 286     {
 287         /* Extraction of current process standard IOE handles */
 288         DWORD idsIOE[3] = {STD_INPUT_HANDLE, STD_OUTPUT_HANDLE, STD_ERROR_HANDLE};
 289         int i;
 290         for (i = 0; i < 3; ++i)
 291             /* Should not be closed by CloseHandle! */
 292             stdIOE[i] = GetStdHandle(idsIOE[i]);
 293     }
 294 
 295     prepareIOEHandleState(stdIOE, inherit);
 296     {
 297         /* Input */
 298         STDHOLDER holderIn = {{INVALID_HANDLE_VALUE, INVALID_HANDLE_VALUE}, OFFSET_READ};
 299         if (initHolder(env, &handles[0], &holderIn, &si.hStdInput)) {
 300 
 301             /* Output */
 302             STDHOLDER holderOut = {{INVALID_HANDLE_VALUE, INVALID_HANDLE_VALUE}, OFFSET_WRITE};
 303             if (initHolder(env, &handles[1], &holderOut, &si.hStdOutput)) {
 304 
 305                 /* Error */
 306                 STDHOLDER holderErr = {{INVALID_HANDLE_VALUE, INVALID_HANDLE_VALUE}, OFFSET_WRITE};
 307                 BOOL success;
 308                 if (redirectErrorStream) {
 309                     si.hStdError = si.hStdOutput;
 310                     /* Here we set the error stream to [ProcessBuilder.NullInputStream.INSTANCE]
 311                        value. That is in accordance with Java Doc for the redirection case.
 312                        The Java file for the [ handles[2] ] will be closed in ANY case. It is not
 313                        a handle leak. */
 314                     handles[2] = JAVA_INVALID_HANDLE_VALUE;
 315                     success = TRUE;
 316                 } else {
 317                     success = initHolder(env, &handles[2], &holderErr, &si.hStdError);
 318                 }
 319 
 320                 if (success) {
 321                     PROCESS_INFORMATION pi;
 322                     DWORD processFlag = CREATE_UNICODE_ENVIRONMENT;
 323 
 324                     /* Suppress popping-up of a console window for non-console applications */
 325                     if (GetConsoleWindow() == NULL)
 326                         processFlag |= CREATE_NO_WINDOW;
 327 
 328                     si.dwFlags = STARTF_USESTDHANDLES;
 329                     if (!CreateProcessW(
 330                         NULL,             /* executable name */
 331                         (LPWSTR)pcmd,     /* command line */
 332                         NULL,             /* process security attribute */
 333                         NULL,             /* thread security attribute */
 334                         TRUE,             /* inherits system handles */
 335                         processFlag,      /* selected based on exe type */
 336                         (LPVOID)penvBlock,/* environment block */
 337                         (LPCWSTR)pdir,    /* change to the new current directory */
 338                         &si,              /* (in)  startup information */
 339                         &pi))             /* (out) process information */
 340                     {
 341                         win32Error(env, L"CreateProcess");
 342                     } else {
 343                         closeSafely(pi.hThread);
 344                         ret = (jlong)pi.hProcess;
 345                     }
 346                 }
 347                 releaseHolder(ret == 0, &holderErr);
 348                 releaseHolder(ret == 0, &holderOut);
 349             }
 350             releaseHolder(ret == 0, &holderIn);
 351         }
 352     }
 353     restoreIOEHandleState(stdIOE, inherit);
 354 
 355     return ret;
 356 }
 357 
 358 JNIEXPORT jlong JNICALL
 359 Java_java_lang_ProcessImpl_create(JNIEnv *env, jclass ignored,
 360                                   jstring cmd,
 361                                   jstring envBlock,
 362                                   jstring dir,
 363                                   jlongArray stdHandles,
 364                                   jboolean redirectErrorStream)
 365 {
 366     jlong ret = 0;
 367     if (cmd != NULL && stdHandles != NULL) {
 368         const jchar *pcmd = (*env)->GetStringChars(env, cmd, NULL);
 369         if (pcmd != NULL) {
 370             const jchar *penvBlock = (envBlock != NULL)
 371                 ? (*env)->GetStringChars(env, envBlock, NULL)
 372                 : NULL;
 373             if (!(*env)->ExceptionCheck(env)) {
 374                 const jchar *pdir = (dir != NULL)
 375                     ? (*env)->GetStringChars(env, dir, NULL)
 376                     : NULL;
 377                 if (!(*env)->ExceptionCheck(env)) {
 378                     jlong *handles = (*env)->GetLongArrayElements(env, stdHandles, NULL);
 379                     if (handles != NULL) {
 380                         ret = processCreate(
 381                             env,
 382                             pcmd,
 383                             penvBlock,
 384                             pdir,
 385                             handles,
 386                             redirectErrorStream);
 387                         (*env)->ReleaseLongArrayElements(env, stdHandles, handles, 0);
 388                     }
 389                     if (pdir != NULL)
 390                         (*env)->ReleaseStringChars(env, dir, pdir);
 391                 }
 392                 if (penvBlock != NULL)
 393                     (*env)->ReleaseStringChars(env, envBlock, penvBlock);
 394             }
 395             (*env)->ReleaseStringChars(env, cmd, pcmd);
 396         }
 397     }
 398     return ret;
 399 }
 400 
 401 JNIEXPORT jint JNICALL
 402 Java_java_lang_ProcessImpl_getExitCodeProcess(JNIEnv *env, jclass ignored, jlong handle)
 403 {
 404     DWORD exit_code;
 405     if (GetExitCodeProcess((HANDLE) handle, &exit_code) == 0)
 406         win32Error(env, L"GetExitCodeProcess");
 407     return exit_code;
 408 }
 409 
 410 JNIEXPORT jint JNICALL
 411 Java_java_lang_ProcessImpl_getStillActive(JNIEnv *env, jclass ignored)
 412 {
 413     return STILL_ACTIVE;
 414 }
 415 
 416 JNIEXPORT void JNICALL
 417 Java_java_lang_ProcessImpl_waitForInterruptibly(JNIEnv *env, jclass ignored, jlong handle)
 418 {
 419     HANDLE events[2];
 420     events[0] = (HANDLE) handle;
 421     events[1] = JVM_GetThreadInterruptEvent();
 422 
 423     if (WaitForMultipleObjects(sizeof(events)/sizeof(events[0]), events,
 424                                FALSE,    /* Wait for ANY event */
 425                                INFINITE)  /* Wait forever */
 426         == WAIT_FAILED)
 427         win32Error(env, L"WaitForMultipleObjects");
 428 }
 429 
 430 JNIEXPORT void JNICALL
 431 Java_java_lang_ProcessImpl_waitForTimeoutInterruptibly(JNIEnv *env,
 432                                                        jclass ignored,
 433                                                        jlong handle,
 434                                                        jlong timeout)
 435 {
 436     HANDLE events[2];
 437     DWORD dwTimeout = (DWORD)timeout;
 438     DWORD result;
 439     events[0] = (HANDLE) handle;
 440     events[1] = JVM_GetThreadInterruptEvent();
 441     result = WaitForMultipleObjects(sizeof(events)/sizeof(events[0]), events,
 442                                     FALSE,    /* Wait for ANY event */
 443                                     dwTimeout);  /* Wait for dwTimeout */
 444 
 445     if (result == WAIT_FAILED)
 446         win32Error(env, L"WaitForMultipleObjects");
 447 }
 448 
 449 JNIEXPORT void JNICALL
 450 Java_java_lang_ProcessImpl_terminateProcess(JNIEnv *env, jclass ignored, jlong handle)
 451 {
 452     TerminateProcess((HANDLE) handle, 1);
 453 }
 454 
 455 JNIEXPORT jboolean JNICALL
 456 Java_java_lang_ProcessImpl_isProcessAlive(JNIEnv *env, jclass ignored, jlong handle)
 457 {
 458     DWORD dwExitStatus;
 459     GetExitCodeProcess((HANDLE) handle, &dwExitStatus);
 460     return dwExitStatus == STILL_ACTIVE;
 461 }
 462 
 463 JNIEXPORT jboolean JNICALL
 464 Java_java_lang_ProcessImpl_closeHandle(JNIEnv *env, jclass ignored, jlong handle)
 465 {
 466     return (jboolean) CloseHandle((HANDLE) handle);
 467 }
 468 
 469 /**
 470  * Returns a copy of the Unicode characters of a string. Fow now this
 471  * function doesn't handle long path names and other issues.
 472  */
 473 static WCHAR* getPath(JNIEnv *env, jstring ps) {
 474     WCHAR *pathbuf = NULL;
 475     const jchar *chars = (*(env))->GetStringChars(env, ps, NULL);
 476     if (chars != NULL) {
 477         size_t pathlen = wcslen(chars);
 478         pathbuf = (WCHAR*)malloc((pathlen + 6) * sizeof(WCHAR));
 479         if (pathbuf == NULL) {
 480             JNU_ThrowOutOfMemoryError(env, NULL);
 481         } else {
 482             wcscpy(pathbuf, chars);
 483         }
 484         (*env)->ReleaseStringChars(env, ps, chars);
 485     }
 486     return pathbuf;
 487 }
 488 
 489 JNIEXPORT jlong JNICALL
 490 Java_java_lang_ProcessImpl_openForAtomicAppend(JNIEnv *env, jclass ignored, jstring path)
 491 {
 492     const DWORD access = (FILE_GENERIC_WRITE & ~FILE_WRITE_DATA);
 493     const DWORD sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
 494     const DWORD disposition = OPEN_ALWAYS;
 495     const DWORD flagsAndAttributes = FILE_ATTRIBUTE_NORMAL;
 496     HANDLE h;
 497     WCHAR *pathbuf = getPath(env, path);
 498     if (pathbuf == NULL) {
 499         /* Exception already pending */
 500         return -1;
 501     }
 502     h = CreateFileW(
 503         pathbuf,            /* Wide char path name */
 504         access,             /* Read and/or write permission */
 505         sharing,            /* File sharing flags */
 506         NULL,               /* Security attributes */
 507         disposition,        /* creation disposition */
 508         flagsAndAttributes, /* flags and attributes */
 509         NULL);
 510     free(pathbuf);
 511     if (h == INVALID_HANDLE_VALUE) {
 512         JNU_ThrowIOExceptionWithLastError(env, "CreateFileW");
 513     }
 514     return ptr_to_jlong(h);
 515 }