diff options
Diffstat (limited to 'client/privsep.lua')
-rw-r--r-- | client/privsep.lua | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/client/privsep.lua b/client/privsep.lua new file mode 100644 index 0000000..432f472 --- /dev/null +++ b/client/privsep.lua @@ -0,0 +1,57 @@ + +ipcmsg = require("cmsgpack") +ipcmsg.encode = ipcmsg.pack +ipcmsg.decode = ipcmsg.unpack + +client = require("client") + +local ml = require("ml") + +local modules_path = "./modules" + +local privsep = {} + +local function call(conn, req) + local n = client.write(conn.fd, ipcmsg.encode(req)) + retmsg, errmsg = client.recv(conn.fd) + if retmsg then + local data = ipcmsg.decode(retmsg) + return unpack(data.result or {}) + end + return nil, errmsg +end + +local function wrap(conn, modname) + local mod = dofile(modules_path.."/"..modname..".lua") + local f = {} + local i = #conn.mods + 1 + local ret = conn:call{ type = "load", modidx = i, + modname = modname } + if ret == nil then + return nil + end + for k,v in pairs(mod) do + f[k] = function(...) + return conn:call{ type = "call", modidx = i, + func = k, args = {...}} + end + end + conn[i] = f + return f +end + +function privsep.connect(token) + return { + ["fd"] = client.connect(), + ["call"] = call, + ["wrap"] = wrap, + ["close"] = function(self) + return client.close(self.fd) + end, + ["mods"] = {}, + } +end + + +return privsep + |