]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/cgroup-attr.c
aed4e99d864d6bf74f6a98d26e086af06f14247f
[thirdparty/systemd.git] / src / core / cgroup-attr.c
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
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
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
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
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"
25
26 int cgroup_attribute_apply(CGroupAttribute *a, CGroupBonding *b) {
27 int r;
28 _cleanup_free_ char *path = NULL, *v = NULL;
29
30 assert(a);
31
32 b = cgroup_bonding_find_list(b, a->controller);
33 if (!b)
34 return 0;
35
36 if (a->map_callback) {
37 r = a->map_callback(a->controller, a->name, a->value, &v);
38 if (r < 0)
39 return r;
40 }
41
42 r = cg_get_path(a->controller, b->path, a->name, &path);
43 if (r < 0)
44 return r;
45
46 r = write_one_line_file(path, v ? v : a->value);
47 if (r < 0)
48 log_warning("Failed to write '%s' to %s: %s", v ? v : a->value, path, strerror(-r));
49
50 return r;
51 }
52
53 int cgroup_attribute_apply_list(CGroupAttribute *first, CGroupBonding *b) {
54 CGroupAttribute *a;
55 int r = 0;
56
57 LIST_FOREACH(by_unit, a, first) {
58 int k;
59
60 k = cgroup_attribute_apply(a, b);
61 if (r == 0)
62 r = k;
63 }
64
65 return r;
66 }
67
68 CGroupAttribute *cgroup_attribute_find_list(
69 CGroupAttribute *first,
70 const char *controller,
71 const char *name) {
72 CGroupAttribute *a;
73
74 assert(name);
75
76 LIST_FOREACH(by_unit, a, first) {
77
78
79 if (controller) {
80 if (streq(a->controller, controller) && streq(a->name, name))
81 return a;
82
83 } else if (streq(a->name, name)) {
84 size_t x, y;
85 x = strlen(a->controller);
86 y = strlen(name);
87
88 if (y > x &&
89 memcmp(a->controller, name, x) == 0 &&
90 name[x] == '.')
91 return a;
92 }
93 }
94
95 return NULL;
96 }
97
98 void cgroup_attribute_free(CGroupAttribute *a) {
99 assert(a);
100
101 if (a->unit)
102 LIST_REMOVE(CGroupAttribute, by_unit, a->unit->cgroup_attributes, a);
103
104 free(a->controller);
105 free(a->name);
106 free(a->value);
107 free(a);
108 }
109
110 void cgroup_attribute_free_list(CGroupAttribute *first) {
111 CGroupAttribute *a, *n;
112
113 LIST_FOREACH_SAFE(by_unit, a, n, first)
114 cgroup_attribute_free(a);
115 }