netlink uevent hello world
This commit is contained in:
parent
b6a054c588
commit
f233acf9ff
4 changed files with 113 additions and 0 deletions
|
@ -82,6 +82,7 @@ in {
|
|||
zyxel-bootconfig = callPackage ./zyxel-bootconfig {};
|
||||
min-collect-garbage = callPackage ./min-collect-garbage {};
|
||||
min-copy-closure = callPackage ./min-copy-closure {};
|
||||
nellie = callPackage ./nellie {};
|
||||
netlink-lua = callPackage ./netlink-lua {};
|
||||
odhcp-script = callPackage ./odhcp-script {};
|
||||
odhcp6c = callPackage ./odhcp6c {};
|
||||
|
|
28
pkgs/nellie/default.nix
Normal file
28
pkgs/nellie/default.nix
Normal file
|
@ -0,0 +1,28 @@
|
|||
{ lua, lib, fetchpatch, fetchFromGitHub, stdenv }:
|
||||
|
||||
let pname = "nellie";
|
||||
in lua.pkgs.buildLuaPackage {
|
||||
inherit pname;
|
||||
version = "0.1.1-1";
|
||||
|
||||
src = ./.;
|
||||
|
||||
buildPhase = "$CC -shared -l lua -o nellie.so nellie.c";
|
||||
|
||||
# for the checks to work you need to
|
||||
# nix-build--option sandbox false
|
||||
# otherwise the sandbox doesn't see any uevent messages
|
||||
|
||||
# doCheck = stdenv.hostPlatform == stdenv.buildPlatform;
|
||||
|
||||
checkPhase = ''
|
||||
export LUA_CPATH=./?.so
|
||||
lua test.lua
|
||||
'';
|
||||
|
||||
installPhase = ''
|
||||
mkdir -p "$out/lib/lua/${lua.luaversion}"
|
||||
cp nellie.so "$out/lib/lua/${lua.luaversion}/"
|
||||
'';
|
||||
|
||||
}
|
79
pkgs/nellie/nellie.c
Normal file
79
pkgs/nellie/nellie.c
Normal file
|
@ -0,0 +1,79 @@
|
|||
#include <lua.h>
|
||||
#include <lualib.h>
|
||||
#include <lauxlib.h>
|
||||
|
||||
#include <linux/netlink.h>
|
||||
#include <sys/socket.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
/* static int l_close_socket(lua_State *L) { */
|
||||
/* LStream *p = (LStream *) luaL_checkudata(L, 1, LUA_FILEHANDLE); */
|
||||
/* int res = fclose(p->f); */
|
||||
/* return luaL_fileresult(L, (res == 0), NULL); */
|
||||
/* } */
|
||||
|
||||
|
||||
static int l_read_from_socket(lua_State *L) {
|
||||
/* struct sockaddr_nl sa; */
|
||||
/* memset(&sa, 0, sizeof(sa)); */
|
||||
int length = 32;
|
||||
|
||||
if(lua_isnumber(L, 2))
|
||||
length = lua_tointeger(L, 2);
|
||||
|
||||
lua_getfield(L, 1, "fileno");
|
||||
int fd = (int) lua_tointeger(L, -1);
|
||||
char *buf = (char *) malloc(length);
|
||||
int bytes = recv(fd, buf, length, 0);
|
||||
|
||||
if(bytes > 0) {
|
||||
lua_pushlstring(L, buf, bytes);
|
||||
free(buf);
|
||||
return 1;
|
||||
} else {
|
||||
free(buf);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
static int l_open_socket(lua_State *L) {
|
||||
int netlink_fd = socket(AF_NETLINK, SOCK_RAW|SOCK_CLOEXEC, NETLINK_KOBJECT_UEVENT);
|
||||
|
||||
struct sockaddr_nl sa;
|
||||
memset(&sa, 0, sizeof(sa));
|
||||
sa.nl_family = AF_NETLINK;
|
||||
sa.nl_pid = getpid();
|
||||
sa.nl_groups = 4; /* rebroadcasts from mdevd */
|
||||
|
||||
if(bind(netlink_fd, (struct sockaddr *) &sa, sizeof(sa))==0) {
|
||||
lua_newtable(L);
|
||||
lua_pushliteral(L, "fileno");
|
||||
lua_pushinteger(L, netlink_fd);
|
||||
lua_settable(L, 1);
|
||||
|
||||
lua_pushliteral(L, "read");
|
||||
lua_pushcfunction(L, l_read_from_socket);
|
||||
lua_settable(L, 1);
|
||||
|
||||
return 1;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static const struct luaL_Reg funcs [] = {
|
||||
{"open", l_open_socket},
|
||||
{NULL, NULL} /* sentinel */
|
||||
};
|
||||
|
||||
|
||||
/* "luaopen_" prefix is magic and tells lua to run this function
|
||||
* when it dlopens the library
|
||||
*/
|
||||
|
||||
int luaopen_nellie (lua_State *L) {
|
||||
luaL_newlib(L, funcs);
|
||||
return 1;
|
||||
}
|
5
pkgs/nellie/test.lua
Normal file
5
pkgs/nellie/test.lua
Normal file
|
@ -0,0 +1,5 @@
|
|||
local nellie = require('nellie')
|
||||
print('dfg')
|
||||
local f = nellie.open()
|
||||
|
||||
print(string.byte(f:read(1000), 0, 60))
|
Loading…
Reference in a new issue