summaryrefslogtreecommitdiffhomepage
path: root/core/src/ffluci/sys.lua
blob: d8fbaa57a05496d3b170e3fd51ee083265dfef87 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
--[[
FFLuCI - System library

Description:
Utilities for interaction with the Linux system

FileId:
$Id$

License:
Copyright 2008 Steven Barth <steven@midlink.org>

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at 

	http://www.apache.org/licenses/LICENSE-2.0 

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

]]--

module("ffluci.sys", package.seeall)
require("posix")

-- Runs "command" and returns its output
function exec(command)
	local pp   = io.popen(command)
	local data = pp:read("*a")
	pp:close()
	
	return data
end

-- Runs "command" and returns its output as a array of lines
function execl(command)
	local pp   = io.popen(command)	
	local line = ""
	local data = {}
	
	while true do
		line = pp:read()
		if (line == nil) then break end
		table.insert(data, line)
	end 
	pp:close()	
	
	return data
end

-- Uses "ffluci-flash" to flash a new image file to the system
function flash(image, kpattern)
	local cmd = "ffluci-flash "
	if kpattern then
		cmd = cmd .. "-k '" .. kapttern:gsub("'", "") .. "' "
	end
	cmd = cmd .. "'" .. image:gsub("'", "") .. "'"
	
	return os.execute(cmd)
end

-- Returns the hostname
function hostname()
	return io.lines("/proc/sys/kernel/hostname")()
end

-- Returns the load average
function loadavg()
	local loadavg = io.lines("/proc/loadavg")()
	return loadavg:match("^(.-) (.-) (.-) (.-) (.-)$")
end

-- Reboots the system
function reboot()
	return os.execute("reboot >/dev/null 2>&1")
end


group = {}
group.getgroup = posix.getgroup

net = {}
-- Returns all available network interfaces
function net.devices()
	local devices = {}
	for line in io.lines("/proc/net/dev") do
		table.insert(devices, line:match(" *(.-):"))
	end
	return devices
end

process = {}
process.info = posix.getpid 

-- Sets the gid of a process
function process.setgroup(pid, gid)
	return posix.setpid("g", pid, gid)
end

-- Sets the uid of a process
function process.setuser(pid, uid)
	return posix.setpid("u", pid, uid)
end

user = {}
-- returns user information to a given uid
user.getuser = posix.getpasswd
	
-- Changes the user password of given user
function user.setpasswd(user, pwd)
	if pwd then
		pwd = pwd:gsub("'", "")
	end
	
	if user then
		user = user:gsub("'", "")
	end
	
	local cmd = "(echo '"..pwd.."';sleep 1;echo '"..pwd.."')|"
	cmd = cmd .. "passwd '"..user.."' >/dev/null 2>&1"
	return os.execute(cmd)
end