ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/src/common/win_process.c
(Generate patch)

Comparing src/common/win_process.c (file contents):
Revision 3.1 by schorsch, Thu Jun 26 00:58:09 2003 UTC vs.
Revision 3.14 by greg, Fri Feb 28 05:18:49 2020 UTC

# Line 16 | Line 16 | static char RCSid[]="$Id$";
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.
24 <   Let's just hope they either make them *all* negative, or none.  */
25 < static int system_uses_negative_pids = 0;
24 > SUBPROC         sp_inactive;    /* zero initialization is fine */
25  
26  
27 + int
28 + win_nice(int inc) /* simple nice(2) replacement for Windows */
29 + {
30 +        /* We don't have much granularity available: IDLE_PRIORITY_CLASS
31 +           will run whenever no other higher priority process is running */
32 +        if (inc > 0) {
33 +                return (int)!SetPriorityClass(GetCurrentProcess(), IDLE_PRIORITY_CLASS);
34 +        }
35 +        return 0;
36 + }
37 +
38 +
39   /*
40      Safely terminate a process by creating a remote thread
41      in the process that calls ExitProcess.
42          As presented by Andrew Tucker in Windows Developer Magazine.
43   */
33 #ifndef OBSOLETE_WINDOWS  /* won't work on Win 9X/ME/CE. */
44   BOOL SafeTerminateProcess(HANDLE hProcess, UINT uExitCode)
45   {
46          DWORD dwTID, dwCode, dwErr = 0;
# Line 70 | Line 80 | BOOL SafeTerminateProcess(HANDLE hProcess, UINT uExitC
80          if ( !bSuccess ) SetLastError(dwErr);
81          return bSuccess;
82   }
73 #endif
83  
84  
85   static int
# Line 159 | Line 168 | start_process(SUBPROC *proc, char *cmdstr)
168          CloseHandle(hFromChildWrite); hFromChildWrite = NULL;
169          CloseHandle(hToChildRead); hToChildRead = NULL;
170          /* get the file descriptors */
171 <        proc->r = _open_osfhandle((long)hRead, _O_RDONLY);
172 <        proc->w = _open_osfhandle((long)hWrite, _O_APPEND);
171 >        proc->r = _open_osfhandle((long)hRead, _O_RDONLY|_O_BINARY);
172 >        proc->w = _open_osfhandle((long)hWrite, _O_APPEND|_O_BINARY);
173          proc->pid = PInfo.dwProcessId;
174 <        proc->running = 1;
174 >        proc->flags = PF_RUNNING;
175          CloseHandle(hCurProc);
176          /* Windows doesn't tell us the actual buffer size */
177          return PIPE_BUF;
# Line 176 | Line 185 | error: /* cleanup */
185          if(hRead) CloseHandle(hRead);
186          if(hWrite) CloseHandle(hWrite);
187          if(hCurProc) CloseHandle(hCurProc);
188 <        proc->running = 0;
189 <        return 0;
188 >        proc->flags = 0;
189 >        return -1;
190          /* There... Are we happy now? */
191   }
192  
193  
194 < static int         /* copied size or -1 on error */
194 > static size_t         /* copied size or -1 on error */
195   wordncopy(         /* copy (quoted) src to dest. */
196  
197   char * dest,
198   char * src,
199 < int dlen,
199 > size_t dlen,
200   int insert_space,  /* prepend a space  */
201   int force_dq       /* turn 'src' into "dest" (for Win command line) */
202   )
203   {
204 <        int slen;
205 <        int pos = 0;
204 >        size_t slen;
205 >        size_t pos = 0;
206  
207          slen = strlen(src);
208          if (insert_space) {
# Line 237 | Line 246 | char *sl[]       /* list of arguments */
246   )
247   {
248          static char *cmdstr;
249 <        static int clen;
249 >        static size_t clen;
250          char *newcs;
251 <        int newlen, pos, res, i;
251 >        size_t newlen, pos, i, res;
252  
253          newlen = strlen(cmdpath) + 3; /* allow two quotes plus the final \0 */
254          for (i = 0; sl[i] != NULL; i++) {
# Line 270 | Line 279 | open_process(SUBPROC *proc, char *av[])
279   {
280          char *cmdpath;
281          char *cmdstr;
273        int res;
282  
283 <        proc->running = 0;
283 >        if (av == NULL || av[0] == NULL || proc->flags) {
284 >                fputs("Illegal call to open_process()!\n", stderr);
285 >                return -1;
286 >        }
287 >        proc->pid = 0;
288 >        if (av == NULL) { return -1; }
289          cmdpath = getpath(av[0], getenv("PATH"), X_OK);
290 <        cmdstr = quoted_cmdline(cmdpath, av);
290 >        cmdstr = quoted_cmdline(cmdpath, av+1);
291          if (cmdstr == NULL) { return 0; }
292          return start_process(proc, cmdstr);
293   }
294  
295  
296 + int win_kill(RT_PID pid, int sig) /* we ignore sig... */
297 + {
298 +        HANDLE hProc;
299 +
300 +        hProc = OpenProcess(SYNCHRONIZE|PROCESS_TERMINATE, FALSE, pid);
301 +        /*  it looks like we want to ignore errors here */
302 +        if(hProc != NULL) {
303 +                SafeTerminateProcess(hProc, 0);
304 +                CloseHandle(hProc);
305 +        }
306 +        return 0; /* XXX we need to figure out more here... */
307 + }
308 +
309 +
310   int
311 < close_process(SUBPROC *proc) {
312 <        int icres, ocres;
311 > close_processes(SUBPROC pd[], int nproc) {
312 >        int i, icres, ocres;
313          DWORD pid;
287        HANDLE hProc;
314  
315 <        ocres = close(proc->w);
316 <        icres = close(proc->r);
317 <        pid = proc->pid;
318 <        if(ocres != 0 || icres != 0) {
319 <                hProc = OpenProcess(SYNCHRONIZE|PROCESS_TERMINATE, FALSE, pid);
320 <                /* something went wrong: enforce infanticide */
321 <                /* other than that, it looks like we want to ignore errors here */
322 <                if (proc->running) {
323 <                        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);
315 >        for (i = 0; i < nproc; i++) {
316 >                if (pd[i].flags & PF_RUNNING) {
317 >                        ocres = close(pd[i].w);
318 >                        icres = close(pd[i].r);
319 >                        pd[i].flags = 0;
320 >                        if(ocres != 0 || icres != 0) {
321 >                                /* something went wrong: enforce infanticide */
322 >                                /* other than that, it looks like we want to ignore errors */
323 >                                win_kill(pd[i].pid, 0);
324                          }
325                  }
326 +                pd[i].pid = 0;
327          }
315        proc->running = 0;
328          return 0; /* XXX we need to figure out more here... */
329   }
330  
# Line 327 | Line 339 | main( int argc, char **argv )
339          char ret[1024];
340          char *command[]= {"word", "gappy word", "\"quoted words\"", "'squoted words'", NULL};
341  
342 <    res = open_process(&proc, command)
342 >        proc.flags = 0;
343 >        proc.pid = 0;
344 >        res = open_process(&proc, command)
345          if (res == 0) {
346                  printf("open_process() failed with return value 0\n");
347                  return -1;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines