---
 lib/Makefile.am |  10 +++-
 lib/plugin.c    | 177 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 lib/plugin.h    | 117 +++++++++++++++++++++++++++++++++++++
 3 files changed, 301 insertions(+), 3 deletions(-)
 create mode 100644 lib/plugin.c
 create mode 100644 lib/plugin.h

diff --git a/lib/Makefile.am b/lib/Makefile.am
index 1e9b05f..1caa7d9 100644
--- a/lib/Makefile.am
+++ b/lib/Makefile.am
@@ -15,13 +15,15 @@ libzebra_la_SOURCES = \
        zclient.c sockopt.c smux.c agentx.c snmp.c md5.c if_rmap.c keychain.c 
privs.c \
        sigevent.c pqueue.c jhash.c workqueue.c vrf.c \
        memory.c \
-       memory_vty.c
+       memory_vty.c \
+       plugin.c \
+       # end
 
 BUILT_SOURCES = route_types.h gitversion.h
 
 libzebra_la_DEPENDENCIES = @LIB_REGEX@
 
-libzebra_la_LIBADD = @LIB_REGEX@ @LIBCAP@
+libzebra_la_LIBADD = -ldl @LIB_REGEX@ @LIBCAP@
 
 pkginclude_HEADERS = \
        buffer.h checksum.h command.h filter.h getopt.h hash.h \
@@ -31,7 +33,9 @@ pkginclude_HEADERS = \
        plist.h zclient.h sockopt.h smux.h md5.h if_rmap.h keychain.h \
        privs.h sigevent.h pqueue.h jhash.h zassert.h \
        workqueue.h route_types.h libospf.h vrf.h fifo.h \
-       memory_vty.h
+       memory_vty.h \
+       plugin.h \
+       # end
 
 noinst_HEADERS = \
        plist_int.h
diff --git a/lib/plugin.c b/lib/plugin.c
new file mode 100644
index 0000000..81635a7
--- /dev/null
+++ b/lib/plugin.c
@@ -0,0 +1,177 @@
+/*
+ * Copyright (c) 2015-16  David Lamparter, for NetDEF, Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+#include "config.h"
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <limits.h>
+#include <dlfcn.h>
+
+#include "plugin.h"
+#include "memory.h"
+#include "version.h"
+
+DEFINE_MTYPE_STATIC(LIB, PLUGIN_LOADNAME, "Plugin loading name")
+DEFINE_MTYPE_STATIC(LIB, PLUGIN_LOADARGS, "Plugin loading arguments")
+
+static struct qplug_info qplug_daemon_executable_info = {
+       .name = "libzebra",
+       .version = QUAGGA_VERSION,
+       .description = "libzebra (no plugin)",
+};
+union _qplug_runtime_u qplug_default = {
+       .r.info = &qplug_daemon_executable_info,
+       .r.finished_loading = 1,
+};
+
+#if defined(HAVE_SYS_WEAK_ALIAS_ATTRIBUTE)
+union _qplug_runtime_u _qplug_this_plugin
+       __attribute__((weak, alias("qplug_default")));
+#elif defined(HAVE_SYS_WEAK_ALIAS_PRAGMA)
+#pragma weak _qplug_this_plugin = qplug_default
+#else
+#error need weak symbol support
+#endif
+
+struct qplug_runtime *qplug_list = &_qplug_this_plugin.r;
+static struct qplug_runtime **qplug_last = &_qplug_this_plugin.r.next;
+
+#define PLUGIN_PATH "/usr/lib/quagga/plugins"
+
+struct qplug_runtime *qplug_load(const char *spec,
+               char *err, size_t err_len)
+{
+       void *handle = NULL, *handle2;
+       char name[PATH_MAX], fullpath[PATH_MAX], *args;
+       struct qplug_runtime *rtinfo;
+       const struct qplug_info *info;
+
+       snprintf(name, sizeof(name), "%s", spec);
+       args = strchr(name, ':');
+       if (args)
+               *args++ = '\0';
+
+       if (!strchr(name, '/')) {
+               snprintf(fullpath, sizeof(fullpath), "%s/%s.so",
+                               PLUGIN_PATH, name);
+               handle = dlopen(fullpath, RTLD_LAZY | RTLD_GLOBAL);
+       }
+       if (!handle) {
+               snprintf(fullpath, sizeof(fullpath), "%s", name);
+               handle = dlopen(fullpath, RTLD_LAZY | RTLD_GLOBAL);
+       }
+       if (!handle) {
+               if (err)
+                       snprintf(err, err_len,
+                                       "loading plugin \"%s\" failed: %s",
+                                       name, dlerror());
+               return NULL;
+       }
+
+       rtinfo = dlsym(handle, "_qplug_this_plugin");
+       if (!rtinfo) {
+               dlclose(handle);
+               if (err)
+                       snprintf(err, err_len,
+                                       "\"%s\" is not a Quagga plugin: %s",
+                                       name, dlerror());
+               return NULL;
+       }
+       rtinfo->load_name = XSTRDUP(MTYPE_PLUGIN_LOADNAME, name);
+       if (args)
+               rtinfo->load_args = XSTRDUP(MTYPE_PLUGIN_LOADARGS, args);
+       info = rtinfo->info;
+
+       if (rtinfo->finished_loading) {
+               dlclose(handle);
+               if (err)
+                       snprintf(err, err_len,
+                                       "plugin \"%s\" already loaded",
+                                       name);
+               goto out_fail;
+       }
+
+#if 0
+       /* not worth the effort - incompatible plugins will fail to load
+        * with undefined references anyway */
+       do {
+               char daemon_delim[64];
+               snprintf(daemon_delim, sizeof(daemon_delim), ";%s;",
+                               qplug_daemon_name);
+
+               if (!info->compat_list)
+                       break;
+               if (strstr(info->compat_list, daemon_delim))
+                       break;
+               if (strstr(info->compat_list, ";all;"))
+                       break;
+
+               dlclose(handle);
+               if (err)
+                       snprintf(err, err_len,
+                                       "plugin \"%s\" not compatible with"
+                                       "this Quagga daemon", name);
+               goto out_fail;
+       } while (0);
+#endif
+
+       /* force symbol resolution now, so we don't explode at runtime.
+        * this is not on first load to make more specific errors possible */
+       handle2 = dlopen(fullpath, RTLD_NOW | RTLD_GLOBAL);
+       if (!handle2) {
+               if (err)
+                       snprintf(err, err_len,
+                                       "plugin \"%s\" symbol resolution "
+                                       "failed: %s", name, dlerror());
+               dlclose(handle);
+               goto out_fail;
+       }
+       dlclose(handle);
+       rtinfo->dl_handle = handle2;
+
+       if (info->init && info->init()) {
+               dlclose(handle2);
+               if (err)
+                       snprintf(err, err_len,
+                                       "plugin \"%s\" initialisation failed",
+                                       name);
+               goto out_fail;
+       }
+
+       rtinfo->finished_loading = 1;
+
+       *qplug_last = rtinfo;
+       qplug_last = &rtinfo->next;
+       return rtinfo;
+
+out_fail:
+       if (rtinfo->load_args)
+               XFREE(MTYPE_PLUGIN_LOADARGS, rtinfo->load_args);
+       XFREE(MTYPE_PLUGIN_LOADNAME, rtinfo->load_name);
+       return NULL;
+}
+
+void qplug_unload(struct qplug_runtime *plugin)
+{
+}
diff --git a/lib/plugin.h b/lib/plugin.h
new file mode 100644
index 0000000..4ddf8df
--- /dev/null
+++ b/lib/plugin.h
@@ -0,0 +1,117 @@
+/*
+ * Copyright (c) 2015-16  David Lamparter, for NetDEF, Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+#ifndef _QUAGGA_PLUGIN_H
+#define _QUAGGA_PLUGIN_H
+
+#include <stdint.h>
+#include <stdbool.h>
+
+#if !defined(__GNUC__)
+# error plugin code needs GCC visibility extensions
+#elif __GNUC__ < 4
+# error plugin code needs GCC visibility extensions
+#else
+# define DSO_PUBLIC __attribute__ ((visibility ("default")))
+# define DSO_SELF   __attribute__ ((visibility ("protected")))
+# define DSO_LOCAL  __attribute__ ((visibility ("hidden")))
+#endif
+
+struct qplug_runtime;
+
+struct qplug_info {
+       /* single-line few-word title */
+       const char *name;
+       /* human-readable version number, should not contain spaces */
+       const char *version;
+       /* one-paragraph description */
+       const char *description;
+
+       int (*init)(void);
+
+#if 0
+       /* list of daemons with which this plugin can be used.
+        * semicolon separated:  ";daemon1;daemon2;daemon3;"
+        * (note semicolon at start!)
+        * NULL or special value "all" means no restrictions.
+        *
+        * NB: in most cases, loading a daemon-specific module will fail
+        * anyway because there will be unresolved symbols
+        * (if not, loader code will unload the module without calling init)
+        */
+       const char *compat_list;
+
+       /* for version checks when depending on other plugins;  not used or
+        * displayed anywhere in main Quagga
+        *
+        * failing with unresolved symbols when a prerequisite plugin is
+        * missing is always an option too */
+       uint8_t v_major, v_minor, v_revision, v_patch;
+#endif
+};
+
+/* primary entry point structure to be present in loadable plugin under
+ * "_qplug_this_plugin" dlsym() name
+ *
+ * note space for future extensions is reserved below, so other modules
+ * (e.g. memory management, hooks) can add fields
+ *
+ * const members/info are in qplug_info.
+ */
+struct qplug_runtime {
+       struct qplug_runtime *next;
+
+       const struct qplug_info *info;
+       void *dl_handle;
+       bool finished_loading;
+
+       char *load_name;
+       char *load_args;
+};
+
+/* space-reserving foo */
+struct _qplug_runtime_size {
+       struct qplug_runtime r;
+       /* this will barf if qplug_runtime exceeds 1024 bytes ... */
+       uint8_t space[1024 - sizeof(struct qplug_runtime)];
+};
+union _qplug_runtime_u {
+       struct qplug_runtime r;
+       struct _qplug_runtime_size s;
+};
+
+extern DSO_SELF union _qplug_runtime_u _qplug_this_plugin;
+#define THIS_PLUGIN (&_qplug_this_plugin.r)
+
+#define QUAGGA_PLUGIN_SETUP(...) \
+       static const struct qplug_info _qplug_info = { __VA_ARGS__ }; \
+       DSO_SELF union _qplug_runtime_u _qplug_this_plugin = { \
+               .r.info = &_qplug_info, \
+       };
+
+extern struct qplug_runtime *qplug_list;
+
+extern struct qplug_runtime *qplug_load(const char *spec,
+               char *err, size_t err_len);
+extern void qplug_unload(struct qplug_runtime *plugin);
+
+#endif /* _QUAGGA_PLUGIN_H */
-- 
2.7.3


_______________________________________________
Quagga-dev mailing list
[email protected]
https://lists.quagga.net/mailman/listinfo/quagga-dev

Reply via email to