Skip to content

Commit 55e073a

Browse files
committed
Use helpers for parsing of cgroup labels
1 parent 915b558 commit 55e073a

1 file changed

Lines changed: 21 additions & 9 deletions

File tree

linux/CGroupUtils.c

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,18 @@ static bool StrBuf_putsz(StrBuf_state* p, StrBuf_putc_t w, const char* s) {
4848
return true;
4949
}
5050

51+
static bool Label_checkEqual(const char* labelStart, size_t labelLen, const char* expected) {
52+
return labelLen == strlen(expected) && String_startsWith(labelStart, expected);
53+
}
54+
55+
static bool Label_checkPrefix(const char* labelStart, size_t labelLen, const char* expected) {
56+
return labelLen > strlen(expected) && String_startsWith(labelStart, expected);
57+
}
58+
59+
static bool Label_checkSuffix(const char* labelStart, size_t labelLen, const char* expected) {
60+
return labelLen > strlen(expected) && String_startsWith(labelStart + labelLen - strlen(expected), expected);
61+
}
62+
5163
static bool CGroup_filterName_internal(const char *cgroup, StrBuf_state* s, StrBuf_putc_t w) {
5264
const char* str_slice_suffix = ".slice";
5365
const char* str_system_slice = "system.slice";
@@ -80,7 +92,7 @@ static bool CGroup_filterName_internal(const char *cgroup, StrBuf_state* s, StrB
8092
const char* nextSlash = strchrnul(labelStart, '/');
8193
const size_t labelLen = nextSlash - labelStart;
8294

83-
if (labelLen == strlen(str_system_slice) && String_startsWith(cgroup, str_system_slice)) {
95+
if (Label_checkEqual(labelStart, labelLen, str_system_slice)) {
8496
cgroup = nextSlash;
8597

8698
if (!StrBuf_putsz(s, w, "[S]"))
@@ -89,7 +101,7 @@ static bool CGroup_filterName_internal(const char *cgroup, StrBuf_state* s, StrB
89101
continue;
90102
}
91103

92-
if (labelLen == strlen(str_machine_slice) && String_startsWith(cgroup, str_machine_slice)) {
104+
if (Label_checkEqual(labelStart, labelLen, str_machine_slice)) {
93105
cgroup = nextSlash;
94106

95107
if (!StrBuf_putsz(s, w, "[M]"))
@@ -98,7 +110,7 @@ static bool CGroup_filterName_internal(const char *cgroup, StrBuf_state* s, StrB
98110
continue;
99111
}
100112

101-
if (labelLen == strlen(str_user_slice) && String_startsWith(cgroup, str_user_slice)) {
113+
if (Label_checkEqual(labelStart, labelLen, str_user_slice)) {
102114
cgroup = nextSlash;
103115

104116
if (!StrBuf_putsz(s, w, "[U]"))
@@ -130,7 +142,7 @@ static bool CGroup_filterName_internal(const char *cgroup, StrBuf_state* s, StrB
130142
continue;
131143
}
132144

133-
if (labelLen > strlen(str_slice_suffix) && String_startsWith(nextSlash - strlen(str_slice_suffix), str_slice_suffix)) {
145+
if (Label_checkSuffix(labelStart, labelLen, str_slice_suffix)) {
134146
const size_t sliceNameLen = labelLen - strlen(str_slice_suffix);
135147

136148
if (!w(s, '['))
@@ -147,7 +159,7 @@ static bool CGroup_filterName_internal(const char *cgroup, StrBuf_state* s, StrB
147159
continue;
148160
}
149161

150-
if (String_startsWith(cgroup, str_lxc_payload_prefix)) {
162+
if (Label_checkPrefix(labelStart, labelLen, str_lxc_payload_prefix)) {
151163
const size_t cgroupNameLen = labelLen - strlen(str_lxc_payload_prefix);
152164

153165
if (!StrBuf_putsz(s, w, "[lxc:"))
@@ -164,7 +176,7 @@ static bool CGroup_filterName_internal(const char *cgroup, StrBuf_state* s, StrB
164176
continue;
165177
}
166178

167-
if (String_startsWith(cgroup, str_lxc_monitor_prefix)) {
179+
if (Label_checkPrefix(labelStart, labelLen, str_lxc_monitor_prefix)) {
168180
const size_t cgroupNameLen = labelLen - strlen(str_lxc_monitor_prefix);
169181

170182
if (!StrBuf_putsz(s, w, "[LXC:"))
@@ -181,7 +193,7 @@ static bool CGroup_filterName_internal(const char *cgroup, StrBuf_state* s, StrB
181193
continue;
182194
}
183195

184-
if (labelLen > strlen(str_service_suffix) && String_startsWith(nextSlash - strlen(str_service_suffix), str_service_suffix)) {
196+
if (Label_checkSuffix(labelStart, labelLen, str_service_suffix)) {
185197
const size_t serviceNameLen = labelLen - strlen(str_service_suffix);
186198

187199
if (String_startsWith(cgroup, "user@")) {
@@ -201,10 +213,10 @@ static bool CGroup_filterName_internal(const char *cgroup, StrBuf_state* s, StrB
201213
continue;
202214
}
203215

204-
if (labelLen > strlen(str_scope_suffix) && String_startsWith(nextSlash - strlen(str_scope_suffix), str_scope_suffix)) {
216+
if (Label_checkSuffix(labelStart, labelLen, str_scope_suffix)) {
205217
const size_t scopeNameLen = labelLen - strlen(str_scope_suffix);
206218

207-
if (String_startsWith(cgroup, str_nspawn_scope_prefix)) {
219+
if (Label_checkPrefix(labelStart, scopeNameLen, str_nspawn_scope_prefix)) {
208220
const size_t machineScopeNameLen = scopeNameLen - strlen(str_nspawn_scope_prefix);
209221

210222
const bool is_monitor = String_startsWith(nextSlash, str_nspawn_monitor_label);

0 commit comments

Comments
 (0)