redo plugin structure
[vlock.git] / src / plugin.c
blob7282b8e22ce71f78aa31e14a6ec25f7c9ebac148
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>
17 #include "list.h"
19 #include "plugin.h"
20 #include "util.h"
22 /* Allocate a new plugin struct. */
23 struct plugin *new_plugin(const char *name, struct plugin_type *type)
25 struct plugin *p = malloc(sizeof *p);
27 if (p == NULL)
28 return NULL;
30 p->name = strdup(name);
32 if (p->name == NULL) {
33 free(p);
34 return NULL;
37 p->context = NULL;
38 p->save_disabled = false;
40 for (size_t i = 0; i < nr_dependencies; i++)
41 p->dependencies[i] = list_new();
43 p->type = type;
45 if (p->type->init(p)) {
46 return p;
47 } else {
48 destroy_plugin(p);
49 return NULL;
53 /* Destroy the given plugin. */
54 void destroy_plugin(struct plugin *p)
56 /* Call destroy method. */
57 p->type->destroy(p);
59 /* Destroy dependency lists. */
60 for (size_t i = 0; i < nr_dependencies; i++) {
61 list_delete_for_each(p->dependencies[i], dependency_item)
62 free(dependency_item->data);
64 list_free(p->dependencies[i]);
67 free(p->name);
68 free(p);
71 bool call_hook(struct plugin *p, const char *hook_name)
73 return p->type->call_hook(p, hook_name);