[Olsr-cvs] olsrd-current/src plugin_loader.c, 1.26, 1.27 plugin_loader.h, 1.16, 1.17 olsrd_plugin.h, 1.2, 1.3

Bernd Petrovitsch (spam-protected)
Sun Jul 15 21:29:39 CEST 2007


Update of /cvsroot/olsrd/olsrd-current/src
In directory sc8-pr-cvs3.sourceforge.net:/tmp/cvs-serv5004/src

Modified Files:
	plugin_loader.c plugin_loader.h olsrd_plugin.h 
Log Message:

I started to clean up the plugin parameter handling:
* Up to now some used case-insensitive, some used case-sensitive (with
  differing cases BTW) parameter names.
* Most plugins silently ignored unknown parameters.
This makes it hard to find errors in the olsrd.conf file.
Instead of simply fixing all the various plugins (and the mostly - more
or less - copied code), I reduced the plugin special code to a minimum
and (more or less automatically) all plugins behave the same (with
respect to the parameter handling).

How does it look now?
Every plugins exports a table of { parameter-name, parse-function,
addr-of-storage } to allow to share more code, e.g. the parsing and
checking of a port number or IP addresses.
Every plugin will export a function
----  snip  ----
void olsrd_get_plugin_parameters(const struct olsrd_plugin_parameters **params, int *size);
----  snip  ----
which delivers the address of the table and it's size. So in theory the
plugin could generate the table at load time though ATM all current
ones export statically defined tables.

What else is different?
- I introduced SUPPORT_OLD_PLUGIN_VERSIONS to simplifiy the compile-time
  removal of legacy support. It is not that much but more of a start.
- Plugin interface version 4 is supported until all plugins are
  migrated.
- The plugin loader produces now much more output - on good and error
  cases. I had too often to look into the source to find that I mistyped
  some parameter .....

ToDo:
- Several plugins cannot handle IPv6 at all - only IPv4 is implemented.
  Some of these functions are locally now but fixed versions can (and
  should IMHO) be shared by all plugins.



Index: plugin_loader.h
===================================================================
RCS file: /cvsroot/olsrd/olsrd-current/src/plugin_loader.h,v
retrieving revision 1.16
retrieving revision 1.17
diff -C2 -d -r1.16 -r1.17
*** plugin_loader.h	4 Jul 2007 01:52:35 -0000	1.16
--- plugin_loader.h	15 Jul 2007 19:29:37 -0000	1.17
***************
*** 43,48 ****
  #define _OLSR_PLUGIN_LOADER
  
! #include <dlfcn.h>
! #include <stdio.h>
  #include "olsr_types.h"
  #include "olsr_cfg.h"
--- 43,47 ----
  #define _OLSR_PLUGIN_LOADER
  
! #include "olsrd_plugin.h"
  #include "olsr_types.h"
  #include "olsr_cfg.h"
***************
*** 50,77 ****
  #ifndef OLSR_PLUGIN
  
  
! struct olsr_plugin
! {
!   /* The handle */
!   void *dlhandle;
    
!   struct plugin_param *params;
!   int plugin_interface_version;
    
!   int (*register_param)(char *, char *);
!   int (*plugin_init)(void);
    
!   struct olsr_plugin *next;
  };
  
! int
! olsr_load_plugins(void);
  
! void
! olsr_close_plugins(void);
  
! int
! olsr_plugin_io(int, void *, size_t);
  
  #endif
  #endif
--- 49,100 ----
  #ifndef OLSR_PLUGIN
  
+ /* all */
+ typedef int (*plugin_init_func)(void);
+ typedef int (*get_interface_version_func)(void);
  
! #if SUPPORT_OLD_PLUGIN_VERSIONS
! /* version 4 */
! typedef int (*register_param_func)(char *, char *);
! #endif
! 
! /* version 5 */
! typedef void (*get_plugin_parameters_func)(const struct olsrd_plugin_parameters **params, unsigned int *size);
! 
! 
! struct olsr_plugin {
!     /* The handle */
!     void *dlhandle;
    
!     struct plugin_param *params;
!     int plugin_interface_version;
    
! #if SUPPORT_OLD_PLUGIN_VERSIONS
!     /* version 4 */
!     register_param_func register_param;
! #endif
!     plugin_init_func plugin_init;
! 
!     /* version 5 */
!     const struct olsrd_plugin_parameters *plugin_parameters;
!     unsigned int plugin_parameters_size;
    
!     struct olsr_plugin *next;
  };
  
! void olsr_load_plugins(void);
  
! void olsr_close_plugins(void);
  
! int olsr_plugin_io(int, void *, size_t);
  
  #endif
  #endif
+ 
+ /*
+  * Local Variables:
+  * mode: c
+  * style: linux
+  * c-basic-offset: 4
+  * indent-tabs-mode: nil
+  * End:
+  */

Index: olsrd_plugin.h
===================================================================
RCS file: /cvsroot/olsrd/olsrd-current/src/olsrd_plugin.h,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -d -r1.2 -r1.3
*** olsrd_plugin.h	20 Apr 2007 13:46:04 -0000	1.2
--- olsrd_plugin.h	15 Jul 2007 19:29:37 -0000	1.3
***************
*** 41,46 ****
  
  
! /* Interface version 4 is new direct access to main functions */
! #define OLSRD_PLUGIN_INTERFACE_VERSION 4
  
  
--- 41,47 ----
  
  
! /* Define the most recent version */
! #define MOST_RECENT_PLUGIN_INTERFACE_VERSION		5
! #define LAST_SUPPORTED_PLUGIN_INTERFACE_VERSION		4
  
  
***************
*** 48,52 ****
   *                Functions that the plugin MUST provide                    *
   ****************************************************************************/
- 
  #if 1
  /* We hide them from the compiler here to allow the plugins itself to declare them
--- 49,52 ----
***************
*** 60,82 ****
   * Used by main olsrd to check plugin interface version
   */
! int 
! olsrd_plugin_interface_version(void);
  
  
  /**
   * Register parameters from config file
   * Called for all plugin parameters
   */
! int
! olsrd_plugin_register_param(char *key, char *value);
  
  
  /**
!  * Initialize plugin
!  * Called after all parameters are passed
   */
! int
! olsrd_plugin_init(void);
  #endif
  
  #endif
--- 60,105 ----
   * Used by main olsrd to check plugin interface version
   */
! int olsrd_plugin_interface_version(void);
  
  
  /**
+  * Initialize plugin
+  * Called after all parameters are passed
+  */
+ int olsrd_plugin_init(void);
+ 
+ 
+ /* Interface version 4 */
+ /**
   * Register parameters from config file
   * Called for all plugin parameters
   */
! int olsrd_plugin_register_param(char *key, char *value);
  
+ /* Interface version 5 */
+ 
+ typedef int (*set_plugin_parameter)(const char *value, void *data);
+ 
+ struct olsrd_plugin_parameters {
+     const char *name;
+     set_plugin_parameter set_plugin_parameter;
+     void *data;
+ };
  
  /**
!  * Delivers the (address of the) table and the size of the parameter description
   */
! void olsrd_get_plugin_parameters(const struct olsrd_plugin_parameters **params, int *size);
! 
  #endif
  
  #endif
+ 
+ /*
+  * Local Variables:
+  * mode: c
+  * style: linux
+  * c-basic-offset: 4
+  * indent-tabs-mode: nil
+  * End:
+  */

Index: plugin_loader.c
===================================================================
RCS file: /cvsroot/olsrd/olsrd-current/src/plugin_loader.c,v
retrieving revision 1.26
retrieving revision 1.27
diff -C2 -d -r1.26 -r1.27
*** plugin_loader.c	4 Jul 2007 01:50:45 -0000	1.26
--- plugin_loader.c	15 Jul 2007 19:29:37 -0000	1.27
***************
*** 42,56 ****
  #include "plugin_loader.h"
  #include "olsrd_plugin.h"
  #include "defs.h"
  #include "olsr.h"
- /* Local functions */
  
! static void
! init_olsr_plugin(struct olsr_plugin *);
  
! static int
! olsr_load_dl(char *, struct plugin_param *);
  
! static struct olsr_plugin *olsr_plugins;
  
  
--- 42,57 ----
  #include "plugin_loader.h"
  #include "olsrd_plugin.h"
+ #include "plugin_util.h"
  #include "defs.h"
  #include "olsr.h"
  
! #include <dlfcn.h>
  
! /* Local functions */
! static int init_olsr_plugin(struct olsr_plugin *);
! static int olsr_load_dl(char *, struct plugin_param *);
! static int olsr_add_dl(struct olsr_plugin *);
  
! static struct olsr_plugin *olsr_plugins = NULL;
  
  
***************
*** 60,83 ****
   *@return the number of plugins loaded
   */
! int
! olsr_load_plugins(void)
  {
!   struct plugin_entry *entry = olsr_cnf->plugins;
!   int loaded = 0;
!   OLSR_PRINTF(0, "Loading plugins...\n\n");
!   while(entry)
!     {  
!       if(olsr_load_dl(entry->name, entry->params) < 0)
! 	OLSR_PRINTF(0, "PLUGIN %s LOADING FAILED! --\n\n", entry->name);
!       else {
! 	loaded++;
!       }
! 
!       entry = entry->next;
      }
!   return loaded;
  }
  
- 
  /**
   *Try to load a shared library and extract
--- 61,80 ----
   *@return the number of plugins loaded
   */
! void olsr_load_plugins(void)
  {
!     struct plugin_entry *entry = olsr_cnf->plugins;
!     int rv = 0;
!     for (entry = olsr_cnf->plugins; entry != NULL; entry = entry->next) {
!         if(olsr_load_dl(entry->name, entry->params) < 0) {
!             rv = 1;
!         }
      }
!     if (rv != 0) {
!         OLSR_PRINTF(0, "-- PLUGIN LOADING FAILED! --\n");
!         exit(1);
!     }
!     OLSR_PRINTF(0, "-- ALL PLUGINS LOADED! --\n\n");
  }
  
  /**
   *Try to load a shared library and extract
***************
*** 88,191 ****
   *@return negative on error
   */
! int
! olsr_load_dl(char *libname, struct plugin_param *params)
  {
!   struct olsr_plugin new_entry, *entry;
!   int (*get_interface_version)(void);
!   int *interface_version;
  
!   OLSR_PRINTF(0, "---------- Plugin loader ----------\nLibrary: %s\n", libname);
  
!   if(NULL == (new_entry.dlhandle = dlopen(libname, RTLD_NOW)))
!     {
!       OLSR_PRINTF(0, "DL loading failed: \"%s\"!\n", dlerror());
!       return -1;
      }
  
!   /* Fetch the interface version function, 3 different ways */
!   OLSR_PRINTF(1, "Checking plugin interface version... ");
!   if((get_interface_version = dlsym(new_entry.dlhandle, "olsrd_plugin_interface_version")) == NULL)
!     {
!       OLSR_PRINTF(1, "trying v2 detection... ");
!       if((get_interface_version = dlsym(new_entry.dlhandle, "get_plugin_interface_version")) == NULL)
!         {
!           OLSR_PRINTF(1, "trying v1 detection... ");
!           if((interface_version = dlsym(new_entry.dlhandle, "plugin_interface_version")) == NULL)
!             {
!               OLSR_PRINTF(0, "FAILED: \"%s\"\n", dlerror());
!               dlclose(new_entry.dlhandle);
!               return -1;
!             }
!           else
!             {
!               new_entry.plugin_interface_version = *interface_version;
!             }
!         }
!       else
!         {
!           new_entry.plugin_interface_version = get_interface_version();
          }
      }
!   else
!     {
!       new_entry.plugin_interface_version = get_interface_version();
      }
!   OLSR_PRINTF(0, " %d - OK\n", new_entry.plugin_interface_version);
    
!   if ( new_entry.plugin_interface_version < 4 ) 
!     {
!       /* old plugin interface */
!    
!       OLSR_PRINTF(0, "\nWARNING: YOU ARE USING AN OLD DEPRECATED PLUGIN INTERFACE! DETECTED VERSION %d CURRENT VERSION %d\nPLEASE UPGRADE YOUR PLUGIN!\nWILL CONTINUE IN 5 SECONDS...\n\n", new_entry.plugin_interface_version, OLSRD_PLUGIN_INTERFACE_VERSION);
!       
!       sleep(5);
!       dlclose(new_entry.dlhandle);
!       return -1;
      }
!   else
!     {
!       /* new plugin interface */
!       
!       if ( new_entry.plugin_interface_version != OLSRD_PLUGIN_INTERFACE_VERSION ) 
!         {
!           OLSR_PRINTF(0, "\n\nWARNING: VERSION MISSMATCH! DETECTED %d CURRENT VERSION %d\nTHIS CAN CAUSE UNEXPECTED BEHAVIOUR AND CRASHES!\nWILL CONTINUE IN 5 SECONDS...\n\n", get_interface_version(), OLSRD_PLUGIN_INTERFACE_VERSION);
!           sleep(5);
!         }
     
!       /* Fetch the init function */
!       OLSR_PRINTF(1, "Trying to fetch plugin init function... ");
!       if((new_entry.plugin_init = dlsym(new_entry.dlhandle, "olsrd_plugin_init")) == NULL)
!         {
!           OLSR_PRINTF(1, "FAILED: \"%s\"\n", dlerror());
!           dlclose(new_entry.dlhandle);
!           return -1;
!         }
!       else
! 	{
! 	  OLSR_PRINTF(1, "OK\n");
! 	}
      }
- 
-   /* Fetch the parameter function */
-   OLSR_PRINTF(1, "Trying to fetch param function... ");
-   if((new_entry.register_param = dlsym(new_entry.dlhandle, "olsrd_plugin_register_param")) == NULL)
-     OLSR_PRINTF(1, "FAILED: \"%s\"\n", dlerror());
-   else
      OLSR_PRINTF(1, "OK\n");
  
!   entry = olsr_malloc(sizeof(struct olsr_plugin), "Plugin entry");
!   memcpy(entry, &new_entry, sizeof(struct olsr_plugin));
!   entry->params = params;
! 
!   /* Initialize the plugin */
!   init_olsr_plugin(entry);
  
!   /* queue */
!   entry->next = olsr_plugins;
!   olsr_plugins = entry;
  
!   OLSR_PRINTF(0, "---------- LIBRARY LOADED ----------\n\n");
  
!   return 0;
  }
  
--- 85,228 ----
   *@return negative on error
   */
! int olsr_load_dl(char *libname, struct plugin_param *params)
  {
!     struct olsr_plugin *plugin = olsr_malloc(sizeof(struct olsr_plugin), "Plugin entry");
!     int rv;
  
!     OLSR_PRINTF(0, "---------- LOADING LIBRARY %s ----------\n", libname);
  
!     plugin->dlhandle = dlopen(libname, RTLD_NOW);
!     if(plugin->dlhandle == NULL) {
!         const int save_errno = errno;
!         OLSR_PRINTF(1, "DL loading failed: \"%s\"!\n", dlerror());
!         free(plugin);
!         errno = save_errno;
!         return -1;
      }
  
!     rv = olsr_add_dl(plugin);
!     if (rv == -1) {
!         const int save_errno = errno;
!         dlclose(plugin->dlhandle);
!         free(plugin);
!         errno = save_errno;
!     } else {
!         plugin->params = params;
! 
!         /* Initialize the plugin */
!         if (init_olsr_plugin(plugin) != 0) {
!             rv = -1;
          }
+ 
+         /* queue */
+         plugin->next = olsr_plugins;
+         olsr_plugins = plugin;
      }
!     OLSR_PRINTF(0, "---------- LIBRARY %s %s ----------\n\n", libname, rv == 0 ? "LOADED" : "FAILED");
!     return rv;
! }
! 
! #if SUPPORT_OLD_PLUGIN_VERSIONS
! static int try_old_versions(const struct olsr_plugin *plugin)
! {
!     get_interface_version_func get_interface_version;
!     int *interface_version;
! 
!     OLSR_PRINTF(1, "trying v2 detection... ");
!     get_interface_version = dlsym(plugin->dlhandle, "get_plugin_interface_version");
!     if (get_interface_version != NULL) {
!         return get_interface_version();
      }
! 
!     OLSR_PRINTF(1, "trying v1 detection... ");
!     interface_version = dlsym(plugin->dlhandle, "plugin_interface_version");
!     if (interface_version != NULL) {
!         return *interface_version;
!     }
!     OLSR_PRINTF(0, "FAILED: \"%s\"\n", dlerror());
!     return -1;
! }
! #else 
! #define try_old_versions(plugin) -1
! #endif
! 
! static int olsr_add_dl(struct olsr_plugin *plugin)
! {
!     get_interface_version_func get_interface_version;
!     get_plugin_parameters_func get_plugin_parameters;
!     int plugin_interface_version;
! 
!     /* Fetch the interface version function, 3 different ways */
!     OLSR_PRINTF(0, "Checking plugin interface version: ");
!     get_interface_version = dlsym(plugin->dlhandle, "olsrd_plugin_interface_version");
!     if (get_interface_version == NULL) {
!         plugin_interface_version = try_old_versions(plugin);
!     } else {
!         plugin_interface_version = get_interface_version();
!     }
!     if (plugin_interface_version == -1) {
!         OLSR_PRINTF(0, "FAILED: \"%s\"\n", dlerror());
!         return -1;
!     }
!     OLSR_PRINTF(0, " %d - OK\n", plugin_interface_version);
    
!     if (plugin_interface_version < 5){
!         /* old plugin interface */   
!         OLSR_PRINTF(0, "\nWARNING: YOU ARE USING AN OLD DEPRECATED PLUGIN INTERFACE!\n"
!                     "DETECTED VERSION %d AND THE CURRENT VERSION IS %d\n"
!                     "PLEASE UPGRADE YOUR PLUGIN!\n", plugin_interface_version, MOST_RECENT_PLUGIN_INTERFACE_VERSION);
! #if SUPPORT_OLD_PLUGIN_VERSIONS
!         OLSR_PRINTF(0, "WILL CONTINUE IN 5 SECONDS...\n\n");      
!         sleep(5);
! #else
!         return -1;
! #endif
      }
! 
! #if SUPPORT_OLD_PLUGIN_VERSIONS
!     /* new plugin interface */      
!     if (plugin_interface_version < LAST_SUPPORTED_PLUGIN_INTERFACE_VERSION) {
!         OLSR_PRINTF(0, "\n\nWARNING: VERSION MISMATCH!\n"
!                     "DETECTED %d AND LAST SUPPORTED VERSION IS %d\n"
!                     "THIS CAN CAUSE UNEXPECTED BEHAVIOUR AND CRASHES!\n"
!                     "WILL CONTINUE IN 5 SECONDS...\n\n", get_interface_version(), LAST_SUPPORTED_PLUGIN_INTERFACE_VERSION);
!         sleep(5);
!     }
! #endif
     
!     /* Fetch the init function */
!     OLSR_PRINTF(1, "Trying to fetch plugin init function: ");
!     plugin->plugin_init = dlsym(plugin->dlhandle, "olsrd_plugin_init");
!     if (plugin->plugin_init == NULL) {
!         OLSR_PRINTF(0, "FAILED: \"%s\"\n", dlerror());
!         return -1;
      }
      OLSR_PRINTF(1, "OK\n");
  
!     OLSR_PRINTF(1, "Trying to fetch parameter table and it's size... \n");
  
!     get_plugin_parameters = dlsym(plugin->dlhandle, "olsrd_get_plugin_parameters");
!     if (get_plugin_parameters != NULL) {
!         (*get_plugin_parameters)(&plugin->plugin_parameters, &plugin->plugin_parameters_size);
!     } else {
! #if SUPPORT_OLD_PLUGIN_VERSIONS
!         /* Fetch the parameter function */
!         OLSR_PRINTF(1, "Trying to fetch param function: ");
  
!         plugin->register_param = dlsym(plugin->dlhandle, "olsrd_plugin_register_param");
!         if(plugin->register_param == NULL) {
!             OLSR_PRINTF(1, "FAILED: \"%s\"\n", dlerror());
!         } else {
!             OLSR_PRINTF(1, "OK\n");
!         }
  
!         plugin->plugin_parameters      = NULL;
!         plugin->plugin_parameters_size = 0;
! #else
!         OLSR_PRINTF(0, "Old plugin interfaces are not supported\n");
!         return -1;
! #endif
!     }
!     return 0;
  }
  
***************
*** 199,226 ****
   *@param entry the plugin to initialize
   *
!  *@return nada
   */
! void
! init_olsr_plugin(struct olsr_plugin *entry)
  {
!   if(entry->register_param)
!     {
!       struct plugin_param *params;
!       OLSR_PRINTF(1, "Sending parameters...\n");
!         for(params = entry->params; params; params = params->next)
!         {
!           int retval = entry->register_param(params->key, params->value);
!           OLSR_PRINTF(1, "\"%s\"/\"%s\"... ", params->key, params->value);
!           if(retval < 0)
!             {
!               fprintf(stderr, "\nFatal error in plugin parameter \"%s\"/\"%s\"\n", params->key, params->value);
!               exit(EXIT_FAILURE);
              }
!           OLSR_PRINTF(1, "%s\n", retval == 0 ? "FAILED" : "OK");
          }
      }
!     
!   OLSR_PRINTF(1, "Running plugin_init function...\n");
      entry->plugin_init();
  }
  
--- 236,290 ----
   *@param entry the plugin to initialize
   *
!  *@return -1 if there was an error
   */
! int init_olsr_plugin(struct olsr_plugin *entry)
  {
!     int rv = 0;
!     struct plugin_param *params;
!     OLSR_PRINTF(1, "Sending parameters...\n");
!     for(params = entry->params; params != NULL; params = params->next) {
!         OLSR_PRINTF(1, "\"%s\"/\"%s\"... ", params->key, params->value);
!         if (entry->plugin_parameters_size != 0) {
!             unsigned int i;
!             int rc = 0;
!             for (i = 0; i < entry->plugin_parameters_size; i++) {
!                 if (strcasecmp(entry->plugin_parameters[i].name, params->key) == 0) {
!                     /* we have found it! */
!                     rc = entry->plugin_parameters[i].set_plugin_parameter(params->value, entry->plugin_parameters[i].data);
!                     if (rc != 0) {
!                         fprintf(stderr, "\nFatal error in plugin parameter \"%s\"/\"%s\"\n", params->key, params->value);
!                         rv = -1;
!                     }
!                     break;
!                 }
              }
!             if (i >= entry->plugin_parameters_size) {
!                 OLSR_PRINTF(0, "Ignored parameter \"%s\"\n", params->key);
!             } else {
!                 OLSR_PRINTF(1, "%s: %s\n", params->key, rc == 0 ? "OK" : "FAILED");
!                 if (rc != 0) {
!                     rv = -1;
!                 }
!             }
! #if SUPPORT_OLD_PLUGIN_VERSIONS
!         } else if(entry->register_param != NULL) {
!             int rc;
!             OLSR_PRINTF(0, "Registering parameter \"%s\": ", params->key);
!             rc = entry->register_param(params->key, params->value);
!             if(rc < 0) {
!                 fprintf(stderr, "\nFatal error in plugin parameter \"%s\"/\"%s\"\n", params->key, params->value);
!                 exit(EXIT_FAILURE);
!             }
!             OLSR_PRINTF(0, "%s\n", rc == 0 ? "FAILED" : "OK");
! #endif
!         } else {
!             OLSR_PRINTF(0, "I don't know what to do with \"%s\"!\n", params->key);
!             rv = -1;
          }
      }
! 
!     OLSR_PRINTF(1, "Running plugin_init function...\n");
      entry->plugin_init();
+     return rv;
  }
  
***************
*** 229,239 ****
   *Close all loaded plugins
   */
! void
! olsr_close_plugins(void)
  {
!   struct olsr_plugin *entry;
  
!   OLSR_PRINTF(1, "Closing plugins...\n");
!   for(entry = olsr_plugins; entry != NULL; entry = entry->next)
!       dlclose(entry->dlhandle);
  }
--- 293,313 ----
   *Close all loaded plugins
   */
! void olsr_close_plugins(void)
  {
!     struct olsr_plugin *entry;
  
!     OLSR_PRINTF(0, "Closing plugins...\n");
!     for(entry = olsr_plugins; entry != NULL; entry = entry->next) {
!         dlclose(entry->dlhandle);
!         entry->dlhandle = NULL;
!     }
  }
+ 
+ /*
+  * Local Variables:
+  * mode: c
+  * style: linux
+  * c-basic-offset: 4
+  * indent-tabs-mode: nil
+  * End:
+  */





More information about the Olsr-cvs mailing list