* sdcc/sim/ucsim/s51.src/glob.cc: Minor fix.
[fw/sdcc] / support / regression / fwk / lib / timeout.c
1 /*-------------------------------------------------------------------------
2   timeout.c - source file for running ucSim within the regression tests
3
4              Written By -  Bernhard Held . bernhard@bernhardheld.de (2001)
5
6    This program is free software; you can redistribute it and/or modify it
7    under the terms of the GNU General Public License as published by the
8    Free Software Foundation; either version 2, or (at your option) any
9    later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19
20    In other words, you are welcome to use, share and improve this program.
21    You are forbidden to forbid anyone else to use, share and improve
22    what you give them.   Help stamp out software-hoarding!
23 -------------------------------------------------------------------------*/
24
25 #define PROGNAME "timeout"
26
27 #define USAGE PROGNAME " : 1.00\n" \
28               "Usage : " PROGNAME " timeout_in_seconds filename [arguments]\n" \
29               "  ´filename´ is executed, the arguments are passed to ´filename´.\n" \
30               "  When ´filename´exits before the timeout expires, the\n" \
31               "  exit-status of ´filename´ is returned.\n" \
32               "  When the timeout expires before ´filename´ exits, ´filename´\n" \
33               "  will be killed and an exit-status of 1 is returned.\n"
34
35 /* First the program tries to limit the maximum CPU-time to the timeout-value.
36    Then the child is run with execvp().
37
38    It's not possible to limit the CPU-time under Cygwin (V1.3.3). If setrlimit (RLIMIT_CPU, rlp)
39    fails, the program will fork() and run the child with execvp(). The fork/exec pair is slow on
40    Cygwin, but what else can we do? The parent sleeps until:
41    - a signal shows the child´s exitus
42         The exit status of the child is returned.
43    - the timeout elapses
44         The child will be killed.
45 */
46
47 #include <signal.h>
48 #include <stdio.h>
49 #include <sys/wait.h>
50 #include <unistd.h>
51 #include <sys/types.h>
52
53 /* Get the status from all child processes that have terminated, without ever waiting.
54    This function is designed to be a handler for SIGCHLD, the signal that indicates
55    that at least one child process has terminated.
56    http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_23.html#SEC401
57 */
58
59 #ifndef WAIT_ANY
60   #define WAIT_ANY -1
61 #endif
62
63 void
64 sigchld_handler (int signum)
65 {
66   int pid;
67   int status;
68   int exit_status = 0;
69
70   while (1)
71     {
72       pid = waitpid (WAIT_ANY, &status, WNOHANG);
73       if (WEXITSTATUS (status))
74         exit_status = 1; // WEXITSTATUS(status);
75       /* pid == -1: no children               */
76       /* pid ==  0: no children to be noticed */
77       if (pid <= 0)
78         break;
79     }
80   exit (exit_status);
81 }
82
83 int
84 main (int argc, char * const *argv)
85 {
86   /* if getrlimit() / setrlimit() succeed, then no fork is neeeded */
87   int flagNoFork = 0;
88   int old_stderr;
89   long timeout;
90   pid_t pid_child;
91   struct rlimit rl;
92
93   if (argc < 3)
94     {
95       fprintf (stderr, USAGE);
96       return 1;
97     }
98   timeout = atol (argv[1]);
99   if (timeout == 0)
100     {
101       fprintf (stderr, "Error parameter " PROGNAME ": must be a non-zero dezimal value\n");
102       return 1;
103     }
104
105   /* try to use getrlimit() / setrlimit() for RLIMIT_CPU */
106   /* to limit the CPU-time                               */
107   if (getrlimit (RLIMIT_CPU, &rl) == 0)
108     {
109       rl.rlim_cur = timeout;
110       if (setrlimit (RLIMIT_CPU, &rl) == 0)
111         flagNoFork = 1;
112     }
113
114   if (flagNoFork)
115     { /* the CPU-time is limited: simple execvp */
116
117       /* s51 prints warnings on stderr:                                  */
118       /* serial input/output interface connected to a non-terminal file. */
119       /* We'll redirect here stderr to stdout, which will be redirected  */
120       /* to /dev/null by the shell. The shell could also redirect stderr */
121       /* to /dev/null, but then this program doesn't have the chance to  */
122       /* output any real error. */
123       old_stderr = dup (STDERR_FILENO);
124       dup2 (STDOUT_FILENO, STDERR_FILENO);
125       /* shouldn't return */
126       execvp (argv[2], argv + 2);
127       /* restore stderr */
128       dup2 (old_stderr, STDERR_FILENO);
129       perror (argv[2]);
130       return 1; /* Error */
131     }
132   else
133     {
134       /* do it the hard way: fork/exec */
135       signal (SIGCHLD, sigchld_handler);
136       pid_child = fork();
137       if (pid_child == 0)
138         {
139            /* s51 prints warnings on stderr:                                  */
140            /* serial input/output interface connected to a non-terminal file. */
141            /* We'll redirect here stderr to stdout, which will be redirected  */
142            /* to /dev/null by the shell. The shell could also redirect stderr */
143            /* to /dev/null, but then this program doesn't have the chance to  */
144            /* output any real error. */
145            old_stderr = dup (STDERR_FILENO);
146            dup2 (STDOUT_FILENO, STDERR_FILENO);
147            /* shouldn't return */
148            execvp (argv[2], argv + 2);
149            /* restore stderr */
150            dup2 (old_stderr, STDERR_FILENO);
151            perror (argv[2]);
152            return 1; /* Error */
153         }
154       else
155         {
156           /* this timeout is hopefully aborted by a SIGCHLD */
157           sleep (timeout);
158           fprintf (stderr, PROGNAME ": timeout, killing child %s\n", argv[2]);
159           kill (pid_child, SIGTERM);
160           return 1; /* Error */
161         }
162     }
163 }