490
|
1 /*
|
|
2 * crashRun - execute command with timeout limit and catch segfaults
|
|
3 *
|
|
4 * Copyright (C) 2005 Thomas Kuehne <thomas@kuehne.cn>
|
|
5 *
|
|
6 * This program is free software; you can redistribute it and/or modify
|
|
7 * it under the terms of the GNU General Public License as published by
|
|
8 * the Free Software Foundation; either version 2 of the License, or
|
|
9 * (at your option) any later version.
|
|
10 *
|
|
11 * This program is distributed in the hope that it will be useful,
|
|
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
14 * GNU General Public License for more details.
|
|
15 *
|
|
16 * You should have received a copy of the GNU General Public License
|
|
17 * along with this program; if not, write to the Free Software
|
|
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
19 *
|
|
20 * $HeadURL$
|
|
21 * $Date$
|
|
22 * $Author$
|
|
23 *
|
|
24 */
|
|
25
|
498
|
26 #include <errno.h>
|
|
27 #include <string.h>
|
|
28 #include <stdlib.h>
|
|
29 #include <stdio.h>
|
|
30
|
|
31 /* time-out in seconds */
|
|
32 #define TIME_OUT 480
|
|
33
|
|
34 #if defined(__GNU_LIBRARY__) || defined(__GLIBC__)
|
|
35 #define USE_POSIX
|
|
36 #endif
|
|
37
|
490
|
38 #ifdef linux
|
|
39 #define USE_POSIX
|
|
40 #endif
|
|
41
|
|
42 #if defined(__APPLE__) && defined(__MACH__)
|
|
43 #define USE_POSIX
|
|
44 #endif
|
|
45
|
|
46 #ifdef __FreeBSD__
|
|
47 #define USE_POSIX
|
|
48 #endif
|
|
49
|
498
|
50
|
490
|
51 #ifdef USE_POSIX
|
|
52
|
|
53 #include <unistd.h>
|
|
54 #include <sys/wait.h>
|
|
55
|
495
|
56 static pid_t pID;
|
|
57
|
498
|
58 void resourceLimit(int signalID){
|
|
59 #ifdef SIGXCPU
|
|
60 if(signalID==SIGALRM || signalID==SIGXCPU){
|
|
61 #else
|
|
62 if(signalID==SIGALRM){
|
|
63 #endif
|
|
64 printf("EXIT CODE: signal %d (time-out after %d seconds)", signalID, TIME_OUT);
|
|
65 }else{
|
|
66 printf("EXIT CODE: signal %d\n", signalID);
|
|
67 }
|
|
68
|
490
|
69 fflush(stdout);
|
|
70 fflush(stderr);
|
495
|
71 kill(-pID, SIGKILL);
|
490
|
72
|
498
|
73 if(signalID==SIGUSR1 || signalID==SIGUSR2){
|
|
74 exit(EXIT_FAILURE);
|
|
75 }else{
|
|
76 exit(EXIT_SUCCESS);
|
|
77 }
|
490
|
78 }
|
|
79
|
|
80 int main(int argc, char** arg){
|
495
|
81 pID = fork();
|
490
|
82 if (pID == 0){
|
498
|
83 /* child: resource management */
|
495
|
84 pID=setsid();
|
|
85
|
498
|
86 /* child: execute args */
|
490
|
87 int cmdLen=1;
|
|
88 int i;
|
|
89 for(i=1; i<argc; i++){
|
|
90 cmdLen+=strlen(arg[i]);
|
|
91 cmdLen+=3;
|
|
92 }
|
|
93
|
|
94 char* cmd = malloc(cmdLen);
|
|
95 *cmd='\x00';
|
|
96
|
|
97 for(i=1; i<argc; i++){
|
|
98 strcat(cmd, "\"");
|
|
99 strcat(cmd, arg[i]);
|
|
100 strcat(cmd, "\" ");
|
|
101 }
|
493
|
102 #ifdef DEBUG
|
490
|
103 printf("cmd[%i s]: %s\n", TIME_OUT, cmd);
|
493
|
104 #endif
|
|
105 fflush(stdout);
|
|
106 fflush(stderr);
|
498
|
107 printf("EXIT CODE: %d\n", system(cmd));
|
490
|
108 }else if (pID < 0){
|
|
109 fprintf(stderr, "failed to fork\n");
|
|
110 return EXIT_FAILURE;
|
|
111 }else{
|
498
|
112 /* parent : clean kill */
|
490
|
113 struct sigaction acti;
|
498
|
114 acti.sa_handler = &resourceLimit;
|
|
115 sigaction(SIGHUP, &acti, NULL);
|
|
116 sigaction(SIGINT, &acti, NULL);
|
|
117 sigaction(SIGQUIT, &acti, NULL);
|
|
118 sigaction(SIGABRT, &acti, NULL);
|
|
119 sigaction(SIGTERM, &acti, NULL);
|
|
120
|
|
121 /* parent : timeout */
|
490
|
122 if(0!=sigaction(SIGALRM, &acti, NULL)){
|
498
|
123 fprintf(stderr, "failed to set timeout (%d)\n", errno);
|
|
124 resourceLimit(SIGUSR1);
|
|
125 return EXIT_FAILURE; /* never executed */
|
490
|
126 }
|
|
127 alarm(TIME_OUT);
|
|
128 wait(NULL);
|
|
129 }
|
|
130
|
|
131 return EXIT_SUCCESS;
|
|
132 }
|
498
|
133 #else /* USE_POSIX */
|
490
|
134
|
498
|
135 #error "no implementation present for your OS"
|
490
|
136
|
|
137 #endif /* USE_POSIX else */
|