2 * (c) The GRASP/AQUA Project, Glasgow University, 1994-1998
4 * $Id: system.c,v 1.11 2001/02/20 03:41:31 qrczak Exp $
6 * system Runtime Support
9 /* The itimer stuff in this module is non-posix */
10 #define NON_POSIX_SOURCE
19 #if TIME_WITH_SYS_TIME
20 # include <sys/time.h>
24 # include <sys/time.h>
30 #ifndef mingw32_TARGET_OS
31 # ifdef HAVE_SYS_WAIT_H
32 # include <sys/wait.h>
35 # include <windows.h> /* for Sleep */
47 systemCmd(StgByteArray cmd)
49 #if defined(mingw32_TARGET_OS)
50 /* There's no fork() under Windows, so we fall back on using libc's
51 system() instead. (It in turn has problems, as it does not wait
52 until the sub shell has finished before returning. Using Sleep()
53 works around that.) */
54 if (system(cmd) < 0) {
65 switch(pid = fork()) {
75 /* Reset the itimers in the child, so it doesn't get plagued
76 * by SIGVTALRM interrupts.
78 struct timeval tv_null = { 0, 0 };
80 itv.it_interval = tv_null;
81 itv.it_value = tv_null;
82 setitimer(ITIMER_REAL, &itv, NULL);
83 setitimer(ITIMER_VIRTUAL, &itv, NULL);
84 setitimer(ITIMER_PROF, &itv, NULL);
88 execl("/bin/sh", "sh", "-c", cmd, NULL);
93 while (waitpid(pid, &wstat, 0) < 0) {
101 if (WIFEXITED(wstat))
102 return WEXITSTATUS(wstat);
103 else if (WIFSIGNALED(wstat)) {
104 ghc_errtype = ERR_INTERRUPTED;
105 ghc_errstr = "system command interrupted";
108 /* This should never happen */
109 ghc_errtype = ERR_OTHERERROR;
110 ghc_errstr = "internal error (process neither exited nor signalled)";