-
Notifications
You must be signed in to change notification settings - Fork 0
/
utee.c
443 lines (368 loc) · 11.7 KB
/
utee.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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
#define _GNU_SOURCE
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <limits.h>
#include <stdbool.h>
#include <sys/types.h>
#include <sys/stat.h>
#ifndef VERSION
#define VERSION "unknown"
#endif
#if __STDC_VERSION__ >= 201112L
#define NORETURN _Noreturn
#else
#define NORETURN
#endif
#define NELEM(x) \
((sizeof(x)/sizeof(0[x])) / ((size_t)(!(sizeof(x) % sizeof(0[x])))))
/* an 8MB window */
#define WINDOW (8 * 1024 * 1024)
/* global variables that control program behaviour */
static int g_verbose = 0;
static bool g_force_no_thrash = false;
/* macro that only prints when verbosity is enabled (easier than messing
* with varargs and vprintf) */
#define TRACE(...) \
do { \
if (g_verbose > 0) fprintf(stderr, __VA_ARGS__); \
} while (0)
/* perror() but with a self-supplied errno */
static void perrorv(const char *msg, int err) {
/* we don't multithread, so strerror() should be fine */
fprintf(stderr, "%s: %s\n", msg, strerror(err));
}
/* simplified version of posix_fadvise */
static void fadvise(int fd, unsigned int advice) {
int err = posix_fadvise(fd, 0, 0, advice);
if (err != 0) {
perrorv("posix_fadvise", err);
}
}
static bool spliceall(int infd, int outfd, size_t len) {
while (len) {
ssize_t written = splice(infd, NULL, outfd, NULL, len,
SPLICE_F_MORE | SPLICE_F_MOVE);
if (written <= 0) {
return false;
}
len -= written;
}
return true;
}
NORETURN static void usage() {
puts("Usage: utee [OPTION]... [FILE]...\n"
"\n -v\tbe verbose"
"\n -c\tforce pagecache cleansing (even if write performance suffers a little)");
exit(EXIT_FAILURE);
}
static mode_t mode(int fd) {
struct stat st;
if (fstat(fd, &st) == -1) {
exit(EXIT_FAILURE);
}
return st.st_mode;
}
/* return a string representation of the fd type */
static const char *typestr(mode_t m) {
if (S_ISFIFO(m)) { return "pipe"; }
else if (S_ISREG(m)) { return "file"; }
else if (S_ISDIR(m)) { return "dir"; }
else if (S_ISBLK(m)) { return "special block file (device)"; }
else if (S_ISCHR(m)) { return "tty"; }
else if (S_ISSOCK(m)) { return "socket"; }
return "unknown";
}
/* asynchronously write the range to disk */
static void writeout(int fd, size_t offset, size_t len) {
/* this won't block, but will induce the kernel to perform a
* writeout of the previous window */
sync_file_range(fd, offset, len, SYNC_FILE_RANGE_WRITE);
}
static void pipesize(int fd, int size) {
if (fcntl(fd, F_SETPIPE_SZ, size) == -1) {
perror("fcntl(F_SETPIPE_SZ)");
}
}
/* write the range and force it out of the page cache */
static void discard(int fd, size_t offset, size_t len) {
/* contrary to the former call this will block, force write
* out the old range, then tell the OS we don't need it
* anymore */
sync_file_range(fd, offset, len,
SYNC_FILE_RANGE_WAIT_BEFORE |
SYNC_FILE_RANGE_WRITE |
SYNC_FILE_RANGE_WAIT_AFTER);
posix_fadvise(fd, offset, len, POSIX_FADV_DONTNEED);
}
/* tell the OS to queue the window we just wrote (idx) for writing, and
* force it to write the window before that (idx - 1) and discard it from
* the page cache */
static void swapwindow(int fd, size_t idx, size_t window) {
writeout(fd, window * idx, window);
if (idx) {
discard(fd, window * (idx - 1), window);
}
}
/* multiplex an input pipe to a bunch of output pipes */
static ssize_t muxpipe(int in, const int *out, size_t nout) {
ssize_t min = SSIZE_MAX;
size_t i = 0;
while (i < nout) {
ssize_t teed = tee(in, out[i], (size_t) INT_MAX, SPLICE_F_NONBLOCK);
if (teed == 0) {
return 0;
}
if (teed == -1) {
if (errno == EAGAIN) {
usleep(1000);
continue;
}
perror("tee()");
return -1;
}
if (teed < min) {
min = teed;
}
++i;
}
return (min == SSIZE_MAX) ? 0 : min;
}
/* drain fds to fds with splice */
static ssize_t drain(const int *in, const int *out, size_t nfds, size_t len) {
for (size_t i = 0; i < nfds; ++i) {
if (spliceall(in[i], out[i], len) == -1) {
return -1;
}
}
return len;
}
typedef union {
struct { int out, in; } fd;
int o[2];
} pipe_t;
static pipe_t xpipe() {
pipe_t p;
if (pipe(p.o) < 0) {
perror("pipe()");
memset(&p, 0x0, sizeof(p));
return p;
}
pipesize(p.fd.out, 0x100000);
return p;
}
/**
* the tee to rule all tees
*
* ,-> outp2 (output to pipes)
* tee() |-> outp1
* |-> p2w | p2r -> out3 (output to file, through a pipe)
* |-> p1w | p1r -> out2
* p --------------------> out1 (output to one file can be done directly)
* splice()
*/
static ssize_t utee(int in, int *out, size_t nout) {
bool ok = false;
int origin = 0;
int originpipe[2];
bool copyin = !S_ISFIFO(mode(in));
if (copyin) {
TRACE("input is not a pipe, using an intermediate\n");
/* we have to create an intermediate pipe to act as the origin */
if (pipe(originpipe) < 0) {
perror("pipe()");
return -1;
}
origin = originpipe[0];
}
else {
TRACE("tee'ing directly from the input pipe\n");
/* if the input is already a pipe, we can directly use it as the
* origin pipe */
origin = in;
}
pipesize(origin, 0x100000);
/* possibly slightly overallocate, doesn't matter a lot */
int *teeto = malloc((nout + 1) * sizeof(int));
int *splicefrom = malloc((nout + 1) * sizeof(int));
int *spliceto = malloc((nout + 1) * sizeof(int));
size_t ntee = 0;
size_t nsplice = 0;
/* first, fill up the final output array (spliceto) */
for (size_t i = 0; i < nout; ++i) {
/* anything that's not a pipe, goes in the splice output array */
if (!S_ISFIFO(mode(out[i]))) {
spliceto[nsplice++] = out[i];
}
}
if (nsplice == 0) {
/* It seems everything was a pipe, this is possible if a FIFO is
* passed on the commandline, e.g.: ls -l | utee myfifo | sort -k5n.
* Yet we need _something_ to drain the origin, so we force the
* last pipe to be spliced to. */
spliceto[nsplice++] = out[nout - 1];
pipesize(out[nout - 1], 0x100000);
--nout;
}
/* we always splice directly from the origin */
splicefrom[0] = origin;
/* add as much intermediate pipes as necessary to be able to feed the
* files in the spliceto array (files can't be teed to, so we need to
* tee to an intermediate pipe and then splice from it to a file). This
* loop starts at idx 1 because we already added the origin. */
for (size_t i = 1; i < nsplice; ++i) {
pipe_t p = xpipe();
if (p.fd.in == 0) goto error;
teeto[ntee++] = p.fd.in;
splicefrom[i] = p.fd.out;
}
/* loop again over the output fds, but this time only look for pipes,
* these pipes are output pipes themselves, they won't be spliced into
* another fd */
for (size_t i = 0; i < nout; ++i) {
if (S_ISFIFO(mode(out[i]))) {
teeto[ntee++] = out[i];
pipesize(out[i], 0x100000);
}
}
ssize_t written = 0;
size_t wfilled = 0;
size_t widx = 0;
/* start shuttling data */
while (1) {
/* if the input wasn't a pipe, we have to splice its data into the
* pipe we created */
if (copyin) {
ssize_t rcvd = splice(in, NULL, originpipe[1], NULL, (size_t) INT_MAX,
SPLICE_F_MORE | SPLICE_F_MOVE);
if (rcvd == -1) {
perror("input splice()");
goto error;
}
if (rcvd == 0) {
break; /* we're done */
}
TRACE("push -> %zd bytes\n", rcvd);
}
ssize_t teed = muxpipe(origin, teeto, ntee);
if (teed == -1) {
goto error;
}
if (teed == 0) {
break; /* we're done */
}
TRACE("mux 1 -> %zu, %zd bytes\n", ntee, teed);
ssize_t drained = drain(splicefrom, spliceto, nsplice, (size_t) teed);
if (drained == -1) {
goto error;
}
TRACE("drain %zu -> %zu, %zd bytes\n", nsplice, nsplice, drained);
/* do our best to not thrash the page cache, we won't be reading the
* file anyway, we don't call it on every iteration (save CPU), just
* when a window has filled up */
if (wfilled >= WINDOW) {
swapwindow(spliceto[0], widx, WINDOW);
if (g_force_no_thrash) {
for (size_t i = 1; i < nsplice; ++i) {
swapwindow(spliceto[i], widx, WINDOW);
}
}
wfilled = 0;
widx++;
}
written += teed;
wfilled += teed;
}
/* discard the last slice of data */
if (widx) {
discard(spliceto[0], WINDOW * (widx - 1), WINDOW + wfilled);
if (g_force_no_thrash) {
for (size_t i = 1; i < nsplice; ++i) {
discard(spliceto[i], WINDOW * (widx - 1), WINDOW + wfilled);
}
}
}
ok = true;
error:
/* close all pipes that we created ourselves */
if (copyin) {
close(originpipe[0]);
close(originpipe[1]);
}
for (size_t i = 1; i < nsplice; ++i) {
close(teeto[i - 1]);
close(splicefrom[i]);
}
free(splicefrom);
free(spliceto);
free(teeto);
return ok ? written : -1;
}
static int parseopts(int argc, char *argv[]) {
int option = 0;
while ((option = getopt(argc, argv, "vc")) != -1) {
switch (option) {
case 'v': g_verbose = 1; break;
case 'c': g_force_no_thrash = true; break;
default: return -1;
}
}
if (argc <= optind) {
return -1;
}
return optind;
}
int main(int argc, char *argv[]) {
int idx = parseopts(argc, argv);
if (idx == -1) {
usage();
}
TRACE("Welcome to utee version " VERSION "\n");
if (fcntl(STDOUT_FILENO, F_GETFL) & O_APPEND) {
fputs("can't output to an append-mode file, use regular tee\n", stderr);
exit(EXIT_FAILURE);
}
int nfiles = argc - idx;
int *out = calloc(nfiles + 1, sizeof(int));
/* the first one is standard output */
out[0] = STDOUT_FILENO;
for (int i = 1; i < nfiles + 1; ++i) {
int fd = open(argv[idx + i - 1], O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (fd < 0) {
perror("couldn't create file");
exit(EXIT_FAILURE);
}
out[i] = fd;
}
mode_t inmode = mode(STDIN_FILENO);
TRACE("STDIN is a %s!\n", typestr(inmode));
TRACE("STDOUT is a %s!\n", typestr(mode(STDOUT_FILENO)));
if (S_ISREG(inmode)) {
/* technically it would be best to supply POSIX_FADV_NOREUSE, but
* since that's a no-op on Linux we'll at least try to make linux
* perform more readahead. */
fadvise(STDIN_FILENO, POSIX_FADV_SEQUENTIAL);
}
ssize_t written = utee(STDIN_FILENO, out, nfiles + 1);
if (written != -1) {
TRACE("wrote %zd bytes\n", written);
}
if (S_ISREG(inmode)) {
/* restore the advice for the infd */
fadvise(STDIN_FILENO, POSIX_FADV_NORMAL);
}
for (int i = 1; i < nfiles + 1; ++i) {
if (close(out[i]) == -1) {
perror("close()");
}
}
if (close(STDIN_FILENO) == -1) {
perror("close()");
}
free(out);
exit(written == -1 ? EXIT_FAILURE : EXIT_SUCCESS);
}