summaryrefslogtreecommitdiff
path: root/src/core/cgroup-semantics.c
blob: 82b02bbd78b3854bcd7cc0def8cb305b54e03512 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/

/***
  This file is part of systemd.

  Copyright 2013 Lennart Poettering

  systemd is free software; you can redistribute it and/or modify it
  under the terms of the GNU Lesser General Public License as published by
  the Free Software Foundation; either version 2.1 of the License, or
  (at your option) any later version.

  systemd is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public License
  along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/

#include "util.h"
#include "strv.h"
#include "path-util.h"
#include "cgroup-util.h"

#include "cgroup-semantics.h"

static int parse_cpu_shares(const CGroupSemantics *s, const char *value, char **ret) {
        unsigned long ul;

        assert(s);
        assert(value);
        assert(ret);

        if (safe_atolu(value, &ul) < 0 || ul < 1)
                return -EINVAL;

        if (asprintf(ret, "%lu", ul) < 0)
                return -ENOMEM;

        return 1;
}

static int parse_memory_limit(const CGroupSemantics *s, const char *value, char **ret) {
        off_t sz;

        assert(s);
        assert(value);
        assert(ret);

        if (parse_bytes(value, &sz) < 0 || sz <= 0)
                return -EINVAL;

        if (asprintf(ret, "%llu", (unsigned long long) sz) < 0)
                return -ENOMEM;

        return 1;
}

static int parse_device(const CGroupSemantics *s, const char *value, char **ret) {
        _cleanup_strv_free_ char **l = NULL;
        char *x;
        unsigned k;

        assert(s);
        assert(value);
        assert(ret);

        l = strv_split_quoted(value);
        if (!l)
                return -ENOMEM;

        k = strv_length(l);
        if (k < 1 || k > 2)
                return -EINVAL;

        if (!streq(l[0], "*") && !path_startswith(l[0], "/dev"))
                return -EINVAL;

        if (!isempty(l[1]) && !in_charset(l[1], "rwm"))
                return -EINVAL;

        x = strdup(value);
        if (!x)
                return -ENOMEM;

        *ret = x;
        return 1;
}

static int parse_blkio_weight(const CGroupSemantics *s, const char *value, char **ret) {
        _cleanup_strv_free_ char **l = NULL;
        unsigned long ul;

        assert(s);
        assert(value);
        assert(ret);

        l = strv_split_quoted(value);
        if (!l)
                return -ENOMEM;

        if (strv_length(l) != 1)
                return 0; /* Returning 0 will cause parse_blkio_weight_device() be tried instead */

        if (safe_atolu(l[0], &ul) < 0 || ul < 10 || ul > 1000)
                return -EINVAL;

        if (asprintf(ret, "%lu", ul) < 0)
                return -ENOMEM;

        return 1;
}

static int parse_blkio_weight_device(const CGroupSemantics *s, const char *value, char **ret) {
        _cleanup_strv_free_ char **l = NULL;
        unsigned long ul;

        assert(s);
        assert(value);
        assert(ret);

        l = strv_split_quoted(value);
        if (!l)
                return -ENOMEM;

        if (strv_length(l) != 2)
                return -EINVAL;

        if (!path_startswith(l[0], "/dev"))
                return -EINVAL;

        if (safe_atolu(l[1], &ul) < 0 || ul < 10 || ul > 1000)
                return -EINVAL;

        if (asprintf(ret, "%s %lu", l[0], ul) < 0)
                return -ENOMEM;

        return 1;
}

static int parse_blkio_bandwidth(const CGroupSemantics *s, const char *value, char **ret) {
        _cleanup_strv_free_ char **l = NULL;
        off_t bytes;

        assert(s);
        assert(value);
        assert(ret);

        l = strv_split_quoted(value);
        if (!l)
                return -ENOMEM;

        if (strv_length(l) != 2)
                return -EINVAL;

        if (!path_startswith(l[0], "/dev")) {
                return -EINVAL;
        }

        if (parse_bytes(l[1], &bytes) < 0 || bytes <= 0)
                return -EINVAL;

        if (asprintf(ret, "%s %llu", l[0], (unsigned long long) bytes) < 0)
                return -ENOMEM;

        return 0;
}

static int map_device(const CGroupSemantics *s, const char *value, char **ret) {
        _cleanup_strv_free_ char **l = NULL;
        unsigned k;

        assert(s);
        assert(value);
        assert(ret);

        l = strv_split_quoted(value);
        if (!l)
                return -ENOMEM;

        k = strv_length(l);
        if (k < 1 || k > 2)
                return -EINVAL;

        if (streq(l[0], "*")) {

                if (asprintf(ret, "a *:*%s%s",
                             isempty(l[1]) ? "" : " ", strempty(l[1])) < 0)
                        return -ENOMEM;
        } else {
                struct stat st;

                if (stat(l[0], &st) < 0) {
                        log_warning("Couldn't stat device %s", l[0]);
                        return -errno;
                }

                if (!S_ISCHR(st.st_mode) && !S_ISBLK(st.st_mode)) {
                        log_warning("%s is not a device.", l[0]);
                        return -ENODEV;
                }

                if (asprintf(ret, "%c %u:%u%s%s",
                             S_ISCHR(st.st_mode) ? 'c' : 'b',
                             major(st.st_rdev), minor(st.st_rdev),
                             isempty(l[1]) ? "" : " ", strempty(l[1])) < 0)
                        return -ENOMEM;
        }

        return 0;
}

static int map_blkio(const CGroupSemantics *s, const char *value, char **ret) {
        _cleanup_strv_free_ char **l = NULL;
        struct stat st;
        dev_t d;

        assert(s);
        assert(value);
        assert(ret);

        l = strv_split_quoted(value);
        if (!l)
                return log_oom();

        if (strv_length(l) != 2)
                return -EINVAL;

        if (stat(l[0], &st) < 0) {
                log_warning("Couldn't stat device %s", l[0]);
                return -errno;
        }

        if (S_ISBLK(st.st_mode))
                d = st.st_rdev;
        else if (major(st.st_dev) != 0) {
                /* If this is not a device node then find the block
                 * device this file is stored on */
                d = st.st_dev;

                /* If this is a partition, try to get the originating
                 * block device */
                block_get_whole_disk(d, &d);
        } else {
                log_warning("%s is not a block device and file system block device cannot be determined or is not local.", l[0]);
                return -ENODEV;
        }

        if (asprintf(ret, "%u:%u %s", major(d), minor(d), l[1]) < 0)
                return -ENOMEM;

        return 0;
}

static const CGroupSemantics semantics[] = {
        { "cpu",     "cpu.shares",                 "CPUShare",              false, parse_cpu_shares,          NULL,       NULL },
        { "memory",  "memory.soft_limit_in_bytes", "MemorySoftLimit",       false, parse_memory_limit,        NULL,       NULL },
        { "memory",  "memory.limit_in_bytes",      "MemoryLimit",           false, parse_memory_limit,        NULL,       NULL },
        { "devices", "devices.allow",              "DeviceAllow",           true,  parse_device,              map_device, NULL },
        { "devices", "devices.deny",               "DeviceDeny",            true,  parse_device,              map_device, NULL },
        { "blkio",   "blkio.weight",               "BlockIOWeight",         false, parse_blkio_weight,        NULL,       NULL },
        { "blkio",   "blkio.weight_device",        "BlockIOWeight",         true,  parse_blkio_weight_device, map_blkio,  NULL },
        { "blkio",   "blkio.read_bps_device",      "BlockIOReadBandwidth",  true,  parse_blkio_bandwidth,     map_blkio,  NULL },
        { "blkio",   "blkio.write_bps_device",     "BlockIOWriteBandwidth", true,  parse_blkio_bandwidth,     map_blkio,  NULL }
};

int cgroup_semantics_find(
                const char *controller,
                const char *name,
                const char *value,
                char **ret,
                const CGroupSemantics **_s) {

        _cleanup_free_ char *c = NULL;
        unsigned i;
        int r;

        assert(name);
        assert(_s);
        assert(!value == !ret);

        if (!controller) {
                r = cg_controller_from_attr(name, &c);
                if (r < 0)
                        return r;

                controller = c;
        }

        for (i = 0; i < ELEMENTSOF(semantics); i++) {
                const CGroupSemantics *s = semantics + i;
                bool matches_name, matches_pretty;

                if (controller && s->controller && !streq(s->controller, controller))
                        continue;

                matches_name = s->name && streq(s->name, name);
                matches_pretty = s->pretty && streq(s->pretty, name);

                if (!matches_name && !matches_pretty)
                        continue;

                if (value) {
                        if (matches_pretty && s->map_pretty) {

                                r = s->map_pretty(s, value, ret);
                                if (r < 0)
                                        return r;

                                if (r == 0)
                                        continue;

                        } else {
                                char *x;

                                x = strdup(value);
                                if (!x)
                                        return -ENOMEM;

                                *ret = x;
                        }
                }

                *_s = s;
                return 1;
        }

        *ret = NULL;
        *_s = NULL;
        return 0;
}