Lines 58-64
Link Here
|
58 |
pProcInfo_t findProcInfo(int pid); |
58 |
pProcInfo_t findProcInfo(int pid); |
59 |
|
59 |
|
60 |
// We launch separate thread for each project to trap it termination |
60 |
// We launch separate thread for each project to trap it termination |
61 |
unsigned int _stdcall waitProcTermination(void* pv) ; |
61 |
void _cdecl waitProcTermination(void* pv) ; |
62 |
|
62 |
|
63 |
// This is a helper function to prevent losing of quotatin marks |
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); |
64 |
static int copyTo(wchar_t * target, const wchar_t * source, int cpyLenght, int availSpace); |
Lines 127-133
Link Here
|
127 |
(JNIEnv * env, jobject process, jobjectArray cmdarray, jobjectArray envp, jstring dir, jintArray channels) |
127 |
(JNIEnv * env, jobject process, jobjectArray cmdarray, jobjectArray envp, jstring dir, jintArray channels) |
128 |
{ |
128 |
{ |
129 |
HANDLE stdHandles[3]; |
129 |
HANDLE stdHandles[3]; |
130 |
PROCESS_INFORMATION pi = {0}; |
130 |
PROCESS_INFORMATION pi = {0}, *piCopy; |
131 |
STARTUPINFOW si; |
131 |
STARTUPINFOW si; |
132 |
DWORD flags = 0; |
132 |
DWORD flags = 0; |
133 |
const wchar_t * cwd = NULL; |
133 |
const wchar_t * cwd = NULL; |
Lines 143-149
Link Here
|
143 |
DWORD pid = GetCurrentProcessId(); |
143 |
DWORD pid = GetCurrentProcessId(); |
144 |
int nPos; |
144 |
int nPos; |
145 |
pProcInfo_t pCurProcInfo; |
145 |
pProcInfo_t pCurProcInfo; |
146 |
DWORD dwThreadId; |
|
|
147 |
wchar_t eventBreakName[20]; |
146 |
wchar_t eventBreakName[20]; |
148 |
wchar_t eventWaitName[20]; |
147 |
wchar_t eventWaitName[20]; |
149 |
wchar_t eventTerminateName[20]; |
148 |
wchar_t eventTerminateName[20]; |
Lines 392-402
Link Here
|
392 |
|
391 |
|
393 |
pCurProcInfo -> pid = pi.dwProcessId; |
392 |
pCurProcInfo -> pid = pi.dwProcessId; |
394 |
h[0] = pCurProcInfo -> eventWait; |
393 |
h[0] = pCurProcInfo -> eventWait; |
395 |
h[1] = (HANDLE)_beginthreadex(NULL, 0, waitProcTermination, |
394 |
h[1] = pi.hProcess; |
396 |
(void *) pi.dwProcessId, 0, (UINT*) &dwThreadId); |
|
|
397 |
|
395 |
|
398 |
what = WaitForMultipleObjects(2, h, FALSE, INFINITE); |
396 |
what = WaitForMultipleObjects(2, h, FALSE, INFINITE); |
399 |
if((what != WAIT_OBJECT_0) && (pCurProcInfo -> pid > 0)) // CreateProcess failed |
397 |
if(what != WAIT_OBJECT_0) // CreateProcess failed |
400 |
{ |
398 |
{ |
401 |
#ifdef DEBUG_MONITOR |
399 |
#ifdef DEBUG_MONITOR |
402 |
swprintf(buffer, _T("Process %i failed\n"), pi.dwProcessId); |
400 |
swprintf(buffer, _T("Process %i failed\n"), pi.dwProcessId); |
Lines 417-433
Link Here
|
417 |
file_handles[1] = (int)stdHandles[1]; |
415 |
file_handles[1] = (int)stdHandles[1]; |
418 |
file_handles[2] = (int)stdHandles[2]; |
416 |
file_handles[2] = (int)stdHandles[2]; |
419 |
env->SetIntArrayRegion(channels, 0, 3, (jint *)file_handles); |
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 |
|
420 |
#ifdef DEBUG_MONITOR |
425 |
#ifdef DEBUG_MONITOR |
421 |
OutputDebugStringW(_T("Process started\n")); |
426 |
OutputDebugStringW(_T("Process started\n")); |
422 |
#endif |
427 |
#endif |
423 |
} |
428 |
} |
424 |
CloseHandle(h[1]); |
|
|
425 |
LeaveCriticalSection(&cs); |
429 |
LeaveCriticalSection(&cs); |
426 |
|
430 |
|
427 |
} |
431 |
} |
428 |
|
432 |
|
429 |
CloseHandle(pi.hThread); |
433 |
CloseHandle(pi.hThread); |
430 |
CloseHandle(pi.hProcess); |
|
|
431 |
|
434 |
|
432 |
return ret; |
435 |
return ret; |
433 |
|
436 |
|
Lines 847-902
Link Here
|
847 |
// pv - (int)pv is a pid |
850 |
// pv - (int)pv is a pid |
848 |
// Return : always 0 |
851 |
// Return : always 0 |
849 |
///////////////////////////////////////////////////////////////////////////////////// |
852 |
///////////////////////////////////////////////////////////////////////////////////// |
850 |
unsigned int _stdcall waitProcTermination(void* pv) |
853 |
void _cdecl waitProcTermination(void* pv) |
851 |
{ |
854 |
{ |
|
|
855 |
PROCESS_INFORMATION *pi = (PROCESS_INFORMATION *)pv; |
852 |
int i; |
856 |
int i; |
853 |
int pid = (int)pv; |
|
|
854 |
#ifdef DEBUG_MONITOR |
857 |
#ifdef DEBUG_MONITOR |
855 |
wchar_t buffer[1000]; |
858 |
wchar_t buffer[1000]; |
856 |
#endif |
859 |
#endif |
857 |
|
860 |
|
858 |
HANDLE hProc = OpenProcess(PROCESS_ALL_ACCESS, 0, pid); |
861 |
// wait for process termination |
859 |
|
862 |
WaitForSingleObject(pi->hProcess, INFINITE); |
860 |
if(NULL == hProc) |
863 |
|
861 |
{ |
|
|
862 |
#ifdef DEBUG_MONITOR |
863 |
swprintf(buffer, _T("waitProcTermination: cannot get handler for PID %i (error %i)\n"), |
864 |
pid, |
865 |
GetLastError()); |
866 |
OutputDebugStringW(buffer); |
867 |
#endif |
868 |
} |
869 |
else |
870 |
{ |
871 |
WaitForSingleObject(hProc, INFINITE); |
872 |
#ifdef DEBUG_MONITOR |
873 |
swprintf(buffer, _T("Process PID %i terminated\n"), pid); |
874 |
OutputDebugStringW(buffer); |
875 |
#endif |
876 |
} |
877 |
|
878 |
for(i = 0; i < MAX_PROCS; ++i) |
864 |
for(i = 0; i < MAX_PROCS; ++i) |
|
|
865 |
{ |
866 |
if(pInfo[i].pid == pi->dwProcessId) |
879 |
{ |
867 |
{ |
880 |
if(pInfo[i].pid == pid) |
868 |
cleanUpProcBlock(pInfo + i); |
881 |
{ |
|
|
882 |
if(WaitForSingleObject(pInfo[i].eventWait, 1) == WAIT_OBJECT_0) // Correct finish |
883 |
{ |
884 |
#ifdef DEBUG_MONITOR |
869 |
#ifdef DEBUG_MONITOR |
885 |
swprintf(buffer, _T("waitProcTermination: set PID %i to 0\n"), |
870 |
swprintf(buffer, _T("waitProcTermination: set PID %i to 0\n"), |
886 |
pid, |
871 |
pid, |
887 |
GetLastError()); |
872 |
GetLastError()); |
888 |
OutputDebugStringW(buffer); |
873 |
OutputDebugStringW(buffer); |
889 |
#endif |
874 |
#endif |
890 |
cleanUpProcBlock(pInfo + i); |
|
|
891 |
} |
892 |
break; |
893 |
} // Otherwise failed because was not started |
894 |
} |
875 |
} |
|
|
876 |
} |
877 |
CloseHandle(pi->hProcess); |
895 |
|
878 |
|
896 |
CloseHandle(hProc); |
879 |
free(pi); |
897 |
|
|
|
898 |
|
899 |
return 0; |
900 |
} |
880 |
} |
901 |
|
881 |
|
902 |
///////////////////////////////////////////////////////////////////////////////////// |
882 |
///////////////////////////////////////////////////////////////////////////////////// |