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
25 #include <bits/libc-lock.h>
28 /* Type for storing results of dynamic loading actions. */
29 struct dl_action_result
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
);
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
);
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
;
77 if (result
->errcode
== 0)
78 n
= __asprintf (&buf
, "%s%s%s",
80 result
->objname
[0] == '\0' ? "" : ": ",
81 _(result
->errstring
));
83 n
= __asprintf (&buf
, "%s%s%s: %s",
85 result
->objname
[0] == '\0' ? "" : ": ",
87 strerror (result
->errcode
));
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. */
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
)
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
);
122 result
= (struct dl_action_result
*) calloc (1, sizeof (*result
));
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
;
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
,
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. */
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
162 static_buf
= &last_result
;
166 __attribute__ ((destructor
))
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. */
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
);
186 __libc_setspecific (key
, NULL
);