diff options
author | Natanael Copa <ncopa@alpinelinux.org> | 2012-12-12 11:04:07 +0100 |
---|---|---|
committer | Natanael Copa <ncopa@alpinelinux.org> | 2012-12-12 11:04:07 +0100 |
commit | 5fef443b54a26a69be9ccdd22b44c62ecb133abc (patch) | |
tree | bcb3118c37451f41a87339990a076eb89ea33133 /lua-privsep.c | |
download | privsep-5fef443b54a26a69be9ccdd22b44c62ecb133abc.tar.bz2 privsep-5fef443b54a26a69be9ccdd22b44c62ecb133abc.tar.xz |
initial prototype
Diffstat (limited to 'lua-privsep.c')
-rw-r--r-- | lua-privsep.c | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/lua-privsep.c b/lua-privsep.c new file mode 100644 index 0000000..2325b65 --- /dev/null +++ b/lua-privsep.c @@ -0,0 +1,58 @@ +#include <stdio.h> + +#include <lua.h> +#include <lauxlib.h> +#include <lualib.h> + +#ifndef PRIVSEP_PATH +#define PRIVSEP_PATH "./" +#endif + +static int traceback (lua_State *L) { + if (!lua_isstring(L, 1)) /* 'message' not a string? */ + return 1; /* keep it intact */ + fprintf(stderr, "traceback\n"); + lua_getfield(L, LUA_GLOBALSINDEX, "debug"); + if (!lua_istable(L, -1)) { + fprintf(stderr, "traceback: debug\n"); + lua_pop(L, 1); + return 1; + } + + lua_getfield(L, -1, "traceback"); + if (!lua_isfunction(L, -1)) { + fprintf(stderr, "traceback: traceback\n"); + lua_pop(L, 2); + return 1; + } + + lua_pushvalue(L, 1); /* pass error message */ + lua_pushinteger(L, 2); /* skip this function and traceback */ + lua_call(L, 2, 1); /* call debug.traceback */ + return 1; +} + +int main(int argc, char *argv[]) +{ + const char *luamain = PRIVSEP_PATH "privsep-main.lua"; + int i, traceback_index; + + lua_State *L = luaL_newstate(); + luaL_openlibs(L); + + lua_pushcfunction(L, traceback); + traceback_index = lua_gettop(L); + + if (luaL_loadfile(L, luamain)) + return luaL_error(L, "%s", luamain); + + for (i = 1; i < argc; i++) + lua_pushstring(L, argv[i]); + + if (lua_pcall(L, argc-1, 0, traceback_index)) + return luaL_error(L, "error"); + + + return 0; +} + |