| 1 | // natWin32Process.cc - Native side of Win32 process code.
|
|---|
| 2 |
|
|---|
| 3 | /* Copyright (C) 2003 Free Software Foundation
|
|---|
| 4 |
|
|---|
| 5 | This file is part of libgcj.
|
|---|
| 6 |
|
|---|
| 7 | This software is copyrighted work licensed under the terms of the
|
|---|
| 8 | Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
|
|---|
| 9 | details. */
|
|---|
| 10 |
|
|---|
| 11 | #include <config.h>
|
|---|
| 12 |
|
|---|
| 13 | #include <stdio.h>
|
|---|
| 14 |
|
|---|
| 15 | #define WIN32_LEAN_AND_MEAN
|
|---|
| 16 | #include <windows.h>
|
|---|
| 17 |
|
|---|
| 18 | // Conflicts with the definition in "java/lang/reflect/Modifier.h"
|
|---|
| 19 | #undef STRICT
|
|---|
| 20 |
|
|---|
| 21 | #include <gcj/cni.h>
|
|---|
| 22 | #include <jvm.h>
|
|---|
| 23 |
|
|---|
| 24 | #include <java/lang/ConcreteProcess.h>
|
|---|
| 25 | #include <java/lang/IllegalThreadStateException.h>
|
|---|
| 26 | #include <java/lang/InterruptedException.h>
|
|---|
| 27 | #include <java/lang/NullPointerException.h>
|
|---|
| 28 | #include <java/lang/Thread.h>
|
|---|
| 29 | #include <java/io/File.h>
|
|---|
| 30 | #include <java/io/FileDescriptor.h>
|
|---|
| 31 | #include <java/io/FileInputStream.h>
|
|---|
| 32 | #include <java/io/FileOutputStream.h>
|
|---|
| 33 | #include <java/io/IOException.h>
|
|---|
| 34 | #include <java/lang/OutOfMemoryError.h>
|
|---|
| 35 |
|
|---|
| 36 | void
|
|---|
| 37 | java::lang::ConcreteProcess::cleanup (void)
|
|---|
| 38 | {
|
|---|
| 39 | if (inputStream != NULL)
|
|---|
| 40 | {
|
|---|
| 41 | inputStream->close ();
|
|---|
| 42 | inputStream = NULL;
|
|---|
| 43 | }
|
|---|
| 44 |
|
|---|
| 45 | if (outputStream != NULL)
|
|---|
| 46 | {
|
|---|
| 47 | outputStream->close ();
|
|---|
| 48 | outputStream = NULL;
|
|---|
| 49 | }
|
|---|
| 50 |
|
|---|
| 51 | if (errorStream != NULL)
|
|---|
| 52 | {
|
|---|
| 53 | errorStream->close ();
|
|---|
| 54 | errorStream = NULL;
|
|---|
| 55 | }
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | void
|
|---|
| 59 | java::lang::ConcreteProcess::destroy (void)
|
|---|
| 60 | {
|
|---|
| 61 | if (! hasExited ())
|
|---|
| 62 | {
|
|---|
| 63 | // Kill it forcibly and assign an (arbitrary) exit code of 0.
|
|---|
| 64 | TerminateProcess ((HANDLE) procHandle, 0);
|
|---|
| 65 | exitCode = 0;
|
|---|
| 66 |
|
|---|
| 67 | cleanup ();
|
|---|
| 68 | }
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | jboolean
|
|---|
| 72 | java::lang::ConcreteProcess::hasExited (void)
|
|---|
| 73 | {
|
|---|
| 74 | DWORD exitStatus;
|
|---|
| 75 |
|
|---|
| 76 | if (GetExitCodeProcess ((HANDLE) procHandle, &exitStatus) != 0)
|
|---|
| 77 | {
|
|---|
| 78 | // NOTE: STILL_ACTIVE is defined as "259" by Win32 - if the
|
|---|
| 79 | // child actually exits with this return code, we have a
|
|---|
| 80 | // problem here. See MSDN documentation on GetExitCodeProcess( ).
|
|---|
| 81 |
|
|---|
| 82 | if (exitStatus == STILL_ACTIVE)
|
|---|
| 83 | return false;
|
|---|
| 84 | else
|
|---|
| 85 | {
|
|---|
| 86 | cleanup ();
|
|---|
| 87 | exitCode = exitStatus;
|
|---|
| 88 | return true;
|
|---|
| 89 | }
|
|---|
| 90 | }
|
|---|
| 91 | else
|
|---|
| 92 | return true;
|
|---|
| 93 | }
|
|---|
| 94 |
|
|---|
| 95 | jint
|
|---|
| 96 | java::lang::ConcreteProcess::waitFor (void)
|
|---|
| 97 | {
|
|---|
| 98 | if (! hasExited ())
|
|---|
| 99 | {
|
|---|
| 100 | DWORD exitStatus = 0UL;
|
|---|
| 101 |
|
|---|
| 102 | // FIXME: The wait should be interruptible.
|
|---|
| 103 | WaitForSingleObject ((HANDLE) procHandle, INFINITE);
|
|---|
| 104 |
|
|---|
| 105 | GetExitCodeProcess ((HANDLE) procHandle, &exitStatus);
|
|---|
| 106 | exitCode = exitStatus;
|
|---|
| 107 |
|
|---|
| 108 | cleanup ();
|
|---|
| 109 | }
|
|---|
| 110 |
|
|---|
| 111 | return exitCode;
|
|---|
| 112 | }
|
|---|
| 113 |
|
|---|
| 114 | static char *
|
|---|
| 115 | new_string (jstring string)
|
|---|
| 116 | {
|
|---|
| 117 | jsize s = _Jv_GetStringUTFLength (string);
|
|---|
|
|---|