tst-res_hconf_reorder.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /* BZ #17977 _res_hconf_reorder_addrs test.
  2. Copyright (C) 2015-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 <errno.h>
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <time.h>
  20. #include <dlfcn.h>
  21. #include <pthread.h>
  22. #include <netdb.h>
  23. #include <netinet/in.h>
  24. #include <sys/socket.h>
  25. static struct timespec ts;
  26. /* The first thread that gets a lock in _res_hconf_reorder_addrs()
  27. should hold the lock long enough to make two other threads blocked.
  28. This is achieved by slowing down realloc(3) that is called several times
  29. by _res_hconf_reorder_addrs(). */
  30. void *
  31. realloc (void *ptr, size_t len)
  32. {
  33. static void *(*fun) (void *, size_t);
  34. if (!fun)
  35. fun = dlsym (RTLD_NEXT, "realloc");
  36. if (ts.tv_nsec)
  37. nanosleep (&ts, NULL);
  38. return (*fun) (ptr, len);
  39. }
  40. static void *
  41. resolve (void *arg)
  42. {
  43. struct in_addr addr;
  44. struct hostent ent;
  45. struct hostent *result;
  46. int err;
  47. char buf[1024];
  48. addr.s_addr = htonl (INADDR_LOOPBACK);
  49. (void) gethostbyaddr_r ((void *) &addr, sizeof (addr), AF_INET,
  50. &ent, buf, sizeof (buf), &result, &err);
  51. return arg;
  52. }
  53. static int
  54. do_test (void)
  55. {
  56. #define N 3
  57. pthread_t thr[N];
  58. unsigned int i;
  59. int result = 0;
  60. /* turn on realloc slowdown */
  61. ts.tv_nsec = 100000000;
  62. for (i = 0; i < N; ++i)
  63. {
  64. int rc = pthread_create (&thr[i], NULL, resolve, NULL);
  65. if (rc)
  66. {
  67. printf ("pthread_create: %s\n", strerror(rc));
  68. exit (1);
  69. }
  70. }
  71. for (i = 0; i < N; ++i)
  72. {
  73. void *retval;
  74. int rc = pthread_join (thr[i], &retval);
  75. if (rc)
  76. {
  77. printf ("pthread_join: %s\n", strerror(rc));
  78. exit (1);
  79. }
  80. if (retval)
  81. {
  82. printf ("thread %u exit status %p\n", i, retval);
  83. result = 1;
  84. }
  85. }
  86. /* turn off realloc slowdown, no longer needed */
  87. ts.tv_nsec = 0;
  88. return result;
  89. }
  90. #define TEST_FUNCTION do_test ()
  91. #include "../test-skeleton.c"