include/hyperman/backend_epoll.c

filter: Statementstmt 99.3% 149 / 151 (1 uncoverable) Branchbran 83.6% 77 / 98 (11 uncoverable) Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 93.1% 226 / 249 (12 uncoverable) CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/hyperman/hm_sa_board.h ↑ Summary include/hyperman/hm_win.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 "hyperman.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 63507 static int ep_ctl_io(hm_ep_state *st, int fd) {
41 struct epoll_event ev;
42 int op;
43 63507 memset(&ev, 0, sizeof(ev));
44 63507 ev.data.fd = fd;
45 63507 ▶ if (st->mask[fd] & HM_EV_READ) ev.events |= EPOLLIN;
Branch: 1 branch
BranchTrueFalseTotal
if (st->mask[fd] & HM_EV_READ) ev.events |= EPOLLIN; 31822 31685 63507
46 63507 ▶ if (st->mask[fd] & HM_EV_WRITE) ev.events |= EPOLLOUT;
Branch: 1 branch
BranchTrueFalseTotal
if (st->mask[fd] & HM_EV_WRITE) ev.events |= EPOLLOUT; 10 63497 63507
47 63507 ▶ if (st->mask[fd] == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (st->mask[fd] == 0) { 31684 31823 63507
48 31684 st->kind[fd] = EP_NONE;
49 31684 return epoll_ctl(st->ep, EPOLL_CTL_DEL, fd, NULL);
50 }
51 31823 ▶ 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; 19 31804 31823
52 31823 st->kind[fd] = EP_IO;
53 31823 return epoll_ctl(st->ep, op, fd, &ev);
54 }
55  
56 31815 static int ep_add_io(hm_backend *be, int fd, int mask, int oneshot) {
57 31815 hm_ep_state *st = (hm_ep_state *)be->state;
58 /*
59 # uncoverable condition left count:1
60 # uncoverable branch true
61 # uncoverable condition left count:2 */
62 31815 ▶ if (fd < 0 || fd >= HM_MAXFD) return -1;
Branch: 2 branches
BranchTrueFalseTotal
if (fd < 0 || fd >= HM_MAXFD) return -1; 31815 0 31815
if (fd < 0 || fd >= HM_MAXFD) return -1; 0 31815 31815
63 31815 st->mask[fd] |= (unsigned char)mask;
64 31815 ▶ if (oneshot) st->once[fd] |= (unsigned char)mask;
Branch: 1 branch
BranchTrueFalseTotal
if (oneshot) st->once[fd] |= (unsigned char)mask; 7 31808 31815
65 31808 else st->once[fd] &= (unsigned char)~mask;
66 31815 return ep_ctl_io(st, fd);
67 }
68  
69 /*
70 # uncoverable statement
71 # uncoverable subroutine */
72 0 static int ep_modify_io(hm_backend *be, int fd, int mask, int oneshot) {
73 0 return ep_add_io(be, fd, mask, oneshot); /* # uncoverable statement */
74 }
75  
76 31693 static int ep_remove_io(hm_backend *be, int fd, int mask) {
77 31693 hm_ep_state *st = (hm_ep_state *)be->state;
78 /*
79 # uncoverable condition left count:1
80 # uncoverable branch true
81 # uncoverable condition left count:2 */
82 31693 ▶ if (fd < 0 || fd >= HM_MAXFD) return -1;
Branch: 2 branches
BranchTrueFalseTotal
if (fd < 0 || fd >= HM_MAXFD) return -1; 31693 0 31693
if (fd < 0 || fd >= HM_MAXFD) return -1; 0 31693 31693
83 31693 ▶ if (st->kind[fd] != EP_IO) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (st->kind[fd] != EP_IO) return 0; 1 31692 31693
84 31692 st->mask[fd] &= (unsigned char)~mask;
85 31692 st->once[fd] &= (unsigned char)~mask;
86 31692 return ep_ctl_io(st, fd);
87 }
88  
89 33143 static int ep_add_timer(hm_backend *be, double secs, int oneshot, void *udata) {
90 33143 hm_ep_state *st = (hm_ep_state *)be->state;
91 struct itimerspec its;
92 struct epoll_event ev;
93 33143 int tfd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK | TFD_CLOEXEC);
94 33143 ▶ if (tfd < 0 || tfd >= HM_MAXFD) { if (tfd >= 0) close(tfd); return -1; } /* # uncoverable condition left count:2 */
Branch: 3 branches
BranchTrueFalseTotal
if (tfd < 0 || tfd >= HM_MAXFD) { if (tfd >= 0) close(tfd); return -1; } /* # uncoverable condition left count:2 */ 2592 30551 33143
if (tfd < 0 || tfd >= HM_MAXFD) { if (tfd >= 0) close(tfd); return -1; } /* # uncoverable condition left count:2 */ 0 2592 2592
if (tfd < 0 || tfd >= HM_MAXFD) { if (tfd >= 0) close(tfd); return -1; } /* # uncoverable condition left count:2 */ 0 30551 30551
95 2592 memset(&its, 0, sizeof(its));
96 2592 its.it_value.tv_sec = (time_t)secs;
97 2592 its.it_value.tv_nsec = (long)((secs - (double)its.it_value.tv_sec) * 1e9);
98 2592 ▶ 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) 2209 383 2592
if (its.it_value.tv_sec == 0 && its.it_value.tv_nsec == 0) 2039 170 2209
99 2039 its.it_value.tv_nsec = 1;
100 2592 ▶ if (!oneshot) its.it_interval = its.it_value;
Branch: 1 branch
BranchTrueFalseTotal
if (!oneshot) its.it_interval = its.it_value; 132 2460 2592
101 /*
102 # uncoverable branch true
103 # uncoverable condition left count:1 */
104 2592 ▶ 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 2592 2592
105 2592 st->kind[tfd] = EP_TIMER;
106 2592 st->tudata[tfd] = udata;
107 2592 ▶ st->toneshot[tfd] = (unsigned char)(oneshot ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
st->toneshot[tfd] = (unsigned char)(oneshot ? 1 : 0); 2460 132 2592
108 2592 memset(&ev, 0, sizeof(ev));
109 2592 ev.events = EPOLLIN;
110 2592 ev.data.fd = tfd;
111 2592 return epoll_ctl(st->ep, EPOLL_CTL_ADD, tfd, &ev);
112 }
113  
114 11 static int ep_del_timer(hm_backend *be, void *udata) {
115 11 hm_ep_state *st = (hm_ep_state *)be->state;
116 int fd;
117 655381 ▶ for (fd = 0; fd < HM_MAXFD; fd++) {
Branch: 1 branch
BranchTrueFalseTotal
for (fd = 0; fd < HM_MAXFD; fd++) { 655371 10 655381
118 655371 ▶ if (st->kind[fd] == EP_TIMER && st->tudata[fd] == udata) { /* # uncoverable condition right count:2 */
Branch: 2 branches
BranchTrueFalseTotal
if (st->kind[fd] == EP_TIMER && st->tudata[fd] == udata) { /* # uncoverable condition right count:2 */ 1 655370 655371
if (st->kind[fd] == EP_TIMER && st->tudata[fd] == udata) { /* # uncoverable condition right count:2 */ 1 0 1
119 1 epoll_ctl(st->ep, EPOLL_CTL_DEL, fd, NULL);
120 1 close(fd);
121 1 st->kind[fd] = EP_NONE;
122 1 st->tudata[fd] = NULL;
123 1 return 0;
124 }
125 }
126 10 return -1;
127 }
128  
129 396 static int ep_add_signal(hm_backend *be, int signo) {
130 396 hm_ep_state *st = (hm_ep_state *)be->state;
131 396 sigaddset(&st->sigs, signo);
132 /*
133 # uncoverable branch true
134 # uncoverable condition left count:1 */
135 396 ▶ 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 396 396
136 {
137 396 int sfd = signalfd(st->sfd, &st->sigs, SFD_NONBLOCK | SFD_CLOEXEC);
138 /*
139 # uncoverable branch true
140 # uncoverable condition left count:1 */
141 396 ▶ if (sfd < 0) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (sfd < 0) return -1; 0 396 396
142 396 ▶ if (st->sfd < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (st->sfd < 0) { 132 264 396
143 struct epoll_event ev;
144 132 st->sfd = sfd;
145 132 st->kind[sfd] = EP_SIGNAL;
146 132 memset(&ev, 0, sizeof(ev));
147 132 ev.events = EPOLLIN;
148 132 ev.data.fd = sfd;
149 132 return epoll_ctl(st->ep, EPOLL_CTL_ADD, sfd, &ev);
150 }
151 }
152 264 return 0;
153 }
154  
155 35319 static int ep_wait(hm_backend *be, hm_event *out, int max, double timeout) {
156 35319 hm_ep_state *st = (hm_ep_state *)be->state;
157 struct epoll_event evs[HM_MAXEV];
158 35319 ▶ int tmo_ms = timeout < 0 ? -1 : (int)(timeout * 1000);
Branch: 1 branch
BranchTrueFalseTotal
int tmo_ms = timeout < 0 ? -1 : (int)(timeout * 1000); 35317 2 35319
159 35319 int n, i, nev = 0;
160 /*
161 # uncoverable branch true
162 # uncoverable condition left count:1 */
163 35319 ▶ if (max > HM_MAXEV) max = HM_MAXEV;
Branch: 1 branch
BranchTrueFalseTotal
if (max > HM_MAXEV) max = HM_MAXEV; 0 35319 35319
164 35319 n = epoll_wait(st->ep, evs, max, tmo_ms);
165 35319 ▶ if (n < 0) return (errno == EINTR) ? 0 : -1; /* # uncoverable condition right count:2 */
Branch: 2 branches
BranchTrueFalseTotal
if (n < 0) return (errno == EINTR) ? 0 : -1; /* # uncoverable condition right count:2 */ 1 35318 35319
if (n < 0) return (errno == EINTR) ? 0 : -1; /* # uncoverable condition right count:2 */ 1 0 1
166 70734 ▶ for (i = 0; i < n && nev < max; i++) { /* # uncoverable condition right count:2 */
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < n && nev < max; i++) { /* # uncoverable condition right count:2 */ 35416 35318 70734
for (i = 0; i < n && nev < max; i++) { /* # uncoverable condition right count:2 */ 35416 0 35416
167 35416 int fd = evs[i].data.fd;
168 35416 switch (st->kind[fd]) {
169 2334 case EP_TIMER: {
170 unsigned long long exp;
171 2334 ssize_t r = read(fd, &exp, sizeof(exp));
172 (void)r;
173 2334 out[nev].kind = HM_EV_TIMER;
174 2334 out[nev].fd = -1;
175 2334 out[nev].udata = st->tudata[fd];
176 2334 nev++;
177 2334 ▶ if (st->toneshot[fd]) {
Branch: 1 branch
BranchTrueFalseTotal
if (st->toneshot[fd]) { 2319 15 2334
178 2319 epoll_ctl(st->ep, EPOLL_CTL_DEL, fd, NULL);
179 2319 close(fd);
180 2319 st->kind[fd] = EP_NONE;
181 2319 st->tudata[fd] = NULL;
182 }
183 2334 break;
184 }
185 127 case EP_SIGNAL: {
186 struct signalfd_siginfo si;
187 127 while (nev < max
188 254 ▶ && read(fd, &si, sizeof(si)) == (ssize_t)sizeof(si)) { /* # uncoverable condition right count:1 */
Branch: 2 branches
BranchTrueFalseTotal
&& read(fd, &si, sizeof(si)) == (ssize_t)sizeof(si)) { /* # uncoverable condition right count:1 */ 254 0 254
&& read(fd, &si, sizeof(si)) == (ssize_t)sizeof(si)) { /* # uncoverable condition right count:1 */ 127 127 254
189 127 out[nev].kind = HM_EV_SIGNAL;
190 127 out[nev].fd = (int)si.ssi_signo;
191 127 out[nev].udata = NULL;
192 127 nev++;
193 }
194 127 break;
195 }
196 32955 default: {
197 32955 unsigned ev = evs[i].events;
198 32955 int got = 0;
199 32955 ▶ if (ev & (EPOLLIN | EPOLLHUP | EPOLLERR)) got |= HM_EV_READ;
Branch: 1 branch
BranchTrueFalseTotal
if (ev & (EPOLLIN | EPOLLHUP | EPOLLERR)) got |= HM_EV_READ; 32943 12 32955
200 32955 ▶ if (ev & (EPOLLOUT | EPOLLHUP | EPOLLERR)) got |= HM_EV_WRITE;
Branch: 1 branch
BranchTrueFalseTotal
if (ev & (EPOLLOUT | EPOLLHUP | EPOLLERR)) got |= HM_EV_WRITE; 12 32943 32955
201 32955 got &= st->mask[fd];
202 /*
203 # uncoverable branch true
204 # uncoverable condition left count:1 */
205 32955 ▶ if (!got) got = st->mask[fd];
Branch: 1 branch
BranchTrueFalseTotal
if (!got) got = st->mask[fd]; 0 32955 32955
206 32955 ▶ if (got & HM_EV_READ) {
Branch: 1 branch
BranchTrueFalseTotal
if (got & HM_EV_READ) { 32943 12 32955
207 32943 out[nev].kind = HM_EV_READ;
208 32943 out[nev].fd = fd;
209 32943 out[nev].udata = NULL;
210 32943 nev++;
211 }
212 32955 ▶ if ((got & HM_EV_WRITE) && nev < max) { /* # uncoverable condition right count:2 */
Branch: 2 branches
BranchTrueFalseTotal
if ((got & HM_EV_WRITE) && nev < max) { /* # uncoverable condition right count:2 */ 12 32943 32955
if ((got & HM_EV_WRITE) && nev < max) { /* # uncoverable condition right count:2 */ 12 0 12
213 12 out[nev].kind = HM_EV_WRITE;
214 12 out[nev].fd = fd;
215 12 out[nev].udata = NULL;
216 12 nev++;
217 }
218 32955 ▶ if (st->once[fd] & got)
Branch: 1 branch
BranchTrueFalseTotal
if (st->once[fd] & got) 5 32950 32955
219 5 ep_remove_io(be, fd, st->once[fd] & got);
220 32955 break;
221 }
222 }
223 }
224 35318 return nev;
225 }
226  
227 154 static void ep_destroy(hm_backend *be) {
228 154 hm_ep_state *st = (hm_ep_state *)be->state;
229 /*
230 # uncoverable branch false
231 # uncoverable condition right count:1 */
232 154 ▶ if (st) {
Branch: 1 branch
BranchTrueFalseTotal
if (st) { 154 0 154
233 /* be->foreign: inherited across a fork - these descriptors are the
234 * parent's, and closing our duplicates only frees numbers the child
235 * has already reused */
236 154 ▶ if (!be->foreign) {
Branch: 1 branch
BranchTrueFalseTotal
if (!be->foreign) { 151 3 154
237 int fd;
238 9896087 ▶ for (fd = 0; fd < HM_MAXFD; fd++)
Branch: 1 branch
BranchTrueFalseTotal
for (fd = 0; fd < HM_MAXFD; fd++) 9895936 151 9896087
239 9895936 ▶ if (st->kind[fd] == EP_TIMER) close(fd);
Branch: 1 branch
BranchTrueFalseTotal
if (st->kind[fd] == EP_TIMER) close(fd); 271 9895665 9895936
240 151 ▶ if (st->sfd >= 0) close(st->sfd);
Branch: 1 branch
BranchTrueFalseTotal
if (st->sfd >= 0) close(st->sfd); 132 19 151
241 /*
242 # uncoverable branch false
243 # uncoverable condition right count:1 */
244 151 ▶ if (st->ep >= 0) close(st->ep);
Branch: 1 branch
BranchTrueFalseTotal
if (st->ep >= 0) close(st->ep); 151 0 151
245 }
246 154 free(st);
247 }
248 154 free(be);
249 154 }
250  
251 156 hm_backend *hm_backend_epoll_new(void) {
252 156 hm_backend *be = (hm_backend *)hm_xcalloc(1, sizeof(hm_backend));
253 156 hm_ep_state *st = (hm_ep_state *)hm_xcalloc(1, sizeof(hm_ep_state));
254 /*
255 # uncoverable branch true
256 # uncoverable condition left count:1
257 # uncoverable condition left count:2 */
258 156 ▶ if (!be || !st) { free(be); free(st); return NULL; }
Branch: 2 branches
BranchTrueFalseTotal
if (!be || !st) { free(be); free(st); return NULL; } 156 0 156
if (!be || !st) { free(be); free(st); return NULL; } 0 156 156
259 156 st->ep = epoll_create1(EPOLL_CLOEXEC);
260 /*
261 # uncoverable branch true
262 # uncoverable condition left count:1 */
263 156 ▶ 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 156 156
264 156 st->sfd = -1;
265 156 sigemptyset(&st->sigs);
266 156 be->name = "epoll";
267 156 be->state = st;
268 156 be->add_io = ep_add_io;
269 156 be->modify_io = ep_modify_io;
270 156 be->remove_io = ep_remove_io;
271 156 be->add_timer = ep_add_timer;
272 156 be->del_timer = ep_del_timer;
273 156 be->add_signal = ep_add_signal;
274 156 be->wait = ep_wait;
275 156 be->destroy = ep_destroy;
276 156 return be;
277 }
278  
279 5 int hm_backend_epoll_available(void) { return 1; }
280  
281 #else /* !HM_HAVE_EPOLL */
282  
283 hm_backend *hm_backend_epoll_new(void) { return 0; }
284 int hm_backend_epoll_available(void) { return 0; }
285  
286 #endif
« include/hyperman/hm_sa_board.h ↑ Summary include/hyperman/hm_win.h »