2.3.3-96
[glibc/history.git] / dlfcn / dlerror.c
blob8789f4f68b7d2079028b92347c7d9e9dd47cedad
1 /* Return error detail for failing <dlfcn.h> functions.
2 Copyright (C) 1995-2000,2002,2003,2004 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <dlfcn.h>
21 #include <libintl.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <bits/libc-lock.h>
26 #include <ldsodefs.h>
28 #if !defined SHARED && defined IS_IN_libdl
30 char *
31 dlerror (void)
33 return __dlerror ();
36 #else
38 /* Type for storing results of dynamic loading actions. */
39 struct dl_action_result
41 int errcode;
42 int returned;
43 const char *objname;
44 const char *errstring;
46 static struct dl_action_result last_result;
47 static struct dl_action_result *static_buf;
49 /* This is the key for the thread specific memory. */
50 static __libc_key_t key;
51 __libc_once_define (static, once);
53 /* Destructor for the thread-specific data. */
54 static void init (void);
55 static void free_key_mem (void *mem);
58 char *
59 __dlerror (void)
61 char *buf = NULL;
62 struct dl_action_result *result;
64 # ifdef SHARED
65 if (__builtin_expect (_dlfcn_hook != NULL, 0))
66 return _dlfcn_hook->dlerror ();
67 # endif
69 /* If we have not yet initialized the buffer do it now. */
70 __libc_once (once, init);
72 /* Get error string. */
73 result = (struct dl_action_result *) __libc_getspecific (key);
74 if (result == NULL)
75 result = &last_result;
77 /* Test whether we already returned the string. */
78 if (result->returned != 0)
80 /* We can now free the string. */
81 if (result->errstring != NULL)
83 if (strcmp (result->errstring, "out of memory") != 0)
84 free ((char *) result->errstring);
85 result->errstring = NULL;
88 else if (result->errstring != NULL)
90 buf = (char *) result->errstring;
91 int n;
92 if (result->errcode == 0)
93 n = __asprintf (&buf, "%s%s%s",
94 result->objname,
95 result->objname[0] == '\0' ? "" : ": ",
96 _(result->errstring));
97 else
98 n = __asprintf (&buf, "%s%s%s: %s",
99 result->objname,
100 result->objname[0] == '\0' ? "" : ": ",
101 _(result->errstring),
102 strerror (result->errcode));
103 if (n != -1)
105 /* We don't need the error string anymore. */
106 if (strcmp (result->errstring, "out of memory") != 0)
107 free ((char *) result->errstring);
108 result->errstring = buf;
111 /* Mark the error as returned. */
112 result->returned = 1;
115 return buf;
117 # ifdef SHARED
118 strong_alias (__dlerror, dlerror)
119 # endif
122 internal_function
123 _dlerror_run (void (*operate) (void *), void *args)
125 struct dl_action_result *result;
127 /* If we have not yet initialized the buffer do it now. */
128 __libc_once (once, init);
130 /* Get error string and number. */
131 if (static_buf != NULL)
132 result = static_buf;
133 else
135 /* We don't use the static buffer and so we have a key. Use it
136 to get the thread-specific buffer. */
137 result = __libc_getspecific (key);
138 if (result == NULL)
140 result = (struct dl_action_result *) calloc (1, sizeof (*result));
141 if (result == NULL)
142 /* We are out of memory. Since this is no really critical
143 situation we carry on by using the global variable.
144 This might lead to conflicts between the threads but
145 they soon all will have memory problems. */
146 result = &last_result;
147 else
148 /* Set the tsd. */
149 __libc_setspecific (key, result);
153 if (result->errstring != NULL)
155 /* Free the error string from the last failed command. This can
156 happen if `dlerror' was not run after an error was found. */
157 if (strcmp (result->errstring, "out of memory") != 0)
158 free ((char *) result->errstring);
159 result->errstring = NULL;
162 result->errcode = GLRO(dl_catch_error) (&result->objname, &result->errstring,
163 operate, args);
165 /* If no error we mark that no error string is available. */
166 result->returned = result->errstring == NULL;
168 return result->errstring != NULL;
172 /* Initialize buffers for results. */
173 static void
174 init (void)
176 if (__libc_key_create (&key, free_key_mem))
177 /* Creating the key failed. This means something really went
178 wrong. In any case use a static buffer which is better than
179 nothing. */
180 static_buf = &last_result;
183 static void
184 __attribute__ ((destructor))
185 fini (void)
187 if (last_result.errstring != NULL
188 && strcmp (last_result.errstring, "out of memory") != 0)
189 free ((char *) last_result.errstring);
193 /* Free the thread specific data, this is done if a thread terminates. */
194 static void
195 free_key_mem (void *mem)
197 struct dl_action_result *result = (struct dl_action_result *) mem;
199 if (result->errstring != NULL
200 && strcmp (result->errstring, "out of memory") != 0)
201 free ((char *) result->errstring);
203 free (mem);
204 __libc_setspecific (key, NULL);
207 # ifdef SHARED
209 struct dlfcn_hook *_dlfcn_hook __attribute__((nocommon));
210 libdl_hidden_data_def (_dlfcn_hook)
212 # else
214 static struct dlfcn_hook _dlfcn_hooks =
216 .dlopen = __dlopen,
217 .dlclose = __dlclose,
218 .dlsym = __dlsym,
219 .dlvsym = __dlvsym,
220 .dlerror = __dlerror,
221 .dladdr = __dladdr,
222 .dladdr1 = __dladdr1,
223 .dlinfo = __dlinfo,
224 .dlmopen = __dlmopen
227 void
228 __libc_register_dlfcn_hook (struct link_map *map)
230 struct dlfcn_hook **hook;
232 hook = (struct dlfcn_hook **) __libc_dlsym_private (map, "_dlfcn_hook");
233 if (hook != NULL)
234 *hook = &_dlfcn_hooks;
236 # endif
237 #endif