From 88136b53f59e3b81aacc28df18bda575da35b02d Mon Sep 17 00:00:00 2001 From: Vijay Bellur Date: Sun, 11 Jan 2015 23:13:08 +0530 Subject: libglusterfs: Avoid initializing per process globals more than once. gfapi consumers can invoke glusters_globals_init() multiple times through glfs_new(). This will result in re-initialization of already inited variables and non deterministic behavior. To avoid this, a new function gf_globals_init_once() has been added. The invocation of this function is guarded through pthread_once(), thereby ensuring single initialization of per process globals. Change-Id: I0ecde02ee49e0c7379c2eb0f1c879d89774ec82f BUG: 1184366 Signed-off-by: Vijay Bellur Signed-off-by: Raghavendra Bhat Reviewed-on: http://review.gluster.org/9430 Tested-by: Gluster Build System --- libglusterfs/src/globals.c | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) (limited to 'libglusterfs') diff --git a/libglusterfs/src/globals.c b/libglusterfs/src/globals.c index cf707c7af4b..57467ecde1d 100644 --- a/libglusterfs/src/globals.c +++ b/libglusterfs/src/globals.c @@ -82,6 +82,7 @@ static char global_uuid_buf[GF_UUID_BUF_SIZE]; static pthread_key_t lkowner_buf_key; static char global_lkowner_buf[GF_LKOWNER_BUF_SIZE]; static int gf_global_mem_acct_enable = 1; +static pthread_once_t globals_inited = PTHREAD_ONCE_INIT; int @@ -329,13 +330,11 @@ glusterfs_lkowner_buf_get () return buf; } -int -glusterfs_globals_init (glusterfs_ctx_t *ctx) +static void +gf_globals_init_once () { int ret = 0; - gf_log_globals_init (ctx); - ret = glusterfs_this_init (); if (ret) { gf_log ("", GF_LOG_CRITICAL, @@ -371,5 +370,26 @@ glusterfs_globals_init (glusterfs_ctx_t *ctx) goto out; } out: + + if (ret) { + gf_log ("", GF_LOG_CRITICAL, "Exiting as global " + "initialization failed"); + exit (ret); + } +} + +int +glusterfs_globals_init (glusterfs_ctx_t *ctx) +{ + int ret = 0; + + gf_log_globals_init (ctx); + + ret = pthread_once (&globals_inited, gf_globals_init_once); + + if (ret) + gf_log ("", GF_LOG_CRITICAL, "pthread_once failed with: %d", + ret); + return ret; } -- cgit