summaryrefslogtreecommitdiff
path: root/src/shared
diff options
context:
space:
mode:
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2013-01-18 01:13:27 -0500
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2013-01-18 11:14:00 -0500
commit96cde13ace6406582688028f3df5668a172ba628 (patch)
treeb8e3cdb44626ba863097efa7874e417c2f1c9fee /src/shared
parent3f98659cce700fea91959312297950f15011b07b (diff)
core/cgroup-util: simplify functions and add tests
Diffstat (limited to 'src/shared')
-rw-r--r--src/shared/cgroup-util.c62
-rw-r--r--src/shared/cgroup-util.h2
2 files changed, 27 insertions, 37 deletions
diff --git a/src/shared/cgroup-util.c b/src/shared/cgroup-util.c
index af5227848d..f0d0d4855b 100644
--- a/src/shared/cgroup-util.c
+++ b/src/shared/cgroup-util.c
@@ -1211,69 +1211,57 @@ int cg_pid_get_cgroup(pid_t pid, char **root, char **cgroup) {
return 0;
}
-static int instance_unit_from_cgroup(char **cgroup){
+static int instance_unit_from_cgroup(char *cgroup){
char *at;
assert(cgroup);
- at = memchr(*cgroup, '@', strlen(*cgroup));
- if (at && at[1] == '.') {
- char *i, *s;
-
+ at = strstr(cgroup, "@.");
+ if (at) {
/* This is a templated service */
- i = memchr(at, '/', strlen(at));
- if(!i)
- return -EIO;
- s = strndup(at + 1, i - at);
- if (!s)
- return -ENOMEM;
+ char *i;
+ char _cleanup_free_ *i2 = NULL, *s = NULL;
- i = strdup(i + 1);
- if (!i) {
- free(s);
- return -ENOMEM;
- }
+ i = strchr(at, '/');
+ if (!i || !i[1]) /* disallow empty instances */
+ return -EINVAL;
- strcpy(at + 1, i);
- strcpy(at + strlen(i) + 1, s);
- at[strlen(at) - 1] = '\0';
+ s = strndup(at + 1, i - at - 1);
+ i2 = strdup(i + 1);
+ if (!s || !i2)
+ return -ENOMEM;
- free(i);
- free(s);
+ strcpy(at + 1, i2);
+ strcat(at + 1, s);
}
return 0;
}
-static int cgroup_to_unit(char *cgroup, char **unit){
+/* non-static only for testing purposes */
+int cgroup_to_unit(char *cgroup, char **unit){
int r;
- char *b, *p;
- size_t k;
+ char *p;
assert(cgroup);
assert(unit);
- r = instance_unit_from_cgroup(&cgroup);
+ r = instance_unit_from_cgroup(cgroup);
if (r < 0)
return r;
- p = strrchr(cgroup, '/') + 1;
- k = strlen(p);
+ p = strrchr(cgroup, '/');
+ assert(p);
- b = strndup(p, k);
+ r = unit_name_is_valid(p + 1, true);
+ if (!r)
+ return -EINVAL;
- if (!b)
+ *unit = strdup(p + 1);
+ if (!*unit)
return -ENOMEM;
- r = unit_name_is_valid(b, true);
- if (!r) {
- free(b);
- return -ENOENT;
- }
-
- *unit = b;
-
return 0;
}
diff --git a/src/shared/cgroup-util.h b/src/shared/cgroup-util.h
index 2429ba2430..920cf631e5 100644
--- a/src/shared/cgroup-util.h
+++ b/src/shared/cgroup-util.h
@@ -73,4 +73,6 @@ int cg_pid_get_cgroup(pid_t pid, char **root, char **cgroup);
int cg_pid_get_unit(pid_t pid, char **unit);
int cg_pid_get_user_unit(pid_t pid, char **unit);
+int cgroup_to_unit(char *cgroup, char **unit);
+
char **cg_shorten_controllers(char **controllers);