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