1 /* ----------------------------------------------------------------------------
2 (c) The University of Glasgow 2004
4 Support for System.Process
5 ------------------------------------------------------------------------- */
9 #if defined(mingw32_HOST_OS)
26 #if !defined(mingw32_HOST_OS) && !defined(__MINGW32__)
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 chdir (workingDirectory);
53 /* Set the SIGINT/SIGQUIT signal handlers in the child, if requested
55 (void)sigemptyset(&dfl.sa_mask);
58 dfl.sa_handler = (void *)inthandler;
59 (void)sigaction(SIGINT, &dfl, NULL);
61 if (set_quithandler) {
62 dfl.sa_handler = (void *)quithandler;
63 (void)sigaction(SIGQUIT, &dfl, NULL);
66 dup2 (fdStdInput, STDIN_FILENO);
67 dup2 (fdStdOutput, STDOUT_FILENO);
68 dup2 (fdStdError, STDERR_FILENO);
71 execvpe(args[0], args, environment);
73 execvp(args[0], args);
83 runInteractiveProcess (char *const args[],
84 char *workingDirectory, char **environment,
85 int *pfdStdInput, int *pfdStdOutput, int *pfdStdError)
88 int fdStdInput[2], fdStdOutput[2], fdStdError[2];
99 close(fdStdOutput[0]);
100 close(fdStdOutput[1]);
101 close(fdStdError[0]);
102 close(fdStdError[1]);
107 pPrPr_disableITimers();
109 if (workingDirectory) {
110 chdir (workingDirectory);
113 dup2 (fdStdInput[0], STDIN_FILENO);
114 dup2 (fdStdOutput[1], STDOUT_FILENO);
115 dup2 (fdStdError[1], STDERR_FILENO);
117 close(fdStdInput[0]);
118 close(fdStdInput[1]);
119 close(fdStdOutput[0]);
120 close(fdStdOutput[1]);
121 close(fdStdError[0]);
122 close(fdStdError[1]);
126 execvpe(args[0], args, environment);
128 execvp(args[0], args);
134 close(fdStdInput[0]);
135 close(fdStdOutput[1]);
136 close(fdStdError[1]);
138 *pfdStdInput = fdStdInput[1];
139 *pfdStdOutput = fdStdOutput[0];
140 *pfdStdError = fdStdError[0];
148 terminateProcess (ProcHandle handle)
150 return (kill(handle, SIGTERM) == 0);
154 getProcessExitCode (ProcHandle handle, int *pExitCode)
160 if ((res = waitpid(handle, &wstat, WNOHANG)) > 0)
162 if (WIFEXITED(wstat))
164 *pExitCode = WEXITSTATUS(wstat);
168 if (WIFSIGNALED(wstat))
175 /* This should never happen */
179 if (res == 0) return 0;
190 int waitForProcess (ProcHandle handle)
194 while (waitpid(handle, &wstat, 0) < 0)
202 if (WIFEXITED(wstat))
203 return WEXITSTATUS(wstat);
205 if (WIFSIGNALED(wstat))
211 /* This should never happen */
218 /* ----------------------------------------------------------------------------
220 ------------------------------------------------------------------------- */
222 /* -------------------- WINDOWS VERSION --------------------- */
224 /* This is the error table that defines the mapping between OS error
225 codes and errno values */
228 unsigned long oscode; /* OS return value */
229 int errnocode; /* System V error code */
232 static struct errentry errtable[] = {
233 { ERROR_INVALID_FUNCTION, EINVAL }, /* 1 */
234 { ERROR_FILE_NOT_FOUND, ENOENT }, /* 2 */
235 { ERROR_PATH_NOT_FOUND, ENOENT }, /* 3 */
236 { ERROR_TOO_MANY_OPEN_FILES, EMFILE }, /* 4 */
237 { ERROR_ACCESS_DENIED, EACCES }, /* 5 */
238 { ERROR_INVALID_HANDLE, EBADF }, /* 6 */
239 { ERROR_ARENA_TRASHED, ENOMEM }, /* 7 */
240 { ERROR_NOT_ENOUGH_MEMORY, ENOMEM }, /* 8 */
241 { ERROR_INVALID_BLOCK, ENOMEM }, /* 9 */
242 { ERROR_BAD_ENVIRONMENT, E2BIG }, /* 10 */
243 { ERROR_BAD_FORMAT, ENOEXEC }, /* 11 */
244 { ERROR_INVALID_ACCESS, EINVAL }, /* 12 */
245 { ERROR_INVALID_DATA, EINVAL }, /* 13 */
246 { ERROR_INVALID_DRIVE, ENOENT }, /* 15 */
247 { ERROR_CURRENT_DIRECTORY, EACCES }, /* 16 */
248 { ERROR_NOT_SAME_DEVICE, EXDEV }, /* 17 */
249 { ERROR_NO_MORE_FILES, ENOENT }, /* 18 */
250 { ERROR_LOCK_VIOLATION, EACCES }, /* 33 */
251 { ERROR_BAD_NETPATH, ENOENT }, /* 53 */
252 { ERROR_NETWORK_ACCESS_DENIED, EACCES }, /* 65 */
253 { ERROR_BAD_NET_NAME, ENOENT }, /* 67 */
254 { ERROR_FILE_EXISTS, EEXIST }, /* 80 */
255 { ERROR_CANNOT_MAKE, EACCES }, /* 82 */
256 { ERROR_FAIL_I24, EACCES }, /* 83 */
257 { ERROR_INVALID_PARAMETER, EINVAL }, /* 87 */
258 { ERROR_NO_PROC_SLOTS, EAGAIN }, /* 89 */
259 { ERROR_DRIVE_LOCKED, EACCES }, /* 108 */
260 { ERROR_BROKEN_PIPE, EPIPE }, /* 109 */
261 { ERROR_DISK_FULL, ENOSPC }, /* 112 */
262 { ERROR_INVALID_TARGET_HANDLE, EBADF }, /* 114 */
263 { ERROR_INVALID_HANDLE, EINVAL }, /* 124 */
264 { ERROR_WAIT_NO_CHILDREN, ECHILD }, /* 128 */
265 { ERROR_CHILD_NOT_COMPLETE, ECHILD }, /* 129 */
266 { ERROR_DIRECT_ACCESS_HANDLE, EBADF }, /* 130 */
267 { ERROR_NEGATIVE_SEEK, EINVAL }, /* 131 */
268 { ERROR_SEEK_ON_DEVICE, EACCES }, /* 132 */
269 { ERROR_DIR_NOT_EMPTY, ENOTEMPTY }, /* 145 */
270 { ERROR_NOT_LOCKED, EACCES }, /* 158 */
271 { ERROR_BAD_PATHNAME, ENOENT }, /* 161 */
272 { ERROR_MAX_THRDS_REACHED, EAGAIN }, /* 164 */
273 { ERROR_LOCK_FAILED, EACCES }, /* 167 */
274 { ERROR_ALREADY_EXISTS, EEXIST }, /* 183 */
275 { ERROR_FILENAME_EXCED_RANGE, ENOENT }, /* 206 */
276 { ERROR_NESTING_NOT_ALLOWED, EAGAIN }, /* 215 */
277 { ERROR_NOT_ENOUGH_QUOTA, ENOMEM } /* 1816 */
280 /* size of the table */
281 #define ERRTABLESIZE (sizeof(errtable)/sizeof(errtable[0]))
283 /* The following two constants must be the minimum and maximum
284 values in the (contiguous) range of Exec Failure errors. */
285 #define MIN_EXEC_ERROR ERROR_INVALID_STARTING_CODESEG
286 #define MAX_EXEC_ERROR ERROR_INFLOOP_IN_RELOC_CHAIN
288 /* These are the low and high value in the range of errors that are
290 #define MIN_EACCES_RANGE ERROR_WRITE_PROTECT
291 #define MAX_EACCES_RANGE ERROR_SHARING_BUFFER_EXCEEDED
293 static void maperrno (void)
298 dwErrorCode = GetLastError();
300 /* check the table for the OS error code */
301 for (i = 0; i < ERRTABLESIZE; ++i)
303 if (dwErrorCode == errtable[i].oscode)
305 errno = errtable[i].errnocode;
310 /* The error code wasn't in the table. We check for a range of */
311 /* EACCES errors or exec failure errors (ENOEXEC). Otherwise */
312 /* EINVAL is returned. */
314 if (dwErrorCode >= MIN_EACCES_RANGE && dwErrorCode <= MAX_EACCES_RANGE)
317 if (dwErrorCode >= MIN_EXEC_ERROR && dwErrorCode <= MAX_EXEC_ERROR)
324 * Function: mkAnonPipe
326 * Purpose: create an anonymous pipe with read and write ends being
327 * optionally (non-)inheritable.
330 mkAnonPipe (HANDLE* pHandleIn, BOOL isInheritableIn,
331 HANDLE* pHandleOut, BOOL isInheritableOut)
333 HANDLE hTemporaryIn = NULL;
334 HANDLE hTemporaryOut = NULL;
336 SECURITY_ATTRIBUTES sec_attrs;
338 /* Create inheritable security attributes */
339 sec_attrs.nLength = sizeof(SECURITY_ATTRIBUTES);
340 sec_attrs.lpSecurityDescriptor = NULL;
341 sec_attrs.bInheritHandle = TRUE;
343 /* Create the anon pipe with both ends inheritable */
344 if (!CreatePipe(&hTemporaryIn, &hTemporaryOut, &sec_attrs, 0))
353 *pHandleIn = hTemporaryIn;
356 /* Make the read end non-inheritable */
357 status = DuplicateHandle(GetCurrentProcess(), hTemporaryIn,
358 GetCurrentProcess(), pHandleIn,
360 FALSE, /* non-inheritable */
361 DUPLICATE_SAME_ACCESS);
362 CloseHandle(hTemporaryIn);
368 CloseHandle(hTemporaryOut);
373 if (isInheritableOut)
374 *pHandleOut = hTemporaryOut;
377 /* Make the write end non-inheritable */
378 status = DuplicateHandle(GetCurrentProcess(), hTemporaryOut,
379 GetCurrentProcess(), pHandleOut,
381 FALSE, /* non-inheritable */
382 DUPLICATE_SAME_ACCESS);
383 CloseHandle(hTemporaryOut);
389 CloseHandle(*pHandleIn);
398 runProcess (char *cmd, char *workingDirectory, void *environment,
399 int fdStdInput, int fdStdOutput, int fdStdError)
402 PROCESS_INFORMATION pInfo;
406 ZeroMemory(&sInfo, sizeof(sInfo));
407 sInfo.cb = sizeof(sInfo);
408 sInfo.hStdInput = (HANDLE) _get_osfhandle(fdStdInput);
409 sInfo.hStdOutput= (HANDLE) _get_osfhandle(fdStdOutput);
410 sInfo.hStdError = (HANDLE) _get_osfhandle(fdStdError);
412 if (sInfo.hStdInput == INVALID_HANDLE_VALUE)
413 sInfo.hStdInput = NULL;
414 if (sInfo.hStdOutput == INVALID_HANDLE_VALUE)
415 sInfo.hStdOutput = NULL;
416 if (sInfo.hStdError == INVALID_HANDLE_VALUE)
417 sInfo.hStdError = NULL;
419 if (sInfo.hStdInput || sInfo.hStdOutput || sInfo.hStdError)
420 sInfo.dwFlags = STARTF_USESTDHANDLES;
422 if (sInfo.hStdInput != GetStdHandle(STD_INPUT_HANDLE) &&
423 sInfo.hStdOutput != GetStdHandle(STD_OUTPUT_HANDLE) &&
424 sInfo.hStdError != GetStdHandle(STD_ERROR_HANDLE))
425 flags = CREATE_NO_WINDOW; // Run without console window only when both output and error are redirected
429 if (!CreateProcess(NULL, cmd, NULL, NULL, TRUE, flags, environment, workingDirectory, &sInfo, &pInfo))
435 CloseHandle(pInfo.hThread);
436 return (ProcHandle)pInfo.hProcess;
440 runInteractiveProcess (char *cmd, char *workingDirectory, void *environment,
441 int *pfdStdInput, int *pfdStdOutput, int *pfdStdError)
444 PROCESS_INFORMATION pInfo;
445 HANDLE hStdInputRead, hStdInputWrite;
446 HANDLE hStdOutputRead, hStdOutputWrite;
447 HANDLE hStdErrorRead, hStdErrorWrite;
449 if (!mkAnonPipe(&hStdInputRead, TRUE, &hStdInputWrite, FALSE))
452 if (!mkAnonPipe(&hStdOutputRead, FALSE, &hStdOutputWrite, TRUE))
454 CloseHandle(hStdInputRead);
455 CloseHandle(hStdInputWrite);
459 if (!mkAnonPipe(&hStdErrorRead, FALSE, &hStdErrorWrite, TRUE))
461 CloseHandle(hStdInputRead);
462 CloseHandle(hStdInputWrite);
463 CloseHandle(hStdOutputRead);
464 CloseHandle(hStdOutputWrite);
468 ZeroMemory(&sInfo, sizeof(sInfo));
469 sInfo.cb = sizeof(sInfo);
470 sInfo.dwFlags = STARTF_USESTDHANDLES;
471 sInfo.hStdInput = hStdInputRead;
472 sInfo.hStdOutput= hStdOutputWrite;
473 sInfo.hStdError = hStdErrorWrite;
475 if (!CreateProcess(NULL, cmd, NULL, NULL, TRUE, CREATE_NO_WINDOW, environment, workingDirectory, &sInfo, &pInfo))
478 CloseHandle(hStdInputRead);
479 CloseHandle(hStdInputWrite);
480 CloseHandle(hStdOutputRead);
481 CloseHandle(hStdOutputWrite);
482 CloseHandle(hStdErrorRead);
483 CloseHandle(hStdErrorWrite);
486 CloseHandle(pInfo.hThread);
488 // Close the ends of the pipes that were inherited by the
489 // child process. This is important, otherwise we won't see
490 // EOF on these pipes when the child process exits.
491 CloseHandle(hStdInputRead);
492 CloseHandle(hStdOutputWrite);
493 CloseHandle(hStdErrorWrite);
495 *pfdStdInput = _open_osfhandle((intptr_t) hStdInputWrite, _O_WRONLY);
496 *pfdStdOutput = _open_osfhandle((intptr_t) hStdOutputRead, _O_RDONLY);
497 *pfdStdError = _open_osfhandle((intptr_t) hStdErrorRead, _O_RDONLY);
499 return (int) pInfo.hProcess;
503 terminateProcess (ProcHandle handle)
505 if (!TerminateProcess((HANDLE) handle, 1)) {
510 CloseHandle((HANDLE) handle);
515 getProcessExitCode (ProcHandle handle, int *pExitCode)
519 if (WaitForSingleObject((HANDLE) handle, 1) == WAIT_OBJECT_0)
521 if (GetExitCodeProcess((HANDLE) handle, (DWORD *) pExitCode) == 0)
527 CloseHandle((HANDLE) handle);
535 waitForProcess (ProcHandle handle)
539 if (WaitForSingleObject((HANDLE) handle, INFINITE) == WAIT_OBJECT_0)
541 if (GetExitCodeProcess((HANDLE) handle, &retCode) == 0)
547 CloseHandle((HANDLE) handle);