From 4f4f59f3330d7022cdc7a758176887d46714d458 Mon Sep 17 00:00:00 2001 From: Kurt Zeilenga Date: Thu, 6 Jul 2000 01:24:52 +0000 Subject: [PATCH] Rework to provide a STRERROR. Prefers errlist over strerror() as strerror() may not be thread safe. Likely should be used by SLAPD. --- include/ac/errno.h | 24 ++++++++++++++++++------ libraries/liblber/sockbuf.c | 4 ++-- libraries/libldap/os-local.c | 2 +- libraries/libldbm/ldbm.c | 36 ++++++++++-------------------------- 4 files changed, 31 insertions(+), 35 deletions(-) diff --git a/include/ac/errno.h b/include/ac/errno.h index 7e6abae9c0..ec9169911f 100644 --- a/include/ac/errno.h +++ b/include/ac/errno.h @@ -25,15 +25,27 @@ # define sys_errlist ((char **)0) #elif DECL_SYS_ERRLIST /* have sys_errlist but need declaration */ - LDAP_LIBC_V (int) sys_nerr; - LDAP_LIBC_V (char) *sys_errlist[]; + LDAP_LIBC_V(int) sys_nerr; + LDAP_LIBC_V(char) *sys_errlist[]; #endif -#ifdef HAVE_STRERROR -#define STRERROR(err) strerror(err) +#undef _AC_ERRNO_UNKNOWN +#define _AC_ERRNO_UNKNOWN "unknown error" + +#ifdef HAVE_SYS_ERRLIST + /* this is thread safe */ +# define STRERROR(e) ( (e) > -1 && (e) < sys_nerr \ + ? sys_errlist[(e)] : _AC_ERRNO_UNKNOWN ) + +#elif defined( HAVE_STRERROR ) + /* this may not be thread safe */ + /* and, yes, some implementations of strerror may return NULL */ +# define STRERROR(e) ( strerror(e) \ + ? strerror(e) : _AC_ERRNO_UNKNOWN ) + #else -#define STRERROR(err) \ - ((err) > -1 && (err) < sys_nerr ? sys_errlist[(err)] : "unknown") + /* this is thread safe */ +# define STRERROR(e) ( _AC_ERRNO_UNKNOWN ) #endif #endif /* _AC_ERRNO_H */ diff --git a/libraries/liblber/sockbuf.c b/libraries/liblber/sockbuf.c index e3ddbcd44a..5c133e6540 100644 --- a/libraries/liblber/sockbuf.c +++ b/libraries/liblber/sockbuf.c @@ -914,7 +914,7 @@ sb_debug_read( Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len ) if ( ret < 0 ) { ber_log_printf( LDAP_DEBUG_PACKETS, sbiod->sbiod_sb->sb_debug, "%sread: want=%ld error=%s\n", (char *)sbiod->sbiod_pvt, - (long)len, strerror( errno ) ); + (long)len, STRERROR( errno ) ); } else { ber_log_printf( LDAP_DEBUG_PACKETS, sbiod->sbiod_sb->sb_debug, @@ -936,7 +936,7 @@ sb_debug_write( Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len ) ber_log_printf( LDAP_DEBUG_PACKETS, sbiod->sbiod_sb->sb_debug, "%swrite: want=%ld error=%s\n", (char *)sbiod->sbiod_pvt, (long)len, - strerror( errno ) ); + STRERROR( errno ) ); } else { ber_log_printf( LDAP_DEBUG_PACKETS, sbiod->sbiod_sb->sb_debug, diff --git a/libraries/libldap/os-local.c b/libraries/libldap/os-local.c index a2cd834308..89d4278e18 100644 --- a/libraries/libldap/os-local.c +++ b/libraries/libldap/os-local.c @@ -83,7 +83,7 @@ ldap_pvt_close_socket(LDAP *ld, int s) "ldap_is_socket_ready: errror on socket %d: errno: %d (%s)\n", \ s, \ errno, \ - strerror(errno) ); \ + STRERROR(errno) ); \ } while( 0 ) /* diff --git a/libraries/libldbm/ldbm.c b/libraries/libldbm/ldbm.c index 2560fd8727..3e6825ac0a 100644 --- a/libraries/libldbm/ldbm.c +++ b/libraries/libldbm/ldbm.c @@ -135,11 +135,8 @@ int ldbm_initialize( void ) if ( err ) { char error[BUFSIZ]; - if ( err < 0 ) { - sprintf( error, "%ld\n", (long) err ); - } else { - sprintf( error, "%s\n", strerror( err )); - } + sprintf( error, "%s (%d)\n", STRERROR( err ), err ); + #ifdef LDAP_SYSLOG syslog( LOG_INFO, "ldbm_initialize(): FATAL error in db_appinit() : %s\n", @@ -158,11 +155,8 @@ int ldbm_initialize( void ) { char error[BUFSIZ]; - if ( err < 0 ) { - sprintf( error, "%ld\n", (long) err ); - } else { - sprintf( error, "%s\n", strerror( err )); - } + sprintf( error, "%s (%d)\n", STRERROR( err ), err ); + #ifdef LDAP_SYSLOG syslog( LOG_INFO, "ldbm_initialize(): FATAL error in db_appinit() : %s\n", @@ -233,11 +227,8 @@ ldbm_open( char *name, int rw, int mode, int dbcachesize ) { char error[BUFSIZ]; - if ( err < 0 ) { - sprintf( error, "%ld\n", (long) err ); - } else { - sprintf( error, "%s\n", strerror( err )); - } + sprintf( error, "%s (%d)\n", STRERROR( err ), err ); + (void)ret->close(ret, 0); return NULL; } @@ -251,11 +242,8 @@ ldbm_open( char *name, int rw, int mode, int dbcachesize ) { char error[BUFSIZ]; - if ( err < 0 ) { - sprintf( error, "%ld\n", (long) err ); - } else { - sprintf( error, "%s\n", strerror( err )); - } + sprintf( error, "%s (%d)\n", STRERROR( err ), err ); + (void)ret->close(ret, 0); return NULL; } @@ -383,13 +371,9 @@ ldbm_store( LDBM ldbm, Datum key, Datum data, int flags ) { char error[BUFSIZ]; - if ( rc < 0 ) { - sprintf( error, "%ld\n", (long) rc ); - } else { - sprintf( error, "%s\n", strerror( rc )); - } + sprintf( error, "%s (%d)\n", STRERROR( rc ), rc ); } - rc = (-1) * rc; + rc = (-1) * rc; #elif DB_VERSION_MAJOR >= 2 rc = (*ldbm->put)( ldbm, NULL, &key, &data, flags & ~LDBM_SYNC ); -- 2.39.5