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>
|
684
|
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
|
684
|
42 #if defined(linux) || defined(__FreeBsd__) || defined(__OpenBSD__)
|
490
|
43 #define USE_POSIX
|
|
44 #endif
|
502
|
45
|
490
|
46 #if defined(__APPLE__) && defined(__MACH__)
|
|
47 #define USE_POSIX
|
|
48 #endif
|
|
49
|
545
|
50 #if !defined(USE_POSIX) && \
|
|
51 (defined(WINDOWS) || defined(WIN) || defined(WINVER) || defined(WIN32))
|
|
52 #define USE_WINDOWS
|
|
53 #endif
|
498
|
54
|
545
|
55
|
|
56 /* is the environment sane? */
|
|
57 #if defined(USE_POSIX) && defined(USE_WINDOWS)
|
|
58 #error USE_WINDOWS and USE_POSIX are defined
|
|
59 #endif
|
|
60
|
|
61 #if !defined(USE_POSIX) && !defined(USE_WINDOWS)
|
|
62 #error neither USE_POSIX nor USE_WINDOWS are defined
|
|
63 #endif
|
|
64
|
|
65
|
|
66 /* API inludes and config */
|
490
|
67 #ifdef USE_POSIX
|
545
|
68 #define USE_POSIX_LIMITS
|
|
69 #include <sys/types.h>
|
490
|
70 #include <unistd.h>
|
|
71 #include <sys/wait.h>
|
545
|
72 static pid_t pID;
|
|
73 #endif
|
490
|
74
|
545
|
75 #ifdef USE_POSIX_LIMITS
|
|
76 #ifndef USE_POSIX
|
|
77 #error USE_POSIX_LIMITS requires USE_POSIX
|
|
78 #endif
|
|
79 #include <sys/resource.h>
|
502
|
80 #include <sys/types.h>
|
|
81 #endif
|
|
82
|
545
|
83
|
|
84 /* let's start implementing :) */
|
495
|
85
|
545
|
86 void handleSignal(int signalID){
|
|
87 #ifdef SIGALARM
|
|
88 if( signalID==SIGALRM
|
498
|
89 #ifdef SIGXCPU
|
545
|
90 || signalID==SIGXCPU
|
498
|
91 #endif
|
545
|
92 )
|
|
93 printf("EXIT CODE: signal %d (time-out after %d seconds)", signalID, TIME_OUT);
|
|
94 else
|
|
95 #endif
|
|
96 printf("EXIT CODE: signal %d, errno %d\n", signalID, errno);
|
498
|
97
|
490
|
98 fflush(stdout);
|
|
99 fflush(stderr);
|
545
|
100
|
|
101 #ifdef USE_POSIX
|
502
|
102 kill(-pID, SIGTERM);
|
|
103 sleep(1);
|
495
|
104 kill(-pID, SIGKILL);
|
545
|
105 #else
|
|
106 #error sub processes have to be killed
|
|
107 #endif
|
490
|
108
|
545
|
109 #ifdef SIGUSR1
|
|
110 if(signalID==SIGUSR1 || signalID==SIGUSR2)
|
|
111 exit(EXIT_SUCCESS);
|
|
112 else
|
|
113 #endif
|
|
114 exit(EXIT_FAILURE);
|
|
115 }
|
|
116
|
|
117 void setupLimits(){
|
|
118 #ifdef USE_POSIX_LIMITS
|
|
119 struct rlimit limit;
|
|
120
|
|
121 limit.rlim_cur = TIME_OUT;
|
|
122 limit.rlim_max = TIME_OUT;
|
|
123 if(0!=setrlimit(RLIMIT_CPU, &limit)){
|
|
124 fprintf(stderr, "failed to set cpu limit [%d]\n", errno);
|
498
|
125 exit(EXIT_FAILURE);
|
545
|
126 }
|
|
127
|
|
128 limit.rlim_cur = MEM_LIMIT * 1024L * 1024L;
|
|
129 limit.rlim_max = MEM_LIMIT * 1024L * 1024L;
|
|
130 #ifdef RLIMIT_AS
|
|
131 if(0!=setrlimit(RLIMIT_AS, &limit)){
|
|
132 fprintf(stderr, "failed to set mem limit (AS) [%d]\n", errno);
|
|
133 exit(EXIT_FAILURE);
|
498
|
134 }
|
545
|
135 #endif
|
|
136
|
|
137 if(0!=setrlimit(RLIMIT_DATA, &limit)){
|
|
138 fprintf(stderr, "failed to set mem limit (DATA) [%d]\n", errno);
|
|
139 exit(EXIT_FAILURE);
|
|
140 }
|
|
141
|
|
142 if(0!=setrlimit(RLIMIT_RSS, &limit)){
|
|
143 fprintf(stderr, "failed to set mem limit (RSS) [%d]\n", errno);
|
|
144 exit(EXIT_FAILURE);
|
|
145 }
|
|
146 #if defined(RLIMIT_MEMLOCK) && !defined(linux)
|
|
147 if(0!=setrlimit(RLIMIT_MEMLOCK, &limit)){
|
|
148 fprintf(stderr, "failed to set mem limit (MEMLOCK) [%d]\n", errno);
|
|
149 exit(EXIT_FAILURE);
|
|
150 }
|
|
151 #endif
|
|
152 #endif /* USE_POSIX_LIMITS */
|
490
|
153 }
|
|
154
|
545
|
155 void setupHandlers(){
|
|
156 #ifdef USE_POSIX
|
|
157 #ifdef SIGHUP
|
|
158 signal(SIGHUP, &handleSignal);
|
|
159 #endif
|
|
160 signal(SIGINT, &handleSignal);
|
|
161 #ifdef SIGQUIT
|
|
162 signal(SIGQUIT, &handleSignal);
|
|
163 #endif
|
|
164 signal(SIGILL, &handleSignal);
|
|
165 #ifdef SIGTRAP
|
|
166 signal(SIGTRAP, &handleSignal);
|
|
167 #endif
|
|
168 signal(SIGABRT, &handleSignal);
|
|
169 #ifdef SIGIOT
|
|
170 signal(SIGIOT, &handleSignal);
|
|
171 #endif
|
|
172 #ifdef SIGBUS
|
|
173 signal(SIGBUS, &handleSignal);
|
|
174 #endif
|
|
175 signal(SIGFPE, &handleSignal);
|
|
176 #ifdef SIGKILL
|
|
177 signal(SIGKILL, &handleSignal);
|
|
178 #endif
|
|
179 #ifdef SIGUSR1
|
|
180 signal(SIGUSR1, &handleSignal);
|
|
181 #endif
|
|
182 signal(SIGSEGV, &handleSignal);
|
|
183 #ifdef SIGUSR2
|
|
184 signal(SIGUSR2, &handleSignal);
|
|
185 #endif
|
|
186 #ifdef SIGPIPE
|
|
187 signal(SIGPIPE, &handleSignal);
|
|
188 #endif
|
|
189 #ifdef SIGALRM
|
|
190 signal(SIGALRM, &handleSignal);
|
|
191 #endif
|
|
192 signal(SIGTERM, &handleSignal);
|
|
193 #ifdef SIGSTKFLT
|
|
194 signal(SIGSTKFLT, &handleSignal);
|
|
195 #endif
|
|
196 #ifdef SIGTSTP
|
|
197 signal(SIGTSTP, &handleSignal);
|
|
198 #endif
|
|
199 #ifdef SIGXCPU
|
|
200 signal(SIGXCPU, &handleSignal);
|
|
201 #endif
|
|
202 #ifdef SIGXFSZ
|
|
203 signal(SIGXFSZ, &handleSignal);
|
|
204 #endif
|
|
205 #ifdef SIGVTALRM
|
|
206 signal(SIGVTALRM, &handleSignal);
|
|
207 #endif
|
|
208 #ifdef SIGSYS
|
|
209 signal(SIGSYS, &handleSignal);
|
|
210 #endif
|
|
211 #endif /* USE_POSIX */
|
|
212 }
|
502
|
213
|
545
|
214 char* reconstructCmd(int argc, char** argv){
|
684
|
215 size_t cmdLen=1;
|
|
216 size_t tmpLen;
|
|
217 size_t i;
|
545
|
218 char* cmd;
|
684
|
219 char* tmp;
|
545
|
220
|
|
221 for(i=0; i<argc; i++){
|
|
222 cmdLen+=strlen(argv[i]);
|
|
223 cmdLen+=3;
|
502
|
224 }
|
545
|
225
|
|
226 cmd = (char*)malloc(cmdLen);
|
|
227 *cmd = '\x00';
|
684
|
228 tmp = cmd;
|
495
|
229
|
545
|
230 for(i=0; i<argc; i++){
|
684
|
231 tmpLen = snprintf(tmp, cmdLen, "\"%s\" ", argv[i]);
|
|
232 tmp += tmpLen;
|
|
233 cmdLen -= tmpLen;
|
545
|
234 }
|
|
235 return cmd;
|
|
236 }
|
490
|
237
|
545
|
238 int main(int argc, char** argv){
|
|
239 char* cmd;
|
|
240 if(argc<2){
|
|
241 fprintf(stderr, "name of command to call required\n");
|
|
242 return EXIT_FAILURE;
|
|
243 }
|
|
244 cmd = reconstructCmd(argc-1, argv+1);
|
|
245
|
|
246 #ifdef USE_POSIX
|
|
247 setupLimits();
|
|
248 pID = fork();
|
|
249 if(pID == 0){
|
|
250 /* child */
|
|
251 pID=setsid();
|
493
|
252 #ifdef DEBUG
|
545
|
253 printf("cmd[%i sec]: %s \n", TIME_OUT, cmd);
|
493
|
254 #endif
|
498
|
255 printf("EXIT CODE: %d\n", system(cmd));
|
545
|
256 }else if(pID < 0){
|
|
257 fprintf(stderr, "failed to fork\n");
|
490
|
258 return EXIT_FAILURE;
|
|
259 }else{
|
545
|
260 /* parent */
|
|
261 setupHandlers();
|
|
262 #if !(defined(USE_POSIX_LIMITS) && defined(SIGXCPU))
|
|
263 alarm(TIME_OUT);
|
502
|
264 #endif
|
490
|
265 wait(NULL);
|
|
266 }
|
|
267 return EXIT_SUCCESS;
|
498
|
268 #else /* USE_POSIX */
|
490
|
269
|
545
|
270 #error no test run implmentation present for your system
|
490
|
271
|
|
272 #endif /* USE_POSIX else */
|
545
|
273 }
|