-
Notifications
You must be signed in to change notification settings - Fork 22
/
main.c
364 lines (307 loc) · 9.15 KB
/
main.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include <signal.h>
#include <sched.h>
#include <fcntl.h>
#include "types.h"
#include "list.h"
#include "fluxcapacitor.h"
#include "uevent.h"
#include "trace.h"
static void usage() {
ERRORF(
"Usage:\n"
"\n"
" fluxcapacitor [options] [ -- command [ arguments ... ] ... ]\n"
"\n"
"Options:\n"
"\n"
" --output=FILENAME Write logs to FILENAME instead of stderr.\n"
" --libpath=PATH Load " PRELOAD_LIBNAME " from\n"
" selected PATH directory.\n"
" --signal=SIGNAL Use specified signal to interrupt blocking\n"
" syscall instead of SIGURG.\n"
" --verbose,-v Print more stuff. Repeat for debugging\n"
" messages.\n"
" --help Print this message.\n"
"\n"
);
exit(EXIT_FAILURE);
}
/* Global */
struct options options;
static flux_time main_loop(char ***list_of_argv);
int main(int argc, char **argv) {
options.verbose = 0;
options.shoutstream = stderr;
options.signo = SIGURG;
handle_backtrace();
pin_cpu();
optind = 1;
while (1) {
int option_index = 0;
static struct option long_options[] = {
{"output", required_argument, 0, 'o' },
{"libpath", required_argument, 0, 0 },
{"help", no_argument, 0, 'h' },
{"verbose", no_argument, 0, 'v' },
{"signal", required_argument, 0, 0 },
{0, 0, 0, 0 }
};
int arg = getopt_long(argc, argv, "vho:",
long_options, &option_index);
if (arg == -1) {
break;
}
switch (arg) {
case 0: {
const char *opt_name = long_options[option_index].name;
if (0 == strcasecmp(opt_name, "libpath")) {
options.libpath = strdup(optarg);
} else if (0 == strcasecmp(opt_name, "signal")) {
options.signo = str_to_signal(optarg);
if (!options.signo)
FATAL("Unrecognised signal \"%s\"", optarg);
} else {
FATAL("Unknown option: %s", argv[optind]);
}
break; }
case 'v':
options.verbose += 1;
break;
case 'h':
usage();
break;
case 'o': {
if (strcmp(optarg, "-") == 0) {
options.shoutstream = stdout;
} else {
FILE *f = fopen(optarg, "a+");
if (f == NULL)
PFATAL("fopen(%s)", optarg);
if (fcntl(fileno(f), F_SETFD, FD_CLOEXEC) == -1)
PFATAL("fcntl(FD_CLOEXEC)");
options.shoutstream = f;
}
/* Make sure there's something to be logged */
if (options.verbose == 0)
options.verbose += 1;
break; }
default:
FATAL("Unknown option: %s", argv[optind]);
}
}
if (!argv[optind]) {
FATAL("You must specify at least one command to execute.");
}
char ***list_of_argv = argv_split(&argv[optind], "--", argc);
ensure_libpath(argv[0]);
ldpreload_extend(options.libpath, PRELOAD_LIBNAME);
SHOUT("--- Flux Capacitor ---\n");
SHOUT("[.] LD_PRELOAD=%s", ldpreload_get());
u64 time_drift = main_loop(list_of_argv);
free(options.libpath);
fflush(options.shoutstream);
char ***child_argv = list_of_argv;
while (*child_argv) {
free(*child_argv);
child_argv ++;
}
free(list_of_argv);
PRINT(" ~ Exiting with code %i. Speedup %.3f sec.",
options.exit_status, time_drift / 1000000000.);
return options.exit_status;
}
static int on_signal(struct uevent *uevent, int sfd, int mask, void *userdata) {
struct trace *trace = userdata;
trace_read(trace);
return 0;
}
static int on_trace(struct trace_process *process, int type, void *arg,
void *userdata);
static int on_trace_start(struct trace_process *process, int type, void *arg,
void *userdata) {
if (type != TRACE_ENTER)
FATAL("");
int pid = (long)arg;
SHOUT("[+] %i started", pid);
struct parent *parent = (struct parent *)userdata;
struct child *child = child_new(parent, process, pid);
return trace_continue(process, on_trace, child);
}
static int on_trace(struct trace_process *process, int type, void *arg,
void *userdata) {
struct child *child = userdata;
switch (type) {
case TRACE_EXIT: {
struct trace_exitarg *exitarg = arg;
if (exitarg->type == TRACE_EXIT_NORMAL) {
SHOUT("[-] %i exited with return status %u",
child->pid, exitarg->value);
options.exit_status = MAX(options.exit_status,
(unsigned)exitarg->value);
} else {
SHOUT("[-] %i exited due to signal %u",
child->pid, exitarg->value);
}
child_del(child);
break; }
case TRACE_SYSCALL_ENTER: {
struct trace_sysarg *sysarg = arg;
child_mark_blocked(child);
wrapper_syscall_enter(child, sysarg);
break; }
case TRACE_SYSCALL_EXIT: {
struct trace_sysarg *sysarg = arg;
child_mark_unblocked(child);
wrapper_syscall_exit(child, sysarg);
if (child->interrupted) {
child->interrupted = 0;
wrapper_pacify_signal(child, sysarg);
}
break; }
case TRACE_SIGNAL: {
int *signal_ptr = (int*)arg;
if (*signal_ptr == options.signo)
*signal_ptr = 0;
break; }
default:
FATAL("");
}
return 0;
}
static flux_time main_loop(char ***list_of_argv) {
struct timeval timeout;
struct parent *parent = parent_new();
struct trace *trace = trace_new(on_trace_start, parent);
struct uevent *uevent = uevent_new(NULL);
parent_run_one(parent, trace, *list_of_argv);
list_of_argv ++;
uevent_yield(uevent, trace_sfd(trace), UEVENT_READ, on_signal, trace);
while ((parent->child_count || *list_of_argv) && !options.exit_forced) {
/* Is everyone blocking? */
if (parent->blocked_count != parent->child_count) {
/* Nope, need to wait for some process to block */
uevent_select(uevent, NULL);
continue;
}
/* Continue only after some time passed with no action. */
if (parent->child_count) {
/* Say a child process did a syscall that
* produces side effects. For example a
* network write. It make take a while before
* the side effects become visible to another
* watched process.
*
* Although from our point of view everyone's
* "blocked", there may be some stuff
* available but not yet processed by the
* kernel. We must give some time for a kernel
* to work it out. */
/* First. Let's make it clear we want to give
* priority to anybody requiring CPU now. */
sched_yield();
sched_yield();
#if 0
/* Next, let's wait until we're the only
* process in running state. This can be
* painful on SMP.
*
* This also means fluxcapacitor won't work on
* a busy system. */
if (proc_running() > 1) {
int c = 0;
for (c = 0; c < 3 * parent->child_count; c++) {
if (proc_running() < 2)
break;
sched_yield();
}
SHOUT("[ ] Your system looks busy. I waited %i sched_yields.", c);
}
#endif
/* Now, lets wait for 1us to see if anything
* new arrived. Setting timeout to zero
* doesn't work - kernel returns immediately
* and doesn't do any work. Therefore we must
* set the timeout to a next smallest value,
* and 'select()' granularity is in us. */
timeout = NSEC_TIMEVAL(1000ULL);
int r = uevent_select(uevent, &timeout);
if (r != 0)
continue;
/* Next, make sure all processes are in 'S'
* sleeping state. They should be! */
struct child *woken = parent_woken_child(parent);
if (woken) {
int woken_pid = woken->pid;
SHOUT("[ ] %i not in 'S' state but in '%c'. "
"Waiting for a state change.",
woken_pid, woken->stat);
timeout = NSEC_TIMEVAL(1000000ULL);
uevent_select(uevent, &timeout);
continue;
}
/* Finally, send something to myself using
* localhost to make sure network buffers are
* drained. */
ping_myself();
if (parent->child_count) {
timeout = NSEC_TIMEVAL(0);
r = uevent_select(uevent, &timeout);
if (r != 0)
continue;
}
}
/* All children started? */
if (*list_of_argv) {
parent_run_one(parent, trace, *list_of_argv);
list_of_argv ++;
uevent_select(uevent, NULL);
continue;
}
/* Hurray, we're most likely waiting for a timeout. */
struct child *min_child = parent_min_timeout_child(parent);
if (min_child) {
flux_time now = (flux_time)TIMESPEC_NSEC(&uevent_now) + parent->time_drift;
flux_time speedup = min_child->blocked_until - now;
/* Don't speed up less than 10ms */
if (speedup > 0 && speedup < 10 * 1000000) {
SHOUT("[ ] %i too small speedup on %s(), waiting",
min_child->pid,
syscall_to_str(min_child->syscall_no));
timeout = NSEC_TIMEVAL(speedup);
uevent_select(uevent, &timeout);
continue;
} else if (speedup > 0) {
SHOUT("[ ] %i speeding up %s() by %.3f sec",
min_child->pid,
syscall_to_str(min_child->syscall_no),
speedup / 1000000000.0);
} else {
/* Timeout already passed, wake up the process */
speedup = 0;
SHOUT("[ ] %i waking expired %s()",
min_child->pid,
syscall_to_str(min_child->syscall_no));
}
parent->time_drift += speedup;
min_child->interrupted = 1;
child_kill(min_child, options.signo);
} else {
SHOUT("[ ] Can't speedup!");
/* Wait for any event. */
if (parent->child_count) {
timeout = NSEC_TIMEVAL(1000000000ULL);
uevent_select(uevent, &timeout);
}
}
}
parent_kill_all(parent, SIGINT);
trace_free(trace);
flux_time time_drift = parent->time_drift;
free(parent);
free(uevent);
return time_drift;
}