Native.xs

filter: Statementstmt 73.8% 274 / 371 Branchbran 56.9% 131 / 230 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 67.3% 405 / 601 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/Net/DNS/Native.pm ↑ Summary queue.h »
1 #ifdef __linux__
2 # define _GNU_SOURCE
3 #endif
4 #include <pthread.h>
5 #include <semaphore.h>
6 #include <stdint.h>
7 #include <fcntl.h>
8 #include "EXTERN.h"
9 #include "perl.h"
10 #include "XSUB.h"
11 #include "ppport.h"
12 #include "bstree.h"
13  
14 #pragma push_macro("free")
15 #pragma push_macro("malloc")
16 #undef free
17 #undef malloc
18 #include "queue.h" // will be used outside of the main thread
19 #pragma pop_macro("free")
20 #pragma pop_macro("malloc")
21  
22 // write() is deprecated in favor of _write() - windows way
23 #if defined(WIN32) && !defined(UNDER_CE)
24 # include <io.h>
25 # define write _write
26 # define read _read
27 #endif
28  
29 // unnamed semaphores are not implemented in this POSIX compatible UNIX system
30 #ifdef PERL_DARWIN
31 # include "mysemaphore.h"
32 # define sem_t my_sem_t
33 # define sem_init my_sem_init
34 # define sem_wait my_sem_wait
35 # define sem_post my_sem_post
36 # define sem_destroy my_sem_destroy
37 #endif
38  
39 #ifdef __linux__
40 # include <link.h>
41 0 int _dl_phdr_cb(struct dl_phdr_info *info, size_t size, void *data) {
42 int i;
43 0 char *found = (char*)data;
44
45 0 ▶ if (*found) {
Branch: 1 branch
BranchTrueFalseTotal
if (*found) { 0 0 0
46 0 return *found;
47 }
48
49 0 ▶ for (i=0; i < info->dlpi_phnum; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i=0; i < info->dlpi_phnum; i++) { 0 0 0
50 0 ▶ if (instr(info->dlpi_name, "libnss_files") != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (instr(info->dlpi_name, "libnss_files") != NULL) { 0 0 0
51 0 *found = 1;
52 0 break;
53 }
54 }
55
56 0 return *found;
57 }
58 #endif
59  
60 typedef struct DNS_result DNS_result;
61  
62 typedef struct {
63 pthread_mutex_t mutex;
64 pthread_attr_t thread_attrs;
65 pthread_t *threads_pool;
66 #ifndef WIN32
67 sigset_t blocked_sig;
68 #endif
69 sem_t semaphore;
70 bstree* fd_map;
71 queue* in_queue;
72 int pool;
73 char extra_thread;
74 char notify_on_begin;
75 int extra_threads_cnt;
76 int busy_threads;
77 queue* tout_queue;
78 char forked;
79 char need_pool_reinit;
80 PerlInterpreter *perl;
81 } Net_DNS_Native;
82  
83 typedef struct {
84 Net_DNS_Native *self;
85 char *host;
86 char *service;
87 struct addrinfo *hints;
88 char extra;
89 char pool;
90 DNS_result *res;
91 } DNS_thread_arg;
92  
93 struct DNS_result {
94 int fd1;
95 int error;
96 struct addrinfo *hostinfo;
97 int type;
98 DNS_thread_arg *arg;
99 char dequeued;
100 };
101  
102 queue *DNS_instances = NULL;
103  
104 314 void *DNS_getaddrinfo(void *v_arg) {
105 314 DNS_thread_arg *arg = (DNS_thread_arg *)v_arg;
106 #ifndef WIN32
107 314 ▶ if (!arg->pool)
Branch: 1 branch
BranchTrueFalseTotal
if (!arg->pool) 280 34 314
108 280 pthread_sigmask(SIG_BLOCK, &arg->self->blocked_sig, NULL);
109 #endif
110
111 314 ▶ if (arg->self->notify_on_begin)
Branch: 1 branch
BranchTrueFalseTotal
if (arg->self->notify_on_begin) 16 298 314
112 16 write(arg->res->fd1, "1", 1);
113 314 arg->res->error = getaddrinfo(arg->host, arg->service, arg->hints, &arg->res->hostinfo);
114
115 314 pthread_mutex_lock(&arg->self->mutex);
116 314 arg->res->arg = arg;
117 314 ▶ if (arg->extra) arg->self->extra_threads_cnt--;
Branch: 1 branch
BranchTrueFalseTotal
if (arg->extra) arg->self->extra_threads_cnt--; 18 296 314
118 314 write(arg->res->fd1, "2", 1);
119 314 pthread_mutex_unlock(&arg->self->mutex);
120
121 314 return NULL;
122 }
123  
124 16 void *DNS_pool_worker(void *v_arg) {
125 16 Net_DNS_Native *self = (Net_DNS_Native*)v_arg;
126 #ifndef WIN32
127 16 pthread_sigmask(SIG_BLOCK, &self->blocked_sig, NULL);
128 #endif
129
130 50 ▶ while (sem_wait(&self->semaphore) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
while (sem_wait(&self->semaphore) == 0) { 49 0 49
131 49 pthread_mutex_lock(&self->mutex);
132 49 void *arg = queue_shift(self->in_queue);
133 49 ▶ if (arg != NULL) self->busy_threads++;
Branch: 1 branch
BranchTrueFalseTotal
if (arg != NULL) self->busy_threads++; 34 15 49
134 49 pthread_mutex_unlock(&self->mutex);
135
136 49 ▶ if (arg == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (arg == NULL) { 15 34 49
137 // this was request to quit thread
138 15 break;
139 }
140
141 34 DNS_getaddrinfo(arg);
142
143 34 pthread_mutex_lock(&self->mutex);
144 34 self->busy_threads--;
145 34 pthread_mutex_unlock(&self->mutex);
146 }
147
148 15 return NULL;
149 }
150  
151 332 void DNS_free_timedout(Net_DNS_Native *self, char force) {
152 332 ▶ if (queue_size(self->tout_queue)) {
Branch: 1 branch
BranchTrueFalseTotal
if (queue_size(self->tout_queue)) { 48 284 332
153 48 queue_iterator *it = queue_iterator_new(self->tout_queue);
154 int fd;
155 DNS_result *res;
156
157 96 ▶ while (!queue_iterator_end(it)) {
Branch: 1 branch
BranchTrueFalseTotal
while (!queue_iterator_end(it)) { 48 48 96
158 48 fd = (intptr_t)queue_at(self->tout_queue, it);
159 48 res = bstree_get(self->fd_map, fd);
160 48 ▶ if (res == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (res == NULL) { 0 48 48
161 0 goto FREE_TOUT;
162 }
163
164 48 ▶ if (force || res->arg) {
Branch: 2 branches
BranchTrueFalseTotal
if (force || res->arg) { 48 0 48
if (force || res->arg) { 9 39 48
165 9 bstree_del(self->fd_map, fd);
166 9 ▶ if (!res->error && res->hostinfo)
Branch: 2 branches
BranchTrueFalseTotal
if (!res->error && res->hostinfo) 6 3 9
if (!res->error && res->hostinfo) 6 0 6
167 6 freeaddrinfo(res->hostinfo);
168
169 9 close(fd);
170 9 close(res->fd1);
171 9 ▶ if (res->arg) {
Branch: 1 branch
BranchTrueFalseTotal
if (res->arg) { 9 0 9
172 9 ▶ if (res->arg->hints) free(res->arg->hints);
Branch: 1 branch
BranchTrueFalseTotal
if (res->arg->hints) free(res->arg->hints); 9 0 9
173 9 ▶ if (res->arg->host) Safefree(res->arg->host);
Branch: 1 branch
BranchTrueFalseTotal
if (res->arg->host) Safefree(res->arg->host); 9 0 9
174 9 ▶ if (res->arg->service) Safefree(res->arg->service);
Branch: 1 branch
BranchTrueFalseTotal
if (res->arg->service) Safefree(res->arg->service); 0 9 9
175 9 free(res->arg);
176 }
177 9 free(res);
178
179 9 FREE_TOUT:
180 9 queue_del(self->tout_queue, it);
181 9 continue;
182 }
183
184 39 queue_iterator_next(it);
185 }
186
187 48 queue_iterator_destroy(it);
188 }
189 332 }
190  
191 3 void DNS_lock_semaphore(sem_t *s) {
192 #ifdef PERL_DARWIN
193 pthread_mutex_lock(&s->lock);
194 #endif
195 3 }
196  
197 3 void DNS_unlock_semaphore(sem_t *s) {
198 #ifdef PERL_DARWIN
199 pthread_mutex_unlock(&s->lock);
200 #endif
201 3 }
202  
203 5 void DNS_before_fork_handler() {
204 5 ▶ if (queue_size(DNS_instances) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (queue_size(DNS_instances) == 0) { 0 5 5
205 0 return;
206 }
207
208 Net_DNS_Native *self;
209 5 queue_iterator *it = queue_iterator_new(DNS_instances);
210 10 ▶ while (!queue_iterator_end(it)) {
Branch: 1 branch
BranchTrueFalseTotal
while (!queue_iterator_end(it)) { 5 5 10
211 5 self = queue_at(DNS_instances, it);
212 5 pthread_mutex_lock(&self->mutex);
213 5 ▶ if (self->pool) DNS_lock_semaphore(&self->semaphore);
Branch: 1 branch
BranchTrueFalseTotal
if (self->pool) DNS_lock_semaphore(&self->semaphore); 3 2 5
214 5 queue_iterator_next(it);
215 }
216 5 queue_iterator_destroy(it);
217 }
218  
219 3 void DNS_after_fork_handler_parent() {
220 3 ▶ if (queue_size(DNS_instances) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (queue_size(DNS_instances) == 0) { 0 3 3
221 0 return;
222 }
223
224 Net_DNS_Native *self;
225 3 queue_iterator *it = queue_iterator_new(DNS_instances);
226 6 ▶ while (!queue_iterator_end(it)) {
Branch: 1 branch
BranchTrueFalseTotal
while (!queue_iterator_end(it)) { 3 3 6
227 3 self = queue_at(DNS_instances, it);
228 3 pthread_mutex_unlock(&self->mutex);
229 3 ▶ if (self->pool) DNS_unlock_semaphore(&self->semaphore);
Branch: 1 branch
BranchTrueFalseTotal
if (self->pool) DNS_unlock_semaphore(&self->semaphore); 2 1 3
230 3 queue_iterator_next(it);
231 }
232 3 queue_iterator_destroy(it);
233 }
234  
235 1 void DNS_reinit_pool(Net_DNS_Native *self) {
236 pthread_t tid;
237 int i, rc;
238
239 2 ▶ for (i=0; i<self->pool; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i=0; i<self->pool; i++) { 1 1 2
240 1 rc = pthread_create(&tid, NULL, DNS_pool_worker, (void*)self);
241 1 ▶ if (rc == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rc == 0) { 1 0 1
242 1 self->threads_pool[i] = tid;
243 }
244 else {
245 0 croak("Can't recreate thread #%d after fork: %s", i+1, strerror(rc));
246 }
247 }
248 1 }
249  
250 2 void DNS_after_fork_handler_child() {
251 2 ▶ if (queue_size(DNS_instances) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (queue_size(DNS_instances) == 0) { 0 2 2
252 0 return;
253 }
254
255 Net_DNS_Native *self;
256 2 queue_iterator *it = queue_iterator_new(DNS_instances);
257
258 4 ▶ while (!queue_iterator_end(it)) {
Branch: 1 branch
BranchTrueFalseTotal
while (!queue_iterator_end(it)) { 2 2 4
259 2 self = queue_at(DNS_instances, it);
260 2 pthread_mutex_unlock(&self->mutex);
261 2 ▶ if (self->pool) DNS_unlock_semaphore(&self->semaphore);
Branch: 1 branch
BranchTrueFalseTotal
if (self->pool) DNS_unlock_semaphore(&self->semaphore); 1 1 2
262
263 // reinitialize stuff
264 2 DNS_free_timedout(self, 1);
265
266 2 self->extra_threads_cnt = 0;
267 2 self->busy_threads = 0;
268 2 self->perl = PERL_GET_THX;
269 2 self->forked = 1;
270
271 2 ▶ if (self->pool) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->pool) { 1 1 2
272 #ifdef __NetBSD__
273 // unfortunetly under NetBSD threads created here will misbehave
274 self->need_pool_reinit = 1;
275 #else
276 1 DNS_reinit_pool(self);
277 #endif
278 }
279
280 2 queue_iterator_next(it);
281 }
282
283 2 queue_iterator_destroy(it);
284 }
285  
286 MODULE = Net::DNS::Native PACKAGE = Net::DNS::Native
287  
288 PROTOTYPES: DISABLE
289  
290 SV*
291 new(char* class, ...)
292 PREINIT:
293 Net_DNS_Native *self;
294 CODE:
295 16 ▶ if (items % 2 == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (items % 2 == 0) 0 16 16
296 0 croak("odd number of parameters");
297
298 16 Newx(self, 1, Net_DNS_Native);
299
300 int i, rc;
301 16 self->pool = 0;
302 16 self->notify_on_begin = 0;
303 16 self->extra_thread = 0;
304 16 self->extra_threads_cnt = 0;
305 16 self->busy_threads = 0;
306 16 self->forked = 0;
307 16 self->need_pool_reinit = 0;
308 16 self->perl = PERL_GET_THX;
309 #ifndef WIN32
310 16 sigfillset(&self->blocked_sig);
311 #endif
312 char *opt;
313
314 31 ▶ for (i=1; i<items; i+=2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i=1; i<items; i+=2) { 15 16 31
315 15 opt = SvPV_nolen(ST(i));
316
317 15 ▶ if (strEQ(opt, "pool")) {
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(opt, "pool")) { 9 6 15
318 9 self->pool = SvIV(ST(i+1));
319 9 ▶ if (self->pool < 0) self->pool = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (self->pool < 0) self->pool = 0; 0 9 9
320 }
321 6 ▶ else if (strEQ(opt, "extra_thread")) {
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(opt, "extra_thread")) { 3 3 6
322 3 self->extra_thread = SvIV(ST(i+1));
323 }
324 3 ▶ else if (strEQ(opt, "notify_on_begin")) {
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(opt, "notify_on_begin")) { 3 0 3
325 3 self->notify_on_begin = SvIV(ST(i+1));
326 }
327 else {
328 0 warn("unsupported option: %s", SvPV_nolen(ST(i)));
329 }
330 }
331
332 16 char attr_ok = 0, mutex_ok = 0, sem_ok = 0;
333
334 16 rc = pthread_attr_init(&self->thread_attrs);
335 16 ▶ if (rc != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rc != 0) { 0 16 16
336 0 warn("pthread_attr_init(): %s", strerror(rc));
337 0 goto FAIL;
338 }
339 16 attr_ok = 1;
340 16 rc = pthread_attr_setdetachstate(&self->thread_attrs, PTHREAD_CREATE_DETACHED);
341 16 ▶ if (rc != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rc != 0) { 0 16 16
342 0 warn("pthread_attr_setdetachstate(): %s", strerror(rc));
343 0 goto FAIL;
344 }
345 16 rc = pthread_mutex_init(&self->mutex, NULL);
346 16 ▶ if (rc != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rc != 0) { 0 16 16
347 0 warn("pthread_mutex_init(): %s", strerror(rc));
348 0 goto FAIL;
349 }
350 16 mutex_ok = 1;
351
352 16 self->in_queue = NULL;
353 16 self->threads_pool = NULL;
354
355 16 ▶ if (DNS_instances == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (DNS_instances == NULL) { 5 11 16
356 5 DNS_instances = queue_new();
357 #ifndef WIN32
358 5 rc = pthread_atfork(DNS_before_fork_handler, DNS_after_fork_handler_parent, DNS_after_fork_handler_child);
359 5 ▶ if (rc != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rc != 0) { 0 5 5
360 0 warn("Can't install fork handler: %s", strerror(rc));
361 0 goto FAIL;
362 }
363 #endif
364 }
365
366 16 ▶ if (self->pool) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->pool) { 9 7 16
367 9 ▶ if (sem_init(&self->semaphore, 0, 0) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (sem_init(&self->semaphore, 0, 0) != 0) { 0 9 9
368 0 warn("sem_init(): %s", strerror(errno));
369 0 goto FAIL;
370 }
371 9 sem_ok = 1;
372
373 9 self->threads_pool = malloc(self->pool*sizeof(pthread_t));
374 pthread_t tid;
375 9 int j = 0;
376
377 24 ▶ for (i=0; i<self->pool; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i=0; i<self->pool; i++) { 15 9 24
378 15 rc = pthread_create(&tid, NULL, DNS_pool_worker, (void*)self);
379 15 ▶ if (rc == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rc == 0) { 15 0 15
380 15 self->threads_pool[j++] = tid;
381 }
382 else {
383 0 warn("Can't create thread #%d: %s", i+1, strerror(rc));
384 }
385 }
386
387 9 ▶ if (j == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (j == 0) { 0 9 9
388 0 goto FAIL;
389 }
390
391 9 self->pool = j;
392 9 self->in_queue = queue_new();
393 }
394
395 16 self->fd_map = bstree_new();
396 16 self->tout_queue = queue_new();
397 16 RETVAL = newSV(0);
398 16 sv_setref_pv(RETVAL, class, (void *)self);
399
400 if (0) {
401 0 FAIL:
402 0 ▶ if (attr_ok) pthread_attr_destroy(&self->thread_attrs);
Branch: 1 branch
BranchTrueFalseTotal
if (attr_ok) pthread_attr_destroy(&self->thread_attrs); 0 0 0
403 0 ▶ if (mutex_ok) pthread_mutex_destroy(&self->mutex);
Branch: 1 branch
BranchTrueFalseTotal
if (mutex_ok) pthread_mutex_destroy(&self->mutex); 0 0 0
404 0 ▶ if (sem_ok) sem_destroy(&self->semaphore);
Branch: 1 branch
BranchTrueFalseTotal
if (sem_ok) sem_destroy(&self->semaphore); 0 0 0
405 0 ▶ if (self->threads_pool) free(self->threads_pool);
Branch: 1 branch
BranchTrueFalseTotal
if (self->threads_pool) free(self->threads_pool); 0 0 0
406 0 Safefree(self);
407 0 RETVAL = &PL_sv_undef;
408 }
409
410 16 queue_push(DNS_instances, self);
411 OUTPUT:
412 RETVAL
413  
414 int
415 _getaddrinfo(Net_DNS_Native *self, char *host, SV* sv_service, SV* sv_hints, int type)
416 INIT:
417 int fd[2];
418 CODE:
419 #ifdef __NetBSD__
420 if (self->need_pool_reinit) {
421 self->need_pool_reinit = 0;
422 DNS_reinit_pool(self);
423 }
424 #endif
425 314 ▶ if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, fd) != 0)
Branch: 1 branch
BranchTrueFalseTotal
if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, fd) != 0) 0 314 314
426 0 croak("socketpair(): %s", strerror(errno));
427  
428 314 fcntl(fd[0], F_SETFD, FD_CLOEXEC);
429 314 fcntl(fd[1], F_SETFD, FD_CLOEXEC);
430  
431 314 ▶ char *service = SvOK(sv_service) ? SvPV_nolen(sv_service) : "";
Branch: 1 branch
BranchTrueFalseTotal
char *service = SvOK(sv_service) ? SvPV_nolen(sv_service) : ""; 0 314 314
432 314 struct addrinfo *hints = NULL;
433
434 314 ▶ if (SvOK(sv_hints)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(sv_hints)) { 81 233 314
435 // defined
436 81 ▶ if (!SvROK(sv_hints) || SvTYPE(SvRV(sv_hints)) != SVt_PVHV) {
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(sv_hints) || SvTYPE(SvRV(sv_hints)) != SVt_PVHV) { 81 0 81
if (!SvROK(sv_hints) || SvTYPE(SvRV(sv_hints)) != SVt_PVHV) { 0 81 81
437 // not reference or not a hash inside reference
438 0 croak("hints should be reference to hash");
439 }
440
441 81 hints = malloc(sizeof(struct addrinfo));
442 81 hints->ai_flags = 0;
443 81 hints->ai_family = AF_UNSPEC;
444 81 hints->ai_socktype = 0;
445 81 hints->ai_protocol = 0;
446 81 hints->ai_addrlen = 0;
447 81 hints->ai_addr = NULL;
448 81 hints->ai_canonname = NULL;
449 81 hints->ai_next = NULL;
450
451 81 HV* hv_hints = (HV*)SvRV(sv_hints);
452
453 81 SV **flags_ptr = hv_fetch(hv_hints, "flags", 5, 0);
454 81 ▶ if (flags_ptr != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (flags_ptr != NULL) { 45 36 81
455 45 hints->ai_flags = SvIV(*flags_ptr);
456 }
457
458 81 SV **family_ptr = hv_fetch(hv_hints, "family", 6, 0);
459 81 ▶ if (family_ptr != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (family_ptr != NULL) { 81 0 81
460 81 hints->ai_family = SvIV(*family_ptr);
461 }
462
463 81 SV **socktype_ptr = hv_fetch(hv_hints, "socktype", 8, 0);
464 81 ▶ if (socktype_ptr != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (socktype_ptr != NULL) { 81 0 81
465 81 hints->ai_socktype = SvIV(*socktype_ptr);
466 }
467
468 81 SV **protocol_ptr = hv_fetch(hv_hints, "protocol", 8, 0);
469 81 ▶ if (protocol_ptr != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (protocol_ptr != NULL) { 0 81 81
470 0 hints->ai_protocol = SvIV(*protocol_ptr);
471 }
472 }
473
474 314 DNS_result *res = malloc(sizeof(DNS_result));
475 314 res->fd1 = fd[1];
476 314 res->error = 0;
477 314 res->hostinfo = NULL;
478 314 res->type = type;
479 314 res->arg = NULL;
480 314 res->dequeued = 0;
481
482 314 DNS_thread_arg *arg = malloc(sizeof(DNS_thread_arg));
483 314 arg->self = self;
484 314 ▶ arg->host = strlen(host) ? savepv(host) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
arg->host = strlen(host) ? savepv(host) : NULL; 314 0 314
485 314 ▶ arg->service = strlen(service) ? savepv(service) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
arg->service = strlen(service) ? savepv(service) : NULL; 0 314 314
486 314 arg->hints = hints;
487 314 arg->extra = 0;
488 314 arg->pool = 0;
489 314 arg->res = res;
490
491 314 pthread_mutex_lock(&self->mutex);
492 314 DNS_free_timedout(self, 0);
493 314 bstree_put(self->fd_map, fd[0], res);
494 314 ▶ if (self->pool) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->pool) { 52 262 314
495 52 ▶ if (self->busy_threads == self->pool && (self->extra_thread || queue_size(self->tout_queue) > self->extra_threads_cnt)) {
Branch: 3 branches
BranchTrueFalseTotal
if (self->busy_threads == self->pool && (self->extra_thread || queue_size(self->tout_queue) > self->extra_threads_cnt)) { 35 17 52
if (self->busy_threads == self->pool && (self->extra_thread || queue_size(self->tout_queue) > self->extra_threads_cnt)) { 20 15 35
if (self->busy_threads == self->pool && (self->extra_thread || queue_size(self->tout_queue) > self->extra_threads_cnt)) { 3 17 20
496 18 arg->extra = 1;
497 18 self->extra_threads_cnt++;
498 }
499 else {
500 34 arg->pool = 1;
501 34 queue_push(self->in_queue, arg);
502 34 sem_post(&self->semaphore);
503 }
504 }
505 314 pthread_mutex_unlock(&self->mutex);
506
507 314 ▶ if (!self->pool || arg->extra) {
Branch: 2 branches
BranchTrueFalseTotal
if (!self->pool || arg->extra) { 52 262 314
if (!self->pool || arg->extra) { 18 34 52
508 pthread_t tid;
509 280 int rc = pthread_create(&tid, &self->thread_attrs, DNS_getaddrinfo, (void *)arg);
510 280 ▶ if (rc != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rc != 0) { 0 280 280
511 0 ▶ if (arg->host) Safefree(arg->host);
Branch: 1 branch
BranchTrueFalseTotal
if (arg->host) Safefree(arg->host); 0 0 0
512 0 ▶ if (arg->service) Safefree(arg->service);
Branch: 1 branch
BranchTrueFalseTotal
if (arg->service) Safefree(arg->service); 0 0 0
513 0 free(arg);
514 0 free(res);
515 0 ▶ if (hints) free(hints);
Branch: 1 branch
BranchTrueFalseTotal
if (hints) free(hints); 0 0 0
516 0 pthread_mutex_lock(&self->mutex);
517 0 bstree_del(self->fd_map, fd[0]);
518 0 pthread_mutex_unlock(&self->mutex);
519 0 close(fd[0]);
520 0 close(fd[1]);
521 0 croak("pthread_create(): %s", strerror(rc));
522 }
523 }
524
525 314 ▶ RETVAL = fd[0];
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = fd[0]; 301 13 314
526 OUTPUT:
527 RETVAL
528  
529 void
530 _get_result(Net_DNS_Native *self, int fd)
531 PPCODE:
532 317 pthread_mutex_lock(&self->mutex);
533 317 DNS_result *res = bstree_get(self->fd_map, fd);
534 317 bstree_del(self->fd_map, fd);
535 317 pthread_mutex_unlock(&self->mutex);
536
537 317 ▶ if (res == NULL) croak("attempt to get result which doesn't exists");
Branch: 1 branch
BranchTrueFalseTotal
if (res == NULL) croak("attempt to get result which doesn't exists"); 12 305 317
538 305 ▶ if (!res->arg) {
Branch: 1 branch
BranchTrueFalseTotal
if (!res->arg) { 0 305 305
539 0 pthread_mutex_lock(&self->mutex);
540 0 bstree_put(self->fd_map, fd, res);
541 0 pthread_mutex_unlock(&self->mutex);
542 0 croak("attempt to get not ready result");
543 }
544
545 305 ▶ XPUSHs(sv_2mortal(newSViv(res->type)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSViv(res->type))); 0 305 305
546 305 SV *err = newSV(0);
547 305 sv_setiv(err, (IV)res->error);
548 305 ▶ sv_setpv(err, res->error ? gai_strerror(res->error) : "");
Branch: 1 branch
BranchTrueFalseTotal
sv_setpv(err, res->error ? gai_strerror(res->error) : ""); 23 282 305
549 305 SvIOK_on(err);
550 305 ▶ XPUSHs(sv_2mortal(err));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(err)); 0 305 305
551
552 305 ▶ if (!res->error) {
Branch: 1 branch
BranchTrueFalseTotal
if (!res->error) { 282 23 305
553 struct addrinfo *info;
554 1848 ▶ for (info = res->hostinfo; info != NULL; info = info->ai_next) {
Branch: 1 branch
BranchTrueFalseTotal
for (info = res->hostinfo; info != NULL; info = info->ai_next) { 1566 282 1848
555 1566 HV *hv_info = newHV();
556 1566 hv_store(hv_info, "family", 6, newSViv(info->ai_family), 0);
557 1566 hv_store(hv_info, "socktype", 8, newSViv(info->ai_socktype), 0);
558 1566 hv_store(hv_info, "protocol", 8, newSViv(info->ai_protocol), 0);
559 1566 hv_store(hv_info, "addr", 4, newSVpvn((char*)info->ai_addr, info->ai_addrlen), 0);
560 1566 ▶ hv_store(hv_info, "canonname", 9, info->ai_canonname ? newSVpv(info->ai_canonname, 0) : newSV(0), 0);
Branch: 1 branch
BranchTrueFalseTotal
hv_store(hv_info, "canonname", 9, info->ai_canonname ? newSVpv(info->ai_canonname, 0) : newSV(0), 0); 30 1536 1566
561 1566 ▶ XPUSHs(sv_2mortal(newRV_noinc((SV*)hv_info)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newRV_noinc((SV*)hv_info))); 0 1566 1566
562 }
563
564 282 ▶ if (res->hostinfo) freeaddrinfo(res->hostinfo);
Branch: 1 branch
BranchTrueFalseTotal
if (res->hostinfo) freeaddrinfo(res->hostinfo); 282 0 282
565 }
566
567 305 close(fd);
568 305 close(res->fd1);
569 305 ▶ if (res->arg->hints) free(res->arg->hints);
Branch: 1 branch
BranchTrueFalseTotal
if (res->arg->hints) free(res->arg->hints); 72 233 305
570 305 ▶ if (res->arg->host) Safefree(res->arg->host);
Branch: 1 branch
BranchTrueFalseTotal
if (res->arg->host) Safefree(res->arg->host); 305 0 305
571 305 ▶ if (res->arg->service) Safefree(res->arg->service);
Branch: 1 branch
BranchTrueFalseTotal
if (res->arg->service) Safefree(res->arg->service); 0 305 305
572 305 free(res->arg);
573 305 free(res);
574  
575 void
576 _timedout(Net_DNS_Native *self, int fd)
577 PPCODE:
578 9 char unknown = 0;
579
580 9 pthread_mutex_lock(&self->mutex);
581 9 ▶ if (bstree_get(self->fd_map, fd) == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (bstree_get(self->fd_map, fd) == NULL) { 0 9 9
582 0 unknown = 1;
583 }
584 else {
585 9 queue_push(self->tout_queue, (void*)(intptr_t)fd);
586 }
587 9 pthread_mutex_unlock(&self->mutex);
588
589 9 ▶ if (unknown)
Branch: 1 branch
BranchTrueFalseTotal
if (unknown) 0 9 9
590 0 croak("attempt to set timeout on unknown source");
591  
592 void
593 DESTROY(Net_DNS_Native *self)
594 CODE:
595 16 ▶ if (PERL_GET_THX != self->perl) {
Branch: 1 branch
BranchTrueFalseTotal
if (PERL_GET_THX != self->perl) { 0 16 16
596 // attempt to destroy from another perl thread
597 0 return;
598 }
599
600 16 ▶ if (self->pool) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->pool) { 9 7 16
601 9 pthread_mutex_lock(&self->mutex);
602 9 ▶ if (queue_size(self->in_queue) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (queue_size(self->in_queue) > 0) { 0 9 9
603 // warnings are useless in global destruction
604 0 ▶ if (!PL_dirty)
Branch: 1 branch
BranchTrueFalseTotal
if (!PL_dirty) 0 0 0
605 0 warn("destroying Net::DNS::Native object while queue for resolver has %d elements", queue_size(self->in_queue));
606
607 0 queue_iterator *it = queue_iterator_new(self->in_queue);
608 DNS_thread_arg *arg;
609
610 0 ▶ while (!queue_iterator_end(it)) {
Branch: 1 branch
BranchTrueFalseTotal
while (!queue_iterator_end(it)) { 0 0 0
611 0 arg = queue_at(self->in_queue, it);
612 0 arg->res->dequeued = 1;
613 0 free(arg);
614 0 queue_iterator_next(it);
615 }
616
617 0 queue_iterator_destroy(it);
618 0 queue_clear(self->in_queue);
619 }
620 9 pthread_mutex_unlock(&self->mutex);
621
622 int i;
623 24 ▶ for (i=0; i<self->pool; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i=0; i<self->pool; i++) { 15 9 24
624 15 sem_post(&self->semaphore);
625 }
626
627 void *rv;
628
629 24 ▶ for (i=0; i<self->pool; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i=0; i<self->pool; i++) { 15 9 24
630 #ifdef __NetBSD__
631 // unfortunetly NetBSD can join only first thread after fork
632 if (self->forked && i > 0) break;
633 #endif
634 15 pthread_join(self->threads_pool[i], &rv);
635 }
636
637 9 queue_destroy(self->in_queue);
638 9 free(self->threads_pool);
639 9 sem_destroy(&self->semaphore);
640 }
641
642 16 pthread_mutex_lock(&self->mutex);
643 16 DNS_free_timedout(self, 0);
644 16 pthread_mutex_unlock(&self->mutex);
645
646 16 ▶ if (bstree_size(self->fd_map) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (bstree_size(self->fd_map) > 0) { 0 16 16
647 0 ▶ if (!PL_dirty)
Branch: 1 branch
BranchTrueFalseTotal
if (!PL_dirty) 0 0 0
648 0 warn("destroying Net::DNS::Native object with %d non-received results", bstree_size(self->fd_map));
649
650 0 int *fds = bstree_keys(self->fd_map);
651 int i, l, j;
652 char buf[1];
653
654 0 ▶ for (i=0, l=bstree_size(self->fd_map); i<l; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i=0, l=bstree_size(self->fd_map); i<l; i++) { 0 0 0
655 0 DNS_result *res = bstree_get(self->fd_map, fds[i]);
656
657 0 ▶ if (!res->dequeued) {
Branch: 1 branch
BranchTrueFalseTotal
if (!res->dequeued) { 0 0 0
658 0 ▶ for (j=0; j<2; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j=0; j<2; j++) { 0 0 0
659 0 read(fds[i], buf, 1);
660 // notify_on_begin may send 1
661 0 ▶ if (buf[0] == '2') break;
Branch: 1 branch
BranchTrueFalseTotal
if (buf[0] == '2') break; 0 0 0
662 }
663
664 0 ▶ if (!res->error && res->hostinfo) freeaddrinfo(res->hostinfo);
Branch: 2 branches
BranchTrueFalseTotal
if (!res->error && res->hostinfo) freeaddrinfo(res->hostinfo); 0 0 0
if (!res->error && res->hostinfo) freeaddrinfo(res->hostinfo); 0 0 0
665 0 ▶ if (res->arg->hints) free(res->arg->hints);
Branch: 1 branch
BranchTrueFalseTotal
if (res->arg->hints) free(res->arg->hints); 0 0 0
666 0 ▶ if (res->arg->host) Safefree(res->arg->host);
Branch: 1 branch
BranchTrueFalseTotal
if (res->arg->host) Safefree(res->arg->host); 0 0 0
667 0 ▶ if (res->arg->service) Safefree(res->arg->service);
Branch: 1 branch
BranchTrueFalseTotal
if (res->arg->service) Safefree(res->arg->service); 0 0 0
668 0 free(res->arg);
669 }
670
671 0 close(res->fd1);
672 0 close(fds[i]);
673 0 free(res);
674 }
675
676 0 free(fds);
677 }
678
679 16 queue_iterator *it = queue_iterator_new(DNS_instances);
680 16 ▶ while (!queue_iterator_end(it)) {
Branch: 1 branch
BranchTrueFalseTotal
while (!queue_iterator_end(it)) { 16 0 16
681 16 ▶ if (queue_at(DNS_instances, it) == self) {
Branch: 1 branch
BranchTrueFalseTotal
if (queue_at(DNS_instances, it) == self) { 16 0 16
682 16 queue_del(DNS_instances, it);
683 16 break;
684 }
685 0 queue_iterator_next(it);
686 }
687 16 queue_iterator_destroy(it);
688
689 16 pthread_attr_destroy(&self->thread_attrs);
690 16 pthread_mutex_destroy(&self->mutex);
691 16 bstree_destroy(self->fd_map);
692 16 queue_destroy(self->tout_queue);
693 16 Safefree(self);
694  
695 void
696 pack_sockaddr_in6(int port, SV *sv_address)
697 PPCODE:
698 STRLEN len;
699 0 char *address = SvPV(sv_address, len);
700 0 ▶ if (len != 16)
Branch: 1 branch
BranchTrueFalseTotal
if (len != 16) 0 0 0
701 0 croak("address length is %lu should be 16", len);
702
703 0 struct sockaddr_in6 *addr = malloc(sizeof(struct sockaddr_in6));
704 0 memcpy(addr->sin6_addr.s6_addr, address, 16);
705 0 addr->sin6_family = AF_INET6;
706 0 addr->sin6_port = port;
707
708 0 ▶ XPUSHs(sv_2mortal(newSVpvn((char*) addr, sizeof(struct sockaddr_in6))));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVpvn((char*) addr, sizeof(struct sockaddr_in6)))); 0 0 0
709  
710 void
711 unpack_sockaddr_in6(SV *sv_addr)
712 PPCODE:
713 STRLEN len;
714 6 char *addr = SvPV(sv_addr, len);
715 6 ▶ if (len != sizeof(struct sockaddr_in6))
Branch: 1 branch
BranchTrueFalseTotal
if (len != sizeof(struct sockaddr_in6)) 0 6 6
716 0 croak("address length is %lu should be %lu", len, sizeof(struct sockaddr_in6));
717
718 6 struct sockaddr_in6 *struct_addr = (struct sockaddr_in6*) addr;
719 6 ▶ XPUSHs(sv_2mortal(newSViv(struct_addr->sin6_port)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSViv(struct_addr->sin6_port))); 0 6 6
720 6 ▶ XPUSHs(sv_2mortal(newSVpvn((char*)struct_addr->sin6_addr.s6_addr, 16)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVpvn((char*)struct_addr->sin6_addr.s6_addr, 16))); 0 6 6
721  
722 int
723 _is_non_safe_symbols_loaded()
724 INIT:
725 0 char found = 0;
726 CODE:
727 #ifdef __linux__
728 0 dl_iterate_phdr(_dl_phdr_cb, (void*)&found);
729 #endif
730 0 ▶ RETVAL = found;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = found; 0 0 0
731 OUTPUT:
732 RETVAL
« blib/lib/Net/DNS/Native.pm ↑ Summary queue.h »