Update.
[platform/upstream/glibc.git] / iconv / gconv_dl.c
1 /* Handle loading/unloading of shared object for transformation.
2    Copyright (C) 1997, 1998, 1999 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
5
6    The GNU C Library is free software; you can redistribute it and/or
7    modify it under the terms of the GNU Library General Public License as
8    published by the Free Software Foundation; either version 2 of the
9    License, or (at your option) any later version.
10
11    The GNU C Library is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Library General Public License for more details.
15
16    You should have received a copy of the GNU Library General Public
17    License along with the GNU C Library; see the file COPYING.LIB.  If not,
18    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19    Boston, MA 02111-1307, USA.  */
20
21 #include <dlfcn.h>
22 #include <inttypes.h>
23 #include <search.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <bits/libc-lock.h>
27 #include <sys/param.h>
28
29 #include <ldsodefs.h>
30 #include <gconv_int.h>
31
32
33 /* This is a tuning parameter.  If a transformation module is not used
34    anymore it gets not immediately unloaded.  Instead we wait a certain
35    number of load attempts for further modules.  If none of the
36    subsequent load attempts name the same object it finally gets unloaded.
37    Otherwise it is still available which hopefully is the frequent case.
38    The following number is the number of unloading attempts we wait
39    before unloading.  */
40 #define TRIES_BEFORE_UNLOAD     2
41
42
43 /* Array of loaded objects.  This is shared by all threads so we have
44    to use semaphores to access it.  */
45 static void *loaded;
46
47
48
49 /* Comparison function for searching `loaded_object' tree.  */
50 static int
51 known_compare (const void *p1, const void *p2)
52 {
53   const struct gconv_loaded_object *s1 =
54     (const struct gconv_loaded_object *) p1;
55   const struct gconv_loaded_object *s2 =
56     (const struct gconv_loaded_object *) p2;
57
58   return (intptr_t) s1->handle - (intptr_t) s2->handle;
59 }
60
61
62 static void
63 do_open (void *a)
64 {
65   struct gconv_loaded_object *args = (struct gconv_loaded_object *) a;
66   /* Open and relocate the shared object.  */
67   args->handle = _dl_open (args->name, RTLD_LAZY);
68 }
69
70
71 static int
72 internal_function
73 dlerror_run (void (*operate) (void *), void *args)
74 {
75   char *last_errstring = NULL;
76   int result;
77
78   (void) _dl_catch_error (&last_errstring, operate, args);
79
80   result = last_errstring != NULL;
81   if (result)
82     free (last_errstring);
83
84   return result;
85 }
86
87
88 struct get_sym_args
89 {
90   /* Arguments to get_sym.  */
91   struct link_map *map;
92   const char *name;
93
94   /* Return values of get_sym.  */
95   ElfW(Addr) loadbase;
96   const ElfW(Sym) *ref;
97 };
98
99 static void
100 get_sym (void *a)
101 {
102   struct get_sym_args *args = (struct get_sym_args *) a;
103   args->ref = NULL;
104   args->loadbase = _dl_lookup_symbol (args->name, &args->ref,
105                                       args->map->l_local_scope,
106                                       args->map->l_name, 0);
107 }
108
109
110 void *
111 internal_function
112 __gconv_find_func (void *handle, const char *name)
113 {
114   struct get_sym_args args;
115
116   args.map = handle;
117   args.name = name;
118
119   return (dlerror_run (get_sym, &args) ? NULL
120           : (void *) (args.loadbase + args.ref->st_value));
121 }
122
123
124
125 /* Open the gconv database if necessary.  A non-negative return value
126    means success.  */
127 struct gconv_loaded_object *
128 internal_function
129 __gconv_find_shlib (const char *name)
130 {
131   struct gconv_loaded_object *found;
132   void *keyp;
133
134   /* Search the tree of shared objects previously requested.  Data in
135      the tree are `loaded_object' structures, whose first member is a
136      `const char *', the lookup key.  The search returns a pointer to
137      the tree node structure; the first member of the is a pointer to
138      our structure (i.e. what will be a `loaded_object'); since the
139      first member of that is the lookup key string, &FCT_NAME is close
140      enough to a pointer to our structure to use as a lookup key that
141      will be passed to `known_compare' (above).  */
142
143   keyp = __tfind (&name, &loaded, known_compare);
144   if (keyp == NULL)
145     {
146       /* This name was not known before.  */
147       found = malloc (sizeof (struct gconv_loaded_object));
148       if (found != NULL)
149         {
150           /* Point the tree node at this new structure.  */
151           found->name = name;
152           found->counter = -TRIES_BEFORE_UNLOAD - 1;
153           found->handle = NULL;
154
155           if (__tsearch (found, &loaded, known_compare) == NULL)
156             {
157               /* Something went wrong while inserting the entry.  */
158               free (found);
159               found = NULL;
160             }
161         }
162     }
163   else
164     found = *(struct gconv_loaded_object **) keyp;
165
166   /* Try to load the shared object if the usage count is 0.  This
167      implies that if the shared object is not loadable, the handle is
168      NULL and the usage count > 0.  */
169   if (found != NULL)
170     {
171       if (found->counter < -TRIES_BEFORE_UNLOAD)
172         {
173           if (dlerror_run (do_open, found) == 0)
174             {
175               found->fct = __gconv_find_func (found->handle, "gconv");
176               if (found->fct == NULL)
177                 {
178                   /* Argh, no conversion function.  There is something
179                      wrong here.  */
180                   __gconv_release_shlib (found);
181                   found = NULL;
182                 }
183               else
184                 {
185                   found->init_fct = __gconv_find_func (found->handle,
186                                                        "gconv_init");
187                   found->end_fct = __gconv_find_func (found->handle,
188                                                       "gconv_end");
189
190                   /* We have succeeded in loading the shared object.  */
191                   found->counter = 1;
192                 }
193             }
194           else
195             /* Error while loading the shared object.  */
196             found = NULL;
197         }
198       else if (found->handle != NULL)
199         found->counter = MAX (found->counter + 1, 1);
200     }
201
202   return found;
203 }
204
205
206 /* This is very ugly but the tsearch functions provide no way to pass
207    information to the walker function.  So we use a global variable.
208    It is MT safe since we use a lock.  */
209 static struct gconv_loaded_object *release_handle;
210
211 static void
212 do_release_shlib (const void *nodep, VISIT value, int level)
213 {
214   struct gconv_loaded_object *obj = *(struct gconv_loaded_object **) nodep;
215
216   if (value != preorder && value != leaf)
217     return;
218
219   if (obj == release_handle)
220     /* This is the object we want to unload.  Now set the release
221        counter to zero.  */
222     obj->counter = 0;
223   else if (obj->counter <= 0)
224     {
225       if (--obj->counter < -TRIES_BEFORE_UNLOAD && obj->handle != NULL)
226         {
227           /* Unload the shared object.  We don't use the trick to
228              catch errors since in the case an error is signalled
229              something is really wrong.  */
230           _dl_close (obj->handle);
231
232           obj->handle = NULL;
233         }
234     }
235 }
236
237
238 /* Notify system that a shared object is not longer needed.  */
239 int
240 internal_function
241 __gconv_release_shlib (struct gconv_loaded_object *handle)
242 {
243   /* Urgh, this is ugly but we have no other possibility.  */
244   release_handle = handle;
245
246   /* Process all entries.  Please note that we also visit entries
247      with release counts <= 0.  This way we can finally unload them
248      if necessary.  */
249   __twalk (loaded, do_release_shlib);
250
251   return GCONV_OK;
252 }
253
254
255 /* We run this if we debug the memory allocation.  */
256 static void
257 do_release_all (const void *nodep, VISIT value, int level)
258 {
259   struct gconv_loaded_object *obj = *(struct gconv_loaded_object **) nodep;
260
261   if (value != preorder && value != leaf)
262     return;
263
264   /* Unload the shared object.  We don't use the trick to
265      catch errors since in the case an error is signalled
266      something is really wrong.  */
267   _dl_close (obj->handle);
268
269   free (obj);
270 }
271
272 static void __attribute__ ((unused))
273 free_mem (void)
274 {
275   __twalk (loaded, do_release_all);
276 }
277 text_set_element (__libc_subfreeres, free_mem);