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