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 466 static int ep_ctl_io(hm_ep_state *st, int fd) {
41 struct epoll_event ev;
42 int op;
43 466 memset(&ev, 0, sizeof(ev));
44 466 ev.data.fd = fd;
45 466 ▶ if (st->mask[fd] & HM_EV_READ) ev.events |= EPOLLIN;
Branch: 1 branch
BranchTrueFalseTotal
if (st->mask[fd] & HM_EV_READ) ev.events |= EPOLLIN; 120 346 466
46 466 ▶ if (st->mask[fd] & HM_EV_WRITE) ev.events |= EPOLLOUT;
Branch: 1 branch
BranchTrueFalseTotal
if (st->mask[fd] & HM_EV_WRITE) ev.events |= EPOLLOUT; 119 347 466
47 466 ▶ if (st->mask[fd] == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (st->mask[fd] == 0) { 227 239 466
48 227 st->kind[fd] = EP_NONE;
49 227 return epoll_ctl(st->ep, EPOLL_CTL_DEL, fd, NULL);
50 }
51 239 ▶ 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 239 239
52 239 st->kind[fd] = EP_IO;
53 239 return epoll_ctl(st->ep, op, fd, &ev);
54 }
55  
56 239 static int ep_add_io(hm_backend *be, int fd, int mask, int oneshot) {
57 239 hm_ep_state *st = (hm_ep_state *)be->state;
58 239 ▶ if (fd < 0 || fd >= HM_MAXFD) return -1;
Branch: 2 branches
BranchTrueFalseTotal
if (fd < 0 || fd >= HM_MAXFD) return -1; 239 0 239
if (fd < 0 || fd >= HM_MAXFD) return -1; 0 239 239
59 239 st->mask[fd] |= (unsigned char)mask;
60 239 ▶ if (oneshot) st->once[fd] |= (unsigned char)mask;
Branch: 1 branch
BranchTrueFalseTotal
if (oneshot) st->once[fd] |= (unsigned char)mask; 0 239 239
61 239 else st->once[fd] &= (unsigned char)~mask;
62 239 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 227 static int ep_remove_io(hm_backend *be, int fd, int mask) {
70 227 hm_ep_state *st = (hm_ep_state *)be->state;
71 227 ▶ if (fd < 0 || fd >= HM_MAXFD) return -1;
Branch: 2 branches
BranchTrueFalseTotal
if (fd < 0 || fd >= HM_MAXFD) return -1; 227 0 227
if (fd < 0 || fd >= HM_MAXFD) return -1; 0 227 227
72 227 ▶ if (st->kind[fd] != EP_IO) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (st->kind[fd] != EP_IO) return 0; 0 227 227
73 227 st->mask[fd] &= (unsigned char)~mask;
74 227 st->once[fd] &= (unsigned char)~mask;
75 227 return ep_ctl_io(st, fd);
76 }
77  
78 39 static int ep_add_timer(hm_backend *be, double secs, int oneshot, void *udata) {
79 39 hm_ep_state *st = (hm_ep_state *)be->state;
80 struct itimerspec its;
81 struct epoll_event ev;
82 39 int tfd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK | TFD_CLOEXEC);
83 39 ▶ 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; } 39 0 39
if (tfd < 0 || tfd >= HM_MAXFD) { if (tfd >= 0) close(tfd); return -1; } 0 39 39
if (tfd < 0 || tfd >= HM_MAXFD) { if (tfd >= 0) close(tfd); return -1; } 0 0 0
84 39 memset(&its, 0, sizeof(its));
85 39 its.it_value.tv_sec = (time_t)secs;
86 39 its.it_value.tv_nsec = (long)((secs - (double)its.it_value.tv_sec) * 1e9);
87 39 ▶ 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 16 39
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 39 ▶ if (!oneshot) its.it_interval = its.it_value;
Branch: 1 branch
BranchTrueFalseTotal
if (!oneshot) its.it_interval = its.it_value; 0 39 39
90 39 ▶ 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 39 39
91 39 st->kind[tfd] = EP_TIMER;
92 39 st->tudata[tfd] = udata;
93 39 ▶ st->toneshot[tfd] = (unsigned char)(oneshot ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
st->toneshot[tfd] = (unsigned char)(oneshot ? 1 : 0); 39 0 39
94 39 memset(&ev, 0, sizeof(ev));
95 39 ev.events = EPOLLIN;
96 39 ev.data.fd = tfd;
97 39 return epoll_ctl(st->ep, EPOLL_CTL_ADD, tfd, &ev);
98 }
99  
100 16 static int ep_del_timer(hm_backend *be, void *udata) {
101 16 hm_ep_state *st = (hm_ep_state *)be->state;
102 int fd;
103 166 ▶ for (fd = 0; fd < HM_MAXFD; fd++) {
Branch: 1 branch
BranchTrueFalseTotal
for (fd = 0; fd < HM_MAXFD; fd++) { 166 0 166
104 166 ▶ if (st->kind[fd] == EP_TIMER && st->tudata[fd] == udata) {
Branch: 2 branches
BranchTrueFalseTotal
if (st->kind[fd] == EP_TIMER && st->tudata[fd] == udata) { 16 150 166
if (st->kind[fd] == EP_TIMER && st->tudata[fd] == udata) { 16 0 16
105 16 epoll_ctl(st->ep, EPOLL_CTL_DEL, fd, NULL);
106 16 close(fd);
107 16 st->kind[fd] = EP_NONE;
108 16 st->tudata[fd] = NULL;
109 16 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 284 static int ep_wait(hm_backend *be, hm_event *out, int max, double timeout) {
136 284 hm_ep_state *st = (hm_ep_state *)be->state;
137 struct epoll_event evs[HM_MAXEV];
138 284 ▶ int tmo_ms = timeout < 0 ? -1 : (int)(timeout * 1000);
Branch: 1 branch
BranchTrueFalseTotal
int tmo_ms = timeout < 0 ? -1 : (int)(timeout * 1000); 284 0 284
139 284 int n, i, nev = 0;
140 284 ▶ if (max > HM_MAXEV) max = HM_MAXEV;
Branch: 1 branch
BranchTrueFalseTotal
if (max > HM_MAXEV) max = HM_MAXEV; 0 284 284
141 284 n = epoll_wait(st->ep, evs, max, tmo_ms);
142 284 ▶ if (n < 0) return (errno == EINTR) ? 0 : -1;
Branch: 2 branches
BranchTrueFalseTotal
if (n < 0) return (errno == EINTR) ? 0 : -1; 0 284 284
if (n < 0) return (errno == EINTR) ? 0 : -1; 0 0 0
143 596 ▶ for (i = 0; i < n && nev < max; i++) {
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < n && nev < max; i++) { 312 284 596
for (i = 0; i < n && nev < max; i++) { 312 0 312
144 312 int fd = evs[i].data.fd;
145 312 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 289 default: {
174 289 unsigned ev = evs[i].events;
175 289 int got = 0;
176 289 ▶ if (ev & (EPOLLIN | EPOLLHUP | EPOLLERR)) got |= HM_EV_READ;
Branch: 1 branch
BranchTrueFalseTotal
if (ev & (EPOLLIN | EPOLLHUP | EPOLLERR)) got |= HM_EV_READ; 179 110 289
177 289 ▶ if (ev & (EPOLLOUT | EPOLLHUP | EPOLLERR)) got |= HM_EV_WRITE;
Branch: 1 branch
BranchTrueFalseTotal
if (ev & (EPOLLOUT | EPOLLHUP | EPOLLERR)) got |= HM_EV_WRITE; 131 158 289
178 289 got &= st->mask[fd];
179 289 ▶ if (!got) got = st->mask[fd];
Branch: 1 branch
BranchTrueFalseTotal
if (!got) got = st->mask[fd]; 0 289 289
180 289 ▶ if (got & HM_EV_READ) {
Branch: 1 branch
BranchTrueFalseTotal
if (got & HM_EV_READ) { 164 125 289
181 164 out[nev].kind = HM_EV_READ;
182 164 out[nev].fd = fd;
183 164 out[nev].udata = NULL;
184 164 nev++;
185 }
186 289 ▶ if ((got & HM_EV_WRITE) && nev < max) {
Branch: 2 branches
BranchTrueFalseTotal
if ((got & HM_EV_WRITE) && nev < max) { 125 164 289
if ((got & HM_EV_WRITE) && nev < max) { 125 0 125
187 125 out[nev].kind = HM_EV_WRITE;
188 125 out[nev].fd = fd;
189 125 out[nev].udata = NULL;
190 125 nev++;
191 }
192 289 ▶ if (st->once[fd] & got)
Branch: 1 branch
BranchTrueFalseTotal
if (st->once[fd] & got) 0 289 289
193 0 ep_remove_io(be, fd, st->once[fd] & got);
194 289 break;
195 }
196 }
197 }
198 284 return nev;
199 }
200  
201 47 static void ep_destroy(hm_backend *be) {
202 47 hm_ep_state *st = (hm_ep_state *)be->state;
203 47 ▶ if (st) {
Branch: 1 branch
BranchTrueFalseTotal
if (st) { 47 0 47
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 47 ▶ if (!be->foreign) {
Branch: 1 branch
BranchTrueFalseTotal
if (!be->foreign) { 42 5 47
208 int fd;
209 2752554 ▶ for (fd = 0; fd < HM_MAXFD; fd++)
Branch: 1 branch
BranchTrueFalseTotal
for (fd = 0; fd < HM_MAXFD; fd++) 2752512 42 2752554
210 2752512 ▶ if (st->kind[fd] == EP_TIMER) close(fd);
Branch: 1 branch
BranchTrueFalseTotal
if (st->kind[fd] == EP_TIMER) close(fd); 0 2752512 2752512
211 42 ▶ if (st->sfd >= 0) close(st->sfd);
Branch: 1 branch
BranchTrueFalseTotal
if (st->sfd >= 0) close(st->sfd); 0 42 42
212 42 ▶ if (st->ep >= 0) close(st->ep);
Branch: 1 branch
BranchTrueFalseTotal
if (st->ep >= 0) close(st->ep); 42 0 42
213 }
214 47 free(st);
215 }
216 47 free(be);
217 47 }
218  
219 47 hm_backend *hm_backend_epoll_new(void) {
220 47 hm_backend *be = (hm_backend *)calloc(1, sizeof(hm_backend));
221 47 hm_ep_state *st = (hm_ep_state *)calloc(1, sizeof(hm_ep_state));
222 47 ▶ if (!be || !st) { free(be); free(st); return NULL; }
Branch: 2 branches
BranchTrueFalseTotal
if (!be || !st) { free(be); free(st); return NULL; } 47 0 47
if (!be || !st) { free(be); free(st); return NULL; } 0 47 47
223 47 st->ep = epoll_create1(EPOLL_CLOEXEC);
224 47 ▶ 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 47 47
225 47 st->sfd = -1;
226 47 sigemptyset(&st->sigs);
227 47 be->name = "epoll";
228 47 be->state = st;
229 47 be->add_io = ep_add_io;
230 47 be->modify_io = ep_modify_io;
231 47 be->remove_io = ep_remove_io;
232 47 be->add_timer = ep_add_timer;
233 47 be->del_timer = ep_del_timer;
234 47 be->add_signal = ep_add_signal;
235 47 be->wait_ev = ep_wait;
236 47 be->destroy = ep_destroy;
237 47 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 »