chiark / gitweb /
man: link in API FS documentation from the wiki
[elogind.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         char *path = NULL;
29         char *v = NULL;
30
31         assert(a);
32
33         b = cgroup_bonding_find_list(b, a->controller);
34         if (!b)
35                 return 0;
36
37         if (a->map_callback) {
38                 r = a->map_callback(a->controller, a->name, a->value, &v);
39                 if (r < 0)
40                         return r;
41         }
42
43         r = cg_get_path(a->controller, b->path, a->name, &path);
44         if (r < 0) {
45                 free(v);
46                 return r;
47         }
48
49         r = write_one_line_file(path, v ? v : a->value);
50         if (r < 0)
51                 log_warning("Failed to write '%s' to %s: %s", v ? v : a->value, path, strerror(-r));
52
53         free(path);
54         free(v);
55
56         return r;
57 }
58
59 int cgroup_attribute_apply_list(CGroupAttribute *first, CGroupBonding *b) {
60         CGroupAttribute *a;
61         int r = 0;
62
63         LIST_FOREACH(by_unit, a, first) {
64                 int k;
65
66                 k = cgroup_attribute_apply(a, b);
67                 if (r == 0)
68                         r = k;
69         }
70
71         return r;
72 }
73
74 CGroupAttribute *cgroup_attribute_find_list(
75                 CGroupAttribute *first,
76                 const char *controller,
77                 const char *name) {
78         CGroupAttribute *a;
79
80         assert(name);
81
82         LIST_FOREACH(by_unit, a, first) {
83
84
85                 if (controller) {
86                         if (streq(a->controller, controller) && streq(a->name, name))
87                                 return a;
88
89                 } else if (streq(a->name, name)) {
90                         size_t x, y;
91                         x = strlen(a->controller);
92                         y = strlen(name);
93
94                         if (y > x &&
95                             memcmp(a->controller, name, x) == 0 &&
96                             name[x] == '.')
97                                 return a;
98                 }
99         }
100
101         return NULL;
102 }
103
104 void cgroup_attribute_free(CGroupAttribute *a) {
105         assert(a);
106
107         if (a->unit)
108                 LIST_REMOVE(CGroupAttribute, by_unit, a->unit->cgroup_attributes, a);
109
110         free(a->controller);
111         free(a->name);
112         free(a->value);
113         free(a);
114 }
115
116 void cgroup_attribute_free_list(CGroupAttribute *first) {
117         CGroupAttribute *a, *n;
118
119         LIST_FOREACH_SAFE(by_unit, a, n, first)
120                 cgroup_attribute_free(a);
121 }