]>
Commit | Line | Data |
---|---|---|
ab1f0633 LP |
1 | /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ |
2 | ||
3 | /*** | |
4 | This file is part of systemd. | |
5 | ||
6 | Copyright 2011 Lennart Poettering | |
7 | ||
8 | systemd is free software; you can redistribute it and/or modify it | |
5430f7f2 LP |
9 | under the terms of the GNU Lesser General Public License as published by |
10 | the Free Software Foundation; either version 2.1 of the License, or | |
ab1f0633 LP |
11 | (at your option) any later version. |
12 | ||
13 | systemd is distributed in the hope that it will be useful, but | |
14 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
5430f7f2 | 16 | Lesser General Public License for more details. |
ab1f0633 | 17 | |
5430f7f2 | 18 | You should have received a copy of the GNU Lesser General Public License |
ab1f0633 LP |
19 | along with systemd; If not, see <http://www.gnu.org/licenses/>. |
20 | ***/ | |
21 | ||
22 | #include "cgroup-attr.h" | |
23 | #include "cgroup-util.h" | |
24 | #include "list.h" | |
a5c32cff | 25 | #include "fileio.h" |
ab1f0633 LP |
26 | |
27 | int cgroup_attribute_apply(CGroupAttribute *a, CGroupBonding *b) { | |
d2a30975 | 28 | _cleanup_free_ char *path = NULL, *v = NULL; |
26d04f86 | 29 | int r; |
ab1f0633 LP |
30 | |
31 | assert(a); | |
32 | ||
33 | b = cgroup_bonding_find_list(b, a->controller); | |
34 | if (!b) | |
35 | return 0; | |
36 | ||
26d04f86 LP |
37 | if (a->semantics && a->semantics->map_write) { |
38 | r = a->semantics->map_write(a->semantics, a->value, &v); | |
ab1f0633 LP |
39 | if (r < 0) |
40 | return r; | |
41 | } | |
42 | ||
43 | r = cg_get_path(a->controller, b->path, a->name, &path); | |
d2a30975 | 44 | if (r < 0) |
ab1f0633 | 45 | return r; |
ab1f0633 | 46 | |
574d5f2d | 47 | r = write_string_file(path, v ? v : a->value); |
ab1f0633 LP |
48 | if (r < 0) |
49 | log_warning("Failed to write '%s' to %s: %s", v ? v : a->value, path, strerror(-r)); | |
50 | ||
ab1f0633 LP |
51 | return r; |
52 | } | |
53 | ||
54 | int cgroup_attribute_apply_list(CGroupAttribute *first, CGroupBonding *b) { | |
55 | CGroupAttribute *a; | |
56 | int r = 0; | |
57 | ||
58 | LIST_FOREACH(by_unit, a, first) { | |
59 | int k; | |
60 | ||
61 | k = cgroup_attribute_apply(a, b); | |
62 | if (r == 0) | |
63 | r = k; | |
64 | } | |
65 | ||
66 | return r; | |
67 | } | |
68 | ||
26d04f86 LP |
69 | bool cgroup_attribute_matches(CGroupAttribute *a, const char *controller, const char *name) { |
70 | assert(a); | |
71 | ||
72 | if (controller) { | |
73 | if (streq(a->controller, controller) && (!name || streq(a->name, name))) | |
74 | return true; | |
75 | ||
76 | } else if (!name) | |
77 | return true; | |
78 | else if (streq(a->name, name)) { | |
79 | size_t x, y; | |
80 | x = strlen(a->controller); | |
81 | y = strlen(name); | |
82 | ||
83 | if (y > x && | |
84 | memcmp(a->controller, name, x) == 0 && | |
85 | name[x] == '.') | |
86 | return true; | |
87 | } | |
88 | ||
89 | return false; | |
90 | } | |
91 | ||
246aa6dd LP |
92 | CGroupAttribute *cgroup_attribute_find_list( |
93 | CGroupAttribute *first, | |
94 | const char *controller, | |
95 | const char *name) { | |
ab1f0633 LP |
96 | CGroupAttribute *a; |
97 | ||
ab1f0633 LP |
98 | assert(name); |
99 | ||
26d04f86 LP |
100 | LIST_FOREACH(by_unit, a, first) |
101 | if (cgroup_attribute_matches(a, controller, name)) | |
102 | return a; | |
ab1f0633 LP |
103 | |
104 | return NULL; | |
105 | } | |
106 | ||
246aa6dd | 107 | void cgroup_attribute_free(CGroupAttribute *a) { |
ab1f0633 LP |
108 | assert(a); |
109 | ||
246aa6dd LP |
110 | if (a->unit) |
111 | LIST_REMOVE(CGroupAttribute, by_unit, a->unit->cgroup_attributes, a); | |
112 | ||
ab1f0633 LP |
113 | free(a->controller); |
114 | free(a->name); | |
115 | free(a->value); | |
116 | free(a); | |
117 | } | |
118 | ||
119 | void cgroup_attribute_free_list(CGroupAttribute *first) { | |
120 | CGroupAttribute *a, *n; | |
121 | ||
122 | LIST_FOREACH_SAFE(by_unit, a, n, first) | |
123 | cgroup_attribute_free(a); | |
124 | } | |
26d04f86 LP |
125 | |
126 | void cgroup_attribute_free_some(CGroupAttribute *first, const char *controller, const char *name) { | |
127 | CGroupAttribute *a, *n; | |
128 | ||
129 | LIST_FOREACH_SAFE(by_unit, a, n, first) | |
130 | if (cgroup_attribute_matches(a, controller, name)) | |
131 | cgroup_attribute_free(a); | |
132 | } |