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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
|
/*
* luaplugin - fast lua plugin indexing
* Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2
* as published by the Free Software Foundation
*
* 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.
*/
#include <sys/types.h>
#include <sys/time.h>
#include <sys/cdefs.h>
#ifndef _POSIX_C_SOURCE
#define _POSIX_C_SOURCE /* XXX: portability hack for timestamp */
#endif
#include <sys/stat.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <limits.h>
#include <glob.h>
#include <lualib.h>
#include <lauxlib.h>
#include <lib/list.h>
#include <lib/luaplugin.h>
//#define DEBUG 1
#ifdef DEBUG
#define DPRINTF(...) fprintf(stderr, __VA_ARGS__)
#else
#define DPRINTF(...) do {} while (0)
#endif
/**
* list_for_each_offset - iterate over a list, start with the provided pointer
* @pos: the &struct list_head to use as a loop cursor.
* @head: the head for your list.
*/
#define list_for_each_offset(pos, head, offset) \
for (pos = (offset)->next; pos != (offset); \
pos = ((pos->next == (head)) && ((offset) != (head)) ? (head)->next : pos->next))
static char pbuf[PATH_MAX];
static void load_module(struct luaplugin_ctx *ctx, struct luaplugin_entry *e);
static struct luaplugin_entry *
find_entry(struct luaplugin_ctx *ctx, const char *name, bool modname)
{
struct list_head *p;
if (!ctx->last)
ctx->last = &ctx->entries;
list_for_each_offset(p, &ctx->entries, ctx->last) {
struct luaplugin_entry *e;
const char *cmp;
e = container_of(p, struct luaplugin_entry, list);
if (modname)
cmp = e->module;
else
cmp = e->name;
if (!strcmp(cmp, name))
return e;
}
return NULL;
}
static struct luaplugin_entry *
new_entry(struct luaplugin_ctx *ctx, const char *name, const char *modname)
{
struct luaplugin_entry *e;
char *c;
e = malloc(sizeof(struct luaplugin_entry));
if (!e)
goto error;
memset(e, 0, sizeof(struct luaplugin_entry));
INIT_LIST_HEAD(&e->list);
e->ctx = ctx;
e->loaded = false;
e->name = strdup(name);
if (!e->name)
goto error1;
e->module = strdup(modname);
if (!e->module)
goto error2;
/* strip filename extension */
c = strrchr(e->module, '.');
if (c)
*c = 0;
/* lua namespace: replace / with . */
c = e->module;
while ((c = strchr(c, '/')) != NULL) {
*c = '.';
}
return e;
error2:
free(e->name);
error1:
free(e);
error:
return NULL;
}
static const char *module_loader =
"loader = function (newgt, filename)\n"
" setmetatable(newgt, { __index = _G })\n"
" local f = loadfile(filename)\n"
" if (type(f) == \"function\") then\n"
" setfenv(f, newgt)\n"
" f()\n"
" else\n"
" error(f)\n"
" end\n"
"end\n";
static void
access_plugin_table (lua_State *L, const char *modname, bool set)
{
const char *e;
lua_pushvalue(L, LUA_GLOBALSINDEX);
do {
bool _set = true;
e = strchr(modname, '.');
if (e == NULL) {
e = modname + strlen(modname);
_set = set;
}
lua_pushlstring(L, modname, e - modname);
lua_rawget(L, -2);
if (lua_isnil(L, -1) ||
/* no such field or last field */
(lua_istable(L, -1) && (*e != '.'))) {
lua_pop(L, 1); /* remove this result */
if (_set) {
if (*e != '.')
lua_pushvalue(L, -2); /* use table from given index */
else
lua_createtable(L, 0, 1); /* new table for field */
}
lua_pushlstring(L, modname, e - modname);
if (_set) {
lua_pushvalue(L, -2);
lua_settable(L, -4); /* set new table into field */
} else {
lua_gettable(L, -2);
}
}
else if (!lua_istable(L, -1)) { /* field has a non-table value? */
lua_pop(L, 2 + !!set); /* remove table and values */
return;
}
lua_remove(L, -2); /* remove previous table */
modname = e + 1;
} while (*e == '.');
if (set)
lua_pop(L, 2);
}
static void
load_module(struct luaplugin_ctx *ctx, struct luaplugin_entry *e)
{
lua_State *L = ctx->L;
int ret;
/* grab the loader wrapper function */
ret = luaL_dostring(L, module_loader);
if (ret)
return;
lua_getglobal(L, "loader");
lua_pushnil(L);
lua_setglobal(L, "loader");
e->loaded = true;
e->reload = false;
/* new environment table for function call */
lua_newtable(L);
/* register the table globally */
lua_pushvalue(L, -1);
access_plugin_table(L, e->module, true);
lua_pushstring(L, e->name);
if (lua_pcall(L, 2, 0, 0) != 0) {
const char *err = "unknown error";
if (lua_isstring(L, -1))
err = lua_tostring(L, -1);
fprintf(stderr, err);
}
}
static void
free_entry(struct luaplugin_ctx *ctx, struct luaplugin_entry *e)
{
lua_State *L = ctx->L;
if (e->loaded && L) {
/* allow the gc to free the module */
lua_pushnil(L);
access_plugin_table(L, e->module, true);
}
list_del(&e->list);
free(e->name);
free(e->module);
free(e);
}
static void
__luaplugin_scan(struct luaplugin_ctx *ctx, int base_len, int rec)
{
int gl_flags = GLOB_NOESCAPE | GLOB_NOSORT | GLOB_MARK;
glob_t gl;
int i;
strncpy(pbuf + base_len, "*.lua", PATH_MAX - base_len);
if (glob(pbuf, gl_flags, NULL, &gl) < 0) {
globfree(&gl);
return;
}
for (i = 0; i < gl.gl_pathc; i++) {
const char *entry = gl.gl_pathv[i];
struct luaplugin_entry *e;
struct stat st;
int elen;
elen = strlen(entry);
/* should not happen */
if ((elen <= base_len) || (strncmp(entry, pbuf, base_len) != 0)) {
fprintf(stderr, "[%s] sanity check failed in %s(%d)!\n", __FILE__, __func__, __LINE__);
continue;
}
/* descend into subdirectories */
if (entry[elen - 1] == '/') {
strncpy(pbuf + base_len, entry + base_len, PATH_MAX - base_len);
__luaplugin_scan(ctx, base_len, rec + 1);
pbuf[base_len] = '\0';
continue;
}
if (stat(gl.gl_pathv[i], &st))
continue;
if ((st.st_mode & S_IFMT) != S_IFREG)
continue;
e = find_entry(ctx, entry + base_len, false);
if (!e) {
e = new_entry(ctx, entry, entry + base_len);
list_add_tail(&e->list, &ctx->entries);
}
if (!e)
continue;
e->checked = ctx->checked;
e->reload = (e->timestamp < st.st_mtime);
e->timestamp = st.st_mtime;
}
globfree(&gl);
}
int
luaplugin_call(struct luaplugin_entry *e, int narg)
{
struct luaplugin_ctx *ctx = e->ctx;
lua_State *L = ctx->L;
const char *func;
int ret;
func = luaL_checkstring(L, -1 - narg);
/* grab a reference to the plugin's table */
access_plugin_table(L, e->module, false);
lua_getfield(L, -1, func);
if (!lua_isfunction(L, -1)) {
lua_pop(L, narg + 1);
ret = -ENOENT;
goto done;
}
/* replace function name with a ref to the function */
lua_replace(L, -3 - narg);
/* pop the table */
lua_pop(L, 1);
ret = lua_pcall(L, narg, 0, 0);
if (ret != 0) {
fprintf(stderr, lua_tostring(L, -1));
}
done:
return ret;
}
void
luaplugin_scan(struct luaplugin_ctx *ctx)
{
struct list_head *tmp, *p;
sprintf(pbuf, "%s/", ctx->path);
ctx->checked++;
__luaplugin_scan(ctx, strlen(pbuf), 0);
/* expire old entries */
list_for_each_safe(p, tmp, &ctx->entries) {
struct luaplugin_entry *e = container_of(p, struct luaplugin_entry, list);
if (e->checked < ctx->checked)
free_entry(ctx, e);
else if (e->reload)
load_module(ctx, e);
}
}
int
luaplugin_init(struct luaplugin_ctx *ctx, const char *path)
{
memset(ctx, 0, sizeof(struct luaplugin_ctx));
INIT_LIST_HEAD(&ctx->entries);
ctx->path = path;
ctx->L = luaL_newstate();
if (!ctx->L)
return -ENOMEM;
luaL_openlibs(ctx->L);
/* disable the module functionality, a plugin is restricted to its own environment */
/*
lua_pushcfunction(ctx->L, luaplugin_module);
lua_setfield(ctx->L, LUA_GLOBALSINDEX, "module");
*/
return 0;
}
void
luaplugin_done(struct luaplugin_ctx *ctx)
{
struct list_head *p, *tmp;
lua_close(ctx->L);
ctx->L = NULL;
list_for_each_safe(p, tmp, &ctx->entries) {
struct luaplugin_entry *e;
e = container_of(p, struct luaplugin_entry, list);
free_entry(ctx, e);
}
}
|