-
Notifications
You must be signed in to change notification settings - Fork 0
/
lemon.c
310 lines (272 loc) · 6.5 KB
/
lemon.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <poll.h>
#include <string.h>
#include <time.h>
#include <sys/wait.h>
#include <sys/timerfd.h>
#define FORCE_UPDATE_INTERVAL 30 /* seconds */
#define MIN_UPDATE_INTERVAL 500 /* milliseconds */
#define MAX_BLOCKS 1024
#define MAX_CMDS 1024
#define SECONDS(x) (1000 * (x))
#ifdef DEBUG
#define LOG(fmt, ...) fprintf(stderr, "%s:%d:%s " fmt "\n", __FILE__, __LINE__, __func__, ##__VA_ARGS__)
#else
#define LOG(fmt, ...)
#endif
enum {
BLOCK_RAW,
BLOCK_CONTINUOUS,
BLOCK_SINGLE
};
struct block {
int type;
long update_time;
char *cmd[4];
char *str;
};
struct cmd {
char *cmd[4];
int pid;
int block;
int fd;
FILE *ffd;
};
static struct block block[MAX_BLOCKS] = {0, };
static struct cmd cmd[MAX_CMDS] = {0, };
static int nblocks = 0;
static int ncmds = 0;
static char *buf = NULL;
static char **bar_cmd = NULL;
static int bar_pid = -1;
static int bar_fd = STDOUT_FILENO;
static inline void close_cmd(int i)
{
if (cmd[i].ffd) {
fclose(cmd[i].ffd);
} else if (cmd[i].fd) {
close(cmd[i].fd);
}
}
static inline void make_cmd(char *out[3], char *cmd)
{
out[0] = "/bin/sh";
out[1] = "-c";
out[2] = cmd;
out[3] = NULL;
}
static void cleanup()
{
if (buf) {
free(buf);
buf = NULL;
}
for (int i = 0; i < nblocks; ++i) {
if (block[i].type != BLOCK_RAW && block[i].str) {
free(block[i].str);
block[i].str = NULL;
}
}
for (int i = 0; i < ncmds; ++i) close_cmd(i);
kill(0, SIGTERM);
while (wait(NULL) > 0);
}
static void sighandler(int signum)
{
switch (signum) {
case SIGINT:
case SIGTERM:
cleanup();
exit(0);
break;
}
}
static void err(const char *msg)
{
cleanup();
fprintf(stderr, "%s\n", msg);
exit(1);
}
static void strip(char *str)
{
int l = strlen(str);
if (str[l - 1] == '\n')
str[l - 1] = '\0';
}
static void run_command(char **cmd, int *pid, int *out_fd)
{
int p[2];
if (pipe(p) == -1) err("error in pipe()");
*pid = fork();
if (*pid < -1) err("error in fork()");
if (*pid == 0) {
dup2(p[1], STDOUT_FILENO);
for (int i = STDERR_FILENO + 1; i < (int) sysconf(_SC_OPEN_MAX); i++) {
close(i);
}
execvp(cmd[0], cmd);
}
close(p[1]);
*out_fd = p[0];
}
static void restart_cmd(int i)
{
LOG("(re)start cmd %d", i);
if (cmd[i].pid > 0) {
kill(cmd[i].pid, SIGKILL);
waitpid(cmd[i].pid, NULL, 0); // todo: get return code
close_cmd(i);
}
int pid, out_fd;
run_command(cmd[i].cmd, &pid, &out_fd);
LOG("new cmd %d pid=%d fd=%d", i, pid, out_fd);
cmd[i].pid = pid;
cmd[i].fd = out_fd;
cmd[i].ffd = fdopen(out_fd, "r");
}
static void update_block(int i)
{
int pid, out_fd;
struct timespec ts;
long current_time;
if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) err("error in clock_gettime()");
current_time = 1000 * ts.tv_sec + ts.tv_nsec / 1000000;
if (block[i].update_time != 0 && (current_time - block[i].update_time) <= MIN_UPDATE_INTERVAL) {
fprintf(stderr, "block %d: too frequent updates\n", i);
return;
}
run_command(block[i].cmd, &pid, &out_fd);
LOG("update block %d, read fd=%d", i, out_fd);
waitpid(pid, NULL, 0); // todo: get return code
size_t ignore = 0;
FILE *f = fdopen(out_fd, "r");
if (getline(&block[i].str, &ignore, f) < 0) err("error in getline()");
strip(block[i].str);
fclose(f);
}
static void update_blocks()
{
for (int i = 0; i < nblocks; ++i) {
if (block[i].type == BLOCK_SINGLE) {
update_block(i);
}
}
}
static void print_status()
{
/* (re)start bar if needed */
int ws;
if (bar_cmd != NULL && (bar_pid < 0 || (ws = waitpid(bar_pid, NULL, WNOHANG)) != 0)) {
int p[2];
if (pipe(p) == -1) err("error in pipe()");
bar_pid = fork();
if (bar_pid < -1) err("error in fork()");
if (bar_pid == 0) {
dup2(p[0], STDIN_FILENO);
for (int i = STDERR_FILENO + 1; i < (int) sysconf(_SC_OPEN_MAX); i++) {
close(i);
}
execvp(bar_cmd[0], bar_cmd);
}
close(p[0]);
bar_fd = p[1];
LOG("(re)start bar, fd = %d", bar_fd);
}
for (int i = 0; i < nblocks; ++i) {
if (block[i].str) {
write(bar_fd, block[i].str, strlen(block[i].str));
}
}
write(bar_fd, "\n", 1);
fsync(bar_fd);
}
int main(int argc, char *argv[])
{
setpgid(0, 0);
signal(SIGCHLD, SIG_IGN);
signal(SIGTERM, sighandler);
int pos = 1;
while (pos < argc) {
if (strcmp(argv[pos], "-r") == 0) {
++pos;
if (pos >= argc) err("invalid command line arguments");
block[nblocks].type = BLOCK_RAW;
block[nblocks].str = argv[pos];
++nblocks;
++pos;
} else if (strcmp(argv[pos], "-c") == 0) {
++pos;
if (pos >= argc) err("invalid command line arguments");
block[nblocks].type = BLOCK_CONTINUOUS;
make_cmd(cmd[ncmds].cmd, argv[pos]);
cmd[ncmds].block = nblocks;
++nblocks;
++ncmds;
++pos;
} else if (strcmp(argv[pos], "-s") == 0) {
++pos;
if (pos >= argc) err("invalid command line arguments");
block[nblocks].type = BLOCK_SINGLE;
make_cmd(block[nblocks].cmd, argv[pos]);
++pos;
for (; pos < argc && argv[pos][0] != '-'; ++pos) {
make_cmd(cmd[ncmds].cmd, argv[pos]);
cmd[ncmds].block = nblocks;
++ncmds;
}
++nblocks;
} else if (strcmp(argv[pos], "--") == 0) {
bar_cmd = &argv[pos + 1];
break;
} else {
err("invalid command line arguments");
}
}
update_blocks();
print_status();
struct pollfd polls[MAX_CMDS + 1];
for (int i = 0; i < ncmds; ++i) {
restart_cmd(i);
polls[i].fd = cmd[i].fd;
polls[i].events = POLLIN;
}
int timer_fd = timerfd_create(CLOCK_MONOTONIC, 0);
struct itimerspec timer_spec = {{ FORCE_UPDATE_INTERVAL, 0 }, { FORCE_UPDATE_INTERVAL, 0 }};
timerfd_settime(timer_fd, 0, &timer_spec, NULL);
polls[ncmds].fd = timer_fd;
polls[ncmds].events = POLLIN;
int ret;
size_t ignore;
char ignore_buf[8];
while (1) {
ret = poll(polls, ncmds + 1, -1);
if (ret <= 0) err("error in poll()");
for (int i = 0; i < ncmds; ++i) {
if (polls[i].revents & (POLLERR | POLLHUP | POLLNVAL)) {
/* try to restart cmd[i] */
restart_cmd(i);
polls[i].fd = cmd[i].fd;
}
if (polls[i].revents & POLLIN) {
if (block[cmd[i].block].type == BLOCK_SINGLE) {
if (getline(&buf, &ignore, cmd[i].ffd) < 0) err("error in getline()");
update_block(cmd[i].block);
} else {
if (getline(&block[cmd[i].block].str, &ignore, cmd[i].ffd) < 0) err("error in getline()");
strip(block[cmd[i].block].str);
}
}
}
/* timer */
if (polls[ncmds].revents & (POLLERR | POLLHUP | POLLNVAL)) err("some errors have been recieved from timer");
if (polls[ncmds].revents & POLLIN) {
read(timer_fd, ignore_buf, 8);
update_blocks();
}
print_status();
}
return 0;
}