Cache list of plugin names to further simplify its usage.
authorTobias Brunner <tobias@strongswan.org>
Thu, 19 Jan 2012 11:27:56 +0000 (12:27 +0100)
committerTobias Brunner <tobias@strongswan.org>
Thu, 19 Jan 2012 11:37:42 +0000 (12:37 +0100)
Also helpful for ipsec statusall to avoid having to enumerate plugins.

src/libcharon/daemon.c
src/libcharon/plugins/stroke/stroke_list.c
src/libstrongswan/plugins/plugin_loader.c
src/libstrongswan/plugins/plugin_loader.h
src/pki/command.c
src/pluto/log.c
src/pluto/plutomain.c
src/scepclient/scepclient.c

index f175826..3fb49d4 100644 (file)
@@ -201,18 +201,6 @@ METHOD(daemon_t, start, void,
                                                                                           DEFAULT_THREADS));
 }
 
-/**
- * Log loaded plugins
- */
-static void print_plugins()
-{
-       char *plugins;
-
-       plugins = lib->plugins->loaded_plugins(lib->plugins);
-       DBG1(DBG_DMN, "loaded plugins: %s", plugins);
-       free(plugins);
-}
-
 METHOD(daemon_t, initialize, bool,
        private_daemon_t *this)
 {
@@ -233,8 +221,8 @@ METHOD(daemon_t, initialize, bool,
        {
                return FALSE;
        }
-
-       print_plugins();
+       DBG1(DBG_DMN, "loaded plugins: %s",
+                lib->plugins->loaded_plugins(lib->plugins));
 
        this->public.ike_sa_manager = ike_sa_manager_create();
        if (this->public.ike_sa_manager == NULL)
index 8125d37..c056dce 100644 (file)
@@ -414,8 +414,7 @@ METHOD(stroke_list_t, status, void,
        if (all)
        {
                peer_cfg_t *peer_cfg;
-               plugin_t *plugin;
-               char *pool, *plugins;
+               char *pool;
                host_t *host;
                u_int32_t dpd;
                time_t since, now;
@@ -449,9 +448,8 @@ METHOD(stroke_list_t, status, void,
                }
                fprintf(out, ", scheduled: %d\n",
                                lib->scheduler->get_job_load(lib->scheduler));
-               plugins = lib->plugins->loaded_plugins(lib->plugins);
-               fprintf(out, "  loaded plugins: %s\n", plugins);
-               free(plugins);
+               fprintf(out, "  loaded plugins: %s\n",
+                               lib->plugins->loaded_plugins(lib->plugins));
 
                first = TRUE;
                enumerator = this->attribute->create_pool_enumerator(this->attribute);
index 1747cba..164b68e 100644 (file)
@@ -45,6 +45,11 @@ struct private_plugin_loader_t {
         * List of plugins, as plugin_entry_t
         */
        linked_list_t *plugins;
+
+       /**
+        * List of names of loaded plugins
+        */
+       char *loaded_plugins;
 };
 
 /**
@@ -204,6 +209,38 @@ METHOD(plugin_loader_t, create_plugin_enumerator, enumerator_t*,
 }
 
 /**
+ * Create a list of the names of all loaded plugins
+ */
+static char* loaded_plugins_list(private_plugin_loader_t *this)
+{
+       int buf_len = 128, len = 0;
+       char *buf, *name;
+       enumerator_t *enumerator;
+       plugin_t *plugin;
+
+       buf = malloc(buf_len);
+       buf[0] = '\0';
+       enumerator = create_plugin_enumerator(this);
+       while (enumerator->enumerate(enumerator, &plugin, NULL))
+       {
+               name = plugin->get_name(plugin);
+               if (len + (strlen(name) + 1) >= buf_len)
+               {
+                       buf_len <<= 1;
+                       buf = realloc(buf, buf_len);
+               }
+               len += snprintf(&buf[len], buf_len - len, "%s ", name);
+       }
+       enumerator->destroy(enumerator);
+       if (len > 0 && buf[len - 1] == ' ')
+       {
+               buf[len - 1] = '\0';
+       }
+       return buf;
+}
+
+
+/**
  * Check if a plugin is already loaded
  */
 static bool plugin_loaded(private_plugin_loader_t *this, char *name)
@@ -516,6 +553,11 @@ METHOD(plugin_loader_t, load_plugins, bool,
                /* unload plugins that we were not able to load any features for */
                purge_plugins(this);
        }
+       if (!critical_failed)
+       {
+               free(this->loaded_plugins);
+               this->loaded_plugins = loaded_plugins_list(this);
+       }
        return !critical_failed;
 }
 
@@ -558,6 +600,8 @@ METHOD(plugin_loader_t, unload, void,
                }
                enumerator->destroy(enumerator);
        }
+       free(this->loaded_plugins);
+       this->loaded_plugins = NULL;
 }
 
 /**
@@ -609,25 +653,7 @@ METHOD(plugin_loader_t, reload, u_int,
 METHOD(plugin_loader_t, loaded_plugins, char*,
        private_plugin_loader_t *this)
 {
-#define BUF_LEN 512
-       char *buf = malloc(BUF_LEN);
-       int len = 0;
-       enumerator_t *enumerator;
-       plugin_t *plugin;
-
-       buf[0] = '\0';
-       enumerator = create_plugin_enumerator(this);
-       while (len < BUF_LEN && enumerator->enumerate(enumerator, &plugin, NULL))
-       {
-               len += snprintf(&buf[len], BUF_LEN - len, "%s ",
-                                               plugin->get_name(plugin));
-       }
-       enumerator->destroy(enumerator);
-       if (len > 0 && buf[len - 1] == ' ')
-       {
-               buf[len - 1] = '\0';
-       }
-       return buf;
+       return this->loaded_plugins ?: "";
 }
 
 METHOD(plugin_loader_t, destroy, void,
@@ -635,6 +661,7 @@ METHOD(plugin_loader_t, destroy, void,
 {
        unload(this);
        this->plugins->destroy(this->plugins);
+       free(this->loaded_plugins);
        free(this);
 }
 
index 0f677c2..7fd0704 100644 (file)
@@ -71,7 +71,9 @@ struct plugin_loader_t {
        /**
         * Get a simple list the names of all loaded plugins.
         *
-        * @return                              list of the names of all loaded plugins (allocated)
+        * The function returns internal data, do not free.
+        *
+        * @return                              list of the names of all loaded plugins
         */
        char* (*loaded_plugins)(plugin_loader_t *this);
 
index e74072f..07ba5bb 100644 (file)
@@ -179,9 +179,8 @@ int command_usage(char *error)
 
        if (active == help_idx)
        {
-               char *plugins = lib->plugins->loaded_plugins(lib->plugins);
-               fprintf(out, "loaded plugins: %s\n", plugins);
-               free(plugins);
+               fprintf(out, "loaded plugins: %s\n",
+                               lib->plugins->loaded_plugins(lib->plugins));
        }
 
        fprintf(out, "usage:\n");
index 91bc57e..f6fa226 100644 (file)
@@ -868,11 +868,8 @@ DBG_dump(const char *label, const void *p, size_t len)
 
 static void show_loaded_plugins()
 {
-       char *plugins;
-
-       plugins = lib->plugins->loaded_plugins(lib->plugins);
-       whack_log(RC_COMMENT, "loaded plugins: %s", plugins);
-       free(plugins);
+       whack_log(RC_COMMENT, "loaded plugins: %s",
+                         lib->plugins->loaded_plugins(lib->plugins));
 }
 
 void show_status(bool all, const char *name)
index 904ed82..dbc857c 100644 (file)
@@ -264,18 +264,6 @@ static const char *pkcs11_init_args = NULL;
 /* options read by optionsfrom */
 options_t *options;
 
-/**
- * Log loaded plugins
- */
-static void print_plugins()
-{
-       char *plugins;
-
-       plugins = lib->plugins->loaded_plugins(lib->plugins);
-       DBG1(DBG_DMN, "loaded plugins: %s", plugins);
-       free(plugins);
-}
-
 int main(int argc, char **argv)
 {
        bool fork_desired = TRUE;
@@ -693,7 +681,8 @@ int main(int argc, char **argv)
        {
                exit(SS_RC_INITIALIZATION_FAILED);
        }
-       print_plugins();
+       DBG1(DBG_DMN, "loaded plugins: %s",
+                lib->plugins->loaded_plugins(lib->plugins));
 
        init_builder();
        if (!init_secret() || !init_crypto())
index efe13c2..0b54eee 100644 (file)
@@ -276,18 +276,6 @@ usage(const char *message)
 }
 
 /**
- * Log loaded plugins
- */
-static void print_plugins()
-{
-       char *plugins;
-
-       plugins = lib->plugins->loaded_plugins(lib->plugins);
-       DBG1(DBG_LIB, "  loaded plugins: %s", plugins);
-       free(plugins);
-}
-
-/**
  * @brief main of scepclient
  *
  * @param argc number of arguments
@@ -752,7 +740,8 @@ int main(int argc, char **argv)
        {
                exit_scepclient("plugin loading failed");
        }
-       print_plugins();
+       DBG1(DBG_LIB, "  loaded plugins: %s",
+                lib->plugins->loaded_plugins(lib->plugins));
 
        if ((filetype_out == 0) && (!request_ca_certificate))
        {