887953
From 919597d141dd79b34a9c0ef9e52a63cc43320d6c Mon Sep 17 00:00:00 2001
887953
From: Mohit Agrawal <moagrawa@redhat.com>
887953
Date: Mon, 3 Dec 2018 17:05:19 +0530
887953
Subject: [PATCH 453/453] server: Resolve memory leak path in server_init
887953
887953
Problem: 1) server_init does not cleanup allocate resources
887953
            while it is failed before return error
887953
         2) dict leak at the time of graph destroying
887953
887953
Solution: 1) free resources in case of server_init is failed
887953
          2) Take dict_ref of graph xlator before destroying
887953
             the graph to avoid leak
887953
887953
> Change-Id: I9e31e156b9ed6bebe622745a8be0e470774e3d15
887953
> fixes: bz#1654917
887953
> Cherry pick from commit 46c15ea8fa98bb3d92580b192f03863c2e2a2d9c
887953
> Reviewed on upstream link https://review.gluster.org/#/c/glusterfs/+/21750/
887953
887953
Change-Id: I5ba1b37840bcaa4a7fa4c05822c84016a2d89ea2
887953
BUG: 1650138
887953
Signed-off-by: Mohit Agrawal <moagrawa@redhat.com>
887953
Reviewed-on: https://code.engineering.redhat.com/gerrit/157445
887953
Tested-by: RHGS Build Bot <nigelb@redhat.com>
887953
Reviewed-by: Pranith Kumar Karampuri <pkarampu@redhat.com>
887953
Reviewed-by: Raghavendra Gowdappa <rgowdapp@redhat.com>
887953
Reviewed-by: Sunil Kumar Heggodu Gopala Acharya <sheggodu@redhat.com>
887953
---
887953
 glusterfsd/src/glusterfsd.c                |  4 +++
887953
 libglusterfs/src/xlator.c                  | 31 ++++++++++++++++++
887953
 libglusterfs/src/xlator.h                  |  3 ++
887953
 rpc/rpc-lib/src/rpc-transport.c            | 26 ++++++++++++----
887953
 rpc/rpc-lib/src/rpc-transport.h            |  3 ++
887953
 rpc/rpc-lib/src/rpcsvc.c                   | 40 ++++++++++++++++++++++++
887953
 rpc/rpc-lib/src/rpcsvc.h                   |  3 ++
887953
 xlators/mgmt/glusterd/src/glusterd-utils.c | 33 ++------------------
887953
 xlators/protocol/server/src/server.c       | 50 +++++++++++++++++++++++++++---
887953
 9 files changed, 151 insertions(+), 42 deletions(-)
887953
887953
diff --git a/glusterfsd/src/glusterfsd.c b/glusterfsd/src/glusterfsd.c
887953
index 2e43cdb..78f3719 100644
887953
--- a/glusterfsd/src/glusterfsd.c
887953
+++ b/glusterfsd/src/glusterfsd.c
887953
@@ -2411,6 +2411,10 @@ out:
887953
                         xl = graph->first;
887953
                         if ((ctx && (ctx->active != graph)) &&
887953
                             (xl && !strcmp(xl->type, "protocol/server"))) {
887953
+                                /* Take dict ref for every graph xlator to avoid dict leak
887953
+                                   at the time of graph destroying
887953
+                                */
887953
+                                gluster_graph_take_reference(graph->first);
887953
                                 glusterfs_graph_fini(graph);
887953
                                 glusterfs_graph_destroy(graph);
887953
                         }
887953
diff --git a/libglusterfs/src/xlator.c b/libglusterfs/src/xlator.c
887953
index 8aa8aa1..340d83d 100644
887953
--- a/libglusterfs/src/xlator.c
887953
+++ b/libglusterfs/src/xlator.c
887953
@@ -1225,3 +1225,34 @@ glusterfs_delete_volfile_checksum (glusterfs_ctx_t *ctx,
887953
 
887953
         return 0;
887953
 }
887953
+
887953
+/*
887953
+   The function is required to take dict ref for every xlator at graph.
887953
+   At the time of compare graph topology create a graph and populate
887953
+   key values in the dictionary, after finished graph comparison we do destroy
887953
+   the new graph.At the time of construct graph we don't take any reference
887953
+   so to avoid dict leak at the of destroying graph due to ref counter underflow
887953
+   we need to call dict_ref here.
887953
+
887953
+*/
887953
+
887953
+void
887953
+gluster_graph_take_reference(xlator_t *tree)
887953
+{
887953
+    xlator_t *trav = tree;
887953
+    xlator_t *prev = tree;
887953
+
887953
+    if (!tree) {
887953
+        gf_msg("parser", GF_LOG_ERROR, 0, LG_MSG_TREE_NOT_FOUND,
887953
+               "Translator tree not found");
887953
+        return;
887953
+    }
887953
+
887953
+    while (prev) {
887953
+        trav = prev->next;
887953
+        if (prev->options)
887953
+            dict_ref(prev->options);
887953
+        prev = trav;
887953
+    }
887953
+    return;
887953
+}
887953
diff --git a/libglusterfs/src/xlator.h b/libglusterfs/src/xlator.h
887953
index 1879641..f8f2630 100644
887953
--- a/libglusterfs/src/xlator.h
887953
+++ b/libglusterfs/src/xlator.h
887953
@@ -1087,4 +1087,7 @@ glusterfs_delete_volfile_checksum (glusterfs_ctx_t *ctx,
887953
                                    const char *volfile_id);
887953
 int
887953
 xlator_memrec_free (xlator_t *xl);
887953
+
887953
+void
887953
+gluster_graph_take_reference(xlator_t *tree);
887953
 #endif /* _XLATOR_H */
887953
diff --git a/rpc/rpc-lib/src/rpc-transport.c b/rpc/rpc-lib/src/rpc-transport.c
887953
index 94880f4..77abf96 100644
887953
--- a/rpc/rpc-lib/src/rpc-transport.c
887953
+++ b/rpc/rpc-lib/src/rpc-transport.c
887953
@@ -160,6 +160,25 @@ out:
887953
         return msg;
887953
 }
887953
 
887953
+void
887953
+rpc_transport_cleanup(rpc_transport_t *trans)
887953
+{
887953
+    if (!trans)
887953
+        return;
887953
+
887953
+    if (trans->fini)
887953
+        trans->fini(trans);
887953
+
887953
+    GF_FREE(trans->name);
887953
+
887953
+    if (trans->xl)
887953
+        pthread_mutex_destroy(&trans->lock);
887953
+
887953
+    if (trans->dl_handle)
887953
+        dlclose(trans->dl_handle);
887953
+
887953
+    GF_FREE(trans);
887953
+}
887953
 
887953
 
887953
 rpc_transport_t *
887953
@@ -361,12 +380,7 @@ rpc_transport_load (glusterfs_ctx_t *ctx, dict_t *options, char *trans_name)
887953
 
887953
 fail:
887953
         if (trans) {
887953
-                GF_FREE (trans->name);
887953
-
887953
-                if (trans->dl_handle)
887953
-                        dlclose (trans->dl_handle);
887953
-
887953
-                GF_FREE (trans);
887953
+               rpc_transport_cleanup(trans);
887953
         }
887953
 
887953
         GF_FREE (name);
887953
diff --git a/rpc/rpc-lib/src/rpc-transport.h b/rpc/rpc-lib/src/rpc-transport.h
887953
index 33f474e..23246c5 100644
887953
--- a/rpc/rpc-lib/src/rpc-transport.h
887953
+++ b/rpc/rpc-lib/src/rpc-transport.h
887953
@@ -316,4 +316,7 @@ rpc_transport_unix_options_build (dict_t **options, char *filepath,
887953
 
887953
 int
887953
 rpc_transport_inet_options_build (dict_t **options, const char *hostname, int port);
887953
+
887953
+void
887953
+rpc_transport_cleanup(rpc_transport_t *);
887953
 #endif /* __RPC_TRANSPORT_H__ */
887953
diff --git a/rpc/rpc-lib/src/rpcsvc.c b/rpc/rpc-lib/src/rpcsvc.c
887953
index 8d0c409..695e9fb 100644
887953
--- a/rpc/rpc-lib/src/rpcsvc.c
887953
+++ b/rpc/rpc-lib/src/rpcsvc.c
887953
@@ -36,6 +36,7 @@
887953
 #include <fnmatch.h>
887953
 #include <stdarg.h>
887953
 #include <stdio.h>
887953
+#include <dlfcn.h>
887953
 
887953
 #include "xdr-rpcclnt.h"
887953
 #include "glusterfs-acl.h"
887953
@@ -1677,6 +1678,7 @@ rpcsvc_create_listener (rpcsvc_t *svc, dict_t *options, char *name)
887953
 
887953
         listener = rpcsvc_listener_alloc (svc, trans);
887953
         if (listener == NULL) {
887953
+                ret = -1;
887953
                 goto out;
887953
         }
887953
 
887953
@@ -1684,6 +1686,7 @@ rpcsvc_create_listener (rpcsvc_t *svc, dict_t *options, char *name)
887953
 out:
887953
         if (!listener && trans) {
887953
                 rpc_transport_disconnect (trans, _gf_true);
887953
+                rpc_transport_cleanup(trans);
887953
         }
887953
 
887953
         return ret;
887953
@@ -2285,6 +2288,43 @@ rpcsvc_get_throttle (rpcsvc_t *svc)
887953
         return svc->throttle;
887953
 }
887953
 
887953
+/* Function call to cleanup resources for svc
887953
+ */
887953
+int
887953
+rpcsvc_destroy(rpcsvc_t *svc)
887953
+{
887953
+    struct rpcsvc_auth_list *auth = NULL;
887953
+    struct rpcsvc_auth_list *tmp = NULL;
887953
+    rpcsvc_listener_t *listener = NULL;
887953
+    rpcsvc_listener_t *next = NULL;
887953
+    int ret = 0;
887953
+
887953
+    if (!svc)
887953
+        return ret;
887953
+
887953
+    list_for_each_entry_safe(listener, next, &svc->listeners, list)
887953
+    {
887953
+        rpcsvc_listener_destroy(listener);
887953
+    }
887953
+
887953
+    list_for_each_entry_safe(auth, tmp, &svc->authschemes, authlist)
887953
+    {
887953
+        list_del_init(&auth->authlist);
887953
+        GF_FREE(auth);
887953
+    }
887953
+
887953
+    rpcsvc_program_unregister(svc, &gluster_dump_prog);
887953
+    if (svc->rxpool) {
887953
+        mem_pool_destroy(svc->rxpool);
887953
+        svc->rxpool = NULL;
887953
+    }
887953
+
887953
+    pthread_mutex_destroy(&svc->rpclock);
887953
+    GF_FREE(svc);
887953
+
887953
+    return ret;
887953
+}
887953
+
887953
 /* The global RPC service initializer.
887953
  */
887953
 rpcsvc_t *
887953
diff --git a/rpc/rpc-lib/src/rpcsvc.h b/rpc/rpc-lib/src/rpcsvc.h
887953
index 34429b4..d3aafac 100644
887953
--- a/rpc/rpc-lib/src/rpcsvc.h
887953
+++ b/rpc/rpc-lib/src/rpcsvc.h
887953
@@ -610,4 +610,7 @@ rpcsvc_auth_array (rpcsvc_t *svc, char *volname, int *autharr, int arrlen);
887953
 rpcsvc_vector_sizer
887953
 rpcsvc_get_program_vector_sizer (rpcsvc_t *svc, uint32_t prognum,
887953
                                  uint32_t progver, int procnum);
887953
+
887953
+extern int
887953
+rpcsvc_destroy(rpcsvc_t *svc);
887953
 #endif
887953
diff --git a/xlators/mgmt/glusterd/src/glusterd-utils.c b/xlators/mgmt/glusterd/src/glusterd-utils.c
887953
index ec7e27a..b63c95a 100644
887953
--- a/xlators/mgmt/glusterd/src/glusterd-utils.c
887953
+++ b/xlators/mgmt/glusterd/src/glusterd-utils.c
887953
@@ -9384,35 +9384,6 @@ glusterd_defrag_volume_status_update (glusterd_volinfo_t *volinfo,
887953
 
887953
         return ret;
887953
 }
887953
-/*
887953
-   The function is required to take dict ref for every xlator at graph.
887953
-   At the time of compare graph topology create a graph and populate
887953
-   key values in the dictionary, after finished graph comparison we do destroy
887953
-   the new graph.At the time of construct graph we don't take any reference
887953
-   so to avoid leak due to ref counter underflow we need to call dict_ref here.
887953
-
887953
-*/
887953
-
887953
-void
887953
-glusterd_graph_take_reference (xlator_t *tree)
887953
-{        xlator_t *trav = tree;
887953
-         xlator_t *prev = tree;
887953
-
887953
-        if (!tree) {
887953
-                gf_msg ("parser", GF_LOG_ERROR, 0, LG_MSG_TREE_NOT_FOUND,
887953
-                        "Translator tree not found");
887953
-                return;
887953
-        }
887953
-
887953
-        while (prev) {
887953
-                trav = prev->next;
887953
-                if (prev->options)
887953
-                        dict_ref (prev->options);
887953
-                prev = trav;
887953
-        }
887953
-        return;
887953
-}
887953
-
887953
 
887953
 
887953
 int
887953
@@ -9461,14 +9432,14 @@ glusterd_check_topology_identical (const char   *filename1,
887953
         if (grph1 == NULL)
887953
                 goto out;
887953
 
887953
-        glusterd_graph_take_reference (grph1->first);
887953
+        gluster_graph_take_reference (grph1->first);
887953
 
887953
         /* create the graph for filename2 */
887953
         grph2 = glusterfs_graph_construct(fp2);
887953
         if (grph2 == NULL)
887953
                 goto out;
887953
 
887953
-        glusterd_graph_take_reference (grph2->first);
887953
+        gluster_graph_take_reference (grph2->first);
887953
 
887953
         /* compare the graph topology */
887953
         *identical = is_graph_topology_equal(grph1, grph2);
887953
diff --git a/xlators/protocol/server/src/server.c b/xlators/protocol/server/src/server.c
887953
index 65d712f..6f510ea 100644
887953
--- a/xlators/protocol/server/src/server.c
887953
+++ b/xlators/protocol/server/src/server.c
887953
@@ -1144,12 +1144,54 @@ client_destroy_cbk (xlator_t *this, client_t *client)
887953
         return 0;
887953
 }
887953
 
887953
+void
887953
+server_cleanup(xlator_t *this, server_conf_t *conf)
887953
+{
887953
+    if (!this || !conf)
887953
+        return;
887953
+
887953
+    LOCK_DESTROY(&conf->itable_lock);
887953
+    pthread_mutex_destroy(&conf->mutex);
887953
+
887953
+    if (this->ctx->event_pool) {
887953
+        /* Free the event pool */
887953
+        (void)event_pool_destroy(this->ctx->event_pool);
887953
+    }
887953
+
887953
+    if (dict_get(this->options, "config-directory")) {
887953
+        GF_FREE(conf->conf_dir);
887953
+        conf->conf_dir = NULL;
887953
+    }
887953
+
887953
+    if (conf->child_status) {
887953
+        GF_FREE(conf->child_status);
887953
+        conf->child_status = NULL;
887953
+    }
887953
+
887953
+    if (this->ctx->statedump_path) {
887953
+        GF_FREE(this->ctx->statedump_path);
887953
+        this->ctx->statedump_path = NULL;
887953
+    }
887953
+
887953
+    if (conf->auth_modules) {
887953
+        gf_auth_fini(conf->auth_modules);
887953
+        dict_unref(conf->auth_modules);
887953
+    }
887953
+
887953
+    if (conf->rpc) {
887953
+        (void)rpcsvc_destroy(conf->rpc);
887953
+        conf->rpc = NULL;
887953
+    }
887953
+
887953
+    GF_FREE(conf);
887953
+    this->private = NULL;
887953
+}
887953
+
887953
 int
887953
 init (xlator_t *this)
887953
 {
887953
         int32_t            ret      = -1;
887953
         server_conf_t     *conf     = NULL;
887953
-        rpcsvc_listener_t *listener = NULL;
887953
         char              *transport_type = NULL;
887953
         char              *statedump_path = NULL;
887953
         int               total_transport = 0;
887953
@@ -1226,6 +1268,7 @@ init (xlator_t *this)
887953
         ret = gf_auth_init (this, conf->auth_modules);
887953
         if (ret) {
887953
                 dict_unref (conf->auth_modules);
887953
+                conf->auth_modules = NULL;
887953
                 goto out;
887953
         }
887953
 
887953
@@ -1378,10 +1421,7 @@ out:
887953
                 if (this != NULL) {
887953
                         this->fini (this);
887953
                 }
887953
-
887953
-                if (listener != NULL) {
887953
-                        rpcsvc_listener_destroy (listener);
887953
-                }
887953
+                server_cleanup(this, conf);
887953
         }
887953
 
887953
         return ret;
887953
-- 
887953
1.8.3.1
887953