490
|
1 /*
|
502
|
2 * crashRun - execute command with restricted CPU time and memory usage
|
490
|
3 *
|
502
|
4 * Copyright (C)
|
|
5 * 2005 Thomas Kuehne <thomas@kuehne.cn>
|
|
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>
|
|
32
|
502
|
33 /* time-out in minutes (cpu or system time)*/
|
|
34 const int TIME_OUT=5;
|
498
|
35
|
|
36 #if defined(__GNU_LIBRARY__) || defined(__GLIBC__)
|
|
37 #define USE_POSIX
|
|
38 #endif
|
|
39
|
490
|
40 #ifdef linux
|
|
41 #define USE_POSIX
|
|
42 #endif
|
502
|
43
|
490
|
44 #if defined(__APPLE__) && defined(__MACH__)
|
|
45 #define USE_POSIX
|
|
46 #endif
|
|
47
|
|
48 #ifdef __FreeBSD__
|
|
49 #define USE_POSIX
|
|
50 #endif
|
|
51
|
498
|
52
|
490
|
53 #ifdef USE_POSIX
|
502
|
54 #define USE_LIMIT
|
490
|
55
|
|
56 #include <unistd.h>
|
|
57 #include <sys/wait.h>
|
|
58
|
502
|
59 #ifdef USE_LIMIT
|
|
60 #include <sys/types.h>
|
|
61 #include <sys/time.h>
|
|
62 #include <sys/resource.h>
|
|
63
|
|
64 const int PROC_LIMIT = 256; /* no processes */
|
|
65 const int MEM_LIMIT = 200; /* mem megabytes */
|
|
66 #endif
|
|
67
|
495
|
68 static pid_t pID;
|
|
69
|
498
|
70 void resourceLimit(int signalID){
|
|
71 #ifdef SIGXCPU
|
|
72 if(signalID==SIGALRM || signalID==SIGXCPU){
|
|
73 #else
|
|
74 if(signalID==SIGALRM){
|
|
75 #endif
|
502
|
76 printf("EXIT CODE: signal %d (time-out after %d minutes)", signalID, TIME_OUT);
|
498
|
77 }else{
|
|
78 printf("EXIT CODE: signal %d\n", signalID);
|
|
79 }
|
|
80
|
490
|
81 fflush(stdout);
|
|
82 fflush(stderr);
|
502
|
83 kill(-pID, SIGTERM);
|
|
84 sleep(1);
|
495
|
85 kill(-pID, SIGKILL);
|
490
|
86
|
498
|
87 if(signalID==SIGUSR1 || signalID==SIGUSR2){
|
|
88 exit(EXIT_FAILURE);
|
|
89 }else{
|
|
90 exit(EXIT_SUCCESS);
|
|
91 }
|
490
|
92 }
|
|
93
|
|
94 int main(int argc, char** arg){
|
502
|
95 #ifdef USE_LIMIT
|
|
96 {
|
|
97 struct rlimit limit;
|
|
98
|
|
99 limit.rlim_cur = TIME_OUT * 60;
|
|
100 limit.rlim_max = TIME_OUT * 60;
|
|
101 if(0!=setrlimit(RLIMIT_CPU, &limit)){
|
|
102 fprintf(stderr, "failed to set cpu limit [%d]\n", errno);
|
|
103 return EXIT_FAILURE;
|
|
104 }
|
|
105
|
|
106 limit.rlim_cur = PROC_LIMIT;
|
|
107 limit.rlim_max = PROC_LIMIT;
|
|
108 if(0!=setrlimit(RLIMIT_NPROC, &limit)){
|
|
109 fprintf(stderr, "failed to set proc limit [%d]\n", errno);
|
|
110 return EXIT_FAILURE;
|
|
111 }
|
|
112
|
|
113 limit.rlim_cur = MEM_LIMIT * 1024L * 1024L;
|
|
114 limit.rlim_max = MEM_LIMIT * 1024L * 1024L;
|
|
115 #ifdef RLIMIT_AS
|
|
116 if(0!=setrlimit(RLIMIT_AS, &limit)){
|
|
117 fprintf(stderr, "failed to set mem limit (AS) [%d]\n", errno);
|
|
118 return EXIT_FAILURE;
|
|
119 }
|
|
120 #endif /* RLIMIT_AS */
|
|
121
|
|
122 if(0!=setrlimit(RLIMIT_DATA, &limit)){
|
|
123 fprintf(stderr, "failed to set mem limit (DATA) [%d]\n", errno);
|
|
124 return EXIT_FAILURE;
|
|
125 }
|
|
126
|
|
127 if(0!=setrlimit(RLIMIT_RSS, &limit)){
|
|
128 fprintf(stderr, "failed to set mem limit (RSS) [%d]\n", errno);
|
|
129 return EXIT_FAILURE;
|
|
130 }
|
|
131 #if defined(RLIMIT_MEMLOCK) && !defined(linux)
|
|
132 if(0!=setrlimit(RLIMIT_MEMLOCK, &limit)){
|
|
133 fprintf(stderr, "failed to set mem limit (MEMLOCK) [%d]\n", errno);
|
|
134 return EXIT_FAILURE;
|
|
135 }
|
|
136 #endif /* RLIMIT_MEMLOCK */
|
|
137 }
|
|
138 #endif /* USE_LIMIT */
|
495
|
139 pID = fork();
|
490
|
140 if (pID == 0){
|
498
|
141 /* child: resource management */
|
495
|
142 pID=setsid();
|
|
143
|
498
|
144 /* child: execute args */
|
490
|
145 int cmdLen=1;
|
|
146 int i;
|
|
147 for(i=1; i<argc; i++){
|
|
148 cmdLen+=strlen(arg[i]);
|
|
149 cmdLen+=3;
|
|
150 }
|
|
151
|
|
152 char* cmd = malloc(cmdLen);
|
|
153 *cmd='\x00';
|
|
154
|
|
155 for(i=1; i<argc; i++){
|
|
156 strcat(cmd, "\"");
|
|
157 strcat(cmd, arg[i]);
|
|
158 strcat(cmd, "\" ");
|
|
159 }
|
493
|
160 #ifdef DEBUG
|
502
|
161 printf("cmd[%i min]: %s\n", TIME_OUT, cmd);
|
493
|
162 #endif
|
498
|
163 printf("EXIT CODE: %d\n", system(cmd));
|
490
|
164 }else if (pID < 0){
|
|
165 fprintf(stderr, "failed to fork\n");
|
|
166 return EXIT_FAILURE;
|
|
167 }else{
|
498
|
168 /* parent : clean kill */
|
490
|
169 struct sigaction acti;
|
498
|
170 acti.sa_handler = &resourceLimit;
|
|
171 sigaction(SIGHUP, &acti, NULL);
|
|
172 sigaction(SIGINT, &acti, NULL);
|
|
173 sigaction(SIGQUIT, &acti, NULL);
|
|
174 sigaction(SIGABRT, &acti, NULL);
|
|
175 sigaction(SIGTERM, &acti, NULL);
|
|
176
|
|
177 /* parent : timeout */
|
490
|
178 if(0!=sigaction(SIGALRM, &acti, NULL)){
|
502
|
179 fprintf(stderr, "failed to set timeout [%d]\n", errno);
|
498
|
180 resourceLimit(SIGUSR1);
|
|
181 return EXIT_FAILURE; /* never executed */
|
502
|
182 }
|
|
183 #ifndef USE_LIMITS
|
|
184 alarm(TIME_OUT * 60);
|
|
185 #endif
|
490
|
186 wait(NULL);
|
|
187 }
|
|
188
|
|
189 return EXIT_SUCCESS;
|
|
190 }
|
498
|
191 #else /* USE_POSIX */
|
490
|
192
|
498
|
193 #error "no implementation present for your OS"
|
490
|
194
|
|
195 #endif /* USE_POSIX else */
|