X-Git-Url: https://git.octo.it/?p=rrdtool.git;a=blobdiff_plain;f=src%2Frrd_thread_safe.c;h=94af58b4a3586344024abc65fb9679e4ca87d177;hp=e61cd4e2a3c6a90579273158580ce502a791f9b9;hb=19f031713115921bebf5949ce63926d66dd8c6a5;hpb=3d068765c6b6c8d096e0692f22e5b5e407948b54 diff --git a/src/rrd_thread_safe.c b/src/rrd_thread_safe.c index e61cd4e..94af58b 100644 --- a/src/rrd_thread_safe.c +++ b/src/rrd_thread_safe.c @@ -1,5 +1,5 @@ /***************************************************************************** - * RRDtool 1.3rc2 Copyright by Tobi Oetiker, 1997-2008 + * RRDtool 1.4.3 Copyright by Tobi Oetiker, 1997-2010 * This file: Copyright 2003 Peter Stamfest * & Tobias Oetiker * Distributed under the GPL @@ -27,7 +27,7 @@ static pthread_once_t context_key_once = PTHREAD_ONCE_INIT; static void context_destroy_context( void *ctx_) { - struct rrd_context *ctx = ctx_; + rrd_context_t *ctx = ctx_; if (ctx) rrd_free_context(ctx); @@ -40,10 +40,10 @@ static void context_get_key( pthread_key_create(&context_key, context_destroy_context); } -struct rrd_context *rrd_get_context( +rrd_context_t *rrd_get_context( void) { - struct rrd_context *ctx; + rrd_context_t *ctx; pthread_once(&context_key_once, context_get_key); ctx = pthread_getspecific(context_key); @@ -58,12 +58,39 @@ struct rrd_context *rrd_get_context( const char *rrd_strerror( int err) { - struct rrd_context *ctx = rrd_get_context(); + rrd_context_t *ctx = rrd_get_context(); + char *ret = "unknown error"; - if (strerror_r(err, ctx->lib_errstr, ctx->errlen)) - return "strerror_r failed. sorry!"; - else - return ctx->lib_errstr; + *ctx->lib_errstr = '\0'; + + /* Even though POSIX/XSI requires "strerror_r" to return an "int", some + * systems (e.g. the GNU libc) return a "char *" _and_ ignore the second + * argument ... -tokkee */ +#ifdef STRERROR_R_CHAR_P + ret = strerror_r(err, ctx->lib_errstr, sizeof(ctx->lib_errstr)); + if ((! ret) || (*ret == '\0')) { + if (*ctx->lib_errstr != '\0') + ret = ctx->lib_errstr; + else { + /* according to the manpage this should not happen - + let's handle it somehow sanely anyway */ + snprintf(ctx->lib_errstr, sizeof(ctx->lib_errstr), + "unknown error %i - strerror_r did not return anything", + err); + ctx->lib_errstr[sizeof(ctx->lib_errstr) - 1] = '\0'; + ret = ctx->lib_errstr; + } + } +#else /* ! STRERROR_R_CHAR_P */ + if (strerror_r(err, ctx->lib_errstr, sizeof(ctx->lib_errstr))) { + snprintf(ctx->lib_errstr, sizeof(ctx->lib_errstr), + "unknown error %i - strerror_r returned with errno = %i", + err, errno); + ctx->lib_errstr[sizeof(ctx->lib_errstr) - 1] = '\0'; + } + ret = ctx->lib_errstr; +#endif + return ret; } #else #undef strerror @@ -71,12 +98,12 @@ const char *rrd_strerror( int err) { static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER; - struct rrd_context *ctx; + rrd_context_t *ctx; ctx = rrd_get_context(); pthread_mutex_lock(&mtx); - strncpy(ctx->lib_errstr, strerror(err), ctx->errlen); - ctx->lib_errstr[ctx->errlen] = '\0'; + strncpy(ctx->lib_errstr, strerror(err), sizeof(ctx->lib_errstr)); + ctx->lib_errstr[sizeof(ctx->lib_errstr) - 1] = '\0'; pthread_mutex_unlock(&mtx); return ctx->lib_errstr; }