ia64/xen-unstable

changeset 12889:c519ab0f70f3

[LIBXC] Make strerror() thread-safe by protecting it with a mutex.
Using strerror_r() is made difficult by the different GNU definition,
and different distros variously choose the POSIX or GNU prototype.
Signed-off-by: Keir Fraser <keir@xensource.com>
author kfraser@localhost.localdomain
date Fri Dec 08 15:59:30 2006 +0000 (2006-12-08)
parents a4d1f99e5a53
children 7258a2009cfa
files tools/libxc/Makefile tools/libxc/xc_private.c
line diff
     1.1 --- a/tools/libxc/Makefile	Fri Dec 08 13:31:21 2006 +0000
     1.2 +++ b/tools/libxc/Makefile	Fri Dec 08 15:59:30 2006 +0000
     1.3 @@ -119,7 +119,7 @@ libxenctrl.so.$(MAJOR): libxenctrl.so.$(
     1.4  	ln -sf $< $@
     1.5  
     1.6  libxenctrl.so.$(MAJOR).$(MINOR): $(CTRL_PIC_OBJS)
     1.7 -	$(CC) $(CFLAGS) $(LDFLAGS) -Wl,$(SONAME_LDFLAG) -Wl,libxenctrl.so.$(MAJOR) $(SHLIB_CFLAGS) -o $@ $^
     1.8 +	$(CC) $(CFLAGS) $(LDFLAGS) -Wl,$(SONAME_LDFLAG) -Wl,libxenctrl.so.$(MAJOR) $(SHLIB_CFLAGS) -o $@ $^ -lpthread
     1.9  
    1.10  # libxenguest
    1.11  
    1.12 @@ -132,7 +132,7 @@ libxenguest.so.$(MAJOR): libxenguest.so.
    1.13  	ln -sf $< $@
    1.14  
    1.15  libxenguest.so.$(MAJOR).$(MINOR): $(GUEST_PIC_OBJS) libxenctrl.so
    1.16 -	$(CC) $(CFLAGS) $(LDFLAGS) -Wl,$(SONAME_LDFLAG) -Wl,libxenguest.so.$(MAJOR) $(SHLIB_CFLAGS) -o $@ $(GUEST_PIC_OBJS) -lz -lxenctrl
    1.17 +	$(CC) $(CFLAGS) $(LDFLAGS) -Wl,$(SONAME_LDFLAG) -Wl,libxenguest.so.$(MAJOR) $(SHLIB_CFLAGS) -o $@ $(GUEST_PIC_OBJS) -lz -lxenctrl -lpthread
    1.18  
    1.19  -include $(DEPS)
    1.20  
     2.1 --- a/tools/libxc/xc_private.c	Fri Dec 08 13:31:21 2006 +0000
     2.2 +++ b/tools/libxc/xc_private.c	Fri Dec 08 15:59:30 2006 +0000
     2.3 @@ -7,8 +7,8 @@
     2.4  #include <inttypes.h>
     2.5  #include "xc_private.h"
     2.6  #include "xg_private.h"
     2.7 -
     2.8  #include <stdarg.h>
     2.9 +#include <pthread.h>
    2.10  
    2.11  static __thread xc_error last_error = { XC_ERROR_NONE, ""};
    2.12  #if DEBUG
    2.13 @@ -486,14 +486,20 @@ unsigned long xc_make_page_below_4G(
    2.14  char *safe_strerror(int errcode)
    2.15  {
    2.16      static __thread char errbuf[32];
    2.17 -#ifdef __GLIBC__
    2.18 -    /* Broken GNU definition of strerror_r may not use our supplied buffer. */
    2.19 -    return strerror_r(errcode, errbuf, sizeof(errbuf));
    2.20 -#else
    2.21 -    /* Assume we have the POSIX definition of strerror_r. */
    2.22 -    strerror_r(errcode, errbuf, sizeof(errbuf));
    2.23 +    static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
    2.24 +    char *strerror_str;
    2.25 +
    2.26 +    /*
    2.27 +     * Thread-unsafe strerror() is protected by a local mutex. We copy
    2.28 +     * the string to a thread-private buffer before releasing the mutex.
    2.29 +     */
    2.30 +    pthread_mutex_lock(&mutex);
    2.31 +    strerror_str = strerror(errcode);
    2.32 +    strncpy(errbuf, strerror_str, sizeof(errbuf));
    2.33 +    errbuf[sizeof(errbuf)-1] = '\0';
    2.34 +    pthread_mutex_unlock(&mutex);
    2.35 +
    2.36      return errbuf;
    2.37 -#endif
    2.38  }
    2.39  
    2.40  /*