/* Return the buffer to be used. */ static char * getbuffer (void) { char *result; if (static_buf != NULL) result = static_buf; else { /* We don't use the static buffer and so we have a key. Use it to get the thread-specific buffer. */ result = __libc_getspecific (key); if (result == NULL) { /* No buffer allocated so far. */ result = malloc (BUFFERSIZ); if (result == NULL) /* No more memory available. We use the static buffer. */ result = local_buf; else __libc_setspecific (key, result); } } return result; }
char * __dlerror (void) { char *buf = NULL; struct dl_action_result *result; # ifdef SHARED if (__glibc_unlikely (_dlfcn_hook != NULL)) return _dlfcn_hook->dlerror (); # endif /* If we have not yet initialized the buffer do it now. */ __libc_once (once, init); /* Get error string. */ result = (struct dl_action_result *) __libc_getspecific (key); if (result == NULL) result = &last_result; /* Test whether we already returned the string. */ if (result->returned != 0) { /* We can now free the string. */ if (result->errstring != NULL) { if (strcmp (result->errstring, "out of memory") != 0) free ((char *) result->errstring); result->errstring = NULL; } } else if (result->errstring != NULL) { buf = (char *) result->errstring; int n; if (result->errcode == 0) n = __asprintf (&buf, "%s%s%s", result->objname, result->objname[0] == '\0' ? "" : ": ", _(result->errstring)); else n = __asprintf (&buf, "%s%s%s: %s", result->objname, result->objname[0] == '\0' ? "" : ": ", _(result->errstring), strerror (result->errcode)); if (n != -1) { /* We don't need the error string anymore. */ if (strcmp (result->errstring, "out of memory") != 0) free ((char *) result->errstring); result->errstring = buf; } /* Mark the error as returned. */ result->returned = 1; } return buf; }
/* Free the dlerror-related resources. */ void __dlerror_main_freeres (void) { void *mem; /* Free the global memory if used. */ check_free (&last_result); /* Free the TSD memory if used. */ mem = __libc_getspecific (key); if (mem != NULL) free_key_mem (mem); }
int internal_function _dlerror_run (void (*operate) (void *), void *args) { struct dl_action_result *result; /* If we have not yet initialized the buffer do it now. */ __libc_once (once, init); /* Get error string and number. */ if (static_buf != NULL) result = static_buf; else { /* We don't use the static buffer and so we have a key. Use it to get the thread-specific buffer. */ result = __libc_getspecific (key); if (result == NULL) { result = (struct dl_action_result *) calloc (1, sizeof (*result)); if (result == NULL) /* We are out of memory. Since this is no really critical situation we carry on by using the global variable. This might lead to conflicts between the threads but they soon all will have memory problems. */ result = &last_result; else /* Set the tsd. */ __libc_setspecific (key, result); } } if (result->errstring != NULL) { /* Free the error string from the last failed command. This can happen if `dlerror' was not run after an error was found. */ if (result->malloced) free ((char *) result->errstring); result->errstring = NULL; } result->errcode = GLRO(dl_catch_error) (&result->objname, &result->errstring, &result->malloced, operate, args); /* If no error we mark that no error string is available. */ result->returned = result->errstring == NULL; return result->errstring != NULL; }
/* Free the dlerror-related resources. */ void __dlerror_main_freeres (void) { /* Free the global memory if used. */ check_free (&last_result); if (__libc_once_get (once) && static_buf == NULL) { /* init () has been run and we don't use the static buffer. So we have a valid key. */ void *mem; /* Free the TSD memory if used. */ mem = __libc_getspecific (key); if (mem != NULL) free_key_mem (mem); } }