< prev index next >

src/java.base/windows/native/libjava/ProcessImpl_md.c

Print this page
rev 11339 : 8072611: (process) ProcessBuilder redirecting output to file should work with long file names (win)
Reviewed-by: rriggs, simonis
Contributed-by: johannes.scheerer@sap.com, stuefe


  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) {


 450 JNIEXPORT void JNICALL
 451 Java_java_lang_ProcessImpl_terminateProcess(JNIEnv *env, jclass ignored, jlong handle)
 452 {
 453     TerminateProcess((HANDLE) handle, 1);
 454 }
 455 
 456 JNIEXPORT jboolean JNICALL
 457 Java_java_lang_ProcessImpl_isProcessAlive(JNIEnv *env, jclass ignored, jlong handle)
 458 {
 459     DWORD dwExitStatus;
 460     GetExitCodeProcess((HANDLE) handle, &dwExitStatus);
 461     return dwExitStatus == STILL_ACTIVE;
 462 }
 463 
 464 JNIEXPORT jboolean JNICALL
 465 Java_java_lang_ProcessImpl_closeHandle(JNIEnv *env, jclass ignored, jlong handle)
 466 {
 467     return (jboolean) CloseHandle((HANDLE) handle);
 468 }
 469 
 470 /**
 471  * Returns a copy of the Unicode characters of a string. Fow now this
 472  * function doesn't handle long path names and other issues.
 473  */
 474 static WCHAR* getPath(JNIEnv *env, jstring ps) {
 475     WCHAR *pathbuf = NULL;
 476     const jchar *chars = (*(env))->GetStringChars(env, ps, NULL);
 477     if (chars != NULL) {
 478         size_t pathlen = wcslen(chars);
 479         pathbuf = (WCHAR*)malloc((pathlen + 6) * sizeof(WCHAR));
 480         if (pathbuf == NULL) {
 481             JNU_ThrowOutOfMemoryError(env, NULL);
 482         } else {
 483             wcscpy(pathbuf, chars);
 484         }
 485         (*env)->ReleaseStringChars(env, ps, chars);
 486     }
 487     return pathbuf;
 488 }
 489 
 490 JNIEXPORT jlong JNICALL
 491 Java_java_lang_ProcessImpl_openForAtomicAppend(JNIEnv *env, jclass ignored, jstring path)
 492 {
 493     const DWORD access = (FILE_GENERIC_WRITE & ~FILE_WRITE_DATA);
 494     const DWORD sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
 495     const DWORD disposition = OPEN_ALWAYS;
 496     const DWORD flagsAndAttributes = FILE_ATTRIBUTE_NORMAL;
 497     HANDLE h;
 498     WCHAR *pathbuf = getPath(env, path);
 499     if (pathbuf == NULL) {
 500         /* Exception already pending */
 501         return -1;
 502     }
 503     h = CreateFileW(
 504         pathbuf,            /* Wide char path name */
 505         access,             /* Read and/or write permission */
 506         sharing,            /* File sharing flags */
 507         NULL,               /* Security attributes */
 508         disposition,        /* creation disposition */
 509         flagsAndAttributes, /* flags and attributes */
 510         NULL);
 511     free(pathbuf);
 512     if (h == INVALID_HANDLE_VALUE) {
 513         JNU_ThrowIOExceptionWithLastError(env, "CreateFileW");
 514     }
 515     return ptr_to_jlong(h);
 516 }


  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 "io_util_md.h"
  34 #include <windows.h>
  35 #include <io.h>
  36 
  37 /* We try to make sure that we can read and write 4095 bytes (the
  38  * fixed limit on Linux) to the pipe on all operating systems without
  39  * deadlock.  Windows 2000 inexplicably appears to need an extra 24
  40  * bytes of slop to avoid deadlock.
  41  */
  42 #define PIPE_SIZE (4096+24)
  43 
  44 /* We have THREE locales in action:
  45  * 1. Thread default locale - dictates UNICODE-to-8bit conversion
  46  * 2. System locale that defines the message localization
  47  * 3. The file name locale
  48  * Each locale could be an extended locale, that means that text cannot be
  49  * mapped to 8bit sequence without multibyte encoding.
  50  * VM is ready for that, if text is UTF-8.
  51  * Here we make the work right from the beginning.
  52  */
  53 size_t os_error_message(int errnum, WCHAR* utf16_OSErrorMsg, size_t maxMsgLength) {


 451 JNIEXPORT void JNICALL
 452 Java_java_lang_ProcessImpl_terminateProcess(JNIEnv *env, jclass ignored, jlong handle)
 453 {
 454     TerminateProcess((HANDLE) handle, 1);
 455 }
 456 
 457 JNIEXPORT jboolean JNICALL
 458 Java_java_lang_ProcessImpl_isProcessAlive(JNIEnv *env, jclass ignored, jlong handle)
 459 {
 460     DWORD dwExitStatus;
 461     GetExitCodeProcess((HANDLE) handle, &dwExitStatus);
 462     return dwExitStatus == STILL_ACTIVE;
 463 }
 464 
 465 JNIEXPORT jboolean JNICALL
 466 Java_java_lang_ProcessImpl_closeHandle(JNIEnv *env, jclass ignored, jlong handle)
 467 {
 468     return (jboolean) CloseHandle((HANDLE) handle);
 469 }
 470 




















 471 JNIEXPORT jlong JNICALL
 472 Java_java_lang_ProcessImpl_openForAtomicAppend(JNIEnv *env, jclass ignored, jstring path)
 473 {
 474     const DWORD access = (FILE_GENERIC_WRITE & ~FILE_WRITE_DATA);
 475     const DWORD sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
 476     const DWORD disposition = OPEN_ALWAYS;
 477     const DWORD flagsAndAttributes = FILE_ATTRIBUTE_NORMAL;
 478     HANDLE h;
 479     WCHAR *pathbuf = pathToNTPath(env, path, JNI_FALSE);
 480     if (pathbuf == NULL) {
 481         /* Exception already pending */
 482         return -1;
 483     }
 484     h = CreateFileW(
 485         pathbuf,            /* Wide char path name */
 486         access,             /* Read and/or write permission */
 487         sharing,            /* File sharing flags */
 488         NULL,               /* Security attributes */
 489         disposition,        /* creation disposition */
 490         flagsAndAttributes, /* flags and attributes */
 491         NULL);
 492     free(pathbuf);
 493     if (h == INVALID_HANDLE_VALUE) {
 494         JNU_ThrowIOExceptionWithLastError(env, "CreateFileW");
 495     }
 496     return ptr_to_jlong(h);
 497 }
< prev index next >