1 /* ----------------------------------------------------------------------------
2 (c) The University of Glasgow 2004
4 Support for System.Process
5 ------------------------------------------------------------------------- */
9 #if defined(_MSC_VER) || defined(__MINGW32__) || defined(_WIN32)
26 #if !(defined(_MSC_VER) || defined(__MINGW32__) || defined(_WIN32))
27 /* ----------------------------------------------------------------------------
29 ------------------------------------------------------------------------- */
32 runProcess (char *const args[], char *workingDirectory, char **environment,
33 int fdStdInput, int fdStdOutput, int fdStdError,
34 int set_inthandler, long inthandler,
35 int set_quithandler, long quithandler)
47 pPrPr_disableITimers();
49 if (workingDirectory) {
50 if (chdir (workingDirectory) < 0) {
55 /* Set the SIGINT/SIGQUIT signal handlers in the child, if requested
57 (void)sigemptyset(&dfl.sa_mask);
60 dfl.sa_handler = (void *)inthandler;
61 (void)sigaction(SIGINT, &dfl, NULL);
63 if (set_quithandler) {
64 dfl.sa_handler = (void *)quithandler;
65 (void)sigaction(SIGQUIT, &dfl, NULL);
68 dup2 (fdStdInput, STDIN_FILENO);
69 dup2 (fdStdOutput, STDOUT_FILENO);
70 dup2 (fdStdError, STDERR_FILENO);
73 execvpe(args[0], args, environment);
75 execvp(args[0], args);
85 runInteractiveProcess (char *const args[],
86 char *workingDirectory, char **environment,
87 int *pfdStdInput, int *pfdStdOutput, int *pfdStdError)
90 int fdStdInput[2], fdStdOutput[2], fdStdError[2];
100 close(fdStdInput[1]);
101 close(fdStdOutput[0]);
102 close(fdStdOutput[1]);
103 close(fdStdError[0]);
104 close(fdStdError[1]);
109 pPrPr_disableITimers();
111 if (workingDirectory) {
112 if (chdir (workingDirectory) < 0) {
117 if (fdStdInput[0] != STDIN_FILENO) {
118 dup2 (fdStdInput[0], STDIN_FILENO);
119 close(fdStdInput[0]);
122 if (fdStdOutput[1] != STDOUT_FILENO) {
123 dup2 (fdStdOutput[1], STDOUT_FILENO);
124 close(fdStdOutput[1]);
127 if (fdStdError[1] != STDERR_FILENO) {
128 dup2 (fdStdError[1], STDERR_FILENO);
129 close(fdStdError[1]);
132 close(fdStdInput[1]);
133 close(fdStdOutput[0]);
134 close(fdStdError[0]);
138 execvpe(args[0], args, environment);
140 execvp(args[0], args);
146 close(fdStdInput[0]);
147 close(fdStdOutput[1]);
148 close(fdStdError[1]);
150 *pfdStdInput = fdStdInput[1];
151 *pfdStdOutput = fdStdOutput[0];
152 *pfdStdError = fdStdError[0];
160 terminateProcess (ProcHandle handle)
162 return (kill(handle, SIGTERM) == 0);
166 getProcessExitCode (ProcHandle handle, int *pExitCode)
172 if ((res = waitpid(handle, &wstat, WNOHANG)) > 0)
174 if (WIFEXITED(wstat))
176 *pExitCode = WEXITSTATUS(wstat);
180 if (WIFSIGNALED(wstat))
187 /* This should never happen */
191 if (res == 0) return 0;
202 int waitForProcess (ProcHandle handle)
206 while (waitpid(handle, &wstat, 0) < 0)
214 if (WIFEXITED(wstat))
215 return WEXITSTATUS(wstat);
217 if (WIFSIGNALED(wstat))
223 /* This should never happen */
230 /* ----------------------------------------------------------------------------
232 ------------------------------------------------------------------------- */
234 /* -------------------- WINDOWS VERSION --------------------- */
237 * Function: mkAnonPipe
239 * Purpose: create an anonymous pipe with read and write ends being
240 * optionally (non-)inheritable.
243 mkAnonPipe (HANDLE* pHandleIn, BOOL isInheritableIn,
244 HANDLE* pHandleOut, BOOL isInheritableOut)
246 HANDLE hTemporaryIn = NULL;
247 HANDLE hTemporaryOut = NULL;
249 SECURITY_ATTRIBUTES sec_attrs;
251 /* Create inheritable security attributes */
252 sec_attrs.nLength = sizeof(SECURITY_ATTRIBUTES);
253 sec_attrs.lpSecurityDescriptor = NULL;
254 sec_attrs.bInheritHandle = TRUE;
256 /* Create the anon pipe with both ends inheritable */
257 if (!CreatePipe(&hTemporaryIn, &hTemporaryOut, &sec_attrs, 0))
266 *pHandleIn = hTemporaryIn;
269 /* Make the read end non-inheritable */
270 status = DuplicateHandle(GetCurrentProcess(), hTemporaryIn,
271 GetCurrentProcess(), pHandleIn,
273 FALSE, /* non-inheritable */
274 DUPLICATE_SAME_ACCESS);
275 CloseHandle(hTemporaryIn);
281 CloseHandle(hTemporaryOut);
286 if (isInheritableOut)
287 *pHandleOut = hTemporaryOut;
290 /* Make the write end non-inheritable */
291 status = DuplicateHandle(GetCurrentProcess(), hTemporaryOut,
292 GetCurrentProcess(), pHandleOut,
294 FALSE, /* non-inheritable */
295 DUPLICATE_SAME_ACCESS);
296 CloseHandle(hTemporaryOut);
302 CloseHandle(*pHandleIn);
311 runProcess (char *cmd, char *workingDirectory, void *environment,
312 int fdStdInput, int fdStdOutput, int fdStdError)
315 PROCESS_INFORMATION pInfo;
318 ZeroMemory(&sInfo, sizeof(sInfo));
319 sInfo.cb = sizeof(sInfo);
320 sInfo.hStdInput = (HANDLE) _get_osfhandle(fdStdInput);
321 sInfo.hStdOutput= (HANDLE) _get_osfhandle(fdStdOutput);
322 sInfo.hStdError = (HANDLE) _get_osfhandle(fdStdError);
324 if (sInfo.hStdInput == INVALID_HANDLE_VALUE)
325 sInfo.hStdInput = NULL;
326 if (sInfo.hStdOutput == INVALID_HANDLE_VALUE)
327 sInfo.hStdOutput = NULL;
328 if (sInfo.hStdError == INVALID_HANDLE_VALUE)
329 sInfo.hStdError = NULL;
331 if (sInfo.hStdInput || sInfo.hStdOutput || sInfo.hStdError)
332 sInfo.dwFlags = STARTF_USESTDHANDLES;
334 if (sInfo.hStdInput != GetStdHandle(STD_INPUT_HANDLE) &&
335 sInfo.hStdOutput != GetStdHandle(STD_OUTPUT_HANDLE) &&
336 sInfo.hStdError != GetStdHandle(STD_ERROR_HANDLE))
337 flags = CREATE_NO_WINDOW; // Run without console window only when both output and error are redirected
341 if (!CreateProcess(NULL, cmd, NULL, NULL, TRUE, flags, environment, workingDirectory, &sInfo, &pInfo))
347 CloseHandle(pInfo.hThread);
348 return (ProcHandle)pInfo.hProcess;
352 runInteractiveProcess (char *cmd, char *workingDirectory, void *environment,
353 int *pfdStdInput, int *pfdStdOutput, int *pfdStdError)
356 PROCESS_INFORMATION pInfo;
357 HANDLE hStdInputRead, hStdInputWrite;
358 HANDLE hStdOutputRead, hStdOutputWrite;
359 HANDLE hStdErrorRead, hStdErrorWrite;
361 if (!mkAnonPipe(&hStdInputRead, TRUE, &hStdInputWrite, FALSE))
364 if (!mkAnonPipe(&hStdOutputRead, FALSE, &hStdOutputWrite, TRUE))
366 CloseHandle(hStdInputRead);
367 CloseHandle(hStdInputWrite);
371 if (!mkAnonPipe(&hStdErrorRead, FALSE, &hStdErrorWrite, TRUE))
373 CloseHandle(hStdInputRead);
374 CloseHandle(hStdInputWrite);
375 CloseHandle(hStdOutputRead);
376 CloseHandle(hStdOutputWrite);
380 ZeroMemory(&sInfo, sizeof(sInfo));
381 sInfo.cb = sizeof(sInfo);
382 sInfo.dwFlags = STARTF_USESTDHANDLES;
383 sInfo.hStdInput = hStdInputRead;
384 sInfo.hStdOutput= hStdOutputWrite;
385 sInfo.hStdError = hStdErrorWrite;
387 if (!CreateProcess(NULL, cmd, NULL, NULL, TRUE, CREATE_NO_WINDOW, environment, workingDirectory, &sInfo, &pInfo))
390 CloseHandle(hStdInputRead);
391 CloseHandle(hStdInputWrite);
392 CloseHandle(hStdOutputRead);
393 CloseHandle(hStdOutputWrite);
394 CloseHandle(hStdErrorRead);
395 CloseHandle(hStdErrorWrite);
398 CloseHandle(pInfo.hThread);
400 // Close the ends of the pipes that were inherited by the
401 // child process. This is important, otherwise we won't see
402 // EOF on these pipes when the child process exits.
403 CloseHandle(hStdInputRead);
404 CloseHandle(hStdOutputWrite);
405 CloseHandle(hStdErrorWrite);
407 *pfdStdInput = _open_osfhandle((intptr_t) hStdInputWrite, _O_WRONLY);
408 *pfdStdOutput = _open_osfhandle((intptr_t) hStdOutputRead, _O_RDONLY);
409 *pfdStdError = _open_osfhandle((intptr_t) hStdErrorRead, _O_RDONLY);
411 return (int) pInfo.hProcess;
415 terminateProcess (ProcHandle handle)
417 if (!TerminateProcess((HANDLE) handle, 1)) {
425 getProcessExitCode (ProcHandle handle, int *pExitCode)
429 if (WaitForSingleObject((HANDLE) handle, 1) == WAIT_OBJECT_0)
431 if (GetExitCodeProcess((HANDLE) handle, (DWORD *) pExitCode) == 0)
443 waitForProcess (ProcHandle handle)
447 if (WaitForSingleObject((HANDLE) handle, INFINITE) == WAIT_OBJECT_0)
449 if (GetExitCodeProcess((HANDLE) handle, &retCode) == 0)