12 * Read data from a slow device
14 * return 1 when a complete NULL terminated line has been read
16 * return 0 when a partial line has been read and appended to the
19 * return negative on error
21 static int read_log_line(int infd, char *buf, size_t bufsize, int *offset)
26 ret = read(infd, buf + *offset, bufsize - *offset - 1);
33 pr_err("Read EOF, stopping\n");
36 buf[*offset + ret] = 0;
38 for (i = 0; i < ret; i++) {
39 if (buf[i + *offset] == '\n' ||
40 buf[i + *offset] == '\r') {
42 * Got a complete line when there is a newline
43 * at the end. Remove the newline and possible
44 * other junk, such as '\r'
53 * Fixme! Nothing guarantees that there isn't actually
54 * more data (a part of a new log entry perhaps) after
55 * the newline. So in rare cases (we are prevented
56 * from reading the serial line in very long time) we
57 * might lose data from the stream..
65 int read_data(int infd, int outfd)
67 struct epoll_event ev;
68 time_t start_time = 0, cur_time;
74 epoll_fd = epoll_create(1);
76 pr_err("Failed to create epoll socket: %m\n");
82 if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, infd, &ev) == -1) {
83 pr_err("epoll_ctl: %m\n");
91 ret = epoll_wait(epoll_fd, &ev, 1, -1);
96 pr_err("epoll: %m\n");
100 ret = read_log_line(infd, buf, sizeof(buf), &offset);
107 if (strlen(buf) < 5) {
108 pr_debug("discarding truncated log entry\n");
114 start_time = time(NULL);
116 cur_time = time(NULL);
118 pr_info("%s\n", buf);
123 len = snprintf(str, sizeof(str),
124 "%ld;%s\n", cur_time - start_time, buf);
126 ret = write(outfd, str, len);
128 pr_err("write: %m\n");