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

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