getXXbyYY.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /* Copyright (C) 1996-2026 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, see
  13. <https://www.gnu.org/licenses/>. */
  14. #include <assert.h>
  15. #include <errno.h>
  16. #include <libc-lock.h>
  17. #include <stdlib.h>
  18. #include <resolv.h>
  19. #include <set-freeres.h>
  20. #include "nsswitch.h"
  21. /*******************************************************************\
  22. |* Here we assume several symbols to be defined: *|
  23. |* *|
  24. |* LOOKUP_TYPE - the return type of the function *|
  25. |* *|
  26. |* FUNCTION_NAME - name of the non-reentrant function *|
  27. |* *|
  28. |* DATABASE_NAME - name of the database the function accesses *|
  29. |* (e.g., host, services, ...) *|
  30. |* *|
  31. |* ADD_PARAMS - additional parameter, can vary in number *|
  32. |* *|
  33. |* ADD_VARIABLES - names of additional parameter *|
  34. |* *|
  35. |* BUFLEN - length of buffer allocated for the non *|
  36. |* reentrant version *|
  37. |* *|
  38. |* Optionally the following vars can be defined: *|
  39. |* *|
  40. |* NEED_H_ERRNO - an extra parameter will be passed to point to *|
  41. |* the global `h_errno' variable. *|
  42. |* *|
  43. \*******************************************************************/
  44. #ifdef HANDLE_DIGITS_DOTS
  45. # include <resolv/resolv_context.h>
  46. #endif
  47. /* To make the real sources a bit prettier. */
  48. #define REENTRANT_NAME APPEND_R (FUNCTION_NAME)
  49. #define APPEND_R(name) APPEND_R1 (name)
  50. #define APPEND_R1(name) name##_r
  51. #define INTERNAL(name) INTERNAL1 (name)
  52. #define INTERNAL1(name) __##name
  53. #define APPEND_FREEMEM_NAME1(name) __libc_##name##_freemem_ptr
  54. #define APPEND_FREEMEM_NAME(name) APPEND_FREEMEM_NAME1(name)
  55. #define FREEMEM_NAME APPEND_FREEMEM_NAME (FUNCTION_NAME)
  56. /* Sometimes we need to store error codes in the `h_errno' variable. */
  57. #ifdef NEED_H_ERRNO
  58. # define H_ERRNO_PARM , int *h_errnop
  59. # define H_ERRNO_VAR , &h_errno_tmp
  60. # define H_ERRNO_VAR_P &h_errno_tmp
  61. #else
  62. # define H_ERRNO_PARM
  63. # define H_ERRNO_VAR
  64. # define H_ERRNO_VAR_P NULL
  65. #endif
  66. #ifdef HAVE_AF
  67. # define AF_VAL af
  68. #else
  69. # define AF_VAL AF_INET
  70. #endif
  71. /* Prototype for reentrant version we use here. */
  72. extern int INTERNAL (REENTRANT_NAME) (ADD_PARAMS, LOOKUP_TYPE *resbuf,
  73. char *buffer, size_t buflen,
  74. LOOKUP_TYPE **result H_ERRNO_PARM)
  75. attribute_hidden;
  76. /* We need to protect the dynamic buffer handling. */
  77. __libc_lock_define_initialized (static, lock);
  78. /* This points to the static buffer used. */
  79. static char *buffer;
  80. weak_alias (buffer, FREEMEM_NAME)
  81. LOOKUP_TYPE *
  82. FUNCTION_NAME (ADD_PARAMS)
  83. {
  84. static size_t buffer_size;
  85. static LOOKUP_TYPE resbuf;
  86. LOOKUP_TYPE *result;
  87. #ifdef NEED_H_ERRNO
  88. int h_errno_tmp = 0;
  89. #endif
  90. #ifdef HANDLE_DIGITS_DOTS
  91. /* Wrap both __nss_hostname_digits_dots and the actual lookup
  92. function call in the same context. */
  93. struct resolv_context *res_ctx = __resolv_context_get ();
  94. if (res_ctx == NULL)
  95. {
  96. # if NEED_H_ERRNO
  97. __set_h_errno (NETDB_INTERNAL);
  98. # endif
  99. return NULL;
  100. }
  101. #endif
  102. /* Get lock. */
  103. __libc_lock_lock (lock);
  104. if (buffer == NULL)
  105. {
  106. buffer_size = BUFLEN;
  107. buffer = (char *) malloc (buffer_size);
  108. }
  109. #ifdef HANDLE_DIGITS_DOTS
  110. if (buffer != NULL)
  111. {
  112. if (__nss_hostname_digits_dots_context
  113. (res_ctx, name, &resbuf, &buffer, &buffer_size, 0, &result, NULL,
  114. AF_VAL, H_ERRNO_VAR_P))
  115. goto done;
  116. }
  117. #endif
  118. while (buffer != NULL
  119. && (INTERNAL (REENTRANT_NAME) (ADD_VARIABLES, &resbuf, buffer,
  120. buffer_size, &result H_ERRNO_VAR)
  121. == ERANGE)
  122. #ifdef NEED_H_ERRNO
  123. && h_errno_tmp == NETDB_INTERNAL
  124. #endif
  125. )
  126. {
  127. char *new_buf;
  128. buffer_size *= 2;
  129. new_buf = (char *) realloc (buffer, buffer_size);
  130. if (new_buf == NULL)
  131. {
  132. /* We are out of memory. Free the current buffer so that the
  133. process gets a chance for a normal termination. */
  134. free (buffer);
  135. __set_errno (ENOMEM);
  136. }
  137. buffer = new_buf;
  138. }
  139. if (buffer == NULL)
  140. result = NULL;
  141. #ifdef HANDLE_DIGITS_DOTS
  142. done:
  143. #endif
  144. /* Release lock. */
  145. __libc_lock_unlock (lock);
  146. #ifdef HANDLE_DIGITS_DOTS
  147. __resolv_context_put (res_ctx);
  148. #endif
  149. #ifdef NEED_H_ERRNO
  150. if (h_errno_tmp != 0)
  151. __set_h_errno (h_errno_tmp);
  152. #endif
  153. return result;
  154. }
  155. nss_interface_function (FUNCTION_NAME)