-
Notifications
You must be signed in to change notification settings - Fork 1
/
module.c
127 lines (115 loc) · 3.01 KB
/
module.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/**
* @file module.c
* Module abstraction
*
* Copyright 2017, Allied Telesis Labs New Zealand, Ltd
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this library. If not, see <http://www.gnu.org/licenses/>
*/
#include "kermond.h"
/* Modules */
extern struct module_table __start_module_table;
extern struct module_table __stop_module_table;
bool
modules_enable (const char *modules)
{
gchar **list = g_strsplit (modules, ",", -1);
gchar **plist = list;
module_table *mt;
/* Disable all modules */
for (mt = &__start_module_table; mt != &__stop_module_table; mt++)
{
mt->enabled = false;
}
/* Enable required modules */
while (*plist)
{
for (mt = &__start_module_table; mt != &__stop_module_table; mt++)
{
if (strcmp (*plist, mt->name) == 0)
{
mt->enabled = true;
break;
}
}
if (mt == &__stop_module_table)
{
ERROR ("MODULE: no such module \"%s\"\n", *plist);
modules_dump ();
g_strfreev (list);
return false;
}
plist++;
}
g_strfreev (list);
return true;
}
void
modules_dump ()
{
module_table *mt;
printf ("Modules: ");
for (mt = &__start_module_table; mt != &__stop_module_table; mt++)
{
printf ("%s ", mt->name);
}
printf ("\n");
}
bool
modules_init (void)
{
module_table *mt;
for (mt = &__start_module_table; mt != &__stop_module_table; mt++)
{
if (!mt->enabled || !mt->init)
continue;
VERBOSE ("MODULE: Initialising %s\n", mt->name);
if (!(*mt->init) ())
{
ERROR ("MODULE: Failed to initialise \"%s\"\n", mt->name);
return false;
}
}
return true;
}
bool
modules_start (void)
{
module_table *mt;
for (mt = &__start_module_table; mt != &__stop_module_table; mt++)
{
if (!mt->enabled || !mt->start)
continue;
VERBOSE ("MODULE: Starting %s\n", mt->name);
if (!(*mt->start) ())
{
ERROR ("MODULE: Failed to start \"%s\"\n", mt->name);
return false;
}
}
return true;
}
void
modules_exit (void)
{
module_table *mt;
for (mt = &__stop_module_table - 1; mt >= &__start_module_table; mt--)
{
if (!mt->enabled || !mt->exit)
continue;
VERBOSE ("MODULE: Stopping %s\n", mt->name);
(*mt->exit) ();
}
return;
}