From 99affdd759afb115a521718d945dc12108959757 Mon Sep 17 00:00:00 2001 From: Frank Benkstein Date: Fri, 16 Jan 2009 19:50:49 +0100 Subject: [PATCH] src/plugin.{c,h}: improve VlockPlugin implementation and remove legacy code --- src/plugin.c | 91 +++++++++++++++++++----------------------------------------- src/plugin.h | 59 ++++----------------------------------- 2 files changed, 35 insertions(+), 115 deletions(-) diff --git a/src/plugin.c b/src/plugin.c index 8fe337f..f9054f6 100644 --- a/src/plugin.c +++ b/src/plugin.c @@ -20,67 +20,6 @@ #include "plugin.h" #include "util.h" -/* Allocate a new plugin struct. */ -struct plugin *new_plugin(const char *name, struct plugin_type *type) -{ - struct plugin *p = malloc(sizeof *p); - char *last_slash; - - if (p == NULL) - return NULL; - - /* For security plugin names must not contain a slash. */ - last_slash = strrchr(name, '/'); - - if (last_slash != NULL) - name = last_slash+1; - - p->name = strdup(name); - - if (p->name == NULL) { - free(p); - return NULL; - } - - p->context = NULL; - p->save_disabled = false; - - for (size_t i = 0; i < nr_dependencies; i++) - p->dependencies[i] = NULL; - - p->type = type; - - if (p->type->init(p)) { - return p; - } else { - destroy_plugin(p); - return NULL; - } -} - -/* Destroy the given plugin. */ -void destroy_plugin(struct plugin *p) -{ - /* Call destroy method. */ - p->type->destroy(p); - - /* Destroy dependency lists. */ - for (size_t i = 0; i < nr_dependencies; i++) { - while (p->dependencies[i] != NULL) { - free(p->dependencies[i]->data); - p->dependencies[i] = g_list_delete_link(p->dependencies[i], p->dependencies[i]); - } - } - - free(p->name); - free(p); -} - -bool call_hook(struct plugin *p, const char *hook_name) -{ - return p->type->call_hook(p, hook_name); -} - GQuark vlock_plugin_error_quark(void) { return g_quark_from_static_string("vlock-plugin-error-quark"); @@ -113,9 +52,18 @@ static GObject *vlock_plugin_constructor( static void vlock_plugin_finalize(GObject *object) { VlockPlugin *self = VLOCK_PLUGIN(object); + g_free(self->name); self->name = NULL; + /* Destroy dependency lists. */ + for (size_t i = 0; i < nr_dependencies; i++) { + while (self->dependencies[i] != NULL) { + g_free(self->dependencies[i]->data); + self->dependencies[i] = g_list_delete_link(self->dependencies[i], self->dependencies[i]); + } + } + G_OBJECT_CLASS(vlock_plugin_parent_class)->finalize(object); } @@ -125,6 +73,17 @@ enum { PROP_VLOCK_PLUGIN_NAME }; +static void vlock_plugin_set_name(VlockPlugin *self, const gchar *name) +{ + /* For security plugin names must not contain a slash. */ + char *last_slash = strrchr(name, '/'); + + if (last_slash != NULL) + name = last_slash+1; + + self->name = g_strdup(name); +} + /* Set properties. */ static void vlock_plugin_set_property( GObject *object, @@ -138,7 +97,7 @@ static void vlock_plugin_set_property( { case PROP_VLOCK_PLUGIN_NAME: g_free(self->name); - self->name = g_value_dup_string(value); + vlock_plugin_set_name(self, g_value_get_string(value)); break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec); @@ -174,6 +133,7 @@ static void vlock_plugin_class_init(VlockPluginClass *klass) /* Virtual methods. */ klass->open = NULL; + klass->call_hook = NULL; /* Install overridden methods. */ gobject_class->constructor = vlock_plugin_constructor; @@ -203,3 +163,10 @@ bool vlock_plugin_open(VlockPlugin *self, GError **error) g_assert(klass->open != NULL); return klass->open(self, error); } + +bool vlock_plugin_call_hook(VlockPlugin *self, const gchar *hook_name) +{ + VlockPluginClass *klass = VLOCK_PLUGIN_GET_CLASS(self); + g_assert(klass->call_hook != NULL); + return klass->call_hook(self, hook_name); +} diff --git a/src/plugin.h b/src/plugin.h index 38a064a..3af3809 100644 --- a/src/plugin.h +++ b/src/plugin.h @@ -32,58 +32,6 @@ struct hook #define nr_hooks 4 extern const struct hook hooks[nr_hooks]; -struct plugin_type; - -/* Struct representing a plugin instance. */ -struct plugin -{ - /* The name of the plugin. */ - char *name; - - /* Array of dependencies. Each dependency is a (possibly empty) list of - * strings. The dependencies must be stored in the same order as the - * dependency names above. The strings a freed when the plugin is destroyed - * thus must be stored as copies. */ - GList *dependencies[nr_dependencies]; - - /* Did one of the save hooks fail? */ - bool save_disabled; - - /* The type of the plugin. */ - struct plugin_type *type; - - /* The call_hook and close functions may use this pointer. */ - void *context; -}; - - -/* Struct representing the type of a plugin. */ -struct plugin_type -{ - /* Method that is called on plugin initialization. */ - bool (*init)(struct plugin *p); - /* Method that is called on plugin destruction. */ - void (*destroy)(struct plugin *p); - /* Method that is called when a hook should be executed. */ - bool (*call_hook)(struct plugin *p, const char *name); -}; - -/* Modules. */ -extern struct plugin_type *module; -/* Scripts. */ -extern struct plugin_type *script; - -/* Open a new plugin struct of the given type. On error errno is set and NULL - * is returned. */ -struct plugin *new_plugin(const char *name, struct plugin_type *type); - -/* Destroy the given plugin. This is the opposite of of __allocate_plugin. - * This function should not be called directly. */ -void destroy_plugin(struct plugin *p); - -/* Call the hook of a plugin. */ -bool call_hook(struct plugin *p, const char *hook_name); - /* Errors */ #define VLOCK_PLUGIN_ERROR vlock_plugin_error_quark() GQuark vlock_plugin_error_quark(void); @@ -108,7 +56,8 @@ typedef struct _VlockPluginClass VlockPluginClass; struct _VlockPlugin { - GObject *parent_instance; + GObject parent_instance; + gchar *name; GList *dependencies[nr_dependencies]; @@ -119,9 +68,13 @@ struct _VlockPluginClass GObjectClass parent_class; bool (*open)(VlockPlugin *self, GError **error); + bool (*call_hook)(VlockPlugin *self, const gchar *hook_name); }; GType vlock_plugin_get_type(void); /* Open the plugin. */ bool vlock_plugin_open(VlockPlugin *self, GError **error); + +GList *vlock_plugin_get_dependencies(VlockPlugin *self, const gchar *dependency_name); +bool vlock_plugin_call_hook(VlockPlugin *self, const gchar *hook_name); -- 2.11.4.GIT