File: timeout.c

package info (click to toggle)
cc1111 2.9.0-4
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 38,692 kB
  • ctags: 132,262
  • sloc: ansic: 442,650; cpp: 37,006; sh: 10,334; makefile: 5,511; asm: 5,279; yacc: 2,953; lisp: 1,524; perl: 807; awk: 493; python: 468; lex: 447
file content (286 lines) | stat: -rw-r--r-- 8,796 bytes parent folder | download | duplicates (13)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*-------------------------------------------------------------------------
  timeout.c - source file for running ucSim within the regression tests

             Written By -  Bernhard Held . bernhard@bernhardheld.de (2001)
   Native WIN32 port by -  Borut Razem . borut.razem@siol.net (2007)

   This program is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by the
   Free Software Foundation; either version 2, or (at your option) any
   later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

   In other words, you are welcome to use, share and improve this program.
   You are forbidden to forbid anyone else to use, share and improve
   what you give them.   Help stamp out software-hoarding!
-------------------------------------------------------------------------*/

#define PROGNAME "timeout"

#define USAGE PROGNAME " : 1.10\n" \
              "Usage : " PROGNAME " timeout_in_seconds filename [arguments]\n" \
              "  \"filename\" is executed, the arguments are passed to \"filename\".\n" \
              "  When \"filename\" exits before the timeout expires, the\n" \
              "  exit-status of \"filename\" is returned.\n" \
              "  When the timeout expires before \"filename\" exits, \"filename\"\n" \
              "  will be killed and an exit-status of 1 is returned.\n"

#ifdef _WIN32
#include <windows.h>
#include <stdio.h>
#include <malloc.h>

/*
   Native WIN32 version:

   The program creates a chile process using CreateProcess(). The waits until:
   - the child exits
        The exit status of the child is returned.
   - the timeout elapses
        The child will be killed.
*/

int
makeCmdLine(char *buf, int argc, const char * const *argv)
{
  int len = 0;

  argv += 2;
  while (argc-- > 2)
    {
      int argLen = strlen(*argv);
      len += argLen;
      if (buf)
        {
          memcpy(buf, *argv, argLen);
          buf += argLen;
        }
      if (argc > 2)
        {
          ++len;
          if (buf)
            *buf++ = ' ';
        }
      ++argv;
    }
  if (buf)
    *buf = '\0';

  return len + 1;
}

int
main (int argc, const char * const *argv)
{
  STARTUPINFO si;
  PROCESS_INFORMATION pi;
  char *cmdLine;
  long timeout;
  DWORD exitCode;
  HANDLE oldStderr;

  if (argc < 3)
    {
      fprintf (stderr, USAGE);
      return 1;
    }
  timeout = atol (argv[1]);
  if (timeout == 0)
    {
      fprintf (stderr, "Error parameter " PROGNAME ": must be a non-zero dezimal value\n");
      return 1;
    }

  cmdLine = alloca(makeCmdLine(NULL, argc, argv));
  makeCmdLine(cmdLine, argc, argv);

  ZeroMemory(&si, sizeof (si));
  si.cb = sizeof (si);
  ZeroMemory(&pi, sizeof (pi));

  /* We'll redirect here stderr to stdout, which will be redirected  */
  /* to /dev/null by the shell. The shell could also redirect stderr */
  /* to /dev/null, but then this program doesn't have the chance to  */
  /* output any real error. */
  oldStderr = GetStdHandle (STD_ERROR_HANDLE);
  SetStdHandle (STD_ERROR_HANDLE, GetStdHandle (STD_OUTPUT_HANDLE));

  /* Start the child process. */
  if(!CreateProcess (NULL, // No module name (use command line)
    cmdLine,        // Command line
    NULL,           // Process handle not inheritable
    NULL,           // Thread handle not inheritable
    TRUE,           // Set handle inheritance to TRUE
    0,              // No creation flags
    NULL,           // Use parent's environment block
    NULL,           // Use parent's starting directory 
    &si,            // Pointer to STARTUPINFO structure
    &pi)            // Pointer to PROCESS_INFORMATION structure
    ) 
    {
      printf ("CreateProcess failed (%d).\n", GetLastError ());
      return 1;
    }

  /* Restore stderr */
  SetStdHandle (STD_ERROR_HANDLE, oldStderr);

  /* Wait until child process exits or timeout expires. */
  if (WaitForSingleObject (pi.hProcess, timeout * 1000) == WAIT_TIMEOUT)
    {
      TerminateProcess (pi.hProcess, 1);
      WaitForSingleObject (pi.hProcess, INFINITE);
    }

  GetExitCodeProcess (pi.hProcess, &exitCode);

  /* Close process and thread handles. */
  CloseHandle (pi.hProcess);
  CloseHandle (pi.hThread);

  return exitCode;
}

#else

#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <unistd.h>

/* First the program tries to limit the maximum CPU-time to the timeout-value.
   Then the child is run with execvp().

   It's not possible to limit the CPU-time under Cygwin (V1.3.3). If setrlimit (RLIMIT_CPU, rlp)
   fails, the program will fork() and run the child with execvp(). The fork/exec pair is slow on
   Cygwin, but what else can we do? The parent sleeps until:
   - a signal shows the childs exitus
        The exit status of the child is returned.
   - the timeout elapses
        The child will be killed.
*/

/* Get the status from all child processes that have terminated, without ever waiting.
   This function is designed to be a handler for SIGCHLD, the signal that indicates
   that at least one child process has terminated.
   http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_23.html#SEC401
*/

#ifndef WAIT_ANY
  #define WAIT_ANY -1
#endif

void
sigchld_handler (int signum)
{
  int pid;
  int status;
  int exit_status = 0;

  while (1)
    {
      pid = waitpid (WAIT_ANY, &status, WNOHANG);
      if (WEXITSTATUS (status))
        exit_status = 1; // WEXITSTATUS(status);
      /* pid == -1: no children               */
      /* pid ==  0: no children to be noticed */
      if (pid <= 0)
        break;
    }
  exit (exit_status);
}

int
main (int argc, char * const *argv)
{
  /* if getrlimit() / setrlimit() succeed, then no fork is neeeded */
  int flagNoFork = 0;
  int old_stderr;
  long timeout;
  pid_t pid_child;
  struct rlimit rl;

  if (argc < 3)
    {
      fprintf (stderr, USAGE);
      return 1;
    }
  timeout = atol (argv[1]);
  if (timeout == 0)
    {
      fprintf (stderr, "Error parameter " PROGNAME ": must be a non-zero dezimal value\n");
      return 1;
    }

  /* try to use getrlimit() / setrlimit() for RLIMIT_CPU */
  /* to limit the CPU-time                               */
  if (getrlimit (RLIMIT_CPU, &rl) == 0)
    {
      rl.rlim_cur = timeout;
      if (setrlimit (RLIMIT_CPU, &rl) == 0)
        flagNoFork = 1;
    }

  if (flagNoFork)
    { /* the CPU-time is limited: simple execvp */

      /* s51 prints warnings on stderr:                                  */
      /* serial input/output interface connected to a non-terminal file. */
      /* We'll redirect here stderr to stdout, which will be redirected  */
      /* to /dev/null by the shell. The shell could also redirect stderr */
      /* to /dev/null, but then this program doesn't have the chance to  */
      /* output any real error. */
      old_stderr = dup (STDERR_FILENO);
      dup2 (STDOUT_FILENO, STDERR_FILENO);
      /* shouldn't return */
      execvp (argv[2], argv + 2);
      /* restore stderr */
      dup2 (old_stderr, STDERR_FILENO);
      perror (argv[2]);
      return 1; /* Error */
    }
  else
    {
      /* do it the hard way: fork/exec */
      signal (SIGCHLD, sigchld_handler);
      pid_child = fork();
      if (pid_child == 0)
        {
           /* s51 prints warnings on stderr:                                  */
           /* serial input/output interface connected to a non-terminal file. */
           /* We'll redirect here stderr to stdout, which will be redirected  */
           /* to /dev/null by the shell. The shell could also redirect stderr */
           /* to /dev/null, but then this program doesn't have the chance to  */
           /* output any real error. */
           old_stderr = dup (STDERR_FILENO);
           dup2 (STDOUT_FILENO, STDERR_FILENO);
           /* shouldn't return */
           execvp (argv[2], argv + 2);
           /* restore stderr */
           dup2 (old_stderr, STDERR_FILENO);
           perror (argv[2]);
           return 1; /* Error */
        }
      else
        {
          /* this timeout is hopefully aborted by a SIGCHLD */
          sleep (timeout);
          fprintf (stderr, PROGNAME ": timeout, killing child %s\n", argv[2]);
          kill (pid_child, SIGTERM);
          return 1; /* Error */
        }
    }
}
#endif