allow the sgi-webuci prepare_req function to reload the lua context if necessary (not used yet)

This commit is contained in:
Felix Fietkau 2008-06-04 00:01:21 +00:00
parent 18efb3f75b
commit f7fab0d54f
2 changed files with 36 additions and 16 deletions

View file

@ -47,6 +47,7 @@ function prepare_req(uri)
luci.dispatcher.createindex() luci.dispatcher.createindex()
env = {} env = {}
env.REQUEST_URI = uri env.REQUEST_URI = uri
-- TODO: setting luci-plugin.reload = true allows this function to trigger a context reload
end end
function handle_req(context) function handle_req(context)

View file

@ -27,56 +27,63 @@ static lua_State *L = NULL;
extern int luci_parse_header (lua_State *L); extern int luci_parse_header (lua_State *L);
static int luci_init(struct httpd_plugin *p) static lua_State *luci_context_init(struct httpd_plugin *p)
{ {
char *path = NULL; char *path = NULL;
lua_State *Lnew;
int ret = 0; int ret = 0;
L = luaL_newstate(); Lnew = luaL_newstate();
if (!L) if (!Lnew)
goto error; goto error;
luaL_openlibs(L); luaL_openlibs(Lnew);
path = malloc(strlen(p->dir) + sizeof(LUAMAIN) + 2); path = malloc(strlen(p->dir) + sizeof(LUAMAIN) + 2);
strcpy(path, p->dir); strcpy(path, p->dir);
strcat(path, "/" LUAMAIN); strcat(path, "/" LUAMAIN);
ret = luaL_dofile(L, path); ret = luaL_dofile(Lnew, path);
lua_getfield(L, LUA_GLOBALSINDEX, "luci-plugin"); lua_getfield(Lnew, LUA_GLOBALSINDEX, "luci-plugin");
do { do {
if (!lua_istable(L, -1)) { if (!lua_istable(Lnew, -1)) {
ret = 1; ret = 1;
break; break;
} }
lua_getfield(L, -1, "init"); lua_getfield(Lnew, -1, "init");
if (!lua_isfunction(L, -1)) if (!lua_isfunction(Lnew, -1))
break; break;
lua_pushstring(L, p->dir); lua_pushstring(Lnew, p->dir);
ret = lua_pcall(L, 1, 0, 0); ret = lua_pcall(Lnew, 1, 0, 0);
} while (0); } while (0);
free(path); free(path);
if (ret != 0) if (ret != 0)
goto error; goto error;
return 1; return Lnew;
error: error:
fprintf(stderr, "Error: "); fprintf(stderr, "Error: ");
if (L) { if (Lnew) {
const char *s = lua_tostring(L, -1); const char *s = lua_tostring(Lnew, -1);
if (!s) if (!s)
s = "unknown error"; s = "unknown error";
fprintf(stderr, "%s\n", s); fprintf(stderr, "%s\n", s);
lua_close(L); lua_close(Lnew);
} else { } else {
fprintf(stderr, "Out of memory!\n"); fprintf(stderr, "Out of memory!\n");
} }
return 0; return NULL;
}
static int luci_init(struct httpd_plugin *p)
{
L = luci_context_init(p);
return (L != NULL);
} }
static void pushvar(char *name, char *val) static void pushvar(char *name, char *val)
@ -116,8 +123,20 @@ done:
static int luci_prepare_req(struct httpd_plugin *p, struct http_context *ctx) static int luci_prepare_req(struct httpd_plugin *p, struct http_context *ctx)
{ {
int ret; int ret;
bool reload = false;
lua_getglobal(L, "luci-plugin"); lua_getglobal(L, "luci-plugin");
lua_getfield(L, -1, "reload");
if (lua_isboolean(L, -1))
reload = lua_toboolean(L, -1);
lua_pop(L, 1);
if (reload) {
lua_close(L);
L = luci_context_init(p);
lua_getglobal(L, "luci-plugin");
}
lua_getfield(L, -1, "prepare_req"); lua_getfield(L, -1, "prepare_req");
ret = lua_isfunction(L, -1); ret = lua_isfunction(L, -1);