diff options
author | Benny Baumann <BenBE@geshi.org> | 2021-11-21 19:57:44 +0100 |
---|---|---|
committer | BenBE <BenBE@geshi.org> | 2021-11-28 20:19:10 +0100 |
commit | 55e073a455b32ffef7992cfdefded9978b8d0468 (patch) | |
tree | b2889387d259da9d4115730fdb6f296d9f27deaa | |
parent | 915b558bbe1b13d6cb0dd099197c9fb1e2470cc6 (diff) |
Use helpers for parsing of cgroup labels
-rw-r--r-- | linux/CGroupUtils.c | 30 |
1 files changed, 21 insertions, 9 deletions
diff --git a/linux/CGroupUtils.c b/linux/CGroupUtils.c index 5d9d2027..1c8b05ff 100644 --- a/linux/CGroupUtils.c +++ b/linux/CGroupUtils.c @@ -48,6 +48,18 @@ static bool StrBuf_putsz(StrBuf_state* p, StrBuf_putc_t w, const char* s) { return true; } +static bool Label_checkEqual(const char* labelStart, size_t labelLen, const char* expected) { + return labelLen == strlen(expected) && String_startsWith(labelStart, expected); +} + +static bool Label_checkPrefix(const char* labelStart, size_t labelLen, const char* expected) { + return labelLen > strlen(expected) && String_startsWith(labelStart, expected); +} + +static bool Label_checkSuffix(const char* labelStart, size_t labelLen, const char* expected) { + return labelLen > strlen(expected) && String_startsWith(labelStart + labelLen - strlen(expected), expected); +} + static bool CGroup_filterName_internal(const char *cgroup, StrBuf_state* s, StrBuf_putc_t w) { const char* str_slice_suffix = ".slice"; const char* str_system_slice = "system.slice"; @@ -80,7 +92,7 @@ static bool CGroup_filterName_internal(const char *cgroup, StrBuf_state* s, StrB const char* nextSlash = strchrnul(labelStart, '/'); const size_t labelLen = nextSlash - labelStart; - if (labelLen == strlen(str_system_slice) && String_startsWith(cgroup, str_system_slice)) { + if (Label_checkEqual(labelStart, labelLen, str_system_slice)) { cgroup = nextSlash; if (!StrBuf_putsz(s, w, "[S]")) @@ -89,7 +101,7 @@ static bool CGroup_filterName_internal(const char *cgroup, StrBuf_state* s, StrB continue; } - if (labelLen == strlen(str_machine_slice) && String_startsWith(cgroup, str_machine_slice)) { + if (Label_checkEqual(labelStart, labelLen, str_machine_slice)) { cgroup = nextSlash; if (!StrBuf_putsz(s, w, "[M]")) @@ -98,7 +110,7 @@ static bool CGroup_filterName_internal(const char *cgroup, StrBuf_state* s, StrB continue; } - if (labelLen == strlen(str_user_slice) && String_startsWith(cgroup, str_user_slice)) { + if (Label_checkEqual(labelStart, labelLen, str_user_slice)) { cgroup = nextSlash; if (!StrBuf_putsz(s, w, "[U]")) @@ -130,7 +142,7 @@ static bool CGroup_filterName_internal(const char *cgroup, StrBuf_state* s, StrB continue; } - if (labelLen > strlen(str_slice_suffix) && String_startsWith(nextSlash - strlen(str_slice_suffix), str_slice_suffix)) { + if (Label_checkSuffix(labelStart, labelLen, str_slice_suffix)) { const size_t sliceNameLen = labelLen - strlen(str_slice_suffix); if (!w(s, '[')) @@ -147,7 +159,7 @@ static bool CGroup_filterName_internal(const char *cgroup, StrBuf_state* s, StrB continue; } - if (String_startsWith(cgroup, str_lxc_payload_prefix)) { + if (Label_checkPrefix(labelStart, labelLen, str_lxc_payload_prefix)) { const size_t cgroupNameLen = labelLen - strlen(str_lxc_payload_prefix); if (!StrBuf_putsz(s, w, "[lxc:")) @@ -164,7 +176,7 @@ static bool CGroup_filterName_internal(const char *cgroup, StrBuf_state* s, StrB continue; } - if (String_startsWith(cgroup, str_lxc_monitor_prefix)) { + if (Label_checkPrefix(labelStart, labelLen, str_lxc_monitor_prefix)) { const size_t cgroupNameLen = labelLen - strlen(str_lxc_monitor_prefix); if (!StrBuf_putsz(s, w, "[LXC:")) @@ -181,7 +193,7 @@ static bool CGroup_filterName_internal(const char *cgroup, StrBuf_state* s, StrB continue; } - if (labelLen > strlen(str_service_suffix) && String_startsWith(nextSlash - strlen(str_service_suffix), str_service_suffix)) { + if (Label_checkSuffix(labelStart, labelLen, str_service_suffix)) { const size_t serviceNameLen = labelLen - strlen(str_service_suffix); if (String_startsWith(cgroup, "user@")) { @@ -201,10 +213,10 @@ static bool CGroup_filterName_internal(const char *cgroup, StrBuf_state* s, StrB continue; } - if (labelLen > strlen(str_scope_suffix) && String_startsWith(nextSlash - strlen(str_scope_suffix), str_scope_suffix)) { + if (Label_checkSuffix(labelStart, labelLen, str_scope_suffix)) { const size_t scopeNameLen = labelLen - strlen(str_scope_suffix); - if (String_startsWith(cgroup, str_nspawn_scope_prefix)) { + if (Label_checkPrefix(labelStart, scopeNameLen, str_nspawn_scope_prefix)) { const size_t machineScopeNameLen = scopeNameLen - strlen(str_nspawn_scope_prefix); const bool is_monitor = String_startsWith(nextSlash, str_nspawn_monitor_label); |