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
;
39 /* This is the key for the thread specific memory. */
40 static __libc_key_t key
;
42 /* Destructor for the thread-specific data. */
43 static void init (void);
44 static void free_key_mem (void *mem
);
51 struct dl_action_result
*result
;
53 /* Get error string. */
54 result
= (struct dl_action_result
*) __libc_getspecific (key
);
56 result
= &last_result
;
58 /* Test whether we already returned the string. */
59 if (result
->returned
!= 0)
61 /* We can now free the string. */
62 if (result
->errstring
!= NULL
)
64 free ((char *) result
->errstring
);
65 result
->errstring
= NULL
;
71 buf
= (char *) result
->errstring
;
72 if (result
->errcode
!= 0
73 && __asprintf (&buf
, "%s: %s: %s",
74 result
->objname
, _(result
->errstring
),
75 strerror (result
->errcode
)) != -1)
77 /* We don't need the error string anymore. */
78 free ((char *) result
->errstring
);
79 result
->errstring
= buf
;
82 /* Mark the error as returned. */
91 _dlerror_run (void (*operate
) (void *), void *args
)
93 __libc_once_define (static, once
);
94 struct dl_action_result
*result
;
96 /* If we have not yet initialized the buffer do it now. */
97 __libc_once (once
, init
);
99 /* Get error string and number. */
100 if (static_buf
!= NULL
)
104 /* We don't use the static buffer and so we have a key. Use it
105 to get the thread-specific buffer. */
106 result
= __libc_getspecific (key
);
109 result
= (struct dl_action_result
*) calloc (1, sizeof (*result
));
111 /* We are out of memory. Since this is no really critical
112 situation we carry on by using the global variable.
113 This might lead to conflicts between the threads but
114 they soon all will have memory problems. */
115 result
= &last_result
;
118 __libc_setspecific (key
, result
);
122 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 free ((char *) result
->errstring
);
127 result
->errcode
= _dl_catch_error (&result
->objname
, &result
->errstring
,
130 /* If no error we mark that no error string is available. */
131 result
->returned
= result
->errstring
== NULL
;
133 return result
->errstring
!= NULL
;
137 /* Initialize buffers for results. */
141 if (__libc_key_create (&key
, free_key_mem
))
142 /* Creating the key failed. This means something really went
143 wrong. In any case use a static buffer which is better than
145 static_buf
= &last_result
;
149 /* Free the thread specific data, this is done if a thread terminates. */
151 free_key_mem (void *mem
)
154 __libc_setspecific (key
, NULL
);