-
Notifications
You must be signed in to change notification settings - Fork 0
/
retry.c
172 lines (156 loc) · 3.57 KB
/
retry.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
============================================================================
Name : retry.c
Author : Heikki Pernu
Version :
Copyright :
Description : Retry ptog in C, Ansi-style
============================================================================
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <getopt.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include "version.h"
void usage() {
fprintf(stderr,
"%s [--version] [-hv] [-t timeout] times delay cmd [cmdarg] [cmdarg] [...]\n",
"retry");
exit(EXIT_FAILURE);
}
/**
* Convert a time in arg string to seconds in integer
*/
int totime(char *arg) {
if (strlen(arg) > 0) {
int number = atoi(arg);
switch (*(arg + strlen(arg) - 1)) {
case 'h': // Hours
return number * 60 * 60;
case 'm': // Minutes
return number * 60;
default:
return number;
}
}
return 0;
}
extern char *optarg;
extern int optind;
int main(int argc, char * const argv[]) {
int opt = 0;
int verbosity = 0;
int timeout = 0;
int count = 0;
int delay = 0;
int expect = 0; // What value to expect
if (argc>=2 && strcmp("--version",argv[1])==0) {
fprintf(stderr,"%s\n",version);
exit(0);
}
while ((opt = getopt(argc, argv, "+hvt:")) != -1) {
switch (opt) {
case 'h':
usage();
break;
case 'v':
verbosity++;
break;
case 't':
timeout = totime(optarg);
break;
default:
fprintf(stderr, "Unknown option %c\n", opt);
usage();
break;
}
}
argv += optind;
argc -= optind;
if (argc < 3) {
fprintf(stderr, "At least three arguments expected\n");
usage();
/* NOT REACHED */
}
count = totime(argv[0]);
argv++;
delay = totime(argv[0]);
argv++;
// Do fork, run the actual command in the background, possibly with timeout
pid_t pid = 0;
int status;
FILE *out = NULL;
FILE *err = NULL;
do {
if (out != NULL)
fclose(out);
if (err != NULL)
fclose(err);
if ((out = tmpfile()) == NULL) {
perror("Unable to create temp file for stdout buffer");
exit(-1);
}
if ((err = tmpfile()) == NULL) {
perror("Unable to create temp file for stderr buffer");
exit(-1);
}
if ((pid = fork()) == 0) {
/* CHILD
* Run the actual program */
// Should not go outside of execvp
alarm(timeout); // Timeout with signal if timeout reached
if (dup2(fileno(out), STDOUT_FILENO) == -1) {
perror("Unable to redirecto stdout in subprocess");
exit(-1);
}
fclose(out);
if (dup2(fileno(err), STDERR_FILENO) == -1) {
perror("Unable to redirecto stderr in subprocess");
exit(-1);
}
fclose(err);
exit(execvp(argv[0], argv));
}
/* PARENT
* Wait for child termination */
wait(&status);
if (WEXITSTATUS(status) != expect)
sleep(delay);
} while (--count > 0 && WEXITSTATUS(status) != expect);
fflush(out);
fflush(err);
struct stat st;
// Copy saved stdout from subprocess to real stdout
if (fstat(fileno(out), &st) != 0) {
perror("Unable to check the stdout of subprocess");
exit(-1);
} else if (st.st_size > 0) {
char buf[4096];
size_t n;
fseek(out, 0, SEEK_SET);
while (!feof(out)) {
n = fread(buf, 1, sizeof(buf), out);
fwrite(buf, 1, n, stdout);
}
}
fflush(stdout);
// Copy saved stderr from subprocess to real stderr
if (fstat(fileno(err), &st) != 0) {
perror("Unable to check the stdout of subprocess");
exit(-1);
} else if (st.st_size > 0) {
char buf[4096];
size_t n;
fseek(err, 0, SEEK_SET);
while (!feof(err)) {
n = fread(buf, 1, sizeof(buf), err);
fwrite(buf, 1, n, stderr);
}
}
fflush(stderr);
return WEXITSTATUS(status);
}