Updated to fedora-glibc-20050504T0728
[glibc.git] / iconv / gconv_dl.c
blob95040172108d41b38be00aa55f964773a8cd8e55
1 /* Handle loading/unloading of shared object for transformation.
2 Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2004
3 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
5 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
7 The GNU C Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
12 The GNU C Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public
18 License along with the GNU C Library; if not, write to the Free
19 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 02111-1307 USA. */
22 #include <assert.h>
23 #include <dlfcn.h>
24 #include <inttypes.h>
25 #include <search.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <bits/libc-lock.h>
29 #include <sys/param.h>
31 #include <gconv_int.h>
34 #ifdef DEBUG
35 /* For debugging purposes. */
36 static void print_all (void);
37 #endif
40 /* This is a tuning parameter. If a transformation module is not used
41 anymore it gets not immediately unloaded. Instead we wait a certain
42 number of load attempts for further modules. If none of the
43 subsequent load attempts name the same object it finally gets unloaded.
44 Otherwise it is still available which hopefully is the frequent case.
45 The following number is the number of unloading attempts we wait
46 before unloading. */
47 #define TRIES_BEFORE_UNLOAD 2
49 /* Array of loaded objects. This is shared by all threads so we have
50 to use semaphores to access it. */
51 static void *loaded;
53 /* Comparison function for searching `loaded_object' tree. */
54 static int
55 known_compare (const void *p1, const void *p2)
57 const struct __gconv_loaded_object *s1 =
58 (const struct __gconv_loaded_object *) p1;
59 const struct __gconv_loaded_object *s2 =
60 (const struct __gconv_loaded_object *) p2;
62 return strcmp (s1->name, s2->name);
65 /* Open the gconv database if necessary. A non-negative return value
66 means success. */
67 struct __gconv_loaded_object *
68 internal_function
69 __gconv_find_shlib (const char *name)
71 struct __gconv_loaded_object *found;
72 void *keyp;
74 /* Search the tree of shared objects previously requested. Data in
75 the tree are `loaded_object' structures, whose first member is a
76 `const char *', the lookup key. The search returns a pointer to
77 the tree node structure; the first member of the is a pointer to
78 our structure (i.e. what will be a `loaded_object'); since the
79 first member of that is the lookup key string, &FCT_NAME is close
80 enough to a pointer to our structure to use as a lookup key that
81 will be passed to `known_compare' (above). */
83 keyp = __tfind (&name, &loaded, known_compare);
84 if (keyp == NULL)
86 /* This name was not known before. */
87 size_t namelen = strlen (name) + 1;
89 found = malloc (sizeof (struct __gconv_loaded_object) + namelen);
90 if (found != NULL)
92 /* Point the tree node at this new structure. */
93 found->name = (char *) memcpy (found + 1, name, namelen);
94 found->counter = -TRIES_BEFORE_UNLOAD - 1;
95 found->handle = NULL;
97 if (__builtin_expect (__tsearch (found, &loaded, known_compare)
98 == NULL, 0))
100 /* Something went wrong while inserting the entry. */
101 free (found);
102 found = NULL;
106 else
107 found = *(struct __gconv_loaded_object **) keyp;
109 /* Try to load the shared object if the usage count is 0. This
110 implies that if the shared object is not loadable, the handle is
111 NULL and the usage count > 0. */
112 if (found != NULL)
114 if (found->counter < -TRIES_BEFORE_UNLOAD)
116 assert (found->handle == NULL);
117 found->handle = __libc_dlopen (found->name);
118 if (found->handle != NULL)
120 found->fct = __libc_dlsym (found->handle, "gconv");
121 if (found->fct == NULL)
123 /* Argh, no conversion function. There is something
124 wrong here. */
125 __gconv_release_shlib (found);
126 found = NULL;
128 else
130 found->init_fct = __libc_dlsym (found->handle, "gconv_init");
131 found->end_fct = __libc_dlsym (found->handle, "gconv_end");
133 /* We have succeeded in loading the shared object. */
134 found->counter = 1;
137 else
138 /* Error while loading the shared object. */
139 found = NULL;
141 else if (found->handle != NULL)
142 found->counter = MAX (found->counter + 1, 1);
145 return found;
149 /* This is very ugly but the tsearch functions provide no way to pass
150 information to the walker function. So we use a global variable.
151 It is MT safe since we use a lock. */
152 static struct __gconv_loaded_object *release_handle;
154 static void
155 do_release_shlib (void *nodep, VISIT value, int level)
157 struct __gconv_loaded_object *obj = *(struct __gconv_loaded_object **) nodep;
159 if (value != preorder && value != leaf)
160 return;
162 if (obj == release_handle)
164 /* This is the object we want to unload. Now decrement the
165 reference counter. */
166 assert (obj->counter > 0);
167 --obj->counter;
169 else if (obj->counter <= 0 && obj->counter >= -TRIES_BEFORE_UNLOAD
170 && --obj->counter < -TRIES_BEFORE_UNLOAD && obj->handle != NULL)
172 /* Unload the shared object. */
173 __libc_dlclose (obj->handle);
174 obj->handle = NULL;
179 /* Notify system that a shared object is not longer needed. */
180 void
181 internal_function
182 __gconv_release_shlib (struct __gconv_loaded_object *handle)
184 /* Urgh, this is ugly but we have no other possibility. */
185 release_handle = handle;
187 /* Process all entries. Please note that we also visit entries
188 with release counts <= 0. This way we can finally unload them
189 if necessary. */
190 __twalk (loaded, (__action_fn_t) do_release_shlib);
194 /* We run this if we debug the memory allocation. */
195 static void __libc_freeres_fn_section
196 do_release_all (void *nodep)
198 struct __gconv_loaded_object *obj = (struct __gconv_loaded_object *) nodep;
200 /* Unload the shared object. */
201 if (obj->handle != NULL)
202 __libc_dlclose (obj->handle);
204 free (obj);
207 libc_freeres_fn (free_mem)
209 __tdestroy (loaded, do_release_all);
210 loaded = NULL;
214 #ifdef DEBUG
215 static void
216 do_print (const void *nodep, VISIT value, int level)
218 struct __gconv_loaded_object *obj = *(struct __gconv_loaded_object **) nodep;
220 printf ("%10s: \"%s\", %d\n",
221 value == leaf ? "leaf" :
222 value == preorder ? "preorder" :
223 value == postorder ? "postorder" : "endorder",
224 obj->name, obj->counter);
227 static void
228 print_all (void)
230 __twalk (loaded, do_print);
232 #endif