From 23938783e3bdde959751b17339fb57ccfc7a80d6 Mon Sep 17 00:00:00 2001 From: Natanael Copa Date: Fri, 11 Jan 2008 08:44:33 +0000 Subject: renamed pidof lib to procps. should be safe since noone is using it git-svn-id: svn://svn.alpinelinux.org/acf/core/trunk@548 ab2d0c66-481e-0410-8bed-d214d4d58bed --- lib/pidof.lua | 71 ---------------------------------------------------------- lib/procps.lua | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 71 deletions(-) delete mode 100644 lib/pidof.lua create mode 100644 lib/procps.lua diff --git a/lib/pidof.lua b/lib/pidof.lua deleted file mode 100644 index d05ee14..0000000 --- a/lib/pidof.lua +++ /dev/null @@ -1,71 +0,0 @@ - -module(..., package.seeall) - -require("posix") - --- the following methods are available: --- /proc//stat the comm field (2nd) field contains name but only up --- to 15 chars. does not resolve links --- --- /proc//cmdline argv[0] contains the command. However if it is a script --- then will the interpreter show up --- --- /proc//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 - diff --git a/lib/procps.lua b/lib/procps.lua new file mode 100644 index 0000000..d05ee14 --- /dev/null +++ b/lib/procps.lua @@ -0,0 +1,71 @@ + +module(..., package.seeall) + +require("posix") + +-- the following methods are available: +-- /proc//stat the comm field (2nd) field contains name but only up +-- to 15 chars. does not resolve links +-- +-- /proc//cmdline argv[0] contains the command. However if it is a script +-- then will the interpreter show up +-- +-- /proc//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 + -- cgit v1.2.3