aboutsummaryrefslogtreecommitdiff
path: root/libjava/java/lang/natWin32Process.cc
blob: ff7ddb5f50a91f5b0e8858232e3136b039e68b1f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
// natWin32Process.cc - Native side of Win32 process code.

/* Copyright (C) 2003  Free Software Foundation

   This file is part of libgcj.

This software is copyrighted work licensed under the terms of the
Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
details.  */

#include <config.h>

#include <stdio.h>

#define WIN32_LEAN_AND_MEAN
#include <windows.h>

// Conflicts with the definition in "java/lang/reflect/Modifier.h"
#undef STRICT

#include <gcj/cni.h>
#include <jvm.h>

#include <java/lang/ConcreteProcess.h>
#include <java/lang/IllegalThreadStateException.h>
#include <java/lang/InterruptedException.h>
#include <java/lang/NullPointerException.h>
#include <java/lang/Thread.h>
#include <java/io/File.h>
#include <java/io/FileDescriptor.h>
#include <java/io/FileInputStream.h>
#include <java/io/FileOutputStream.h>
#include <java/io/IOException.h>
#include <java/lang/OutOfMemoryError.h>

void
java::lang::ConcreteProcess::cleanup (void)
{
  if (inputStream != NULL)
    {
      inputStream->close ();
      inputStream = NULL;
    }

  if (outputStream != NULL)
    {
      outputStream->close ();
      outputStream = NULL;
    }

  if (errorStream != NULL)
    {
      errorStream->close ();
      errorStream = NULL;
    }
}

void
java::lang::ConcreteProcess::destroy (void)
{
  if (! hasExited ())
    {
      // Kill it forcibly and assign an (arbitrary) exit code of 0.
      TerminateProcess ((HANDLE) procHandle, 0);
      exitCode = 0;

      cleanup ();
    }
}

jboolean
java::lang::ConcreteProcess::hasExited (void)
{
  DWORD exitStatus;

  if (GetExitCodeProcess ((HANDLE) procHandle, &exitStatus) != 0)
    {
      // NOTE: STILL_ACTIVE is defined as "259" by Win32 - if the
      // child actually exits with this return code, we have a
      // problem here. See MSDN documentation on GetExitCodeProcess( ).

      if (exitStatus == STILL_ACTIVE)
        return false;
      else
        {
          cleanup ();
          exitCode = exitStatus;
          return true;
        }
    }
  else
    return true;
}

jint
java::lang::ConcreteProcess::waitFor (void)
{
  if (! hasExited ())
    {
      DWORD exitStatus = 0UL;

      // FIXME: The wait should be interruptible.
      WaitForSingleObject ((HANDLE) procHandle, INFINITE);

      GetExitCodeProcess ((HANDLE) procHandle, &exitStatus);
      exitCode = exitStatus;

      cleanup ();
    }

  return exitCode;
}

static char *
new_string (jstring string)
{
  jsize s = _Jv_GetStringUTFLength (string);
  char *buf = (char *) _Jv_Malloc (s + 1);
  _Jv_GetStringUTFRegion (string, 0, s, buf);
  buf[s] = '\0';
  return buf;
}

void
java::lang::ConcreteProcess::startProcess (jstringArray progarray,
                                           jstringArray envp,
                                           java::io::File *dir)
{
  using namespace java::io;

  procHandle = (jint) INVALID_HANDLE_VALUE;

  // Reconstruct the command line.
  jstring *elts = elements (progarray);

  int cmdLineLen = 0;

  for (int i = 0; i < progarray->length; ++i)
    cmdLineLen += (_Jv_GetStringUTFLength (elts[i]) + 3);

  char *cmdLine = (char *) _Jv_Malloc (cmdLineLen + 1);
  char *cmdLineCurPos = cmdLine;

  for (int i = 0; i < progarray->length; ++i)
    {
      if (i > 0)
        *cmdLineCurPos++ = ' ';
      *cmdLineCurPos++ = '\"';
      jsize s = _Jv_GetStringUTFLength (elts[i]);
      _Jv_GetStringUTFRegion (elts[i], 0, s, cmdLineCurPos);
      cmdLineCurPos += s;
      *cmdLineCurPos++ = '\"';
    }
  *cmdLineCurPos = '\0';

  // Get the environment, if any.
  char *env = NULL;
  if (envp)
    {
      elts = elements (envp);

      int envLen = 0;
      for (int i = 0; i < envp->length; ++i)
        envLen += (_Jv_GetStringUTFLength (elts[i]) + 1);

      env = (char *) _Jv_Malloc (envLen + 1);

      int j = 0;
      for (int i = 0; i < envp->length; ++i)
        {
          jsize s = _Jv_GetStringUTFLength (elts[i]);
          _Jv_GetStringUTFRegion (elts[i], 0, s, (env + j));

          j += s;
          *(env + j) = '\0';
          j++;
        }
      *(env + j) = '\0';
    }

  // Get the working directory path, if specified.
  char *wdir = NULL;
  if (dir != NULL)
    wdir = new_string (dir->getPath ());

  errorStream = NULL;
  inputStream = NULL;
  outputStream = NULL;

  java::lang::Throwable *exc = NULL;

  try
    {
      // We create anonymous pipes to communicate with the child
      // on each of standard streams.

      HANDLE cldStdInRd, cldStdInWr;
      HANDLE cldStdOutRd, cldStdOutWr;
      HANDLE cldStdErrRd, cldStdErrWr;

      SECURITY_ATTRIBUTES sAttrs;

      // Explicitly allow the handles to the pipes to be inherited.
      sAttrs.nLength = sizeof (SECURITY_ATTRIBUTES);
      sAttrs.bInheritHandle = 1;
      sAttrs.lpSecurityDescriptor = NULL;


      char tmpBuff[64];
      if (CreatePipe (&cldStdInRd, &cldStdInWr, &sAttrs, 0) == 0)
        {
          sprintf (tmpBuff,
                   "Error creating stdin pipe (Win32 Error Code: %lu)",
                   GetLastError ());
          throw new IOException (JvNewStringLatin1 (tmpBuff));
        }

      if (CreatePipe (&cldStdOutRd, &cldStdOutWr, &sAttrs, 0) == 0)
        {
          sprintf (tmpBuff,
                   "Error creating stdout pipe (Win32 Error Code: %lu)",
                   GetLastError ());
          throw new IOException (JvNewStringLatin1 (tmpBuff));
        }

      if (CreatePipe (&cldStdErrRd, &cldStdErrWr, &sAttrs, 0) == 0)
        {
          sprintf (tmpBuff,
                   "Error creating stderr pipe (Win32 Error Code: %lu)",
                   GetLastError ());
          throw new IOException (JvNewStringLatin1 (tmpBuff));
        }

      outputStream = new FileOutputStream
                         (new FileDescriptor ((jint) cldStdInWr));
      inputStream = new FileInputStream
                        (new FileDescriptor ((jint) cldStdOutRd));
      errorStream = new FileInputStream
                        (new FileDescriptor ((jint) cldStdErrRd));

      // Now create the child process.
      PROCESS_INFORMATION pi;
      STARTUPINFO si;

      ZeroMemory (&pi, sizeof (PROCESS_INFORMATION));

      ZeroMemory (&si, sizeof (STARTUPINFO));
      si.cb = sizeof (STARTUPINFO);

      // Explicitly specify the handles to the standard streams.
      si.dwFlags |= STARTF_USESTDHANDLES;

      si.hStdInput = cldStdInRd;
      si.hStdOutput = cldStdOutWr;
      si.hStdError = cldStdErrWr;

      if (CreateProcess (NULL,
                         cmdLine,
                         NULL,
                         NULL,
                         1,
                         0,
                         env,
                         wdir,
                         &si,
                         &pi) == 0)
        {
          sprintf (tmpBuff,
                   "Error creating child process (Win32 Error Code: %lu)",
                   GetLastError ());
          throw new IOException (JvNewStringLatin1 (tmpBuff));
        }

      procHandle = (jint ) pi.hProcess;

      // Close the wrong ends (for the parent) of the pipes.
      CloseHandle (cldStdInRd);
      CloseHandle (cldStdOutWr);
      CloseHandle (cldStdErrWr);

      _Jv_Free (cmdLine);
      if (env != NULL)
        _Jv_Free (env);
      if (wdir != NULL)
        _Jv_Free (wdir);
    }
  catch (java::lang::Throwable *thrown)
    {
      cleanup ();
      exc = thrown;
    }

  if (exc != NULL)
    throw exc;
}