1 /*-------------------------------------------------------------------------
2 timeout.c - source file for running ucSim within the regression tests
4 Written By - Bernhard Held . bernhard@bernhardheld.de (2001)
5 Native WIN32 port by - Borut Razem . borut.razem@siol.net (2007)
7 This program is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 2, or (at your option) any
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 In other words, you are welcome to use, share and improve this program.
22 You are forbidden to forbid anyone else to use, share and improve
23 what you give them. Help stamp out software-hoarding!
24 -------------------------------------------------------------------------*/
26 #define PROGNAME "timeout"
28 #define USAGE PROGNAME " : 1.10\n" \
29 "Usage : " PROGNAME " timeout_in_seconds filename [arguments]\n" \
30 " \"filename\" is executed, the arguments are passed to \"filename\".\n" \
31 " When \"filename\" exits before the timeout expires, the\n" \
32 " exit-status of \"filename\" is returned.\n" \
33 " When the timeout expires before \"filename\" exits, \"filename\"\n" \
34 " will be killed and an exit-status of 1 is returned.\n"
44 The program creates a chile process using CreateProcess(). The waits until:
46 The exit status of the child is returned.
48 The child will be killed.
52 makeCmdLine(char *buf
, int argc
, const char * const *argv
)
59 int argLen
= strlen(*argv
);
63 memcpy(buf
, *argv
, argLen
);
81 main (int argc
, const char * const *argv
)
84 PROCESS_INFORMATION pi
;
92 fprintf (stderr
, USAGE
);
95 timeout
= atol (argv
[1]);
98 fprintf (stderr
, "Error parameter " PROGNAME
": must be a non-zero dezimal value\n");
102 cmdLine
= alloca(makeCmdLine(NULL
, argc
, argv
));
103 makeCmdLine(cmdLine
, argc
, argv
);
105 ZeroMemory(&si
, sizeof (si
));
107 ZeroMemory(&pi
, sizeof (pi
));
109 /* We'll redirect here stderr to stdout, which will be redirected */
110 /* to /dev/null by the shell. The shell could also redirect stderr */
111 /* to /dev/null, but then this program doesn't have the chance to */
112 /* output any real error. */
113 oldStderr
= GetStdHandle (STD_ERROR_HANDLE
);
114 SetStdHandle (STD_ERROR_HANDLE
, GetStdHandle (STD_OUTPUT_HANDLE
));
116 /* Start the child process. */
117 if(!CreateProcess (NULL
, // No module name (use command line)
118 cmdLine
, // Command line
119 NULL
, // Process handle not inheritable
120 NULL
, // Thread handle not inheritable
121 TRUE
, // Set handle inheritance to TRUE
122 0, // No creation flags
123 NULL
, // Use parent's environment block
124 NULL
, // Use parent's starting directory
125 &si
, // Pointer to STARTUPINFO structure
126 &pi
) // Pointer to PROCESS_INFORMATION structure
129 printf ("CreateProcess failed (%d).\n", GetLastError ());
134 SetStdHandle (STD_ERROR_HANDLE
, oldStderr
);
136 /* Wait until child process exits or timeout expires. */
137 if (WaitForSingleObject (pi
.hProcess
, timeout
* 1000) == WAIT_TIMEOUT
)
139 TerminateProcess (pi
.hProcess
, 1);
140 WaitForSingleObject (pi
.hProcess
, INFINITE
);
143 GetExitCodeProcess (pi
.hProcess
, &exitCode
);
145 /* Close process and thread handles. */
146 CloseHandle (pi
.hProcess
);
147 CloseHandle (pi
.hThread
);
157 #include <sys/wait.h>
158 #include <sys/types.h>
159 #include <sys/time.h>
160 #include <sys/resource.h>
163 /* First the program tries to limit the maximum CPU-time to the timeout-value.
164 Then the child is run with execvp().
166 It's not possible to limit the CPU-time under Cygwin (V1.3.3). If setrlimit (RLIMIT_CPU, rlp)
167 fails, the program will fork() and run the child with execvp(). The fork/exec pair is slow on
168 Cygwin, but what else can we do? The parent sleeps until:
169 - a signal shows the childĀ“s exitus
170 The exit status of the child is returned.
171 - the timeout elapses
172 The child will be killed.
175 /* Get the status from all child processes that have terminated, without ever waiting.
176 This function is designed to be a handler for SIGCHLD, the signal that indicates
177 that at least one child process has terminated.
178 http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_23.html#SEC401
186 sigchld_handler (int signum
)
194 pid
= waitpid (WAIT_ANY
, &status
, WNOHANG
);
195 if (WEXITSTATUS (status
))
196 exit_status
= 1; // WEXITSTATUS(status);
197 /* pid == -1: no children */
198 /* pid == 0: no children to be noticed */
206 main (int argc
, char * const *argv
)
208 /* if getrlimit() / setrlimit() succeed, then no fork is neeeded */
217 fprintf (stderr
, USAGE
);
220 timeout
= atol (argv
[1]);
223 fprintf (stderr
, "Error parameter " PROGNAME
": must be a non-zero dezimal value\n");
227 /* try to use getrlimit() / setrlimit() for RLIMIT_CPU */
228 /* to limit the CPU-time */
229 if (getrlimit (RLIMIT_CPU
, &rl
) == 0)
231 rl
.rlim_cur
= timeout
;
232 if (setrlimit (RLIMIT_CPU
, &rl
) == 0)
237 { /* the CPU-time is limited: simple execvp */
239 /* s51 prints warnings on stderr: */
240 /* serial input/output interface connected to a non-terminal file. */
241 /* We'll redirect here stderr to stdout, which will be redirected */
242 /* to /dev/null by the shell. The shell could also redirect stderr */
243 /* to /dev/null, but then this program doesn't have the chance to */
244 /* output any real error. */
245 old_stderr
= dup (STDERR_FILENO
);
246 dup2 (STDOUT_FILENO
, STDERR_FILENO
);
247 /* shouldn't return */
248 execvp (argv
[2], argv
+ 2);
250 dup2 (old_stderr
, STDERR_FILENO
);
252 return 1; /* Error */
256 /* do it the hard way: fork/exec */
257 signal (SIGCHLD
, sigchld_handler
);
261 /* s51 prints warnings on stderr: */
262 /* serial input/output interface connected to a non-terminal file. */
263 /* We'll redirect here stderr to stdout, which will be redirected */
264 /* to /dev/null by the shell. The shell could also redirect stderr */
265 /* to /dev/null, but then this program doesn't have the chance to */
266 /* output any real error. */
267 old_stderr
= dup (STDERR_FILENO
);
268 dup2 (STDOUT_FILENO
, STDERR_FILENO
);
269 /* shouldn't return */
270 execvp (argv
[2], argv
+ 2);
272 dup2 (old_stderr
, STDERR_FILENO
);
274 return 1; /* Error */
278 /* this timeout is hopefully aborted by a SIGCHLD */
280 fprintf (stderr
, PROGNAME
": timeout, killing child %s\n", argv
[2]);
281 kill (pid_child
, SIGTERM
);
282 return 1; /* Error */