* Replaced luafilesystem with luaposix library
[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 -- Returns the hostname
31 function hostname()
32         return io.lines("/proc/sys/kernel/hostname")()
33 end
34
35 -- Returns the load average
36 function loadavg()
37         local loadavg = io.lines("/proc/loadavg")()
38         return loadavg:match("^(.-) (.-) (.-) (.-) (.-)$")
39 end
40
41
42 group = {}
43 group.getgroup = posix.getgroup
44
45 net = {}
46 -- Returns all available network interfaces
47 function net.devices()
48         local devices = {}
49         for line in io.lines("/proc/net/dev") do
50                 table.insert(devices, line:match(" *(.-):"))
51         end
52         return devices
53 end
54
55 process = {}
56 process.info = posix.getpid 
57
58 -- Sets the gid of a process
59 function process.setgroup(pid, gid)
60         return posix.setpid("g", pid, gid)
61 end
62
63 -- Sets the uid of a process
64 function process.setuser(pid, uid)
65         return posix.setpid("u", pid, uid)
66 end
67
68 user = {}
69 -- returns user information to a given uid
70 user.getuser = posix.getpasswd
71         
72 -- Changes the user password of given user
73 function user.setpasswd(user, pwd1, pwd2)
74         local cmd = "(echo '"..pwd1.."';sleep 1;echo '"..pwd2.."')|"
75         cmd = cmd .. "passwd "..user.." 2>&1"
76         return ffluci.util.exec(cmd)
77 end