--- ray/src/common/win_process.c 2003/06/26 00:58:09 3.1 +++ ray/src/common/win_process.c 2016/03/06 01:13:17 3.12 @@ -1,5 +1,5 @@ #ifndef lint -static char RCSid[]="$Id: win_process.c,v 3.1 2003/06/26 00:58:09 schorsch Exp $"; +static char RCSid[]="$Id: win_process.c,v 3.12 2016/03/06 01:13:17 schorsch Exp $"; #endif /* * Routines to communicate with separate process via dual pipes. @@ -16,13 +16,21 @@ static char RCSid[]="$Id: win_process.c,v 3.1 2003/06/ #include /* _open_osfhandle */ #include /* _O_XXX */ -#include "standard.h" +#include "rterror.h" +#include "rtio.h" #include "rtprocess.h" -/* Looks like some Windows versions use negative PIDs. - Let's just hope they either make them *all* negative, or none. */ -static int system_uses_negative_pids = 0; +int +win_nice(int inc) /* simple nice(2) replacement for Windows */ +{ + /* We don't have much granularity available: IDLE_PRIORITY_CLASS + will run whenever no other higher priority process is running */ + if (inc > 0) { + return (int)!SetPriorityClass(GetCurrentProcess(), IDLE_PRIORITY_CLASS); + } + return 0; +} /* @@ -159,8 +167,8 @@ start_process(SUBPROC *proc, char *cmdstr) CloseHandle(hFromChildWrite); hFromChildWrite = NULL; CloseHandle(hToChildRead); hToChildRead = NULL; /* get the file descriptors */ - proc->r = _open_osfhandle((long)hRead, _O_RDONLY); - proc->w = _open_osfhandle((long)hWrite, _O_APPEND); + proc->r = _open_osfhandle((long)hRead, _O_RDONLY|_O_BINARY); + proc->w = _open_osfhandle((long)hWrite, _O_APPEND|_O_BINARY); proc->pid = PInfo.dwProcessId; proc->running = 1; CloseHandle(hCurProc); @@ -177,23 +185,23 @@ error: /* cleanup */ if(hWrite) CloseHandle(hWrite); if(hCurProc) CloseHandle(hCurProc); proc->running = 0; - return 0; + return -1; /* There... Are we happy now? */ } -static int /* copied size or -1 on error */ +static size_t /* copied size or -1 on error */ wordncopy( /* copy (quoted) src to dest. */ char * dest, char * src, -int dlen, +size_t dlen, int insert_space, /* prepend a space */ int force_dq /* turn 'src' into "dest" (for Win command line) */ ) { - int slen; - int pos = 0; + size_t slen; + size_t pos = 0; slen = strlen(src); if (insert_space) { @@ -237,9 +245,9 @@ char *sl[] /* list of arguments */ ) { static char *cmdstr; - static int clen; + static size_t clen; char *newcs; - int newlen, pos, res, i; + size_t newlen, pos, i, res; newlen = strlen(cmdpath) + 3; /* allow two quotes plus the final \0 */ for (i = 0; sl[i] != NULL; i++) { @@ -270,49 +278,65 @@ open_process(SUBPROC *proc, char *av[]) { char *cmdpath; char *cmdstr; - int res; + if (av == NULL || av[0] == NULL) { + fputs("Illegal call to open_process()!\n", stderr); + return -1; + } + proc->pid = 0; proc->running = 0; + if (av == NULL) { return -1; } cmdpath = getpath(av[0], getenv("PATH"), X_OK); - cmdstr = quoted_cmdline(cmdpath, av); + cmdstr = quoted_cmdline(cmdpath, av+1); if (cmdstr == NULL) { return 0; } return start_process(proc, cmdstr); } -int -close_process(SUBPROC *proc) { - int icres, ocres; - DWORD pid; +int win_kill(RT_PID pid, int sig) /* we ignore sig... */ +{ HANDLE hProc; - ocres = close(proc->w); - icres = close(proc->r); - pid = proc->pid; - if(ocres != 0 || icres != 0) { - hProc = OpenProcess(SYNCHRONIZE|PROCESS_TERMINATE, FALSE, pid); - /* something went wrong: enforce infanticide */ - /* other than that, it looks like we want to ignore errors here */ - if (proc->running) { - if(hProc != NULL) { + hProc = OpenProcess(SYNCHRONIZE|PROCESS_TERMINATE, FALSE, pid); + /* it looks like we want to ignore errors here */ + if(hProc != NULL) { #ifdef OBSOLETE_WINDOWS #define KILL_TIMEOUT 10 * 1000 /* milliseconds */ - /* it might have some windows open... */ - EnumWindows((WNDENUMPROC)TerminateAppEnum, (LPARAM)pid); - if(WaitForSingleObject(hProc, KILL_TIMEOUT)!=WAIT_OBJECT_0) { - /* No way to avoid dangling DLLs here. */ - TerminateProcess(hProc, 0); - } + /* it might have some windows open... */ + EnumWindows((WNDENUMPROC)TerminateAppEnum, (LPARAM)pid); + if(WaitForSingleObject(hProc, KILL_TIMEOUT)!=WAIT_OBJECT_0) { + /* No way to avoid dangling DLLs here. */ + TerminateProcess(hProc, 0); + } #else - SafeTerminateProcess(hProc, 0); + SafeTerminateProcess(hProc, 0); #endif - /* WaitForSingleObject(hProc, 0); */ - /* not much use to wait on Windows */ - CloseHandle(hProc); + /* WaitForSingleObject(hProc, 0); */ + /* not much use to wait on Windows */ + CloseHandle(hProc); + } + return 0; /* XXX we need to figure out more here... */ +} + + +int +close_processes(SUBPROC pd[], int nproc) { + int i, icres, ocres; + DWORD pid; + + for (i = 0; i < nproc; i++) { + if (pd[i].running) { + ocres = close(pd[i].w); + icres = close(pd[i].r); + pd[i].running = 0; + if(ocres != 0 || icres != 0) { + /* something went wrong: enforce infanticide */ + /* other than that, it looks like we want to ignore errors here */ + win_kill(pd[i].pid, 0); } } + pd[i].pid = 0; } - proc->running = 0; return 0; /* XXX we need to figure out more here... */ }