| 16 |
|
#include <io.h> /* _open_osfhandle */ |
| 17 |
|
#include <fcntl.h> /* _O_XXX */ |
| 18 |
|
|
| 19 |
< |
#include "standard.h" |
| 19 |
> |
#include "rterror.h" |
| 20 |
> |
#include "rtio.h" |
| 21 |
|
#include "rtprocess.h" |
| 22 |
|
|
| 23 |
|
|
| 24 |
< |
/* Looks like some Windows versions use negative PIDs. |
| 25 |
< |
Let's just hope they either make them *all* negative, or none. */ |
| 26 |
< |
static int system_uses_negative_pids = 0; |
| 24 |
> |
int |
| 25 |
> |
win_nice(int inc) /* simple nice(2) replacement for Windows */ |
| 26 |
> |
{ |
| 27 |
> |
/* We don't have much granularity available: IDLE_PRIORITY_CLASS |
| 28 |
> |
will run whenever no other higher priority process is running */ |
| 29 |
> |
if (inc > 0) { |
| 30 |
> |
return (int)!SetPriorityClass(GetCurrentProcess(), IDLE_PRIORITY_CLASS); |
| 31 |
> |
} |
| 32 |
> |
return 0; |
| 33 |
> |
} |
| 34 |
|
|
| 35 |
|
|
| 36 |
|
/* |
| 38 |
|
in the process that calls ExitProcess. |
| 39 |
|
As presented by Andrew Tucker in Windows Developer Magazine. |
| 40 |
|
*/ |
| 33 |
– |
#ifndef OBSOLETE_WINDOWS /* won't work on Win 9X/ME/CE. */ |
| 41 |
|
BOOL SafeTerminateProcess(HANDLE hProcess, UINT uExitCode) |
| 42 |
|
{ |
| 43 |
|
DWORD dwTID, dwCode, dwErr = 0; |
| 77 |
|
if ( !bSuccess ) SetLastError(dwErr); |
| 78 |
|
return bSuccess; |
| 79 |
|
} |
| 73 |
– |
#endif |
| 80 |
|
|
| 81 |
|
|
| 82 |
|
static int |
| 165 |
|
CloseHandle(hFromChildWrite); hFromChildWrite = NULL; |
| 166 |
|
CloseHandle(hToChildRead); hToChildRead = NULL; |
| 167 |
|
/* get the file descriptors */ |
| 168 |
< |
proc->r = _open_osfhandle((long)hRead, _O_RDONLY); |
| 169 |
< |
proc->w = _open_osfhandle((long)hWrite, _O_APPEND); |
| 168 |
> |
proc->r = _open_osfhandle((long)hRead, _O_RDONLY|_O_BINARY); |
| 169 |
> |
proc->w = _open_osfhandle((long)hWrite, _O_APPEND|_O_BINARY); |
| 170 |
|
proc->pid = PInfo.dwProcessId; |
| 171 |
|
proc->running = 1; |
| 172 |
|
CloseHandle(hCurProc); |
| 183 |
|
if(hWrite) CloseHandle(hWrite); |
| 184 |
|
if(hCurProc) CloseHandle(hCurProc); |
| 185 |
|
proc->running = 0; |
| 186 |
< |
return 0; |
| 186 |
> |
return -1; |
| 187 |
|
/* There... Are we happy now? */ |
| 188 |
|
} |
| 189 |
|
|
| 190 |
|
|
| 191 |
< |
static int /* copied size or -1 on error */ |
| 191 |
> |
static size_t /* copied size or -1 on error */ |
| 192 |
|
wordncopy( /* copy (quoted) src to dest. */ |
| 193 |
|
|
| 194 |
|
char * dest, |
| 195 |
|
char * src, |
| 196 |
< |
int dlen, |
| 196 |
> |
size_t dlen, |
| 197 |
|
int insert_space, /* prepend a space */ |
| 198 |
|
int force_dq /* turn 'src' into "dest" (for Win command line) */ |
| 199 |
|
) |
| 200 |
|
{ |
| 201 |
< |
int slen; |
| 202 |
< |
int pos = 0; |
| 201 |
> |
size_t slen; |
| 202 |
> |
size_t pos = 0; |
| 203 |
|
|
| 204 |
|
slen = strlen(src); |
| 205 |
|
if (insert_space) { |
| 243 |
|
) |
| 244 |
|
{ |
| 245 |
|
static char *cmdstr; |
| 246 |
< |
static int clen; |
| 246 |
> |
static size_t clen; |
| 247 |
|
char *newcs; |
| 248 |
< |
int newlen, pos, res, i; |
| 248 |
> |
size_t newlen, pos, i, res; |
| 249 |
|
|
| 250 |
|
newlen = strlen(cmdpath) + 3; /* allow two quotes plus the final \0 */ |
| 251 |
|
for (i = 0; sl[i] != NULL; i++) { |
| 276 |
|
{ |
| 277 |
|
char *cmdpath; |
| 278 |
|
char *cmdstr; |
| 273 |
– |
int res; |
| 279 |
|
|
| 280 |
+ |
if (av == NULL || av[0] == NULL) { |
| 281 |
+ |
fputs("Illegal call to open_process()!\n", stderr); |
| 282 |
+ |
return -1; |
| 283 |
+ |
} |
| 284 |
+ |
proc->pid = 0; |
| 285 |
|
proc->running = 0; |
| 286 |
+ |
if (av == NULL) { return -1; } |
| 287 |
|
cmdpath = getpath(av[0], getenv("PATH"), X_OK); |
| 288 |
< |
cmdstr = quoted_cmdline(cmdpath, av); |
| 288 |
> |
cmdstr = quoted_cmdline(cmdpath, av+1); |
| 289 |
|
if (cmdstr == NULL) { return 0; } |
| 290 |
|
return start_process(proc, cmdstr); |
| 291 |
|
} |
| 292 |
|
|
| 293 |
|
|
| 294 |
+ |
int win_kill(RT_PID pid, int sig) /* we ignore sig... */ |
| 295 |
+ |
{ |
| 296 |
+ |
HANDLE hProc; |
| 297 |
+ |
|
| 298 |
+ |
hProc = OpenProcess(SYNCHRONIZE|PROCESS_TERMINATE, FALSE, pid); |
| 299 |
+ |
/* it looks like we want to ignore errors here */ |
| 300 |
+ |
if(hProc != NULL) { |
| 301 |
+ |
SafeTerminateProcess(hProc, 0); |
| 302 |
+ |
CloseHandle(hProc); |
| 303 |
+ |
} |
| 304 |
+ |
return 0; /* XXX we need to figure out more here... */ |
| 305 |
+ |
} |
| 306 |
+ |
|
| 307 |
+ |
|
| 308 |
|
int |
| 309 |
< |
close_process(SUBPROC *proc) { |
| 310 |
< |
int icres, ocres; |
| 309 |
> |
close_processes(SUBPROC pd[], int nproc) { |
| 310 |
> |
int i, icres, ocres; |
| 311 |
|
DWORD pid; |
| 287 |
– |
HANDLE hProc; |
| 312 |
|
|
| 313 |
< |
ocres = close(proc->w); |
| 314 |
< |
icres = close(proc->r); |
| 315 |
< |
pid = proc->pid; |
| 316 |
< |
if(ocres != 0 || icres != 0) { |
| 317 |
< |
hProc = OpenProcess(SYNCHRONIZE|PROCESS_TERMINATE, FALSE, pid); |
| 318 |
< |
/* something went wrong: enforce infanticide */ |
| 319 |
< |
/* other than that, it looks like we want to ignore errors here */ |
| 320 |
< |
if (proc->running) { |
| 321 |
< |
if(hProc != NULL) { |
| 298 |
< |
#ifdef OBSOLETE_WINDOWS |
| 299 |
< |
#define KILL_TIMEOUT 10 * 1000 /* milliseconds */ |
| 300 |
< |
/* it might have some windows open... */ |
| 301 |
< |
EnumWindows((WNDENUMPROC)TerminateAppEnum, (LPARAM)pid); |
| 302 |
< |
if(WaitForSingleObject(hProc, KILL_TIMEOUT)!=WAIT_OBJECT_0) { |
| 303 |
< |
/* No way to avoid dangling DLLs here. */ |
| 304 |
< |
TerminateProcess(hProc, 0); |
| 305 |
< |
} |
| 306 |
< |
#else |
| 307 |
< |
SafeTerminateProcess(hProc, 0); |
| 308 |
< |
#endif |
| 309 |
< |
/* WaitForSingleObject(hProc, 0); */ |
| 310 |
< |
/* not much use to wait on Windows */ |
| 311 |
< |
CloseHandle(hProc); |
| 313 |
> |
for (i = 0; i < nproc; i++) { |
| 314 |
> |
if (pd[i].running) { |
| 315 |
> |
ocres = close(pd[i].w); |
| 316 |
> |
icres = close(pd[i].r); |
| 317 |
> |
pd[i].running = 0; |
| 318 |
> |
if(ocres != 0 || icres != 0) { |
| 319 |
> |
/* something went wrong: enforce infanticide */ |
| 320 |
> |
/* other than that, it looks like we want to ignore errors */ |
| 321 |
> |
win_kill(pd[i].pid, 0); |
| 322 |
|
} |
| 323 |
|
} |
| 324 |
+ |
pd[i].pid = 0; |
| 325 |
|
} |
| 315 |
– |
proc->running = 0; |
| 326 |
|
return 0; /* XXX we need to figure out more here... */ |
| 327 |
|
} |
| 328 |
|
|