From b7275a0fcb85be77c4dfdc953f16adfcaa558723 Mon Sep 17 00:00:00 2001 From: Kamalesh Babulal Date: Tue, 18 Jan 2022 08:35:04 -0700 Subject: [PATCH] api.c: cache cgrules with CGFLAG_USECACHE flag pam_cgroup.so plugin uses /etc/cgrules.conf to assign processes to the requested cgroup. This works well with active cgrulesengd but in the instances where cgrulesengd is disabled, the rules are not read and cached by default. Fix this is by reloading and caching the rules when called with CGFLAG_USECACHE flag. Fixes: https://github.com/libcgroup/libcgroup/issues/84 Reported-by: Kent Overstreet Signed-off-by: Kamalesh Babulal Signed-off-by: Tom Hromatka TJH: Minor commit comment changes (cherry picked from commit 80a0bd4b8873df38060309e775d609dcc219531f) --- src/api.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/api.c b/src/api.c index 88ae1adb..1cadc73f 100644 --- a/src/api.c +++ b/src/api.c @@ -3876,6 +3876,20 @@ int cgroup_change_cgroup_flags(uid_t uid, gid_t gid, goto finished; } + /* + * User had asked to find the matching rule (if one exist) in the + * cached rules but the list might be empty due to the inactive + * cgrulesengd. Lets emulate its behaviour of caching the rules + * by reloading the rules from the configuration file. + */ + if ((flags & CGFLAG_USECACHE) && (rl.head == NULL)) { + cgroup_warn("Warning: no cached rules found, trying to reload " + " from %s.\n", CGRULES_CONF_FILE); + ret = cgroup_reload_cached_rules(); + if (ret != 0) + goto finished; + } + /* * If the user did not ask for cached rules, we must parse the * configuration to find a matching rule (if one exists). Else, we'll -- 2.47.2