comparison libpurple/protocols/gg/lib/resolver.c @ 32777:e2ff2ac0e022

Gadu-Gadu: synchronize internal libgadu with upstream - second encounter. Refs #343
author tomkiewicz@cpw.pidgin.im
date Fri, 21 Oct 2011 12:48:30 +0000
parents ef01f180114b
children
comparison
equal deleted inserted replaced
32776:8c71a7e95f16 32777:e2ff2ac0e022
24 * \file resolver.c 24 * \file resolver.c
25 * 25 *
26 * \brief Funkcje rozwiązywania nazw 26 * \brief Funkcje rozwiązywania nazw
27 */ 27 */
28 28
29 #ifndef _WIN32
30 # include <sys/wait.h>
31 # include <netdb.h>
32 #endif
33 #include <errno.h> 29 #include <errno.h>
34 #include <stdlib.h> 30 #include <stdlib.h>
35 #include <string.h> 31 #include <string.h>
36 #include <unistd.h> 32 #include <unistd.h>
37 #ifndef _WIN32
38 # include <signal.h>
39 # include <netinet/in.h>
40 # include <arpa/inet.h>
41 #endif
42 33
43 #include "libgadu.h" 34 #include "libgadu.h"
44 #include "resolver.h" 35 #include "resolver.h"
45 #include "compat.h" 36 #include "compat.h"
46 #include "session.h" 37 #include "session.h"
38
39 #ifdef GG_CONFIG_HAVE_FORK
40 #include <sys/wait.h>
41 #include <signal.h>
42 #endif
47 43
48 /** Sposób rozwiązywania nazw serwerów */ 44 /** Sposób rozwiązywania nazw serwerów */
49 static gg_resolver_t gg_global_resolver_type = GG_RESOLVER_DEFAULT; 45 static gg_resolver_t gg_global_resolver_type = GG_RESOLVER_DEFAULT;
50 46
51 /** Funkcja rozpoczynająca rozwiązywanie nazwy */ 47 /** Funkcja rozpoczynająca rozwiązywanie nazwy */
247 return 0; 243 return 0;
248 #endif /* GG_CONFIG_HAVE_GETHOSTBYNAME_R */ 244 #endif /* GG_CONFIG_HAVE_GETHOSTBYNAME_R */
249 } 245 }
250 246
251 #if defined(GG_CONFIG_HAVE_PTHREAD) || defined(GG_CONFIG_HAVE_FORK) 247 #if defined(GG_CONFIG_HAVE_PTHREAD) || defined(GG_CONFIG_HAVE_FORK)
248
252 /** 249 /**
253 * \internal Rozwiązuje nazwę i zapisuje wynik do podanego desktyptora. 250 * \internal Rozwiązuje nazwę i zapisuje wynik do podanego desktyptora.
254 * 251 *
255 * \param fd Deskryptor 252 * \param fd Deskryptor
256 * \param hostname Nazwa serwera 253 * \param hostname Nazwa serwera
284 if (addr_list != addr_ip) 281 if (addr_list != addr_ip)
285 free(addr_list); 282 free(addr_list);
286 283
287 return res; 284 return res;
288 } 285 }
289 #endif 286
287 #endif /* GG_CONFIG_HAVE_PTHREAD || GG_CONFIG_HAVE_FORK */
290 288
291 /** 289 /**
292 * \internal Odpowiednik \c gethostbyname zapewniający współbieżność. 290 * \internal Odpowiednik \c gethostbyname zapewniający współbieżność.
293 * 291 *
294 * Jeśli dany system dostarcza \c gethostbyname_r, używa się tej wersji, jeśli 292 * Jeśli dany system dostarcza \c gethostbyname_r, używa się tej wersji, jeśli
309 return NULL; 307 return NULL;
310 308
311 return result; 309 return result;
312 } 310 }
313 311
312 #ifdef GG_CONFIG_HAVE_FORK
313
314 /** 314 /**
315 * \internal Struktura przekazywana do wątku rozwiązującego nazwę. 315 * \internal Struktura przekazywana do wątku rozwiązującego nazwę.
316 */ 316 */
317 struct gg_resolver_fork_data { 317 struct gg_resolver_fork_data {
318 int pid; /*< Identyfikator procesu */ 318 int pid; /*< Identyfikator procesu */
319 }; 319 };
320 320
321 #ifdef GG_CONFIG_HAVE_FORK
322 /** 321 /**
323 * \internal Rozwiązuje nazwę serwera w osobnym procesie. 322 * \internal Rozwiązuje nazwę serwera w osobnym procesie.
324 * 323 *
325 * Połączenia asynchroniczne nie mogą blokować procesu w trakcie rozwiązywania 324 * Połączenia asynchroniczne nie mogą blokować procesu w trakcie rozwiązywania
326 * nazwy serwera. W tym celu tworzony jest potok, nowy proces i dopiero w nim 325 * nazwy serwera. W tym celu tworzony jest potok, nowy proces i dopiero w nim
368 new_errno = errno; 367 new_errno = errno;
369 goto cleanup; 368 goto cleanup;
370 } 369 }
371 370
372 if (data->pid == 0) { 371 if (data->pid == 0) {
372 int status;
373
373 close(pipes[0]); 374 close(pipes[0]);
374 375
375 if (gg_resolver_run(pipes[1], hostname) == -1) 376 status = (gg_resolver_run(pipes[1], hostname) == -1) ? 1 : 0;
376 _exit(1); 377
377 else 378 #ifdef GG_CONFIG_HAVE__EXIT
378 _exit(0); 379 _exit(status);
380 #else
381 exit(status);
382 #endif
379 } 383 }
380 384
381 close(pipes[1]); 385 close(pipes[1]);
382 386
383 gg_debug(GG_DEBUG_MISC, "// gg_resolver_fork_start() %p\n", data); 387 gg_debug(GG_DEBUG_MISC, "// gg_resolver_fork_start() %p\n", data);
422 426
423 waitpid(data->pid, NULL, WNOHANG); 427 waitpid(data->pid, NULL, WNOHANG);
424 428
425 free(data); 429 free(data);
426 } 430 }
427 #endif 431
432 #endif /* GG_CONFIG_HAVE_FORK */
428 433
429 #ifdef GG_CONFIG_HAVE_PTHREAD 434 #ifdef GG_CONFIG_HAVE_PTHREAD
430 435
431 /** 436 /**
432 * \internal Struktura przekazywana do wątku rozwiązującego nazwę. 437 * \internal Struktura przekazywana do wątku rozwiązującego nazwę.
592 gs->resolver_start = gg_global_resolver_start; 597 gs->resolver_start = gg_global_resolver_start;
593 gs->resolver_cleanup = gg_global_resolver_cleanup; 598 gs->resolver_cleanup = gg_global_resolver_cleanup;
594 return 0; 599 return 0;
595 } 600 }
596 601
597 #if !defined(GG_CONFIG_HAVE_PTHREAD) || !defined(GG_CONFIG_PTHREAD_DEFAULT) 602 #if defined(GG_CONFIG_HAVE_PTHREAD) && defined(GG_CONFIG_PTHREAD_DEFAULT)
603 type = GG_RESOLVER_PTHREAD;
604 #elif defined(GG_CONFIG_HAVE_FORK)
598 type = GG_RESOLVER_FORK; 605 type = GG_RESOLVER_FORK;
599 #else
600 type = GG_RESOLVER_PTHREAD;
601 #endif 606 #endif
602 } 607 }
603 608
604 switch (type) { 609 switch (type) {
605 #ifdef GG_CONFIG_HAVE_FORK 610 #ifdef GG_CONFIG_HAVE_FORK
703 gh->resolver_start = gg_global_resolver_start; 708 gh->resolver_start = gg_global_resolver_start;
704 gh->resolver_cleanup = gg_global_resolver_cleanup; 709 gh->resolver_cleanup = gg_global_resolver_cleanup;
705 return 0; 710 return 0;
706 } 711 }
707 712
708 #if !defined(GG_CONFIG_HAVE_PTHREAD) || !defined(GG_CONFIG_PTHREAD_DEFAULT) 713 #if defined(GG_CONFIG_HAVE_PTHREAD) && defined(GG_CONFIG_PTHREAD_DEFAULT)
714 type = GG_RESOLVER_PTHREAD;
715 #elif defined(GG_CONFIG_HAVE_FORK)
709 type = GG_RESOLVER_FORK; 716 type = GG_RESOLVER_FORK;
710 #else
711 type = GG_RESOLVER_PTHREAD;
712 #endif 717 #endif
713 } 718 }
714 719
715 switch (type) { 720 switch (type) {
716 #ifdef GG_CONFIG_HAVE_FORK 721 #ifdef GG_CONFIG_HAVE_FORK