* Completed Firmware-Upgrade page
[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 -- Uses "ffluci-flash" to flash a new image file to the system
56 function flash(image, kpattern)
57 local cmd = "ffluci-flash "
58 if kpattern then
59 cmd = cmd .. "-k '" .. kapttern:gsub("'", "") .. "' "
60 end
61 cmd = cmd .. "'" .. image:gsub("'", "") .. "'"
62
63 return os.execute(cmd)
64 end
65
66 -- Returns the hostname
67 function hostname()
68 return io.lines("/proc/sys/kernel/hostname")()
69 end
70
71 -- Returns the load average
72 function loadavg()
73 local loadavg = io.lines("/proc/loadavg")()
74 return loadavg:match("^(.-) (.-) (.-) (.-) (.-)$")
75 end
76
77 -- Reboots the system
78 function reboot()
79 return os.execute("reboot >/dev/null 2>&1")
80 end
81
82
83 group = {}
84 group.getgroup = posix.getgroup
85
86 net = {}
87 -- Returns all available network interfaces
88 function net.devices()
89 local devices = {}
90 for line in io.lines("/proc/net/dev") do
91 table.insert(devices, line:match(" *(.-):"))
92 end
93 return devices
94 end
95
96 process = {}
97 process.info = posix.getpid
98
99 -- Sets the gid of a process
100 function process.setgroup(pid, gid)
101 return posix.setpid("g", pid, gid)
102 end
103
104 -- Sets the uid of a process
105 function process.setuser(pid, uid)
106 return posix.setpid("u", pid, uid)
107 end
108
109 user = {}
110 -- returns user information to a given uid
111 user.getuser = posix.getpasswd
112
113 -- Changes the user password of given user
114 function user.setpasswd(user, pwd)
115 if pwd then
116 pwd = pwd:gsub("'", "")
117 end
118
119 if user then
120 user = user:gsub("'", "")
121 end
122
123 local cmd = "(echo '"..pwd.."';sleep 1;echo '"..pwd.."')|"
124 cmd = cmd .. "passwd '"..user.."' >/dev/null 2>&1"
125 return os.execute(cmd)
126 end