1 -- Copyright 2008 Steven Barth <steven@midlink.org>
2 -- Licensed to the public under the Apache License 2.0.
4 local io = require "io"
5 local os = require "os"
6 local table = require "table"
7 local nixio = require "nixio"
8 local fs = require "nixio.fs"
9 local uci = require "luci.model.uci"
10 local ntm = require "luci.model.network"
13 luci.util = require "luci.util"
14 luci.ip = require "luci.ip"
16 local tonumber, ipairs, pairs, pcall, type, next, setmetatable, require, select, unpack =
17 tonumber, ipairs, pairs, pcall, type, next, setmetatable, require, select, unpack
23 return os.execute(...) / 256
30 local k = {"fs", "blocks", "used", "available", "percent", "mountpoint"}
31 local ps = luci.util.execi("df")
43 for value in line:gmatch("[^%s]+") do
50 -- this is a rather ugly workaround to cope with wrapped lines in
53 -- /dev/scsi/host0/bus0/target0/lun0/part3
54 -- 114382024 93566472 15005244 86% /mnt/usb
60 for value in line:gmatch("[^%s]+") do
66 table.insert(data, row)
76 if fs.access("/proc/mtd") then
77 for l in io.lines("/proc/mtd") do
78 local d, s, e, n = l:match('^([^%s]+)%s+([^%s]+)%s+([^%s]+)%s+"([^%s]+)"')
81 d.size = tonumber(s, 16)
91 -- containing the whole environment is returned otherwise this function returns
92 -- the corresponding string value for the given name or nil if no such variable
96 function hostname(newname)
97 if type(newname) == "string" and #newname > 0 then
98 fs.writefile( "/proc/sys/kernel/hostname", newname )
101 return nixio.uname().nodename
105 function httpget(url, stream, target)
107 local source = stream and io.popen or luci.util.exec
108 return source("wget -qO- %s" % luci.util.shellquote(url))
110 return os.execute("wget -qO %s %s" %
111 {luci.util.shellquote(target), luci.util.shellquote(url)})
116 return os.execute("reboot >/dev/null 2>&1")
120 return luci.util.exec("logread")
124 return luci.util.exec("dmesg")
127 function uniqueid(bytes)
128 local rand = fs.readfile("/dev/urandom", bytes)
129 return rand and nixio.bin.hexlify(rand)
133 return nixio.sysinfo().uptime
139 local function _nethints(what, callback)
140 local _, k, e, mac, ip, name
141 local cur = uci.cursor()
146 local function _add(i, ...)
147 local k = select(i, ...)
149 if not hosts[k] then hosts[k] = { } end
150 hosts[k][1] = select(1, ...) or hosts[k][1]
151 hosts[k][2] = select(2, ...) or hosts[k][2]
152 hosts[k][3] = select(3, ...) or hosts[k][3]
153 hosts[k][4] = select(4, ...) or hosts[k][4]
157 luci.ip.neighbors(nil, function(neigh)
158 if neigh.mac and neigh.family == 4 then
159 _add(what, neigh.mac:string(), neigh.dest:string(), nil, nil)
160 elseif neigh.mac and neigh.family == 6 then
161 _add(what, neigh.mac:string(), nil, neigh.dest:string(), nil)
165 if fs.access("/etc/ethers") then
166 for e in io.lines("/etc/ethers") do
167 mac, name = e:match("^([a-fA-F0-9:-]+)%s+(%S+)")
168 mac = luci.ip.checkmac(mac)
170 if luci.ip.checkip4(name) then
171 _add(what, mac, name, nil, nil)
173 _add(what, mac, nil, nil, name)
179 cur:foreach("dhcp", "dnsmasq",
181 if s.leasefile and fs.access(s.leasefile) then
182 for e in io.lines(s.leasefile) do
183 mac, ip, name = e:match("^%d+ (%S+) (%S+) (%S+)")
184 mac = luci.ip.checkmac(mac)
186 _add(what, mac, ip, nil, name ~= "*" and name)
193 cur:foreach("dhcp", "host",
195 for mac in luci.util.imatch(s.mac) do
196 mac = luci.ip.checkmac(mac)
198 _add(what, mac, s.ip, nil, s.name)
203 for _, e in ipairs(nixio.getifaddrs()) do
204 if e.name ~= "lo" then
205 ifn[e.name] = ifn[e.name] or { }
206 if e.family == "packet" and e.addr and #e.addr == 17 then
207 ifn[e.name][1] = e.addr:upper()
208 elseif e.family == "inet" then
209 ifn[e.name][2] = e.addr
210 elseif e.family == "inet6" then
211 ifn[e.name][3] = e.addr
216 for _, e in pairs(ifn) do
217 if e[what] and (e[2] or e[3]) then
218 _add(what, e[1], e[2], e[3], e[4])
222 for _, e in pairs(hosts) do
223 lookup[#lookup+1] = (what > 1) and e[what] or (e[2] or e[3])
227 lookup = luci.util.ubus("network.rrdns", "lookup", {
234 for _, e in luci.util.kspairs(hosts) do
235 callback(e[1], e[2], e[3], lookup[e[2]] or lookup[e[3]] or e[4])
239 -- Each entry contains the values in the following order:
241 function net.mac_hints(callback)
243 _nethints(1, function(mac, v4, v6, name)
245 if name and name ~= mac then
246 callback(mac, name or v4)
251 _nethints(1, function(mac, v4, v6, name)
253 if name and name ~= mac then
254 rv[#rv+1] = { mac, name or v4 }
261 -- Each entry contains the values in the following order:
263 function net.ipv4_hints(callback)
265 _nethints(2, function(mac, v4, v6, name)
267 if name and name ~= v4 then
273 _nethints(2, function(mac, v4, v6, name)
275 if name and name ~= v4 then
276 rv[#rv+1] = { v4, name }
283 -- Each entry contains the values in the following order:
285 function net.ipv6_hints(callback)
287 _nethints(3, function(mac, v4, v6, name)
289 if name and name ~= v6 then
295 _nethints(3, function(mac, v4, v6, name)
297 if name and name ~= v6 then
298 rv[#rv+1] = { v6, name }
305 function net.host_hints(callback)
307 _nethints(1, function(mac, v4, v6, name)
308 if mac and mac ~= "00:00:00:00:00:00" and (v4 or v6 or name) then
309 callback(mac, v4, v6, name)
314 _nethints(1, function(mac, v4, v6, name)
315 if mac and mac ~= "00:00:00:00:00:00" and (v4 or v6 or name) then
317 if v4 then e.ipv4 = v4 end
318 if v6 then e.ipv6 = v6 end
319 if name then e.name = name end
327 function net.conntrack(callback)
328 local ok, nfct = pcall(io.lines, "/proc/net/nf_conntrack")
329 if not ok or not nfct then
333 local line, connt = nil, (not callback) and { }
335 local fam, l3, l4, timeout, tuples =
336 line:match("^(ipv[46]) +(%d+) +%S+ +(%d+) +(%d+) +(.+)$")
338 if fam and l3 and l4 and timeout and not tuples:match("^TIME_WAIT ") then
339 l4 = nixio.getprotobynumber(l4)
345 layer4 = l4 and l4.name or "unknown",
346 timeout = tonumber(timeout, 10)
350 for key, val in tuples:gmatch("(%w+)=(%S+)") do
351 if key == "bytes" or key == "packets" then
352 entry[key] = entry[key] + tonumber(val, 10)
353 elseif key == "src" or key == "dst" then
354 if entry[key] == nil then
355 entry[key] = luci.ip.new(val):string()
357 elseif key == "sport" or key == "dport" then
358 if entry[key] == nil then
369 connt[#connt+1] = entry
374 return callback and true or connt
377 function net.devices()
380 for k, v in ipairs(nixio.getifaddrs()) do
381 if v.name and not seen[v.name] then
383 devs[#devs+1] = v.name
392 function process.info(key)
393 local s = {uid = nixio.getuid(), gid = nixio.getgid()}
394 return not key and s or s[key]
397 function process.list()
400 local ps = luci.util.execi("/bin/busybox top -bn1")
407 local pid, ppid, user, stat, vsz, mem, cpu, cmd = line:match(
408 "^ *(%d+) +(%d+) +(%S.-%S) +([RSDZTW][W ][<N ]) +(%d+) +(%d+%%) +(%d+%%) +(.+)"
411 local idx = tonumber(pid)
429 function process.setgroup(gid)
430 return nixio.setgid(gid)
433 function process.setuser(uid)
434 return nixio.setuid(uid)
437 process.signal = nixio.kill
439 local function xclose(fd)
440 if fd and fd:fileno() > 2 then
445 function process.exec(command, stdout, stderr, nowait)
446 local out_r, out_w, err_r, err_w
447 if stdout then out_r, out_w = nixio.pipe() end
448 if stderr then err_r, err_w = nixio.pipe() end
450 local pid = nixio.fork()
454 local null = nixio.open("/dev/null", "w+")
456 nixio.dup(out_w or null, nixio.stdout)
457 nixio.dup(err_w or null, nixio.stderr)
458 nixio.dup(null, nixio.stdin)
466 nixio.exec(unpack(command))
470 local _, pfds, rv = nil, {}, { code = -1, pid = pid }
478 cb = type(stdout) == "function" and stdout,
480 events = nixio.poll_flags("in", "err", "hup")
487 cb = type(stderr) == "function" and stderr,
489 events = nixio.poll_flags("in", "err", "hup")
494 local nfds, err = nixio.poll(pfds, -1)
495 if not nfds and err ~= nixio.const.EINTR then
500 for i = #pfds, 1, -1 do
502 if rfd.revents > 0 then
503 local chunk, err = rfd.fd:read(4096)
504 if chunk and #chunk > 0 then
508 rfd.buf = rfd.buf or {}
509 rfd.buf[#rfd.buf + 1] = chunk
512 table.remove(pfds, i)
514 rv[rfd.name] = table.concat(rfd.buf, "")
523 _, _, rv.code = nixio.waitpid(pid)
532 -- { "uid", "gid", "name", "passwd", "dir", "shell", "gecos" }
533 user.getuser = nixio.getpw
535 function user.getpasswd(username)
536 local pwe = nixio.getsp and nixio.getsp(username) or nixio.getpw(username)
537 local pwh = pwe and (pwe.pwdp or pwe.passwd)
538 if not pwh or #pwh < 1 or pwh == "!" or pwh == "x" then
545 function user.checkpasswd(username, pass)
546 local pwh, pwe = user.getpasswd(username)
548 return (pwh == nil or nixio.crypt(pass, pwh) == pwh)
553 function user.setpasswd(username, password)
554 return os.execute("(echo %s; sleep 1; echo %s) | passwd %s >/dev/null 2>&1" %{
555 luci.util.shellquote(password),
556 luci.util.shellquote(password),
557 luci.util.shellquote(username)
564 function wifi.getiwinfo(ifname)
567 local wnet = ntm:get_wifinet(ifname)
568 if wnet and wnet.iwinfo then
572 local wdev = ntm:get_wifidev(ifname)
573 if wdev and wdev.iwinfo then
577 return { ifname = ifname }
582 init.dir = "/etc/init.d/"
584 function init.names()
586 for name in fs.glob(init.dir.."*") do
587 names[#names+1] = fs.basename(name)
592 function init.index(name)
593 if fs.access(init.dir..name) then
594 return call("env -i sh -c 'source %s%s enabled; exit ${START:-255}' >/dev/null"
599 local function init_action(action, name)
600 if fs.access(init.dir..name) then
601 return call("env -i %s%s %s >/dev/null" %{ init.dir, name, action })
605 function init.enabled(name)
606 return (init_action("enabled", name) == 0)
609 function init.enable(name)
610 return (init_action("enable", name) == 1)
613 function init.disable(name)
614 return (init_action("disable", name) == 0)
617 function init.start(name)
618 return (init_action("start", name) == 0)
621 function init.stop(name)
622 return (init_action("stop", name) == 0)