0
|
1 |
#define _POSIX_C_SOURCE 199309L |
|
2 |
#include <stdio.h> |
|
3 |
#include <stdlib.h> |
|
4 |
#include <stdarg.h> |
|
5 |
#include <string.h> |
|
6 |
#include <unistd.h> |
|
7 |
#include <poll.h> |
|
8 |
#include <time.h> |
|
9 |
#include <signal.h> |
|
10 |
#include <sys/types.h> |
|
11 |
#include <sys/wait.h> |
|
12 |
#include <fcntl.h> |
|
13 |
#include <errno.h> |
|
14 |
#include <math.h> |
|
15 |
|
|
16 |
|
|
17 |
enum DPokeSourceType_en { |
|
18 |
DPOKE_PROGRAM, |
|
19 |
DPOKE_FILE, |
|
20 |
DPOKE_FUNCTION, |
|
21 |
}; |
|
22 |
typedef enum DPokeSourceType_en DPokeSourceType; |
|
23 |
|
1
|
24 |
#define DPOKE_BUFFER 128 |
0
|
25 |
struct DPokeSource_st { |
|
26 |
DPokeSourceType type; |
|
27 |
char *path; |
|
28 |
int arg; |
|
29 |
|
|
30 |
//FILE *stream; |
|
31 |
int fd; |
|
32 |
enum DPokeSourceStatus_en { |
|
33 |
DPOKE_STARTED, |
|
34 |
DPOKE_RUNNING, |
|
35 |
DPOKE_DIED, |
|
36 |
DPOKE_FAIL, |
|
37 |
DPOKE_FAILED, |
|
38 |
} status; |
|
39 |
char buffer[DPOKE_BUFFER+1]; |
|
40 |
char current[DPOKE_BUFFER+1]; |
|
41 |
int buffer_usage; |
|
42 |
|
|
43 |
// DPOKE_PROGRAM-specific fields: |
|
44 |
int pid; |
|
45 |
}; |
|
46 |
|
|
47 |
typedef struct DPokeSource_st DPokeSource; |
|
48 |
|
|
49 |
#define LENGTH(X) (sizeof X / sizeof X[0]) |
|
50 |
|
1
|
51 |
char * |
|
52 |
smprintf(char *fmt, ...) |
|
53 |
{ |
|
54 |
va_list fmtargs; |
|
55 |
char *ret; |
|
56 |
int len; |
0
|
57 |
|
1
|
58 |
va_start(fmtargs, fmt); |
|
59 |
len = vsnprintf(NULL, 0, fmt, fmtargs); |
|
60 |
va_end(fmtargs); |
0
|
61 |
|
1
|
62 |
ret = malloc(++len); |
|
63 |
if (ret == NULL) { |
|
64 |
perror("malloc"); |
|
65 |
exit(1); |
|
66 |
} |
0
|
67 |
|
1
|
68 |
va_start(fmtargs, fmt); |
|
69 |
vsnprintf(ret, len, fmt, fmtargs); |
|
70 |
va_end(fmtargs); |
0
|
71 |
|
1
|
72 |
return ret; |
0
|
73 |
} |
|
74 |
|
1
|
75 |
void display(char *v[], size_t vc); |
19
|
76 |
void setup(int argc, char **argv); |
1
|
77 |
void cleanup(int exitcode); |
0
|
78 |
|
|
79 |
extern char* v[]; |
|
80 |
#include "config.h" |
|
81 |
#ifdef DEBUG |
1
|
82 |
#define errprintf(...) fprintf(stderr,__VA_ARGS__) |
0
|
83 |
#else |
|
84 |
#define errprintf(...) |
|
85 |
#endif |
|
86 |
|
|
87 |
char* v[LENGTH(sources)]; |
|
88 |
|
|
89 |
static struct pollfd source_fds[LENGTH(sources)]; |
|
90 |
|
1
|
91 |
void die(int exitcode) { |
|
92 |
fprintf(stderr, "Dying with exit code %d\n", exitcode); |
|
93 |
cleanup(exitcode); |
|
94 |
exit(exitcode); |
|
95 |
} |
|
96 |
|
0
|
97 |
void source_open(int source_id) { |
|
98 |
DPokeSource *src = &sources[source_id]; |
|
99 |
//FILE* stream; |
|
100 |
int p_stdout[2]; |
|
101 |
switch (src->type) { |
|
102 |
case DPOKE_PROGRAM: |
|
103 |
//stream = popen(src->path, "r"); |
|
104 |
if (pipe(p_stdout)!=0) { |
|
105 |
perror("pipe"); |
1
|
106 |
die(7); |
0
|
107 |
} |
|
108 |
pid_t pid = fork(); |
|
109 |
if (pid < 0) { |
|
110 |
perror("fork"); |
1
|
111 |
die(7); |
0
|
112 |
} else if (pid == 0) { |
|
113 |
dup2(open("/dev/null",O_RDONLY),0); |
|
114 |
close(p_stdout[0]); |
|
115 |
dup2(p_stdout[1],1); |
|
116 |
execl("/bin/sh", "sh", "-c", src->path, NULL); |
|
117 |
perror("execl"); |
|
118 |
exit(1); |
|
119 |
} |
|
120 |
src->fd = p_stdout[0]; |
|
121 |
src->pid = pid; |
|
122 |
break; |
|
123 |
default: |
|
124 |
errprintf("Don't know how to handle source type %d.\n",src->type); |
1
|
125 |
die(2); |
0
|
126 |
} |
|
127 |
//src->stream = stream; |
|
128 |
//src->fd = fileno(stream); |
|
129 |
src->buffer_usage = 0; |
|
130 |
src->status = DPOKE_STARTED; |
|
131 |
source_fds[source_id].fd = sources[source_id].fd; |
|
132 |
source_fds[source_id].events = POLLIN; |
|
133 |
} |
|
134 |
|
|
135 |
static void sigchld_hdl (int sig) |
|
136 |
{ |
|
137 |
pid_t pid; |
|
138 |
while ((pid = waitpid(-1, NULL, WNOHANG)) > 0) { |
|
139 |
for (int i=0;i<LENGTH(sources);i++) |
|
140 |
if ((sources[i].type == DPOKE_PROGRAM) && (sources[i].pid == pid)) { |
|
141 |
sources[i].pid = -1; |
|
142 |
errprintf("#%d pid %d died.\n",i,pid); |
|
143 |
switch (sources[i].status) { |
|
144 |
case DPOKE_STARTED: |
|
145 |
sources[i].status = DPOKE_FAIL; break; |
|
146 |
case DPOKE_RUNNING: |
|
147 |
sources[i].status = DPOKE_DIED; break; |
|
148 |
default: |
|
149 |
errprintf("#%d died in state %d. Whaddafuck?!\n",i,sources[i].status); |
1
|
150 |
die(8); |
0
|
151 |
} |
|
152 |
} |
|
153 |
} |
|
154 |
} |
|
155 |
|
|
156 |
void source_close(int source_id) { |
|
157 |
DPokeSource *src = &sources[source_id]; |
|
158 |
if (src->pid != -1) kill(src->pid,SIGTERM); |
|
159 |
if (src->fd != -1) close(src->fd); |
|
160 |
source_fds[source_id].fd = -1; |
|
161 |
} |
|
162 |
|
|
163 |
double dpoke_time() { |
|
164 |
struct timespec tp; |
|
165 |
if (clock_gettime(CLOCK_MONOTONIC,&tp)) { |
|
166 |
perror("clock_gettime"); |
1
|
167 |
die(5); |
0
|
168 |
} |
|
169 |
return tp.tv_sec + tp.tv_nsec*1E-9; |
|
170 |
} |
|
171 |
|
|
172 |
int main(int argc, char* argv[]) { |
19
|
173 |
setup(argc, argv); |
0
|
174 |
struct sigaction act; |
|
175 |
|
|
176 |
memset (&act, 0, sizeof(act)); |
|
177 |
act.sa_handler = sigchld_hdl; |
|
178 |
if (sigaction(SIGCHLD, &act, 0)) { |
|
179 |
perror ("sigaction"); |
1
|
180 |
die(6); |
0
|
181 |
} |
|
182 |
|
|
183 |
for (int i = 0; i < LENGTH(sources); i++ ) { |
|
184 |
source_open(i); |
|
185 |
v[i] = sources[i].current; |
|
186 |
} |
|
187 |
int status; |
|
188 |
int sleeptime = 10000; |
|
189 |
double prevtime=dpoke_time(); |
|
190 |
while (1) { |
|
191 |
status = poll(source_fds,LENGTH(sources), sleeptime); |
|
192 |
if (status < 0) { |
|
193 |
if (errno == EINTR) |
|
194 |
continue; |
|
195 |
} |
|
196 |
for (int i = 0; i < LENGTH(sources); i++) { |
|
197 |
DPokeSource *src = sources+i; |
|
198 |
if (source_fds[i].revents & POLLIN) { |
1
|
199 |
errprintf("Event: %d\n", source_fds[i].revents); |
0
|
200 |
int data_read = read(src->fd,src->buffer+src->buffer_usage,DPOKE_BUFFER-src->buffer_usage); |
|
201 |
if (data_read>0) { |
|
202 |
src->buffer_usage += data_read; |
1
|
203 |
if (src->buffer_usage>=DPOKE_BUFFER) { |
|
204 |
fprintf(stderr, "Buffer is full for #%d. Resetting.\n",i); |
|
205 |
src->buffer_usage = 0; |
0
|
206 |
} |
|
207 |
src->buffer[src->buffer_usage] = '\0'; |
|
208 |
char* eolpos = strchr(src->buffer + src->buffer_usage - data_read,'\n'); |
|
209 |
if (eolpos) { |
|
210 |
memcpy(src->current,src->buffer,eolpos-src->buffer); |
|
211 |
src->current[eolpos-src->buffer] = '\0'; |
|
212 |
src->buffer_usage -= (eolpos - src->buffer + 1); |
|
213 |
memmove(src->buffer,eolpos + 1,src->buffer_usage + 1); |
|
214 |
} |
|
215 |
if (src->status == DPOKE_STARTED) |
|
216 |
src->status = DPOKE_RUNNING; |
|
217 |
} else { |
1
|
218 |
fprintf(stderr, "Buffer usage: %d\n", src->buffer_usage); |
0
|
219 |
perror("read"); |
1
|
220 |
die(3); |
0
|
221 |
} |
|
222 |
} else if ((source_fds[i].revents & POLLHUP)||(src->status==DPOKE_DIED)) { |
|
223 |
errprintf("#%d HUP\n",i); |
|
224 |
source_close(i); |
|
225 |
if (src->status==DPOKE_STARTED) |
|
226 |
src->status = DPOKE_FAIL; |
|
227 |
else |
|
228 |
source_open(i); |
|
229 |
} else if (source_fds[i].revents) { |
|
230 |
errprintf("#%d revents: %d\n",i,source_fds[i].revents); |
|
231 |
} |
|
232 |
if (src->status==DPOKE_FAIL) { |
|
233 |
src->status = DPOKE_FAILED; |
|
234 |
errprintf("#%d Marked as failure\n",i); |
|
235 |
strcpy(src->current,FAILURE_MSG); |
|
236 |
} |
|
237 |
} |
|
238 |
double curtime = dpoke_time(); |
|
239 |
if ((curtime-prevtime)>MINTIME) { |
1
|
240 |
display(v,LENGTH(sources)); |
0
|
241 |
prevtime = curtime; |
|
242 |
sleeptime = 10000; |
|
243 |
} else { |
|
244 |
double sleepd = (prevtime+MINTIME - curtime)*1000; |
|
245 |
sleeptime = ceil(sleepd); |
|
246 |
errprintf("%f %f %f %f\n",prevtime,curtime,MINTIME,sleepd); |
|
247 |
} |
|
248 |
errprintf("Will sleep for %d\n",sleeptime); |
|
249 |
} |
1
|
250 |
cleanup(0); |
0
|
251 |
return 0; |
|
252 |
} |
|
253 |
/* |
|
254 |
ABCnDEF0 |
|
255 |
01234567 |
|
256 |
eolpos = 3 |
|
257 |
bu = 7 |
|
258 |
3-0+1 = 4 |
|
259 |
bu = 3 |
|
260 |
memmove: |
|
261 |
01234567 |
|
262 |
34534567 |
|
263 |
nDEn |
|
264 |
*/ |