[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Xen-changelog] [xen-unstable] [LIBXC] Make strerror() thread-safe by protecting it with a mutex.



# HG changeset patch
# User kfraser@xxxxxxxxxxxxxxxxxxxxx
# Node ID c519ab0f70f3be3128b1940027a4ad3774df4c58
# Parent  a4d1f99e5a534d31501ecad91ae1de53d391b4a1
[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@xxxxxxxxxxxxx>
---
 tools/libxc/Makefile     |    4 ++--
 tools/libxc/xc_private.c |   22 ++++++++++++++--------
 2 files changed, 16 insertions(+), 10 deletions(-)

diff -r a4d1f99e5a53 -r c519ab0f70f3 tools/libxc/Makefile
--- a/tools/libxc/Makefile      Fri Dec 08 13:31:21 2006 +0000
+++ b/tools/libxc/Makefile      Fri Dec 08 15:59:30 2006 +0000
@@ -119,7 +119,7 @@ libxenctrl.so.$(MAJOR): libxenctrl.so.$(
        ln -sf $< $@
 
 libxenctrl.so.$(MAJOR).$(MINOR): $(CTRL_PIC_OBJS)
-       $(CC) $(CFLAGS) $(LDFLAGS) -Wl,$(SONAME_LDFLAG) 
-Wl,libxenctrl.so.$(MAJOR) $(SHLIB_CFLAGS) -o $@ $^
+       $(CC) $(CFLAGS) $(LDFLAGS) -Wl,$(SONAME_LDFLAG) 
-Wl,libxenctrl.so.$(MAJOR) $(SHLIB_CFLAGS) -o $@ $^ -lpthread
 
 # libxenguest
 
@@ -132,7 +132,7 @@ libxenguest.so.$(MAJOR): libxenguest.so.
        ln -sf $< $@
 
 libxenguest.so.$(MAJOR).$(MINOR): $(GUEST_PIC_OBJS) libxenctrl.so
-       $(CC) $(CFLAGS) $(LDFLAGS) -Wl,$(SONAME_LDFLAG) 
-Wl,libxenguest.so.$(MAJOR) $(SHLIB_CFLAGS) -o $@ $(GUEST_PIC_OBJS) -lz 
-lxenctrl
+       $(CC) $(CFLAGS) $(LDFLAGS) -Wl,$(SONAME_LDFLAG) 
-Wl,libxenguest.so.$(MAJOR) $(SHLIB_CFLAGS) -o $@ $(GUEST_PIC_OBJS) -lz 
-lxenctrl -lpthread
 
 -include $(DEPS)
 
diff -r a4d1f99e5a53 -r c519ab0f70f3 tools/libxc/xc_private.c
--- a/tools/libxc/xc_private.c  Fri Dec 08 13:31:21 2006 +0000
+++ b/tools/libxc/xc_private.c  Fri Dec 08 15:59:30 2006 +0000
@@ -7,8 +7,8 @@
 #include <inttypes.h>
 #include "xc_private.h"
 #include "xg_private.h"
-
 #include <stdarg.h>
+#include <pthread.h>
 
 static __thread xc_error last_error = { XC_ERROR_NONE, ""};
 #if DEBUG
@@ -486,14 +486,20 @@ char *safe_strerror(int errcode)
 char *safe_strerror(int errcode)
 {
     static __thread char errbuf[32];
-#ifdef __GLIBC__
-    /* Broken GNU definition of strerror_r may not use our supplied buffer. */
-    return strerror_r(errcode, errbuf, sizeof(errbuf));
-#else
-    /* Assume we have the POSIX definition of strerror_r. */
-    strerror_r(errcode, errbuf, sizeof(errbuf));
+    static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
+    char *strerror_str;
+
+    /*
+     * Thread-unsafe strerror() is protected by a local mutex. We copy
+     * the string to a thread-private buffer before releasing the mutex.
+     */
+    pthread_mutex_lock(&mutex);
+    strerror_str = strerror(errcode);
+    strncpy(errbuf, strerror_str, sizeof(errbuf));
+    errbuf[sizeof(errbuf)-1] = '\0';
+    pthread_mutex_unlock(&mutex);
+
     return errbuf;
-#endif
 }
 
 /*

_______________________________________________
Xen-changelog mailing list
Xen-changelog@xxxxxxxxxxxxxxxxxxx
http://lists.xensource.com/xen-changelog


 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.