Skip to content

Commit 2a8020f

Browse files
committed
basic/cgroup-util: modernize cg_split_spec()
Those cryptic one letter variable names, yuck!
1 parent b35e997 commit 2a8020f

File tree

2 files changed

+36
-60
lines changed

2 files changed

+36
-60
lines changed

src/basic/cgroup-util.c

Lines changed: 35 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -878,86 +878,62 @@ int cg_is_empty_recursive(const char *controller, const char *path) {
878878
}
879879
}
880880

881-
int cg_split_spec(const char *spec, char **controller, char **path) {
882-
char *t = NULL, *u = NULL;
883-
const char *e;
881+
int cg_split_spec(const char *spec, char **ret_controller, char **ret_path) {
882+
_cleanup_free_ char *controller = NULL, *path = NULL;
884883

885884
assert(spec);
886885

887886
if (*spec == '/') {
888887
if (!path_is_normalized(spec))
889888
return -EINVAL;
890889

891-
if (path) {
892-
t = strdup(spec);
893-
if (!t)
890+
if (ret_path) {
891+
path = strdup(spec);
892+
if (!path)
894893
return -ENOMEM;
895894

896-
*path = path_simplify(t, false);
895+
path_simplify(path, false);
897896
}
898897

899-
if (controller)
900-
*controller = NULL;
901-
902-
return 0;
903-
}
904-
905-
e = strchr(spec, ':');
906-
if (!e) {
907-
if (!cg_controller_is_valid(spec))
908-
return -EINVAL;
898+
} else {
899+
const char *e;
909900

910-
if (controller) {
911-
t = strdup(spec);
912-
if (!t)
901+
e = strchr(spec, ':');
902+
if (e) {
903+
controller = strndup(spec, e-spec);
904+
if (!controller)
913905
return -ENOMEM;
906+
if (!cg_controller_is_valid(controller))
907+
return -EINVAL;
914908

915-
*controller = t;
916-
}
917-
918-
if (path)
919-
*path = NULL;
909+
if (!isempty(e + 1)) {
910+
path = strdup(e+1);
911+
if (!path)
912+
return -ENOMEM;
920913

921-
return 0;
922-
}
914+
if (!path_is_normalized(path) ||
915+
!path_is_absolute(path))
916+
return -EINVAL;
923917

924-
t = strndup(spec, e-spec);
925-
if (!t)
926-
return -ENOMEM;
927-
if (!cg_controller_is_valid(t)) {
928-
free(t);
929-
return -EINVAL;
930-
}
918+
path_simplify(path, false);
919+
}
931920

932-
if (isempty(e+1))
933-
u = NULL;
934-
else {
935-
u = strdup(e+1);
936-
if (!u) {
937-
free(t);
938-
return -ENOMEM;
939-
}
921+
} else {
922+
if (!cg_controller_is_valid(spec))
923+
return -EINVAL;
940924

941-
if (!path_is_normalized(u) ||
942-
!path_is_absolute(u)) {
943-
free(t);
944-
free(u);
945-
return -EINVAL;
925+
if (ret_controller) {
926+
controller = strdup(spec);
927+
if (!controller)
928+
return -ENOMEM;
929+
}
946930
}
947-
948-
path_simplify(u, false);
949931
}
950932

951-
if (controller)
952-
*controller = t;
953-
else
954-
free(t);
955-
956-
if (path)
957-
*path = u;
958-
else
959-
free(u);
960-
933+
if (ret_controller)
934+
*ret_controller = TAKE_PTR(controller);
935+
if (ret_path)
936+
*ret_path = TAKE_PTR(path);
961937
return 0;
962938
}
963939

src/basic/cgroup-util.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ typedef int (*cg_kill_log_func_t)(pid_t pid, int sig, void *userdata);
170170
int cg_kill(const char *controller, const char *path, int sig, CGroupFlags flags, Set *s, cg_kill_log_func_t kill_log, void *userdata);
171171
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);
172172

173-
int cg_split_spec(const char *spec, char **controller, char **path);
173+
int cg_split_spec(const char *spec, char **ret_controller, char **ret_path);
174174
int cg_mangle_path(const char *path, char **result);
175175

176176
int cg_get_path(const char *controller, const char *path, const char *suffix, char **fs);

0 commit comments

Comments
 (0)