From b1a673c0a7ab8669e5c814979ab07b893efd9d50 Mon Sep 17 00:00:00 2001 From: Andreas Baumann Date: Tue, 18 May 2010 17:03:31 +0200 Subject: first proper error handling for loader --- src/library/loader.c | 33 ++++++++++++--------------------- 1 file changed, 12 insertions(+), 21 deletions(-) (limited to 'src') diff --git a/src/library/loader.c b/src/library/loader.c index 912afa4..3e58e0f 100644 --- a/src/library/loader.c +++ b/src/library/loader.c @@ -22,13 +22,18 @@ #include "port/stdlib.h" /* for malloc, free */ #include "port/stdio.h" /* for snprintf */ #include "port/gettext.h" /* for i18n */ +#include "port/string.h" /* for strncpy */ #ifdef HAVE_DLFCN #include /* for dlopen. dlclose functions */ #endif +#define INTERNAL_ERRBUF_SIZE 256 + struct wolf_library_t { - void *handle; /**< the OS handle for the library */ + void *handle; /**< the OS handle for the library */ + char errbuf[INTERNAL_ERRBUF_SIZE]; /**< internal buffer to hold the error message + * we get with dlerror */ }; wolf_library_p wolf_library_load( const char *name, wolf_error_t *error ) { @@ -49,8 +54,9 @@ wolf_library_p wolf_library_load( const char *name, wolf_error_t *error ) { l->handle = dlopen( name, flags ); if( l->handle == NULL ) { + strncpy( l->errbuf, dlerror( ), INTERNAL_ERRBUF_SIZE ); *error = WOLF_ERR_INTERNAL; - return NULL; + return l; } #else #error Not using DLFCN as shared loader. Port first! @@ -69,7 +75,7 @@ wolf_error_t wolf_library_unload( wolf_library_p l ) { res = dlclose( l->handle ); if( res != 0 ) { - return WOLF_ERR_INVALID_STATE; + return WOLF_ERR_INTERNAL; } free( l ); @@ -78,26 +84,11 @@ wolf_error_t wolf_library_unload( wolf_library_p l ) { return WOLF_OK; } -char *wolf_libary_errmsg( const wolf_error_t error, const wolf_library_p library, char *buf, size_t buflen ) { - WOLF_UNUSED( library ); +char *wolf_libary_errmsg( const wolf_error_t error, const wolf_library_p l, char *buf, size_t buflen ) { (void)wolf_error_msg( error, buf, buflen ); - -#if 0 - switch( error ) { - /* TODO: we repeat ourself here! Have a function in errors.h for generic mapping? */ - case WOLF_ERR_OUT_OF_MEMORY: - snprintf( buf, buflen, _( "Out of memory" ) ); - break; - - case WOLF_ERR_INVALID_STATE: - snprintf( buf, buflen, _( "Invalid state" ) ); - break; - - default: - snprintf( buf, buflen, _( "" ) ); - break; + if( error == WOLF_ERR_INTERNAL ) { + strncpy( buf + strlen( buf ), l->errbuf, buflen - strlen( buf ) ); } -#endif return buf; } -- cgit v1.2.3-54-g00ecf