aboutsummaryrefslogtreecommitdiff
path: root/fdm.c
blob: e26d394cdcbc317a411d08e4bfd36556ecdb9c9c (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include "fdm.h"

#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
#include <errno.h>
#include <assert.h>
#include <sys/epoll.h>

#define LOG_MODULE "fdm"
#define LOG_ENABLE_DBG 0
#include "log.h"
#include "tllist.h"

struct fd {
    int fd;
    void *data;
    fdm_handler_t handler;
    bool deleted;
};

struct fdm {
    int epoll_fd;
    bool is_polling;
    tll(struct fd *) fds;
    tll(struct fd *) deferred_delete;
};

struct fdm *
fdm_init(void)
{
    int epoll_fd = epoll_create1(EPOLL_CLOEXEC);
    if (epoll_fd == -1) {
        LOG_ERRNO("failed to create epoll FD");
        return NULL;
    }

    struct fdm *fdm = malloc(sizeof(*fdm));
    *fdm = (struct fdm){
        .epoll_fd = epoll_fd,
        .is_polling = false,
        .fds = tll_init(),
        .deferred_delete = tll_init(),
    };
    return fdm;
}

void
fdm_destroy(struct fdm *fdm)
{
    if (fdm == NULL)
        return;

    if (tll_length(fdm->fds) > 0)
        LOG_WARN("FD list not empty");

    assert(tll_length(fdm->fds) == 0);

    tll_free(fdm->fds);
    close(fdm->epoll_fd);
    free(fdm);
}

bool
fdm_add(struct fdm *fdm, int fd, int events, fdm_handler_t handler, void *data)
{
#if defined(_DEBUG)
    tll_foreach(fdm->fds, it) {
        if (it->item->fd == fd) {
            LOG_ERR("FD=%d already registered", fd);
            return false;
        }
    }
#endif

    struct fd *fd_data = malloc(sizeof(*fd_data));
    *fd_data = (struct fd) {
        .fd = fd,
        .data = data,
        .handler = handler,
        .deleted = false,
    };

    tll_push_back(fdm->fds, fd_data);

    struct epoll_event ev = {
        .events = events,
        .data = {.ptr = fd_data},
    };

    if (epoll_ctl(fdm->epoll_fd, EPOLL_CTL_ADD, fd, &ev) < 0) {
        LOG_ERRNO("failed to register FD with epoll");
        free(fd_data);
        tll_pop_back(fdm->fds);
        return false;
    }

    return true;
}

static bool
fdm_del_internal(struct fdm *fdm, int fd, bool close_fd)
{
    if (fd == -1)
        return true;

    tll_foreach(fdm->fds, it) {
        if (it->item->fd == fd) {
            if (epoll_ctl(fdm->epoll_fd, EPOLL_CTL_DEL, fd, NULL) < 0)
                LOG_ERRNO("failed to unregister FD=%d from epoll", fd);

            if (close_fd)
                close(it->item->fd);

            it->item->deleted = true;
            if (fdm->is_polling)
                tll_push_back(fdm->deferred_delete, it->item);
            else
                free(it->item);

            tll_remove(fdm->fds, it);
            return true;
        }
    }

    LOG_ERR("no such FD: %d", fd);
    return false;
}

bool
fdm_del(struct fdm *fdm, int fd)
{
    return fdm_del_internal(fdm, fd, true);
}

bool
fdm_del_no_close(struct fdm *fdm, int fd)
{
    return fdm_del_internal(fdm, fd, false);
}

bool
fdm_poll(struct fdm *fdm)
{
    struct epoll_event events[tll_length(fdm->fds)];
    int ret = epoll_wait(fdm->epoll_fd, events, tll_length(fdm->fds), -1);
    if (ret == -1) {
        if (errno == EINTR)
            return true;

        LOG_ERRNO("failed to epoll");
        return false;
    }

    fdm->is_polling = true;
    for (int i = 0; i < ret; i++) {
        struct fd *fd = events[i].data.ptr;
        if (fd->deleted)
            continue;

        if (!fd->handler(fdm, fd->fd, events[i].events, fd->data)) {
            fdm->is_polling = false;
            return false;
        }
    }
    fdm->is_polling = false;

    tll_foreach(fdm->deferred_delete, it) {
        free(it->item);
        tll_remove(fdm->deferred_delete, it);
    }

    return true;
}