annotate crashRun.c @ 923:bcba57cc349c

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