libxl: fix mutex initialization
PATCH 1/1 libxl: fix mutex initialization
# HG changeset patch
# User Roger Pau Monne <[email protected]> # Date 1326460029 -3600
# Node ID bb6b01995d64f7c2887b7174536861b83ef04364 # Parent 117ad4634ba11c10e45f3d86e6baff35d7d2691c
libxl: fix mutex initialization
The macro PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP is not defined on NetBSD, so define mutex attributes manually.
tools/libxl/libxl.c +4 -5
@@ -41,7 +41,6 @@ int libxl_ctx_alloc(libxl_ctx **pctx, in {libxl_ctx *ctx;struct stat stat_buf; -const pthread_mutex_t mutex_value =PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP ;if (version !=LIBXL_VERSION )return ERROR_VERSION ;
@@ -55,10 +54,10 @@ int libxl_ctx_alloc(libxl_ctx **pctx, inmemset (ctx,0 ,sizeof (libxl_ctx)); ctx->lg = lg; -/* This somewhat convoluted approach is needed because -* PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP is defined to be valid -* only as an initialiser, not as an expression. */ -memcpy (&ctx->lock, &mutex_value,sizeof (ctx->lock)); +if (libxl__init_recursive_mutex (ctx, &ctx->lock) <0 ) { +LIBXL__LOG (ctx,LIBXL__LOG_ERROR ,"Failed to initialize mutex" ); +return ERROR_FAIL ; + }if (stat (XENSTORE_PID_FILE , &stat_buf) !=0 ) { LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "Is xenstore daemon running?\n"
tools/libxl/libxl_internal.c +22 -0
@@ -304,6 +304,28 @@ _hidden int libxl__compare_macs(libxl_mareturn 0 ; } +_hidden int libxl__init_recursive_mutex(libxl_ctx *ctx, pthread_mutex_t *lock) +{ +pthread_mutexattr_t attr; + +if (pthread_mutexattr_init (&attr) !=0 ) { +LIBXL__LOG_ERRNO (ctx,LIBXL__LOG_ERROR , +"Failed to init mutex attributes\n" ); +return ERROR_FAIL ; + } +if (pthread_mutexattr_settype (&attr,PTHREAD_MUTEX_RECURSIVE ) !=0 ) { +LIBXL__LOG_ERRNO (ctx,LIBXL__LOG_ERROR , +"Failed to set mutex attributes\n" ); +return ERROR_FAIL ; + } +if (pthread_mutex_init (lock, &attr) !=0 ) { +LIBXL__LOG_ERRNO (ctx,LIBXL__LOG_ERROR , +"Failed to init mutex\n" ); +return ERROR_FAIL ; + } +return 0 ; +} +libxl_device_model_version libxl__device_model_version_running (libxl__gc *gc,uint32_t domid) {
tools/libxl/libxl_internal.h +2 -0
@@ -606,6 +606,8 @@ _hidden int libxl__e820_alloc(libxl__gc _hidden int libxl__parse_mac(const char *s, libxl_mac mac);/* compare mac address @a and @b. 0 if the same, -ve if a<b and +ve if a>b */ _hidden int libxl__compare_macs(libxl_mac *a, libxl_mac *b); +/* init a recursive mutex */ +_hidden int libxl__init_recursive_mutex(libxl_ctx *ctx, pthread_mutex_t *lock);#define STRINGIFY (x) #x#define TOSTRING (x) STRINGIFY(x)
Quoted 11 lines · Roger Pau Monne
# HG changeset patch
# User Roger Pau Monne<[email protected]> # Date 1326460029 -3600
# Node IDbb6b01995d64f7c2887b7174536861b83ef04364# Parent117ad4634ba11c10e45f3d86e6baff35d7d2691c
libxl: fix mutex initializationThe macro
PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NPis not defined on NetBSD, so define mutex attributes manually.Signed-off-by Roger Pau Monne<[email protected]>
Quoted 77 lines
diff -r
117ad4634ba1-rbb6b01995d64tools/libxl/libxl.ctools/libxl/libxl.c +4
-5@@ -41,7 +41,6 @@ int libxl_ctx_alloc(libxl_ctx **pctx, in {libxl_ctx *ctx;struct stat stat_buf; -const pthread_mutex_t mutex_value =PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP ;if (version !=LIBXL_VERSION )return ERROR_VERSION ;@@ -55,10 +54,10 @@ int libxl_ctx_alloc(libxl_ctx **pctx, inmemset (ctx,0 ,sizeof (libxl_ctx)); ctx->lg = lg; -/* This somewhat convoluted approach is needed because -* PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP is defined to be valid -* only as an initialiser, not as an expression. */ -memcpy (&ctx->lock,&mutex_value,sizeof (ctx->lock)); +if (libxl__init_recursive_mutex (ctx,&ctx->lock)<0 ) { +LIBXL__LOG (ctx,LIBXL__LOG_ERROR ,"Failed to initialize mutex" ); +return ERROR_FAIL ; + }if (stat (XENSTORE_PID_FILE ,&stat_buf) !=0 ) { LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "Is xenstore daemon running?\n"diff -r
117ad4634ba1-rbb6b01995d64tools/libxl/libxl_internal.ctools/libxl/libxl_internal.c +22
-0@@ -304,6 +304,28 @@ _hidden int libxl__compare_macs(libxl_mareturn 0 ; } +_hidden int libxl__init_recursive_mutex(libxl_ctx *ctx, pthread_mutex_t *lock) +{ +pthread_mutexattr_t attr; + +if (pthread_mutexattr_init (&attr) !=0 ) { +LIBXL__LOG_ERRNO (ctx,LIBXL__LOG_ERROR , +"Failed to init mutex attributes\n" ); +return ERROR_FAIL ; + } +if (pthread_mutexattr_settype (&attr,PTHREAD_MUTEX_RECURSIVE ) !=0 ) { +LIBXL__LOG_ERRNO (ctx,LIBXL__LOG_ERROR , +"Failed to set mutex attributes\n" ); +return ERROR_FAIL ; + } +if (pthread_mutex_init (lock,&attr) !=0 ) { +LIBXL__LOG_ERRNO (ctx,LIBXL__LOG_ERROR , +"Failed to init mutex\n" ); +return ERROR_FAIL ; + } +return 0 ; +} +libxl_device_model_version libxl__device_model_version_running (libxl__gc *gc,uint32_t domid) {diff -r
117ad4634ba1-rbb6b01995d64tools/libxl/libxl_internal.htools/libxl/libxl_internal.h +2
-0@@ -606,6 +606,8 @@ _hidden int libxl__e820_alloc(libxl__gc _hidden int libxl__parse_mac(const char *s, libxl_mac mac);/* compare mac address @a and @b. 0 if the same, -ve if a<b and +ve if a>b */ _hidden int libxl__compare_macs(libxl_mac *a, libxl_mac *b); +/* init a recursive mutex */ +_hidden int libxl__init_recursive_mutex(libxl_ctx *ctx, pthread_mutex_t *lock);#define STRINGIFY (x) #x#define TOSTRING (x) STRINGIFY(x)_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel
Signature · 6 lines
---to satisfy European Law for business letters: Advanced Micro Devices GmbH Einsteinring 24, 85689 Dornach b. Muenchen Geschaeftsfuehrer: Alberto Bozzo, Andrew Bowd Sitz: Dornach, Gemeinde Aschheim, Landkreis Muenchen Registergericht Muenchen, HRB Nr. 43632
Roger Pau Monne writes ("[Xen-devel] [PATCH] libxl: fix mutex initialization"):
Quoted 8 lines
tools/libxl/libxl_internal.c +8
-0+pthread_mutexattr_t attr; + +if (pthread_mutexattr_init (&attr) !=0 ) { +LIBXL__LOG_ERRNO (ctx,LIBXL__LOG_ERROR , +"Failed to init mutex attributes\n" ); +return ERROR_FAIL ; + } +if (pthread_mutexattr_settype (&attr,PTHREAD_MUTEX_RECURSIVE ) !=0 ) {
tools/libxl/libxl_internal.c _hidden int libxl__compare_macs(libxl_ma · line 326
Quoted 10 lines
tools/libxl/libxl_internal.c +10
-0+LIBXL__LOG_ERRNO (ctx,LIBXL__LOG_ERROR , +"Failed to set mutex attributes\n" ); +return ERROR_FAIL ; + } +if (pthread_mutex_init (lock, &attr) !=0 ) { +LIBXL__LOG_ERRNO (ctx,LIBXL__LOG_ERROR , +"Failed to init mutex\n" ); +return ERROR_FAIL ; + } +return 0 ;
This leaks the contents of attr. You need to call pthread_mutexattr_destroy on all the exit paths (except the one where _init failed).
What a horrible API for passing in some trivial flags!
Ian.