2004-09-18 Roland McGrath <roland@redhat.com>
[glibc/history.git] / dlfcn / dlerror.c
blob1cde04b08019625ab62254756dabe1ebee6153a3
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 /* Type for storing results of dynamic loading actions. */
29 struct dl_action_result
31 int errcode;
32 int returned;
33 const char *objname;
34 const char *errstring;
36 static struct dl_action_result last_result;
37 static struct dl_action_result *static_buf;
39 /* This is the key for the thread specific memory. */
40 static __libc_key_t key;
41 __libc_once_define (static, once);
43 /* Destructor for the thread-specific data. */
44 static void init (void);
45 static void free_key_mem (void *mem);
48 char *
49 dlerror (void)
51 char *buf = NULL;
52 struct dl_action_result *result;
54 /* If we have not yet initialized the buffer do it now. */
55 __libc_once (once, init);
57 /* Get error string. */
58 result = (struct dl_action_result *) __libc_getspecific (key);
59 if (result == NULL)
60 result = &last_result;
62 /* Test whether we already returned the string. */
63 if (result->returned != 0)
65 /* We can now free the string. */
66 if (result->errstring != NULL)
68 if (strcmp (result->errstring, "out of memory") != 0)
69 free ((char *) result->errstring);
70 result->errstring = NULL;
73 else if (result->errstring != NULL)
75 buf = (char *) result->errstring;
76 int n;
77 if (result->errcode == 0)
78 n = __asprintf (&buf, "%s%s%s",
79 result->objname,
80 result->objname[0] == '\0' ? "" : ": ",
81 _(result->errstring));
82 else
83 n = __asprintf (&buf, "%s%s%s: %s",
84 result->objname,
85 result->objname[0] == '\0' ? "" : ": ",
86 _(result->errstring),
87 strerror (result->errcode));
88 if (n != -1)
90 /* We don't need the error string anymore. */
91 if (strcmp (result->errstring, "out of memory") != 0)
92 free ((char *) result->errstring);
93 result->errstring = buf;
96 /* Mark the error as returned. */
97 result->returned = 1;
100 return buf;
104 internal_function
105 _dlerror_run (void (*operate) (void *), void *args)
107 struct dl_action_result *result;
109 /* If we have not yet initialized the buffer do it now. */
110 __libc_once (once, init);
112 /* Get error string and number. */
113 if (static_buf != NULL)
114 result = static_buf;
115 else
117 /* We don't use the static buffer and so we have a key. Use it
118 to get the thread-specific buffer. */
119 result = __libc_getspecific (key);
120 if (result == NULL)
122 result = (struct dl_action_result *) calloc (1, sizeof (*result));
123 if (result == NULL)
124 /* We are out of memory. Since this is no really critical
125 situation we carry on by using the global variable.
126 This might lead to conflicts between the threads but
127 they soon all will have memory problems. */
128 result = &last_result;
129 else
130 /* Set the tsd. */
131 __libc_setspecific (key, result);
135 if (result->errstring != NULL)
137 /* Free the error string from the last failed command. This can
138 happen if `dlerror' was not run after an error was found. */
139 if (strcmp (result->errstring, "out of memory") != 0)
140 free ((char *) result->errstring);
141 result->errstring = NULL;
144 result->errcode = GLRO(dl_catch_error) (&result->objname, &result->errstring,
145 operate, args);
147 /* If no error we mark that no error string is available. */
148 result->returned = result->errstring == NULL;
150 return result->errstring != NULL;
154 /* Initialize buffers for results. */
155 static void
156 init (void)
158 if (__libc_key_create (&key, free_key_mem))
159 /* Creating the key failed. This means something really went
160 wrong. In any case use a static buffer which is better than
161 nothing. */
162 static_buf = &last_result;
165 static void
166 __attribute__ ((destructor))
167 fini (void)
169 if (last_result.errstring != NULL
170 && strcmp (last_result.errstring, "out of memory") != 0)
171 free ((char *) last_result.errstring);
175 /* Free the thread specific data, this is done if a thread terminates. */
176 static void
177 free_key_mem (void *mem)
179 struct dl_action_result *result = (struct dl_action_result *) mem;
181 if (result->errstring != NULL
182 && strcmp (result->errstring, "out of memory") != 0)
183 free ((char *) result->errstring);
185 free (mem);
186 __libc_setspecific (key, NULL);