]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/lib/plugins.c
Detect mount/junction points and ignore junctions in Windows
[bacula/bacula] / bacula / src / lib / plugins.c
index 2a135dd470b289d50f3669315ddb7e2894743122..35a0abf3679ebe4d0edf491576cb3e0145d2fedd 100644 (file)
@@ -1,12 +1,12 @@
 /*
    Bacula® - The Network Backup Solution
 
-   Copyright (C) 2007-2008 Free Software Foundation Europe e.V.
+   Copyright (C) 2007-2009 Free Software Foundation Europe e.V.
 
    The main author of Bacula is Kern Sibbald, with contributions from
    many others, a complete list can be found in the file AUTHORS.
    This program is Free Software; you can redistribute it and/or
-   modify it under the terms of version two of the GNU General Public
+   modify it under the terms of version three of the GNU Affero General Public
    License as published by the Free Software Foundation, which is 
    listed in the file LICENSE.
 
@@ -15,7 +15,7 @@
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    General Public License for more details.
 
-   You should have received a copy of the GNU General Public License
+   You should have received a copy of the GNU Affero General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301, USA.
  *
  * Kern Sibbald, October 2007
  */
+
 #include "bacula.h"
+#include <dlfcn.h>
+#ifdef HAVE_DIRENT_H
+#include <dirent.h>
+#define NAMELEN(dirent) (strlen((dirent)->d_name))
+#endif
+#ifndef HAVE_READDIR_R
+int readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result);
+#endif
+
+#ifndef RTLD_NOW
+#define RTLD_NOW 2
+#endif
+
 #include "plugins.h"
 
 static const int dbglvl = 50;
@@ -49,19 +63,35 @@ Plugin *new_plugin()
 
    plugin = (Plugin *)malloc(sizeof(Plugin));
    memset(plugin, 0, sizeof(Plugin));
-   plugin_list->append(plugin);
    return plugin;
 }
 
+static void close_plugin(Plugin *plugin)
+{
+   if (plugin->file) {
+      Dmsg1(50, "Got plugin=%s but not accepted.\n", plugin->file);
+   }
+   if (plugin->unloadPlugin) {
+      plugin->unloadPlugin();
+   }
+   if (plugin->pHandle) {
+      dlclose(plugin->pHandle);
+   }
+   if (plugin->file) {
+      free(plugin->file);
+   }
+   free(plugin);
+}
 
 /*
  * Load all the plugins in the specified directory.
  */
-bool load_plugins(void *binfo, void *bfuncs, const char *plugin_dir, const char *type)
+bool load_plugins(void *binfo, void *bfuncs, const char *plugin_dir, 
+        const char *type, bool is_plugin_compatible(Plugin *plugin))
 {
    bool found = false;
    t_loadPlugin loadPlugin;
-   Plugin *plugin;
+   Plugin *plugin = NULL;
    DIR* dp = NULL;
    struct dirent *entry = NULL, *result;
    int name_max;
@@ -91,6 +121,8 @@ bool load_plugins(void *binfo, void *bfuncs, const char *plugin_dir, const char
    }
    entry = (struct dirent *)malloc(sizeof(struct dirent) + name_max + 1000);
    for ( ;; ) {
+      plugin = NULL;            /* Start from a fresh plugin */
+
       if ((readdir_r(dp, entry, &result) != 0) || (result == NULL)) {
          if (!found) {
             Jmsg(NULL, M_WARNING, 0, _("Failed to find any plugins in %s\n"), 
@@ -123,13 +155,15 @@ bool load_plugins(void *binfo, void *bfuncs, const char *plugin_dir, const char
 
       plugin = new_plugin();
       plugin->file = bstrdup(result->d_name);
+      plugin->file_len = strstr(plugin->file, type) - plugin->file;
       plugin->pHandle = dlopen(fname.c_str(), RTLD_NOW);
       if (!plugin->pHandle) {
          Jmsg(NULL, M_ERROR, 0, _("Plugin load %s failed: ERR=%s\n"), 
               fname.c_str(), NPRT(dlerror()));
          Dmsg2(dbglvl, "Plugin load %s failed: ERR=%s\n", fname.c_str(), 
                NPRT(dlerror()));
-         goto get_out;
+         close_plugin(plugin);
+         continue;
       }
 
       /* Get two global entry points */
@@ -139,7 +173,8 @@ bool load_plugins(void *binfo, void *bfuncs, const char *plugin_dir, const char
             fname.c_str(), NPRT(dlerror()));
          Dmsg2(dbglvl, "Lookup of loadPlugin in plugin %s failed: ERR=%s\n", 
             fname.c_str(), NPRT(dlerror()));
-         goto get_out;
+         close_plugin(plugin);
+         continue;
       }
       plugin->unloadPlugin = (t_unloadPlugin)dlsym(plugin->pHandle, "unloadPlugin");
       if (!plugin->unloadPlugin) {
@@ -147,16 +182,30 @@ bool load_plugins(void *binfo, void *bfuncs, const char *plugin_dir, const char
             fname.c_str(), NPRT(dlerror()));
          Dmsg2(dbglvl, "Lookup of unloadPlugin in plugin %s failed: ERR=%s\n",
             fname.c_str(), NPRT(dlerror()));
-         goto get_out;
+         close_plugin(plugin);
+         continue;
       }
 
       /* Initialize the plugin */
-      loadPlugin(binfo, bfuncs, &plugin->pinfo, &plugin->pfuncs);
+      if (loadPlugin(binfo, bfuncs, &plugin->pinfo, &plugin->pfuncs) != bRC_OK) {
+         close_plugin(plugin);
+         continue;
+      }
+      if (!is_plugin_compatible) {
+         Dmsg0(50, "Plugin compatibility pointer not set.\n");   
+      } else if (!is_plugin_compatible(plugin)) {
+         close_plugin(plugin);
+         continue;
+      }
 
       found = true;                /* found a plugin */
+      plugin_list->append(plugin);
    }
 
 get_out:
+   if (!found && plugin) {
+      close_plugin(plugin);
+   }
    if (entry) {
       free(entry);
    }
@@ -188,3 +237,36 @@ void unload_plugins()
    delete plugin_list;
    plugin_list = NULL;
 }
+
+/*
+ * Dump plugin information
+ * Each daemon can register a hook that will be called
+ * after a fatal signal.
+ */
+#define DBG_MAX_HOOK 10
+static dbg_plugin_hook_t *dbg_plugin_hooks[DBG_MAX_HOOK];
+static int dbg_plugin_hook_count=0;
+
+void dbg_plugin_add_hook(dbg_plugin_hook_t *fct)
+{
+   ASSERT(dbg_plugin_hook_count < DBG_MAX_HOOK);
+   dbg_plugin_hooks[dbg_plugin_hook_count++] = fct;
+}
+
+void dbg_print_plugin(FILE *fp)
+{
+   Plugin *plugin;
+   fprintf(fp, "Attempt to dump plugins. Hook count=%d\n", dbg_plugin_hook_count);
+
+   if (!plugin_list) {
+      return;
+   }
+   foreach_alist(plugin, plugin_list) {
+      for(int i=0; i < dbg_plugin_hook_count; i++) {
+//       dbg_plugin_hook_t *fct = dbg_plugin_hooks[i];
+         fprintf(fp, "Plugin %p name=\"%s\" disabled=%d\n",
+                 plugin, plugin->file, plugin->disabled);
+//       fct(plugin, fp);
+      }
+   }
+}