annotate crashRun.c @ 1630:d0efa3ae5522 default tip

run/mini/naked_asm5: New x86_64 ABI passes the arguments in reverse order.
author David Nadlinger <code@klickverbot.at>
date Sat, 23 Apr 2011 22:57:32 +0200
parents 37d80331f28f
children
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)
940
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
5 * 2005, 2006 Thomas Kuehne <thomas@kuehne.cn>
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
6 * 2005 Anders F Björrklund <afb@algonet.se> (BSD)
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
7 * 2005 Stewart Gordon <smjg_1998@yahoo.com> (Windows)
490
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
8 *
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
9 * This program is free software; you can redistribute it and/or modify
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
10 * it under the terms of the GNU General Public License as published by
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
11 * the Free Software Foundation; either version 2 of the License, or
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
12 * (at your option) any later version.
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
13 *
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
14 * This program is distributed in the hope that it will be useful,
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
17 * GNU General Public License for more details.
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
18 *
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
19 * You should have received a copy of the GNU General Public License
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
20 * along with this program; if not, write to the Free Software
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
22 *
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
23 * $HeadURL$
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
24 * $Date$
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
25 * $Author$
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
26 *
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
27 */
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
28
498
1636b8289a73 correctly kill child process if killed by user
thomask
parents: 495
diff changeset
29 #include <errno.h>
1636b8289a73 correctly kill child process if killed by user
thomask
parents: 495
diff changeset
30 #include <string.h>
1636b8289a73 correctly kill child process if killed by user
thomask
parents: 495
diff changeset
31 #include <stdlib.h>
1636b8289a73 correctly kill child process if killed by user
thomask
parents: 495
diff changeset
32 #include <stdio.h>
545
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
33 #include <signal.h>
498
1636b8289a73 correctly kill child process if killed by user
thomask
parents: 495
diff changeset
34
940
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
35 #undef WAIT_GUARD
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
36 #define WAIT_GUARD 3
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
37
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
38 /* time-out in seconds
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
39 * Posix: cpu time(timeOut) and system time(timeOut * WAIT_GUARD)
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
40 * Windows: system time(timeOut)
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
41 */
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
42 long int timeOut;
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
43
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
44 /* max memory usage in megabytes (Posix only) */
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
45 long int memoryLimit;
498
1636b8289a73 correctly kill child process if killed by user
thomask
parents: 495
diff changeset
46
716
528649416e9d infrastructure maintenance
thomask
parents: 684
diff changeset
47 /* identify system API */
940
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
48 #if defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE) || defined(_BSD_SOURCE) || defined(_SVID_SOURCE)
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
49 #define USE_POSIX 1
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
50 #endif
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
51
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
52 #if defined(__GNU_LIBRARY__) || defined(__GLIBC__)
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
53 #define USE_POSIX 1
498
1636b8289a73 correctly kill child process if killed by user
thomask
parents: 495
diff changeset
54 #endif
1636b8289a73 correctly kill child process if killed by user
thomask
parents: 495
diff changeset
55
684
369c59b10266 ported crasRun to OpenBSD
thomask
parents: 545
diff changeset
56 #if defined(linux) || defined(__FreeBsd__) || defined(__OpenBSD__)
940
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
57 #define USE_POSIX 1
490
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
58 #endif
502
a15d15eb3928 added memory limit to crashRun
thomask
parents: 498
diff changeset
59
490
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
60 #if defined(__APPLE__) && defined(__MACH__)
940
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
61 #define USE_POSIX 1
490
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
62 #endif
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
63
545
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
64 #if !defined(USE_POSIX) && \
940
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
65 (defined(WINDOWS) || defined(WIN) || defined(WINVER) || defined(WIN32) \
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
66 || defined(WIN64))
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
67 #define USE_WINDOWS 1
545
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
68 #endif
498
1636b8289a73 correctly kill child process if killed by user
thomask
parents: 495
diff changeset
69
545
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
70
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
71 /* is the environment sane? */
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
72 #if defined(USE_POSIX) && defined(USE_WINDOWS)
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
73 #error USE_WINDOWS and USE_POSIX are defined
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
74 #endif
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
75
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
76 #if !defined(USE_POSIX) && !defined(USE_WINDOWS)
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
77 #error neither USE_POSIX nor USE_WINDOWS are defined
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
78 #endif
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
79
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
80
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
81 /* API inludes and config */
490
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
82 #ifdef USE_POSIX
545
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
83 #define USE_POSIX_LIMITS
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
84 #include <sys/types.h>
490
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
85 #include <unistd.h>
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
86 #include <sys/wait.h>
545
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
87 static pid_t pID;
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
88 #endif
490
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
89
545
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
90 #ifdef USE_POSIX_LIMITS
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
91 #ifndef USE_POSIX
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
92 #error USE_POSIX_LIMITS requires USE_POSIX
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
93 #endif
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
94 #include <sys/resource.h>
502
a15d15eb3928 added memory limit to crashRun
thomask
parents: 498
diff changeset
95 #include <sys/types.h>
a15d15eb3928 added memory limit to crashRun
thomask
parents: 498
diff changeset
96 #endif
a15d15eb3928 added memory limit to crashRun
thomask
parents: 498
diff changeset
97
940
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
98 #ifdef USE_WINDOWS
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
99 #include <windows.h>
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
100 #define snprintf _snprintf
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
101 #endif
545
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
102
940
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
103
923
bcba57cc349c removed uncatchable signal action
thomask
parents: 745
diff changeset
104
940
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
105 #ifdef USE_POSIX
495
e4119df89e01 fixed timeout handing of crashRun
thomask
parents: 493
diff changeset
106
545
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
107 void handleSignal(int signalID){
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
108 if( signalID==SIGALRM
498
1636b8289a73 correctly kill child process if killed by user
thomask
parents: 495
diff changeset
109 #ifdef SIGXCPU
545
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
110 || signalID==SIGXCPU
498
1636b8289a73 correctly kill child process if killed by user
thomask
parents: 495
diff changeset
111 #endif
545
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
112 )
940
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
113 printf("EXIT CODE: signal %d (time-out after CPU/total %li/%li seconds)\n",
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
114 signalID, timeOut, timeOut * WAIT_GUARD);
545
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
115 else
940
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
116 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
117
490
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
118 fflush(stdout);
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
119 fflush(stderr);
545
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
120
502
a15d15eb3928 added memory limit to crashRun
thomask
parents: 498
diff changeset
121 kill(-pID, SIGTERM);
a15d15eb3928 added memory limit to crashRun
thomask
parents: 498
diff changeset
122 sleep(1);
495
e4119df89e01 fixed timeout handing of crashRun
thomask
parents: 493
diff changeset
123 kill(-pID, SIGKILL);
490
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
124
545
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
125 exit(EXIT_FAILURE);
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
126 }
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
127
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
128 void setupLimits(){
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
129 struct rlimit limit;
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
130
940
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
131 limit.rlim_cur = timeOut;
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
132 limit.rlim_max = timeOut;
545
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
133 if(0!=setrlimit(RLIMIT_CPU, &limit)){
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
134 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
135 exit(EXIT_FAILURE);
545
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
136 }
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
137
940
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
138 limit.rlim_cur = memoryLimit * 1024L * 1024L;
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
139 limit.rlim_max = memoryLimit * 1024L * 1024L;
545
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
140 #ifdef RLIMIT_AS
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
141 if(0!=setrlimit(RLIMIT_AS, &limit)){
940
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
142 fprintf(stderr, "failed to set mem limit (AS) %s [%d]\n", strerror(errno), errno);
545
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
143 exit(EXIT_FAILURE);
498
1636b8289a73 correctly kill child process if killed by user
thomask
parents: 495
diff changeset
144 }
545
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
145 #endif
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
146
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
147 if(0!=setrlimit(RLIMIT_DATA, &limit)){
940
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
148 fprintf(stderr, "failed to set mem limit (DATA) %s [%d]\n", strerror(errno), errno);
545
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
149 exit(EXIT_FAILURE);
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
150 }
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
151
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
152 if(0!=setrlimit(RLIMIT_RSS, &limit)){
940
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
153 fprintf(stderr, "failed to set mem limit (RSS) %s [%d]\n", strerror(errno), errno);
545
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
154 exit(EXIT_FAILURE);
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
155 }
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
156 #if defined(RLIMIT_MEMLOCK) && !defined(linux)
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
157 if(0!=setrlimit(RLIMIT_MEMLOCK, &limit)){
940
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
158 fprintf(stderr, "failed to set mem limit (MEMLOCK): %s [%d]\n", strerror(errno), errno);
545
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
159 exit(EXIT_FAILURE);
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
160 }
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
161 #endif
490
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
162 }
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
163
545
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
164 void setupHandlers(){
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
165 #ifdef SIGHUP
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
166 signal(SIGHUP, &handleSignal);
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
167 #endif
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
168 signal(SIGINT, &handleSignal);
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
169 #ifdef SIGQUIT
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
170 signal(SIGQUIT, &handleSignal);
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
171 #endif
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
172 signal(SIGILL, &handleSignal);
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
173 #ifdef SIGTRAP
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
174 signal(SIGTRAP, &handleSignal);
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
175 #endif
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
176 signal(SIGABRT, &handleSignal);
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
177 #ifdef SIGIOT
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
178 signal(SIGIOT, &handleSignal);
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
179 #endif
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
180 #ifdef SIGBUS
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
181 signal(SIGBUS, &handleSignal);
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
182 #endif
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
183 signal(SIGFPE, &handleSignal);
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
184 #ifdef SIGUSR1
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
185 signal(SIGUSR1, &handleSignal);
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
186 #endif
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
187 signal(SIGSEGV, &handleSignal);
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
188 #ifdef SIGUSR2
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
189 signal(SIGUSR2, &handleSignal);
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
190 #endif
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
191 #ifdef SIGPIPE
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
192 signal(SIGPIPE, &handleSignal);
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
193 #endif
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
194 signal(SIGALRM, &handleSignal);
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
195 signal(SIGTERM, &handleSignal);
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
196 #ifdef SIGSTKFLT
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
197 signal(SIGSTKFLT, &handleSignal);
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
198 #endif
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
199 #ifdef SIGTSTP
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
200 signal(SIGTSTP, &handleSignal);
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
201 #endif
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
202 #ifdef SIGXCPU
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
203 signal(SIGXCPU, &handleSignal);
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
204 #endif
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
205 #ifdef SIGXFSZ
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
206 signal(SIGXFSZ, &handleSignal);
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
207 #endif
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
208 #ifdef SIGVTALRM
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
209 signal(SIGVTALRM, &handleSignal);
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
210 #endif
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
211 #ifdef SIGSYS
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
212 signal(SIGSYS, &handleSignal);
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
213 #endif
940
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
214 }
545
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
215 #endif /* USE_POSIX */
923
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;
940
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
222 char* cmd;
545
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
223
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
224 for(i=0; i<argc; i++){
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
225 cmdLen+=strlen(argv[i]);
971
8da09834526d * Valgrind support is now part of dstress.c instead of crashRun.c
thomask
parents: 969
diff changeset
226 cmdLen+=1;
502
a15d15eb3928 added memory limit to crashRun
thomask
parents: 498
diff changeset
227 }
545
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
228
1205
37d80331f28f fixed crashRun.c for C++ compilers
thomask
parents: 971
diff changeset
229 cmd = (char*) malloc(++cmdLen);
940
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
230 if(!cmd){
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
231 fprintf(stderr, "failed to allocate enough memory");
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
232 exit(EXIT_FAILURE);
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
233 }
923
bcba57cc349c removed uncatchable signal action
thomask
parents: 745
diff changeset
234
545
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
235 *cmd = '\x00';
684
369c59b10266 ported crasRun to OpenBSD
thomask
parents: 545
diff changeset
236 tmp = cmd;
495
e4119df89e01 fixed timeout handing of crashRun
thomask
parents: 493
diff changeset
237
545
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
238 for(i=0; i<argc; i++){
971
8da09834526d * Valgrind support is now part of dstress.c instead of crashRun.c
thomask
parents: 969
diff changeset
239 tmpLen = snprintf(tmp, cmdLen, "%s ", argv[i]);
684
369c59b10266 ported crasRun to OpenBSD
thomask
parents: 545
diff changeset
240 tmp += tmpLen;
369c59b10266 ported crasRun to OpenBSD
thomask
parents: 545
diff changeset
241 cmdLen -= tmpLen;
545
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
242 }
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
243 return cmd;
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
244 }
490
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
245
545
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
246 int main(int argc, char** argv){
940
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
247 char* cmd;
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
248 char* end;
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
249
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
250 if(argc<4){
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
251 fprintf(stderr, "crashRun <timeOut (s)> <maxMemory (MB)> <command> [<command arg> ...]\n");
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
252 return EXIT_FAILURE;
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
253 }
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
254 timeOut = strtol(argv[1], &end, 10);
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
255 if(*end){
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
256 fprintf(stderr, "the timeOut argument contains the illegal character: %c (0x%X)", *end, *end);
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
257 return EXIT_FAILURE;
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
258 }else if(timeOut < 1){
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
259 fprintf(stderr, "minimum timeOut is 1 second, got %li seconds\n", timeOut);
545
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
260 return EXIT_FAILURE;
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
261 }
940
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
262
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
263 memoryLimit = strtol(argv[2], &end, 10);
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
264 if(*end){
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
265 fprintf(stderr, "the maxMemory argument contains the illegal character: %c (0x%X)", *end, *end);
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
266 return EXIT_FAILURE;
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
267 }else if(memoryLimit < 1){
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
268 fprintf(stderr, "minimum memoryLimit is 1MB second, got %li MB\n", memoryLimit);
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
269 return EXIT_FAILURE;
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
270 }
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
271 cmd = reconstructCmd(argc-3, argv+3);
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
272
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
273 #ifdef DEBUG
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
274 printf("cmd[%li sec, %li MB]: %s \n", timeOut, memoryLimit, cmd);
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
275 #endif
545
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
276
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
277 #ifdef USE_POSIX
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
278 setupLimits();
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
279 pID = fork();
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
280 if(pID == 0){
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
281 /* child */
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
282 pID=setsid();
498
1636b8289a73 correctly kill child process if killed by user
thomask
parents: 495
diff changeset
283 printf("EXIT CODE: %d\n", system(cmd));
545
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
284 }else if(pID < 0){
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
285 fprintf(stderr, "failed to fork\n");
490
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
286 return EXIT_FAILURE;
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
287 }else{
545
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
288 /* parent */
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
289 setupHandlers();
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
290 #if !(defined(USE_POSIX_LIMITS) && defined(SIGXCPU))
940
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
291 alarm(timeOut);
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
292 #else
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
293 /* safe guard against childs without CPU usage */
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
294 alarm(timeOut * WAIT_GUARD);
502
a15d15eb3928 added memory limit to crashRun
thomask
parents: 498
diff changeset
295 #endif
490
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
296 wait(NULL);
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
297 }
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
298 return EXIT_SUCCESS;
940
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
299 #elif USE_WINDOWS
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
300 /* BUG: maxMemory not implemented */
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
301
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
302 PROCESS_INFORMATION processInfo;
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
303 STARTUPINFO startupInfo;
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
304 SECURITY_ATTRIBUTES sa = {
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
305 sizeof(SECURITY_ATTRIBUTES), NULL, TRUE
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
306 };
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
307 unsigned long exitCode;
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
308 long int timeLeft = timeOut; /* time limit in iterations of WFX loop */
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
309
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
310 memset(&processInfo, 0, sizeof(PROCESS_INFORMATION));
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
311 memset(&startupInfo, 0, sizeof(STARTUPINFO));
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
312 startupInfo.cb = sizeof(STARTUPINFO);
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
313
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
314 if (!CreateProcess(NULL, cmd, NULL, NULL, TRUE, 0, NULL, NULL,
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
315 &startupInfo, &processInfo)) {
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
316 void *message;
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
317
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
318 FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
319 | FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(), 0,
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
320 (char*) &message, 0, NULL);
490
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
321
940
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
322 /* this should never happen */
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
323 fprintf(stderr, "ERROR running %s:\n", cmd);
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
324 fprintf(stderr, "%s\n", message);
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
325 LocalFree((HLOCAL) message);
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
326 return RAND_MAX;
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
327 }
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
328
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
329 /* wait for exit */
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
330 while (TRUE) {
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
331 Sleep(1000);
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
332 GetExitCodeProcess(processInfo.hProcess, &exitCode);
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
333 if (exitCode == 0x103) {
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
334 if (--timeLeft == 0) {
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
335 TerminateProcess(processInfo.hProcess, EXIT_FAILURE);
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
336 printf("EXIT CODE: timeout after %li seconds\n", timeOut);
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
337 Sleep(100);
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
338 return EXIT_SUCCESS;
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
339 }
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
340 } else {
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
341 printf("EXIT CODE: %i\n", exitCode);
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
342 return EXIT_SUCCESS;
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
343 }
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
344 }
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
345
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
346 fprintf(stderr, "crashRun: BUG\n");
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
347 return EXIT_FAILURE;
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
348 #else
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
349
dddc404783c9 added basic Windows support to crashRun.c
thomask
parents: 923
diff changeset
350 #error no implmentation for your system found (supported: Posix, Windows)
490
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
351
d091ff903fa4 added crashRun for POSIX systems
thomask
parents:
diff changeset
352 #endif /* USE_POSIX else */
545
7b5efee9d724 prepared for windows porting
thomask
parents: 502
diff changeset
353 }