* sysdeps/unix/sysv/linux/configure.in
[glibc.git] / dlfcn / dlerror.c
bloba754d3d9f6a89d48cf34a713153b4306683d98c6
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 Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the 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 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <dlfcn.h>
21 #include <libintl.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <bits/libc-lock.h>
27 /* Type for storing results of dynamic loading actions. */
28 struct dl_action_result
30 int errcode;
31 int returned;
32 const char *objname;
33 const char *errstring;
35 static struct dl_action_result last_result;
36 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 char *buf = NULL;
50 struct dl_action_result *result;
52 /* Get error string. */
53 result = (struct dl_action_result *) __libc_getspecific (key);
54 if (result == NULL)
55 result = &last_result;
57 /* Test whether we already returned the string. */
58 if (result->returned != 0)
60 /* We can now free the string. */
61 if (result->errstring != NULL)
63 if (strcmp (result->errstring, "out of memory") != 0)
64 free ((char *) result->errstring);
65 result->errstring = NULL;
68 else if (result->errstring != NULL)
70 buf = (char *) result->errstring;
71 if (__asprintf (&buf, result->errcode != 0 ? "%s: %s: %s" : "%s: %s",
72 result->objname, _(result->errstring),
73 strerror (result->errcode)) != -1)
75 /* We don't need the error string anymore. */
76 if (strcmp (result->errstring, "out of memory") != 0)
77 free ((char *) result->errstring);
78 result->errstring = buf;
81 /* Mark the error as returned. */
82 result->returned = 1;
85 return buf;
88 int
89 internal_function
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)
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 if (strcmp (result->errstring, "out of memory") != 0)
126 free ((char *) result->errstring);
127 result->errstring = NULL;
130 result->errcode = _dl_catch_error (&result->objname, &result->errstring,
131 operate, args);
133 /* If no error we mark that no error string is available. */
134 result->returned = result->errstring == NULL;
136 return result->errstring != NULL;
140 /* Initialize buffers for results. */
141 static void
142 init (void)
144 if (__libc_key_create (&key, free_key_mem))
145 /* Creating the key failed. This means something really went
146 wrong. In any case use a static buffer which is better than
147 nothing. */
148 static_buf = &last_result;
152 /* Free the thread specific data, this is done if a thread terminates. */
153 static void
154 free_key_mem (void *mem)
156 struct dl_action_result *result = (struct dl_action_result *) mem;
158 if (result->errstring != NULL
159 && strcmp (result->errstring, "out of memory") != 0)
160 free ((char *) result->errstring);
162 free (mem);
163 __libc_setspecific (key, NULL);