1 /* Return error detail for failing <dlfcn.h> functions.
2 Copyright (C) 1995, 96, 97, 98, 99, 2000 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 Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 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 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
25 #include <bits/libc-lock.h>
27 /* Type for storing results of dynamic loading actions. */
28 struct dl_action_result
33 const char *errstring
;
35 static struct dl_action_result last_result
;
36 static struct dl_action_result
*static_buf
;
38 /* This is the key for the thread specific memory. */
39 static __libc_key_t key
;
41 /* Destructor for the thread-specific data. */
42 static void init (void);
43 static void free_key_mem (void *mem
);
50 struct dl_action_result
*result
;
52 /* Get error string. */
53 result
= (struct dl_action_result
*) __libc_getspecific (key
);
55 result
= &last_result
;
57 /* Test whether we already returned the string. */
58 if (result
->returned
!= 0)
60 /* We can now free the string. */
61 if (result
->errstring
!= NULL
)
63 if (strcmp (result
->errstring
, "out of memory") != 0)
64 free ((char *) result
->errstring
);
65 result
->errstring
= NULL
;
68 else if (result
->errstring
!= NULL
)
70 buf
= (char *) result
->errstring
;
71 if (__asprintf (&buf
, result
->errcode
!= 0 ? "%s: %s: %s" : "%s: %s",
72 result
->objname
, _(result
->errstring
),
73 strerror (result
->errcode
)) != -1)
75 /* We don't need the error string anymore. */
76 if (strcmp (result
->errstring
, "out of memory") != 0)
77 free ((char *) result
->errstring
);
78 result
->errstring
= buf
;
81 /* Mark the error as returned. */
90 _dlerror_run (void (*operate
) (void *), void *args
)
92 __libc_once_define (static, once
);
93 struct dl_action_result
*result
;
95 /* If we have not yet initialized the buffer do it now. */
96 __libc_once (once
, init
);
98 /* Get error string and number. */
99 if (static_buf
!= NULL
)
103 /* We don't use the static buffer and so we have a key. Use it
104 to get the thread-specific buffer. */
105 result
= __libc_getspecific (key
);
108 result
= (struct dl_action_result
*) calloc (1, sizeof (*result
));
110 /* We are out of memory. Since this is no really critical
111 situation we carry on by using the global variable.
112 This might lead to conflicts between the threads but
113 they soon all will have memory problems. */
114 result
= &last_result
;
117 __libc_setspecific (key
, result
);
121 if (result
->errstring
!= NULL
)
123 /* Free the error string from the last failed command. This can
124 happen if `dlerror' was not run after an error was found. */
125 if (strcmp (result
->errstring
, "out of memory") != 0)
126 free ((char *) result
->errstring
);
127 result
->errstring
= NULL
;
130 result
->errcode
= _dl_catch_error (&result
->objname
, &result
->errstring
,
133 /* If no error we mark that no error string is available. */
134 result
->returned
= result
->errstring
== NULL
;
136 return result
->errstring
!= NULL
;
140 /* Initialize buffers for results. */
144 if (__libc_key_create (&key
, free_key_mem
))
145 /* Creating the key failed. This means something really went
146 wrong. In any case use a static buffer which is better than
148 static_buf
= &last_result
;
152 /* Free the thread specific data, this is done if a thread terminates. */
154 free_key_mem (void *mem
)
156 struct dl_action_result
*result
= (struct dl_action_result
*) mem
;
158 if (result
->errstring
!= NULL
159 && strcmp (result
->errstring
, "out of memory") != 0)
160 free ((char *) result
->errstring
);
163 __libc_setspecific (key
, NULL
);