src/plugin.{c,h}: improve VlockPlugin implementation and remove legacy code
[vlock.git] / src / plugin.c
blobf9054f63b3379ee11cba270ff3e565fce11d2a24
1 /* plugin.c -- generic plugin routines for vlock,
2 * the VT locking program for linux
4 * This program is copyright (C) 2007 Frank Benkstein, and is free
5 * software which is freely distributable under the terms of the
6 * GNU General Public License version 2, included as the file COPYING in this
7 * distribution. It is NOT public domain software, and any
8 * redistribution not permitted by the GNU General Public License is
9 * expressly forbidden without prior written permission from
10 * the author.
14 #include <stdlib.h>
15 #include <string.h>
16 #include <errno.h>
18 #include <glib.h>
20 #include "plugin.h"
21 #include "util.h"
23 GQuark vlock_plugin_error_quark(void)
25 return g_quark_from_static_string("vlock-plugin-error-quark");
28 G_DEFINE_TYPE(VlockPlugin, vlock_plugin, G_TYPE_OBJECT)
30 /* Initialize plugin to default values. */
31 static void vlock_plugin_init(VlockPlugin *self)
33 self->name = NULL;
36 /* Create new plugin object. */
37 static GObject *vlock_plugin_constructor(
38 GType gtype,
39 guint n_properties,
40 GObjectConstructParam *properties)
42 GObjectClass *parent_class = G_OBJECT_CLASS(vlock_plugin_parent_class);
43 GObject *object = parent_class->constructor(gtype, n_properties, properties);
44 VlockPlugin *self = VLOCK_PLUGIN(object);
46 g_return_val_if_fail(self->name != NULL, NULL);
48 return object;
51 /* Destroy plugin object. */
52 static void vlock_plugin_finalize(GObject *object)
54 VlockPlugin *self = VLOCK_PLUGIN(object);
56 g_free(self->name);
57 self->name = NULL;
59 /* Destroy dependency lists. */
60 for (size_t i = 0; i < nr_dependencies; i++) {
61 while (self->dependencies[i] != NULL) {
62 g_free(self->dependencies[i]->data);
63 self->dependencies[i] = g_list_delete_link(self->dependencies[i], self->dependencies[i]);
67 G_OBJECT_CLASS(vlock_plugin_parent_class)->finalize(object);
70 /* Properties. */
71 enum {
72 PROP_VLOCK_PLUGIN_0,
73 PROP_VLOCK_PLUGIN_NAME
76 static void vlock_plugin_set_name(VlockPlugin *self, const gchar *name)
78 /* For security plugin names must not contain a slash. */
79 char *last_slash = strrchr(name, '/');
81 if (last_slash != NULL)
82 name = last_slash+1;
84 self->name = g_strdup(name);
87 /* Set properties. */
88 static void vlock_plugin_set_property(
89 GObject *object,
90 guint property_id,
91 const GValue *value,
92 GParamSpec *pspec)
94 VlockPlugin *self = VLOCK_PLUGIN(object);
96 switch (property_id)
98 case PROP_VLOCK_PLUGIN_NAME:
99 g_free(self->name);
100 vlock_plugin_set_name(self, g_value_get_string(value));
101 break;
102 default:
103 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
104 break;
108 /* Get properties. */
109 static void vlock_plugin_get_property(
110 GObject *object,
111 guint property_id,
112 GValue *value,
113 GParamSpec *pspec)
115 VlockPlugin *self = VLOCK_PLUGIN(object);
117 switch (property_id)
119 case PROP_VLOCK_PLUGIN_NAME:
120 g_value_set_string(value, self->name);
121 break;
122 default:
123 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
124 break;
128 /* Initialize plugin class. */
129 static void vlock_plugin_class_init(VlockPluginClass *klass)
131 GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
132 GParamSpec *vlock_plugin_param_spec;
134 /* Virtual methods. */
135 klass->open = NULL;
136 klass->call_hook = NULL;
138 /* Install overridden methods. */
139 gobject_class->constructor = vlock_plugin_constructor;
140 gobject_class->finalize = vlock_plugin_finalize;
141 gobject_class->set_property = vlock_plugin_set_property;
142 gobject_class->get_property = vlock_plugin_get_property;
144 /* Install properties. */
145 vlock_plugin_param_spec = g_param_spec_string(
146 "name",
147 "plugin name",
148 "Set the plugin's name",
149 NULL,
150 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE
153 g_object_class_install_property(
154 gobject_class,
155 PROP_VLOCK_PLUGIN_NAME,
156 vlock_plugin_param_spec
160 bool vlock_plugin_open(VlockPlugin *self, GError **error)
162 VlockPluginClass *klass = VLOCK_PLUGIN_GET_CLASS(self);
163 g_assert(klass->open != NULL);
164 return klass->open(self, error);
167 bool vlock_plugin_call_hook(VlockPlugin *self, const gchar *hook_name)
169 VlockPluginClass *klass = VLOCK_PLUGIN_GET_CLASS(self);
170 g_assert(klass->call_hook != NULL);
171 return klass->call_hook(self, hook_name);