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 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 --------------------- */
236 /* This is the error table that defines the mapping between OS error
237 codes and errno values */
240 unsigned long oscode; /* OS return value */
241 int errnocode; /* System V error code */
244 static struct errentry errtable[] = {
245 { ERROR_INVALID_FUNCTION, EINVAL }, /* 1 */
246 { ERROR_FILE_NOT_FOUND, ENOENT }, /* 2 */
247 { ERROR_PATH_NOT_FOUND, ENOENT }, /* 3 */
248 { ERROR_TOO_MANY_OPEN_FILES, EMFILE }, /* 4 */
249 { ERROR_ACCESS_DENIED, EACCES }, /* 5 */
250 { ERROR_INVALID_HANDLE, EBADF }, /* 6 */
251 { ERROR_ARENA_TRASHED, ENOMEM }, /* 7 */
252 { ERROR_NOT_ENOUGH_MEMORY, ENOMEM }, /* 8 */
253 { ERROR_INVALID_BLOCK, ENOMEM }, /* 9 */
254 { ERROR_BAD_ENVIRONMENT, E2BIG }, /* 10 */
255 { ERROR_BAD_FORMAT, ENOEXEC }, /* 11 */
256 { ERROR_INVALID_ACCESS, EINVAL }, /* 12 */
257 { ERROR_INVALID_DATA, EINVAL }, /* 13 */
258 { ERROR_INVALID_DRIVE, ENOENT }, /* 15 */
259 { ERROR_CURRENT_DIRECTORY, EACCES }, /* 16 */
260 { ERROR_NOT_SAME_DEVICE, EXDEV }, /* 17 */
261 { ERROR_NO_MORE_FILES, ENOENT }, /* 18 */
262 { ERROR_LOCK_VIOLATION, EACCES }, /* 33 */
263 { ERROR_BAD_NETPATH, ENOENT }, /* 53 */
264 { ERROR_NETWORK_ACCESS_DENIED, EACCES }, /* 65 */
265 { ERROR_BAD_NET_NAME, ENOENT }, /* 67 */
266 { ERROR_FILE_EXISTS, EEXIST }, /* 80 */
267 { ERROR_CANNOT_MAKE, EACCES }, /* 82 */
268 { ERROR_FAIL_I24, EACCES }, /* 83 */
269 { ERROR_INVALID_PARAMETER, EINVAL }, /* 87 */
270 { ERROR_NO_PROC_SLOTS, EAGAIN }, /* 89 */
271 { ERROR_DRIVE_LOCKED, EACCES }, /* 108 */
272 { ERROR_BROKEN_PIPE, EPIPE }, /* 109 */
273 { ERROR_DISK_FULL, ENOSPC }, /* 112 */
274 { ERROR_INVALID_TARGET_HANDLE, EBADF }, /* 114 */
275 { ERROR_INVALID_HANDLE, EINVAL }, /* 124 */
276 { ERROR_WAIT_NO_CHILDREN, ECHILD }, /* 128 */
277 { ERROR_CHILD_NOT_COMPLETE, ECHILD }, /* 129 */
278 { ERROR_DIRECT_ACCESS_HANDLE, EBADF }, /* 130 */
279 { ERROR_NEGATIVE_SEEK, EINVAL }, /* 131 */
280 { ERROR_SEEK_ON_DEVICE, EACCES }, /* 132 */
281 { ERROR_DIR_NOT_EMPTY, ENOTEMPTY }, /* 145 */
282 { ERROR_NOT_LOCKED, EACCES }, /* 158 */
283 { ERROR_BAD_PATHNAME, ENOENT }, /* 161 */
284 { ERROR_MAX_THRDS_REACHED, EAGAIN }, /* 164 */
285 { ERROR_LOCK_FAILED, EACCES }, /* 167 */
286 { ERROR_ALREADY_EXISTS, EEXIST }, /* 183 */
287 { ERROR_FILENAME_EXCED_RANGE, ENOENT }, /* 206 */
288 { ERROR_NESTING_NOT_ALLOWED, EAGAIN }, /* 215 */
289 { ERROR_NOT_ENOUGH_QUOTA, ENOMEM } /* 1816 */
292 /* size of the table */
293 #define ERRTABLESIZE (sizeof(errtable)/sizeof(errtable[0]))
295 /* The following two constants must be the minimum and maximum
296 values in the (contiguous) range of Exec Failure errors. */
297 #define MIN_EXEC_ERROR ERROR_INVALID_STARTING_CODESEG
298 #define MAX_EXEC_ERROR ERROR_INFLOOP_IN_RELOC_CHAIN
300 /* These are the low and high value in the range of errors that are
302 #define MIN_EACCES_RANGE ERROR_WRITE_PROTECT
303 #define MAX_EACCES_RANGE ERROR_SHARING_BUFFER_EXCEEDED
305 static void maperrno (void)
310 dwErrorCode = GetLastError();
312 /* check the table for the OS error code */
313 for (i = 0; i < ERRTABLESIZE; ++i)
315 if (dwErrorCode == errtable[i].oscode)
317 errno = errtable[i].errnocode;
322 /* The error code wasn't in the table. We check for a range of */
323 /* EACCES errors or exec failure errors (ENOEXEC). Otherwise */
324 /* EINVAL is returned. */
326 if (dwErrorCode >= MIN_EACCES_RANGE && dwErrorCode <= MAX_EACCES_RANGE)
329 if (dwErrorCode >= MIN_EXEC_ERROR && dwErrorCode <= MAX_EXEC_ERROR)
336 * Function: mkAnonPipe
338 * Purpose: create an anonymous pipe with read and write ends being
339 * optionally (non-)inheritable.
342 mkAnonPipe (HANDLE* pHandleIn, BOOL isInheritableIn,
343 HANDLE* pHandleOut, BOOL isInheritableOut)
345 HANDLE hTemporaryIn = NULL;
346 HANDLE hTemporaryOut = NULL;
348 SECURITY_ATTRIBUTES sec_attrs;
350 /* Create inheritable security attributes */
351 sec_attrs.nLength = sizeof(SECURITY_ATTRIBUTES);
352 sec_attrs.lpSecurityDescriptor = NULL;
353 sec_attrs.bInheritHandle = TRUE;
355 /* Create the anon pipe with both ends inheritable */
356 if (!CreatePipe(&hTemporaryIn, &hTemporaryOut, &sec_attrs, 0))
365 *pHandleIn = hTemporaryIn;
368 /* Make the read end non-inheritable */
369 status = DuplicateHandle(GetCurrentProcess(), hTemporaryIn,
370 GetCurrentProcess(), pHandleIn,
372 FALSE, /* non-inheritable */
373 DUPLICATE_SAME_ACCESS);
374 CloseHandle(hTemporaryIn);
380 CloseHandle(hTemporaryOut);
385 if (isInheritableOut)
386 *pHandleOut = hTemporaryOut;
389 /* Make the write end non-inheritable */
390 status = DuplicateHandle(GetCurrentProcess(), hTemporaryOut,
391 GetCurrentProcess(), pHandleOut,
393 FALSE, /* non-inheritable */
394 DUPLICATE_SAME_ACCESS);
395 CloseHandle(hTemporaryOut);
401 CloseHandle(*pHandleIn);
410 runProcess (char *cmd, char *workingDirectory, void *environment,
411 int fdStdInput, int fdStdOutput, int fdStdError)
414 PROCESS_INFORMATION pInfo;
418 ZeroMemory(&sInfo, sizeof(sInfo));
419 sInfo.cb = sizeof(sInfo);
420 sInfo.hStdInput = (HANDLE) _get_osfhandle(fdStdInput);
421 sInfo.hStdOutput= (HANDLE) _get_osfhandle(fdStdOutput);
422 sInfo.hStdError = (HANDLE) _get_osfhandle(fdStdError);
424 if (sInfo.hStdInput == INVALID_HANDLE_VALUE)
425 sInfo.hStdInput = NULL;
426 if (sInfo.hStdOutput == INVALID_HANDLE_VALUE)
427 sInfo.hStdOutput = NULL;
428 if (sInfo.hStdError == INVALID_HANDLE_VALUE)
429 sInfo.hStdError = NULL;
431 if (sInfo.hStdInput || sInfo.hStdOutput || sInfo.hStdError)
432 sInfo.dwFlags = STARTF_USESTDHANDLES;
434 if (sInfo.hStdInput != GetStdHandle(STD_INPUT_HANDLE) &&
435 sInfo.hStdOutput != GetStdHandle(STD_OUTPUT_HANDLE) &&
436 sInfo.hStdError != GetStdHandle(STD_ERROR_HANDLE))
437 flags = CREATE_NO_WINDOW; // Run without console window only when both output and error are redirected
441 if (!CreateProcess(NULL, cmd, NULL, NULL, TRUE, flags, environment, workingDirectory, &sInfo, &pInfo))
447 CloseHandle(pInfo.hThread);
448 return (ProcHandle)pInfo.hProcess;
452 runInteractiveProcess (char *cmd, char *workingDirectory, void *environment,
453 int *pfdStdInput, int *pfdStdOutput, int *pfdStdError)
456 PROCESS_INFORMATION pInfo;
457 HANDLE hStdInputRead, hStdInputWrite;
458 HANDLE hStdOutputRead, hStdOutputWrite;
459 HANDLE hStdErrorRead, hStdErrorWrite;
461 if (!mkAnonPipe(&hStdInputRead, TRUE, &hStdInputWrite, FALSE))
464 if (!mkAnonPipe(&hStdOutputRead, FALSE, &hStdOutputWrite, TRUE))
466 CloseHandle(hStdInputRead);
467 CloseHandle(hStdInputWrite);
471 if (!mkAnonPipe(&hStdErrorRead, FALSE, &hStdErrorWrite, TRUE))
473 CloseHandle(hStdInputRead);
474 CloseHandle(hStdInputWrite);
475 CloseHandle(hStdOutputRead);
476 CloseHandle(hStdOutputWrite);
480 ZeroMemory(&sInfo, sizeof(sInfo));
481 sInfo.cb = sizeof(sInfo);
482 sInfo.dwFlags = STARTF_USESTDHANDLES;
483 sInfo.hStdInput = hStdInputRead;
484 sInfo.hStdOutput= hStdOutputWrite;
485 sInfo.hStdError = hStdErrorWrite;
487 if (!CreateProcess(NULL, cmd, NULL, NULL, TRUE, CREATE_NO_WINDOW, environment, workingDirectory, &sInfo, &pInfo))
490 CloseHandle(hStdInputRead);
491 CloseHandle(hStdInputWrite);
492 CloseHandle(hStdOutputRead);
493 CloseHandle(hStdOutputWrite);
494 CloseHandle(hStdErrorRead);
495 CloseHandle(hStdErrorWrite);
498 CloseHandle(pInfo.hThread);
500 // Close the ends of the pipes that were inherited by the
501 // child process. This is important, otherwise we won't see
502 // EOF on these pipes when the child process exits.
503 CloseHandle(hStdInputRead);
504 CloseHandle(hStdOutputWrite);
505 CloseHandle(hStdErrorWrite);
507 *pfdStdInput = _open_osfhandle((intptr_t) hStdInputWrite, _O_WRONLY);
508 *pfdStdOutput = _open_osfhandle((intptr_t) hStdOutputRead, _O_RDONLY);
509 *pfdStdError = _open_osfhandle((intptr_t) hStdErrorRead, _O_RDONLY);
511 return (int) pInfo.hProcess;
515 terminateProcess (ProcHandle handle)
517 if (!TerminateProcess((HANDLE) handle, 1)) {
525 getProcessExitCode (ProcHandle handle, int *pExitCode)
529 if (WaitForSingleObject((HANDLE) handle, 1) == WAIT_OBJECT_0)
531 if (GetExitCodeProcess((HANDLE) handle, (DWORD *) pExitCode) == 0)
543 waitForProcess (ProcHandle handle)
547 if (WaitForSingleObject((HANDLE) handle, INFINITE) == WAIT_OBJECT_0)
549 if (GetExitCodeProcess((HANDLE) handle, &retCode) == 0)