]> sourceware.org Git - glibc.git/blob - dlfcn/dlerror.c
linuxthreads/ChangeLog
[glibc.git] / dlfcn / dlerror.c
1 /* Return error detail for failing <dlfcn.h> functions.
2 Copyright (C) 1995,1996,1997,1998,1999,2000,2002
3 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
5
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
10
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
20
21 #include <dlfcn.h>
22 #include <libintl.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <bits/libc-lock.h>
27
28 /* Type for storing results of dynamic loading actions. */
29 struct dl_action_result
30 {
31 int errcode;
32 int returned;
33 const char *objname;
34 const char *errstring;
35 };
36 static struct dl_action_result last_result;
37 static struct dl_action_result *static_buf;
38
39 /* This is the key for the thread specific memory. */
40 static __libc_key_t key;
41
42 /* Destructor for the thread-specific data. */
43 static void init (void);
44 static void free_key_mem (void *mem);
45
46
47 char *
48 dlerror (void)
49 {
50 char *buf = NULL;
51 struct dl_action_result *result;
52
53 /* Get error string. */
54 result = (struct dl_action_result *) __libc_getspecific (key);
55 if (result == NULL)
56 result = &last_result;
57
58 /* Test whether we already returned the string. */
59 if (result->returned != 0)
60 {
61 /* We can now free the string. */
62 if (result->errstring != NULL)
63 {
64 if (strcmp (result->errstring, "out of memory") != 0)
65 free ((char *) result->errstring);
66 result->errstring = NULL;
67 }
68 }
69 else if (result->errstring != NULL)
70 {
71 buf = (char *) result->errstring;
72 if (__asprintf (&buf, result->errcode != 0 ? "%s: %s: %s" : "%s: %s",
73 result->objname, _(result->errstring),
74 strerror (result->errcode)) != -1)
75 {
76 /* We don't need the error string anymore. */
77 if (strcmp (result->errstring, "out of memory") != 0)
78 free ((char *) result->errstring);
79 result->errstring = buf;
80 }
81
82 /* Mark the error as returned. */
83 result->returned = 1;
84 }
85
86 return buf;
87 }
88
89 int
90 internal_function
91 _dlerror_run (void (*operate) (void *), void *args)
92 {
93 __libc_once_define (static, once);
94 struct dl_action_result *result;
95
96 /* If we have not yet initialized the buffer do it now. */
97 __libc_once (once, init);
98
99 /* Get error string and number. */
100 if (static_buf != NULL)
101 result = static_buf;
102 else
103 {
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);
107 if (result == NULL)
108 {
109 result = (struct dl_action_result *) calloc (1, sizeof (*result));
110 if (result == NULL)
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;
116 else
117 /* Set the tsd. */
118 __libc_setspecific (key, result);
119 }
120 }
121
122 if (result->errstring != NULL)
123 {
124 /* Free the error string from the last failed command. This can
125 happen if `dlerror' was not run after an error was found. */
126 if (strcmp (result->errstring, "out of memory") != 0)
127 free ((char *) result->errstring);
128 result->errstring = NULL;
129 }
130
131 result->errcode = _dl_catch_error (&result->objname, &result->errstring,
132 operate, args);
133
134 /* If no error we mark that no error string is available. */
135 result->returned = result->errstring == NULL;
136
137 return result->errstring != NULL;
138 }
139
140
141 /* Initialize buffers for results. */
142 static void
143 init (void)
144 {
145 if (__libc_key_create (&key, free_key_mem))
146 /* Creating the key failed. This means something really went
147 wrong. In any case use a static buffer which is better than
148 nothing. */
149 static_buf = &last_result;
150 }
151
152 static void
153 __attribute__ ((destructor))
154 fini (void)
155 {
156 if (last_result.errstring != NULL
157 && strcmp (last_result.errstring, "out of memory") != 0)
158 free ((char *) last_result.errstring);
159 }
160
161
162 /* Free the thread specific data, this is done if a thread terminates. */
163 static void
164 free_key_mem (void *mem)
165 {
166 struct dl_action_result *result = (struct dl_action_result *) mem;
167
168 if (result->errstring != NULL
169 && strcmp (result->errstring, "out of memory") != 0)
170 free ((char *) result->errstring);
171
172 free (mem);
173 __libc_setspecific (key, NULL);
174 }
This page took 0.044007 seconds and 5 git commands to generate.