forked from dracutdevs/dracut
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logtee.c
73 lines (63 loc) · 1.32 KB
/
logtee.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
#define _GNU_SOURCE
#include <fcntl.h>
#include <poll.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <limits.h>
#define BUFLEN 4096
int
main(int argc, char *argv[])
{
int fd;
int len, slen;
int ret;
int timeout;
char *timeout_env;
struct pollfd fds[] = {{
.fd = STDIN_FILENO,
.events = POLLIN | POLLERR,
}};
timeout_env = getenv("LOGTEE_TIMEOUT_MS");
if (timeout_env)
timeout = atoi(timeout_env);
else
timeout = -1;
if (argc != 2) {
fprintf(stderr, "Usage: %s <file>\n", argv[0]);
exit(EXIT_FAILURE);
}
fd = open(argv[1], O_WRONLY | O_CREAT | O_TRUNC | O_NONBLOCK, 0644);
if (fd == -1) {
perror("open");
exit(EXIT_FAILURE);
}
fprintf(stderr, "Logging to %s: ", argv[1]);
slen = 0;
do {
ret = poll (fds, sizeof(fds) / sizeof(fds[0]), timeout);
if (ret == 0) {
fprintf (stderr, "Timed out after %d milliseconds of no output.\n", timeout);
exit(EXIT_FAILURE);
}
len = splice(STDIN_FILENO, NULL, fd, NULL,
BUFLEN, SPLICE_F_MOVE | SPLICE_F_NONBLOCK);
if (len < 0) {
if (errno == EAGAIN)
continue;
perror("tee");
exit(EXIT_FAILURE);
} else
if (len == 0)
break;
slen += len;
if ((slen/BUFLEN) > 0) {
fprintf(stderr, ".");
}
slen = slen % BUFLEN;
} while (1);
close(fd);
fprintf(stderr, "\n");
exit(EXIT_SUCCESS);
}