Update.
[glibc.git] / dlfcn / dlerror.c
blobb4ffde5fdfe2521ccdd854a89a62bebcbbb79c43
1 /* Return error detail for failing <dlfcn.h> functions.
2 Copyright (C) 1995, 1996, 1997, 1998, 1999 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 <dlfcn.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <bits/libc-lock.h>
26 /* Type for storing results of dynamic loading actions. */
27 struct dl_action_result
29 int errcode;
30 int returned;
31 char *errstring;
33 static struct dl_action_result last_result;
34 static struct dl_action_result *static_buf;
37 /* This is the key for the thread specific memory. */
38 static __libc_key_t key;
40 /* Destructor for the thread-specific data. */
41 static void init (void);
42 static void free_key_mem (void *mem);
45 char *
46 dlerror (void)
48 char *buf;
49 struct dl_action_result *result;
51 /* Get error string. */
52 result = (struct dl_action_result *) __libc_getspecific (key);
53 if (result == NULL)
54 result = &last_result;
56 /* Test whether we already returned the string. */
57 if (result->returned != 0)
59 /* We can now free the string. */
60 if (result->errstring != NULL)
62 free (result->errstring);
63 result->errstring = NULL;
65 buf = NULL;
67 else
69 buf = result->errstring;
70 if (result->errcode != 0
71 && __asprintf (&buf, "%s: %s",
72 result->errstring, strerror (result->errcode)) != -1)
74 /* We don't need the error string anymore. */
75 free (result->errstring);
76 result->errstring = buf;
79 /* Mark the error as returned. */
80 result->returned = 1;
83 return buf;
86 int
87 internal_function
88 _dlerror_run (void (*operate) (void *), void *args)
90 __libc_once_define (static, once);
91 struct dl_action_result *result;
93 /* If we have not yet initialized the buffer do it now. */
94 __libc_once (once, init);
96 /* Get error string and number. */
97 if (static_buf != NULL)
98 result = static_buf;
99 else
101 /* We don't use the static buffer and so we have a key. Use it
102 to get the thread-specific buffer. */
103 result = __libc_getspecific (key);
104 if (result == NULL)
106 result = (struct dl_action_result *) calloc (1, sizeof (*result));
107 if (result == NULL)
108 /* We are out of memory. Since this is no really critical
109 situation we carry on by using the global variable.
110 This might lead to conflicts between the threads but
111 they soon all will have memory problems. */
112 result = &last_result;
113 else
114 /* Set the tsd. */
115 __libc_setspecific (key, result);
119 if (result->errstring != NULL)
120 /* Free the error string from the last failed command. This can
121 happen if `dlerror' was not run after an error was found. */
122 free (result->errstring);
124 result->errcode = _dl_catch_error (&result->errstring, operate, args);
126 /* If no error we mark that no error string is available. */
127 result->returned = result->errstring == NULL;
129 return result->errstring != NULL;
133 /* Initialize buffers for results. */
134 static void
135 init (void)
137 if (__libc_key_create (&key, free_key_mem))
138 /* Creating the key failed. This means something really went
139 wrong. In any case use a static buffer which is better than
140 nothing. */
141 static_buf = &last_result;
145 /* Free the thread specific data, this is done if a thread terminates. */
146 static void
147 free_key_mem (void *mem)
149 free (mem);
150 __libc_setspecific (key, NULL);