include/fetch/backend_epoll.c

filter: Statementstmt 81.4% 123 / 151 Branchbran 55.1% 54 / 98 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 71.0% 177 / 249 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/fetch/ft_json.h ↑ Summary include/fetch/ft_ua.h »
1 /* Hyperman::Event::Epoll - the Linux readiness backend. Implements the
2 * hm_backend interface from hyperman.h: io via epoll_ctl (per-fd combined
3 * mask, oneshot emulated by clearing after delivery), timers via
4 * timerfd_create, signals via signalfd (the signals are blocked and read
5 * as data). */
6  
7 #include "ft_backend.h"
8  
9 #ifdef __linux__
10 #define HM_HAVE_EPOLL 1
11 #endif
12  
13 #ifdef HM_HAVE_EPOLL
14  
15 #include <sys/epoll.h>
16 #include <sys/timerfd.h>
17 #include <sys/signalfd.h>
18 #include <signal.h>
19 #include <string.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <errno.h>
23  
24 #define EP_NONE 0
25 #define EP_IO 1
26 #define EP_TIMER 2
27 #define EP_SIGNAL 3
28  
29 typedef struct {
30 int ep; /* epoll fd */
31 int sfd; /* signalfd, -1 until first signal */
32 sigset_t sigs;
33 unsigned char kind[HM_MAXFD]; /* EP_* per registered fd */
34 unsigned char mask[HM_MAXFD]; /* current HM_EV_ bits (io fds) */
35 unsigned char once[HM_MAXFD]; /* oneshot HM_EV_ bits (io fds) */
36 void *tudata[HM_MAXFD]; /* timerfd -> watcher udata */
37 unsigned char toneshot[HM_MAXFD];
38 } hm_ep_state;
39  
40 478 static int ep_ctl_io(hm_ep_state *st, int fd) {
41 struct epoll_event ev;
42 int op;
43 478 memset(&ev, 0, sizeof(ev));
44 478 ev.data.fd = fd;
45 478 ▶ if (st->mask[fd] & HM_EV_READ) ev.events |= EPOLLIN;
Branch: 1 branch
BranchTrueFalseTotal
if (st->mask[fd] & HM_EV_READ) ev.events |= EPOLLIN; 123 355 478
46 478 ▶ if (st->mask[fd] & HM_EV_WRITE) ev.events |= EPOLLOUT;
Branch: 1 branch
BranchTrueFalseTotal
if (st->mask[fd] & HM_EV_WRITE) ev.events |= EPOLLOUT; 122 356 478
47 478 ▶ if (st->mask[fd] == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (st->mask[fd] == 0) { 233 245 478
48 233 st->kind[fd] = EP_NONE;
49 233 return epoll_ctl(st->ep, EPOLL_CTL_DEL, fd, NULL);
50 }
51 245 ▶ op = (st->kind[fd] == EP_IO) ? EPOLL_CTL_MOD : EPOLL_CTL_ADD;
Branch: 1 branch
BranchTrueFalseTotal
op = (st->kind[fd] == EP_IO) ? EPOLL_CTL_MOD : EPOLL_CTL_ADD; 0 245 245
52 245 st->kind[fd] = EP_IO;
53 245 return epoll_ctl(st->ep, op, fd, &ev);
54 }
55  
56 245 static int ep_add_io(hm_backend *be, int fd, int mask, int oneshot) {
57 245 hm_ep_state *st = (hm_ep_state *)be->state;
58 245 ▶ if (fd < 0 || fd >= HM_MAXFD) return -1;
Branch: 2 branches
BranchTrueFalseTotal
if (fd < 0 || fd >= HM_MAXFD) return -1; 245 0 245
if (fd < 0 || fd >= HM_MAXFD) return -1; 0 245 245
59 245 st->mask[fd] |= (unsigned char)mask;
60 245 ▶ if (oneshot) st->once[fd] |= (unsigned char)mask;
Branch: 1 branch
BranchTrueFalseTotal
if (oneshot) st->once[fd] |= (unsigned char)mask; 0 245 245
61 245 else st->once[fd] &= (unsigned char)~mask;
62 245 return ep_ctl_io(st, fd);
63 }
64  
65 0 static int ep_modify_io(hm_backend *be, int fd, int mask, int oneshot) {
66 0 return ep_add_io(be, fd, mask, oneshot);
67 }
68  
69 233 static int ep_remove_io(hm_backend *be, int fd, int mask) {
70 233 hm_ep_state *st = (hm_ep_state *)be->state;
71 233 ▶ if (fd < 0 || fd >= HM_MAXFD) return -1;
Branch: 2 branches
BranchTrueFalseTotal
if (fd < 0 || fd >= HM_MAXFD) return -1; 233 0 233
if (fd < 0 || fd >= HM_MAXFD) return -1; 0 233 233
72 233 ▶ if (st->kind[fd] != EP_IO) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (st->kind[fd] != EP_IO) return 0; 0 233 233
73 233 st->mask[fd] &= (unsigned char)~mask;
74 233 st->once[fd] &= (unsigned char)~mask;
75 233 return ep_ctl_io(st, fd);
76 }
77  
78 47 static int ep_add_timer(hm_backend *be, double secs, int oneshot, void *udata) {
79 47 hm_ep_state *st = (hm_ep_state *)be->state;
80 struct itimerspec its;
81 struct epoll_event ev;
82 47 int tfd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK | TFD_CLOEXEC);
83 47 ▶ if (tfd < 0 || tfd >= HM_MAXFD) { if (tfd >= 0) close(tfd); return -1; }
Branch: 3 branches
BranchTrueFalseTotal
if (tfd < 0 || tfd >= HM_MAXFD) { if (tfd >= 0) close(tfd); return -1; } 47 0 47
if (tfd < 0 || tfd >= HM_MAXFD) { if (tfd >= 0) close(tfd); return -1; } 0 47 47
if (tfd < 0 || tfd >= HM_MAXFD) { if (tfd >= 0) close(tfd); return -1; } 0 0 0
84 47 memset(&its, 0, sizeof(its));
85 47 its.it_value.tv_sec = (time_t)secs;
86 47 its.it_value.tv_nsec = (long)((secs - (double)its.it_value.tv_sec) * 1e9);
87 47 ▶ if (its.it_value.tv_sec == 0 && its.it_value.tv_nsec == 0)
Branch: 2 branches
BranchTrueFalseTotal
if (its.it_value.tv_sec == 0 && its.it_value.tv_nsec == 0) 23 24 47
if (its.it_value.tv_sec == 0 && its.it_value.tv_nsec == 0) 0 23 23
88 0 its.it_value.tv_nsec = 1;
89 47 ▶ if (!oneshot) its.it_interval = its.it_value;
Branch: 1 branch
BranchTrueFalseTotal
if (!oneshot) its.it_interval = its.it_value; 0 47 47
90 47 ▶ if (timerfd_settime(tfd, 0, &its, NULL) < 0) { close(tfd); return -1; }
Branch: 1 branch
BranchTrueFalseTotal
if (timerfd_settime(tfd, 0, &its, NULL) < 0) { close(tfd); return -1; } 0 47 47
91 47 st->kind[tfd] = EP_TIMER;
92 47 st->tudata[tfd] = udata;
93 47 ▶ st->toneshot[tfd] = (unsigned char)(oneshot ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
st->toneshot[tfd] = (unsigned char)(oneshot ? 1 : 0); 47 0 47
94 47 memset(&ev, 0, sizeof(ev));
95 47 ev.events = EPOLLIN;
96 47 ev.data.fd = tfd;
97 47 return epoll_ctl(st->ep, EPOLL_CTL_ADD, tfd, &ev);
98 }
99  
100 24 static int ep_del_timer(hm_backend *be, void *udata) {
101 24 hm_ep_state *st = (hm_ep_state *)be->state;
102 int fd;
103 246 ▶ for (fd = 0; fd < HM_MAXFD; fd++) {
Branch: 1 branch
BranchTrueFalseTotal
for (fd = 0; fd < HM_MAXFD; fd++) { 246 0 246
104 246 ▶ if (st->kind[fd] == EP_TIMER && st->tudata[fd] == udata) {
Branch: 2 branches
BranchTrueFalseTotal
if (st->kind[fd] == EP_TIMER && st->tudata[fd] == udata) { 24 222 246
if (st->kind[fd] == EP_TIMER && st->tudata[fd] == udata) { 24 0 24
105 24 epoll_ctl(st->ep, EPOLL_CTL_DEL, fd, NULL);
106 24 close(fd);
107 24 st->kind[fd] = EP_NONE;
108 24 st->tudata[fd] = NULL;
109 24 return 0;
110 }
111 }
112 0 return -1;
113 }
114  
115 0 static int ep_add_signal(hm_backend *be, int signo) {
116 0 hm_ep_state *st = (hm_ep_state *)be->state;
117 0 sigaddset(&st->sigs, signo);
118 0 ▶ if (sigprocmask(SIG_BLOCK, &st->sigs, NULL) < 0) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (sigprocmask(SIG_BLOCK, &st->sigs, NULL) < 0) return -1; 0 0 0
119 {
120 0 int sfd = signalfd(st->sfd, &st->sigs, SFD_NONBLOCK | SFD_CLOEXEC);
121 0 ▶ if (sfd < 0) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (sfd < 0) return -1; 0 0 0
122 0 ▶ if (st->sfd < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (st->sfd < 0) { 0 0 0
123 struct epoll_event ev;
124 0 st->sfd = sfd;
125 0 st->kind[sfd] = EP_SIGNAL;
126 0 memset(&ev, 0, sizeof(ev));
127 0 ev.events = EPOLLIN;
128 0 ev.data.fd = sfd;
129 0 return epoll_ctl(st->ep, EPOLL_CTL_ADD, sfd, &ev);
130 }
131 }
132 0 return 0;
133 }
134  
135 312 static int ep_wait(hm_backend *be, hm_event *out, int max, double timeout) {
136 312 hm_ep_state *st = (hm_ep_state *)be->state;
137 struct epoll_event evs[HM_MAXEV];
138 312 ▶ int tmo_ms = timeout < 0 ? -1 : (int)(timeout * 1000);
Branch: 1 branch
BranchTrueFalseTotal
int tmo_ms = timeout < 0 ? -1 : (int)(timeout * 1000); 312 0 312
139 312 int n, i, nev = 0;
140 312 ▶ if (max > HM_MAXEV) max = HM_MAXEV;
Branch: 1 branch
BranchTrueFalseTotal
if (max > HM_MAXEV) max = HM_MAXEV; 0 312 312
141 312 n = epoll_wait(st->ep, evs, max, tmo_ms);
142 312 ▶ if (n < 0) return (errno == EINTR) ? 0 : -1;
Branch: 2 branches
BranchTrueFalseTotal
if (n < 0) return (errno == EINTR) ? 0 : -1; 0 312 312
if (n < 0) return (errno == EINTR) ? 0 : -1; 0 0 0
143 649 ▶ for (i = 0; i < n && nev < max; i++) {
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < n && nev < max; i++) { 337 312 649
for (i = 0; i < n && nev < max; i++) { 337 0 337
144 337 int fd = evs[i].data.fd;
145 337 switch (st->kind[fd]) {
146 23 case EP_TIMER: {
147 unsigned long long exp;
148 23 ssize_t r = read(fd, &exp, sizeof(exp));
149 (void)r;
150 23 out[nev].kind = HM_EV_TIMER;
151 23 out[nev].fd = -1;
152 23 out[nev].udata = st->tudata[fd];
153 23 nev++;
154 23 ▶ if (st->toneshot[fd]) {
Branch: 1 branch
BranchTrueFalseTotal
if (st->toneshot[fd]) { 23 0 23
155 23 epoll_ctl(st->ep, EPOLL_CTL_DEL, fd, NULL);
156 23 close(fd);
157 23 st->kind[fd] = EP_NONE;
158 23 st->tudata[fd] = NULL;
159 }
160 23 break;
161 }
162 0 case EP_SIGNAL: {
163 struct signalfd_siginfo si;
164 0 while (nev < max
165 0 ▶ && read(fd, &si, sizeof(si)) == (ssize_t)sizeof(si)) {
Branch: 2 branches
BranchTrueFalseTotal
&& read(fd, &si, sizeof(si)) == (ssize_t)sizeof(si)) { 0 0 0
&& read(fd, &si, sizeof(si)) == (ssize_t)sizeof(si)) { 0 0 0
166 0 out[nev].kind = HM_EV_SIGNAL;
167 0 out[nev].fd = (int)si.ssi_signo;
168 0 out[nev].udata = NULL;
169 0 nev++;
170 }
171 0 break;
172 }
173 314 default: {
174 314 unsigned ev = evs[i].events;
175 314 int got = 0;
176 314 ▶ if (ev & (EPOLLIN | EPOLLHUP | EPOLLERR)) got |= HM_EV_READ;
Branch: 1 branch
BranchTrueFalseTotal
if (ev & (EPOLLIN | EPOLLHUP | EPOLLERR)) got |= HM_EV_READ; 201 113 314
177 314 ▶ if (ev & (EPOLLOUT | EPOLLHUP | EPOLLERR)) got |= HM_EV_WRITE;
Branch: 1 branch
BranchTrueFalseTotal
if (ev & (EPOLLOUT | EPOLLHUP | EPOLLERR)) got |= HM_EV_WRITE; 134 180 314
178 314 got &= st->mask[fd];
179 314 ▶ if (!got) got = st->mask[fd];
Branch: 1 branch
BranchTrueFalseTotal
if (!got) got = st->mask[fd]; 0 314 314
180 314 ▶ if (got & HM_EV_READ) {
Branch: 1 branch
BranchTrueFalseTotal
if (got & HM_EV_READ) { 186 128 314
181 186 out[nev].kind = HM_EV_READ;
182 186 out[nev].fd = fd;
183 186 out[nev].udata = NULL;
184 186 nev++;
185 }
186 314 ▶ if ((got & HM_EV_WRITE) && nev < max) {
Branch: 2 branches
BranchTrueFalseTotal
if ((got & HM_EV_WRITE) && nev < max) { 128 186 314
if ((got & HM_EV_WRITE) && nev < max) { 128 0 128
187 128 out[nev].kind = HM_EV_WRITE;
188 128 out[nev].fd = fd;
189 128 out[nev].udata = NULL;
190 128 nev++;
191 }
192 314 ▶ if (st->once[fd] & got)
Branch: 1 branch
BranchTrueFalseTotal
if (st->once[fd] & got) 0 314 314
193 0 ep_remove_io(be, fd, st->once[fd] & got);
194 314 break;
195 }
196 }
197 }
198 312 return nev;
199 }
200  
201 48 static void ep_destroy(hm_backend *be) {
202 48 hm_ep_state *st = (hm_ep_state *)be->state;
203 48 ▶ if (st) {
Branch: 1 branch
BranchTrueFalseTotal
if (st) { 48 0 48
204 /* be->foreign: inherited across a fork - these descriptors are the
205 * parent's, and closing our duplicates only frees numbers the child
206 * has already reused */
207 48 ▶ if (!be->foreign) {
Branch: 1 branch
BranchTrueFalseTotal
if (!be->foreign) { 43 5 48
208 int fd;
209 2818091 ▶ for (fd = 0; fd < HM_MAXFD; fd++)
Branch: 1 branch
BranchTrueFalseTotal
for (fd = 0; fd < HM_MAXFD; fd++) 2818048 43 2818091
210 2818048 ▶ if (st->kind[fd] == EP_TIMER) close(fd);
Branch: 1 branch
BranchTrueFalseTotal
if (st->kind[fd] == EP_TIMER) close(fd); 0 2818048 2818048
211 43 ▶ if (st->sfd >= 0) close(st->sfd);
Branch: 1 branch
BranchTrueFalseTotal
if (st->sfd >= 0) close(st->sfd); 0 43 43
212 43 ▶ if (st->ep >= 0) close(st->ep);
Branch: 1 branch
BranchTrueFalseTotal
if (st->ep >= 0) close(st->ep); 43 0 43
213 }
214 48 free(st);
215 }
216 48 free(be);
217 48 }
218  
219 48 hm_backend *hm_backend_epoll_new(void) {
220 48 hm_backend *be = (hm_backend *)calloc(1, sizeof(hm_backend));
221 48 hm_ep_state *st = (hm_ep_state *)calloc(1, sizeof(hm_ep_state));
222 48 ▶ if (!be || !st) { free(be); free(st); return NULL; }
Branch: 2 branches
BranchTrueFalseTotal
if (!be || !st) { free(be); free(st); return NULL; } 48 0 48
if (!be || !st) { free(be); free(st); return NULL; } 0 48 48
223 48 st->ep = epoll_create1(EPOLL_CLOEXEC);
224 48 ▶ if (st->ep < 0) { free(be); free(st); return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (st->ep < 0) { free(be); free(st); return NULL; } 0 48 48
225 48 st->sfd = -1;
226 48 sigemptyset(&st->sigs);
227 48 be->name = "epoll";
228 48 be->state = st;
229 48 be->add_io = ep_add_io;
230 48 be->modify_io = ep_modify_io;
231 48 be->remove_io = ep_remove_io;
232 48 be->add_timer = ep_add_timer;
233 48 be->del_timer = ep_del_timer;
234 48 be->add_signal = ep_add_signal;
235 48 be->wait_ev = ep_wait;
236 48 be->destroy = ep_destroy;
237 48 return be;
238 }
239  
240 0 int hm_backend_epoll_available(void) { return 1; }
241  
242 #else /* !HM_HAVE_EPOLL */
243  
244 hm_backend *hm_backend_epoll_new(void) { return 0; }
245 int hm_backend_epoll_available(void) { return 0; }
246  
247 #endif
« include/fetch/ft_json.h ↑ Summary include/fetch/ft_ua.h »