summaryrefslogtreecommitdiff
path: root/redir_scripts
diff options
context:
space:
mode:
authorLauri Leukkunen <lle@rahina.org>2007-01-10 21:09:27 +0200
committerLauri Leukkunen <lleukkun@leka.rahina.org>2007-01-10 21:09:27 +0200
commit5d5d5b9783cab6a232c2564f5594fe7d41b802c0 (patch)
tree354b35f780a3f2d17623a39fec6fd1cff7c96729 /redir_scripts
parenta53075d602be75206e0ace99542380d2f5714232 (diff)
* rework lua scripts to work a bit like iptables chains
Diffstat (limited to 'redir_scripts')
-rw-r--r--redir_scripts/main.lua88
-rw-r--r--redir_scripts/preload/basic_chains.lua58
-rw-r--r--redir_scripts/preload/default.lua87
3 files changed, 140 insertions, 93 deletions
diff --git a/redir_scripts/main.lua b/redir_scripts/main.lua
index 262aa9e..1f55d39 100644
--- a/redir_scripts/main.lua
+++ b/redir_scripts/main.lua
@@ -27,41 +27,46 @@ if (rsdir == nil) then
rsdir = "/scratchbox/redir_scripts"
end
-rules = {}
+chains = {}
-- sb.sb_getdirlisting is provided by lua_bindings.c
-- it returns a table listing all files in a directory
t = sb.sb_getdirlisting(rsdir .. "/preload")
if (t ~= nil) then
+ local i = 0
+ local r = 0
table.sort(t)
-- load the individual parts ($SBOX_REDIR_SCRIPTS/parts/*.lua)
for n = 1,table.maxn(t) do
if (string.match(t[n], "%a*%.lua$")) then
-- print("loading part: " .. t[n])
- filename = rsdir .. "/parts/" .. t[n]
+ filename = rsdir .. "/preload/" .. t[n]
f, err = loadfile(filename)
if (f == nil) then
error("\nError while loading " .. filename .. ": \n" .. err .. "\n")
else
f() -- execute the loaded chunk
- -- export_rules variable contains now the rules
+ -- export_chains variable contains now the chains
-- from the chunk
- for i = 1,table.maxn(export_rules) do
- -- print("loading rule:" .. export_rules[i].binary)
+ for i = 1,table.maxn(export_chains) do
+ --print("loading chain:" .. export_chains[i].binary)
-- fill in the default values
- if (not export_rules[i].binary) then
- export_rules[i].binary = ".*"
+ if (not export_chains[i].binary) then
+ export_chains[i].binary = ".*"
end
- if (not export_rules[i].func_name) then
- export_rules[i].func_name = ".*"
+ -- loop through the rules
+ for r = 1, table.maxn(export_chains[i].rules) do
+ if (not export_chains[i].rules[r].func_name) then
+ export_chains[i].rules[r].func_name = ".*"
+ end
+ if (not export_chains[i].rules[r].path) then
+ -- this is an error, report and exit
+ print("path not specified for a rule in " .. filename)
+ os.exit(1)
+ end
end
- if (not export_rules[i].path) then
- -- this is an error, report and exit
- print("path not specified for a rule in " .. filename)
- os.exit(1)
- end
- table.insert(rules, export_rules[i])
+ table.insert(chains, export_chains[i])
end
end
end
@@ -118,6 +123,23 @@ function sbox_map_to(binary_name, func_name, work_dir, rp, path, rule)
end
+function find_rule(chain, func, path)
+ local i = 0
+ local wrk = chain
+ while (wrk) do
+ -- travel the chains
+ for i = 1, table.maxn(wrk.rules) do
+ -- loop the rules in a chain
+ if (string.match(func, wrk.rules[i].func_name) and
+ string.match(path, wrk.rules[i].path)) then
+ return wrk.rules[i]
+ end
+ end
+ wrk = wrk.next
+ end
+ return nil
+end
+
-- sbox_translate_path is the function called from libsb2.so
-- preload library and the FUSE system for each path that needs
-- translating
@@ -126,35 +148,31 @@ function sbox_translate_path(binary_name, func_name, work_dir, path)
--print(string.format("[%s]:", binary_name))
--print(string.format("debug: [%s][%s][%s][%s]", binary_name, func_name, work_dir, path))
- ret = path
- rp = path
-
--- if (rp == "no such file") then
--- rp = path
--- end
+ local ret = path
+ local rp = path
+ local rule = nil
--- if (string.sub(rp, 1, 1) ~= "/") then
--- -- relative path, convert to absolute
--- rp = work_dir .. "/" .. rp
--- end
-
- -- loop through the rules, first match is used
- for n=1,table.maxn(rules) do
+ -- loop through the chains, first match is used
+ for n=1,table.maxn(chains) do
-- print(string.format("looping through rules: %s, %s, %s", rules[n].binary, rules[n].func_name, rules[n].path))
- if (string.match(binary_name, rules[n].binary) and
- string.match(func_name, rules[n].func_name) and
- string.match(rp, rules[n].path)) then
- if (rules[n].custom_map_func ~= nil) then
- return rules[n].custom_map_func(binary_name, func_name, work_dir, rp, path, rules[n])
+ if (string.match(binary_name, chains[n].binary)) then
+ rule = find_rule(chains[n], func_name, rp)
+ if (not rule) then
+ -- error, not even a default rule found
+ --print(string.format("Unable to find a match at all: [%s][%s][%s]", binary_name, func_name, path))
+ return path
+ end
+ if (rule.custom_map_func ~= nil) then
+ return rule.custom_map_func(binary_name, func_name, work_dir, rp, path, rules[n])
else
- ret = sbox_map_to(binary_name, func_name, work_dir, rp, path, rules[n])
+ ret = sbox_map_to(binary_name, func_name, work_dir, rp, path, rule)
--print(string.format("[%i]%s: %s(%s) -> [%s]", n, binary_name, func_name, path, ret))
return ret
end
end
end
- -- fail safe, if none matched, map
+ -- we should never ever get here, if we still do, map
return target_root .. rp
end
diff --git a/redir_scripts/preload/basic_chains.lua b/redir_scripts/preload/basic_chains.lua
new file mode 100644
index 0000000..1701335
--- /dev/null
+++ b/redir_scripts/preload/basic_chains.lua
@@ -0,0 +1,58 @@
+-- Copyright (C) 2007 Lauri Leukkunen <lle@rahina.org>
+
+install = {
+ next = default_chain,
+ binary = "^install$",
+ rules = {
+ {path = ".*", map_to = "="}
+ }
+}
+
+ln = {
+ next = default_chain,
+ binary = "^ln$",
+ rules = {
+ {path = ".*", map_to = "="}
+ }
+}
+
+cp = {
+ next = default_chain,
+ binary = "^cp$",
+ rules = {
+ {path = ".*", map_to = "="}
+ }
+}
+
+rm = {
+ next = default_chain,
+ binary = "^rm$",
+ rules = {
+ {path = ".*", map_to = "="}
+ }
+}
+
+qemu = {
+ next = default_chain,
+ binary = ".*qemu.*",
+ rules = {
+ {path = "^/", map_to = "="}
+ }
+}
+
+perl = {
+ next = default_chain,
+ binary = ".*perl.*",
+ rules = {
+ {path = "^/usr/lib/perl.*", map_to = nil}
+ }
+}
+
+export_chains = {
+ install,
+ ln,
+ cp,
+ rm,
+ qemu,
+ perl
+}
diff --git a/redir_scripts/preload/default.lua b/redir_scripts/preload/default.lua
index 97d1fcd..7343c1f 100644
--- a/redir_scripts/preload/default.lua
+++ b/redir_scripts/preload/default.lua
@@ -1,4 +1,4 @@
--- Copyright (C) 2006 Lauri Leukkunen <lle@rahina.org>
+-- Copyright (C) 2006,2007 Lauri Leukkunen <lle@rahina.org>
-- Licensed under so called MIT license.
-- print "hello from sample.lua!\n"
@@ -68,29 +68,6 @@ default_dev = {
path = "^/dev",
}
-install = {
- binary = "^install$",
- path = ".*",
- map_to = "="
-}
-
-ln = {
- binary = "^ln$",
- path = ".*",
- map_to = "="
-}
-
-cp = {
- binary = "^cp$",
- path = ".*",
- map_to = "="
-}
-
-rm = {
- binary = "^rm$",
- path = ".*",
- map_to = "="
-}
libtool = {
func_name = "exec.*",
@@ -108,12 +85,6 @@ ltdlm4 = {
map_to = "+/arch_tools/share/aclocal"
}
-qemu = {
- binary = ".*qemu.*",
- path = "^/",
- map_to = "="
-}
-
autoconf = {
path = "^/usr/share/autoconf.*"
}
@@ -126,10 +97,6 @@ aclocal = {
path = "^/usr/share/aclocal.*"
}
-perl = {
- binary = ".*perl.*",
- path = "^/usr/lib/perl.*"
-}
hostgcc = {
path = "^/host_usr",
@@ -143,29 +110,33 @@ default_rootdir = {
map_to = "=",
}
-export_rules = {
- ln,
- install,
- cp,
- rm,
- libtool,
- libtoolm4,
- ltdlm4,
- qemu,
- autoconf,
- automake,
- aclocal,
- perl,
- default_bin,
- default_usrbin,
- default_usrlocalbin,
- default_scratchbox,
- default_dev,
- default_home,
- default_proc,
- default_tmp,
- default_etc,
- hostgcc,
- default_rootdir
+
+-- the actual chain
+default_chain = {
+ next = nil,
+ binary = nil,
+ rules = {
+ libtool,
+ libtoolm4,
+ ltdlm4,
+ autoconf,
+ automake,
+ aclocal,
+ default_bin,
+ default_usrbin,
+ default_usrlocalbin,
+ default_scratchbox,
+ default_dev,
+ default_home,
+ default_proc,
+ default_tmp,
+ default_etc,
+ hostgcc,
+ default_rootdir
+ }
+}
+
+export_chains = {
+ default_chain
}