author | timkelly |
Wed, 05 Aug 2009 17:35:39 -0500 | |
changeset 51 | 49c226a8748e |
parent 37 | c2bce6dd59e7 |
permissions | -rw-r--r-- |
37 | 1 |
/******************************************************************************* |
2 |
* Copyright (c) 2002, 2008 QNX Software Systems and others. |
|
3 |
* All rights reserved. This program and the accompanying materials |
|
4 |
* are made available under the terms of the Eclipse Public License v1.0 |
|
5 |
* which accompanies this distribution, and is available at |
|
6 |
* http://www.eclipse.org/legal/epl-v10.html |
|
7 |
* |
|
8 |
* Contributors: |
|
9 |
* QNX Software Systems - initial API and implementation |
|
10 |
* Wind River Systems, Inc. |
|
11 |
* |
|
12 |
* Win32ProcessEx.c |
|
13 |
* |
|
14 |
* This is a JNI implementation of spawner |
|
15 |
*******************************************************************************/ |
|
16 |
||
17 |
#include "stdafx.h" |
|
18 |
#include <string.h> |
|
19 |
#include <stdlib.h> |
|
20 |
#include <process.h> |
|
21 |
#include "Spawner.h" |
|
22 |
||
23 |
#include "jni.h" |
|
24 |
#include "io.h" |
|
25 |
||
26 |
||
27 |
#define PIPE_SIZE 512 // Size of pipe buffer |
|
28 |
#define MAX_CMD_SIZE 2049 // Initial size of command line |
|
29 |
#define MAX_ENV_SIZE 4096 // Initial size of environment block |
|
30 |
#define PIPE_NAME_LENGTH 100 // Size of pipe name buffer |
|
31 |
#define PIPE_TIMEOUT 10000 // Default time-out value, in milliseconds. |
|
32 |
||
33 |
#define MAX_PROCS (100) // Maximum number of simultaneiously runnig processes |
|
34 |
||
35 |
||
36 |
// Process description block. Should be created for each launched process |
|
37 |
typedef struct _procInfo { |
|
38 |
int pid; // Process ID |
|
39 |
int uid; // quasi-unique process ID; we have to create it to avoid duplicated pid |
|
40 |
// (actually this impossible from OS point of view but it is still possible |
|
41 |
// a clash of new created and already finished process with one and the same PID. |
|
51
49c226a8748e
CDT 6.0 from cdt_6_0 branch (eclipse.org cvs repository). This overwrites previous CDT 6.0 merges.
timkelly
parents:
37
diff
changeset
|
42 |
// 3 events connected to this process (see starter) |
49c226a8748e
CDT 6.0 from cdt_6_0 branch (eclipse.org cvs repository). This overwrites previous CDT 6.0 merges.
timkelly
parents:
37
diff
changeset
|
43 |
HANDLE eventBreak; |
37 | 44 |
HANDLE eventWait; |
51
49c226a8748e
CDT 6.0 from cdt_6_0 branch (eclipse.org cvs repository). This overwrites previous CDT 6.0 merges.
timkelly
parents:
37
diff
changeset
|
45 |
HANDLE eventTerminate; |
37 | 46 |
} procInfo_t, * pProcInfo_t; |
47 |
||
48 |
static int procCounter = 0; // Number of running processes |
|
49 |
||
50 |
||
51 |
// This is a VM helper |
|
52 |
void ThrowByName(JNIEnv *env, const char *name, const char *msg); |
|
53 |
||
54 |
// Creates _procInfo block for every launched procss |
|
55 |
pProcInfo_t createProcInfo(); |
|
56 |
||
57 |
// Find process description for this pid |
|
58 |
pProcInfo_t findProcInfo(int pid); |
|
59 |
||
60 |
// We launch separate thread for each project to trap it termination |
|
61 |
void _cdecl waitProcTermination(void* pv) ; |
|
62 |
||
63 |
// This is a helper function to prevent losing of quotatin marks |
|
64 |
static int copyTo(wchar_t * target, const wchar_t * source, int cpyLenght, int availSpace); |
|
65 |
||
66 |
// Use this function to clean project descriptor and return it to the pool of available blocks. |
|
67 |
static void cleanUpProcBlock(pProcInfo_t pCurProcInfo); |
|
68 |
||
69 |
||
70 |
// Signal codes |
|
71 |
typedef enum { |
|
72 |
SIG_NOOP, |
|
73 |
SIG_HUP, |
|
74 |
SIG_INT, |
|
75 |
SIG_KILL = 9, |
|
76 |
SIG_TERM = 15, |
|
77 |
} signals; |
|
78 |
||
79 |
extern CRITICAL_SECTION cs; |
|
80 |
||
81 |
||
82 |
extern wchar_t path[MAX_PATH]; // Directory where spawner.dll is located |
|
83 |
||
84 |
static HMODULE hVM = NULL; // VM handler |
|
85 |
||
86 |
||
87 |
static pProcInfo_t pInfo = NULL; |
|
88 |
||
89 |
static int nCounter = 0; // We use it to build unique synchronisation object names |
|
90 |
||
91 |
///////////////////////////////////////////////////////////////////////////////////// |
|
92 |
// Launcher; launchess process and traps its termination |
|
93 |
// Arguments: (see Spawner.java) |
|
94 |
// [in] cmdarray - array of command line elements |
|
95 |
// [in] envp - array of environment variables |
|
96 |
// [in] dir - working directory |
|
97 |
// [out] channels - streams handlers |
|
98 |
///////////////////////////////////////////////////////////////////////////////////// |
|
99 |
||
100 |
extern "C" |
|
101 |
JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_spawner_Spawner_exec2 |
|
102 |
(JNIEnv * env, jobject process, jobjectArray cmdarray, jobjectArray envp, jstring dir, jintArray channels, jstring slaveName, jint fdm) |
|
103 |
{ |
|
104 |
return -1; |
|
105 |
} |
|
106 |
||
107 |
void ensureSize(wchar_t** ptr, int* psize, int requiredLength) |
|
108 |
{ |
|
109 |
int size= *psize; |
|
110 |
if (requiredLength > size) { |
|
111 |
size= 2*size; |
|
112 |
if (size < requiredLength) { |
|
113 |
size= requiredLength; |
|
114 |
} |
|
115 |
*ptr= (wchar_t *)realloc(*ptr, size * sizeof(wchar_t)); |
|
116 |
if (NULL == *ptr) { |
|
117 |
*psize= 0; |
|
118 |
} |
|
119 |
else { |
|
120 |
*psize= size; |
|
121 |
} |
|
122 |
} |
|
123 |
} |
|
124 |
||
125 |
extern "C" |
|
126 |
JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_spawner_Spawner_exec0 |
|
127 |
(JNIEnv * env, jobject process, jobjectArray cmdarray, jobjectArray envp, jstring dir, jintArray channels) |
|
128 |
{ |
|
129 |
HANDLE stdHandles[3]; |
|
130 |
PROCESS_INFORMATION pi = {0}, *piCopy; |
|
131 |
STARTUPINFOW si; |
|
132 |
DWORD flags = 0; |
|
133 |
const wchar_t * cwd = NULL; |
|
134 |
LPVOID envBlk = NULL; |
|
135 |
int ret = 0; |
|
136 |
int nCmdLineLength= 0; |
|
137 |
wchar_t * szCmdLine= 0; |
|
138 |
int nBlkSize = MAX_ENV_SIZE; |
|
139 |
wchar_t * szEnvBlock = NULL; |
|
140 |
jsize nCmdTokens = 0; |
|
141 |
jsize nEnvVars = 0; |
|
142 |
int i; |
|
143 |
DWORD pid = GetCurrentProcessId(); |
|
144 |
int nPos; |
|
145 |
pProcInfo_t pCurProcInfo; |
|
146 |
wchar_t eventBreakName[20]; |
|
147 |
wchar_t eventWaitName[20]; |
|
148 |
wchar_t eventTerminateName[20]; |
|
149 |
#ifdef DEBUG_MONITOR |
|
150 |
wchar_t buffer[1000]; |
|
151 |
#endif |
|
152 |
int nLocalCounter; |
|
153 |
wchar_t inPipeName[PIPE_NAME_LENGTH]; |
|
154 |
wchar_t outPipeName[PIPE_NAME_LENGTH]; |
|
155 |
wchar_t errPipeName[PIPE_NAME_LENGTH]; |
|
156 |
||
157 |
nCmdLineLength= MAX_CMD_SIZE; |
|
158 |
szCmdLine= (wchar_t *)malloc(nCmdLineLength * sizeof(wchar_t)); |
|
159 |
szCmdLine[0]= _T('\0'); |
|
160 |
if((HIBYTE(LOWORD(GetVersion()))) & 0x80) |
|
161 |
{ |
|
162 |
ThrowByName(env, "java/io/IOException", "Does not support Windows 3.1/95/98/Me"); |
|
163 |
return 0; |
|
164 |
} |
|
165 |
||
166 |
if (cmdarray == 0) |
|
167 |
{ |
|
168 |
ThrowByName(env, "java/lang/NullPointerException", "No command line specified"); |
|
169 |
return 0; |
|
170 |
} |
|
171 |
||
172 |
ZeroMemory(stdHandles, sizeof(stdHandles)); |
|
173 |
||
174 |
// Create pipe names |
|
175 |
EnterCriticalSection(&cs); |
|
176 |
swprintf(inPipeName, L"\\\\.\\pipe\\stdin%08i%010i", pid, nCounter); |
|
177 |
swprintf(outPipeName, L"\\\\.\\pipe\\stdout%08i%010i", pid, nCounter); |
|
178 |
swprintf(errPipeName, L"\\\\.\\pipe\\stderr%08i%010i", pid, nCounter); |
|
179 |
nLocalCounter = nCounter; |
|
180 |
++nCounter; |
|
181 |
LeaveCriticalSection(&cs); |
|
182 |
||
183 |
if ((INVALID_HANDLE_VALUE == (stdHandles[0] = CreateNamedPipeW(inPipeName, PIPE_ACCESS_OUTBOUND, |
|
184 |
PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT, |
|
185 |
PIPE_UNLIMITED_INSTANCES, PIPE_SIZE, PIPE_SIZE, PIPE_TIMEOUT, NULL))) || |
|
186 |
(INVALID_HANDLE_VALUE == (stdHandles[1] = CreateNamedPipeW(outPipeName, PIPE_ACCESS_INBOUND | FILE_FLAG_OVERLAPPED, |
|
187 |
PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT, |
|
188 |
PIPE_UNLIMITED_INSTANCES, PIPE_SIZE, PIPE_SIZE, PIPE_TIMEOUT, NULL))) || |
|
189 |
(INVALID_HANDLE_VALUE == (stdHandles[2] = CreateNamedPipeW(errPipeName, PIPE_ACCESS_INBOUND | FILE_FLAG_OVERLAPPED, |
|
190 |
PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT, |
|
191 |
PIPE_UNLIMITED_INSTANCES, PIPE_SIZE, PIPE_SIZE, PIPE_TIMEOUT, NULL)))) { |
|
192 |
CloseHandle(stdHandles[0]); |
|
193 |
CloseHandle(stdHandles[1]); |
|
194 |
CloseHandle(stdHandles[2]); |
|
195 |
ThrowByName(env, "java/io/IOException", "CreatePipe"); |
|
196 |
return 0; |
|
197 |
} |
|
198 |
||
199 |
#ifdef DEBUG_MONITOR |
|
200 |
swprintf(buffer, _T("Opened pipes: %s, %s, %s\n"), inPipeName, outPipeName, errPipeName); |
|
201 |
OutputDebugStringW(buffer); |
|
202 |
#endif |
|
203 |
||
204 |
||
205 |
nCmdTokens = env->GetArrayLength(cmdarray); |
|
206 |
nEnvVars = env->GetArrayLength(envp); |
|
207 |
||
208 |
pCurProcInfo = createProcInfo(); |
|
209 |
||
210 |
if(NULL == pCurProcInfo) |
|
211 |
{ |
|
212 |
ThrowByName(env, "java/io/IOException", "Too many processes"); |
|
213 |
return 0; |
|
214 |
} |
|
215 |
||
216 |
// Construct starter's command line |
|
217 |
swprintf(eventBreakName, L"SABreak%p", pCurProcInfo); |
|
218 |
swprintf(eventWaitName, L"SAWait%p", pCurProcInfo); |
|
219 |
swprintf(eventTerminateName, L"SATerm%p", pCurProcInfo); |
|
51
49c226a8748e
CDT 6.0 from cdt_6_0 branch (eclipse.org cvs repository). This overwrites previous CDT 6.0 merges.
timkelly
parents:
37
diff
changeset
|
220 |
pCurProcInfo -> eventBreak = CreateEventW(NULL, TRUE, FALSE, eventBreakName); |
49c226a8748e
CDT 6.0 from cdt_6_0 branch (eclipse.org cvs repository). This overwrites previous CDT 6.0 merges.
timkelly
parents:
37
diff
changeset
|
221 |
ResetEvent(pCurProcInfo -> eventBreak); |
49c226a8748e
CDT 6.0 from cdt_6_0 branch (eclipse.org cvs repository). This overwrites previous CDT 6.0 merges.
timkelly
parents:
37
diff
changeset
|
222 |
pCurProcInfo -> eventWait = CreateEventW(NULL, TRUE, FALSE, eventWaitName); |
49c226a8748e
CDT 6.0 from cdt_6_0 branch (eclipse.org cvs repository). This overwrites previous CDT 6.0 merges.
timkelly
parents:
37
diff
changeset
|
223 |
ResetEvent(pCurProcInfo -> eventWait); |
49c226a8748e
CDT 6.0 from cdt_6_0 branch (eclipse.org cvs repository). This overwrites previous CDT 6.0 merges.
timkelly
parents:
37
diff
changeset
|
224 |
pCurProcInfo -> eventTerminate = CreateEventW(NULL, TRUE, FALSE, eventTerminateName); |
49c226a8748e
CDT 6.0 from cdt_6_0 branch (eclipse.org cvs repository). This overwrites previous CDT 6.0 merges.
timkelly
parents:
37
diff
changeset
|
225 |
ResetEvent(pCurProcInfo -> eventTerminate); |
37 | 226 |
|
51
49c226a8748e
CDT 6.0 from cdt_6_0 branch (eclipse.org cvs repository). This overwrites previous CDT 6.0 merges.
timkelly
parents:
37
diff
changeset
|
227 |
swprintf(szCmdLine, L"\"%sstarter.exe\" %i %i %s %s %s ", path, pid, nLocalCounter, eventBreakName, eventWaitName, eventTerminateName); |
37 | 228 |
nPos = wcslen(szCmdLine); |
229 |
||
230 |
// Prepare command line |
|
231 |
for(i = 0; i < nCmdTokens; ++i) |
|
232 |
{ |
|
233 |
jstring item = (jstring)env->GetObjectArrayElement(cmdarray, i); |
|
234 |
jsize len = env->GetStringLength(item); |
|
235 |
int nCpyLen; |
|
236 |
const wchar_t * str = (const wchar_t *)env->GetStringChars(item, 0); |
|
237 |
if(NULL != str) |
|
238 |
{ |
|
239 |
int requiredSize= nPos+len+2; |
|
240 |
if (requiredSize > 32*1024) { |
|
241 |
ThrowByName(env, "java/io/IOException", "Command line too long"); |
|
242 |
return 0; |
|
243 |
} |
|
244 |
ensureSize(&szCmdLine, &nCmdLineLength, requiredSize); |
|
245 |
if (NULL == szCmdLine) { |
|
246 |
ThrowByName(env, "java/io/IOException", "Not enough memory"); |
|
247 |
return 0; |
|
248 |
} |
|
249 |
||
250 |
if(0 > (nCpyLen = copyTo(szCmdLine + nPos, str, len, nCmdLineLength - nPos))) |
|
251 |
{ |
|
252 |
ThrowByName(env, "java/io/IOException", "Command line too long"); |
|
253 |
return 0; |
|
254 |
} |
|
255 |
nPos += nCpyLen; |
|
256 |
szCmdLine[nPos] = _T(' '); |
|
257 |
++nPos; |
|
258 |
env->ReleaseStringChars(item, (const jchar *)str); |
|
259 |
} |
|
260 |
} |
|
261 |
szCmdLine[nPos] = _T('\0'); |
|
262 |
||
263 |
#ifdef DEBUG_MONITOR |
|
264 |
swprintf(buffer, _T("There are %i environment variables \n"), nEnvVars); |
|
265 |
OutputDebugStringW(buffer); |
|
266 |
#endif |
|
267 |
// Prepare environment block |
|
268 |
if (nEnvVars > 0) |
|
269 |
{ |
|
270 |
nPos = 0; |
|
271 |
szEnvBlock = (wchar_t *)malloc(nBlkSize * sizeof(wchar_t)); |
|
272 |
for(i = 0; i < nEnvVars; ++i) |
|
273 |
{ |
|
274 |
jstring item = (jstring)env->GetObjectArrayElement(envp, i); |
|
275 |
jsize len = env->GetStringLength(item); |
|
276 |
const wchar_t * str = (const wchar_t *)env->GetStringChars(item, 0); |
|
277 |
if(NULL != str) |
|
278 |
{ |
|
279 |
while((nBlkSize - nPos) <= (len + 2)) // +2 for two '\0' |
|
280 |
{ |
|
281 |
nBlkSize += MAX_ENV_SIZE; |
|
282 |
szEnvBlock = (wchar_t *)realloc(szEnvBlock, nBlkSize * sizeof(wchar_t)); |
|
283 |
if(NULL == szEnvBlock) |
|
284 |
{ |
|
285 |
ThrowByName(env, "java/io/IOException", "Not enough memory"); |
|
286 |
return 0; |
|
287 |
} |
|
288 |
#ifdef DEBUG_MONITOR |
|
289 |
swprintf(buffer, _T("Realloc environment block; new length is %i \n"), nBlkSize); |
|
290 |
OutputDebugStringW(buffer); |
|
291 |
#endif |
|
292 |
||
293 |
} |
|
294 |
#ifdef DEBUG_MONITOR |
|
295 |
swprintf(buffer, _T("%s\n"), str); |
|
296 |
OutputDebugStringW(buffer); |
|
297 |
#endif |
|
298 |
wcsncpy(szEnvBlock + nPos, str, len); |
|
299 |
nPos += len; |
|
300 |
szEnvBlock[nPos] = _T('\0'); |
|
301 |
++nPos; |
|
302 |
env->ReleaseStringChars(item, (const jchar *)str); |
|
303 |
} |
|
304 |
} |
|
305 |
szEnvBlock[nPos] = _T('\0'); |
|
306 |
} |
|
307 |
||
308 |
||
309 |
||
310 |
if (dir != 0) |
|
311 |
{ |
|
312 |
const wchar_t * str = (const wchar_t *)env->GetStringChars(dir, 0); |
|
313 |
if(NULL != str) |
|
314 |
{ |
|
315 |
cwd = wcsdup(str); |
|
316 |
env->ReleaseStringChars(dir, (const jchar *)str); |
|
317 |
} |
|
318 |
} |
|
319 |
||
320 |
||
321 |
ZeroMemory(&si, sizeof(si)); |
|
322 |
si.cb = sizeof(si); |
|
323 |
si.dwFlags |= STARTF_USESHOWWINDOW; |
|
324 |
si.wShowWindow = SW_HIDE; // Processes in the Process Group are hidden |
|
325 |
||
326 |
||
327 |
||
328 |
SetHandleInformation(stdHandles[0], HANDLE_FLAG_INHERIT, FALSE); |
|
329 |
SetHandleInformation(stdHandles[1], HANDLE_FLAG_INHERIT, FALSE); |
|
330 |
SetHandleInformation(stdHandles[2], HANDLE_FLAG_INHERIT, FALSE); |
|
331 |
||
332 |
flags = CREATE_NEW_CONSOLE; |
|
333 |
flags |= CREATE_NO_WINDOW; |
|
334 |
flags |= CREATE_UNICODE_ENVIRONMENT; |
|
335 |
||
336 |
#ifdef DEBUG_MONITOR |
|
337 |
OutputDebugStringW(szCmdLine); |
|
338 |
#endif |
|
339 |
// launches starter; we need it to create another console group to correctly process |
|
340 |
// emulation of SYSint signal (Ctrl-C) |
|
341 |
ret = CreateProcessW(0, /* executable name */ |
|
342 |
szCmdLine, /* command line */ |
|
343 |
0, /* process security attribute */ |
|
344 |
0, /* thread security attribute */ |
|
345 |
FALSE, /* inherits system handles */ |
|
346 |
flags, /* normal attached process */ |
|
347 |
szEnvBlock, /* environment block */ |
|
348 |
cwd, /* change to the new current directory */ |
|
349 |
&si, /* (in) startup information */ |
|
350 |
&pi); /* (out) process information */ |
|
351 |
||
352 |
if(NULL != cwd) |
|
353 |
free((void *)cwd); |
|
354 |
||
355 |
if(NULL != szEnvBlock) |
|
356 |
free(szEnvBlock); |
|
357 |
||
358 |
if(NULL != szCmdLine) |
|
359 |
free(szCmdLine); |
|
360 |
||
361 |
if (!ret) // Launching error |
|
362 |
{ |
|
363 |
char * lpMsgBuf; |
|
364 |
CloseHandle(stdHandles[0]); |
|
365 |
CloseHandle(stdHandles[1]); |
|
366 |
CloseHandle(stdHandles[2]); |
|
367 |
FormatMessageA( |
|
368 |
FORMAT_MESSAGE_ALLOCATE_BUFFER | |
|
369 |
FORMAT_MESSAGE_FROM_SYSTEM | |
|
370 |
FORMAT_MESSAGE_IGNORE_INSERTS, |
|
371 |
NULL, |
|
372 |
GetLastError(), |
|
373 |
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language |
|
374 |
(char *)&lpMsgBuf, |
|
375 |
0, |
|
376 |
NULL |
|
377 |
); |
|
378 |
ThrowByName(env, "java/io/IOException", lpMsgBuf); |
|
379 |
// Free the buffer. |
|
380 |
LocalFree( lpMsgBuf ); |
|
381 |
cleanUpProcBlock(pCurProcInfo); |
|
382 |
ret = -1; |
|
383 |
} |
|
384 |
else |
|
385 |
{ |
|
386 |
int file_handles[3]; |
|
387 |
HANDLE h[2]; |
|
388 |
int what; |
|
389 |
||
390 |
EnterCriticalSection(&cs); |
|
391 |
||
392 |
pCurProcInfo -> pid = pi.dwProcessId; |
|
393 |
h[0] = pCurProcInfo -> eventWait; |
|
394 |
h[1] = pi.hProcess; |
|
395 |
||
396 |
what = WaitForMultipleObjects(2, h, FALSE, INFINITE); |
|
397 |
if(what != WAIT_OBJECT_0) // CreateProcess failed |
|
398 |
{ |
|
399 |
#ifdef DEBUG_MONITOR |
|
400 |
swprintf(buffer, _T("Process %i failed\n"), pi.dwProcessId); |
|
401 |
OutputDebugStringW(buffer); |
|
402 |
#endif |
|
403 |
cleanUpProcBlock(pCurProcInfo); |
|
404 |
ThrowByName(env, "java/io/IOException", "Launching failed"); |
|
405 |
#ifdef DEBUG_MONITOR |
|
406 |
OutputDebugStringW(_T("Process failed\n")); |
|
407 |
#endif |
|
408 |
} |
|
409 |
else |
|
410 |
{ |
|
411 |
ret = (long)(pCurProcInfo -> uid); |
|
412 |
||
413 |
// Prepare stream handlers to return to java program |
|
414 |
file_handles[0] = (int)stdHandles[0]; |
|
415 |
file_handles[1] = (int)stdHandles[1]; |
|
416 |
file_handles[2] = (int)stdHandles[2]; |
|
417 |
env->SetIntArrayRegion(channels, 0, 3, (jint *)file_handles); |
|
418 |
||
419 |
// do the cleanup so launch the according thread |
|
420 |
// create a copy of the PROCESS_INFORMATION as this might get destroyed |
|
421 |
piCopy = (PROCESS_INFORMATION *)malloc(sizeof(PROCESS_INFORMATION)); |
|
422 |
memcpy(piCopy, &pi, sizeof(PROCESS_INFORMATION)); |
|
423 |
_beginthread(waitProcTermination, 0, (void *)piCopy); |
|
424 |
||
425 |
#ifdef DEBUG_MONITOR |
|
426 |
OutputDebugStringW(_T("Process started\n")); |
|
427 |
#endif |
|
428 |
} |
|
429 |
LeaveCriticalSection(&cs); |
|
430 |
||
431 |
} |
|
432 |
||
433 |
CloseHandle(pi.hThread); |
|
434 |
||
435 |
return ret; |
|
436 |
||
437 |
} |
|
438 |
||
439 |
||
440 |
///////////////////////////////////////////////////////////////////////////////////// |
|
441 |
// Launcher; just launches process and don't care about it any more |
|
442 |
// Arguments: (see Spawner.java) |
|
443 |
// [in] cmdarray - array of command line elements |
|
444 |
// [in] envp - array of environment variables |
|
445 |
// [in] dir - working directory |
|
446 |
///////////////////////////////////////////////////////////////////////////////////// |
|
447 |
extern "C" |
|
448 |
JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_spawner_Spawner_exec1 |
|
449 |
(JNIEnv * env, jobject process, jobjectArray cmdarray, jobjectArray envp, jstring dir) |
|
450 |
{ |
|
451 |
||
452 |
SECURITY_ATTRIBUTES sa; |
|
453 |
PROCESS_INFORMATION pi = {0}; |
|
454 |
STARTUPINFOW si; |
|
455 |
DWORD flags = 0; |
|
456 |
wchar_t * cwd = NULL; |
|
457 |
wchar_t * envBlk = NULL; |
|
458 |
int ret = 0; |
|
459 |
jsize nCmdTokens = 0; |
|
460 |
jsize nEnvVars = 0; |
|
461 |
int i; |
|
462 |
int nPos; |
|
463 |
int nCmdLineLength= 0; |
|
464 |
wchar_t * szCmdLine= 0; |
|
465 |
int nBlkSize = MAX_ENV_SIZE; |
|
466 |
wchar_t * szEnvBlock = NULL; |
|
467 |
||
468 |
nCmdLineLength= MAX_CMD_SIZE; |
|
469 |
szCmdLine= (wchar_t *)malloc(nCmdLineLength * sizeof(wchar_t)); |
|
470 |
szCmdLine[0]= 0; |
|
471 |
||
472 |
sa.nLength = sizeof(sa); |
|
473 |
sa.lpSecurityDescriptor = 0; |
|
474 |
sa.bInheritHandle = TRUE; |
|
475 |
||
476 |
||
477 |
nCmdTokens = env->GetArrayLength(cmdarray); |
|
478 |
nEnvVars = env->GetArrayLength(envp); |
|
479 |
||
480 |
nPos = 0; |
|
481 |
||
482 |
// Prepare command line |
|
483 |
for(i = 0; i < nCmdTokens; ++i) |
|
484 |
{ |
|
485 |
jstring item = (jstring)env->GetObjectArrayElement(cmdarray, i); |
|
486 |
jsize len = env->GetStringLength(item); |
|
487 |
int nCpyLen; |
|
488 |
const wchar_t * str = (const wchar_t *)env->GetStringChars(item, 0); |
|
489 |
if(NULL != str) |
|
490 |
{ |
|
491 |
int requiredSize= nPos+len+2; |
|
492 |
if (requiredSize > 32*1024) { |
|
493 |
ThrowByName(env, "java/io/IOException", "Command line too long"); |
|
494 |
return 0; |
|
495 |
} |
|
496 |
ensureSize(&szCmdLine, &nCmdLineLength, requiredSize); |
|
497 |
if (NULL == szCmdLine) { |
|
498 |
ThrowByName(env, "java/io/IOException", "Not enough memory"); |
|
499 |
return 0; |
|
500 |
} |
|
501 |
||
502 |
if(0 > (nCpyLen = copyTo(szCmdLine + nPos, str, len, nCmdLineLength - nPos))) |
|
503 |
{ |
|
504 |
ThrowByName(env, "java/io/Exception", "Command line too long"); |
|
505 |
return 0; |
|
506 |
} |
|
507 |
nPos += nCpyLen; |
|
508 |
szCmdLine[nPos] = _T(' '); |
|
509 |
++nPos; |
|
510 |
env->ReleaseStringChars(item, (const jchar *)str); |
|
511 |
} |
|
512 |
} |
|
513 |
||
514 |
szCmdLine[nPos] = _T('\0'); |
|
515 |
||
516 |
// Prepare environment block |
|
517 |
if (nEnvVars > 0) |
|
518 |
{ |
|
519 |
szEnvBlock = (wchar_t *)malloc(nBlkSize * sizeof(wchar_t)); |
|
520 |
nPos = 0; |
|
521 |
for(i = 0; i < nEnvVars; ++i) |
|
522 |
{ |
|
523 |
jstring item = (jstring)env->GetObjectArrayElement(envp, i); |
|
524 |
jsize len = env->GetStringLength(item); |
|
525 |
const wchar_t * str = (const wchar_t *)env->GetStringChars(item, 0); |
|
526 |
if(NULL != str) |
|
527 |
{ |
|
528 |
while((nBlkSize - nPos) <= (len + 2)) // +2 for two '\0' |
|
529 |
{ |
|
530 |
nBlkSize += MAX_ENV_SIZE; |
|
531 |
szEnvBlock = (wchar_t *)realloc(szEnvBlock, nBlkSize * sizeof(wchar_t)); |
|
532 |
if(NULL == szEnvBlock) |
|
533 |
{ |
|
534 |
ThrowByName(env, "java/io/Exception", "Not enough memory"); |
|
535 |
return 0; |
|
536 |
} |
|
537 |
} |
|
538 |
wcsncpy(szEnvBlock + nPos, str, len); |
|
539 |
nPos += len; |
|
540 |
szEnvBlock[nPos] = _T('\0'); |
|
541 |
++nPos; |
|
542 |
env->ReleaseStringChars(item, (const jchar *)str); |
|
543 |
} |
|
544 |
} |
|
545 |
szEnvBlock[nPos] = _T('\0'); |
|
546 |
envBlk = szEnvBlock; |
|
547 |
} |
|
548 |
||
549 |
||
550 |
||
551 |
if (dir != 0) |
|
552 |
{ |
|
553 |
const wchar_t * str = (const wchar_t *)env->GetStringChars(dir, 0); |
|
554 |
if(NULL != str) |
|
555 |
{ |
|
556 |
cwd = wcsdup(str); |
|
557 |
env->ReleaseStringChars(dir, (const jchar *)str); |
|
558 |
} |
|
559 |
} |
|
560 |
||
561 |
||
562 |
ZeroMemory(&si, sizeof(si)); |
|
563 |
si.cb = sizeof(si); |
|
564 |
||
565 |
||
566 |
||
567 |
||
568 |
||
569 |
flags = CREATE_NEW_CONSOLE; |
|
570 |
flags |= CREATE_UNICODE_ENVIRONMENT; |
|
571 |
ret = CreateProcessW(0, /* executable name */ |
|
572 |
szCmdLine, /* command line */ |
|
573 |
0, /* process security attribute */ |
|
574 |
0, /* thread security attribute */ |
|
575 |
TRUE, /* inherits system handles */ |
|
576 |
flags, /* normal attached process */ |
|
577 |
envBlk, /* environment block */ |
|
578 |
cwd, /* change to the new current directory */ |
|
579 |
&si, /* (in) startup information */ |
|
580 |
&pi); /* (out) process information */ |
|
581 |
||
582 |
||
583 |
||
584 |
if(NULL != cwd) |
|
585 |
free(cwd); |
|
586 |
if(NULL != szEnvBlock) |
|
587 |
free(szEnvBlock); |
|
588 |
if(NULL != szCmdLine) |
|
589 |
free(szCmdLine); |
|
590 |
||
591 |
if (!ret) // error |
|
592 |
{ |
|
593 |
char * lpMsgBuf; |
|
594 |
||
595 |
FormatMessage( |
|
596 |
FORMAT_MESSAGE_ALLOCATE_BUFFER | |
|
597 |
FORMAT_MESSAGE_FROM_SYSTEM | |
|
598 |
FORMAT_MESSAGE_IGNORE_INSERTS, |
|
599 |
NULL, |
|
600 |
GetLastError(), |
|
601 |
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language |
|
602 |
(wchar_t *)&lpMsgBuf, |
|
603 |
0, |
|
604 |
NULL |
|
605 |
); |
|
606 |
ThrowByName(env, "java/io/IOException", lpMsgBuf); |
|
607 |
// Free the buffer. |
|
608 |
LocalFree( lpMsgBuf ); |
|
609 |
ret = -1; |
|
610 |
} |
|
611 |
else |
|
612 |
{ |
|
613 |
// Clean-up |
|
614 |
CloseHandle(pi.hThread); |
|
615 |
CloseHandle(pi.hProcess); |
|
616 |
ret = (long)pi.dwProcessId; //hProcess; |
|
617 |
} |
|
618 |
||
619 |
||
620 |
return ret; |
|
621 |
||
622 |
} |
|
623 |
||
624 |
||
625 |
///////////////////////////////////////////////////////////////////////////////////// |
|
626 |
// Emulation of the signal raising |
|
627 |
// Arguments: (see Spawner.java) |
|
628 |
// [in] uid - unique process ID |
|
629 |
// [in] signal - signal to raise |
|
630 |
///////////////////////////////////////////////////////////////////////////////////// |
|
631 |
extern "C" |
|
632 |
JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_spawner_Spawner_raise |
|
633 |
(JNIEnv * env, jobject process, jint uid, jint signal) |
|
634 |
{ |
|
635 |
jint ret = 0; |
|
636 |
||
637 |
HANDLE hProc; |
|
638 |
pProcInfo_t pCurProcInfo = findProcInfo(uid); |
|
639 |
#ifdef DEBUG_MONITOR |
|
640 |
wchar_t buffer[100]; |
|
641 |
#endif |
|
642 |
||
643 |
if(NULL == pCurProcInfo) { |
|
644 |
if(SIG_INT == signal) { // Try another way |
|
645 |
return interruptProcess(uid) ; |
|
646 |
} |
|
647 |
return -1; |
|
648 |
} |
|
649 |
||
650 |
#ifdef DEBUG_MONITOR |
|
651 |
swprintf(buffer, _T("Spawner received signal %i for process %i\n"), signal, pCurProcInfo -> pid); |
|
652 |
OutputDebugStringW(buffer); |
|
653 |
#endif |
|
654 |
||
655 |
hProc = OpenProcess(PROCESS_ALL_ACCESS, 0, pCurProcInfo -> pid); |
|
656 |
||
657 |
if(NULL == hProc) |
|
658 |
return -1; |
|
659 |
||
660 |
switch(signal) |
|
661 |
{ |
|
662 |
case SIG_NOOP: |
|
663 |
// Wait 0 msec -just check if the process has been still running |
|
664 |
ret = ((WAIT_TIMEOUT == WaitForSingleObject(hProc, 0)) ? 0 : -1); |
|
665 |
break; |
|
666 |
case SIG_HUP: |
|
667 |
// Temporary do nothing |
|
668 |
ret = 0; |
|
669 |
break; |
|
51
49c226a8748e
CDT 6.0 from cdt_6_0 branch (eclipse.org cvs repository). This overwrites previous CDT 6.0 merges.
timkelly
parents:
37
diff
changeset
|
670 |
case SIG_KILL: |
37 | 671 |
case SIG_TERM: |
672 |
#ifdef DEBUG_MONITOR |
|
51
49c226a8748e
CDT 6.0 from cdt_6_0 branch (eclipse.org cvs repository). This overwrites previous CDT 6.0 merges.
timkelly
parents:
37
diff
changeset
|
673 |
swprintf(buffer, _T("Spawner received KILL or TERM signal for process %i\n"), |
37 | 674 |
pCurProcInfo -> pid); |
675 |
OutputDebugStringW(buffer); |
|
676 |
#endif |
|
677 |
SetEvent(pCurProcInfo -> eventTerminate); |
|
678 |
#ifdef DEBUG_MONITOR |
|
679 |
OutputDebugStringW(_T("Spawner signalled KILL event\n")); |
|
680 |
#endif |
|
681 |
ret = 0; |
|
682 |
break; |
|
683 |
case SIG_INT: |
|
684 |
ResetEvent(pCurProcInfo -> eventWait); |
|
51
49c226a8748e
CDT 6.0 from cdt_6_0 branch (eclipse.org cvs repository). This overwrites previous CDT 6.0 merges.
timkelly
parents:
37
diff
changeset
|
685 |
PulseEvent(pCurProcInfo -> eventBreak); |
37 | 686 |
ret = (WaitForSingleObject(pCurProcInfo -> eventWait, 100) == WAIT_OBJECT_0); |
687 |
break; |
|
688 |
default: |
|
689 |
break; |
|
690 |
} |
|
691 |
||
692 |
CloseHandle(hProc); |
|
693 |
return ret; |
|
694 |
||
695 |
||
696 |
} |
|
697 |
||
698 |
||
699 |
||
700 |
///////////////////////////////////////////////////////////////////////////////////// |
|
701 |
// Wait for process termination |
|
702 |
// Arguments: (see Spawner.java) |
|
703 |
// [in] uid - unique process ID |
|
704 |
///////////////////////////////////////////////////////////////////////////////////// |
|
705 |
extern "C" |
|
706 |
JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_spawner_Spawner_waitFor |
|
707 |
(JNIEnv * env, jobject process, jint uid) |
|
708 |
{ |
|
709 |
DWORD exit_code; |
|
710 |
int what=0; |
|
711 |
HANDLE hProc; |
|
712 |
pProcInfo_t pCurProcInfo = findProcInfo(uid); |
|
713 |
||
714 |
if(NULL == pCurProcInfo) |
|
715 |
return -1; |
|
716 |
||
717 |
hProc = OpenProcess(PROCESS_ALL_ACCESS, 0, pCurProcInfo -> pid); |
|
718 |
||
719 |
if(NULL == hProc) |
|
720 |
return -1; |
|
721 |
||
722 |
what = WaitForSingleObject(hProc, INFINITE); |
|
723 |
||
724 |
||
725 |
if (what == WAIT_OBJECT_0) |
|
726 |
{ |
|
727 |
GetExitCodeProcess(hProc, &exit_code); |
|
728 |
} |
|
729 |
||
730 |
||
731 |
if(hProc) |
|
732 |
CloseHandle(hProc); |
|
733 |
||
734 |
return exit_code; |
|
735 |
} |
|
736 |
||
737 |
||
738 |
||
739 |
||
740 |
||
741 |
// Utilities |
|
742 |
||
743 |
///////////////////////////////////////////////////////////////////////////////////// |
|
744 |
// Throws Java exception (will be trapped by VM). |
|
745 |
// Arguments: |
|
746 |
// [in] name - name of exception class |
|
747 |
// [in] message to assign thi event |
|
748 |
///////////////////////////////////////////////////////////////////////////////////// |
|
749 |
void ThrowByName(JNIEnv *env, const char *name, const char *msg) |
|
750 |
{ |
|
751 |
jclass cls = env->FindClass(name); |
|
752 |
||
753 |
if (cls != 0) /* Otherwise an exception has already been thrown */ |
|
754 |
env->ThrowNew(cls, msg); |
|
755 |
||
756 |
/* It's a good practice to clean up the local references. */ |
|
757 |
env->DeleteLocalRef(cls); |
|
758 |
} |
|
759 |
||
760 |
||
761 |
||
762 |
||
763 |
///////////////////////////////////////////////////////////////////////////////////// |
|
764 |
// Create process description block. |
|
765 |
// Arguments: no |
|
766 |
// Return : pointer to the process descriptor |
|
767 |
///////////////////////////////////////////////////////////////////////////////////// |
|
768 |
pProcInfo_t createProcInfo() |
|
769 |
{ |
|
770 |
int i; |
|
771 |
pProcInfo_t p = NULL; |
|
772 |
||
773 |
EnterCriticalSection(&cs); |
|
774 |
||
775 |
if(NULL == pInfo) |
|
776 |
{ |
|
777 |
pInfo = (pProcInfo_t)malloc(sizeof(procInfo_t) * MAX_PROCS); |
|
778 |
ZeroMemory(pInfo, sizeof(procInfo_t) * MAX_PROCS); |
|
779 |
} |
|
780 |
||
781 |
for(i = 0; i < MAX_PROCS; ++i) |
|
782 |
{ |
|
783 |
if(pInfo[i].pid == 0) |
|
784 |
{ |
|
785 |
pInfo[i].pid = -1; |
|
786 |
pInfo[i].uid = ++procCounter; |
|
787 |
p = pInfo + i; |
|
788 |
break; |
|
789 |
} |
|
790 |
} |
|
791 |
||
792 |
LeaveCriticalSection(&cs); |
|
793 |
||
794 |
return p; |
|
795 |
} |
|
796 |
||
797 |
///////////////////////////////////////////////////////////////////////////////////// |
|
798 |
// Using unique process ID finds process descriptor |
|
799 |
// Arguments: no |
|
800 |
// Return : pointer to the process descriptor |
|
801 |
///////////////////////////////////////////////////////////////////////////////////// |
|
802 |
pProcInfo_t findProcInfo(int uid) |
|
803 |
{ |
|
804 |
int i; |
|
805 |
pProcInfo_t p = NULL; |
|
806 |
if(NULL == pInfo) |
|
807 |
return NULL; |
|
808 |
||
809 |
for(i = 0; i < MAX_PROCS; ++i) |
|
810 |
{ |
|
811 |
if(pInfo[i].uid == uid) |
|
812 |
{ |
|
813 |
p = pInfo + i; |
|
814 |
break; |
|
815 |
} |
|
816 |
} |
|
817 |
||
818 |
return p; |
|
819 |
} |
|
820 |
||
821 |
///////////////////////////////////////////////////////////////////////////////////// |
|
822 |
// Cleans up vacant process descriptor |
|
823 |
// Arguments: |
|
824 |
// pCurProcInfo - pointer to descriptor to clean up |
|
825 |
// Return : no |
|
826 |
void cleanUpProcBlock(pProcInfo_t pCurProcInfo) |
|
827 |
{ |
|
828 |
if(0 != pCurProcInfo -> eventBreak) |
|
829 |
{ |
|
830 |
CloseHandle(pCurProcInfo -> eventBreak); |
|
831 |
pCurProcInfo -> eventBreak = 0; |
|
832 |
} |
|
833 |
if(0 != pCurProcInfo -> eventWait) |
|
834 |
{ |
|
835 |
CloseHandle(pCurProcInfo -> eventWait); |
|
836 |
pCurProcInfo -> eventWait = 0; |
|
837 |
} |
|
838 |
if(0 != pCurProcInfo -> eventTerminate) |
|
839 |
{ |
|
840 |
CloseHandle(pCurProcInfo -> eventTerminate); |
|
841 |
pCurProcInfo -> eventTerminate = 0; |
|
842 |
} |
|
843 |
||
844 |
pCurProcInfo -> pid = 0; |
|
845 |
} |
|
846 |
||
847 |
///////////////////////////////////////////////////////////////////////////////////// |
|
848 |
// Running in separae thread and waiting for the process termination |
|
849 |
// Arguments: |
|
850 |
// pv - (int)pv is a pid |
|
851 |
// Return : always 0 |
|
852 |
///////////////////////////////////////////////////////////////////////////////////// |
|
853 |
void _cdecl waitProcTermination(void* pv) |
|
854 |
{ |
|
855 |
PROCESS_INFORMATION *pi = (PROCESS_INFORMATION *)pv; |
|
856 |
int i; |
|
857 |
#ifdef DEBUG_MONITOR |
|
858 |
wchar_t buffer[1000]; |
|
859 |
#endif |
|
860 |
||
861 |
// wait for process termination |
|
862 |
WaitForSingleObject(pi->hProcess, INFINITE); |
|
863 |
||
864 |
for(i = 0; i < MAX_PROCS; ++i) |
|
865 |
{ |
|
866 |
if(pInfo[i].pid == pi->dwProcessId) |
|
867 |
{ |
|
868 |
cleanUpProcBlock(pInfo + i); |
|
869 |
#ifdef DEBUG_MONITOR |
|
870 |
swprintf(buffer, _T("waitProcTermination: set PID %i to 0\n"), |
|
871 |
pInfo[i].pid, |
|
872 |
GetLastError()); |
|
873 |
OutputDebugStringW(buffer); |
|
874 |
#endif |
|
875 |
} |
|
876 |
} |
|
877 |
CloseHandle(pi->hProcess); |
|
878 |
||
879 |
free(pi); |
|
880 |
} |
|
881 |
||
882 |
///////////////////////////////////////////////////////////////////////////////////// |
|
883 |
// Use this utility program to process correctly quotation marks in the command line |
|
884 |
// Arguments: |
|
885 |
// target - string to copy to |
|
886 |
// source - string to copy from |
|
887 |
// cpyLength - copy length |
|
888 |
// availSpace - size of the target buffer |
|
889 |
// Return :number of bytes used in target, or -1 in case of error |
|
890 |
///////////////////////////////////////////////////////////////////////////////////// |
|
891 |
int copyTo(wchar_t * target, const wchar_t * source, int cpyLength, int availSpace) |
|
892 |
{ |
|
893 |
BOOL bSlash = FALSE; |
|
894 |
int i = 0, j = 0; |
|
895 |
int totCpyLength = cpyLength; |
|
896 |
||
897 |
#define QUOTATION_DO 0 |
|
898 |
#define QUOTATION_DONE 1 |
|
899 |
#define QUOTATION_NONE 2 |
|
900 |
||
901 |
int nQuotationMode = 0; |
|
902 |
||
903 |
||
904 |
||
905 |
if(availSpace <= cpyLength) // = to reserve space for final '\0' |
|
906 |
return -1; |
|
907 |
||
908 |
if((_T('\"') == *source) && (_T('\"') == *(source + cpyLength - 1))) |
|
909 |
{ |
|
910 |
nQuotationMode = QUOTATION_DONE; |
|
911 |
} |
|
912 |
else |
|
913 |
if(wcschr(source, _T(' ')) == NULL) |
|
914 |
{ |
|
915 |
// No reason to quotate term becase it doesn't have embedded spaces |
|
916 |
nQuotationMode = QUOTATION_NONE; |
|
917 |
} |
|
918 |
else |
|
919 |
{ |
|
920 |
// Needs to be quotated |
|
921 |
nQuotationMode = QUOTATION_DO; |
|
922 |
*target = _T('\"'); |
|
923 |
++j; |
|
924 |
} |
|
925 |
||
926 |
||
927 |
for(; i < cpyLength; ++i, ++j) |
|
928 |
{ |
|
929 |
if(source[i] == _T('\\')) |
|
930 |
bSlash = TRUE; |
|
931 |
else |
|
932 |
{ |
|
933 |
// Don't escape embracing quotation marks |
|
934 |
if((source[i] == _T('\"')) && !((nQuotationMode == QUOTATION_DONE) && ((i == 0) || (i == (cpyLength - 1))) ) ) |
|
935 |
{ |
|
936 |
if(!bSlash) // If still not escaped |
|
937 |
{ |
|
938 |
if(j == availSpace) |
|
939 |
return -1; |
|
940 |
target[j] = _T('\\'); |
|
941 |
++j; |
|
942 |
} |
|
943 |
} |
|
944 |
bSlash = FALSE; |
|
945 |
} |
|
946 |
||
947 |
if(j == availSpace) |
|
948 |
return -1; |
|
949 |
target[j] = source[i]; |
|
950 |
} |
|
951 |
||
952 |
if(nQuotationMode == QUOTATION_DO) |
|
953 |
{ |
|
954 |
if(j == availSpace) |
|
955 |
return -1; |
|
956 |
target[j] = _T('\"'); |
|
957 |
++j; |
|
958 |
} |
|
959 |
||
960 |
return j; |
|
961 |
} |