getaddrinfo_a.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /* Copyright (C) 2001-2019 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Ulrich Drepper <drepper@redhat.com>, 2001.
  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 <netdb.h>
  17. #include <pthread.h>
  18. #include <stdlib.h>
  19. #include <unistd.h>
  20. #include <gai_misc.h>
  21. /* We need this special structure to handle asynchronous I/O. */
  22. struct async_waitlist
  23. {
  24. unsigned int counter;
  25. struct sigevent sigev;
  26. struct waitlist list[0];
  27. };
  28. int
  29. getaddrinfo_a (int mode, struct gaicb *list[], int ent, struct sigevent *sig)
  30. {
  31. struct sigevent defsigev;
  32. struct requestlist *requests[ent];
  33. int cnt;
  34. volatile unsigned int total = 0;
  35. int result = 0;
  36. /* Check arguments. */
  37. if (mode != GAI_WAIT && mode != GAI_NOWAIT)
  38. {
  39. __set_errno (EINVAL);
  40. return EAI_SYSTEM;
  41. }
  42. if (sig == NULL)
  43. {
  44. defsigev.sigev_notify = SIGEV_NONE;
  45. sig = &defsigev;
  46. }
  47. /* Request the mutex. */
  48. pthread_mutex_lock (&__gai_requests_mutex);
  49. /* Now we can enqueue all requests. Since we already acquired the
  50. mutex the enqueue function need not do this. */
  51. for (cnt = 0; cnt < ent; ++cnt)
  52. if (list[cnt] != NULL)
  53. {
  54. requests[cnt] = __gai_enqueue_request (list[cnt]);
  55. if (requests[cnt] != NULL)
  56. /* Successfully enqueued. */
  57. ++total;
  58. else
  59. /* Signal that we've seen an error. `errno' and the error code
  60. of the gaicb will tell more. */
  61. result = EAI_SYSTEM;
  62. }
  63. else
  64. requests[cnt] = NULL;
  65. if (total == 0)
  66. {
  67. /* We don't have anything to do except signalling if we work
  68. asynchronously. */
  69. /* Release the mutex. We do this before raising a signal since the
  70. signal handler might do a `siglongjmp' and then the mutex is
  71. locked forever. */
  72. pthread_mutex_unlock (&__gai_requests_mutex);
  73. if (mode == GAI_NOWAIT)
  74. __gai_notify_only (sig,
  75. sig->sigev_notify == SIGEV_SIGNAL ? getpid () : 0);
  76. return result;
  77. }
  78. else if (mode == GAI_WAIT)
  79. {
  80. #ifndef DONT_NEED_GAI_MISC_COND
  81. pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
  82. #endif
  83. struct waitlist waitlist[ent];
  84. int oldstate;
  85. total = 0;
  86. for (cnt = 0; cnt < ent; ++cnt)
  87. if (requests[cnt] != NULL)
  88. {
  89. #ifndef DONT_NEED_GAI_MISC_COND
  90. waitlist[cnt].cond = &cond;
  91. #endif
  92. waitlist[cnt].next = requests[cnt]->waiting;
  93. waitlist[cnt].counterp = &total;
  94. waitlist[cnt].sigevp = NULL;
  95. waitlist[cnt].caller_pid = 0; /* Not needed. */
  96. requests[cnt]->waiting = &waitlist[cnt];
  97. ++total;
  98. }
  99. /* Since `pthread_cond_wait'/`pthread_cond_timedwait' are cancelation
  100. points we must be careful. We added entries to the waiting lists
  101. which we must remove. So defer cancelation for now. */
  102. pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, &oldstate);
  103. while (total > 0)
  104. {
  105. #ifdef DONT_NEED_GAI_MISC_COND
  106. int not_used __attribute__ ((unused));
  107. GAI_MISC_WAIT (not_used, total, NULL, 1);
  108. #else
  109. pthread_cond_wait (&cond, &__gai_requests_mutex);
  110. #endif
  111. }
  112. /* Now it's time to restore the cancelation state. */
  113. pthread_setcancelstate (oldstate, NULL);
  114. #ifndef DONT_NEED_GAI_MISC_COND
  115. /* Release the conditional variable. */
  116. if (pthread_cond_destroy (&cond) != 0)
  117. /* This must never happen. */
  118. abort ();
  119. #endif
  120. }
  121. else
  122. {
  123. struct async_waitlist *waitlist;
  124. waitlist = (struct async_waitlist *)
  125. malloc (sizeof (struct async_waitlist)
  126. + (ent * sizeof (struct waitlist)));
  127. if (waitlist == NULL)
  128. result = EAI_AGAIN;
  129. else
  130. {
  131. pid_t caller_pid = sig->sigev_notify == SIGEV_SIGNAL ? getpid () : 0;
  132. total = 0;
  133. for (cnt = 0; cnt < ent; ++cnt)
  134. if (requests[cnt] != NULL)
  135. {
  136. #ifndef DONT_NEED_GAI_MISC_COND
  137. waitlist->list[cnt].cond = NULL;
  138. #endif
  139. waitlist->list[cnt].next = requests[cnt]->waiting;
  140. waitlist->list[cnt].counterp = &waitlist->counter;
  141. waitlist->list[cnt].sigevp = &waitlist->sigev;
  142. waitlist->list[cnt].caller_pid = caller_pid;
  143. requests[cnt]->waiting = &waitlist->list[cnt];
  144. ++total;
  145. }
  146. waitlist->counter = total;
  147. waitlist->sigev = *sig;
  148. }
  149. }
  150. /* Release the mutex. */
  151. pthread_mutex_unlock (&__gai_requests_mutex);
  152. return result;
  153. }