490
|
1 /*
|
545
|
2 * crashRun - execute command with restricted CPU time and memory usage
|
490
|
3 *
|
502
|
4 * Copyright (C)
|
545
|
5 * 2005 Thomas Kuehne <thomas@kuehne.cn>
|
502
|
6 * 2005 Anders F Björklund <afb@algonet.se>
|
490
|
7 *
|
|
8 * This program is free software; you can redistribute it and/or modify
|
|
9 * it under the terms of the GNU General Public License as published by
|
|
10 * the Free Software Foundation; either version 2 of the License, or
|
|
11 * (at your option) any later version.
|
|
12 *
|
|
13 * This program is distributed in the hope that it will be useful,
|
|
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
16 * GNU General Public License for more details.
|
|
17 *
|
|
18 * You should have received a copy of the GNU General Public License
|
|
19 * along with this program; if not, write to the Free Software
|
|
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
21 *
|
|
22 * $HeadURL$
|
|
23 * $Date$
|
|
24 * $Author$
|
|
25 *
|
|
26 */
|
|
27
|
498
|
28 #include <errno.h>
|
|
29 #include <string.h>
|
|
30 #include <stdlib.h>
|
|
31 #include <stdio.h>
|
545
|
32 #include <signal.h>
|
498
|
33
|
545
|
34 const int TIME_OUT= 5*60; /* time-out in seconds (might be cpu or system time)*/
|
|
35 const int MEM_LIMIT = 200; /* mem megabytes */
|
498
|
36
|
545
|
37 /* try to cope with -ansi and other threads */
|
|
38 #if defined(__GNU_LIBRARY__) || defined(__GLIBC__) || defined(__USE_POSIX)
|
498
|
39 #define USE_POSIX
|
|
40 #endif
|
|
41
|
490
|
42 #ifdef linux
|
|
43 #define USE_POSIX
|
|
44 #endif
|
502
|
45
|
490
|
46 #if defined(__APPLE__) && defined(__MACH__)
|
|
47 #define USE_POSIX
|
|
48 #endif
|
|
49
|
|
50 #ifdef __FreeBSD__
|
|
51 #define USE_POSIX
|
|
52 #endif
|
|
53
|
545
|
54 #if !defined(USE_POSIX) && \
|
|
55 (defined(WINDOWS) || defined(WIN) || defined(WINVER) || defined(WIN32))
|
|
56 #define USE_WINDOWS
|
|
57 #endif
|
498
|
58
|
545
|
59
|
|
60 /* is the environment sane? */
|
|
61 #if defined(USE_POSIX) && defined(USE_WINDOWS)
|
|
62 #error USE_WINDOWS and USE_POSIX are defined
|
|
63 #endif
|
|
64
|
|
65 #if !defined(USE_POSIX) && !defined(USE_WINDOWS)
|
|
66 #error neither USE_POSIX nor USE_WINDOWS are defined
|
|
67 #endif
|
|
68
|
|
69
|
|
70 /* API inludes and config */
|
490
|
71 #ifdef USE_POSIX
|
545
|
72 #define USE_POSIX_LIMITS
|
|
73 #include <sys/types.h>
|
490
|
74 #include <unistd.h>
|
|
75 #include <sys/wait.h>
|
545
|
76 static pid_t pID;
|
|
77 #endif
|
490
|
78
|
545
|
79 #ifdef USE_POSIX_LIMITS
|
|
80 #ifndef USE_POSIX
|
|
81 #error USE_POSIX_LIMITS requires USE_POSIX
|
|
82 #endif
|
|
83 #include <sys/resource.h>
|
502
|
84 #include <sys/types.h>
|
|
85 #endif
|
|
86
|
545
|
87
|
|
88 /* let's start implementing :) */
|
495
|
89
|
545
|
90 void handleSignal(int signalID){
|
|
91 #ifdef SIGALARM
|
|
92 if( signalID==SIGALRM
|
498
|
93 #ifdef SIGXCPU
|
545
|
94 || signalID==SIGXCPU
|
498
|
95 #endif
|
545
|
96 )
|
|
97 printf("EXIT CODE: signal %d (time-out after %d seconds)", signalID, TIME_OUT);
|
|
98 else
|
|
99 #endif
|
|
100 printf("EXIT CODE: signal %d, errno %d\n", signalID, errno);
|
498
|
101
|
490
|
102 fflush(stdout);
|
|
103 fflush(stderr);
|
545
|
104
|
|
105 #ifdef USE_POSIX
|
502
|
106 kill(-pID, SIGTERM);
|
|
107 sleep(1);
|
495
|
108 kill(-pID, SIGKILL);
|
545
|
109 #else
|
|
110 #error sub processes have to be killed
|
|
111 #endif
|
490
|
112
|
545
|
113 #ifdef SIGUSR1
|
|
114 if(signalID==SIGUSR1 || signalID==SIGUSR2)
|
|
115 exit(EXIT_SUCCESS);
|
|
116 else
|
|
117 #endif
|
|
118 exit(EXIT_FAILURE);
|
|
119 }
|
|
120
|
|
121 void setupLimits(){
|
|
122 #ifdef USE_POSIX_LIMITS
|
|
123 struct rlimit limit;
|
|
124
|
|
125 limit.rlim_cur = TIME_OUT;
|
|
126 limit.rlim_max = TIME_OUT;
|
|
127 if(0!=setrlimit(RLIMIT_CPU, &limit)){
|
|
128 fprintf(stderr, "failed to set cpu limit [%d]\n", errno);
|
498
|
129 exit(EXIT_FAILURE);
|
545
|
130 }
|
|
131
|
|
132 limit.rlim_cur = MEM_LIMIT * 1024L * 1024L;
|
|
133 limit.rlim_max = MEM_LIMIT * 1024L * 1024L;
|
|
134 #ifdef RLIMIT_AS
|
|
135 if(0!=setrlimit(RLIMIT_AS, &limit)){
|
|
136 fprintf(stderr, "failed to set mem limit (AS) [%d]\n", errno);
|
|
137 exit(EXIT_FAILURE);
|
498
|
138 }
|
545
|
139 #endif
|
|
140
|
|
141 if(0!=setrlimit(RLIMIT_DATA, &limit)){
|
|
142 fprintf(stderr, "failed to set mem limit (DATA) [%d]\n", errno);
|
|
143 exit(EXIT_FAILURE);
|
|
144 }
|
|
145
|
|
146 if(0!=setrlimit(RLIMIT_RSS, &limit)){
|
|
147 fprintf(stderr, "failed to set mem limit (RSS) [%d]\n", errno);
|
|
148 exit(EXIT_FAILURE);
|
|
149 }
|
|
150 #if defined(RLIMIT_MEMLOCK) && !defined(linux)
|
|
151 if(0!=setrlimit(RLIMIT_MEMLOCK, &limit)){
|
|
152 fprintf(stderr, "failed to set mem limit (MEMLOCK) [%d]\n", errno);
|
|
153 exit(EXIT_FAILURE);
|
|
154 }
|
|
155 #endif
|
|
156 #endif /* USE_POSIX_LIMITS */
|
490
|
157 }
|
|
158
|
545
|
159 void setupHandlers(){
|
|
160 #ifdef USE_POSIX
|
|
161 #ifdef SIGHUP
|
|
162 signal(SIGHUP, &handleSignal);
|
|
163 #endif
|
|
164 signal(SIGINT, &handleSignal);
|
|
165 #ifdef SIGQUIT
|
|
166 signal(SIGQUIT, &handleSignal);
|
|
167 #endif
|
|
168 signal(SIGILL, &handleSignal);
|
|
169 #ifdef SIGTRAP
|
|
170 signal(SIGTRAP, &handleSignal);
|
|
171 #endif
|
|
172 signal(SIGABRT, &handleSignal);
|
|
173 #ifdef SIGIOT
|
|
174 signal(SIGIOT, &handleSignal);
|
|
175 #endif
|
|
176 #ifdef SIGBUS
|
|
177 signal(SIGBUS, &handleSignal);
|
|
178 #endif
|
|
179 signal(SIGFPE, &handleSignal);
|
|
180 #ifdef SIGKILL
|
|
181 signal(SIGKILL, &handleSignal);
|
|
182 #endif
|
|
183 #ifdef SIGUSR1
|
|
184 signal(SIGUSR1, &handleSignal);
|
|
185 #endif
|
|
186 signal(SIGSEGV, &handleSignal);
|
|
187 #ifdef SIGUSR2
|
|
188 signal(SIGUSR2, &handleSignal);
|
|
189 #endif
|
|
190 #ifdef SIGPIPE
|
|
191 signal(SIGPIPE, &handleSignal);
|
|
192 #endif
|
|
193 #ifdef SIGALRM
|
|
194 signal(SIGALRM, &handleSignal);
|
|
195 #endif
|
|
196 signal(SIGTERM, &handleSignal);
|
|
197 #ifdef SIGSTKFLT
|
|
198 signal(SIGSTKFLT, &handleSignal);
|
|
199 #endif
|
|
200 #ifdef SIGTSTP
|
|
201 signal(SIGTSTP, &handleSignal);
|
|
202 #endif
|
|
203 #ifdef SIGXCPU
|
|
204 signal(SIGXCPU, &handleSignal);
|
|
205 #endif
|
|
206 #ifdef SIGXFSZ
|
|
207 signal(SIGXFSZ, &handleSignal);
|
|
208 #endif
|
|
209 #ifdef SIGVTALRM
|
|
210 signal(SIGVTALRM, &handleSignal);
|
|
211 #endif
|
|
212 #ifdef SIGSYS
|
|
213 signal(SIGSYS, &handleSignal);
|
|
214 #endif
|
|
215 #endif /* USE_POSIX */
|
|
216 }
|
502
|
217
|
545
|
218 char* reconstructCmd(int argc, char** argv){
|
|
219 int cmdLen=1;
|
|
220 int i;
|
|
221 char* cmd;
|
|
222
|
|
223 for(i=0; i<argc; i++){
|
|
224 cmdLen+=strlen(argv[i]);
|
|
225 cmdLen+=3;
|
502
|
226 }
|
545
|
227
|
|
228 cmd = (char*)malloc(cmdLen);
|
|
229 *cmd = '\x00';
|
495
|
230
|
545
|
231 for(i=0; i<argc; i++){
|
|
232 strcat(cmd, "\"");
|
|
233 strcat(cmd, argv[i]);
|
|
234 strcat(cmd, "\" ");
|
|
235 }
|
|
236 return cmd;
|
|
237 }
|
490
|
238
|
545
|
239 int main(int argc, char** argv){
|
|
240 char* cmd;
|
|
241 if(argc<2){
|
|
242 fprintf(stderr, "name of command to call required\n");
|
|
243 return EXIT_FAILURE;
|
|
244 }
|
|
245 cmd = reconstructCmd(argc-1, argv+1);
|
|
246
|
|
247 #ifdef USE_POSIX
|
|
248 setupLimits();
|
|
249 pID = fork();
|
|
250 if(pID == 0){
|
|
251 /* child */
|
|
252 pID=setsid();
|
493
|
253 #ifdef DEBUG
|
545
|
254 printf("cmd[%i sec]: %s \n", TIME_OUT, cmd);
|
493
|
255 #endif
|
498
|
256 printf("EXIT CODE: %d\n", system(cmd));
|
545
|
257 }else if(pID < 0){
|
|
258 fprintf(stderr, "failed to fork\n");
|
490
|
259 return EXIT_FAILURE;
|
|
260 }else{
|
545
|
261 /* parent */
|
|
262 setupHandlers();
|
|
263 #if !(defined(USE_POSIX_LIMITS) && defined(SIGXCPU))
|
|
264 alarm(TIME_OUT);
|
502
|
265 #endif
|
490
|
266 wait(NULL);
|
|
267 }
|
|
268 return EXIT_SUCCESS;
|
498
|
269 #else /* USE_POSIX */
|
490
|
270
|
545
|
271 #error no test run implmentation present for your system
|
490
|
272
|
|
273 #endif /* USE_POSIX else */
|
545
|
274 }
|