]>
Commit | Line | Data |
---|---|---|
51d0a545 HH |
1 | #define _GNU_SOURCE |
2 | #include <fcntl.h> | |
10f8438c | 3 | #include <poll.h> |
51d0a545 HH |
4 | #include <stdio.h> |
5 | #include <stdlib.h> | |
6 | #include <unistd.h> | |
7 | #include <errno.h> | |
8 | #include <limits.h> | |
9 | ||
f59664a0 HH |
10 | #define BUFLEN 4096 |
11 | ||
51d0a545 HH |
12 | int |
13 | main(int argc, char *argv[]) | |
14 | { | |
15 | int fd; | |
16 | int len, slen; | |
10f8438c LR |
17 | int ret; |
18 | int timeout; | |
19 | char *timeout_env; | |
20 | struct pollfd fds[] = {{ | |
21 | .fd = STDIN_FILENO, | |
22 | .events = POLLIN | POLLERR, | |
23 | }}; | |
24 | ||
25 | timeout_env = getenv("LOGTEE_TIMEOUT_MS"); | |
26 | if (timeout_env) | |
27 | timeout = atoi(timeout_env); | |
28 | else | |
29 | timeout = -1; | |
51d0a545 HH |
30 | |
31 | if (argc != 2) { | |
32 | fprintf(stderr, "Usage: %s <file>\n", argv[0]); | |
33 | exit(EXIT_FAILURE); | |
34 | } | |
35 | ||
10f8438c | 36 | fd = open(argv[1], O_WRONLY | O_CREAT | O_TRUNC | O_NONBLOCK, 0644); |
51d0a545 HH |
37 | if (fd == -1) { |
38 | perror("open"); | |
39 | exit(EXIT_FAILURE); | |
40 | } | |
41 | ||
42 | fprintf(stderr, "Logging to %s: ", argv[1]); | |
43 | ||
f59664a0 HH |
44 | slen = 0; |
45 | ||
51d0a545 | 46 | do { |
10f8438c LR |
47 | ret = poll (fds, sizeof(fds) / sizeof(fds[0]), timeout); |
48 | if (ret == 0) { | |
49 | fprintf (stderr, "Timed out after %d milliseconds of no output.\n", timeout); | |
50 | exit(EXIT_FAILURE); | |
51 | } | |
51d0a545 | 52 | len = splice(STDIN_FILENO, NULL, fd, NULL, |
10f8438c | 53 | BUFLEN, SPLICE_F_MOVE | SPLICE_F_NONBLOCK); |
51d0a545 HH |
54 | |
55 | if (len < 0) { | |
56 | if (errno == EAGAIN) | |
57 | continue; | |
58 | perror("tee"); | |
59 | exit(EXIT_FAILURE); | |
60 | } else | |
61 | if (len == 0) | |
62 | break; | |
f59664a0 HH |
63 | slen += len; |
64 | if ((slen/BUFLEN) > 0) { | |
65 | fprintf(stderr, "."); | |
66 | } | |
67 | slen = slen % BUFLEN; | |
68 | ||
51d0a545 HH |
69 | } while (1); |
70 | close(fd); | |
71 | fprintf(stderr, "\n"); | |
72 | exit(EXIT_SUCCESS); | |
10f8438c | 73 | } |