From b13ddadad16b2b39ad00cef1ceb19c865335c0ae Mon Sep 17 00:00:00 2001 From: Andreas Baumann Date: Mon, 24 May 2010 20:53:46 +0200 Subject: better error handling in library loader --- docs/libraries/README | 2 ++ include/wolf/library/loader.h | 3 +-- src/library/loader.c | 15 +++++++-------- tests/library/test_loader.c | 23 ++++++++++++++++------- 4 files changed, 26 insertions(+), 17 deletions(-) diff --git a/docs/libraries/README b/docs/libraries/README index 23919c3..f2ecb2a 100644 --- a/docs/libraries/README +++ b/docs/libraries/README @@ -19,6 +19,8 @@ Some more unsorted links: - On MacX: http://developer.apple.com/mac/library/releasenotes/DeveloperTools/RN-dyld/index.html - http://www.flounder.com/loadlibrary_explorer.htm: how LoadLibrary works internally (search path for DLLs, security, etc.) +- http://tldp.org/HOWTO/Program-Library-HOWTO/dl-libraries.html: explainig about + the dlerror odity around dlsym Goal: - we want abstractions for varying versions of dlopen/LoadLibrary(Ex) etc. diff --git a/include/wolf/library/loader.h b/include/wolf/library/loader.h index 20414c8..2f37da2 100644 --- a/include/wolf/library/loader.h +++ b/include/wolf/library/loader.h @@ -99,14 +99,13 @@ wolf_error_t wolf_library_unload( wolf_library_p library ); /** * Converts the last error and state of the library object in a human readable message. * - * @param error the error which happened after calling a library loader function * @param library the library object for which we want a error message * @param buf the buffer which will hold the error message * @param buflen the size of the buffer * * @return a pointer to buf for convenience */ -char *wolf_library_errmsg( const wolf_error_t error, const wolf_library_p library, char *buf, size_t buflen ); +char *wolf_library_error_msg( const wolf_library_p library, char *buf, size_t buflen ); /** * Gets a symbol (a function) from the library object. It has to be diff --git a/src/library/loader.c b/src/library/loader.c index 93b9298..1606557 100644 --- a/src/library/loader.c +++ b/src/library/loader.c @@ -93,7 +93,11 @@ wolf_error_t wolf_library_unload( wolf_library_p l ) { BOOL res = FALSE; #endif - if( l == NULL || l->handle == NULL ) { + if( l == NULL ) { + return WOLF_OK; + } + + if( l->handle == NULL ) { return WOLF_ERR_INVALID_STATE; } @@ -121,13 +125,8 @@ wolf_error_t wolf_library_unload( wolf_library_p l ) { return WOLF_OK; } -char *wolf_library_errmsg( const wolf_error_t error, const wolf_library_p library, char *buf, size_t buflen ) { - (void)wolf_error_msg( error, buf, buflen ); - strncat( buf, " - ", buflen - strlen( buf ) ); - if( error == WOLF_ERR_INTERNAL ) { - strncat( buf, library->errbuf, buflen - strlen( buf ) ); - } - +char *wolf_library_error_msg( const wolf_library_p library, char *buf, size_t buflen ) { + strncpy( buf, library->errbuf, buflen ); return buf; } diff --git a/tests/library/test_loader.c b/tests/library/test_loader.c index bda32b3..75f968b 100644 --- a/tests/library/test_loader.c +++ b/tests/library/test_loader.c @@ -26,6 +26,7 @@ #else #define LIBRARY_NAME "./testlib.so.0.0.0" #endif +#define LIBRARY_FUNC "multiply_by_two2" int main( void ) { wolf_library_p library; @@ -39,16 +40,22 @@ int main( void ) { /* open the libray */ library = wolf_library_load( LIBRARY_NAME, &error ); if( error != WOLF_OK ) { - fprintf( stderr, "Error %d loading the library: %s\n", - error, wolf_library_errmsg( error, library, errbuf, 512 ) ); + fprintf( stderr, "Error %d (%s) loading library '%s':\n", + error, wolf_error_msg( error, errbuf, 512 ), LIBRARY_NAME ); + fprintf( stderr, "Internal loader error: %s\n", + wolf_library_error_msg( library, errbuf, 512 ) ); + (void)wolf_library_unload( library ); return EXIT_FAILURE; } /* fetch a known function symbol (multiply_by_two) */ - symbol = wolf_library_get_func( library, "multiply_by_two", &error ); + symbol = wolf_library_get_func( library, LIBRARY_FUNC, &error ); if( error != WOLF_OK ) { - fprintf( stderr, "Error %d fetching a function from the library: %s\n", - error, wolf_library_errmsg( error, library, errbuf, 512 ) ); + fprintf( stderr, "Error %d (%s) fetching function '%s' from the library\n", + error, wolf_error_msg( error, errbuf, 512 ), LIBRARY_FUNC ); + fprintf( stderr, "Internal loader error: %s\n", + wolf_library_error_msg( library, errbuf, 512 ) ); + (void)wolf_library_unload( library ); return EXIT_FAILURE; } WOLF_LIBRARY_FUNC_CAST( symbol, multiply_by_two_func, func ); @@ -60,8 +67,10 @@ int main( void ) { /* close library */ error = wolf_library_unload( library ); if( error != WOLF_OK ) { - fprintf( stderr, "Error %d unloading the library: %s\n", - error, wolf_library_errmsg( error, library, errbuf, 512 ) ); + fprintf( stderr, "Error %d (%s) unloading the library\n", + error, wolf_error_msg( error, errbuf, 512 ) ); + fprintf( stderr, "Internal loader error: %s\n", + wolf_library_error_msg( library, errbuf, 512 ) ); return EXIT_FAILURE; } -- cgit v1.2.3-54-g00ecf