Skip to content

Commit

Permalink
lua: refactor the cgroup API
Browse files Browse the repository at this point in the history
    
This is a simplification which is necessary for adding consistent
support for accessing the parent cgroup.
  • Loading branch information
MaxKellermann committed Dec 4, 2023
1 parent bb7bf9e commit 3903544
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 28 deletions.
2 changes: 1 addition & 1 deletion debian/changelog
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cm4all-qrelay (0.26) unstable; urgency=low

* lua: remove deprecated method "mail:get_cgroup()"
* lua: refactor the cgroup API
* lua: close Lua file descriptors when connection is closed
* lua: fix broken cgroup xattr access

Expand Down
12 changes: 7 additions & 5 deletions doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,14 @@ The following attributes can be queried:

* :samp:`gid`: The client's group id.

* :samp:`cgroup`: The control group path of the client process as
noted in :file:`/proc/self/cgroup`,
e.g. :file:`/user.slice/user-1000.slice/session-42.scope`
* :samp:`cgroup`: The control group of the client process with the
following attributes:

* ``cgroup_xattr``: A table containing extended attributes of the
control group.
* ``path``: the cgroup path as noted in :file:`/proc/self/cgroup`,
e.g. :file:`/user.slice/user-1000.slice/session-42.scope`

* ``xattr``: A table containing extended attributes of the
control group.

The following methods can access more data:

Expand Down
2 changes: 1 addition & 1 deletion libcommon
23 changes: 2 additions & 21 deletions src/LMail.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include "lua/Class.hxx"
#include "lua/Error.hxx"
#include "lua/FenvCache.hxx"
#include "lua/io/XattrTable.hxx"
#include "lua/io/CgroupInfo.hxx"
#include "lua/net/SocketAddress.hxx"
#include "uri/EmailAddress.hxx"
#include "util/StringAPI.hxx"
Expand Down Expand Up @@ -314,26 +314,7 @@ IncomingMail::Index(lua_State *L)
if (path.empty())
return 0;

Lua::Push(L, path);
return 1;
} else if (StringIsEqual(name, "cgroup_xattr")) {
if (!HavePeerCred())
return 0;

const auto path = ReadProcessCgroup(GetPid());
if (path.empty())
return 0;

try {
const auto sys_fs_cgroup = OpenPath("/sys/fs/cgroup");
auto fd = OpenReadOnlyBeneath({sys_fs_cgroup, path.c_str() + 1});
Lua::NewXattrTable(L, std::move(fd));
} catch (...) {
Lua::RaiseCurrent(L);
}

// auto-close the file descriptor when the connection is closed
auto_close->Add(L, Lua::RelativeStackIndex{-1});
Lua::NewCgroupInfo(L, *auto_close, path);

// copy a reference to the fenv (our cache)
Lua::SetFenvCache(L, 1, name, Lua::RelativeStackIndex{-1});
Expand Down
2 changes: 2 additions & 0 deletions src/Main.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "lua/PushCClosure.hxx"
#include "lua/RunFile.hxx"
#include "lua/io/XattrTable.hxx"
#include "lua/io/CgroupInfo.hxx"
#include "lua/net/SocketAddress.hxx"
#include "lua/event/Init.hxx"
#include "lua/pg/Init.hxx"
Expand Down Expand Up @@ -143,6 +144,7 @@ SetupRuntimeState(lua_State *L)
Lua::SetGlobal(L, "qmqp_listen", nullptr);

Lua::InitXattrTable(L);
Lua::RegisterCgroupInfo(L);

QmqpRelayConnection::Register(L);

Expand Down

0 comments on commit 3903544

Please sign in to comment.