diff options
author | Natanael Copa <ncopa@alpinelinux.org> | 2011-09-22 13:49:53 +0200 |
---|---|---|
committer | Natanael Copa <ncopa@alpinelinux.org> | 2011-09-22 13:50:30 +0200 |
commit | 8a901ee2447e2a2f4f3abdd57bd67dbe5c575812 (patch) | |
tree | ce00370b3933745891d0c58c49c77efc5b48b5c9 | |
parent | f1d03e76ef1a192002f226c747fa2003c2e96f0b (diff) | |
download | pingu-8a901ee2447e2a2f4f3abdd57bd67dbe5c575812.tar.bz2 pingu-8a901ee2447e2a2f4f3abdd57bd67dbe5c575812.tar.xz |
lua: added initial lua client
-rw-r--r-- | Makefile | 32 | ||||
-rw-r--r-- | lua-client.c | 77 | ||||
-rw-r--r-- | pingu.lua | 39 |
3 files changed, 141 insertions, 7 deletions
@@ -1,5 +1,5 @@ -TARGETS = mtu pingu pinguctl +TARGETS = mtu pingu pinguctl client.so VERSION = 0.5 prefix = /usr @@ -7,17 +7,23 @@ localstatedir = /var rundir = $(localstatedir)/run pingustatedir = $(rundir)/pingu +luasharedir = /usr/share/lua/5.1 +lualibdir = /usr/lib/lua/5.1 + BINDIR = $(prefix)/bin DESTDIR ?= INSTALL = install INSTALLDIR = $(INSTALL) -d +PKG_CONFIG ?= pkg-config + + CFLAGS ?= -g CFLAGS += -DPINGU_VERSION=\"$(VERSION)\" CFLAGS += -Wall -Wstrict-prototypes -D_GNU_SOURCE -std=gnu99 CFLAGS += -DDEFAULT_PIDFILE=\"$(pingustatedir)/pingu.pid\" -CFLAGS += -DDEFAULT_ADM_SOCKET=\"$(pingustatedir)/pingu.ctl\" +CFLAGS += -DDEFAULT_ADM_client=\"$(pingustatedir)/pingu.ctl\" pingu_OBJS = \ icmp.o \ @@ -47,21 +53,33 @@ mtu_OBJS = \ netlink.o \ icmp.o -ALL_OBJS= $(pingu_OBJS) $(pinguctl_OBJS) $(mtu_OBJS) +client.so_OBJS = \ + lua-client.o + +client.so_LIBS = $(shell $(PKG_CONFIG) --libs lua) +client.so_LDFLAGS = -shared + +ALL_OBJS= $(pingu_OBJS) $(pinguctl_OBJS) $(mtu_OBJS) $(client.so_OBJS) all: $(TARGETS) -pingu: $(pingu_OBJS) - $(CC) $(LDFLAGS) $(pingu_OBJS) $(pingu_LIBS) -o $@ +$(TARGETS): + $(CC) $(LDFLAGS) $($@_LDFLAGS) $($@_OBJS) $($@_LIBS) -o $@ +pingu: $(pingu_OBJS) pinguctl: $(pinguctl_OBJS) - $(CC) $(LDFLAGS) $(pinguctl_OBJS) $(pinguctl_LIBS) -o $@ - +client.so: $(client.so_OBJS) mtu: $(mtu_OBJS) install: $(TARGETS) $(INSTALLDIR) $(DESTDIR)/$(BINDIR) $(DESTDIR)/$(pingustatedir) $(INSTALL) $(TARGETS) $(DESTDIR)/$(BINDIR) +install-lua: client.so pingu.lua + $(INSTALLDIR) $(DESTDIR)$(luasharedir) \ + $(DESTDIR)$(lualibdir)/pingu + $(INSTALL) pingu.lua $(DESTDIR)$(luasharedir)/ + $(INSTALL) client.so $(DESTDIR)$(lualibdir)/pingu/ + clean: rm -f $(TARGETS) $(ALL_OBJS) diff --git a/lua-client.c b/lua-client.c new file mode 100644 index 0000000..9004dbf --- /dev/null +++ b/lua-client.c @@ -0,0 +1,77 @@ + +#include <sys/socket.h> +#include <sys/un.h> + +#include <errno.h> +#include <stdio.h> +#include <unistd.h> + +#include <lua.h> +#include <lualib.h> +#include <lauxlib.h> + +#include "pingu_adm.h" + +#define LIBNAME "pingu.client" + +static int pusherror(lua_State *L, const char *info) +{ + lua_pushnil(L); + if (info == NULL) + lua_pushstring(L, strerror(errno)); + else + lua_pushfstring(L, "%s: %s", info, strerror(errno)); + lua_pushinteger(L, errno); + return 3; +} + +static int pushfile(lua_State *L, int fd, const char *mode) +{ + FILE **f = (FILE **)lua_newuserdata(L, sizeof(FILE *)); + *f = NULL; + luaL_getmetatable(L, "FILE*"); + lua_setmetatable(L, -2); + *f = fdopen(fd, mode); + return (*f != NULL); +} + +static int Padm_open(lua_State *L) +{ + const char *socket_path = luaL_optstring(L, 1, DEFAULT_ADM_SOCKET); + struct sockaddr_un sun; + int fd, ret; + + memset(&sun, 0, sizeof(sun)); + sun.sun_family = AF_UNIX; + strncpy(sun.sun_path, socket_path, sizeof(sun.sun_path)); + + fd = socket(AF_UNIX, SOCK_STREAM, 0); + if (fd < 0) + return pusherror(L, "socket"); + + if (connect(fd, (struct sockaddr *) &sun, sizeof(sun)) < 0) { + ret = pusherror(L, socket_path); + goto close_err; + } + + return pushfile(L, fd, "r+"); + +close_err: + close(fd); + return ret; +} + +static const luaL_reg reg_pingu_methods[] = { + {"open", Padm_open}, + {NULL, NULL}, +}; + + +LUALIB_API int luaopen_pingu_client(lua_State *L) +{ + luaL_register(L, LIBNAME, reg_pingu_methods); + lua_pushliteral(L, "version"); + lua_pushliteral(L, PINGU_VERSION); + lua_settable(L, -3); + return 1; +} diff --git a/pingu.lua b/pingu.lua new file mode 100644 index 0000000..02c3bb6 --- /dev/null +++ b/pingu.lua @@ -0,0 +1,39 @@ + +module(..., package.seeall) + + +local function status(self) + self.handle:write("status\n") + self.handle:flush() + + local t = {} + local line = self.handle:read("*line") + while line ~= "" do + local host, status = string.match(line, "^(.*): (.*)$") + t[host] = status + line = self.handle:read("*line") + end + return t +end + +local function close(self) + return self.handle:close() +end + +function connect(socket_path) + local socket = require("pingu.client") + local fh, err + if socket ~= nil then + fh, err = socket.open(socket_path) + end + if fh == nil then + return fh, err + end + return { + ["handle"] = fh, + ["status"] = status, + ["close"] = close + } +end + + |