Blame SOURCES/Use-pthread-keys-for-thread-local-storage.patch

4bd34d
From e0b142320342ef16260b6072f1c83d6fcf4142e6 Mon Sep 17 00:00:00 2001
4bd34d
From: Simo Sorce <simo@redhat.com>
4bd34d
Date: Thu, 20 Sep 2018 17:37:53 -0400
4bd34d
Subject: [PATCH] Use pthread keys for thread local storage
4bd34d
4bd34d
This interface is slower but also more portable, and more importantly
4bd34d
it provides a way to specify destructor that is called when a thread
4bd34d
is canceled so we stop leaking memory.
4bd34d
4bd34d
Signed-off-by: Simo Sorce <simo@redhat.com>
4bd34d
Reviewed-by: Robbie Harwood <rharwood@redhat.com>
4bd34d
Merges: #233
4bd34d
(cherry picked from commit 0faccc1441bc7a6b3e8bd806f22c8a961e5f586e)
4bd34d
(cherry picked from commit 89dc0ee157caa4617d32fd72287849296d7fe26d)
4bd34d
---
4bd34d
 src/client/gpm_common.c         |  2 ++
4bd34d
 src/client/gpm_display_status.c | 57 ++++++++++++++++++++++-----------
4bd34d
 src/client/gssapi_gpm.h         |  1 +
4bd34d
 3 files changed, 42 insertions(+), 18 deletions(-)
4bd34d
4bd34d
diff --git a/src/client/gpm_common.c b/src/client/gpm_common.c
4bd34d
index dd29519..c254280 100644
4bd34d
--- a/src/client/gpm_common.c
4bd34d
+++ b/src/client/gpm_common.c
4bd34d
@@ -55,6 +55,8 @@ static void gpm_init_once(void)
4bd34d
     gpm_global_ctx.next_xid = rand_r(&seedp);
4bd34d
 
4bd34d
     pthread_mutexattr_destroy(&attr);
4bd34d
+
4bd34d
+    gpm_display_status_init_once();
4bd34d
 }
4bd34d
 
4bd34d
 static int get_pipe_name(char *name)
4bd34d
diff --git a/src/client/gpm_display_status.c b/src/client/gpm_display_status.c
4bd34d
index bbb546f..e3aa4ea 100644
4bd34d
--- a/src/client/gpm_display_status.c
4bd34d
+++ b/src/client/gpm_display_status.c
4bd34d
@@ -1,27 +1,47 @@
4bd34d
 /* Copyright (C) 2011 the GSS-PROXY contributors, see COPYING for license */
4bd34d
 
4bd34d
 #include "gssapi_gpm.h"
4bd34d
+#include <pthread.h>
4bd34d
 
4bd34d
-__thread gssx_status *tls_last_status = NULL;
4bd34d
+static pthread_key_t gpm_last_status;
4bd34d
 
4bd34d
-/* Thread local storage for return status.
4bd34d
- * FIXME: it's not the most portable construct, so may need fixing in future */
4bd34d
+static void gpm_destroy_last_status(void *arg)
4bd34d
+{
4bd34d
+    gssx_status *status = (gssx_status *)arg;
4bd34d
+    xdr_free((xdrproc_t)xdr_gssx_status, (char *)status);
4bd34d
+    free(status);
4bd34d
+}
4bd34d
+
4bd34d
+void gpm_display_status_init_once(void)
4bd34d
+{
4bd34d
+    (void)pthread_key_create(&gpm_last_status, gpm_destroy_last_status);
4bd34d
+}
4bd34d
+
4bd34d
+/* Portable thread local storage for return status. */
4bd34d
 void gpm_save_status(gssx_status *status)
4bd34d
 {
4bd34d
+    gssx_status *last_status;
4bd34d
     int ret;
4bd34d
 
4bd34d
-    if (tls_last_status) {
4bd34d
-        xdr_free((xdrproc_t)xdr_gssx_status, (char *)tls_last_status);
4bd34d
-        free(tls_last_status);
4bd34d
+    last_status = (gssx_status *)pthread_getspecific(gpm_last_status);
4bd34d
+    if (last_status != NULL) {
4bd34d
+        /* store NULL first so we do not risk a double free if we are
4bd34d
+         * racing on a pthread_cancel */
4bd34d
+        pthread_setspecific(gpm_last_status, NULL);
4bd34d
+        gpm_destroy_last_status(last_status);
4bd34d
     }
4bd34d
 
4bd34d
-    ret = gp_copy_gssx_status_alloc(status, &tls_last_status);
4bd34d
-    if (ret) {
4bd34d
-        /* make sure tls_last_status is zeored on error */
4bd34d
-        tls_last_status = NULL;
4bd34d
+    ret = gp_copy_gssx_status_alloc(status, &last_status);
4bd34d
+    if (ret == 0) {
4bd34d
+        pthread_setspecific(gpm_last_status, last_status);
4bd34d
     }
4bd34d
 }
4bd34d
 
4bd34d
+gssx_status *gpm_get_saved_status(void)
4bd34d
+{
4bd34d
+    return (gssx_status *)pthread_getspecific(gpm_last_status);
4bd34d
+}
4bd34d
+
4bd34d
 /* This funciton is used to record internal mech errors that are
4bd34d
  * generated by the proxy client code */
4bd34d
 void gpm_save_internal_status(uint32_t err, char *err_str)
4bd34d
@@ -47,15 +67,16 @@ OM_uint32 gpm_display_status(OM_uint32 *minor_status,
4bd34d
                              OM_uint32 *message_context,
4bd34d
                              gss_buffer_t status_string)
4bd34d
 {
4bd34d
+    gssx_status *last_status = gpm_get_saved_status();
4bd34d
     utf8string tmp;
4bd34d
     int ret;
4bd34d
 
4bd34d
     switch(status_type) {
4bd34d
     case GSS_C_GSS_CODE:
4bd34d
-        if (tls_last_status &&
4bd34d
-            tls_last_status->major_status == status_value &&
4bd34d
-            tls_last_status->major_status_string.utf8string_len) {
4bd34d
-                ret = gp_copy_utf8string(&tls_last_status->major_status_string,
4bd34d
+        if (last_status &&
4bd34d
+            last_status->major_status == status_value &&
4bd34d
+            last_status->major_status_string.utf8string_len) {
4bd34d
+                ret = gp_copy_utf8string(&last_status->major_status_string,
4bd34d
                                          &tmp);
4bd34d
                 if (ret) {
4bd34d
                     *minor_status = ret;
4bd34d
@@ -70,9 +91,9 @@ OM_uint32 gpm_display_status(OM_uint32 *minor_status,
4bd34d
             return GSS_S_UNAVAILABLE;
4bd34d
         }
4bd34d
     case GSS_C_MECH_CODE:
4bd34d
-        if (tls_last_status &&
4bd34d
-            tls_last_status->minor_status == status_value &&
4bd34d
-            tls_last_status->minor_status_string.utf8string_len) {
4bd34d
+        if (last_status &&
4bd34d
+            last_status->minor_status == status_value &&
4bd34d
+            last_status->minor_status_string.utf8string_len) {
4bd34d
 
4bd34d
             if (*message_context) {
4bd34d
                 /* we do not support multiple messages for now */
4bd34d
@@ -80,7 +101,7 @@ OM_uint32 gpm_display_status(OM_uint32 *minor_status,
4bd34d
                 return GSS_S_FAILURE;
4bd34d
             }
4bd34d
 
4bd34d
-            ret = gp_copy_utf8string(&tls_last_status->minor_status_string,
4bd34d
+            ret = gp_copy_utf8string(&last_status->minor_status_string,
4bd34d
                                      &tmp);
4bd34d
             if (ret) {
4bd34d
                 *minor_status = ret;
4bd34d
diff --git a/src/client/gssapi_gpm.h b/src/client/gssapi_gpm.h
4bd34d
index 22beecf..61124e0 100644
4bd34d
--- a/src/client/gssapi_gpm.h
4bd34d
+++ b/src/client/gssapi_gpm.h
4bd34d
@@ -23,6 +23,7 @@ OM_uint32 gpm_release_name(OM_uint32 *minor_status,
4bd34d
 OM_uint32 gpm_release_buffer(OM_uint32 *minor_status,
4bd34d
                              gss_buffer_t buffer);
4bd34d
 
4bd34d
+void gpm_display_status_init_once(void);
4bd34d
 void gpm_save_status(gssx_status *status);
4bd34d
 void gpm_save_internal_status(uint32_t err, char *err_str);
4bd34d