tst-resolv-res_init-multi.c 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /* Multi-threaded test for resolver initialization.
  2. Copyright (C) 2017-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <netdb.h>
  16. #include <resolv.h>
  17. #include <stdlib.h>
  18. #include <support/check.h>
  19. #include <support/support.h>
  20. #include <support/xthread.h>
  21. /* Whether name lookups succeed does not really matter. We use this
  22. to trigger initialization of the resolver. */
  23. static const char *test_hostname = "www.gnu.org";
  24. /* The different initialization methods. */
  25. enum test_type { init, byname, gai };
  26. enum { type_count = 3 };
  27. /* Thread function. Perform a few resolver options. */
  28. static void *
  29. thread_func (void *closure)
  30. {
  31. enum test_type *ptype = closure;
  32. /* Perform a few calls to the requested operation. */
  33. TEST_VERIFY (*ptype >= 0);
  34. TEST_VERIFY (*ptype < (int) type_count);
  35. for (int i = 0; i < 3; ++i)
  36. switch (*ptype)
  37. {
  38. case init:
  39. res_init ();
  40. break;
  41. case byname:
  42. gethostbyname (test_hostname);
  43. break;
  44. case gai:
  45. {
  46. struct addrinfo hints = { 0, };
  47. struct addrinfo *ai = NULL;
  48. if (getaddrinfo (test_hostname, "80", &hints, &ai) == 0)
  49. freeaddrinfo (ai);
  50. }
  51. break;
  52. }
  53. free (ptype);
  54. return NULL;
  55. }
  56. static int
  57. do_test (void)
  58. {
  59. /* Start a small number of threads which perform resolver
  60. operations. */
  61. enum { thread_count = 30 };
  62. pthread_t threads[thread_count];
  63. for (int i = 0; i < thread_count; ++i)
  64. {
  65. enum test_type *ptype = xmalloc (sizeof (*ptype));
  66. *ptype = i % type_count;
  67. threads[i] = xpthread_create (NULL, thread_func, ptype);
  68. }
  69. for (int i = 0; i < type_count; ++i)
  70. {
  71. enum test_type *ptype = xmalloc (sizeof (*ptype));
  72. *ptype = i;
  73. thread_func (ptype);
  74. }
  75. for (int i = 0; i < thread_count; ++i)
  76. xpthread_join (threads[i]);
  77. return 0;
  78. }
  79. #include <support/test-driver.c>