]> git.ipfire.org Git - thirdparty/mdadm.git/commitdiff
Make cmap_* also has same policy as dlm_*
authorGuoqing Jiang <gqjiang@suse.com>
Mon, 19 Oct 2015 08:03:20 +0000 (16:03 +0800)
committerNeilBrown <neilb@suse.com>
Wed, 21 Oct 2015 00:19:35 +0000 (11:19 +1100)
Let libcmap lib and related funs also only need one-time
setup during mdadm running period.

Signed-off-by: Guoqing Jiang <gqjiang@suse.com>
Signed-off-by: NeilBrown <neilb@suse.com>
mdadm.c
mdadm.h
util.c

diff --git a/mdadm.c b/mdadm.c
index 8a758eab18d95cc2952b6696785c65d97d96a123..f56a8cf856e375fa8a905a057f7d12418ae055f4 100644 (file)
--- a/mdadm.c
+++ b/mdadm.c
@@ -1317,6 +1317,9 @@ int main(int argc, char *argv[])
        }
 
        rv = 0;
+
+       set_hooks(); /* set hooks from libs */
+
        if (c.homecluster == NULL && (c.nodes > 0)) {
                c.homecluster = conf_get_homecluster();
                if (c.homecluster == NULL)
@@ -1346,8 +1349,6 @@ int main(int argc, char *argv[])
                /* --scan implied --brief unless -vv */
                c.brief = 1;
 
-       set_dlm_hooks(); /* get dlm funcs from libdlm_lt.so.3 */
-
        switch(mode) {
        case MANAGE:
                /* readonly, add/remove, readwrite, runstop */
diff --git a/mdadm.h b/mdadm.h
index b95697df128ccb6f505d42b62587bab64773da01..1f3c4bd0f4f529fa387c3ef5ff5700794b923d44 100644 (file)
--- a/mdadm.h
+++ b/mdadm.h
@@ -52,6 +52,13 @@ extern __off64_t lseek64 __P ((int __fd, __off64_t __offset, int __whence));
 #define srandom srand
 #endif
 
+#ifdef NO_COROSYNC
+#define CS_OK 1
+typedef uint64_t cmap_handle_t;
+#else
+#include       <corosync/cmap.h>
+#endif
+
 #ifndef NO_DLM
 #include       <libdlm.h>
 #include       <errno.h>
@@ -1444,6 +1451,19 @@ extern char *fd2devnm(int fd);
 
 extern int in_initrd(void);
 
+struct cmap_hooks {
+       void *cmap_handle;      /* corosync lib related */
+
+       int (*initialize)(cmap_handle_t *handle);
+       int (*get_string)(cmap_handle_t handle,
+                         const char *string,
+                         char **name);
+       int (*finalize)(cmap_handle_t handle);
+};
+
+extern void set_cmap_hooks(void);
+extern void set_hooks(void);
+
 struct dlm_hooks {
        void *dlm_handle;       /* dlm lib related */
 
diff --git a/util.c b/util.c
index 1d2f1b39ab92300f51eb2d53921f4d8acc767f9a..8217e117fb546f6430a7b1b240619e79683f6589 100644 (file)
--- a/util.c
+++ b/util.c
 #include       <dirent.h>
 #include       <signal.h>
 #include       <dlfcn.h>
-#include       <stdint.h>
-#ifdef NO_COROSYNC
- typedef uint64_t cmap_handle_t;
- #define CS_OK 1
-#else
- #include      <corosync/cmap.h>
-#endif
 
 
 /*
@@ -2121,41 +2114,42 @@ void reopen_mddev(int mdfd)
        if (fd >= 0 && fd != mdfd)
                dup2(fd, mdfd);
 }
-#ifndef MDASSEMBLE
-int get_cluster_name(char **cluster_name)
-{
-        void *lib_handle = NULL;
-        int rv = -1;
 
-        cmap_handle_t handle;
-        static int (*initialize)(cmap_handle_t *handle);
-        static int (*get_string)(cmap_handle_t handle,
-                                const char *string,
-                                char **name);
-        static int (*finalize)(cmap_handle_t handle);
+static struct cmap_hooks *cmap_hooks = NULL;
+static int is_cmap_hooks_ready = 0;
 
+#ifndef MDASSEMBLE
+void set_cmap_hooks(void)
+{
+       cmap_hooks = xmalloc(sizeof(struct cmap_hooks));
+       cmap_hooks->cmap_handle = dlopen("libcmap.so.4", RTLD_NOW | RTLD_LOCAL);
+       if (!cmap_hooks->cmap_handle)
+               return;
 
-        lib_handle = dlopen("libcmap.so.4", RTLD_NOW | RTLD_LOCAL);
-        if (!lib_handle)
-                return rv;
+       cmap_hooks->initialize = dlsym(cmap_hooks->cmap_handle, "cmap_initialize");
+       cmap_hooks->get_string = dlsym(cmap_hooks->cmap_handle, "cmap_get_string");
+       cmap_hooks->finalize = dlsym(cmap_hooks->cmap_handle, "cmap_finalize");
 
-        initialize = dlsym(lib_handle, "cmap_initialize");
-        if (!initialize)
-                goto out;
+       if (!cmap_hooks->initialize || !cmap_hooks->get_string ||
+           !cmap_hooks->finalize)
+               dlclose(cmap_hooks->cmap_handle);
+       else
+               is_cmap_hooks_ready = 1;
+}
 
-        get_string = dlsym(lib_handle, "cmap_get_string");
-        if (!get_string)
-                goto out;
+int get_cluster_name(char **cluster_name)
+{
+        int rv = -1;
+       cmap_handle_t handle;
 
-        finalize = dlsym(lib_handle, "cmap_finalize");
-        if (!finalize)
-                goto out;
+       if (!is_cmap_hooks_ready)
+               return rv;
 
-        rv = initialize(&handle);
+        rv = cmap_hooks->initialize(&handle);
         if (rv != CS_OK)
                 goto out;
 
-        rv = get_string(handle, "totem.cluster_name", cluster_name);
+        rv = cmap_hooks->get_string(handle, "totem.cluster_name", cluster_name);
         if (rv != CS_OK) {
                 free(*cluster_name);
                 rv = -1;
@@ -2164,9 +2158,8 @@ int get_cluster_name(char **cluster_name)
 
         rv = 0;
 name_err:
-        finalize(handle);
+        cmap_hooks->finalize(handle);
 out:
-        dlclose(lib_handle);
         return rv;
 }
 
@@ -2191,4 +2184,10 @@ void set_dlm_hooks(void)
        else
                is_dlm_hooks_ready = 1;
 }
+
+void set_hooks(void)
+{
+       set_dlm_hooks();
+       set_cmap_hooks();
+}
 #endif