forked from dimkr/lazy-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
module.c
226 lines (185 loc) · 5.12 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include <sys/syscall.h>
#include <libgen.h>
#include <errno.h>
#include "common.h"
#include "module.h"
bool module_open(module_t *module, const char *path) {
/* a loop index */
size_t i = 0;
/* the module file name extension */
char *extension = NULL;
assert(NULL != module);
assert(NULL != path);
/* get the module size */
if (-1 == stat(path, &module->attributes)) {
goto failure;
}
/* copy the module path */
module->path = strdup(path);
if (NULL == module->path) {
goto failure;
}
/* get the module name */
module->name = basename(module->path);
if (NULL == module->name) {
goto free_path;
}
/* strip the module file name extension */
extension = strchr(module->name, '.');
if (NULL == extension) {
goto free_path;
}
extension[0] = '\0';
/* replace all hyphens in the module name with underscores */
for ( ; strlen(module->name) > i ; ++i) {
if ('-' == module->name[i]) {
module->name[i] = '_';
}
}
/* open the module for reading */
module->fd = open(path, O_RDONLY);
if (-1 == module->fd) {
goto free_path;
}
/* map the module to memory*/
module->contents = mmap(NULL,
(size_t) module->attributes.st_size,
PROT_READ,
MAP_PRIVATE,
module->fd,
0);
if (MAP_FAILED == module->contents) {
goto close_module;
}
return true;
/* unmap the module contents */
(void) munmap(module->contents, (size_t) module->attributes.st_size);
close_module:
/* close the module */
(void) close(module->fd);
free_path:
/* free the module path */
free(module->path);
failure:
return false;
}
void module_close(module_t *module) {
assert(NULL != module);
assert(NULL != module->contents);
assert(NULL != module->path);
/* unmap the module contents */
(void) munmap(module->contents, (size_t) module->attributes.st_size);
/* close the module */
(void) close(module->fd);
/* free the module path */
free(module->path);
}
bool module_load(module_t *module) {
assert(NULL != module);
assert(NULL != module->contents);
assert(0 < module->attributes.st_size);
if (-1 == syscall(SYS_init_module,
module->contents,
(unsigned long) module->attributes.st_size,
"")) {
if (EEXIST != errno) {
return false;
}
}
return true;
}
bool module_for_each_dependency(module_t *module,
const dependency_callback_t callback,
void *arg) {
/* the current offset within the module contents */
off_t offset = 0;
/* the module dependencies */
char *dependencies = NULL;
/* strtok_r()'s position within the dependencies list */
char *position = NULL;
/* a single dependency */
char *dependency = NULL;
/* the return value */
bool result = false;
assert(NULL != module);
assert(NULL != module->contents);
assert(STRLEN("depends=") < module->attributes.st_size);
assert(NULL != callback);
/* find the module dependencies */
for ( ;
module->attributes.st_size - STRLEN("depends=") > offset;
++offset) {
if (0 == strncmp((char *) (module->contents + offset),
"depends=",
STRLEN("depends="))) {
dependencies = (char *) (module->contents + \
offset + \
STRLEN("depends="));
break;
}
}
/* if the dependencies were not found, report failure */
if (NULL == dependencies) {
goto end;
}
/* if the dependencies list is empty, report success */
if ('\0' == dependencies[0]) {
result = true;
goto end;
}
/* duplicate the dependencies list */
dependencies = strdup(dependencies);
if (NULL == dependencies) {
goto end;
}
/* run the callback for each dependency */
dependency = strtok_r(dependencies, ",", &position);
while (NULL != dependency) {
if (false == callback(dependency, arg)) {
goto free_dependencies;
}
dependency = strtok_r(NULL, ",", &position);
}
/* report success */
result = true;
free_dependencies:
/* free the dependencies list */
free(dependencies);
end:
return result;
}
bool module_for_each_alias(module_t *module,
const alias_callback_t callback,
void *arg) {
/* the current offset within the module contents */
off_t offset = 0;
assert(NULL != module);
assert(NULL != module->contents);
assert(STRLEN("depends=") < module->attributes.st_size);
assert(NULL != callback);
/* find the module aliases and run the callback for each */
for ( ;
module->attributes.st_size - STRLEN("alias=") > offset;
++offset) {
if (0 == strncmp((char *) (module->contents + offset),
"alias=",
STRLEN("alias="))) {
if (false == callback(module->name,
(char *) (module->contents + \
offset + \
STRLEN("alias=")),
arg)) {
return false;
}
}
}
return true;
}