135-inet_fix_threaded_use_of_res_functions.patch 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. --- a/libc/inet/resolv.c
  2. +++ b/libc/inet/resolv.c
  3. @@ -3536,6 +3536,61 @@ __res_vinit(res_state rp, int preinit)
  4. return 0;
  5. }
  6. +static unsigned int
  7. +res_randomid(void)
  8. +{
  9. + return 0xffff & getpid();
  10. +}
  11. +
  12. +/* Our res_init never fails (always returns 0) */
  13. +int
  14. +res_init(void)
  15. +{
  16. + /*
  17. + * These three fields used to be statically initialized. This made
  18. + * it hard to use this code in a shared library. It is necessary,
  19. + * now that we're doing dynamic initialization here, that we preserve
  20. + * the old semantics: if an application modifies one of these three
  21. + * fields of _res before res_init() is called, res_init() will not
  22. + * alter them. Of course, if an application is setting them to
  23. + * _zero_ before calling res_init(), hoping to override what used
  24. + * to be the static default, we can't detect it and unexpected results
  25. + * will follow. Zero for any of these fields would make no sense,
  26. + * so one can safely assume that the applications were already getting
  27. + * unexpected results.
  28. + *
  29. + * _res.options is tricky since some apps were known to diddle the bits
  30. + * before res_init() was first called. We can't replicate that semantic
  31. + * with dynamic initialization (they may have turned bits off that are
  32. + * set in RES_DEFAULT). Our solution is to declare such applications
  33. + * "broken". They could fool us by setting RES_INIT but none do (yet).
  34. + */
  35. +
  36. + __UCLIBC_MUTEX_LOCK(__resolv_lock);
  37. +
  38. + if (!_res.retrans)
  39. + _res.retrans = RES_TIMEOUT;
  40. + if (!_res.retry)
  41. + _res.retry = 4;
  42. + if (!(_res.options & RES_INIT))
  43. + _res.options = RES_DEFAULT;
  44. +
  45. + /*
  46. + * This one used to initialize implicitly to zero, so unless the app
  47. + * has set it to something in particular, we can randomize it now.
  48. + */
  49. + if (!_res.id)
  50. + _res.id = res_randomid();
  51. +
  52. + __UCLIBC_MUTEX_UNLOCK(__resolv_lock);
  53. +
  54. + __res_vinit(&_res, 1);
  55. + __res_sync = res_sync_func;
  56. +
  57. + return 0;
  58. +}
  59. +libc_hidden_def(res_init)
  60. +
  61. static void
  62. __res_iclose(void)
  63. {
  64. @@ -3608,61 +3663,6 @@ struct __res_state *__resp = &_res;
  65. # endif
  66. #endif /* !__UCLIBC_HAS_THREADS__ */
  67. -static unsigned int
  68. -res_randomid(void)
  69. -{
  70. - return 0xffff & getpid();
  71. -}
  72. -
  73. -/* Our res_init never fails (always returns 0) */
  74. -int
  75. -res_init(void)
  76. -{
  77. - /*
  78. - * These three fields used to be statically initialized. This made
  79. - * it hard to use this code in a shared library. It is necessary,
  80. - * now that we're doing dynamic initialization here, that we preserve
  81. - * the old semantics: if an application modifies one of these three
  82. - * fields of _res before res_init() is called, res_init() will not
  83. - * alter them. Of course, if an application is setting them to
  84. - * _zero_ before calling res_init(), hoping to override what used
  85. - * to be the static default, we can't detect it and unexpected results
  86. - * will follow. Zero for any of these fields would make no sense,
  87. - * so one can safely assume that the applications were already getting
  88. - * unexpected results.
  89. - *
  90. - * _res.options is tricky since some apps were known to diddle the bits
  91. - * before res_init() was first called. We can't replicate that semantic
  92. - * with dynamic initialization (they may have turned bits off that are
  93. - * set in RES_DEFAULT). Our solution is to declare such applications
  94. - * "broken". They could fool us by setting RES_INIT but none do (yet).
  95. - */
  96. -
  97. - __UCLIBC_MUTEX_LOCK(__resolv_lock);
  98. -
  99. - if (!_res.retrans)
  100. - _res.retrans = RES_TIMEOUT;
  101. - if (!_res.retry)
  102. - _res.retry = 4;
  103. - if (!(_res.options & RES_INIT))
  104. - _res.options = RES_DEFAULT;
  105. -
  106. - /*
  107. - * This one used to initialize implicitly to zero, so unless the app
  108. - * has set it to something in particular, we can randomize it now.
  109. - */
  110. - if (!_res.id)
  111. - _res.id = res_randomid();
  112. -
  113. - __UCLIBC_MUTEX_UNLOCK(__resolv_lock);
  114. -
  115. - __res_vinit(&_res, 1);
  116. - __res_sync = res_sync_func;
  117. -
  118. - return 0;
  119. -}
  120. -libc_hidden_def(res_init)
  121. -
  122. /*
  123. * Set up default settings. If the configuration file exist, the values
  124. * there will have precedence. Otherwise, the server address is set to