annotate crashRun.c @ 502:a15d15eb3928

added memory limit to crashRun Anders <afb@algonet.se> 2005-05-02 mail:df2a237070cefb9f108f409d9a5e7d13@algonet.se
author thomask
date Tue, 10 May 2005 23:38:22 +0000
parents 1636b8289a73
children 7b5efee9d724
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
490
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
1 /*
502
a15d15eb3928 added memory limit to crashRun
thomask
parents: 498
diff changeset
2 * crashRun - execute command with restricted CPU time and memory usage
490
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
3 *
502
a15d15eb3928 added memory limit to crashRun
thomask
parents: 498
diff changeset
4 * Copyright (C)
a15d15eb3928 added memory limit to crashRun
thomask
parents: 498
diff changeset
5 * 2005 Thomas Kuehne <thomas@kuehne.cn>
a15d15eb3928 added memory limit to crashRun
thomask
parents: 498
diff changeset
6 * 2005 Anders F Björklund <afb@algonet.se>
490
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
7 *
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
8 * This program is free software; you can redistribute it and/or modify
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
9 * it under the terms of the GNU General Public License as published by
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
10 * the Free Software Foundation; either version 2 of the License, or
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
11 * (at your option) any later version.
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
12 *
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
13 * This program is distributed in the hope that it will be useful,
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
16 * GNU General Public License for more details.
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
17 *
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
18 * You should have received a copy of the GNU General Public License
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
19 * along with this program; if not, write to the Free Software
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
21 *
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
22 * $HeadURL$
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
23 * $Date$
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
24 * $Author$
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
25 *
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
26 */
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
27
498
1636b8289a73 correctly kill child process if killed by user
thomask
parents: 495
diff changeset
28 #include <errno.h>
1636b8289a73 correctly kill child process if killed by user
thomask
parents: 495
diff changeset
29 #include <string.h>
1636b8289a73 correctly kill child process if killed by user
thomask
parents: 495
diff changeset
30 #include <stdlib.h>
1636b8289a73 correctly kill child process if killed by user
thomask
parents: 495
diff changeset
31 #include <stdio.h>
1636b8289a73 correctly kill child process if killed by user
thomask
parents: 495
diff changeset
32
502
a15d15eb3928 added memory limit to crashRun
thomask
parents: 498
diff changeset
33 /* time-out in minutes (cpu or system time)*/
a15d15eb3928 added memory limit to crashRun
thomask
parents: 498
diff changeset
34 const int TIME_OUT=5;
498
1636b8289a73 correctly kill child process if killed by user
thomask
parents: 495
diff changeset
35
1636b8289a73 correctly kill child process if killed by user
thomask
parents: 495
diff changeset
36 #if defined(__GNU_LIBRARY__) || defined(__GLIBC__)
1636b8289a73 correctly kill child process if killed by user
thomask
parents: 495
diff changeset
37 #define USE_POSIX
1636b8289a73 correctly kill child process if killed by user
thomask
parents: 495
diff changeset
38 #endif
1636b8289a73 correctly kill child process if killed by user
thomask
parents: 495
diff changeset
39
490
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
40 #ifdef linux
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
41 #define USE_POSIX
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
42 #endif
502
a15d15eb3928 added memory limit to crashRun
thomask
parents: 498
diff changeset
43
490
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
44 #if defined(__APPLE__) && defined(__MACH__)
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
45 #define USE_POSIX
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
46 #endif
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
47
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
48 #ifdef __FreeBSD__
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
49 #define USE_POSIX
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
50 #endif
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
51
498
1636b8289a73 correctly kill child process if killed by user
thomask
parents: 495
diff changeset
52
490
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
53 #ifdef USE_POSIX
502
a15d15eb3928 added memory limit to crashRun
thomask
parents: 498
diff changeset
54 #define USE_LIMIT
490
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
55
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
56 #include <unistd.h>
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
57 #include <sys/wait.h>
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
58
502
a15d15eb3928 added memory limit to crashRun
thomask
parents: 498
diff changeset
59 #ifdef USE_LIMIT
a15d15eb3928 added memory limit to crashRun
thomask
parents: 498
diff changeset
60 #include <sys/types.h>
a15d15eb3928 added memory limit to crashRun
thomask
parents: 498
diff changeset
61 #include <sys/time.h>
a15d15eb3928 added memory limit to crashRun
thomask
parents: 498
diff changeset
62 #include <sys/resource.h>
a15d15eb3928 added memory limit to crashRun
thomask
parents: 498
diff changeset
63
a15d15eb3928 added memory limit to crashRun
thomask
parents: 498
diff changeset
64 const int PROC_LIMIT = 256; /* no processes */
a15d15eb3928 added memory limit to crashRun
thomask
parents: 498
diff changeset
65 const int MEM_LIMIT = 200; /* mem megabytes */
a15d15eb3928 added memory limit to crashRun
thomask
parents: 498
diff changeset
66 #endif
a15d15eb3928 added memory limit to crashRun
thomask
parents: 498
diff changeset
67
495
e4119df89e01 fixed timeout handing of crashRun
thomask
parents: 493
diff changeset
68 static pid_t pID;
e4119df89e01 fixed timeout handing of crashRun
thomask
parents: 493
diff changeset
69
498
1636b8289a73 correctly kill child process if killed by user
thomask
parents: 495
diff changeset
70 void resourceLimit(int signalID){
1636b8289a73 correctly kill child process if killed by user
thomask
parents: 495
diff changeset
71 #ifdef SIGXCPU
1636b8289a73 correctly kill child process if killed by user
thomask
parents: 495
diff changeset
72 if(signalID==SIGALRM || signalID==SIGXCPU){
1636b8289a73 correctly kill child process if killed by user
thomask
parents: 495
diff changeset
73 #else
1636b8289a73 correctly kill child process if killed by user
thomask
parents: 495
diff changeset
74 if(signalID==SIGALRM){
1636b8289a73 correctly kill child process if killed by user
thomask
parents: 495
diff changeset
75 #endif
502
a15d15eb3928 added memory limit to crashRun
thomask
parents: 498
diff changeset
76 printf("EXIT CODE: signal %d (time-out after %d minutes)", signalID, TIME_OUT);
498
1636b8289a73 correctly kill child process if killed by user
thomask
parents: 495
diff changeset
77 }else{
1636b8289a73 correctly kill child process if killed by user
thomask
parents: 495
diff changeset
78 printf("EXIT CODE: signal %d\n", signalID);
1636b8289a73 correctly kill child process if killed by user
thomask
parents: 495
diff changeset
79 }
1636b8289a73 correctly kill child process if killed by user
thomask
parents: 495
diff changeset
80
490
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
81 fflush(stdout);
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
82 fflush(stderr);
502
a15d15eb3928 added memory limit to crashRun
thomask
parents: 498
diff changeset
83 kill(-pID, SIGTERM);
a15d15eb3928 added memory limit to crashRun
thomask
parents: 498
diff changeset
84 sleep(1);
495
e4119df89e01 fixed timeout handing of crashRun
thomask
parents: 493
diff changeset
85 kill(-pID, SIGKILL);
490
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
86
498
1636b8289a73 correctly kill child process if killed by user
thomask
parents: 495
diff changeset
87 if(signalID==SIGUSR1 || signalID==SIGUSR2){
1636b8289a73 correctly kill child process if killed by user
thomask
parents: 495
diff changeset
88 exit(EXIT_FAILURE);
1636b8289a73 correctly kill child process if killed by user
thomask
parents: 495
diff changeset
89 }else{
1636b8289a73 correctly kill child process if killed by user
thomask
parents: 495
diff changeset
90 exit(EXIT_SUCCESS);
1636b8289a73 correctly kill child process if killed by user
thomask
parents: 495
diff changeset
91 }
490
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
92 }
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
93
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
94 int main(int argc, char** arg){
502
a15d15eb3928 added memory limit to crashRun
thomask
parents: 498
diff changeset
95 #ifdef USE_LIMIT
a15d15eb3928 added memory limit to crashRun
thomask
parents: 498
diff changeset
96 {
a15d15eb3928 added memory limit to crashRun
thomask
parents: 498
diff changeset
97 struct rlimit limit;
a15d15eb3928 added memory limit to crashRun
thomask
parents: 498
diff changeset
98
a15d15eb3928 added memory limit to crashRun
thomask
parents: 498
diff changeset
99 limit.rlim_cur = TIME_OUT * 60;
a15d15eb3928 added memory limit to crashRun
thomask
parents: 498
diff changeset
100 limit.rlim_max = TIME_OUT * 60;
a15d15eb3928 added memory limit to crashRun
thomask
parents: 498
diff changeset
101 if(0!=setrlimit(RLIMIT_CPU, &limit)){
a15d15eb3928 added memory limit to crashRun
thomask
parents: 498
diff changeset
102 fprintf(stderr, "failed to set cpu limit [%d]\n", errno);
a15d15eb3928 added memory limit to crashRun
thomask
parents: 498
diff changeset
103 return EXIT_FAILURE;
a15d15eb3928 added memory limit to crashRun
thomask
parents: 498
diff changeset
104 }
a15d15eb3928 added memory limit to crashRun
thomask
parents: 498
diff changeset
105
a15d15eb3928 added memory limit to crashRun
thomask
parents: 498
diff changeset
106 limit.rlim_cur = PROC_LIMIT;
a15d15eb3928 added memory limit to crashRun
thomask
parents: 498
diff changeset
107 limit.rlim_max = PROC_LIMIT;
a15d15eb3928 added memory limit to crashRun
thomask
parents: 498
diff changeset
108 if(0!=setrlimit(RLIMIT_NPROC, &limit)){
a15d15eb3928 added memory limit to crashRun
thomask
parents: 498
diff changeset
109 fprintf(stderr, "failed to set proc limit [%d]\n", errno);
a15d15eb3928 added memory limit to crashRun
thomask
parents: 498
diff changeset
110 return EXIT_FAILURE;
a15d15eb3928 added memory limit to crashRun
thomask
parents: 498
diff changeset
111 }
a15d15eb3928 added memory limit to crashRun
thomask
parents: 498
diff changeset
112
a15d15eb3928 added memory limit to crashRun
thomask
parents: 498
diff changeset
113 limit.rlim_cur = MEM_LIMIT * 1024L * 1024L;
a15d15eb3928 added memory limit to crashRun
thomask
parents: 498
diff changeset
114 limit.rlim_max = MEM_LIMIT * 1024L * 1024L;
a15d15eb3928 added memory limit to crashRun
thomask
parents: 498
diff changeset
115 #ifdef RLIMIT_AS
a15d15eb3928 added memory limit to crashRun
thomask
parents: 498
diff changeset
116 if(0!=setrlimit(RLIMIT_AS, &limit)){
a15d15eb3928 added memory limit to crashRun
thomask
parents: 498
diff changeset
117 fprintf(stderr, "failed to set mem limit (AS) [%d]\n", errno);
a15d15eb3928 added memory limit to crashRun
thomask
parents: 498
diff changeset
118 return EXIT_FAILURE;
a15d15eb3928 added memory limit to crashRun
thomask
parents: 498
diff changeset
119 }
a15d15eb3928 added memory limit to crashRun
thomask
parents: 498
diff changeset
120 #endif /* RLIMIT_AS */
a15d15eb3928 added memory limit to crashRun
thomask
parents: 498
diff changeset
121
a15d15eb3928 added memory limit to crashRun
thomask
parents: 498
diff changeset
122 if(0!=setrlimit(RLIMIT_DATA, &limit)){
a15d15eb3928 added memory limit to crashRun
thomask
parents: 498
diff changeset
123 fprintf(stderr, "failed to set mem limit (DATA) [%d]\n", errno);
a15d15eb3928 added memory limit to crashRun
thomask
parents: 498
diff changeset
124 return EXIT_FAILURE;
a15d15eb3928 added memory limit to crashRun
thomask
parents: 498
diff changeset
125 }
a15d15eb3928 added memory limit to crashRun
thomask
parents: 498
diff changeset
126
a15d15eb3928 added memory limit to crashRun
thomask
parents: 498
diff changeset
127 if(0!=setrlimit(RLIMIT_RSS, &limit)){
a15d15eb3928 added memory limit to crashRun
thomask
parents: 498
diff changeset
128 fprintf(stderr, "failed to set mem limit (RSS) [%d]\n", errno);
a15d15eb3928 added memory limit to crashRun
thomask
parents: 498
diff changeset
129 return EXIT_FAILURE;
a15d15eb3928 added memory limit to crashRun
thomask
parents: 498
diff changeset
130 }
a15d15eb3928 added memory limit to crashRun
thomask
parents: 498
diff changeset
131 #if defined(RLIMIT_MEMLOCK) && !defined(linux)
a15d15eb3928 added memory limit to crashRun
thomask
parents: 498
diff changeset
132 if(0!=setrlimit(RLIMIT_MEMLOCK, &limit)){
a15d15eb3928 added memory limit to crashRun
thomask
parents: 498
diff changeset
133 fprintf(stderr, "failed to set mem limit (MEMLOCK) [%d]\n", errno);
a15d15eb3928 added memory limit to crashRun
thomask
parents: 498
diff changeset
134 return EXIT_FAILURE;
a15d15eb3928 added memory limit to crashRun
thomask
parents: 498
diff changeset
135 }
a15d15eb3928 added memory limit to crashRun
thomask
parents: 498
diff changeset
136 #endif /* RLIMIT_MEMLOCK */
a15d15eb3928 added memory limit to crashRun
thomask
parents: 498
diff changeset
137 }
a15d15eb3928 added memory limit to crashRun
thomask
parents: 498
diff changeset
138 #endif /* USE_LIMIT */
495
e4119df89e01 fixed timeout handing of crashRun
thomask
parents: 493
diff changeset
139 pID = fork();
490
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
140 if (pID == 0){
498
1636b8289a73 correctly kill child process if killed by user
thomask
parents: 495
diff changeset
141 /* child: resource management */
495
e4119df89e01 fixed timeout handing of crashRun
thomask
parents: 493
diff changeset
142 pID=setsid();
e4119df89e01 fixed timeout handing of crashRun
thomask
parents: 493
diff changeset
143
498
1636b8289a73 correctly kill child process if killed by user
thomask
parents: 495
diff changeset
144 /* child: execute args */
490
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
145 int cmdLen=1;
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
146 int i;
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
147 for(i=1; i<argc; i++){
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
148 cmdLen+=strlen(arg[i]);
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
149 cmdLen+=3;
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
150 }
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
151
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
152 char* cmd = malloc(cmdLen);
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
153 *cmd='\x00';
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
154
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
155 for(i=1; i<argc; i++){
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
156 strcat(cmd, "\"");
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
157 strcat(cmd, arg[i]);
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
158 strcat(cmd, "\" ");
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
159 }
493
1418f225a81e disabled debug output
thomask
parents: 490
diff changeset
160 #ifdef DEBUG
502
a15d15eb3928 added memory limit to crashRun
thomask
parents: 498
diff changeset
161 printf("cmd[%i min]: %s\n", TIME_OUT, cmd);
493
1418f225a81e disabled debug output
thomask
parents: 490
diff changeset
162 #endif
498
1636b8289a73 correctly kill child process if killed by user
thomask
parents: 495
diff changeset
163 printf("EXIT CODE: %d\n", system(cmd));
490
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
164 }else if (pID < 0){
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
165 fprintf(stderr, "failed to fork\n");
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
166 return EXIT_FAILURE;
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
167 }else{
498
1636b8289a73 correctly kill child process if killed by user
thomask
parents: 495
diff changeset
168 /* parent : clean kill */
490
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
169 struct sigaction acti;
498
1636b8289a73 correctly kill child process if killed by user
thomask
parents: 495
diff changeset
170 acti.sa_handler = &resourceLimit;
1636b8289a73 correctly kill child process if killed by user
thomask
parents: 495
diff changeset
171 sigaction(SIGHUP, &acti, NULL);
1636b8289a73 correctly kill child process if killed by user
thomask
parents: 495
diff changeset
172 sigaction(SIGINT, &acti, NULL);
1636b8289a73 correctly kill child process if killed by user
thomask
parents: 495
diff changeset
173 sigaction(SIGQUIT, &acti, NULL);
1636b8289a73 correctly kill child process if killed by user
thomask
parents: 495
diff changeset
174 sigaction(SIGABRT, &acti, NULL);
1636b8289a73 correctly kill child process if killed by user
thomask
parents: 495
diff changeset
175 sigaction(SIGTERM, &acti, NULL);
1636b8289a73 correctly kill child process if killed by user
thomask
parents: 495
diff changeset
176
1636b8289a73 correctly kill child process if killed by user
thomask
parents: 495
diff changeset
177 /* parent : timeout */
490
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
178 if(0!=sigaction(SIGALRM, &acti, NULL)){
502
a15d15eb3928 added memory limit to crashRun
thomask
parents: 498
diff changeset
179 fprintf(stderr, "failed to set timeout [%d]\n", errno);
498
1636b8289a73 correctly kill child process if killed by user
thomask
parents: 495
diff changeset
180 resourceLimit(SIGUSR1);
1636b8289a73 correctly kill child process if killed by user
thomask
parents: 495
diff changeset
181 return EXIT_FAILURE; /* never executed */
502
a15d15eb3928 added memory limit to crashRun
thomask
parents: 498
diff changeset
182 }
a15d15eb3928 added memory limit to crashRun
thomask
parents: 498
diff changeset
183 #ifndef USE_LIMITS
a15d15eb3928 added memory limit to crashRun
thomask
parents: 498
diff changeset
184 alarm(TIME_OUT * 60);
a15d15eb3928 added memory limit to crashRun
thomask
parents: 498
diff changeset
185 #endif
490
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
186 wait(NULL);
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
187 }
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
188
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
189 return EXIT_SUCCESS;
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
190 }
498
1636b8289a73 correctly kill child process if killed by user
thomask
parents: 495
diff changeset
191 #else /* USE_POSIX */
490
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
192
498
1636b8289a73 correctly kill child process if killed by user
thomask
parents: 495
diff changeset
193 #error "no implementation present for your OS"
490
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
194
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
195 #endif /* USE_POSIX else */