]> git.ipfire.org Git - thirdparty/glibc.git/blob - dlfcn/dlerror.c
Update.
[thirdparty/glibc.git] / dlfcn / dlerror.c
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.
4
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.
9
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.
14
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. */
19
20 #include <dlfcn.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <bits/libc-lock.h>
25
26 /* Type for storing results of dynamic loading actions. */
27 struct dl_action_result
28 {
29 int errcode;
30 char *errstring;
31 };
32 static struct dl_action_result last_result;
33 static struct dl_action_result *static_buf;
34
35
36 /* This is the key for the thread specific memory. */
37 static __libc_key_t key;
38
39 /* Destructor for the thread-specific data. */
40 static void init (void);
41 static void free_key_mem (void *mem);
42
43
44 char *
45 dlerror (void)
46 {
47 static char *buf;
48 struct dl_action_result *result;
49
50 if (buf)
51 {
52 free (buf);
53 buf = NULL;
54 }
55
56 /* Get error string. */
57 result = (struct dl_action_result *) __libc_getspecific (key);
58 if (result == NULL)
59 result = &last_result;
60
61 if (! result->errstring)
62 return NULL;
63
64 if (result->errcode == 0)
65 buf = result->errstring;
66 else
67 {
68 if (__asprintf (&buf, "%s: %s",
69 result->errstring, strerror (result->errcode)) == -1)
70 buf = NULL;
71
72 /* We don't need the error string anymore. */
73 free (result->errstring);
74 }
75
76 /* Reset the error indicator. */
77 result->errstring = NULL;
78
79 return buf;
80 }
81
82 int
83 internal_function
84 _dlerror_run (void (*operate) (void *), void *args)
85 {
86 __libc_once_define (static, once);
87 struct dl_action_result *result;
88
89 /* If we have not yet initialized the buffer do it now. */
90 __libc_once (once, init);
91
92 /* Get error string and number. */
93 if (static_buf != NULL)
94 result = static_buf;
95 else
96 {
97 /* We don't use the static buffer and so we have a key. Use it
98 to get the thread-specific buffer. */
99 result = __libc_getspecific (key);
100 if (result == NULL)
101 {
102 result = (struct dl_action_result *) calloc (1, sizeof (*result));
103 if (result == NULL)
104 /* We are out of memory. Since this is no really critical
105 situation we carry on by using the global variable.
106 This might lead to conflicts between the threads but
107 they soon all will have memory problems. */
108 result = &last_result;
109 else
110 /* Set the tsd. */
111 __libc_setspecific (key, result);
112 }
113 }
114
115 if (result->errstring != NULL)
116 /* Free the error string from the last failed command. This can
117 happen if `dlerror' was not run after an error was found. */
118 free (result->errstring);
119
120 result->errcode = _dl_catch_error (&result->errstring, operate, args);
121
122 return result->errstring != NULL;
123 }
124
125
126 /* Initialize buffers for results. */
127 static void
128 init (void)
129 {
130 if (__libc_key_create (&key, free_key_mem))
131 /* Creating the key failed. This means something really went
132 wrong. In any case use a static buffer which is better than
133 nothing. */
134 static_buf = &last_result;
135 }
136
137
138 /* Free the thread specific data, this is done if a thread terminates. */
139 static void
140 free_key_mem (void *mem)
141 {
142 free (mem);
143 __libc_setspecific (key, NULL);
144 }