aboutsummaryrefslogtreecommitdiff
path: root/src/watcher.c
blob: 71b6c52b726c55714257fc668432f3fd2051c6ca (plain)
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
#include "watcher.h"

#include <errno.h>
#include <poll.h>
#include <stdbool.h>
#include <stdio.h>

#include "block.h"
#include "util.h"

static bool watcher_fd_is_readable(const watcher_fd* const watcher_fd) {
    return (watcher_fd->revents & POLLIN) != 0;
}

int watcher_init(watcher* const watcher, const block* const blocks,
                 const unsigned short block_count, const int signal_fd) {
    if (signal_fd == -1) {
        (void)fprintf(
            stderr,
            "error: invalid signal file descriptor passed to watcher\n");
        return 1;
    }

    watcher_fd* const fd = &watcher->fds[SIGNAL_FD];
    fd->fd = signal_fd;
    fd->events = POLLIN;

    for (unsigned short i = 0; i < block_count; ++i) {
        const int block_fd = blocks[i].pipe[READ_END];
        if (block_fd == -1) {
            (void)fprintf(
                stderr,
                "error: invalid block file descriptors passed to watcher\n");
            return 1;
        }

        watcher_fd* const fd = &watcher->fds[i];
        fd->fd = block_fd;
        fd->events = POLLIN;
    }

    return 0;
}

int watcher_poll(watcher* watcher, const int timeout_ms) {
    int event_count = poll(watcher->fds, LEN(watcher->fds), timeout_ms);

    // Don't return non-zero status for signal interruptions.
    if (event_count == -1 && errno != EINTR) {
        (void)fprintf(stderr, "error: watcher could not poll blocks\n");
        return 1;
    }

    watcher->got_signal = watcher_fd_is_readable(&watcher->fds[SIGNAL_FD]);

    watcher->active_block_count = event_count - (int)watcher->got_signal;
    unsigned short i = 0;
    unsigned short j = 0;
    while (i < event_count && j < LEN(watcher->active_blocks)) {
        if (watcher_fd_is_readable(&watcher->fds[j])) {
            watcher->active_blocks[i] = j;
            ++i;
        }

        ++j;
    }

    return 0;
}