lib/Linux/Epoll.xs

filter: Statementstmt 83.2% 124 / 149 Branchbran 52.3% 44 / 84 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 72.1% 168 / 233 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
↑ Summary blib/lib/Linux/Epoll.pm »
1 #ifndef _GNU_SOURCE
2 # define _GNU_SOURCE
3 #endif
4 #define GNU_STRERROR_R
5  
6 #include <math.h>
7 #include <string.h>
8  
9 #include <sys/epoll.h>
10  
11 #define PERL_NO_GET_CONTEXT
12 #include "EXTERN.h"
13 #include "perl.h"
14 #include "XSUB.h"
15 #define NEED_mg_findext
16 #include "ppport.h"
17  
18 17 static int S_get_fd(pTHX_ SV* fh) {
19 17 IO* io = sv_2io(fh);
20  
21 17 MAGIC* magic = mg_find((SV*)io, PERL_MAGIC_tiedscalar);
22 17 ▶ if (magic) {
Branch: 1 branch
BranchTrueFalseTotal
if (magic) { 1 16 17
23 1 int ret = -1;
24 1 dSP;
25 1 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 1 1
26 1 PUSHs(magic->mg_obj);
27 1 PUTBACK;
28 1 call_method("FILENO", G_SCALAR);
29 1 SPAGAIN;
30 1 ret = POPi;
31 1 PUTBACK;
32 1 return ret;
33 }
34 else
35 16 return PerlIO_fileno(IoIFP(io));
36 }
37 #define get_fd(fh) S_get_fd(aTHX_ fh)
38  
39 0 static void get_sys_error(char* buffer, size_t buffer_size) {
40 #if _POSIX_VERSION >= 200112L
41 # ifdef GNU_STRERROR_R
42 0 const char* message = strerror_r(errno, buffer, buffer_size);
43 0 ▶ if (message != buffer)
Branch: 1 branch
BranchTrueFalseTotal
if (message != buffer) 0 0 0
44 0 memcpy(buffer, message, buffer_size);
45 # else
46 strerror_r(errno, buffer, buffer_size);
47 # endif
48 #else
49 const char* message = strerror(errno);
50 strncpy(buffer, message, buffer_size - 1);
51 buffer[buffer_size - 1] = '\0';
52 #endif
53 0 }
54  
55 0 static void S_die_sys(pTHX_ const char* format) {
56 char buffer[128];
57 0 get_sys_error(buffer, sizeof buffer);
58 0 Perl_croak(aTHX_ format, buffer);
59 }
60 #define die_sys(format) S_die_sys(aTHX_ format)
61  
62 typedef struct { const char* key; size_t keylen; uint32_t value; } entry;
63 typedef entry map[];
64  
65 static map events = {
66 { "in" , 2, EPOLLIN },
67 { "out" , 3, EPOLLOUT },
68 { "err" , 3, EPOLLERR },
69 { "prio" , 4, EPOLLPRI },
70 { "et" , 2, EPOLLET },
71 { "hup" , 3, EPOLLHUP },
72 #ifdef EPOLLRDHUP
73 { "rdhup" , 5, EPOLLRDHUP },
74 #endif
75 #ifdef EPOLLWAKEUP
76 { "wakeup" , 6, EPOLLWAKEUP },
77 #endif
78 #ifdef EPOLLEXCLUSIVE
79 { "exclusive", 9, EPOLLEXCLUSIVE },
80 #endif
81 { "oneshot" , 7, EPOLLONESHOT }
82 };
83  
84 5 static uint32_t S_get_eventid(pTHX_ SV* event) {
85 STRLEN len;
86 5 const char* event_name = SvPV(event, len);
87 size_t i;
88 8 ▶ for (i = 0; i < sizeof events / sizeof *events; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < sizeof events / sizeof *events; ++i) { 8 0 8
89 8 ▶ if (events[i].keylen == len && strEQ(events[i].key, event_name))
Branch: 2 branches
BranchTrueFalseTotal
if (events[i].keylen == len && strEQ(events[i].key, event_name)) 5 3 8
if (events[i].keylen == len && strEQ(events[i].key, event_name)) 5 0 5
90 5 return events[i].value;
91 }
92 0 Perl_croak(aTHX_ "No such event type '%s' known", event_name);
93 }
94 #define get_eventid(name) S_get_eventid(aTHX_ name)
95  
96 4 static uint32_t S_event_names_to_bits(pTHX_ SV* names) {
97 4 ▶ if (SvROK(names)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(names)) { 1 3 4
98 1 AV* array = (AV*)SvRV(names);
99 1 uint32_t ret = 0;
100 int i, len;
101 1 ▶ if (SvTYPE(array) != SVt_PVAV)
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(array) != SVt_PVAV) 0 1 1
102 0 Perl_croak(aTHX_ "event names must be string or arrayref");
103 1 len = av_len(array) + 1;
104 3 ▶ for (i = 0; i < len; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < len; ++i) { 2 1 3
105 2 SV** elem = av_fetch(array, i, FALSE);
106 2 ret |= get_eventid(*elem);
107 }
108 1 return ret;
109 }
110 else
111 3 return get_eventid(names);
112 }
113 #define event_names_to_bits(name) S_event_names_to_bits(aTHX_ name)
114  
115 4 static entry* S_get_event_name(pTHX_ uint32_t event_bit) {
116 size_t i;
117 9 ▶ for (i = 0; i < sizeof events / sizeof *events; ++i)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < sizeof events / sizeof *events; ++i) 9 0 9
118 9 ▶ if (events[i].value == event_bit)
Branch: 1 branch
BranchTrueFalseTotal
if (events[i].value == event_bit) 4 5 9
119 4 return &events[i];
120 0 Perl_croak(aTHX_ "No such event type '%d' known", event_bit);
121 }
122 #define get_event_name(event_bit) S_get_event_name(aTHX_ event_bit)
123  
124 4 static CV* S_extract_cv(pTHX_ SV* sv) {
125 HV* stash;
126 GV* gv;
127 4 CV* ret = sv_2cv(sv, &stash, &gv, FALSE);
128 4 ▶ if (!ret)
Branch: 1 branch
BranchTrueFalseTotal
if (!ret) 0 4 4
129 0 Perl_croak(aTHX_ "Couldn't convert callback parameter to a CV");
130 4 return ret;
131 }
132 #define extract_cv(sv) S_extract_cv(aTHX_ sv)
133  
134 struct data {
135 AV* backrefs;
136 int index;
137 };
138  
139 1 static int weak_set(pTHX_ SV* sv, MAGIC* magic) {
140 1 struct data* data = (struct data*)magic->mg_ptr;
141 1 av_delete(data->backrefs, data->index, G_DISCARD);
142 1 return 0;
143 }
144  
145 static int weak_free(pTHX_ SV* sv, MAGIC* magic);
146  
147 static const MGVTBL epoll_magic = { NULL };
148 static const MGVTBL weak_magic = { NULL, weak_set, NULL, NULL, weak_free };
149  
150 3 static int weak_free(pTHX_ SV* sv, MAGIC* magic) {
151 3 struct data* data = (struct data*)magic->mg_ptr;
152 3 SvREFCNT_dec((SV*)data->backrefs);
153 3 return 0;
154 }
155  
156 #define get_backrefs(epoll) (AV*)mg_findext(SvRV(epoll), PERL_MAGIC_ext, &epoll_magic)->mg_obj
157  
158 4 static void S_set_backref(pTHX_ SV* epoll, SV* fh, int fd, CV* callback) {
159 4 AV* backrefs = get_backrefs(epoll);
160 4 struct data backref = { backrefs, fd };
161 4 ▶ SV* ref = sv_rvweaken(SvROK(fh) ? newSVsv(fh) : newRV_inc(fh));
Branch: 1 branch
BranchTrueFalseTotal
SV* ref = sv_rvweaken(SvROK(fh) ? newSVsv(fh) : newRV_inc(fh)); 3 1 4
162  
163 4 av_store(backrefs, fd, ref);
164 4 SvREFCNT_inc((SV*)backrefs);
165 4 sv_magicext(ref, (SV*)callback, PERL_MAGIC_ext, &weak_magic, (const char*)&backref, sizeof backref);
166 4 }
167 #define set_backref(epoll, fh, fd, cb) S_set_backref(aTHX_ epoll, fh, fd, cb)
168  
169 1 static void S_del_backref(pTHX_ SV* epoll, int fd) {
170 1 av_delete(get_backrefs(epoll), fd, G_DISCARD);
171 1 }
172 #define del_backref(epoll, fd) S_del_backref(aTHX_ epoll, fd)
173  
174 #define undef &PL_sv_undef
175  
176 2 static SV* S_io_fdopen(pTHX_ int fd, const char* package) {
177 2 PerlIO* pio = PerlIO_fdopen(fd, "r");
178 2 GV* gv = newGVgen(package);
179 2 SV* ret = newRV_inc((SV*)gv);
180 2 hv_delete(GvSTASH(gv), GvNAME(gv), GvNAMELEN(gv), G_DISCARD);
181 2 ▶ IO* io = GvIOn(gv);
Branch: 5 branches
BranchTrueFalseTotal
IO* io = GvIOn(gv); 2 0 2
IO* io = GvIOn(gv); 0 2 2
IO* io = GvIOn(gv); 0 0 0
IO* io = GvIOn(gv); 2 0 2
IO* io = GvIOn(gv); 0 2 2
182 2 IoTYPE(io) = '<';
183 2 IoIFP(io) = pio;
184 2 IoOFP(io) = pio;
185 2 sv_bless(ret, gv_stashpv(package, TRUE));
186 2 return ret;
187 }
188 #define io_fdopen(fd, package) S_io_fdopen(aTHX_ fd, package)
189  
190 3 static SV* S_event_bits_to_hash(pTHX_ UV bits) {
191 int shift;
192 3 HV* ret = newHV();
193 35 ▶ for (shift = 0; shift < 32; ++shift) {
Branch: 1 branch
BranchTrueFalseTotal
for (shift = 0; shift < 32; ++shift) { 34 1 35
194 34 int bit_value = 1 << shift;
195 34 ▶ if (bits & bit_value) {
Branch: 1 branch
BranchTrueFalseTotal
if (bits & bit_value) { 4 30 34
196 4 entry* tmp = get_event_name(bit_value);
197 4 hv_store(ret, tmp->key, tmp->keylen, &PL_sv_yes, 0);
198 4 ▶ if (bits == bit_value)
Branch: 1 branch
BranchTrueFalseTotal
if (bits == bit_value) 2 2 4
199 2 break;
200 }
201 }
202 3 return newRV_noinc((SV*)ret);
203 }
204 #define event_bits_to_hash(bits) S_event_bits_to_hash(aTHX_ bits)
205  
206 MODULE = Linux::Epoll PACKAGE = Linux::Epoll
207  
208 SV* new(const char* package)
209 PREINIT:
210 int fd;
211 CODE:
212 #ifdef EPOLL_CLOEXEC
213 2 fd = epoll_create1(EPOLL_CLOEXEC);
214 #else
215 fd = epoll_create(0);
216 #endif
217 2 ▶ if (fd < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (fd < 0) 0 2 2
218 0 die_sys("Couldn't open epollfd: %s");
219 2 RETVAL = io_fdopen(fd, package);
220 2 sv_magicext(SvRV(RETVAL), sv_2mortal((SV*)newAV()), PERL_MAGIC_ext, &epoll_magic, NULL, 0);
221 OUTPUT:
222 RETVAL
223  
224 const char* add(SV* self, SV* fh, SV* events, SV* callback)
225 PREINIT:
226 int efd, ofd;
227 struct epoll_event event;
228 CV* real_callback;
229 CODE:
230 3 efd = get_fd(self);
231 3 ofd = get_fd(fh);
232 3 event.events = event_names_to_bits(events);
233 3 real_callback = extract_cv(callback);
234 3 event.data.ptr = real_callback;
235 3 ▶ if (epoll_ctl(efd, EPOLL_CTL_ADD, ofd, &event) == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (epoll_ctl(efd, EPOLL_CTL_ADD, ofd, &event) == -1) { 0 3 3
236 0 ▶ if (GIMME_V != G_VOID && (errno == EEXIST || errno == EPERM))
Branch: 3 branches
BranchTrueFalseTotal
if (GIMME_V != G_VOID && (errno == EEXIST || errno == EPERM)) 0 0 0
if (GIMME_V != G_VOID && (errno == EEXIST || errno == EPERM)) 0 0 0
if (GIMME_V != G_VOID && (errno == EEXIST || errno == EPERM)) 0 0 0
237 0 XSRETURN_EMPTY;
238 else
239 0 die_sys("Couldn't add filehandle from epoll set: %s");
240 }
241 3 set_backref(self, fh, ofd, real_callback);
242 3 RETVAL = "0 but true";
243 OUTPUT:
244 RETVAL
245  
246 const char* modify(SV* self, SV* fh, SV* events, SV* callback)
247 PREINIT:
248 int efd, ofd;
249 struct epoll_event event;
250 CV* real_callback;
251 CODE:
252 1 efd = get_fd(self);
253 1 ofd = get_fd(fh);
254 1 event.events = event_names_to_bits(events);
255 1 real_callback = extract_cv(callback);
256 1 event.data.ptr = real_callback;
257 1 ▶ if (epoll_ctl(efd, EPOLL_CTL_MOD, ofd, &event) == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (epoll_ctl(efd, EPOLL_CTL_MOD, ofd, &event) == -1) { 0 1 1
258 0 ▶ if (GIMME_V != G_VOID && errno == ENOENT)
Branch: 2 branches
BranchTrueFalseTotal
if (GIMME_V != G_VOID && errno == ENOENT) 0 0 0
if (GIMME_V != G_VOID && errno == ENOENT) 0 0 0
259 0 XSRETURN_EMPTY;
260 else
261 0 die_sys("Couldn't modify filehandle from epoll set: %s");
262 }
263 1 set_backref(self, fh, ofd, real_callback);
264 1 RETVAL = "0 but true";
265 OUTPUT:
266 RETVAL
267  
268 const char* delete(SV* self, SV* fh)
269 PREINIT:
270 int efd, ofd;
271 CODE:
272 1 efd = get_fd(self);
273 1 ofd = get_fd(fh);
274 1 ▶ if (epoll_ctl(efd, EPOLL_CTL_DEL, ofd, NULL) == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (epoll_ctl(efd, EPOLL_CTL_DEL, ofd, NULL) == -1) { 0 1 1
275 0 ▶ if (GIMME_V != G_VOID && errno == ENOENT)
Branch: 2 branches
BranchTrueFalseTotal
if (GIMME_V != G_VOID && errno == ENOENT) 0 0 0
if (GIMME_V != G_VOID && errno == ENOENT) 0 0 0
276 0 XSRETURN_EMPTY;
277 else
278 0 die_sys("Couldn't delete filehandle from epoll set: %s");
279 }
280 1 del_backref(self, ofd);
281 1 RETVAL = "0 but true";
282 OUTPUT:
283 RETVAL
284  
285 int wait(SV* self, ssize_t maxevents = 1, SV* timeout = undef, sigset_t* sigset = NULL)
286 PREINIT:
287 int efd, i;
288 int real_timeout;
289 const sigset_t* real_sigset;
290 struct epoll_event* events;
291 CODE:
292 7 ▶ if (maxevents <= 0)
Branch: 1 branch
BranchTrueFalseTotal
if (maxevents <= 0) 0 7 7
293 0 Perl_croak(aTHX_ "Can't wait for a non-positive number of events (maxevents = %ld)", maxevents);
294 7 efd = get_fd(self);
295 7 ▶ real_timeout = SvOK(timeout) ? (int)ceil(SvNV(timeout) * 1000) : -1;
Branch: 1 branch
BranchTrueFalseTotal
real_timeout = SvOK(timeout) ? (int)ceil(SvNV(timeout) * 1000) : -1; 7 0 7
296  
297 7 events = alloca(sizeof(struct epoll_event) * maxevents);
298 7 RETVAL = epoll_pwait(efd, events, maxevents, real_timeout, sigset);
299 7 ▶ if (RETVAL == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (RETVAL == -1) { 1 6 7
300 1 ▶ if (errno != EINTR)
Branch: 1 branch
BranchTrueFalseTotal
if (errno != EINTR) 0 1 1
301 0 die_sys("Couldn't wait on epollfd: %s");
302 1 ▶ PERL_ASYNC_CHECK();
Branch: 1 branch
BranchTrueFalseTotal
PERL_ASYNC_CHECK(); 1 0 1
303 1 XSRETURN_EMPTY;
304 }
305 9 ▶ for (i = 0; i < RETVAL; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < RETVAL; ++i) { 3 6 9
306 3 SV* tmp = (SV*)events[i].data.ptr;
307 3 SvREFCNT_inc(tmp);
308 3 SAVEFREESV(tmp);
309 }
310 9 ▶ for (i = 0; i < RETVAL; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < RETVAL; ++i) { 3 6 9
311 3 CV* callback = (CV*) events[i].data.ptr;
312 3 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 3 3
313 3 ▶ mXPUSHs(event_bits_to_hash(events[i].events));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(event_bits_to_hash(events[i].events)); 0 3 3
314 3 PUTBACK;
315 3 call_sv((SV*)callback, G_VOID | G_DISCARD);
316 3 SPAGAIN;
317 }
318 OUTPUT:
319 RETVAL
320  
321 int
322 CLONE_SKIP(...)
323 CODE:
324 0 ▶ RETVAL = 1;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = 1; 0 0 0
325 OUTPUT:
326 RETVAL
327  
↑ Summary blib/lib/Linux/Epoll.pm »