]> git.ipfire.org Git - thirdparty/dracut.git/blob - logtee.c
Revert "github workflow"
[thirdparty/dracut.git] / logtee.c
1 #define _GNU_SOURCE
2 #include <fcntl.h>
3 #include <poll.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <unistd.h>
7 #include <errno.h>
8 #include <limits.h>
9
10 #define BUFLEN 4096
11
12 int
13 main(int argc, char *argv[])
14 {
15 int fd;
16 int len, slen;
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;
30
31 if (argc != 2) {
32 fprintf(stderr, "Usage: %s <file>\n", argv[0]);
33 exit(EXIT_FAILURE);
34 }
35
36 fd = open(argv[1], O_WRONLY | O_CREAT | O_TRUNC | O_NONBLOCK, 0644);
37 if (fd == -1) {
38 perror("open");
39 exit(EXIT_FAILURE);
40 }
41
42 fprintf(stderr, "Logging to %s: ", argv[1]);
43
44 slen = 0;
45
46 do {
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 }
52 len = splice(STDIN_FILENO, NULL, fd, NULL,
53 BUFLEN, SPLICE_F_MOVE | SPLICE_F_NONBLOCK);
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;
63 slen += len;
64 if ((slen/BUFLEN) > 0) {
65 fprintf(stderr, ".");
66 }
67 slen = slen % BUFLEN;
68
69 } while (1);
70 close(fd);
71 fprintf(stderr, "\n");
72 exit(EXIT_SUCCESS);
73 }