Blame SOURCES/Add-Client-ID-to-debug-messages.patch

1f3433
From 9e9b8597c09605438a6d880b6df5aaa4696f4b21 Mon Sep 17 00:00:00 2001
1f3433
From: Simo Sorce <simo@redhat.com>
1f3433
Date: Thu, 25 May 2017 15:22:37 -0400
1f3433
Subject: [PATCH] Add Client ID to debug messages
1f3433
1f3433
This allows to sort out which debug message belongs to which client when
1f3433
multiple clients are preforming operations at the same time.
1f3433
1f3433
Signed-off-by: Simo Sorce <simo@redhat.com>
1f3433
Reviewed-by: Robbie Harwood <rharwood@redhat.com>
1f3433
1f3433
Resolves: #189
1f3433
Merges: #191
1f3433
(cherry picked from commit 2f158fe4d39c11589d214d3d602c6d10411052dc)
1f3433
---
1f3433
 proxy/src/gp_debug.c   | 28 +++++++++++++++++++++++++++-
1f3433
 proxy/src/gp_debug.h   |  1 +
1f3433
 proxy/src/gp_proxy.h   |  1 +
1f3433
 proxy/src/gp_socket.c  |  5 +++++
1f3433
 proxy/src/gp_workers.c |  6 ++++++
1f3433
 proxy/src/gssproxy.c   |  4 ++++
1f3433
 6 files changed, 44 insertions(+), 1 deletion(-)
1f3433
1f3433
diff --git a/proxy/src/gp_debug.c b/proxy/src/gp_debug.c
1f3433
index 3029574..4a141fc 100644
1f3433
--- a/proxy/src/gp_debug.c
1f3433
+++ b/proxy/src/gp_debug.c
1f3433
@@ -64,6 +64,32 @@ const char *gp_debug_timestamp(void)
1f3433
     return buffer;
1f3433
 }
1f3433
 
1f3433
+/* thread local connection/client id */
1f3433
+static __thread int cid;
1f3433
+
1f3433
+void gp_debug_set_conn_id(int id)
1f3433
+{
1f3433
+    cid = id;
1f3433
+}
1f3433
+
1f3433
+static const char*gp_debug_conn_id(void)
1f3433
+{
1f3433
+    static __thread char buffer[18];
1f3433
+    static __thread int last_cid = 0;
1f3433
+
1f3433
+    if (cid == 0) {
1f3433
+        buffer[0] = '\0';
1f3433
+        return buffer;
1f3433
+    }
1f3433
+
1f3433
+    if (last_cid == cid) return buffer;
1f3433
+
1f3433
+    (void)snprintf(buffer, 17, "[CID %d]", cid);
1f3433
+    buffer[17] = '\0';
1f3433
+    last_cid = cid;
1f3433
+    return buffer;
1f3433
+}
1f3433
+
1f3433
 void gp_debug_printf(const char *format, ...)
1f3433
 {
1f3433
     va_list varargs;
1f3433
@@ -76,7 +102,7 @@ void gp_debug_time_printf(const char *format, ...)
1f3433
 {
1f3433
     va_list varargs;
1f3433
 
1f3433
-    fprintf(stderr, "%s", gp_debug_timestamp());
1f3433
+    fprintf(stderr, "%s%s", gp_debug_conn_id(), gp_debug_timestamp());
1f3433
 
1f3433
     va_start(varargs, format);
1f3433
     vfprintf(stderr, format, varargs);
1f3433
diff --git a/proxy/src/gp_debug.h b/proxy/src/gp_debug.h
1f3433
index d3420b0..1c2f8a3 100644
1f3433
--- a/proxy/src/gp_debug.h
1f3433
+++ b/proxy/src/gp_debug.h
1f3433
@@ -14,6 +14,7 @@ int gp_debug_args(int level);
1f3433
 void gp_debug_toggle(int);
1f3433
 void gp_debug_printf(const char *format, ...);
1f3433
 void gp_debug_time_printf(const char *format, ...);
1f3433
+void gp_debug_set_conn_id(int id);
1f3433
 
1f3433
 #define GPDEBUG(...) do { \
1f3433
     if (gp_debug) { \
1f3433
diff --git a/proxy/src/gp_proxy.h b/proxy/src/gp_proxy.h
1f3433
index 971a7b6..55ab83c 100644
1f3433
--- a/proxy/src/gp_proxy.h
1f3433
+++ b/proxy/src/gp_proxy.h
1f3433
@@ -113,6 +113,7 @@ void gp_socket_send_data(verto_ctx *vctx, struct gp_conn *conn,
1f3433
 struct gp_creds *gp_conn_get_creds(struct gp_conn *conn);
1f3433
 uid_t gp_conn_get_uid(struct gp_conn *conn);
1f3433
 const char *gp_conn_get_socket(struct gp_conn *conn);
1f3433
+int gp_conn_get_cid(struct gp_conn *conn);
1f3433
 bool gp_selinux_ctx_equal(SELINUX_CTX ctx1, SELINUX_CTX ctx2);
1f3433
 bool gp_conn_check_selinux(struct gp_conn *conn, SELINUX_CTX ctx);
1f3433
 
1f3433
diff --git a/proxy/src/gp_socket.c b/proxy/src/gp_socket.c
1f3433
index 29b6a44..5064e51 100644
1f3433
--- a/proxy/src/gp_socket.c
1f3433
+++ b/proxy/src/gp_socket.c
1f3433
@@ -103,6 +103,11 @@ const char *gp_conn_get_socket(struct gp_conn *conn)
1f3433
     return conn->sock_ctx->socket;
1f3433
 }
1f3433
 
1f3433
+int gp_conn_get_cid(struct gp_conn *conn)
1f3433
+{
1f3433
+    return conn->us.sd;
1f3433
+}
1f3433
+
1f3433
 void gp_conn_free(struct gp_conn *conn)
1f3433
 {
1f3433
     if (!conn) return;
1f3433
diff --git a/proxy/src/gp_workers.c b/proxy/src/gp_workers.c
1f3433
index c089b54..d37e57c 100644
1f3433
--- a/proxy/src/gp_workers.c
1f3433
+++ b/proxy/src/gp_workers.c
1f3433
@@ -357,6 +357,9 @@ static void *gp_worker_main(void *pvt)
1f3433
 
1f3433
     while (!t->pool->shutdown) {
1f3433
 
1f3433
+        /* initialize debug client id to 0 until work is scheduled */
1f3433
+        gp_debug_set_conn_id(0);
1f3433
+
1f3433
         /* ======> COND_MUTEX */
1f3433
         pthread_mutex_lock(&t->cond_mutex);
1f3433
         while (t->query == NULL) {
1f3433
@@ -374,6 +377,9 @@ static void *gp_worker_main(void *pvt)
1f3433
         /* <====== COND_MUTEX */
1f3433
         pthread_mutex_unlock(&t->cond_mutex);
1f3433
 
1f3433
+        /* set client id before hndling requests */
1f3433
+        gp_debug_set_conn_id(gp_conn_get_cid(q->conn));
1f3433
+
1f3433
         /* handle the client request */
1f3433
         gp_handle_query(t->pool, q);
1f3433
 
1f3433
diff --git a/proxy/src/gssproxy.c b/proxy/src/gssproxy.c
1f3433
index 5c5937d..94a6a61 100644
1f3433
--- a/proxy/src/gssproxy.c
1f3433
+++ b/proxy/src/gssproxy.c
1f3433
@@ -159,6 +159,10 @@ int main(int argc, const char *argv[])
1f3433
     int wait_fd;
1f3433
     int ret = -1;
1f3433
 
1f3433
+    /* initialize debug client id to 0 in the main thread */
1f3433
+    /* we do this early, before any code starts using debug statements */
1f3433
+    gp_debug_set_conn_id(0);
1f3433
+
1f3433
     struct poptOption long_options[] = {
1f3433
         POPT_AUTOHELP
1f3433
         {"daemon", 'D', POPT_ARG_NONE, &opt_daemon, 0, \