* Rewritten menu builder
[project/luci.git] / src / ffluci / sys.lua
1 --[[
2 FFLuCI - System library
3
4 Description:
5 Utilities for interaction with the Linux system
6
7 FileId:
8 $Id$
9
10 License:
11 Copyright 2008 Steven Barth <steven@midlink.org>
12
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 
16
17         http://www.apache.org/licenses/LICENSE-2.0 
18
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.
24
25 ]]--
26
27 module("ffluci.sys", package.seeall)
28 require("posix")
29
30 -- Runs "command" and returns its output
31 function exec(command)
32         local pp   = io.popen(command)
33         local data = pp:read("*a")
34         pp:close()
35         
36         return data
37 end
38
39 -- Runs "command" and returns its output as a array of lines
40 function execl(command)
41         local pp   = io.popen(command)  
42         local line = ""
43         local data = {}
44         
45         while true do
46                 line = pp:read()
47                 if (line == nil) then break end
48                 table.insert(data, line)
49         end 
50         pp:close()      
51         
52         return data
53 end
54
55 -- Returns the hostname
56 function hostname()
57         return io.lines("/proc/sys/kernel/hostname")()
58 end
59
60 -- Returns the load average
61 function loadavg()
62         local loadavg = io.lines("/proc/loadavg")()
63         return loadavg:match("^(.-) (.-) (.-) (.-) (.-)$")
64 end
65
66 -- Reboots the system
67 function reboot()
68         return os.execute("reboot >/dev/null 2>&1")
69 end
70
71
72 group = {}
73 group.getgroup = posix.getgroup
74
75 net = {}
76 -- Returns all available network interfaces
77 function net.devices()
78         local devices = {}
79         for line in io.lines("/proc/net/dev") do
80                 table.insert(devices, line:match(" *(.-):"))
81         end
82         return devices
83 end
84
85 process = {}
86 process.info = posix.getpid 
87
88 -- Sets the gid of a process
89 function process.setgroup(pid, gid)
90         return posix.setpid("g", pid, gid)
91 end
92
93 -- Sets the uid of a process
94 function process.setuser(pid, uid)
95         return posix.setpid("u", pid, uid)
96 end
97
98 user = {}
99 -- returns user information to a given uid
100 user.getuser = posix.getpasswd
101         
102 -- Changes the user password of given user
103 function user.setpasswd(user, pwd)
104         if pwd then
105                 pwd = pwd:gsub("'", "")
106         end
107         
108         if user then
109                 user = user:gsub("'", "")
110         end
111         
112         local cmd = "(echo '"..pwd.."';sleep 1;echo '"..pwd.."')|"
113         cmd = cmd .. "passwd '"..user.."' >/dev/null 2>&1"
114         return os.execute(cmd)
115 end