X-Git-Url: https://git.octo.it/?p=rrdtool.git;a=blobdiff_plain;f=src%2Frrd_thread_safe.c;fp=src%2Frrd_thread_safe.c;h=6e30115bf7ea61a30a66435015f33adacf7269b3;hp=c910b988227c5968db5b59f92570c2b04f796c75;hb=7002f39fe85f0d540dd97659f18ca5347bef90f1;hpb=adced9c3ee51a50a65f6ea255a6017dff856a78b diff --git a/src/rrd_thread_safe.c b/src/rrd_thread_safe.c index c910b98..6e30115 100644 --- a/src/rrd_thread_safe.c +++ b/src/rrd_thread_safe.c @@ -59,11 +59,38 @@ const char *rrd_strerror( int err) { rrd_context_t *ctx = rrd_get_context(); + char *ret = "unknown error"; - if (strerror_r(err, ctx->lib_errstr, sizeof(ctx->lib_errstr))) - 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 */ +#if 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