diff options
Diffstat (limited to 'src/port')
-rw-r--r-- | src/port/string.c | 17 |
1 files changed, 14 insertions, 3 deletions
diff --git a/src/port/string.c b/src/port/string.c index da0dfce..83b1daa 100644 --- a/src/port/string.c +++ b/src/port/string.c @@ -44,16 +44,25 @@ char *wolf_port_strdup( const char *s ) { #if !defined HAVE_STRERROR_R || defined TEST_STRERROR_R #include "port/stdio.h" /* for snprintf */ +#include "port/stdbool.h" /* for bool */ #include <sys/types.h> /* for size_t */ #include <errno.h> /* for errno */ +#include "mutex.h" /* for mutexes */ + +static bool mutex_initialized = false; +static wolf_mutex_t mutex; -/* FIXME: this version is NOT thread and signal safe! But we have to port - * mutexes first - */ int wolf_port_strerror_r( int num, char *buf, size_t buflen ) { int safe_errno = errno; const char *msg; + /* critical section as strerror is not thread-safe */ + if( !mutex_initialized ) { + wolf_mutex_init( &mutex ); + mutex_initialized = true; + } + wolf_mutex_lock( &mutex ); + msg = strerror( num ); /* TODO: can we detect illegal error numbers? For sure on Linux NULL * is never returned and a strcmp with prefix "Unknown error" or similar @@ -64,11 +73,13 @@ int wolf_port_strerror_r( int num, char *buf, size_t buflen ) { /* Linux returns an empty string in this case, why? */ (void)snprintf( buf, buflen, "Unknown error %d", num ); errno = EINVAL; + wolf_mutex_unlock( &mutex ); return -1; } strncpy( buf, msg, buflen-1 ); errno = safe_errno; + wolf_mutex_unlock( &mutex ); return 0; } |