lib/Linux/FD.xs

filter: Statementstmt 72.9% 89 / 122 Branchbran 50.9% 53 / 104 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 62.8% 142 / 226 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/Linux/FD/Signal.pm ↑ Summary blib/lib/Linux/FD.pm »
1 #include <string.h>
2  
3 #include <sys/eventfd.h>
4 #include <sys/signalfd.h>
5 #include <sys/timerfd.h>
6  
7 #define PERL_NO_GET_CONTEXT
8 #define PERL_REENTR_API 1
9 #include "EXTERN.h"
10 #include "perl.h"
11 #include "XSUB.h"
12 #include "ppport.h"
13  
14 #define die_sys(format) Perl_croak(aTHX_ format, strerror(errno))
15  
16 #define NANO_SECONDS 1000000000
17  
18 4 static NV timespec_to_nv(struct timespec* time) {
19 4 return time->tv_sec + time->tv_nsec / (double)NANO_SECONDS;
20 }
21  
22 typedef struct { const char* key; size_t length; int value; } map[];
23  
24 static map clocks = {
25 { STR_WITH_LEN("monotonic") , CLOCK_MONOTONIC },
26 { STR_WITH_LEN("realtime") , CLOCK_REALTIME },
27 #ifdef CLOCK_BOOTTIME
28 { STR_WITH_LEN("boottime") , CLOCK_BOOTTIME },
29 #endif
30 #ifdef CLOCK_REALTIME_ALARM
31 { STR_WITH_LEN("realtime_alarm"), CLOCK_REALTIME_ALARM },
32 #endif
33 #ifdef CLOCK_BOOTTIME_ALARM
34 { STR_WITH_LEN("boottime_alarm"), CLOCK_BOOTTIME_ALARM },
35 #endif
36 };
37  
38 1 static clockid_t S_get_clock(pTHX_ SV* clock, const char* funcname) {
39 1 ▶ if (SvROK(clock)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(clock)) { 0 1 1
40 SV* value;
41 0 ▶ if (!SvROK(clock) || !(value = SvRV(clock)))
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(clock) || !(value = SvRV(clock))) 0 0 0
if (!SvROK(clock) || !(value = SvRV(clock))) 0 0 0
42 0 Perl_croak(aTHX_ "Could not %s: this variable is not a clock", funcname);
43 0 return SvIV(value);
44 } else {
45 int i;
46 1 const char* clock_name = SvPV_nolen(clock);
47 2 ▶ for (i = 0; i < sizeof clocks / sizeof *clocks; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < sizeof clocks / sizeof *clocks; ++i) { 2 0 2
48 2 ▶ if (strEQ(clock_name, clocks[i].key))
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(clock_name, clocks[i].key)) 1 1 2
49 1 return clocks[i].value;
50 }
51 0 Perl_croak(aTHX_ "No such timer '%s' known", clock_name);
52 }
53 }
54 #define get_clock(ref, func) S_get_clock(aTHX_ ref, func)
55  
56 6 static SV* S_io_fdopen(pTHX_ int fd, const char* classname, char type) {
57 6 PerlIO* pio = PerlIO_fdopen(fd, "r");
58 6 GV* gv = newGVgen(classname);
59 6 SV* ret = newRV_inc((SV*)gv);
60 6 hv_delete(GvSTASH(gv), GvNAME(gv), GvNAMELEN(gv), G_DISCARD);
61 6 ▶ IO* io = GvIOn(gv);
Branch: 5 branches
BranchTrueFalseTotal
IO* io = GvIOn(gv); 6 0 6
IO* io = GvIOn(gv); 0 6 6
IO* io = GvIOn(gv); 0 0 0
IO* io = GvIOn(gv); 6 0 6
IO* io = GvIOn(gv); 0 6 6
62 6 HV* stash = gv_stashpv(classname, FALSE);
63 6 IoTYPE(io) = type;
64 6 IoIFP(io) = pio;
65 6 IoOFP(io) = pio;
66 6 sv_bless(ret, stash);
67 6 return ret;
68 }
69 #define io_fdopen(fd, classname, type) S_io_fdopen(aTHX_ fd, classname, type)
70  
71 #define SET_HASH_IMPL(key,value) hv_store(hash, key, sizeof key - 1, value, 0)
72 #define SET_HASH_U(key) SET_HASH_IMPL(#key, newSVuv(buffer.ssi_##key))
73 #define SET_HASH_I(key) SET_HASH_IMPL(#key, newSViv(buffer.ssi_##key))
74  
75 5 static UV S_get_flag(pTHX_ map flags, size_t map_size, SV* flag_name) {
76 int i;
77 6 ▶ for (i = 0; i < map_size / sizeof *flags; ++i)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < map_size / sizeof *flags; ++i) 6 0 6
78 12 ▶ if (strEQ(SvPV_nolen(flag_name), flags[i].key))
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(SvPV_nolen(flag_name), flags[i].key)) 5 1 6
79 5 return flags[i].value;
80 0 Perl_croak(aTHX_ "No such flag '%s' known", SvPV_nolen(flag_name));
81 }
82 #define get_flag(map, name) S_get_flag(aTHX_ map, sizeof(map), name)
83  
84 static map event_flags = {
85 { STR_WITH_LEN("non-blocking") , EFD_NONBLOCK },
86 { STR_WITH_LEN("semaphore") , EFD_SEMAPHORE },
87 };
88 #define get_event_flag(name) get_flag(event_flags, name)
89  
90 2 static SV* S_new_eventfd(pTHX_ const char* classname, UV initial, int flags) {
91 2 int fd = eventfd(initial, flags);
92 2 ▶ if (fd < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (fd < 0) 0 2 2
93 0 die_sys("Can't open eventfd descriptor: %s");
94 2 return io_fdopen(fd, classname, '|');
95 }
96 #define new_eventfd(classname, initial, flags) S_new_eventfd(aTHX_ classname, initial, flags)
97  
98 static map signal_flags = {
99 { STR_WITH_LEN("non-blocking") , SFD_NONBLOCK },
100 };
101 #define get_signal_flag(name) get_flag(signal_flags, name)
102  
103 3 static SV* S_new_signalfd(pTHX_ const char* classname, const sigset_t* sigmask, int flags) {
104 3 int fd = signalfd(-1, sigmask, flags);
105 3 ▶ if (fd < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (fd < 0) 0 3 3
106 0 die_sys("Can't open signalfd descriptor: %s");
107 3 return io_fdopen(fd, classname, '<');
108 }
109 #define new_signalfd(classname, sigset, flags) S_new_signalfd(aTHX_ classname, sigset, flags)
110  
111 static map timer_flags = {
112 { STR_WITH_LEN("non-blocking") , TFD_NONBLOCK },
113 };
114 #define get_timer_flag(name) get_flag(timer_flags, name)
115  
116 1 static SV* S_new_timerfd(pTHX_ const char* classname, SV* clock, int flags, const char* funcname) {
117 1 clockid_t clock_id = get_clock(clock, funcname);
118 1 int fd = timerfd_create(clock_id, flags);
119 1 ▶ if (fd < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (fd < 0) 0 1 1
120 0 die_sys("Can't open timerfd descriptor: %s");
121 1 return io_fdopen(fd, classname, '<');
122 }
123 #define new_timerfd(classname, clock, flags, func) S_new_timerfd(aTHX_ classname, clock, flags, func)
124  
125 13 static int S_interrupted(pTHX_ int value) {
126 13 ▶ if (value == -1 && errno == EINTR) {
Branch: 2 branches
BranchTrueFalseTotal
if (value == -1 && errno == EINTR) { 5 8 13
if (value == -1 && errno == EINTR) { 0 5 5
127 0 ▶ PERL_ASYNC_CHECK();
Branch: 1 branch
BranchTrueFalseTotal
PERL_ASYNC_CHECK(); 0 0 0
128 0 return 1;
129 }
130 13 return 0;
131 }
132 #define interrupted(value) S_interrupted(aTHX_ value)
133  
134 #define NEVER (struct timespec) { 0 }
135  
136 typedef int Fd;
137  
138 MODULE = Linux::FD PACKAGE = Linux::FD
139  
140 BOOT:
141 3 load_module(PERL_LOADMOD_NOIMPORT, newSVpvs("IO::Handle"), NULL);
142 3 av_push(get_av("Linux::FD::Event::ISA" , GV_ADD), newSVpvs("IO::Handle"));
143 3 av_push(get_av("Linux::FD::Signal::ISA", GV_ADD), newSVpvs("IO::Handle"));
144 3 av_push(get_av("Linux::FD::Timer::ISA" , GV_ADD), newSVpvs("IO::Handle"));
145  
146 SV* eventfd(UV initial = 0, ...)
147 PREINIT:
148 2 ▶ int i, flags = EFD_CLOEXEC;
Branch: 1 branch
BranchTrueFalseTotal
int i, flags = EFD_CLOEXEC; 0 2 2
149 CODE:
150 5 ▶ for (i = 1; i < items; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < items; i++) 3 2 5
151 3 flags |= get_event_flag(ST(i));
152 2 RETVAL = new_eventfd("Linux::FD::Event", initial, flags);
153 OUTPUT:
154 RETVAL
155  
156 SV* signalfd(sigset_t* sigmask, ...)
157 PREINIT:
158 3 ▶ int i, flags = SFD_CLOEXEC;
Branch: 1 branch
BranchTrueFalseTotal
int i, flags = SFD_CLOEXEC; 1 2 3
159 CODE:
160 4 ▶ for (i = 1; i < items; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < items; i++) 1 3 4
161 1 flags |= get_signal_flag(ST(i));
162 3 RETVAL = new_signalfd("Linux::FD::Signal", sigmask, flags);
163 OUTPUT:
164 RETVAL
165  
166 SV* timerfd(SV* clock, ...)
167 PREINIT:
168 1 int i, flags = TFD_CLOEXEC;
169 CODE:
170 2 ▶ for (i = 1; i < items; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < items; i++) 1 1 2
171 1 flags |= get_timer_flag(ST(i));
172 1 RETVAL = new_timerfd("Linux::FD::Timer", clock, flags, "timerfd");
173 OUTPUT:
174 RETVAL
175  
176 MODULE = Linux::FD PACKAGE = Linux::FD::Event
177  
178 SV* new(const char* classname, UV initial = 0, ...)
179 PREINIT:
180 0 int i, flags = EFD_CLOEXEC;
181 CODE:
182 0 ▶ for (i = 2; i < items; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 2; i < items; i++) 0 0 0
183 0 flags |= get_event_flag(ST(i));
184 0 RETVAL = new_eventfd(classname, initial, flags);
185 OUTPUT:
186 RETVAL
187  
188 UV get(Fd eventfd)
189 PREINIT:
190 uint64_t buffer;
191 int ret;
192 CODE:
193 do {
194 6 ret = read(eventfd, &buffer, sizeof buffer);
195 6 ▶ } while (interrupted(ret));
Branch: 1 branch
BranchTrueFalseTotal
} while (interrupted(ret)); 0 6 6
196 6 ▶ if (ret == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (ret == -1) { 3 3 6
197 3 ▶ if (errno == EAGAIN)
Branch: 1 branch
BranchTrueFalseTotal
if (errno == EAGAIN) 3 0 3
198 3 XSRETURN_EMPTY;
199 else
200 0 die_sys("Couldn't read from eventfd: %s");
201 }
202 3 ▶ RETVAL = buffer;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = buffer; 0 3 3
203 OUTPUT:
204 RETVAL
205  
206 UV add(Fd eventfd, UV value)
207 PREINIT:
208 uint64_t buffer;
209 int ret;
210 CODE:
211 2 buffer = value;
212 do {
213 2 ret = write(eventfd, &buffer, sizeof buffer);
214 2 ▶ } while (interrupted(ret));
Branch: 1 branch
BranchTrueFalseTotal
} while (interrupted(ret)); 0 2 2
215 2 ▶ if (ret == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (ret == -1) { 0 2 2
216 0 ▶ if (errno == EAGAIN)
Branch: 1 branch
BranchTrueFalseTotal
if (errno == EAGAIN) 0 0 0
217 0 XSRETURN_EMPTY;
218 else
219 0 die_sys("Couldn't write to eventfd: %s");
220 }
221 2 ▶ RETVAL = value;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = value; 0 2 2
222 OUTPUT:
223 RETVAL
224  
225  
226 MODULE = Linux::FD PACKAGE = Linux::FD::Signal
227  
228 SV* new(const char* classname, sigset_t* sigmask, ...)
229 PREINIT:
230 0 int i, flags = SFD_CLOEXEC;
231 CODE:
232 0 ▶ for (i = 2; i < items; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 2; i < items; i++) 0 0 0
233 0 flags |= get_signal_flag(ST(i));
234 0 RETVAL = new_signalfd(classname, sigmask, flags);
235 OUTPUT:
236 RETVAL
237  
238 void set_mask(Fd fd, sigset_t* sigmask)
239 CODE:
240 0 ▶ if(signalfd(fd, sigmask, 0) == -1)
Branch: 1 branch
BranchTrueFalseTotal
if(signalfd(fd, sigmask, 0) == -1) 0 0 0
241 0 die_sys("Couldn't set_mask: %s");
242  
243 struct signalfd_siginfo receive(Fd fd)
244 PREINIT:
245 int tmp;
246 CODE:
247 do {
248 2 tmp = read(fd, &RETVAL, sizeof(RETVAL));
249 2 ▶ } while (interrupted(tmp));
Branch: 1 branch
BranchTrueFalseTotal
} while (interrupted(tmp)); 0 2 2
250 2 ▶ if (tmp == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (tmp == -1) { 1 1 2
251 1 ▶ if (errno == EAGAIN)
Branch: 1 branch
BranchTrueFalseTotal
if (errno == EAGAIN) 1 0 1
252 1 XSRETURN_EMPTY;
253 else
254 0 die_sys("Couldn't read from signalfd: %s");
255 }
256 OUTPUT:
257 RETVAL
258  
259  
260 MODULE = Linux::FD PACKAGE = Linux::FD::Timer
261  
262 SV* new(const char* classname, SV* clock, ...)
263 PREINIT:
264 0 int i, flags = TFD_CLOEXEC;
265 CODE:
266 0 ▶ for (i = 2; i < items; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 2; i < items; i++) 0 0 0
267 0 flags |= get_timer_flag(ST(i));
268 0 RETVAL = new_timerfd(classname, clock, flags, "Linux::FD::Timer->new");
269 OUTPUT:
270 RETVAL
271  
272 void get_timeout(Fd timerfd)
273 PREINIT:
274 struct itimerspec value;
275 PPCODE:
276 1 ▶ if (timerfd_gettime(timerfd, &value) == -1)
Branch: 1 branch
BranchTrueFalseTotal
if (timerfd_gettime(timerfd, &value) == -1) 0 1 1
277 0 die_sys("Couldn't get_timeout: %s");
278 1 ▶ mXPUSHn(timespec_to_nv(&value.it_value));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHn(timespec_to_nv(&value.it_value)); 0 1 1
279 1 ▶ if (GIMME_V == G_LIST)
Branch: 1 branch
BranchTrueFalseTotal
if (GIMME_V == G_LIST) 1 0 1
280 1 ▶ mXPUSHn(timespec_to_nv(&value.it_interval));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHn(timespec_to_nv(&value.it_interval)); 0 1 1
281  
282 SV* set_timeout(Fd timerfd, struct timespec new_value, struct timespec new_interval = NEVER, bool abstime = FALSE)
283 PREINIT:
284 struct itimerspec new_itimer, old_itimer;
285 PPCODE:
286 2 new_itimer.it_value = new_value;
287 2 new_itimer.it_interval = new_interval;
288 2 ▶ if (timerfd_settime(timerfd, (abstime ? TIMER_ABSTIME : 0), &new_itimer, &old_itimer) == -1)
Branch: 1 branch
BranchTrueFalseTotal
if (timerfd_settime(timerfd, (abstime ? TIMER_ABSTIME : 0), &new_itimer, &old_itimer) == -1) 0 2 2
289 0 die_sys("Couldn't set_timeout: %s");
290 2 ▶ mXPUSHn(timespec_to_nv(&old_itimer.it_value));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHn(timespec_to_nv(&old_itimer.it_value)); 0 2 2
291 2 ▶ if (GIMME_V == G_LIST)
Branch: 1 branch
BranchTrueFalseTotal
if (GIMME_V == G_LIST) 0 2 2
292 0 ▶ mXPUSHn(timespec_to_nv(&old_itimer.it_interval));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHn(timespec_to_nv(&old_itimer.it_interval)); 0 0 0
293  
294 IV receive(Fd timerfd)
295 PREINIT:
296 uint64_t buffer;
297 int ret;
298 CODE:
299 do {
300 3 ret = read(timerfd, &buffer, sizeof buffer);
301 3 ▶ } while (interrupted(ret));
Branch: 1 branch
BranchTrueFalseTotal
} while (interrupted(ret)); 0 3 3
302 3 ▶ if (ret == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (ret == -1) { 1 2 3
303 1 ▶ if (errno == EAGAIN)
Branch: 1 branch
BranchTrueFalseTotal
if (errno == EAGAIN) 1 0 1
304 1 XSRETURN_EMPTY;
305 else
306 0 die_sys("Couldn't read from timerfd: %s");
307 }
308 2 ▶ RETVAL = buffer;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = buffer; 0 2 2
309 OUTPUT:
310 RETVAL
311  
312 void clocks(SV* classname)
313 INIT:
314 int i;
315 PPCODE:
316 6 ▶ for (i = 0; i < sizeof clocks / sizeof *clocks; ++i)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < sizeof clocks / sizeof *clocks; ++i) 5 1 6
317 5 ▶ mXPUSHp(clocks[i].key, clocks[i].length);
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHp(clocks[i].key, clocks[i].length); 0 5 5
« blib/lib/Linux/FD/Signal.pm ↑ Summary blib/lib/Linux/FD.pm »