summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorNatanael Copa <natanael.copa@gmail.com>2007-11-17 00:10:20 +0000
committerNatanael Copa <natanael.copa@gmail.com>2007-11-17 00:10:20 +0000
commit038245bfbb96cbe4d7c73759bbf0ceb8351d43ba (patch)
treefb246d0a25ca1d72939b33dd76a37a53513c5bb2 /lib
parent0c51db7b65016f0be4259d09fd76fcb69c2cfa23 (diff)
downloadacf-core-038245bfbb96cbe4d7c73759bbf0ceb8351d43ba.tar.bz2
acf-core-038245bfbb96cbe4d7c73759bbf0ceb8351d43ba.tar.xz
added library for pidof
git-svn-id: svn://svn.alpinelinux.org/acf/core/trunk@329 ab2d0c66-481e-0410-8bed-d214d4d58bed
Diffstat (limited to 'lib')
-rw-r--r--lib/Makefile1
-rw-r--r--lib/pidof.lua71
2 files changed, 72 insertions, 0 deletions
diff --git a/lib/Makefile b/lib/Makefile
index d444f24..5fa957c 100644
--- a/lib/Makefile
+++ b/lib/Makefile
@@ -5,6 +5,7 @@ LIB_DIST=fs.lua\
join.lua\
log_view.lua\
menubuilder.lua\
+ pidof.lua\
privsep.lua\
service_controller.lua\
service_model.lua\
diff --git a/lib/pidof.lua b/lib/pidof.lua
new file mode 100644
index 0000000..d05ee14
--- /dev/null
+++ b/lib/pidof.lua
@@ -0,0 +1,71 @@
+
+module(..., package.seeall)
+
+require("posix")
+
+-- 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")
+ local line = f:read()
+ local p = string.match(line, "^%d+ %((%d+)%)")
+ f:close()
+ if p ~= nil and string.len(name) <= 15 and p == name then
+ return true
+ else
+ return false
+ end
+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)..".*", "")
+ return posix.basename(arg0) == name
+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
+