1 /* Return error detail for failing <dlfcn.h> functions.
2 Copyright (C) 1995, 1996, 1997, 1998 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. */
24 #include <bits/libc-lock.h>
25 #include <elf/ldsodefs.h>
28 /* Type for storing results of dynamic loading actions. */
29 struct dl_action_result
34 static struct dl_action_result last_result
;
35 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
;
58 /* Get error string. */
59 result
= (struct dl_action_result
*) __libc_getspecific (key
);
61 result
= &last_result
;
63 if (! result
->errstring
)
66 if (result
->errcode
== 0)
67 buf
= result
->errstring
;
70 if (__asprintf (&buf
, "%s: %s",
71 result
->errstring
, strerror (result
->errcode
)) == -1)
74 /* We don't need the error string anymore. */
75 free (result
->errstring
);
78 /* Reset the error indicator. */
79 result
->errstring
= NULL
;
86 _dlerror_run (void (*operate
) (void *), void *args
)
88 __libc_once_define (static, once
);
89 struct dl_action_result
*result
;
91 /* If we have not yet initialized the buffer do it now. */
92 __libc_once (once
, init
);
94 /* Get error string and number. */
95 if (static_buf
!= NULL
)
99 /* We don't use the static buffer and so we have a key. Use it
100 to get the thread-specific buffer. */
101 result
= __libc_getspecific (key
);
104 result
= (struct dl_action_result
*) calloc (1, sizeof (*result
));
106 /* We are out of memory. Since this is no really critical
107 situation we carry on by using the global variable.
108 This might lead to conflicts between the threads but
109 they soon all will have memory problems. */
110 result
= &last_result
;
113 __libc_setspecific (key
, result
);
117 if (result
->errstring
!= NULL
)
118 /* Free the error string from the last failed command. This can
119 happen if `dlerror' was not run after an error was found. */
120 free (result
->errstring
);
122 result
->errcode
= _dl_catch_error (&result
->errstring
, operate
, args
);
124 return result
->errstring
!= NULL
;
128 /* Initialize buffers for results. */
132 if (__libc_key_create (&key
, free_key_mem
))
133 /* Creating the key failed. This means something really went
134 wrong. In any case use a static buffer which is better than
136 static_buf
= &last_result
;
140 /* Free the thread specific data, this is done if a thread terminates. */
142 free_key_mem (void *mem
)
145 __libc_setspecific (key
, NULL
);