summaryrefslogtreecommitdiffstats
path: root/lib/processinfo.lua
blob: dd15fa84597b5e5a65c1f36e81c035c80765ea5c (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

module(..., package.seeall)

require("posix")

function package_version(packagename)
	local cmderrors
	local f = io.popen( "PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin apk_version -vs " .. packagename .." | egrep -v 'acf' 2>/dev/null" )
	local cmdresult = f:read("*l")
	if (cmdresult) and (#cmdresult > 0) then
		cmdresult = (string.match(cmdresult,"^%S*") or "Unknown")
	else
		cmderrors = "Program not installed"
	end	
	f:close()
	return cmdresult,cmderrors
end

function process_botsequence(processname)
	local cmderrors
	local f = io.popen( "/sbin/rc_status | egrep '^S' | egrep '" .. processname .."' 2>/dev/null" )
	local cmdresult = f:read("*a")
	if (cmdresult) and (#cmdresult > 0) then
		cmdresult = "Process will autostart at next boot (at sequence '" .. string.match(cmdresult,"^%a+(%d%d)") .. "')"
	else
		cmderrors = "Not programmed to autostart"
	end	
	f:close()
	return cmdresult,cmderrors
end

function daemoncontrol (process, action)
	local cmdmessage = ""
	if (string.lower(action) == "start") or (string.lower(action) == "stop") or (string.lower(action) == "restart") then
		local file = io.popen( "PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin /etc/init.d/" .. 
			process .. " " .. string.lower(action) .. " 2>&1" )
		if file ~= nil then
			cmdmessage = file:read( "*a" )
			file:close()
		end
	else
		return false,nil,"Unknown command!",action
	end
	posix.sleep(2)	-- Wait for the process to start|stop
	return true,cmdmessage,nil,action
end

-- the following methods are available:
-- /proc/<pid>/stat	the comm field (2nd) field contains name but only up 
--   			to 15 chars. does not resolve links
--
-- /proc/<pid>/cmdline	argv[0] contains the command. However if it is a script
--   			then will the interpreter show up
--
-- /proc/<pid>/exe	link to exe file. this will resolv links
--
-- returns list of all pids for given exe name

--[[
-- gives lots of false positives for busybox
local function is_exe(path, name)
	local f = posix.readlink(path.."/exe")
	if f and (f == name or posix.basename(f) == name) then
		return true
	else
		return false
	end
end
]]--


local function is_stat(path, name)
	local f = io.open(path.."/stat")
	if (f) then
		local line = f:read()
		local p = string.gsub(line, ".*%(", "")
		p = string.gsub(p, "%).*", "")
		f:close()
	end
	if p ~= nil then	
		if string.len(name) <= 15 and p == name then
			return true
		end
	end
	return false
end

local function is_cmdline(path, name)
	local f = io.open(path.."/cmdline")
	if f == nil then
		return false
	end
	local line = f:read()
	f:close()
	if line == nil then 
		return false
	end
	local arg0 = string.gsub(line, string.char(0)..".*", "")
	if posix.basename(arg0) == name then
		return true
	end
end

function pidof(name)
	local pids = {}
	local i, j

	for i,j in pairs(posix.glob("/proc/[0-9]*")) do
		local pid = tonumber(posix.basename(j))
		if is_stat(j, name) or is_cmdline(j, name) then
			table.insert(pids, pid)
		end
	end
	if #pids == 0 then
		pids = nil
	end
	return pids
end