Added process control page
[project/luci.git] / libs / sys / luasrc / sys.lua
index 14a2f29..6b6ea70 100644 (file)
@@ -27,9 +27,9 @@ limitations under the License.
 --- LuCI Linux and POSIX system utilities.
 module("luci.sys", package.seeall)
 require("posix")
-require("luci.bits")
 require("luci.util")
 require("luci.fs")
+require("luci.ip")
 
 --- Invoke the luci-flash executable to write an image to the flash memory.
 -- @param kpattern     Pattern of files to keep over flash process
@@ -44,6 +44,36 @@ function flash(image, kpattern)
        return os.execute(cmd)
 end
 
+--- Retrieve information about currently mounted file systems.
+-- @return     Table containing mount information
+function mounts()
+       local data = {}
+       local k = {"fs", "blocks", "used", "available", "percent", "mountpoint"}
+       local ps = luci.util.execi("df")
+       
+       if not ps then
+               return
+       else
+               ps()
+       end
+       
+       for line in ps do
+               local row = {}
+               
+               local j = 1
+               for value in line:gmatch("[^%s]+") do
+                       row[k[j]] = value
+                       j = j + 1
+               end
+               
+               if row[k[1]] then
+                       table.insert(data, row)
+               end
+       end
+       
+       return data
+end
+
 --- Retrieve environment variables. If no variable is given then a table
 -- containing the whole environment is returned otherwise this function returns
 -- the corresponding string value for the given name or nil if no such variable
@@ -183,15 +213,6 @@ function net.arptable()
        return _parse_delimited_table(io.lines("/proc/net/arp"), "%s%s+")
 end
 
---- Test whether an IP-Adress belongs to a certain net.
--- @param ip           IPv4 address to test
--- @param ipnet                IPv4 network address of the net range to compare against
--- @param prefix       Network prefix of the net range to compare against
--- @return                     Boolean indicating wheather the ip is within the range
-function net.belongs(ip, ipnet, prefix)
-       return (net.ip4bin(ip):sub(1, prefix) == net.ip4bin(ipnet):sub(1, prefix))
-end
-
 --- Determine the current default route.
 -- @return     Table with the properties of the current default route.
 --                     The following fields are defined:
@@ -220,6 +241,21 @@ function net.devices()
        return devices
 end
 
+
+--- Return information about available network interfaces.
+-- @return     Table containing all current interface names and their information
+function net.deviceinfo()
+       local devices = {}
+       for line in io.lines("/proc/net/dev") do
+               local name, data = line:match("^ *(.-): *(.*)$")
+               if name and data then
+                       devices[name] = luci.util.split(data, " +", nil, true)
+               end
+       end
+       return devices
+end
+
+
 -- Determine the MAC address belonging to the given IP address.
 -- @param ip   IPv4 address
 -- @return             String containing the MAC address or nil if it cannot be found
@@ -235,19 +271,6 @@ function net.ip4mac(ip)
        return mac
 end
 
---- Calculate the prefix from a given netmask.
--- @param mask IPv4 net mask
--- @return             Number containing the corresponding numerical prefix
-function net.mask4prefix(mask)
-       local bin = net.ip4bin(mask)
-
-       if not bin then
-               return nil
-       end
-
-       return #luci.util.split(bin, "1")-1
-end
-
 --- Returns the current kernel routing table entries.
 -- @return     Table of tables with properties of the corresponding routes.
 --                     The following fields are defined for route entry tables:
@@ -257,54 +280,6 @@ function net.routes()
        return _parse_delimited_table(io.lines("/proc/net/route"))
 end
 
---- Convert hexadecimal 32 bit value to IPv4 address.
--- @param hex  String containing the hexadecimal value
--- @param be   Boolean indicating wheather the given value is big endian
--- @return             String containing the corresponding IP4 address
-function net.hexip4(hex, be)
-       if #hex ~= 8 then
-               return nil
-       end
-
-       be = be or luci.util.bigendian()
-
-       local hexdec = luci.bits.Hex2Dec
-
-       local ip = ""
-       if be then
-               ip = ip .. tostring(hexdec(hex:sub(1,2))) .. "."
-               ip = ip .. tostring(hexdec(hex:sub(3,4))) .. "."
-               ip = ip .. tostring(hexdec(hex:sub(5,6))) .. "."
-               ip = ip .. tostring(hexdec(hex:sub(7,8)))
-       else
-               ip = ip .. tostring(hexdec(hex:sub(7,8))) .. "."
-               ip = ip .. tostring(hexdec(hex:sub(5,6))) .. "."
-               ip = ip .. tostring(hexdec(hex:sub(3,4))) .. "."
-               ip = ip .. tostring(hexdec(hex:sub(1,2)))
-       end
-
-       return ip
-end
-
---- Convert given IPv4 address to binary value.
--- @param ip   String containing a IPv4 address
--- @return             String containing corresponding binary value
-function net.ip4bin(ip)
-       local parts = luci.util.split(ip, '.')
-       if #parts ~= 4 then
-               return nil
-       end
-
-       local decbin = luci.bits.Dec2Bin
-
-       local bin = ""
-       bin = bin .. decbin(parts[1], 8)
-       bin = bin .. decbin(parts[2], 8)
-       bin = bin .. decbin(parts[3], 8)
-       bin = bin .. decbin(parts[4], 8)
-
-       return bin
-end
 
 --- Tests whether the given host responds to ping probes.
 -- @param host String containing a hostname or IPv4 address
@@ -323,6 +298,46 @@ process = {}
 -- @return     Number containing the current pid
 process.info = posix.getpid
 
+--- Retrieve information about currently running processes.
+-- @return     Table containing process information
+function process.list()
+       local data = {}
+       local k
+       local ps = luci.util.execi("top -bn1")
+       
+       if not ps then
+               return
+       end
+       
+       while true do
+               local line = ps()
+               if not line then
+                       return
+               end
+               
+               k = luci.util.split(luci.util.trim(line), "%s+", nil, true)
+               if k[1] == "PID" then
+                       break
+               end
+       end
+       
+       for line in ps do
+               local row = {}
+               
+               line = luci.util.trim(line)
+               for i, value in ipairs(luci.util.split(line, "%s+", #k-1, true)) do
+                       row[k[i]] = value
+               end
+               
+               local pid = tonumber(row[k[1]])
+               if pid then
+                       data[pid] = row
+               end
+       end
+       
+       return data
+end
+
 --- Set the gid of a process identified by given pid.
 -- @param pid  Number containing the process id
 -- @param gid  Number containing the Unix group id
@@ -343,6 +358,13 @@ function process.setuser(pid, uid)
        return posix.setpid("u", pid, uid)
 end
 
+--- Send a signal to a process identified by given pid.
+-- @param pid  Number containing the process id
+-- @param sig  Signal to send (default: 15 [SIGTERM])
+-- @return             Boolean indicating successful operation
+-- @return             Number containing the error code if failed
+process.signal = posix.kill
+
 
 --- LuCI system utilities / user related functions.
 -- @class      module
@@ -364,10 +386,7 @@ user.getuser = posix.getpasswd
 function user.checkpasswd(username, password)
        local account = user.getuser(username)
 
-       -- FIXME: detect testing environment
-       if luci.fs.stat("/etc/shadow") and not luci.fs.access("/etc/shadow", "r") then
-               return true
-       elseif account then
+       if account then
                if account.passwd == "!" then
                        return true
                else