chiark
/
gitweb
/
~ianmdlvl
/
elogind.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
bus: add convenience calls for method replies, too
[elogind.git]
/
src
/
shared
/
cgroup-util.c
diff --git
a/src/shared/cgroup-util.c
b/src/shared/cgroup-util.c
index 3d12233a1b394bd8b04a2ff0e5ed4ab1410b6fac..15e1b7c055331279e1ff471826cb4565e39d5b70 100644
(file)
--- a/
src/shared/cgroup-util.c
+++ b/
src/shared/cgroup-util.c
@@
-37,6
+37,7
@@
#include "path-util.h"
#include "strv.h"
#include "unit-name.h"
#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;
int cg_enumerate_processes(const char *controller, const char *path, FILE **_f) {
char *fs;
@@
-676,9
+677,9
@@
int cg_delete(const char *controller, const char *path) {
}
int cg_attach(const char *controller, const char *path, pid_t pid) {
}
int cg_attach(const char *controller, const char *path, pid_t pid) {
- char *fs;
+ _cleanup_free_ char *fs = NULL;
+ char c[DECIMAL_STR_MAX(pid_t) + 2];
int r;
int r;
- char c[32];
assert(controller);
assert(path);
assert(controller);
assert(path);
@@
-692,16
+693,12
@@
int cg_attach(const char *controller, const char *path, pid_t pid) {
pid = getpid();
snprintf(c, sizeof(c), "%lu\n", (unsigned long) pid);
pid = getpid();
snprintf(c, sizeof(c), "%lu\n", (unsigned long) pid);
- char_array_0(c);
-
- r = write_one_line_file(fs, c);
- free(fs);
- return
r
;
+ return
write_string_file(fs, c)
;
}
int cg_set_group_access(const char *controller, const char *path, mode_t mode, uid_t uid, gid_t gid) {
}
int cg_set_group_access(const char *controller, const char *path, mode_t mode, uid_t uid, gid_t gid) {
-
char *fs
;
+
_cleanup_free_ char *fs = NULL
;
int r;
assert(controller);
int r;
assert(controller);
@@
-714,10
+711,7
@@
int cg_set_group_access(const char *controller, const char *path, mode_t mode, u
if (r < 0)
return r;
if (r < 0)
return r;
- r = chmod_and_chown(fs, mode, uid, gid);
- free(fs);
-
- return r;
+ return chmod_and_chown(fs, mode, uid, gid);
}
int cg_set_task_access(const char *controller, const char *path, mode_t mode, uid_t uid, gid_t gid, int sticky) {
}
int cg_set_task_access(const char *controller, const char *path, mode_t mode, uid_t uid, gid_t gid, int sticky) {
@@
-811,7
+805,7
@@
int cg_get_by_pid(const char *controller, pid_t pid, char **path) {
continue;
l++;
continue;
l++;
- if (
strncmp(l, controller, cs) != 0
)
+ if (
!strneq(l, controller, cs)
)
continue;
if (l[cs] != ':')
continue;
if (l[cs] != ':')
@@
-856,7
+850,8
@@
int cg_install_release_agent(const char *controller, const char *agent) {
goto finish;
}
goto finish;
}
- if ((r = write_one_line_file(fs, line)) < 0)
+ r = write_string_file(fs, line);
+ if (r < 0)
goto finish;
} else if (!streq(sc, agent)) {
goto finish;
} else if (!streq(sc, agent)) {
@@
-877,7
+872,7
@@
int cg_install_release_agent(const char *controller, const char *agent) {
sc = strstrip(contents);
if (streq(sc, "0")) {
sc = strstrip(contents);
if (streq(sc, "0")) {
- if ((r = write_
one_line
_file(fs, "1\n")) < 0)
+ if ((r = write_
string
_file(fs, "1\n")) < 0)
goto finish;
r = 1;
goto finish;
r = 1;
@@
-990,6
+985,8
@@
int cg_split_spec(const char *spec, char **controller, char **path) {
assert(spec);
if (*spec == '/') {
assert(spec);
if (*spec == '/') {
+ if (!path_is_safe(spec))
+ return -EINVAL;
if (path) {
t = strdup(spec);
if (path) {
t = strdup(spec);
@@
-1007,7
+1004,7
@@
int cg_split_spec(const char *spec, char **controller, char **path) {
e = strchr(spec, ':');
if (!e) {
e = strchr(spec, ':');
if (!e) {
- if (
strchr(spec, '/') || spec[0] == 0
)
+ if (
!filename_is_safe(spec)
)
return -EINVAL;
if (controller) {
return -EINVAL;
if (controller) {
@@
-1024,29
+1021,34
@@
int cg_split_spec(const char *spec, char **controller, char **path) {
return 0;
}
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;
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;
}
if (controller)
*controller = t;
+ else
+ free(t);
if (path)
*path = u;
if (path)
*path = u;
+ else
+ free(u);
return 0;
}
return 0;
}
@@
-1211,75
+1213,63
@@
int cg_pid_get_cgroup(pid_t pid, char **root, char **cgroup) {
return 0;
}
return 0;
}
-static int instance_unit_from_cgroup(char *
*
cgroup){
+static int instance_unit_from_cgroup(char *cgroup){
char *at;
assert(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 */
/* 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;
}
}
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;
int r;
- char *b, *p;
- size_t k;
+ char *p;
assert(cgroup);
assert(unit);
assert(cgroup);
assert(unit);
- r = instance_unit_from_cgroup(
&
cgroup);
+ r = instance_unit_from_cgroup(cgroup);
if (r < 0)
return r;
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;
return -ENOMEM;
- r = unit_name_is_valid(b, true);
- if (!r) {
- free(b);
- return -ENOENT;
- }
-
- *unit = b;
-
return 0;
}
return 0;
}
-
int cg_pid_get_unit(
pid_t pid, char **unit) {
+
static int cg_pid_get(const char *prefix,
pid_t pid, char **unit) {
int r;
int r;
- char
*cgroup
;
+ char
_cleanup_free_ *cgroup = NULL
;
assert(pid >= 0);
assert(unit);
assert(pid >= 0);
assert(unit);
@@
-1288,45
+1278,46
@@
int cg_pid_get_unit(pid_t pid, char **unit) {
if (r < 0)
return r;
if (r < 0)
return r;
- if (!startswith(cgroup, "/system/")) {
- free(cgroup);
+ if (!startswith(cgroup, prefix))
return -ENOENT;
return -ENOENT;
- }
r = cgroup_to_unit(cgroup, unit);
r = cgroup_to_unit(cgroup, unit);
- if (r < 0){
- free(cgroup);
- return r;
- }
-
- free(cgroup);
+ return r;
+}
- return 0;
+int cg_pid_get_unit(pid_t pid, char **unit) {
+ return cg_pid_get("/system/", pid, unit);
}
int cg_pid_get_user_unit(pid_t pid, char **unit) {
}
int cg_pid_get_user_unit(pid_t pid, char **unit) {
-
int r
;
- char *cgroup;
+
return cg_pid_get("/user/", pid, unit)
;
+}
- assert(pid >= 0);
- assert(unit);
+int cg_controller_from_attr(const char *attr, char **controller) {
+ const char *dot;
+ char *c;
- r = cg_pid_get_cgroup(pid, NULL, &cgroup);
- if (r < 0)
- return r;
+ assert(attr);
+ assert(controller);
- if (!startswith(cgroup, "/user/")) {
- free(cgroup);
- return -ENOENT;
- }
+ if (!filename_is_safe(attr))
+ return -EINVAL;
-
r = cgroup_to_unit(cgroup, unit
);
- if (
r < 0
) {
-
free(cgroup)
;
- return
r
;
+
dot = strchr(attr, '.'
);
+ if (
!dot
) {
+
*controller = NULL
;
+ return
0
;
}
}
- free(cgroup);
+ c = strndup(attr, dot - attr);
+ if (!c)
+ return -ENOMEM;
- return 0;
+ if (!filename_is_safe(c)) {
+ free(c);
+ return -EINVAL;
+ }
+
+ *controller = c;
+ return 1;
}
}