5 Utilities for interaction with the Linux system
11 Copyright 2008 Steven Barth <steven@midlink.org>
13 Licensed under the Apache License, Version 2.0 (the "License");
14 you may not use this file except in compliance with the License.
15 You may obtain a copy of the License at
17 http://www.apache.org/licenses/LICENSE-2.0
19 Unless required by applicable law or agreed to in writing, software
20 distributed under the License is distributed on an "AS IS" BASIS,
21 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 See the License for the specific language governing permissions and
23 limitations under the License.
28 local io = require "io"
29 local os = require "os"
30 local table = require "table"
31 local nixio = require "nixio"
32 local fs = require "nixio.fs"
33 local iwinfo = require "iwinfo"
36 luci.util = require "luci.util"
37 luci.ip = require "luci.ip"
39 local tonumber, ipairs, pairs, pcall, type, next, setmetatable =
40 tonumber, ipairs, pairs, pcall, type, next, setmetatable
43 --- LuCI Linux and POSIX system utilities.
46 --- Execute a given shell command and return the error code
49 -- @param ... Command to call
50 -- @return Error code of the command
52 return os.execute(...) / 256
55 --- Execute a given shell command and capture its standard output
58 -- @param command Command to call
59 -- @return String containg the return the output of the command
62 --- Invoke the luci-flash executable to write an image to the flash memory.
63 -- @param image Local path or URL to image file
64 -- @param kpattern Pattern of files to keep over flash process
65 -- @return Return value of os.execute()
66 function flash(image, kpattern)
67 local cmd = "luci-flash "
69 cmd = cmd .. "-k '" .. kpattern:gsub("'", "") .. "' "
71 cmd = cmd .. "'" .. image:gsub("'", "") .. "' >/dev/null 2>&1"
73 return os.execute(cmd)
76 --- Retrieve information about currently mounted file systems.
77 -- @return Table containing mount information
80 local k = {"fs", "blocks", "used", "available", "percent", "mountpoint"}
81 local ps = luci.util.execi("df")
93 for value in line:gmatch("[^%s]+") do
100 -- this is a rather ugly workaround to cope with wrapped lines in
103 -- /dev/scsi/host0/bus0/target0/lun0/part3
104 -- 114382024 93566472 15005244 86% /mnt/usb
107 if not row[k[2]] then
110 for value in line:gmatch("[^%s]+") do
116 table.insert(data, row)
123 --- Retrieve environment variables. If no variable is given then a table
124 -- containing the whole environment is returned otherwise this function returns
125 -- the corresponding string value for the given name or nil if no such variable
129 -- @param var Name of the environment variable to retrieve (optional)
130 -- @return String containg the value of the specified variable
131 -- @return Table containing all variables if no variable name is given
132 getenv = nixio.getenv
134 --- Get or set the current hostname.
135 -- @param String containing a new hostname to set (optional)
136 -- @return String containing the system hostname
137 function hostname(newname)
138 if type(newname) == "string" and #newname > 0 then
139 fs.writefile( "/proc/sys/kernel/hostname", newname )
142 return nixio.uname().nodename
146 --- Returns the contents of a documented referred by an URL.
147 -- @param url The URL to retrieve
148 -- @param stream Return a stream instead of a buffer
149 -- @param target Directly write to target file name
150 -- @return String containing the contents of given the URL
151 function httpget(url, stream, target)
153 local source = stream and io.popen or luci.util.exec
154 return source("wget -qO- '"..url:gsub("'", "").."'")
156 return os.execute("wget -qO '%s' '%s'" %
157 {target:gsub("'", ""), url:gsub("'", "")})
161 --- Returns the system load average values.
162 -- @return String containing the average load value 1 minute ago
163 -- @return String containing the average load value 5 minutes ago
164 -- @return String containing the average load value 15 minutes ago
166 local info = nixio.sysinfo()
167 return info.loads[1], info.loads[2], info.loads[3]
170 --- Initiate a system reboot.
171 -- @return Return value of os.execute()
173 return os.execute("reboot >/dev/null 2>&1")
176 --- Returns the system type, cpu name and installed physical memory.
177 -- @return String containing the system or platform identifier
178 -- @return String containing hardware model information
179 -- @return String containing the total memory amount in kB
180 -- @return String containing the memory used for caching in kB
181 -- @return String containing the memory used for buffering in kB
182 -- @return String containing the free memory amount in kB
184 local cpuinfo = fs.readfile("/proc/cpuinfo")
185 local meminfo = fs.readfile("/proc/meminfo")
187 local system = cpuinfo:match("system typ.-:%s*([^\n]+)")
189 local memtotal = tonumber(meminfo:match("MemTotal:%s*(%d+)"))
190 local memcached = tonumber(meminfo:match("\nCached:%s*(%d+)"))
191 local memfree = tonumber(meminfo:match("MemFree:%s*(%d+)"))
192 local membuffers = tonumber(meminfo:match("Buffers:%s*(%d+)"))
195 system = nixio.uname().machine
196 model = cpuinfo:match("model name.-:%s*([^\n]+)")
198 model = cpuinfo:match("Processor.-:%s*([^\n]+)")
201 model = cpuinfo:match("cpu model.-:%s*([^\n]+)")
204 return system, model, memtotal, memcached, membuffers, memfree
207 --- Retrieves the output of the "logread" command.
208 -- @return String containing the current log buffer
210 return luci.util.exec("logread")
213 --- Retrieves the output of the "dmesg" command.
214 -- @return String containing the current log buffer
216 return luci.util.exec("dmesg")
219 --- Generates a random id with specified length.
220 -- @param bytes Number of bytes for the unique id
221 -- @return String containing hex encoded id
222 function uniqueid(bytes)
223 local rand = fs.readfile("/dev/urandom", bytes)
224 return rand and nixio.bin.hexlify(rand)
227 --- Returns the current system uptime stats.
228 -- @return String containing total uptime in seconds
230 return nixio.sysinfo().uptime
234 --- LuCI system utilities / network related functions.
236 -- @name luci.sys.net
239 --- Returns the current arp-table entries as two-dimensional table.
240 -- @return Table of table containing the current arp entries.
241 -- The following fields are defined for arp entry objects:
242 -- { "IP address", "HW address", "HW type", "Flags", "Mask", "Device" }
243 function net.arptable(callback)
244 return _parse_delimited_table(io.lines("/proc/net/arp"), "%s%s+", callback)
247 --- Returns conntrack information
248 -- @return Table with the currently tracked IP connections
249 function net.conntrack(callback)
251 if fs.access("/proc/net/nf_conntrack", "r") then
252 for line in io.lines("/proc/net/nf_conntrack") do
253 line = line:match "^(.-( [^ =]+=).-)%2"
254 local entry, flags = _parse_mixed_record(line, " +")
255 entry.layer3 = flags[1]
256 entry.layer4 = flags[3]
264 connt[#connt+1] = entry
267 elseif fs.access("/proc/net/ip_conntrack", "r") then
268 for line in io.lines("/proc/net/ip_conntrack") do
269 line = line:match "^(.-( [^ =]+=).-)%2"
270 local entry, flags = _parse_mixed_record(line, " +")
271 entry.layer3 = "ipv4"
272 entry.layer4 = flags[1]
280 connt[#connt+1] = entry
289 --- Determine the current IPv4 default route. If multiple default routes exist,
290 -- return the one with the lowest metric.
291 -- @return Table with the properties of the current default route.
292 -- The following fields are defined:
293 -- { "dest", "gateway", "metric", "refcount", "usecount", "irtt",
294 -- "flags", "device" }
295 function net.defaultroute()
298 net.routes(function(rt)
299 if rt.dest:prefix() == 0 and (not route or route.metric > rt.metric) then
307 --- Determine the current IPv6 default route. If multiple default routes exist,
308 -- return the one with the lowest metric.
309 -- @return Table with the properties of the current default route.
310 -- The following fields are defined:
311 -- { "source", "dest", "nexthop", "metric", "refcount", "usecount",
312 -- "flags", "device" }
313 function net.defaultroute6()
316 net.routes6(function(rt)
317 if rt.dest:prefix() == 0 and (not route or route.metric > rt.metric) then
325 --- Determine the names of available network interfaces.
326 -- @return Table containing all current interface names
327 function net.devices()
329 for k, v in ipairs(nixio.getifaddrs()) do
330 if v.family == "packet" then
331 devs[#devs+1] = v.name
338 --- Return information about available network interfaces.
339 -- @return Table containing all current interface names and their information
340 function net.deviceinfo()
342 for k, v in ipairs(nixio.getifaddrs()) do
343 if v.family == "packet" then
368 -- Determine the MAC address belonging to the given IP address.
369 -- @param ip IPv4 address
370 -- @return String containing the MAC address or nil if it cannot be found
371 function net.ip4mac(ip)
373 net.arptable(function(e)
374 if e["IP address"] == ip then
375 mac = e["HW address"]
381 --- Returns the current kernel routing table entries.
382 -- @return Table of tables with properties of the corresponding routes.
383 -- The following fields are defined for route entry tables:
384 -- { "dest", "gateway", "metric", "refcount", "usecount", "irtt",
385 -- "flags", "device" }
386 function net.routes(callback)
389 for line in io.lines("/proc/net/route") do
391 local dev, dst_ip, gateway, flags, refcnt, usecnt, metric,
392 dst_mask, mtu, win, irtt = line:match(
393 "([^%s]+)\t([A-F0-9]+)\t([A-F0-9]+)\t([A-F0-9]+)\t" ..
394 "(%d+)\t(%d+)\t(%d+)\t([A-F0-9]+)\t(%d+)\t(%d+)\t(%d+)"
398 gateway = luci.ip.Hex( gateway, 32, luci.ip.FAMILY_INET4 )
399 dst_mask = luci.ip.Hex( dst_mask, 32, luci.ip.FAMILY_INET4 )
400 dst_ip = luci.ip.Hex(
401 dst_ip, dst_mask:prefix(dst_mask), luci.ip.FAMILY_INET4
407 metric = tonumber(metric),
408 refcount = tonumber(refcnt),
409 usecount = tonumber(usecnt),
411 window = tonumber(window),
412 irtt = tonumber(irtt),
413 flags = tonumber(flags, 16),
420 routes[#routes+1] = rt
428 --- Returns the current ipv6 kernel routing table entries.
429 -- @return Table of tables with properties of the corresponding routes.
430 -- The following fields are defined for route entry tables:
431 -- { "source", "dest", "nexthop", "metric", "refcount", "usecount",
432 -- "flags", "device" }
433 function net.routes6(callback)
434 if fs.access("/proc/net/ipv6_route", "r") then
437 for line in io.lines("/proc/net/ipv6_route") do
439 local dst_ip, dst_prefix, src_ip, src_prefix, nexthop,
440 metric, refcnt, usecnt, flags, dev = line:match(
441 "([a-f0-9]+) ([a-f0-9]+) " ..
442 "([a-f0-9]+) ([a-f0-9]+) " ..
443 "([a-f0-9]+) ([a-f0-9]+) " ..
444 "([a-f0-9]+) ([a-f0-9]+) " ..
445 "([a-f0-9]+) +([^%s]+)"
448 src_ip = luci.ip.Hex(
449 src_ip, tonumber(src_prefix, 16), luci.ip.FAMILY_INET6, false
452 dst_ip = luci.ip.Hex(
453 dst_ip, tonumber(dst_prefix, 16), luci.ip.FAMILY_INET6, false
456 nexthop = luci.ip.Hex( nexthop, 128, luci.ip.FAMILY_INET6, false )
462 metric = tonumber(metric, 16),
463 refcount = tonumber(refcnt, 16),
464 usecount = tonumber(usecnt, 16),
465 flags = tonumber(flags, 16),
472 routes[#routes+1] = rt
480 --- Tests whether the given host responds to ping probes.
481 -- @param host String containing a hostname or IPv4 address
482 -- @return Number containing 0 on success and >= 1 on error
483 function net.pingtest(host)
484 return os.execute("ping -c1 '"..host:gsub("'", '').."' >/dev/null 2>&1")
488 --- LuCI system utilities / process related functions.
490 -- @name luci.sys.process
493 --- Get the current process id.
495 -- @name process.info
496 -- @return Number containing the current pid
497 function process.info(key)
498 local s = {uid = nixio.getuid(), gid = nixio.getgid()}
499 return not key and s or s[key]
502 --- Retrieve information about currently running processes.
503 -- @return Table containing process information
504 function process.list()
507 local ps = luci.util.execi("top -bn1")
519 k = luci.util.split(luci.util.trim(line), "%s+", nil, true)
520 if k[1] == "PID" then
528 line = luci.util.trim(line)
529 for i, value in ipairs(luci.util.split(line, "%s+", #k-1, true)) do
533 local pid = tonumber(row[k[1]])
542 --- Set the gid of a process identified by given pid.
543 -- @param gid Number containing the Unix group id
544 -- @return Boolean indicating successful operation
545 -- @return String containing the error message if failed
546 -- @return Number containing the error code if failed
547 function process.setgroup(gid)
548 return nixio.setgid(gid)
551 --- Set the uid of a process identified by given pid.
552 -- @param uid Number containing the Unix user id
553 -- @return Boolean indicating successful operation
554 -- @return String containing the error message if failed
555 -- @return Number containing the error code if failed
556 function process.setuser(uid)
557 return nixio.setuid(uid)
560 --- Send a signal to a process identified by given pid.
562 -- @name process.signal
563 -- @param pid Number containing the process id
564 -- @param sig Signal to send (default: 15 [SIGTERM])
565 -- @return Boolean indicating successful operation
566 -- @return Number containing the error code if failed
567 process.signal = nixio.kill
570 --- LuCI system utilities / user related functions.
572 -- @name luci.sys.user
575 --- Retrieve user informations for given uid.
578 -- @param uid Number containing the Unix user id
579 -- @return Table containing the following fields:
580 -- { "uid", "gid", "name", "passwd", "dir", "shell", "gecos" }
581 user.getuser = nixio.getpw
583 --- Test whether given string matches the password of a given system user.
584 -- @param username String containing the Unix user name
585 -- @param pass String containing the password to compare
586 -- @return Boolean indicating wheather the passwords are equal
587 function user.checkpasswd(username, pass)
588 local pwe = nixio.getsp and nixio.getsp(username) or nixio.getpw(username)
589 local pwh = pwe and (pwe.pwdp or pwe.passwd)
590 if not pwh or #pwh < 1 or pwh ~= "!" and nixio.crypt(pass, pwh) ~= pwh then
597 --- Change the password of given user.
598 -- @param username String containing the Unix user name
599 -- @param password String containing the password to compare
600 -- @return Number containing 0 on success and >= 1 on error
601 function user.setpasswd(username, password)
603 password = password:gsub("'", "")
607 username = username:gsub("'", "")
610 local cmd = "(echo '"..password.."';sleep 1;echo '"..password.."')|"
611 cmd = cmd .. "passwd '"..username.."' >/dev/null 2>&1"
612 return os.execute(cmd)
616 --- LuCI system utilities / wifi related functions.
618 -- @name luci.sys.wifi
621 --- Get wireless information for given interface.
622 -- @param ifname String containing the interface name
623 -- @return A wrapped iwinfo object instance
624 function wifi.getiwinfo(ifname)
625 local t = iwinfo.type(ifname)
628 return setmetatable({}, {
629 __index = function(t, k)
630 if x[k] then return x[k](ifname) end
636 --- Get iwconfig output for all wireless devices.
637 -- @return Table of tables containing the iwconfing output for each wifi device
638 function wifi.getiwconfig()
639 local cnt = luci.util.exec("PATH=/sbin:/usr/sbin iwconfig 2>/dev/null")
642 for i, l in pairs(luci.util.split(luci.util.trim(cnt), "\n\n")) do
643 local k = l:match("^(.-) ")
644 l = l:gsub("^(.-) +", "", 1)
646 local entry, flags = _parse_mixed_record(l)
657 --- Get iwlist scan output from all wireless devices.
658 -- @return Table of tables contaiing all scan results
659 function wifi.iwscan(iface)
660 local siface = iface or ""
661 local cnt = luci.util.exec("iwlist "..siface.." scan 2>/dev/null")
664 for i, l in pairs(luci.util.split(luci.util.trim(cnt), "\n\n")) do
665 local k = l:match("^(.-) ")
666 l = l:gsub("^[^\n]+", "", 1)
667 l = luci.util.trim(l)
670 for j, c in pairs(luci.util.split(l, "\n Cell")) do
671 c = c:gsub("^(.-)- ", "", 1)
672 c = luci.util.split(c, "\n", 7)
673 c = table.concat(c, "\n", 1)
674 local entry, flags = _parse_mixed_record(c)
678 table.insert(iws[k], entry)
683 return iface and (iws[iface] or {}) or iws
686 --- Get available channels from given wireless iface.
687 -- @param iface Wireless interface (optional)
688 -- @return Table of available channels
689 function wifi.channels(iface)
690 local t = iwinfo.type(iface)
692 if t and iwinfo[t] then
693 cns = iwinfo[t].freqlist(iface)
696 if not cns or #cns == 0 then
698 {channel = 1, mhz = 2.412},
699 {channel = 2, mhz = 2.417},
700 {channel = 3, mhz = 2.422},
701 {channel = 4, mhz = 2.427},
702 {channel = 5, mhz = 2.432},
703 {channel = 6, mhz = 2.437},
704 {channel = 7, mhz = 2.442},
705 {channel = 8, mhz = 2.447},
706 {channel = 9, mhz = 2.452},
707 {channel = 10, mhz = 2.457},
708 {channel = 11, mhz = 2.462}
716 --- LuCI system utilities / init related functions.
718 -- @name luci.sys.init
720 init.dir = "/etc/init.d/"
722 --- Get the names of all installed init scripts
723 -- @return Table containing the names of all inistalled init scripts
724 function init.names()
726 for name in fs.glob(init.dir.."*") do
727 names[#names+1] = fs.basename(name)
732 --- Test whether the given init script is enabled
733 -- @param name Name of the init script
734 -- @return Boolean indicating whether init is enabled
735 function init.enabled(name)
736 if fs.access(init.dir..name) then
737 return ( call(init.dir..name.." enabled") == 0 )
742 --- Get the index of he given init script
743 -- @param name Name of the init script
744 -- @return Numeric index value
745 function init.index(name)
746 if fs.access(init.dir..name) then
747 return call("source "..init.dir..name.."; exit $START")
751 --- Enable the given init script
752 -- @param name Name of the init script
753 -- @return Boolean indicating success
754 function init.enable(name)
755 if fs.access(init.dir..name) then
756 return ( call(init.dir..name.." enable") == 1 )
760 --- Disable the given init script
761 -- @param name Name of the init script
762 -- @return Boolean indicating success
763 function init.disable(name)
764 if fs.access(init.dir..name) then
765 return ( call(init.dir..name.." disable") == 0 )
770 -- Internal functions
772 function _parse_delimited_table(iter, delimiter, callback)
773 delimiter = delimiter or "%s+"
776 local trim = luci.util.trim
777 local split = luci.util.split
779 local keys = split(trim(iter()), delimiter, nil, true)
780 for i, j in pairs(keys) do
781 keys[i] = trim(keys[i])
788 for i, j in pairs(split(line, delimiter, nil, true)) do
805 function _parse_mixed_record(cnt, delimiter)
806 delimiter = delimiter or " "
810 for i, l in pairs(luci.util.split(luci.util.trim(cnt), "\n")) do
811 for j, f in pairs(luci.util.split(luci.util.trim(l), delimiter, nil, true)) do
812 local k, x, v = f:match('([^%s][^:=]*) *([:=]*) *"*([^\n"]*)"*')
816 table.insert(flags, k)