Update.
[platform/upstream/glibc.git] / iconv / gconv_dl.c
1 /* Handle loading/unloading of shared object for transformation.
2    Copyright (C) 1997 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 <gconv.h>
23 #include <inttypes.h>
24 #include <link.h>
25 #include <search.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <bits/libc-lock.h>
29 #include <sys/param.h>
30
31
32 /* This is a tuning parameter.  If a transformation module is not used
33    anymore it gets not immediately unloaded.  Instead we wait a certain
34    number of load attempts for further modules.  If none of the
35    subsequent load attempts name the same object it finally gets unloaded.
36    Otherwise it is still available which hopefully is the frequent case.
37    The following number is the number of unloading attempts we wait
38    before unloading.  */
39 #define TRIES_BEFORE_UNLOAD     2
40
41
42 /* Structure describing one loaded shared object.  This normally are
43    objects to perform conversation but as a special case the db shared
44    object is also handled.  */
45 struct loaded_object
46 {
47   /* Name of the object.  */
48   const char *name;
49
50   /* Reference counter for the db functionality.  If no conversion is
51      needed we unload the db library.  */
52   int counter;
53
54   /* The handle for the shared object.  */
55   void *handle;
56 };
57
58
59 /* Array of loaded objects.  This is shared by all threads so we have
60    to use semaphores to access it.  */
61 static void *loaded;
62 __libc_lock_define_initialized (static, lock)
63
64
65
66 /* Comparison function for searching `loaded_object' tree.  */
67 static int
68 known_compare (const void *p1, const void *p2)
69 {
70   const struct loaded_object *s1 = (const struct loaded_object *) p1;
71   const struct loaded_object *s2 = (const struct loaded_object *) p2;
72
73   return (intptr_t) s1->handle - (intptr_t) s2->handle;
74 }
75
76
77 static void
78 do_open (void *a)
79 {
80   struct loaded_object *args = (struct loaded_object *) a;
81   /* Open and relocate the shared object.  */
82   args->handle = _dl_open (args->name, RTLD_LAZY);
83 }
84
85
86 static int
87 internal_function
88 dlerror_run (void (*operate) (void *), void *args)
89 {
90   char *last_errstring = NULL;
91   const char *last_object_name = NULL;
92   int result;
93
94   (void) _dl_catch_error (&last_errstring, &last_object_name, operate, args);
95
96   result = last_errstring != NULL;
97   if (result)
98     free (last_errstring);
99
100   return result;
101 }
102
103
104 struct get_sym_args
105 {
106   /* Arguments to get_sym.  */
107   struct link_map *map;
108   const char *name;
109
110   /* Return values of get_sym.  */
111   ElfW(Addr) loadbase;
112   const ElfW(Sym) *ref;
113 };
114
115 static void
116 get_sym (void *a)
117 {
118   struct get_sym_args *args = (struct get_sym_args *) a;
119   struct link_map *scope[2] = { args->map, NULL };
120   args->ref = NULL;
121   args->loadbase = _dl_lookup_symbol (args->name, &args->ref,
122                                       scope, args->map->l_name, 0);
123 }
124
125
126 void *
127 __gconv_find_func (void *handle, const char *name)
128 {
129   struct get_sym_args args;
130
131   args.map = handle;
132   args.name = name;
133
134   return (dlerror_run (get_sym, &args) ? NULL
135           : (void *) (args.loadbase + args.ref->st_value));
136 }
137
138
139
140 /* Open the gconv database if necessary.  A non-negative return value
141    means success.  */
142 void *
143 __gconv_find_shlib (const char *name)
144 {
145   void *result = NULL;
146   struct loaded_object *found;
147
148   /* Acquire the lock.  */
149   __libc_lock_lock (lock);
150
151   /* Search the tree of shared objects previously requested.  Data in
152      the tree are `loaded_object' structures, whose first member is a
153      `const char *', the lookup key.  The search returns a pointer to
154      the tree node structure; the first member of the is a pointer to
155      our structure (i.e. what will be a `loaded_object'); since the
156      first member of that is the lookup key string, &FCT_NAME is close
157      enough to a pointer to our structure to use as a lookup key that
158      will be passed to `known_compare' (above).  */
159
160   found = __tfind (&name, &loaded, known_compare);
161   if (found == NULL)
162     {
163       /* This name was not known before.  */
164       found = malloc (sizeof (struct loaded_object));
165       if (found != NULL)
166         {
167           /* Point the tree node at this new structure.  */
168           found->name = name;
169           found->counter = -TRIES_BEFORE_UNLOAD - 1;
170           found->handle = NULL;
171
172           if (__tsearch (found, &loaded, known_compare) == NULL)
173             {
174               /* Something went wrong while inserting the entry.  */
175               free (found);
176               found = NULL;
177             }
178         }
179     }
180
181   /* Try to load the shared object if the usage count is 0.  This
182      implies that if the shared object is not loadable, the handle is
183      NULL and the usage count > 0.  */
184   if (found != NULL)
185     {
186       if (found->counter < -TRIES_BEFORE_UNLOAD)
187         {
188           if (dlerror_run (do_open, found) == 0)
189             found->counter = 1;
190         }
191       else if (found->handle != NULL)
192         found->counter = MAX (found->counter + 1, 1);
193
194       result = found->handle;
195     }
196
197   /* Release the lock.  */
198   __libc_lock_unlock (lock);
199
200   return result;
201 }
202
203
204 /* This is very ugly but the tsearch functions provide no way to pass
205    information to the walker function.  So we use a global variable.
206    It is MT safe since we use a lock.  */
207 static void *release_handle;
208
209 static void
210 do_release_shlib (const void *nodep, VISIT value, int level)
211 {
212   struct loaded_object *obj = *(struct loaded_object **) nodep;
213
214   if (value != preorder && value != leaf)
215     return;
216
217   if (obj->handle == release_handle)
218     /* This is the object we want to unload.  Now set the release
219        counter to zero.  */
220     obj->counter = 0;
221   else if (obj->counter <= 0)
222     {
223       if (--obj->counter < -TRIES_BEFORE_UNLOAD && obj->handle != NULL)
224         {
225           /* Unload the shared object.  We don't use the trick to
226              catch errors since in the case an error is signalled
227              something is really wrong.  */
228           _dl_close ((struct link_map *) obj->handle);
229
230           obj->handle = NULL;
231         }
232     }
233 }
234
235
236 /* Notify system that a shared object is not longer needed.  */
237 int
238 __gconv_release_shlib (void *handle)
239 {
240   /* Acquire the lock.  */
241   __libc_lock_lock (lock);
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   /* Release the lock.  */
252   __libc_lock_unlock (lock);
253
254   return GCONV_OK;
255 }