annotate crashRun.c @ 498:1636b8289a73

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