mirror of
https://github.com/Dasharo/systemd.git
synced 2026-03-06 15:02:31 -08:00
util-lib: move some functions from basic/cgroup-util to shared/cgroup-setup
This way less stuff needs to be in basic. Initially, I wanted to move all the parts of cgroup-utils.[ch] that depend on efivars.[ch] to shared, because efivars.[ch] is in shared/. Later on, I decide to split efivars.[ch], so the move done in this patch is not necessary anymore. Nevertheless, it is still valid on its own. If at some point we want to expose libbasic, it is better to to not have stuff that belong in libshared there.
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -172,10 +172,6 @@ typedef int (*cg_kill_log_func_t)(pid_t pid, int sig, void *userdata);
|
||||
int cg_kill(const char *controller, const char *path, int sig, CGroupFlags flags, Set *s, cg_kill_log_func_t kill_log, void *userdata);
|
||||
int cg_kill_recursive(const char *controller, const char *path, int sig, CGroupFlags flags, Set *s, cg_kill_log_func_t kill_log, void *userdata);
|
||||
|
||||
int cg_migrate(const char *cfrom, const char *pfrom, const char *cto, const char *pto, CGroupFlags flags);
|
||||
int cg_migrate_recursive(const char *cfrom, const char *pfrom, const char *cto, const char *pto, CGroupFlags flags);
|
||||
int cg_migrate_recursive_fallback(const char *cfrom, const char *pfrom, const char *cto, const char *pto, CGroupFlags flags);
|
||||
|
||||
int cg_split_spec(const char *spec, char **controller, char **path);
|
||||
int cg_mangle_path(const char *path, char **result);
|
||||
|
||||
@@ -184,15 +180,8 @@ int cg_get_path_and_check(const char *controller, const char *path, const char *
|
||||
|
||||
int cg_pid_get_path(const char *controller, pid_t pid, char **path);
|
||||
|
||||
int cg_trim(const char *controller, const char *path, bool delete_root);
|
||||
|
||||
int cg_rmdir(const char *controller, const char *path);
|
||||
|
||||
int cg_create(const char *controller, const char *path);
|
||||
int cg_attach(const char *controller, const char *path, pid_t pid);
|
||||
int cg_attach_fallback(const char *controller, const char *path, pid_t pid);
|
||||
int cg_create_and_attach(const char *controller, const char *path, pid_t pid);
|
||||
|
||||
int cg_set_attribute(const char *controller, const char *path, const char *attribute, const char *value);
|
||||
int cg_get_attribute(const char *controller, const char *path, const char *attribute, char **ret);
|
||||
int cg_get_keyed_attribute(const char *controller, const char *path, const char *attribute, char **keys, char **values);
|
||||
@@ -240,13 +229,6 @@ int cg_slice_to_path(const char *unit, char **ret);
|
||||
|
||||
typedef const char* (*cg_migrate_callback_t)(CGroupMask mask, void *userdata);
|
||||
|
||||
int cg_create_everywhere(CGroupMask supported, CGroupMask mask, const char *path);
|
||||
int cg_attach_everywhere(CGroupMask supported, const char *path, pid_t pid, cg_migrate_callback_t callback, void *userdata);
|
||||
int cg_attach_many_everywhere(CGroupMask supported, const char *path, Set* pids, cg_migrate_callback_t callback, void *userdata);
|
||||
int cg_migrate_everywhere(CGroupMask supported, const char *from, const char *to, cg_migrate_callback_t callback, void *userdata);
|
||||
int cg_trim_everywhere(CGroupMask supported, const char *path, bool delete_root);
|
||||
int cg_enable_everywhere(CGroupMask supported, CGroupMask mask, const char *p, CGroupMask *ret_result_mask);
|
||||
|
||||
int cg_mask_supported(CGroupMask *ret);
|
||||
int cg_mask_from_string(const char *s, CGroupMask *ret);
|
||||
int cg_mask_to_string(CGroupMask mask, char **ret);
|
||||
@@ -263,16 +245,8 @@ static inline int cg_unified(void) {
|
||||
return cg_unified_cached(true);
|
||||
}
|
||||
|
||||
bool cg_is_unified_wanted(void);
|
||||
bool cg_is_legacy_wanted(void);
|
||||
bool cg_is_hybrid_wanted(void);
|
||||
|
||||
const char* cgroup_controller_to_string(CGroupController c) _const_;
|
||||
CGroupController cgroup_controller_from_string(const char *s) _pure_;
|
||||
|
||||
int cg_weight_parse(const char *s, uint64_t *ret);
|
||||
int cg_cpu_shares_parse(const char *s, uint64_t *ret);
|
||||
int cg_blkio_weight_parse(const char *s, uint64_t *ret);
|
||||
|
||||
bool is_cgroup_fs(const struct statfs *s);
|
||||
bool fd_is_cgroup_fs(int fd);
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
#include "bpf-firewall.h"
|
||||
#include "btrfs-util.h"
|
||||
#include "bus-error.h"
|
||||
#include "cgroup-setup.h"
|
||||
#include "cgroup-util.h"
|
||||
#include "cgroup.h"
|
||||
#include "fd-util.h"
|
||||
|
||||
@@ -48,6 +48,7 @@
|
||||
#include "cap-list.h"
|
||||
#include "capability-util.h"
|
||||
#include "chown-recursive.h"
|
||||
#include "cgroup-setup.h"
|
||||
#include "cpu-set-util.h"
|
||||
#include "def.h"
|
||||
#include "env-file.h"
|
||||
|
||||
@@ -24,7 +24,7 @@
|
||||
#include "bus-util.h"
|
||||
#include "cap-list.h"
|
||||
#include "capability-util.h"
|
||||
#include "cgroup.h"
|
||||
#include "cgroup-setup.h"
|
||||
#include "conf-parser.h"
|
||||
#include "cpu-set-util.h"
|
||||
#include "env-util.h"
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
#include "bus-util.h"
|
||||
#include "cgroup-util.h"
|
||||
#include "conf-files.h"
|
||||
#include "cgroup-setup.h"
|
||||
#include "dev-setup.h"
|
||||
#include "efivars.h"
|
||||
#include "fd-util.h"
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
#include "bpf-firewall.h"
|
||||
#include "bus-common-errors.h"
|
||||
#include "bus-util.h"
|
||||
#include "cgroup-setup.h"
|
||||
#include "cgroup-util.h"
|
||||
#include "dbus-unit.h"
|
||||
#include "dbus.h"
|
||||
|
||||
@@ -20,7 +20,7 @@
|
||||
#include "bus-error.h"
|
||||
#include "bus-internal.h"
|
||||
#include "bus-util.h"
|
||||
#include "cgroup-util.h"
|
||||
#include "cgroup-setup.h"
|
||||
#include "errno-util.h"
|
||||
#include "fd-util.h"
|
||||
#include "fileio.h"
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
#include <sys/mount.h>
|
||||
|
||||
#include "alloc-util.h"
|
||||
#include "cgroup-setup.h"
|
||||
#include "fd-util.h"
|
||||
#include "fileio.h"
|
||||
#include "format-util.h"
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
#include "bus-unit-util.h"
|
||||
#include "bus-util.h"
|
||||
#include "cap-list.h"
|
||||
#include "cgroup-setup.h"
|
||||
#include "cgroup-util.h"
|
||||
#include "condition.h"
|
||||
#include "cpu-set-util.h"
|
||||
|
||||
860
src/shared/cgroup-setup.c
Normal file
860
src/shared/cgroup-setup.c
Normal file
File diff suppressed because it is too large
Load Diff
34
src/shared/cgroup-setup.h
Normal file
34
src/shared/cgroup-setup.h
Normal file
@@ -0,0 +1,34 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1+ */
|
||||
#pragma once
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#include "cgroup-util.h"
|
||||
|
||||
bool cg_is_unified_wanted(void);
|
||||
bool cg_is_legacy_wanted(void);
|
||||
bool cg_is_hybrid_wanted(void);
|
||||
|
||||
int cg_weight_parse(const char *s, uint64_t *ret);
|
||||
int cg_cpu_shares_parse(const char *s, uint64_t *ret);
|
||||
int cg_blkio_weight_parse(const char *s, uint64_t *ret);
|
||||
|
||||
int cg_trim(const char *controller, const char *path, bool delete_root);
|
||||
|
||||
int cg_create(const char *controller, const char *path);
|
||||
int cg_attach(const char *controller, const char *path, pid_t pid);
|
||||
int cg_attach_fallback(const char *controller, const char *path, pid_t pid);
|
||||
int cg_create_and_attach(const char *controller, const char *path, pid_t pid);
|
||||
|
||||
int cg_migrate(const char *cfrom, const char *pfrom, const char *cto, const char *pto, CGroupFlags flags);
|
||||
int cg_migrate_recursive(const char *cfrom, const char *pfrom, const char *cto, const char *pto, CGroupFlags flags);
|
||||
int cg_migrate_recursive_fallback(const char *cfrom, const char *pfrom, const char *cto, const char *pto, CGroupFlags flags);
|
||||
|
||||
int cg_create_everywhere(CGroupMask supported, CGroupMask mask, const char *path);
|
||||
int cg_attach_everywhere(CGroupMask supported, const char *path, pid_t pid, cg_migrate_callback_t callback, void *userdata);
|
||||
int cg_attach_many_everywhere(CGroupMask supported, const char *path, Set* pids, cg_migrate_callback_t callback, void *userdata);
|
||||
int cg_migrate_everywhere(CGroupMask supported, const char *from, const char *to, cg_migrate_callback_t callback, void *userdata);
|
||||
int cg_trim_everywhere(CGroupMask supported, const char *path, bool delete_root);
|
||||
int cg_enable_everywhere(CGroupMask supported, CGroupMask mask, const char *p, CGroupMask *ret_result_mask);
|
||||
@@ -33,6 +33,8 @@ shared_sources = files('''
|
||||
bus-wait-for-units.h
|
||||
calendarspec.c
|
||||
calendarspec.h
|
||||
cgroup-setup.c
|
||||
cgroup-setup.h
|
||||
cgroup-show.c
|
||||
cgroup-show.h
|
||||
clean-ipc.c
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
|
||||
#include "alloc-util.h"
|
||||
#include "async.h"
|
||||
#include "cgroup-setup.h"
|
||||
#include "cgroup-util.h"
|
||||
#include "def.h"
|
||||
#include "exec-util.h"
|
||||
|
||||
@@ -615,6 +615,10 @@ tests += [
|
||||
[],
|
||||
[]],
|
||||
|
||||
[['src/test/test-cgroup-setup.c'],
|
||||
[],
|
||||
[]],
|
||||
|
||||
[['src/test/test-env-file.c'],
|
||||
[],
|
||||
[]],
|
||||
|
||||
67
src/test/test-cgroup-setup.c
Normal file
67
src/test/test-cgroup-setup.c
Normal file
@@ -0,0 +1,67 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1+ */
|
||||
|
||||
#include "alloc-util.h"
|
||||
#include "build.h"
|
||||
#include "cgroup-setup.h"
|
||||
#include "log.h"
|
||||
#include "proc-cmdline.h"
|
||||
#include "string-util.h"
|
||||
#include "tests.h"
|
||||
|
||||
static void test_is_wanted_print(bool header) {
|
||||
_cleanup_free_ char *cmdline = NULL;
|
||||
|
||||
log_info("-- %s --", __func__);
|
||||
assert_se(proc_cmdline(&cmdline) >= 0);
|
||||
log_info("cmdline: %s", cmdline);
|
||||
if (header) {
|
||||
log_info(_CGROUP_HIERARCHY_);
|
||||
(void) system("findmnt -n /sys/fs/cgroup");
|
||||
}
|
||||
|
||||
log_info("is_unified_wanted() → %s", yes_no(cg_is_unified_wanted()));
|
||||
log_info("is_hybrid_wanted() → %s", yes_no(cg_is_hybrid_wanted()));
|
||||
log_info("is_legacy_wanted() → %s", yes_no(cg_is_legacy_wanted()));
|
||||
log_info(" ");
|
||||
}
|
||||
|
||||
static void test_is_wanted(void) {
|
||||
assert_se(setenv("SYSTEMD_PROC_CMDLINE",
|
||||
"systemd.unified_cgroup_hierarchy", 1) >= 0);
|
||||
test_is_wanted_print(false);
|
||||
|
||||
assert_se(setenv("SYSTEMD_PROC_CMDLINE",
|
||||
"systemd.unified_cgroup_hierarchy=0", 1) >= 0);
|
||||
test_is_wanted_print(false);
|
||||
|
||||
assert_se(setenv("SYSTEMD_PROC_CMDLINE",
|
||||
"systemd.unified_cgroup_hierarchy=0 "
|
||||
"systemd.legacy_systemd_cgroup_controller", 1) >= 0);
|
||||
test_is_wanted_print(false);
|
||||
|
||||
assert_se(setenv("SYSTEMD_PROC_CMDLINE",
|
||||
"systemd.unified_cgroup_hierarchy=0 "
|
||||
"systemd.legacy_systemd_cgroup_controller=0", 1) >= 0);
|
||||
test_is_wanted_print(false);
|
||||
|
||||
/* cgroup_no_v1=all implies unified cgroup hierarchy, unless otherwise
|
||||
* explicitly specified. */
|
||||
assert_se(setenv("SYSTEMD_PROC_CMDLINE",
|
||||
"cgroup_no_v1=all", 1) >= 0);
|
||||
test_is_wanted_print(false);
|
||||
|
||||
assert_se(setenv("SYSTEMD_PROC_CMDLINE",
|
||||
"cgroup_no_v1=all "
|
||||
"systemd.unified_cgroup_hierarchy=0", 1) >= 0);
|
||||
test_is_wanted_print(false);
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
test_setup_logging(LOG_DEBUG);
|
||||
|
||||
test_is_wanted_print(true);
|
||||
test_is_wanted_print(false); /* run twice to test caching */
|
||||
test_is_wanted();
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -333,55 +333,6 @@ static void test_fd_is_cgroup_fs(void) {
|
||||
fd = safe_close(fd);
|
||||
}
|
||||
|
||||
static void test_is_wanted_print(bool header) {
|
||||
_cleanup_free_ char *cmdline = NULL;
|
||||
|
||||
log_info("-- %s --", __func__);
|
||||
assert_se(proc_cmdline(&cmdline) >= 0);
|
||||
log_info("cmdline: %s", cmdline);
|
||||
if (header) {
|
||||
|
||||
log_info(_CGROUP_HIERARCHY_);
|
||||
(void) system("findmnt -n /sys/fs/cgroup");
|
||||
}
|
||||
|
||||
log_info("is_unified_wanted() → %s", yes_no(cg_is_unified_wanted()));
|
||||
log_info("is_hybrid_wanted() → %s", yes_no(cg_is_hybrid_wanted()));
|
||||
log_info("is_legacy_wanted() → %s", yes_no(cg_is_legacy_wanted()));
|
||||
log_info(" ");
|
||||
}
|
||||
|
||||
static void test_is_wanted(void) {
|
||||
assert_se(setenv("SYSTEMD_PROC_CMDLINE",
|
||||
"systemd.unified_cgroup_hierarchy", 1) >= 0);
|
||||
test_is_wanted_print(false);
|
||||
|
||||
assert_se(setenv("SYSTEMD_PROC_CMDLINE",
|
||||
"systemd.unified_cgroup_hierarchy=0", 1) >= 0);
|
||||
test_is_wanted_print(false);
|
||||
|
||||
assert_se(setenv("SYSTEMD_PROC_CMDLINE",
|
||||
"systemd.unified_cgroup_hierarchy=0 "
|
||||
"systemd.legacy_systemd_cgroup_controller", 1) >= 0);
|
||||
test_is_wanted_print(false);
|
||||
|
||||
assert_se(setenv("SYSTEMD_PROC_CMDLINE",
|
||||
"systemd.unified_cgroup_hierarchy=0 "
|
||||
"systemd.legacy_systemd_cgroup_controller=0", 1) >= 0);
|
||||
test_is_wanted_print(false);
|
||||
|
||||
/* cgroup_no_v1=all implies unified cgroup hierarchy, unless otherwise
|
||||
* explicitly specified. */
|
||||
assert_se(setenv("SYSTEMD_PROC_CMDLINE",
|
||||
"cgroup_no_v1=all", 1) >= 0);
|
||||
test_is_wanted_print(false);
|
||||
|
||||
assert_se(setenv("SYSTEMD_PROC_CMDLINE",
|
||||
"cgroup_no_v1=all "
|
||||
"systemd.unified_cgroup_hierarchy=0", 1) >= 0);
|
||||
test_is_wanted_print(false);
|
||||
}
|
||||
|
||||
static void test_cg_tests(void) {
|
||||
int all, hybrid, systemd, r;
|
||||
|
||||
@@ -477,9 +428,6 @@ int main(void) {
|
||||
TEST_REQ_RUNNING_SYSTEMD(test_mask_supported());
|
||||
TEST_REQ_RUNNING_SYSTEMD(test_is_cgroup_fs());
|
||||
TEST_REQ_RUNNING_SYSTEMD(test_fd_is_cgroup_fs());
|
||||
test_is_wanted_print(true);
|
||||
test_is_wanted_print(false); /* run twice to test caching */
|
||||
test_is_wanted();
|
||||
test_cg_tests();
|
||||
test_cg_get_keyed_attribute();
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "cgroup-setup.h"
|
||||
#include "cgroup-util.h"
|
||||
#include "path-util.h"
|
||||
#include "process-util.h"
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
#include "test-helper.h"
|
||||
#include "random-util.h"
|
||||
#include "alloc-util.h"
|
||||
#include "cgroup-util.h"
|
||||
#include "cgroup-setup.h"
|
||||
#include "string-util.h"
|
||||
|
||||
int enter_cgroup_subroot(void) {
|
||||
|
||||
Reference in New Issue
Block a user