Commit | Line | Data |
---|---|---|
55883d9a SK |
1 | #include <errno.h> |
2 | #include <fcntl.h> | |
3 | #include <stdio.h> | |
4 | #include <stdlib.h> | |
5 | #include <string.h> | |
6 | #include <unistd.h> | |
7 | ||
8 | #include "khatus_lib_log.h" | |
9 | #include "khatus_lib_sensor.h" | |
10 | #include "khatus_lib_time.h" | |
11 | ||
12 | void | |
13 | loop(struct timespec *ti, char *fifo, char *buf, int fun(char *)) | |
14 | { | |
15 | int fd = -1; | |
16 | int w = -1; /* written */ | |
17 | int r = -1; /* remaining */ | |
18 | int i = -1; /* buffer position */ | |
19 | ||
20 | for (;;) { | |
21 | debug("openning \"%s\"\n", fifo); | |
22 | fd = open(fifo, O_WRONLY); | |
23 | if (fd < 0) | |
24 | fatal("Failed to open FIFO file: \"%s\". Error: %s\n", | |
25 | fifo, | |
26 | strerror(errno)); | |
27 | debug("openned. fd: %d\n", fd); | |
28 | r = fun(buf); | |
29 | buf[r] = END_OF_MESSAGE; | |
30 | for (i = 0; (w = write(fd, buf + i++, 1)) && r; r--) | |
31 | ; | |
32 | if (w < 0) | |
33 | switch (errno) { | |
34 | case EPIPE: | |
35 | error("Broken pipe. Msg buf: %s\n", buf); | |
36 | break; | |
37 | default: | |
38 | fatal( | |
39 | "Failed to write to %s. " | |
40 | "Err num: %d, Err msg: %s\n", | |
41 | fifo, | |
42 | errno, | |
43 | strerror(errno)); | |
44 | } | |
45 | if (close(fd) < 0) | |
46 | fatal("Failed to close %s. Err num: %d, Err msg: %s\n", | |
47 | fifo, | |
48 | errno, | |
49 | strerror(errno)); | |
50 | fd = -1; | |
51 | debug("closed. fd: %d\n", fd); | |
52 | snooze(ti); | |
53 | } | |
54 | } |