File: cgroup.lua

package info (click to toggle)
lua-ljsyscall 0.12-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,732 kB
  • sloc: ansic: 434; sh: 59; makefile: 3
file content (54 lines) | stat: -rw-r--r-- 1,246 bytes parent folder | download | duplicates (3)
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
-- Linux cgroup API
-- this is all file system operations packaged up to be easier to use

local require, error, assert, tonumber, tostring,
setmetatable, pairs, ipairs, unpack, rawget, rawset,
pcall, type, table, string = 
require, error, assert, tonumber, tostring,
setmetatable, pairs, ipairs, unpack, rawget, rawset,
pcall, type, table, string

local function init(S)

local h = require "syscall.helpers"
local split = h.split

local abi, types, c = S.abi, S.types, S.c
local t, pt, s = types.t, types.pt, types.s

local util = S.util

local cgroup = {}

local function mkgroup(name)
  -- append default location, should be tmpfs mount
  if name:sub(1, 1) ~= "/" then return "/sys/fs/cgroup" .. name else return name end
end

function cgroup.mount(tab)
  tab.source = tab.source or "cgroup"
  tab.type = "cgroup"
  tab.target = mkgroup(tab.target)
  return S.mount(tab)
end

function cgroup.cgroups(ps)
  ps = tostring(ps or "self")
  local cgf = util.readfile("/proc/" .. ps .. "/cgroup")
  local lines = split("\n", cgf)
  local cgroups = {}
  for i = 1, #lines - 1 do
    local parts = split( ":", lines[i])
    cgroups[parts[1]] = {name = parts[2], path = parts[3]}
  end
  return cgroups
end

return cgroup

end

return {init = init}