diff options
-rw-r--r-- | .gitignore | 2 | ||||
-rw-r--r-- | Makefile | 23 | ||||
-rw-r--r-- | README | 3 | ||||
-rw-r--r-- | lib/e.lua | 18 | ||||
-rw-r--r-- | rc.d/example | 23 | ||||
-rw-r--r-- | runlscript.c | 70 |
6 files changed, 139 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7701b0c --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +runlscript + diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..613bb9d --- /dev/null +++ b/Makefile @@ -0,0 +1,23 @@ + +LIBS=-llua + +scriptdir=/etc/rc.d +sbindir=/sbin +rcluadir=/lib/rc/lua + +INSTALL=install + +rcscripts=rc.d/example +rclualibs=lib/e.lua + +runlscript: runlscript.c + $(CC) $(CFLAGS) $(LDFLAGS) $(LIBS) $< -o $@ + + +install: runlscript $(rcscripts) + $(INSTALL) -d $(DESTDIR)$(scriptdir) $(DESTDIR)$(sbindir) \ + $(DESTDIR)$(rcluadir) + $(INSTALL) -m 755 runlscript $(DESTDIR)$(sbindir) + $(INSTALL) -m 755 $(rcscripts) $(DESTDIR)$(scriptdir) + $(INSTALL) -m 644 $(rclualibs) $(DESTDIR)$(rcluadir) + @@ -0,0 +1,3 @@ +This is a proof of concept of a openrc rewrite in Lua + +The idea is that all boot scripts are written in lua. diff --git a/lib/e.lua b/lib/e.lua new file mode 100644 index 0000000..738a519 --- /dev/null +++ b/lib/e.lua @@ -0,0 +1,18 @@ +module(..., package.seeall) + +function ebegin(msg) + io.write(" * "..msg..": ") +end + + +function eend(status, msg) + local msg + if status then + msg = "ok" + else + msg = "failed" + end + io.write(msg.."\n") + return status +end + diff --git a/rc.d/example b/rc.d/example new file mode 100644 index 0000000..308d904 --- /dev/null +++ b/rc.d/example @@ -0,0 +1,23 @@ +#!/sbin/runlscript + +svcname = "example" + +function depend() + return { + need = { "localmount" } + keyword = { "-prefix", "-vserver" } + } +end + +function start() + e.ebegin("starting "..svcname) + -- start something + return e.eend(true) +end + + +function stop() + e.ebegin("Stopping "..svcname) + return e.eend(true) +end + diff --git a/runlscript.c b/runlscript.c new file mode 100644 index 0000000..5eaef8a --- /dev/null +++ b/runlscript.c @@ -0,0 +1,70 @@ + +#include <sys/mman.h> +#include <sys/stat.h> + +#include <fcntl.h> +#include <stdio.h> +#include <string.h> + +#include <lua.h> +#include <lauxlib.h> +#include <lualib.h> + +#define RCLUAPATH "/lib/rc/lua" + +int load_rclibs(lua_State *L) +{ + int err = luaL_loadstring(L, "package.path = package.path..\";" RCLUAPATH "/?.lua\"\n" + "require(\"e\")\n") + || lua_pcall(L, 0, 0, 0); + if (err) { + fprintf(stderr, "error loading libs: %s\n", + lua_tostring(L, -1)); + lua_pop(L, 1); + return -1; + } +} + +int runfile(lua_State *L, const char *filename) +{ + int err; + err = luaL_loadfile(L, filename) + || lua_pcall(L, 0, 0, 0); + if (err) { + fprintf(stderr, "%s", lua_tostring(L, -1)); + lua_pop(L, 1); + } + return(err); +} + +int main(int argc, char *argv[]) +{ + int err; + lua_State *L = lua_open(); + char *filename = NULL; + char *func = NULL; + + luaL_openlibs(L); + if (argc > 1) + filename = argv[1]; + if (argc > 2) + func = argv[2]; + + if (load_rclibs(L) != 0) + return 1; + + err = runfile(L, filename); + if (err || func == NULL) + return err; + + lua_getglobal(L, func); + if (lua_pcall(L, 0, 1, 0) != 0) { + fprintf(stderr, "error running function '%s': %s\n", func, + lua_tostring(L, -1)); + return -1; + } + err = lua_toboolean(L, -1); + lua_close(L); + return !err; +} + |