Function attribute migration.
[fw/sdcc] / support / regression / ports / mcs51 / 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 <sys/time.h>
48 #include <sys/resource.h>
49
50 #include <signal.h>
51 #include <stdio.h>
52 #include <sys/wait.h>
53 #include <unistd.h>
54 #include <sys/types.h>
55
56 /* Get the status from all child processes that have terminated, without ever waiting.
57    This function is designed to be a handler for SIGCHLD, the signal that indicates
58    that at least one child process has terminated.
59    http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_23.html#SEC401
60 */
61
62 #ifndef WAIT_ANY
63   #define WAIT_ANY -1
64 #endif
65
66 void
67 sigchld_handler (int signum)
68 {
69   int pid;
70   int status;
71   int exit_status = 0;
72
73   while (1)
74     {
75       pid = waitpid (WAIT_ANY, &status, WNOHANG);
76       if (WEXITSTATUS (status))
77         exit_status = 1; // WEXITSTATUS(status);
78       /* pid == -1: no children               */
79       /* pid ==  0: no children to be noticed */
80       if (pid <= 0)
81         break;
82     }
83   exit (exit_status);
84 }
85
86 int
87 main (int argc, char * const *argv)
88 {
89   /* if getrlimit() / setrlimit() succeed, then no fork is neeeded */
90   int flagNoFork = 0;
91   int old_stderr;
92   long timeout;
93   pid_t pid_child;
94   struct rlimit rl;
95
96   if (argc < 3)
97     {
98       fprintf (stderr, USAGE);
99       return 1;
100     }
101   timeout = atol (argv[1]);
102   if (timeout == 0)
103     {
104       fprintf (stderr, "Error parameter " PROGNAME ": must be a non-zero dezimal value\n");
105       return 1;
106     }
107
108   /* try to use getrlimit() / setrlimit() for RLIMIT_CPU */
109   /* to limit the CPU-time                               */
110   if (getrlimit (RLIMIT_CPU, &rl) == 0)
111     {
112       rl.rlim_cur = timeout;
113       if (setrlimit (RLIMIT_CPU, &rl) == 0)
114         flagNoFork = 1;
115     }
116
117   if (flagNoFork)
118     { /* the CPU-time is limited: simple execvp */
119
120       /* s51 prints warnings on stderr:                                  */
121       /* serial input/output interface connected to a non-terminal file. */
122       /* We'll redirect here stderr to stdout, which will be redirected  */
123       /* to /dev/null by the shell. The shell could also redirect stderr */
124       /* to /dev/null, but then this program doesn't have the chance to  */
125       /* output any real error. */
126       old_stderr = dup (STDERR_FILENO);
127       dup2 (STDOUT_FILENO, STDERR_FILENO);
128       /* shouldn't return */
129       execvp (argv[2], argv + 2);
130       /* restore stderr */
131       dup2 (old_stderr, STDERR_FILENO);
132       perror (argv[2]);
133       return 1; /* Error */
134     }
135   else
136     {
137       /* do it the hard way: fork/exec */
138       signal (SIGCHLD, sigchld_handler);
139       pid_child = fork();
140       if (pid_child == 0)
141         {
142            /* s51 prints warnings on stderr:                                  */
143            /* serial input/output interface connected to a non-terminal file. */
144            /* We'll redirect here stderr to stdout, which will be redirected  */
145            /* to /dev/null by the shell. The shell could also redirect stderr */
146            /* to /dev/null, but then this program doesn't have the chance to  */
147            /* output any real error. */
148            old_stderr = dup (STDERR_FILENO);
149            dup2 (STDOUT_FILENO, STDERR_FILENO);
150            /* shouldn't return */
151            execvp (argv[2], argv + 2);
152            /* restore stderr */
153            dup2 (old_stderr, STDERR_FILENO);
154            perror (argv[2]);
155            return 1; /* Error */
156         }
157       else
158         {
159           /* this timeout is hopefully aborted by a SIGCHLD */
160           sleep (timeout);
161           fprintf (stderr, PROGNAME ": timeout, killing child %s\n", argv[2]);
162           kill (pid_child, SIGTERM);
163           return 1; /* Error */
164         }
165     }
166 }