Update.
[platform/upstream/glibc.git] / iconv / gconv_dl.c
1 /* Handle loading/unloading of shared object for transformation.
2    Copyright (C) 1997, 1998, 1999, 2000 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 <assert.h>
22 #include <dlfcn.h>
23 #include <inttypes.h>
24 #include <search.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <bits/libc-lock.h>
28 #include <sys/param.h>
29
30 #include <gconv_int.h>
31
32
33 #ifdef DEBUG
34 /* For debugging purposes.  */
35 static void print_all (void);
36 #endif
37
38
39 /* This is a tuning parameter.  If a transformation module is not used
40    anymore it gets not immediately unloaded.  Instead we wait a certain
41    number of load attempts for further modules.  If none of the
42    subsequent load attempts name the same object it finally gets unloaded.
43    Otherwise it is still available which hopefully is the frequent case.
44    The following number is the number of unloading attempts we wait
45    before unloading.  */
46 #define TRIES_BEFORE_UNLOAD     2
47
48 /* Array of loaded objects.  This is shared by all threads so we have
49    to use semaphores to access it.  */
50 static void *loaded;
51
52 /* Comparison function for searching `loaded_object' tree.  */
53 static int
54 known_compare (const void *p1, const void *p2)
55 {
56   const struct __gconv_loaded_object *s1 =
57     (const struct __gconv_loaded_object *) p1;
58   const struct __gconv_loaded_object *s2 =
59     (const struct __gconv_loaded_object *) p2;
60
61   return strcmp (s1->name, s2->name);
62 }
63
64 /* Open the gconv database if necessary.  A non-negative return value
65    means success.  */
66 struct __gconv_loaded_object *
67 internal_function
68 __gconv_find_shlib (const char *name)
69 {
70   struct __gconv_loaded_object *found;
71   void *keyp;
72
73   /* Search the tree of shared objects previously requested.  Data in
74      the tree are `loaded_object' structures, whose first member is a
75      `const char *', the lookup key.  The search returns a pointer to
76      the tree node structure; the first member of the is a pointer to
77      our structure (i.e. what will be a `loaded_object'); since the
78      first member of that is the lookup key string, &FCT_NAME is close
79      enough to a pointer to our structure to use as a lookup key that
80      will be passed to `known_compare' (above).  */
81
82   keyp = __tfind (&name, &loaded, known_compare);
83   if (keyp == NULL)
84     {
85       /* This name was not known before.  */
86       found = malloc (sizeof (struct __gconv_loaded_object));
87       if (found != NULL)
88         {
89           /* Point the tree node at this new structure.  */
90           found->name = name;
91           found->counter = -TRIES_BEFORE_UNLOAD - 1;
92           found->handle = NULL;
93
94           if (__builtin_expect (__tsearch (found, &loaded, known_compare)
95                                 == NULL, 0))
96             {
97               /* Something went wrong while inserting the entry.  */
98               free (found);
99               found = NULL;
100             }
101         }
102     }
103   else
104     found = *(struct __gconv_loaded_object **) keyp;
105
106   /* Try to load the shared object if the usage count is 0.  This
107      implies that if the shared object is not loadable, the handle is
108      NULL and the usage count > 0.  */
109   if (found != NULL)
110     {
111       if (found->counter < -TRIES_BEFORE_UNLOAD)
112         {
113           assert (found->handle == NULL);
114           found->handle = __libc_dlopen (found->name);
115           if (found->handle != NULL)
116             {
117               found->fct = __libc_dlsym (found->handle, "gconv");
118               if (found->fct == NULL)
119                 {
120                   /* Argh, no conversion function.  There is something
121                      wrong here.  */
122                   __gconv_release_shlib (found);
123                   found = NULL;
124                 }
125               else
126                 {
127                   found->init_fct = __libc_dlsym (found->handle, "gconv_init");
128                   found->end_fct = __libc_dlsym (found->handle, "gconv_end");
129
130                   /* We have succeeded in loading the shared object.  */
131                   found->counter = 1;
132                 }
133             }
134           else
135             /* Error while loading the shared object.  */
136             found = NULL;
137         }
138       else if (found->handle != NULL)
139         found->counter = MAX (found->counter + 1, 1);
140     }
141
142   return found;
143 }
144
145
146 /* This is very ugly but the tsearch functions provide no way to pass
147    information to the walker function.  So we use a global variable.
148    It is MT safe since we use a lock.  */
149 static struct __gconv_loaded_object *release_handle;
150
151 static void
152 do_release_shlib (const void *nodep, VISIT value, int level)
153 {
154   struct __gconv_loaded_object *obj = *(struct __gconv_loaded_object **) nodep;
155
156   if (value != preorder && value != leaf)
157     return;
158
159   if (obj == release_handle)
160     {
161       /* This is the object we want to unload.  Now decrement the
162          reference counter.  */
163       assert (obj->counter > 0);
164       --obj->counter;
165     }
166   else if (obj->counter <= 0 && obj->counter >= -TRIES_BEFORE_UNLOAD
167            && --obj->counter < -TRIES_BEFORE_UNLOAD && obj->handle != NULL)
168     {
169       /* Unload the shared object.  */
170       __libc_dlclose (obj->handle);
171       obj->handle = NULL;
172     }
173 }
174
175
176 /* Notify system that a shared object is not longer needed.  */
177 void
178 internal_function
179 __gconv_release_shlib (struct __gconv_loaded_object *handle)
180 {
181   /* Urgh, this is ugly but we have no other possibility.  */
182   release_handle = handle;
183
184   /* Process all entries.  Please note that we also visit entries
185      with release counts <= 0.  This way we can finally unload them
186      if necessary.  */
187   __twalk (loaded, do_release_shlib);
188 }
189
190
191 /* We run this if we debug the memory allocation.  */
192 static void
193 do_release_all (void *nodep)
194 {
195   struct __gconv_loaded_object *obj = (struct __gconv_loaded_object *) nodep;
196
197   /* Unload the shared object.  */
198   if (obj->handle != NULL)
199     __libc_dlclose (obj->handle);
200
201   free (obj);
202 }
203
204 static void __attribute__ ((unused))
205 free_mem (void)
206 {
207   __tdestroy (loaded, do_release_all);
208 }
209 text_set_element (__libc_subfreeres, free_mem);
210
211
212 #ifdef DEBUG
213 static void
214 do_print (const void *nodep, VISIT value, int level)
215 {
216   struct __gconv_loaded_object *obj = *(struct __gconv_loaded_object **) nodep;
217
218   printf ("%10s: \"%s\", %d\n",
219           value == leaf ? "leaf" :
220           value == preorder ? "preorder" :
221           value == postorder ? "postorder" : "endorder",
222           obj->name, obj->counter);
223 }
224
225 static void
226 print_all (void)
227 {
228   __twalk (loaded, do_print);
229 }
230 #endif