Update.
[glibc.git] / elf / dlerror.c
blob3edafe4f1c10fd9d91385da18f3711803b71ab0c
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. */
20 #include <link.h>
21 #include <dlfcn.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <stdlib.h>
25 #include <bits/libc-lock.h>
28 /* Type for storing results of dynamic loading actions. */
29 struct dl_action_result
31 int errcode;
32 char *errstring;
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);
46 char *
47 dlerror (void)
49 static char *buf;
50 struct dl_action_result *result;
52 if (buf)
54 free (buf);
55 buf = NULL;
58 /* Get error string. */
59 if (__libc_internal_tsd_get != NULL)
61 result = (struct dl_action_result *) __libc_getspecific (key);
62 if (result == NULL)
63 result = &last_result;
65 else
66 result = &last_result;
68 if (! result->errstring)
69 return NULL;
71 if (result->errcode == 0)
72 buf = result->errstring;
73 else
75 if (asprintf (&buf, "%s: %s",
76 result->errstring, strerror (result->errcode)) == -1)
77 buf = NULL;
79 /* We don't need the error string anymore. */
80 free (result->errstring);
83 /* Reset the error indicator. */
84 result->errstring = NULL;
86 return buf;
89 int
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)
100 result = static_buf;
101 else
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);
106 if (result == NULL)
108 result = (struct dl_action_result *) calloc (1, sizeof (*result));
109 if (result == NULL)
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;
115 else
116 /* Set the tsd. */
117 __libc_setspecific (key, result);
121 if (result->errstring != NULL)
122 /* Free the error string from the last failed command. This can
123 happen if `dlerror' was not run after an error was found. */
124 free (result->errstring);
126 result->errcode = _dl_catch_error (&result->errstring, operate, args);
128 return result->errstring != NULL;
132 /* Initialize buffers for results. */
133 static void
134 init (void)
136 if (__libc_key_create (&key, free_key_mem))
137 /* Creating the key failed. This means something really went
138 wrong. In any case use a static buffer which is better than
139 nothing. */
140 static_buf = &last_result;
144 /* Free the thread specific data, this is done if a thread terminates. */
145 static void
146 free_key_mem (void *mem)
148 free (mem);
149 __libc_setspecific (key, NULL);