-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathvu_modutils.c
More file actions
216 lines (192 loc) · 5.94 KB
/
vu_modutils.c
File metadata and controls
216 lines (192 loc) · 5.94 KB
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
/*
* VUOS: view OS project
* Copyright (C) 2017 Renzo Davoli <renzo@cs.unibo.it>, Antonio Cardace <anto.cardace@gmail.com>
* VirtualSquare team.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dlfcn.h>
#include <errno.h>
#include <service.h>
#include <pthread.h>
#include <config.h>
#include <xcommon.h>
#include <vu_log.h>
#include <syscall_defs.h>
#include <syscall_table.h>
typedef void (* voidfun)(void);
long sys_enosys(void) {
errno = ENOSYS;
return -1;
}
/* This will be prefixed with getent("$HOME") */
#define USER_MODULES_PATH "/.vu/modules"
#define MODULES_EXT ".so"
#ifndef MAX
#define MAX(a, b) ((a > b) ? a : b)
#endif
static inline char *gethomedir(void) {
char *homedir = getenv("HOME");
/* If there is no home directory, use CWD */
if (!homedir)
homedir = ".";
return homedir;
}
/*
* Try to dlopen a module (o submodule) trying different names and locations:
*
* 1) dlopen(modname)
* 2) dlopen(modname.so)
* 3) dlopen(user_vu_plugin_directory/modname)
* 4) dlopen(user_vu_plugin_directory/modname.so)
* 5) dlopen(global_vu_plugin_directory/modname)
* 6) dlopen(global_vu_plugin_directory/modname.so)
*
*/
static void *module_dlopen(const char *modname, int flags)
{
#define TRY_DLOPEN(...) \
{ \
snprintf(testpath, tplen, __VA_ARGS__); \
if ((handle = dlopen(testpath, flags))) { \
return handle; \
} \
}
void *handle;
char *homedir = gethomedir();
int tplen = strlen(modname) + strlen(MODULES_EXT) +
2 + // + 1 is for a '/' and + 1 for \0
MAX(strlen(MODULES_INSTALL_PATH),
strlen(homedir) + strlen(USER_MODULES_PATH));
char testpath[tplen];
if (!modname)
return NULL;
if ((handle = dlopen(modname, flags)))
return handle;
TRY_DLOPEN("%s%s", modname, MODULES_EXT);
TRY_DLOPEN("%s%s/%s", homedir, USER_MODULES_PATH, modname);
TRY_DLOPEN("%s%s/%s%s", homedir, USER_MODULES_PATH, modname, MODULES_EXT);
TRY_DLOPEN("%s/%s", MODULES_INSTALL_PATH, modname);
TRY_DLOPEN("%s/%s%s", MODULES_INSTALL_PATH, modname, MODULES_EXT);
return NULL;
#undef TRY_DLOPEN
}
/* utility function to load sub-modules.
currently it is a forwarding function to module_dlopen.
it has been defined as a specific function to permit
customization in the future. */
void *vu_mod_dlopen(const char *modname, int flags) {
return module_dlopen(modname, flags);
}
struct vu_service_t *module_load(const char *modname)
{
void *handle;
struct vu_module_t *module;
if (!(handle = module_dlopen(modname, RTLD_LAZY | RTLD_GLOBAL))) {
errno = ENOENT;
return NULL;
}
/* populate umview_service_t structure */
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpedantic"
if ((module = dlsym(handle, "vu_module"))) {
#pragma GCC diagnostic pop
struct vu_service_t *service = malloc(sizeof(struct vu_service_t) +
VU_NR_SYSCALLS * sizeof(syscall_t));
int prefixlen = strlen(module->name) + 4;
int fnamelen = prefixlen + VU_SYSCALL_MAX_NAMELEN + 1;
char fname[fnamelen];
int i;
fatal(service);
printkdebug(m, "Loading %s", module->name);
service->mod = module;
service->dlhandle = handle;
service->service_ht = NULL;
service->private = NULL;
snprintf(fname, fnamelen, "vu_%s_",module->name);
for (i = 0; i < VU_NR_MODULE_SYSCALLS; i++) {
strcpy(fname+prefixlen, vu_syscall_names[i]);
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpedantic"
service->module_syscall[i] = dlsym(handle, fname);
#pragma GCC diagnostic pop
if (service->module_syscall[i] == NULL) {
service->module_syscall[i] = sys_enosys;
} else {
printkdebug(m, "%s syscall %s -> %s", module->name, vu_syscall_names[i], fname);
}
}
return service;
} else {
errno = EINVAL;
return NULL;
}
}
syscall_t *vu_syscall_handler_pointer(struct vu_service_t *service, char *name) {
int i;
static syscall_t useless;
for (i = 0; i < VU_NR_MODULE_SYSCALLS; i++) {
if (strcmp(name, vu_syscall_names[i]) == 0)
return &service->module_syscall[i];
}
useless = NULL;
return &useless;
}
void module_unload(struct vu_service_t *service)
{
printkdebug(m, "Unloading %s", service->mod->name);
fatal(service);
xfree(service);
dlclose(service->dlhandle);
}
voidfun *module_getsym(struct vu_service_t *service, char *symbol) {
char symnamelen = strlen(service->mod->name) + strlen(symbol) + 5;
char symname[symnamelen];
snprintf(symname, symnamelen, "vu_%s_%s",service->mod->name, symbol);
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpedantic"
return dlsym(service->dlhandle, symname);
#pragma GCC diagnostic pop
}
void module_run_init(struct vu_service_t *service) {
void * (*init)(void);
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wincompatible-pointer-types"
init = module_getsym(service, "init");
#pragma GCC diagnostic pop
if (init) {
printkdebug(m, "%s running vu_%s_init", service->mod->name, service->mod->name);
service->private = init();
}
}
int module_run_fini(struct vu_service_t *service) {
int (*fini)(void *);
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wincompatible-pointer-types"
fini = module_getsym(service, "fini");
#pragma GCC diagnostic pop
if (fini) {
printkdebug(m, "%s running vu_%s_fini", service->mod->name, service->mod->name);
return fini(service->private);
} else
return 0;
}
__attribute__((constructor))
static void init(void) {
debug_set_name(m, "MODULE");
}