X-Git-Url: http://winboard.nl/cgi-bin?p=polyglot.git;a=blobdiff_plain;f=pipex_win32.c;h=41431b3a657dcc0a1f5ee7cefc13d44549dc2ea6;hp=24199b71e086b3e5887436044f704f82def6d5b1;hb=HEAD;hpb=1087eb7b2d8447adf9a7deb549d4004a87b46b10 diff --git a/pipex_win32.c b/pipex_win32.c index 24199b7..41431b3 100644 --- a/pipex_win32.c +++ b/pipex_win32.c @@ -72,6 +72,8 @@ void pipex_open(pipex_t *pipex, char *szCurrentDir; pipex->state=0; pipex->name=szName; + pipex->command=szProcFile; + pipex->quit_pending=FALSE; pipex->hProcess=NULL; if (szProcFile == NULL) { pipex->hInput = GetStdHandle(STD_INPUT_HANDLE); @@ -92,14 +94,15 @@ void pipex_open(pipex_t *pipex, si.hStdInput = hStdinRead; si.hStdOutput = hStdoutWrite; si.hStdError = hStdoutWrite; - if((szCurrentDir = _getcwd( NULL, 0 )) == NULL ) + if((szCurrentDir = (char*)_getcwd( NULL, 0 )) == NULL ) my_fatal("pipex_open(): no current directory: %s\n", strerror(errno)); if(_chdir(szWorkingDir)){ - my_fatal("pipex_open(): cannot change directory: %s\n", + my_fatal("pipex_open(): %s: %s\n", + szWorkingDir, strerror(errno)); } - if(CreateProcess(NULL, + if(CreateProcess(NULL, (LPSTR) szProcFile, NULL, NULL, @@ -118,7 +121,7 @@ void pipex_open(pipex_t *pipex, pipex->bConsole = FALSE; pipex->bPipe=TRUE; }else{ - my_fatal("pipex_open(): %s",win32_error()); + my_fatal("pipex_open(): %s: %s",szProcFile,win32_error()); } _chdir(szCurrentDir); } @@ -190,17 +193,47 @@ void pipex_send_eof(pipex_t *pipex) { // pipex_exit() -void pipex_exit(pipex_t *pipex) { +/* This routine waits for kill_timeout milliseconds for + * the process to exit by itself. If that doesn't + * happen it will kill the process. + */ + +void pipex_exit(pipex_t *pipex, int kill_timeout) { + DWORD lpexit; + int elapsed_time; + bool exited; CloseHandle(pipex->hInput); CloseHandle(pipex->hOutput); - DWORD lpexit; - - if(GetExitCodeProcess(pipex->hProcess,&lpexit)){ - if(lpexit==STILL_ACTIVE) - //must be java,hammer it down! - TerminateProcess(pipex->hProcess,lpexit); + // ExitProcess(pipex->hProcess,0); + + my_log("POLYGLOT Waiting for child process to exit.\n"); + elapsed_time=0; + exited=FALSE; + + while(elapsed_timehProcess,&lpexit); + if(lpexit==STILL_ACTIVE){ + my_log("POLYGLOT Child has not exited yet. Sleeping %dms.\n", WAIT_GRANULARITY); + my_sleep(WAIT_GRANULARITY); + elapsed_time+=WAIT_GRANULARITY; + }else{ + exited=TRUE; + break; + } + } + + if(!exited){ + my_log("POLYGLOT Child wouldn't exit by itself. Terminating it.\n"); + TerminateProcess(pipex->hProcess,lpexit); } - CloseHandle(pipex->hProcess); + CloseHandle(pipex->hProcess); + + if(!pipex->quit_pending){ + // suppress further errors + pipex->quit_pending=TRUE; + my_fatal("pipex_exit(): %s: child exited unexpectedly.\n",pipex->command); + } + } // pipex_eof_input() @@ -429,12 +462,13 @@ void pipex_set_priority(pipex_t *pipex, int value){ // pipex_set_affinit() +typedef void (WINAPI *SPAM)(HANDLE, int); void pipex_set_affinity(pipex_t *pipex, int value){ + SPAM pSPAM; + if(pipex->hProcess) return; if(value==-1) return; - typedef void (WINAPI *SPAM)(HANDLE, int); - SPAM pSPAM; pSPAM = (SPAM) GetProcAddress( GetModuleHandle(TEXT("kernel32.dll")), "SetProcessAffinityMask");