#include "path-util.h"
#include "strv.h"
#include "unit-name.h"
+#include "fileio.h"
int cg_enumerate_processes(const char *controller, const char *path, FILE **_f) {
char *fs;
continue;
l++;
- if (strncmp(l, controller, cs) != 0)
+ if (!strneq(l, controller, cs))
continue;
if (l[cs] != ':')
assert(spec);
if (*spec == '/') {
+ if (!path_is_safe(spec))
+ return -EINVAL;
if (path) {
t = strdup(spec);
e = strchr(spec, ':');
if (!e) {
- if (strchr(spec, '/') || spec[0] == 0)
+ if (!filename_is_safe(spec))
return -EINVAL;
if (controller) {
return 0;
}
- if (e[1] != '/' || e == spec || memchr(spec, '/', e-spec))
+ t = strndup(spec, e-spec);
+ if (!t)
+ return -ENOMEM;
+ if (!filename_is_safe(t)) {
+ free(t);
return -EINVAL;
-
- if (controller) {
- t = strndup(spec, e-spec);
- if (!t)
- return -ENOMEM;
-
}
- if (path) {
- u = strdup(e+1);
- if (!u) {
- free(t);
- return -ENOMEM;
- }
+ u = strdup(e+1);
+ if (!u) {
+ free(t);
+ return -ENOMEM;
+ }
+ if (!path_is_safe(u)) {
+ free(t);
+ free(u);
+ return -EINVAL;
}
if (controller)
*controller = t;
+ else
+ free(t);
if (path)
*path = u;
+ else
+ free(u);
return 0;
}
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;
}
int cg_pid_get_user_unit(pid_t pid, char **unit) {
return cg_pid_get("/user/", pid, unit);
}
+
+int cg_controller_from_attr(const char *attr, char **controller) {
+ const char *dot;
+ char *c;
+
+ assert(attr);
+ assert(controller);
+
+ if (!filename_is_safe(attr))
+ return -EINVAL;
+
+ dot = strchr(attr, '.');
+ if (!dot) {
+ *controller = NULL;
+ return 0;
+ }
+
+ c = strndup(attr, dot - attr);
+ if (!c)
+ return -ENOMEM;
+
+ if (!filename_is_safe(c)) {
+ free(c);
+ return -EINVAL;
+ }
+
+ *controller = c;
+ return 1;
+}