chiark / gitweb /
honor SELinux labels, when creating and writing config files
[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 #include "fileio.h"
26
27 int cgroup_attribute_apply(CGroupAttribute *a, CGroupBonding *b) {
28         int r;
29         _cleanup_free_ char *path = NULL, *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                 return r;
46
47         r = write_one_line_file(path, v ? v : a->value);
48         if (r < 0)
49                 log_warning("Failed to write '%s' to %s: %s", v ? v : a->value, path, strerror(-r));
50
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
69 CGroupAttribute *cgroup_attribute_find_list(
70                 CGroupAttribute *first,
71                 const char *controller,
72                 const char *name) {
73         CGroupAttribute *a;
74
75         assert(name);
76
77         LIST_FOREACH(by_unit, a, first) {
78
79
80                 if (controller) {
81                         if (streq(a->controller, controller) && streq(a->name, name))
82                                 return a;
83
84                 } else if (streq(a->name, name)) {
85                         size_t x, y;
86                         x = strlen(a->controller);
87                         y = strlen(name);
88
89                         if (y > x &&
90                             memcmp(a->controller, name, x) == 0 &&
91                             name[x] == '.')
92                                 return a;
93                 }
94         }
95
96         return NULL;
97 }
98
99 void cgroup_attribute_free(CGroupAttribute *a) {
100         assert(a);
101
102         if (a->unit)
103                 LIST_REMOVE(CGroupAttribute, by_unit, a->unit->cgroup_attributes, a);
104
105         free(a->controller);
106         free(a->name);
107         free(a->value);
108         free(a);
109 }
110
111 void cgroup_attribute_free_list(CGroupAttribute *first) {
112         CGroupAttribute *a, *n;
113
114         LIST_FOREACH_SAFE(by_unit, a, n, first)
115                 cgroup_attribute_free(a);
116 }