chiark / gitweb /
Prep v230: Apply missing upstream fixes and updates (4/8) src/core.
[elogind.git] / src / core / cgroup.c
1 /***
2   This file is part of systemd.
3
4   Copyright 2013 Lennart Poettering
5
6   systemd is free software; you can redistribute it and/or modify it
7   under the terms of the GNU Lesser General Public License as published by
8   the Free Software Foundation; either version 2.1 of the License, or
9   (at your option) any later version.
10
11   systemd is distributed in the hope that it will be useful, but
12   WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14   Lesser General Public License for more details.
15
16   You should have received a copy of the GNU Lesser General Public License
17   along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include <fcntl.h>
21 #include <fnmatch.h>
22
23 #include "alloc-util.h"
24 #include "cgroup-util.h"
25 #include "cgroup.h"
26 #include "fd-util.h"
27 #include "fileio.h"
28 #include "fs-util.h"
29 #include "parse-util.h"
30 #include "path-util.h"
31 #include "process-util.h"
32 //#include "special.h"
33 #include "string-table.h"
34 #include "string-util.h"
35
36 #define CGROUP_CPU_QUOTA_PERIOD_USEC ((usec_t) 100 * USEC_PER_MSEC)
37
38 #if 0 /// UNNEEDED by elogind
39 void cgroup_context_init(CGroupContext *c) {
40         assert(c);
41
42         /* Initialize everything to the kernel defaults, assuming the
43          * structure is preinitialized to 0 */
44
45         c->cpu_shares = CGROUP_CPU_SHARES_INVALID;
46         c->startup_cpu_shares = CGROUP_CPU_SHARES_INVALID;
47         c->cpu_quota_per_sec_usec = USEC_INFINITY;
48
49         c->memory_limit = (uint64_t) -1;
50
51         c->io_weight = CGROUP_WEIGHT_INVALID;
52         c->startup_io_weight = CGROUP_WEIGHT_INVALID;
53
54         c->blockio_weight = CGROUP_BLKIO_WEIGHT_INVALID;
55         c->startup_blockio_weight = CGROUP_BLKIO_WEIGHT_INVALID;
56
57         c->tasks_max = (uint64_t) -1;
58 }
59
60 void cgroup_context_free_device_allow(CGroupContext *c, CGroupDeviceAllow *a) {
61         assert(c);
62         assert(a);
63
64         LIST_REMOVE(device_allow, c->device_allow, a);
65         free(a->path);
66         free(a);
67 }
68
69 void cgroup_context_free_io_device_weight(CGroupContext *c, CGroupIODeviceWeight *w) {
70         assert(c);
71         assert(w);
72
73         LIST_REMOVE(device_weights, c->io_device_weights, w);
74         free(w->path);
75         free(w);
76 }
77
78 void cgroup_context_free_io_device_limit(CGroupContext *c, CGroupIODeviceLimit *l) {
79         assert(c);
80         assert(l);
81
82         LIST_REMOVE(device_limits, c->io_device_limits, l);
83         free(l->path);
84         free(l);
85 }
86
87 void cgroup_context_free_blockio_device_weight(CGroupContext *c, CGroupBlockIODeviceWeight *w) {
88         assert(c);
89         assert(w);
90
91         LIST_REMOVE(device_weights, c->blockio_device_weights, w);
92         free(w->path);
93         free(w);
94 }
95
96 void cgroup_context_free_blockio_device_bandwidth(CGroupContext *c, CGroupBlockIODeviceBandwidth *b) {
97         assert(c);
98         assert(b);
99
100         LIST_REMOVE(device_bandwidths, c->blockio_device_bandwidths, b);
101         free(b->path);
102         free(b);
103 }
104
105 void cgroup_context_done(CGroupContext *c) {
106         assert(c);
107
108         while (c->io_device_weights)
109                 cgroup_context_free_io_device_weight(c, c->io_device_weights);
110
111         while (c->io_device_limits)
112                 cgroup_context_free_io_device_limit(c, c->io_device_limits);
113
114         while (c->blockio_device_weights)
115                 cgroup_context_free_blockio_device_weight(c, c->blockio_device_weights);
116
117         while (c->blockio_device_bandwidths)
118                 cgroup_context_free_blockio_device_bandwidth(c, c->blockio_device_bandwidths);
119
120         while (c->device_allow)
121                 cgroup_context_free_device_allow(c, c->device_allow);
122 }
123
124 void cgroup_context_dump(CGroupContext *c, FILE* f, const char *prefix) {
125         CGroupIODeviceLimit *il;
126         CGroupIODeviceWeight *iw;
127         CGroupBlockIODeviceBandwidth *b;
128         CGroupBlockIODeviceWeight *w;
129         CGroupDeviceAllow *a;
130         char u[FORMAT_TIMESPAN_MAX];
131
132         assert(c);
133         assert(f);
134
135         prefix = strempty(prefix);
136
137         fprintf(f,
138                 "%sCPUAccounting=%s\n"
139                 "%sIOAccounting=%s\n"
140                 "%sBlockIOAccounting=%s\n"
141                 "%sMemoryAccounting=%s\n"
142                 "%sTasksAccounting=%s\n"
143                 "%sCPUShares=%" PRIu64 "\n"
144                 "%sStartupCPUShares=%" PRIu64 "\n"
145                 "%sCPUQuotaPerSecSec=%s\n"
146                 "%sIOWeight=%" PRIu64 "\n"
147                 "%sStartupIOWeight=%" PRIu64 "\n"
148                 "%sBlockIOWeight=%" PRIu64 "\n"
149                 "%sStartupBlockIOWeight=%" PRIu64 "\n"
150                 "%sMemoryLimit=%" PRIu64 "\n"
151                 "%sTasksMax=%" PRIu64 "\n"
152                 "%sDevicePolicy=%s\n"
153                 "%sDelegate=%s\n",
154                 prefix, yes_no(c->cpu_accounting),
155                 prefix, yes_no(c->io_accounting),
156                 prefix, yes_no(c->blockio_accounting),
157                 prefix, yes_no(c->memory_accounting),
158                 prefix, yes_no(c->tasks_accounting),
159                 prefix, c->cpu_shares,
160                 prefix, c->startup_cpu_shares,
161                 prefix, format_timespan(u, sizeof(u), c->cpu_quota_per_sec_usec, 1),
162                 prefix, c->io_weight,
163                 prefix, c->startup_io_weight,
164                 prefix, c->blockio_weight,
165                 prefix, c->startup_blockio_weight,
166                 prefix, c->memory_limit,
167                 prefix, c->tasks_max,
168                 prefix, cgroup_device_policy_to_string(c->device_policy),
169                 prefix, yes_no(c->delegate));
170
171         LIST_FOREACH(device_allow, a, c->device_allow)
172                 fprintf(f,
173                         "%sDeviceAllow=%s %s%s%s\n",
174                         prefix,
175                         a->path,
176                         a->r ? "r" : "", a->w ? "w" : "", a->m ? "m" : "");
177
178         LIST_FOREACH(device_weights, iw, c->io_device_weights)
179                 fprintf(f,
180                         "%sIODeviceWeight=%s %" PRIu64,
181                         prefix,
182                         iw->path,
183                         iw->weight);
184
185         LIST_FOREACH(device_limits, il, c->io_device_limits) {
186                 char buf[FORMAT_BYTES_MAX];
187                 CGroupIOLimitType type;
188
189                 for (type = 0; type < _CGROUP_IO_LIMIT_TYPE_MAX; type++)
190                         if (il->limits[type] != cgroup_io_limit_defaults[type])
191                                 fprintf(f,
192                                         "%s%s=%s %s\n",
193                                         prefix,
194                                         cgroup_io_limit_type_to_string(type),
195                                         il->path,
196                                         format_bytes(buf, sizeof(buf), il->limits[type]));
197         }
198
199         LIST_FOREACH(device_weights, w, c->blockio_device_weights)
200                 fprintf(f,
201                         "%sBlockIODeviceWeight=%s %" PRIu64,
202                         prefix,
203                         w->path,
204                         w->weight);
205
206         LIST_FOREACH(device_bandwidths, b, c->blockio_device_bandwidths) {
207                 char buf[FORMAT_BYTES_MAX];
208
209                 if (b->rbps != CGROUP_LIMIT_MAX)
210                         fprintf(f,
211                                 "%sBlockIOReadBandwidth=%s %s\n",
212                                 prefix,
213                                 b->path,
214                                 format_bytes(buf, sizeof(buf), b->rbps));
215                 if (b->wbps != CGROUP_LIMIT_MAX)
216                         fprintf(f,
217                                 "%sBlockIOWriteBandwidth=%s %s\n",
218                                 prefix,
219                                 b->path,
220                                 format_bytes(buf, sizeof(buf), b->wbps));
221         }
222 }
223
224 static int lookup_block_device(const char *p, dev_t *dev) {
225         struct stat st;
226         int r;
227
228         assert(p);
229         assert(dev);
230
231         r = stat(p, &st);
232         if (r < 0)
233                 return log_warning_errno(errno, "Couldn't stat device %s: %m", p);
234
235         if (S_ISBLK(st.st_mode))
236                 *dev = st.st_rdev;
237         else if (major(st.st_dev) != 0) {
238                 /* If this is not a device node then find the block
239                  * device this file is stored on */
240                 *dev = st.st_dev;
241
242                 /* If this is a partition, try to get the originating
243                  * block device */
244                 block_get_whole_disk(*dev, dev);
245         } else {
246                 log_warning("%s is not a block device and file system block device cannot be determined or is not local.", p);
247                 return -ENODEV;
248         }
249
250         return 0;
251 }
252
253 static int whitelist_device(const char *path, const char *node, const char *acc) {
254         char buf[2+DECIMAL_STR_MAX(dev_t)*2+2+4];
255         struct stat st;
256         int r;
257
258         assert(path);
259         assert(acc);
260
261         if (stat(node, &st) < 0) {
262                 log_warning("Couldn't stat device %s", node);
263                 return -errno;
264         }
265
266         if (!S_ISCHR(st.st_mode) && !S_ISBLK(st.st_mode)) {
267                 log_warning("%s is not a device.", node);
268                 return -ENODEV;
269         }
270
271         sprintf(buf,
272                 "%c %u:%u %s",
273                 S_ISCHR(st.st_mode) ? 'c' : 'b',
274                 major(st.st_rdev), minor(st.st_rdev),
275                 acc);
276
277         r = cg_set_attribute("devices", path, "devices.allow", buf);
278         if (r < 0)
279                 log_full_errno(IN_SET(r, -ENOENT, -EROFS, -EINVAL, -EACCES) ? LOG_DEBUG : LOG_WARNING, r,
280                                "Failed to set devices.allow on %s: %m", path);
281
282         return r;
283 }
284
285 static int whitelist_major(const char *path, const char *name, char type, const char *acc) {
286         _cleanup_fclose_ FILE *f = NULL;
287         char line[LINE_MAX];
288         bool good = false;
289         int r;
290
291         assert(path);
292         assert(acc);
293         assert(type == 'b' || type == 'c');
294
295         f = fopen("/proc/devices", "re");
296         if (!f)
297                 return log_warning_errno(errno, "Cannot open /proc/devices to resolve %s (%c): %m", name, type);
298
299         FOREACH_LINE(line, f, goto fail) {
300                 char buf[2+DECIMAL_STR_MAX(unsigned)+3+4], *p, *w;
301                 unsigned maj;
302
303                 truncate_nl(line);
304
305                 if (type == 'c' && streq(line, "Character devices:")) {
306                         good = true;
307                         continue;
308                 }
309
310                 if (type == 'b' && streq(line, "Block devices:")) {
311                         good = true;
312                         continue;
313                 }
314
315                 if (isempty(line)) {
316                         good = false;
317                         continue;
318                 }
319
320                 if (!good)
321                         continue;
322
323                 p = strstrip(line);
324
325                 w = strpbrk(p, WHITESPACE);
326                 if (!w)
327                         continue;
328                 *w = 0;
329
330                 r = safe_atou(p, &maj);
331                 if (r < 0)
332                         continue;
333                 if (maj <= 0)
334                         continue;
335
336                 w++;
337                 w += strspn(w, WHITESPACE);
338
339                 if (fnmatch(name, w, 0) != 0)
340                         continue;
341
342                 sprintf(buf,
343                         "%c %u:* %s",
344                         type,
345                         maj,
346                         acc);
347
348                 r = cg_set_attribute("devices", path, "devices.allow", buf);
349                 if (r < 0)
350                         log_full_errno(IN_SET(r, -ENOENT, -EROFS, -EINVAL, -EACCES) ? LOG_DEBUG : LOG_WARNING, r,
351                                        "Failed to set devices.allow on %s: %m", path);
352         }
353
354         return 0;
355
356 fail:
357         log_warning_errno(errno, "Failed to read /proc/devices: %m");
358         return -errno;
359 }
360
361 static bool cgroup_context_has_io_config(CGroupContext *c) {
362         return c->io_accounting ||
363                 c->io_weight != CGROUP_WEIGHT_INVALID ||
364                 c->startup_io_weight != CGROUP_WEIGHT_INVALID ||
365                 c->io_device_weights ||
366                 c->io_device_limits;
367 }
368
369 static bool cgroup_context_has_blockio_config(CGroupContext *c) {
370         return c->blockio_accounting ||
371                 c->blockio_weight != CGROUP_BLKIO_WEIGHT_INVALID ||
372                 c->startup_blockio_weight != CGROUP_BLKIO_WEIGHT_INVALID ||
373                 c->blockio_device_weights ||
374                 c->blockio_device_bandwidths;
375 }
376
377 static uint64_t cgroup_context_io_weight(CGroupContext *c, ManagerState state) {
378         if (IN_SET(state, MANAGER_STARTING, MANAGER_INITIALIZING) &&
379             c->startup_io_weight != CGROUP_WEIGHT_INVALID)
380                 return c->startup_io_weight;
381         else if (c->io_weight != CGROUP_WEIGHT_INVALID)
382                 return c->io_weight;
383         else
384                 return CGROUP_WEIGHT_DEFAULT;
385 }
386
387 static uint64_t cgroup_context_blkio_weight(CGroupContext *c, ManagerState state) {
388         if (IN_SET(state, MANAGER_STARTING, MANAGER_INITIALIZING) &&
389             c->startup_blockio_weight != CGROUP_BLKIO_WEIGHT_INVALID)
390                 return c->startup_blockio_weight;
391         else if (c->blockio_weight != CGROUP_BLKIO_WEIGHT_INVALID)
392                 return c->blockio_weight;
393         else
394                 return CGROUP_BLKIO_WEIGHT_DEFAULT;
395 }
396
397 static uint64_t cgroup_weight_blkio_to_io(uint64_t blkio_weight) {
398         return CLAMP(blkio_weight * CGROUP_WEIGHT_DEFAULT / CGROUP_BLKIO_WEIGHT_DEFAULT,
399                      CGROUP_WEIGHT_MIN, CGROUP_WEIGHT_MAX);
400 }
401
402 static uint64_t cgroup_weight_io_to_blkio(uint64_t io_weight) {
403         return CLAMP(io_weight * CGROUP_BLKIO_WEIGHT_DEFAULT / CGROUP_WEIGHT_DEFAULT,
404                      CGROUP_BLKIO_WEIGHT_MIN, CGROUP_BLKIO_WEIGHT_MAX);
405 }
406
407 static void cgroup_apply_io_device_weight(const char *path, const char *dev_path, uint64_t io_weight) {
408         char buf[DECIMAL_STR_MAX(dev_t)*2+2+DECIMAL_STR_MAX(uint64_t)+1];
409         dev_t dev;
410         int r;
411
412         r = lookup_block_device(dev_path, &dev);
413         if (r < 0)
414                 return;
415
416         xsprintf(buf, "%u:%u %" PRIu64 "\n", major(dev), minor(dev), io_weight);
417         r = cg_set_attribute("io", path, "io.weight", buf);
418         if (r < 0)
419                 log_full_errno(IN_SET(r, -ENOENT, -EROFS, -EACCES) ? LOG_DEBUG : LOG_WARNING, r,
420                                "Failed to set io.weight on %s: %m", path);
421 }
422
423 static void cgroup_apply_blkio_device_weight(const char *path, const char *dev_path, uint64_t blkio_weight) {
424         char buf[DECIMAL_STR_MAX(dev_t)*2+2+DECIMAL_STR_MAX(uint64_t)+1];
425         dev_t dev;
426         int r;
427
428         r = lookup_block_device(dev_path, &dev);
429         if (r < 0)
430                 return;
431
432         xsprintf(buf, "%u:%u %" PRIu64 "\n", major(dev), minor(dev), blkio_weight);
433         r = cg_set_attribute("blkio", path, "blkio.weight_device", buf);
434         if (r < 0)
435                 log_full_errno(IN_SET(r, -ENOENT, -EROFS, -EACCES) ? LOG_DEBUG : LOG_WARNING, r,
436                                "Failed to set blkio.weight_device on %s: %m", path);
437 }
438
439 static unsigned cgroup_apply_io_device_limit(const char *path, const char *dev_path, uint64_t *limits) {
440         char limit_bufs[_CGROUP_IO_LIMIT_TYPE_MAX][DECIMAL_STR_MAX(uint64_t)];
441         char buf[DECIMAL_STR_MAX(dev_t)*2+2+(6+DECIMAL_STR_MAX(uint64_t)+1)*4];
442         CGroupIOLimitType type;
443         dev_t dev;
444         unsigned n = 0;
445         int r;
446
447         r = lookup_block_device(dev_path, &dev);
448         if (r < 0)
449                 return 0;
450
451         for (type = 0; type < _CGROUP_IO_LIMIT_TYPE_MAX; type++) {
452                 if (limits[type] != cgroup_io_limit_defaults[type]) {
453                         xsprintf(limit_bufs[type], "%" PRIu64, limits[type]);
454                         n++;
455                 } else {
456                         xsprintf(limit_bufs[type], "%s", limits[type] == CGROUP_LIMIT_MAX ? "max" : "0");
457                 }
458         }
459
460         xsprintf(buf, "%u:%u rbps=%s wbps=%s riops=%s wiops=%s\n", major(dev), minor(dev),
461                  limit_bufs[CGROUP_IO_RBPS_MAX], limit_bufs[CGROUP_IO_WBPS_MAX],
462                  limit_bufs[CGROUP_IO_RIOPS_MAX], limit_bufs[CGROUP_IO_WIOPS_MAX]);
463         r = cg_set_attribute("io", path, "io.max", buf);
464         if (r < 0)
465                 log_full_errno(IN_SET(r, -ENOENT, -EROFS, -EACCES) ? LOG_DEBUG : LOG_WARNING, r,
466                                "Failed to set io.max on %s: %m", path);
467         return n;
468 }
469
470 static unsigned cgroup_apply_blkio_device_limit(const char *path, const char *dev_path, uint64_t rbps, uint64_t wbps) {
471         char buf[DECIMAL_STR_MAX(dev_t)*2+2+DECIMAL_STR_MAX(uint64_t)+1];
472         dev_t dev;
473         unsigned n = 0;
474         int r;
475
476         r = lookup_block_device(dev_path, &dev);
477         if (r < 0)
478                 return 0;
479
480         if (rbps != CGROUP_LIMIT_MAX)
481                 n++;
482         sprintf(buf, "%u:%u %" PRIu64 "\n", major(dev), minor(dev), rbps);
483         r = cg_set_attribute("blkio", path, "blkio.throttle.read_bps_device", buf);
484         if (r < 0)
485                 log_full_errno(IN_SET(r, -ENOENT, -EROFS, -EACCES) ? LOG_DEBUG : LOG_WARNING, r,
486                                "Failed to set blkio.throttle.read_bps_device on %s: %m", path);
487
488         if (wbps != CGROUP_LIMIT_MAX)
489                 n++;
490         sprintf(buf, "%u:%u %" PRIu64 "\n", major(dev), minor(dev), wbps);
491         r = cg_set_attribute("blkio", path, "blkio.throttle.write_bps_device", buf);
492         if (r < 0)
493                 log_full_errno(IN_SET(r, -ENOENT, -EROFS, -EACCES) ? LOG_DEBUG : LOG_WARNING, r,
494                                "Failed to set blkio.throttle.write_bps_device on %s: %m", path);
495
496         return n;
497 }
498
499 void cgroup_context_apply(CGroupContext *c, CGroupMask mask, const char *path, ManagerState state) {
500         bool is_root;
501         int r;
502
503         assert(c);
504         assert(path);
505
506         if (mask == 0)
507                 return;
508
509         /* Some cgroup attributes are not supported on the root cgroup,
510          * hence silently ignore */
511         is_root = isempty(path) || path_equal(path, "/");
512         if (is_root)
513                 /* Make sure we don't try to display messages with an empty path. */
514                 path = "/";
515
516         /* We generally ignore errors caused by read-only mounted
517          * cgroup trees (assuming we are running in a container then),
518          * and missing cgroups, i.e. EROFS and ENOENT. */
519
520         if ((mask & CGROUP_MASK_CPU) && !is_root) {
521                 char buf[MAX(DECIMAL_STR_MAX(uint64_t), DECIMAL_STR_MAX(usec_t)) + 1];
522
523                 sprintf(buf, "%" PRIu64 "\n",
524                         IN_SET(state, MANAGER_STARTING, MANAGER_INITIALIZING) && c->startup_cpu_shares != CGROUP_CPU_SHARES_INVALID ? c->startup_cpu_shares :
525                         c->cpu_shares != CGROUP_CPU_SHARES_INVALID ? c->cpu_shares : CGROUP_CPU_SHARES_DEFAULT);
526                 r = cg_set_attribute("cpu", path, "cpu.shares", buf);
527                 if (r < 0)
528                         log_full_errno(IN_SET(r, -ENOENT, -EROFS, -EACCES) ? LOG_DEBUG : LOG_WARNING, r,
529                                        "Failed to set cpu.shares on %s: %m", path);
530
531                 sprintf(buf, USEC_FMT "\n", CGROUP_CPU_QUOTA_PERIOD_USEC);
532                 r = cg_set_attribute("cpu", path, "cpu.cfs_period_us", buf);
533                 if (r < 0)
534                         log_full_errno(IN_SET(r, -ENOENT, -EROFS, -EACCES) ? LOG_DEBUG : LOG_WARNING, r,
535                                        "Failed to set cpu.cfs_period_us on %s: %m", path);
536
537                 if (c->cpu_quota_per_sec_usec != USEC_INFINITY) {
538                         sprintf(buf, USEC_FMT "\n", c->cpu_quota_per_sec_usec * CGROUP_CPU_QUOTA_PERIOD_USEC / USEC_PER_SEC);
539                         r = cg_set_attribute("cpu", path, "cpu.cfs_quota_us", buf);
540                 } else
541                         r = cg_set_attribute("cpu", path, "cpu.cfs_quota_us", "-1");
542                 if (r < 0)
543                         log_full_errno(IN_SET(r, -ENOENT, -EROFS, -EACCES) ? LOG_DEBUG : LOG_WARNING, r,
544                                        "Failed to set cpu.cfs_quota_us on %s: %m", path);
545         }
546
547         if (mask & CGROUP_MASK_IO) {
548                 bool has_io = cgroup_context_has_io_config(c);
549                 bool has_blockio = cgroup_context_has_blockio_config(c);
550
551                 if (!is_root) {
552                         char buf[8+DECIMAL_STR_MAX(uint64_t)+1];
553                         uint64_t weight;
554
555                         if (has_io)
556                                 weight = cgroup_context_io_weight(c, state);
557                         else if (has_blockio)
558                                 weight = cgroup_weight_blkio_to_io(cgroup_context_blkio_weight(c, state));
559                         else
560                                 weight = CGROUP_WEIGHT_DEFAULT;
561
562                         xsprintf(buf, "default %" PRIu64 "\n", weight);
563                         r = cg_set_attribute("io", path, "io.weight", buf);
564                         if (r < 0)
565                                 log_full_errno(IN_SET(r, -ENOENT, -EROFS, -EACCES) ? LOG_DEBUG : LOG_WARNING, r,
566                                                "Failed to set io.weight on %s: %m", path);
567
568                         if (has_io) {
569                                 CGroupIODeviceWeight *w;
570
571                                 /* FIXME: no way to reset this list */
572                                 LIST_FOREACH(device_weights, w, c->io_device_weights)
573                                         cgroup_apply_io_device_weight(path, w->path, w->weight);
574                         } else if (has_blockio) {
575                                 CGroupBlockIODeviceWeight *w;
576
577                                 /* FIXME: no way to reset this list */
578                                 LIST_FOREACH(device_weights, w, c->blockio_device_weights)
579                                         cgroup_apply_io_device_weight(path, w->path, cgroup_weight_blkio_to_io(w->weight));
580                         }
581                 }
582
583                 /* Apply limits and free ones without config. */
584                 if (has_io) {
585                         CGroupIODeviceLimit *l, *next;
586
587                         LIST_FOREACH_SAFE(device_limits, l, next, c->io_device_limits) {
588                                 if (!cgroup_apply_io_device_limit(path, l->path, l->limits))
589                                         cgroup_context_free_io_device_limit(c, l);
590                         }
591                 } else if (has_blockio) {
592                         CGroupBlockIODeviceBandwidth *b, *next;
593
594                         LIST_FOREACH_SAFE(device_bandwidths, b, next, c->blockio_device_bandwidths) {
595                                 uint64_t limits[_CGROUP_IO_LIMIT_TYPE_MAX];
596                                 CGroupIOLimitType type;
597
598                                 for (type = 0; type < _CGROUP_IO_LIMIT_TYPE_MAX; type++)
599                                         limits[type] = cgroup_io_limit_defaults[type];
600
601                                 limits[CGROUP_IO_RBPS_MAX] = b->rbps;
602                                 limits[CGROUP_IO_WBPS_MAX] = b->wbps;
603
604                                 if (!cgroup_apply_io_device_limit(path, b->path, limits))
605                                         cgroup_context_free_blockio_device_bandwidth(c, b);
606                         }
607                 }
608         }
609
610         if (mask & CGROUP_MASK_BLKIO) {
611                 bool has_io = cgroup_context_has_io_config(c);
612                 bool has_blockio = cgroup_context_has_blockio_config(c);
613
614                 if (!is_root) {
615                         char buf[DECIMAL_STR_MAX(uint64_t)+1];
616                         uint64_t weight;
617
618                         if (has_blockio)
619                                 weight = cgroup_context_blkio_weight(c, state);
620                         else if (has_io)
621                                 weight = cgroup_weight_io_to_blkio(cgroup_context_io_weight(c, state));
622                         else
623                                 weight = CGROUP_BLKIO_WEIGHT_DEFAULT;
624
625                         xsprintf(buf, "%" PRIu64 "\n", weight);
626                         r = cg_set_attribute("blkio", path, "blkio.weight", buf);
627                         if (r < 0)
628                                 log_full_errno(IN_SET(r, -ENOENT, -EROFS, -EACCES) ? LOG_DEBUG : LOG_WARNING, r,
629                                                "Failed to set blkio.weight on %s: %m", path);
630
631                         if (has_blockio) {
632                                 CGroupBlockIODeviceWeight *w;
633
634                                 /* FIXME: no way to reset this list */
635                                 LIST_FOREACH(device_weights, w, c->blockio_device_weights)
636                                         cgroup_apply_blkio_device_weight(path, w->path, w->weight);
637                         } else if (has_io) {
638                                 CGroupIODeviceWeight *w;
639
640                                 /* FIXME: no way to reset this list */
641                                 LIST_FOREACH(device_weights, w, c->io_device_weights)
642                                         cgroup_apply_blkio_device_weight(path, w->path, cgroup_weight_io_to_blkio(w->weight));
643                         }
644                 }
645
646                 /* Apply limits and free ones without config. */
647                 if (has_blockio) {
648                         CGroupBlockIODeviceBandwidth *b, *next;
649
650                         LIST_FOREACH_SAFE(device_bandwidths, b, next, c->blockio_device_bandwidths) {
651                                 if (!cgroup_apply_blkio_device_limit(path, b->path, b->rbps, b->wbps))
652                                         cgroup_context_free_blockio_device_bandwidth(c, b);
653                         }
654                 } else if (has_io) {
655                         CGroupIODeviceLimit *l, *next;
656
657                         LIST_FOREACH_SAFE(device_limits, l, next, c->io_device_limits) {
658                                 if (!cgroup_apply_blkio_device_limit(path, l->path, l->limits[CGROUP_IO_RBPS_MAX], l->limits[CGROUP_IO_WBPS_MAX]))
659                                         cgroup_context_free_io_device_limit(c, l);
660                         }
661                 }
662         }
663
664         if ((mask & CGROUP_MASK_MEMORY) && !is_root) {
665                 if (c->memory_limit != (uint64_t) -1) {
666                         char buf[DECIMAL_STR_MAX(uint64_t) + 1];
667
668                         sprintf(buf, "%" PRIu64 "\n", c->memory_limit);
669
670                         if (cg_unified() <= 0)
671                                 r = cg_set_attribute("memory", path, "memory.limit_in_bytes", buf);
672                         else
673                                 r = cg_set_attribute("memory", path, "memory.max", buf);
674
675                 } else {
676                         if (cg_unified() <= 0)
677                                 r = cg_set_attribute("memory", path, "memory.limit_in_bytes", "-1");
678                         else
679                                 r = cg_set_attribute("memory", path, "memory.max", "max");
680                 }
681
682                 if (r < 0)
683                         log_full_errno(IN_SET(r, -ENOENT, -EROFS, -EACCES) ? LOG_DEBUG : LOG_WARNING, r,
684                                        "Failed to set memory.limit_in_bytes/memory.max on %s: %m", path);
685         }
686
687         if ((mask & CGROUP_MASK_DEVICES) && !is_root) {
688                 CGroupDeviceAllow *a;
689
690                 /* Changing the devices list of a populated cgroup
691                  * might result in EINVAL, hence ignore EINVAL
692                  * here. */
693
694                 if (c->device_allow || c->device_policy != CGROUP_AUTO)
695                         r = cg_set_attribute("devices", path, "devices.deny", "a");
696                 else
697                         r = cg_set_attribute("devices", path, "devices.allow", "a");
698                 if (r < 0)
699                         log_full_errno(IN_SET(r, -ENOENT, -EROFS, -EINVAL, -EACCES) ? LOG_DEBUG : LOG_WARNING, r,
700                                        "Failed to reset devices.list on %s: %m", path);
701
702                 if (c->device_policy == CGROUP_CLOSED ||
703                     (c->device_policy == CGROUP_AUTO && c->device_allow)) {
704                         static const char auto_devices[] =
705                                 "/dev/null\0" "rwm\0"
706                                 "/dev/zero\0" "rwm\0"
707                                 "/dev/full\0" "rwm\0"
708                                 "/dev/random\0" "rwm\0"
709                                 "/dev/urandom\0" "rwm\0"
710                                 "/dev/tty\0" "rwm\0"
711                                 "/dev/pts/ptmx\0" "rw\0"; /* /dev/pts/ptmx may not be duplicated, but accessed */
712
713                         const char *x, *y;
714
715                         NULSTR_FOREACH_PAIR(x, y, auto_devices)
716                                 whitelist_device(path, x, y);
717
718                         whitelist_major(path, "pts", 'c', "rw");
719                         whitelist_major(path, "kdbus", 'c', "rw");
720                         whitelist_major(path, "kdbus/*", 'c', "rw");
721                 }
722
723                 LIST_FOREACH(device_allow, a, c->device_allow) {
724                         char acc[4];
725                         unsigned k = 0;
726
727                         if (a->r)
728                                 acc[k++] = 'r';
729                         if (a->w)
730                                 acc[k++] = 'w';
731                         if (a->m)
732                                 acc[k++] = 'm';
733
734                         if (k == 0)
735                                 continue;
736
737                         acc[k++] = 0;
738
739                         if (startswith(a->path, "/dev/"))
740                                 whitelist_device(path, a->path, acc);
741                         else if (startswith(a->path, "block-"))
742                                 whitelist_major(path, a->path + 6, 'b', acc);
743                         else if (startswith(a->path, "char-"))
744                                 whitelist_major(path, a->path + 5, 'c', acc);
745                         else
746                                 log_debug("Ignoring device %s while writing cgroup attribute.", a->path);
747                 }
748         }
749
750         if ((mask & CGROUP_MASK_PIDS) && !is_root) {
751
752                 if (c->tasks_max != (uint64_t) -1) {
753                         char buf[DECIMAL_STR_MAX(uint64_t) + 2];
754
755                         sprintf(buf, "%" PRIu64 "\n", c->tasks_max);
756                         r = cg_set_attribute("pids", path, "pids.max", buf);
757                 } else
758                         r = cg_set_attribute("pids", path, "pids.max", "max");
759
760                 if (r < 0)
761                         log_full_errno(IN_SET(r, -ENOENT, -EROFS, -EACCES) ? LOG_DEBUG : LOG_WARNING, r,
762                                        "Failed to set pids.max on %s: %m", path);
763         }
764 }
765
766 CGroupMask cgroup_context_get_mask(CGroupContext *c) {
767         CGroupMask mask = 0;
768
769         /* Figure out which controllers we need */
770
771         if (c->cpu_accounting ||
772             c->cpu_shares != CGROUP_CPU_SHARES_INVALID ||
773             c->startup_cpu_shares != CGROUP_CPU_SHARES_INVALID ||
774             c->cpu_quota_per_sec_usec != USEC_INFINITY)
775                 mask |= CGROUP_MASK_CPUACCT | CGROUP_MASK_CPU;
776
777         if (cgroup_context_has_io_config(c) || cgroup_context_has_blockio_config(c))
778                 mask |= CGROUP_MASK_IO | CGROUP_MASK_BLKIO;
779
780         if (c->memory_accounting ||
781             c->memory_limit != (uint64_t) -1)
782                 mask |= CGROUP_MASK_MEMORY;
783
784         if (c->device_allow ||
785             c->device_policy != CGROUP_AUTO)
786                 mask |= CGROUP_MASK_DEVICES;
787
788         if (c->tasks_accounting ||
789             c->tasks_max != (uint64_t) -1)
790                 mask |= CGROUP_MASK_PIDS;
791
792         return mask;
793 }
794
795 CGroupMask unit_get_own_mask(Unit *u) {
796         CGroupContext *c;
797
798         /* Returns the mask of controllers the unit needs for itself */
799
800         c = unit_get_cgroup_context(u);
801         if (!c)
802                 return 0;
803
804         /* If delegation is turned on, then turn on all cgroups,
805          * unless we are on the legacy hierarchy and the process we
806          * fork into it is known to drop privileges, and hence
807          * shouldn't get access to the controllers.
808          *
809          * Note that on the unified hierarchy it is safe to delegate
810          * controllers to unprivileged services. */
811
812         if (c->delegate) {
813                 ExecContext *e;
814
815                 e = unit_get_exec_context(u);
816                 if (!e ||
817                     exec_context_maintains_privileges(e) ||
818                     cg_unified() > 0)
819                         return _CGROUP_MASK_ALL;
820         }
821
822         return cgroup_context_get_mask(c);
823 }
824
825 CGroupMask unit_get_members_mask(Unit *u) {
826         assert(u);
827
828         /* Returns the mask of controllers all of the unit's children
829          * require, merged */
830
831         if (u->cgroup_members_mask_valid)
832                 return u->cgroup_members_mask;
833
834         u->cgroup_members_mask = 0;
835
836         if (u->type == UNIT_SLICE) {
837                 Unit *member;
838                 Iterator i;
839
840                 SET_FOREACH(member, u->dependencies[UNIT_BEFORE], i) {
841
842                         if (member == u)
843                                 continue;
844
845                         if (UNIT_DEREF(member->slice) != u)
846                                 continue;
847
848                         u->cgroup_members_mask |=
849                                 unit_get_own_mask(member) |
850                                 unit_get_members_mask(member);
851                 }
852         }
853
854         u->cgroup_members_mask_valid = true;
855         return u->cgroup_members_mask;
856 }
857
858 CGroupMask unit_get_siblings_mask(Unit *u) {
859         assert(u);
860
861         /* Returns the mask of controllers all of the unit's siblings
862          * require, i.e. the members mask of the unit's parent slice
863          * if there is one. */
864
865         if (UNIT_ISSET(u->slice))
866                 return unit_get_members_mask(UNIT_DEREF(u->slice));
867
868         return unit_get_own_mask(u) | unit_get_members_mask(u);
869 }
870
871 CGroupMask unit_get_subtree_mask(Unit *u) {
872
873         /* Returns the mask of this subtree, meaning of the group
874          * itself and its children. */
875
876         return unit_get_own_mask(u) | unit_get_members_mask(u);
877 }
878
879 CGroupMask unit_get_target_mask(Unit *u) {
880         CGroupMask mask;
881
882         /* This returns the cgroup mask of all controllers to enable
883          * for a specific cgroup, i.e. everything it needs itself,
884          * plus all that its children need, plus all that its siblings
885          * need. This is primarily useful on the legacy cgroup
886          * hierarchy, where we need to duplicate each cgroup in each
887          * hierarchy that shall be enabled for it. */
888
889         mask = unit_get_own_mask(u) | unit_get_members_mask(u) | unit_get_siblings_mask(u);
890         mask &= u->manager->cgroup_supported;
891
892         return mask;
893 }
894
895 CGroupMask unit_get_enable_mask(Unit *u) {
896         CGroupMask mask;
897
898         /* This returns the cgroup mask of all controllers to enable
899          * for the children of a specific cgroup. This is primarily
900          * useful for the unified cgroup hierarchy, where each cgroup
901          * controls which controllers are enabled for its children. */
902
903         mask = unit_get_members_mask(u);
904         mask &= u->manager->cgroup_supported;
905
906         return mask;
907 }
908
909 /* Recurse from a unit up through its containing slices, propagating
910  * mask bits upward. A unit is also member of itself. */
911 void unit_update_cgroup_members_masks(Unit *u) {
912         CGroupMask m;
913         bool more;
914
915         assert(u);
916
917         /* Calculate subtree mask */
918         m = unit_get_subtree_mask(u);
919
920         /* See if anything changed from the previous invocation. If
921          * not, we're done. */
922         if (u->cgroup_subtree_mask_valid && m == u->cgroup_subtree_mask)
923                 return;
924
925         more =
926                 u->cgroup_subtree_mask_valid &&
927                 ((m & ~u->cgroup_subtree_mask) != 0) &&
928                 ((~m & u->cgroup_subtree_mask) == 0);
929
930         u->cgroup_subtree_mask = m;
931         u->cgroup_subtree_mask_valid = true;
932
933         if (UNIT_ISSET(u->slice)) {
934                 Unit *s = UNIT_DEREF(u->slice);
935
936                 if (more)
937                         /* There's more set now than before. We
938                          * propagate the new mask to the parent's mask
939                          * (not caring if it actually was valid or
940                          * not). */
941
942                         s->cgroup_members_mask |= m;
943
944                 else
945                         /* There's less set now than before (or we
946                          * don't know), we need to recalculate
947                          * everything, so let's invalidate the
948                          * parent's members mask */
949
950                         s->cgroup_members_mask_valid = false;
951
952                 /* And now make sure that this change also hits our
953                  * grandparents */
954                 unit_update_cgroup_members_masks(s);
955         }
956 }
957
958 static const char *migrate_callback(CGroupMask mask, void *userdata) {
959         Unit *u = userdata;
960
961         assert(mask != 0);
962         assert(u);
963
964         while (u) {
965                 if (u->cgroup_path &&
966                     u->cgroup_realized &&
967                     (u->cgroup_realized_mask & mask) == mask)
968                         return u->cgroup_path;
969
970                 u = UNIT_DEREF(u->slice);
971         }
972
973         return NULL;
974 }
975
976 char *unit_default_cgroup_path(Unit *u) {
977         _cleanup_free_ char *escaped = NULL, *slice = NULL;
978         int r;
979
980         assert(u);
981
982         if (unit_has_name(u, SPECIAL_ROOT_SLICE))
983                 return strdup(u->manager->cgroup_root);
984
985         if (UNIT_ISSET(u->slice) && !unit_has_name(UNIT_DEREF(u->slice), SPECIAL_ROOT_SLICE)) {
986                 r = cg_slice_to_path(UNIT_DEREF(u->slice)->id, &slice);
987                 if (r < 0)
988                         return NULL;
989         }
990
991         escaped = cg_escape(u->id);
992         if (!escaped)
993                 return NULL;
994
995         if (slice)
996                 return strjoin(u->manager->cgroup_root, "/", slice, "/", escaped, NULL);
997         else
998                 return strjoin(u->manager->cgroup_root, "/", escaped, NULL);
999 }
1000
1001 int unit_set_cgroup_path(Unit *u, const char *path) {
1002         _cleanup_free_ char *p = NULL;
1003         int r;
1004
1005         assert(u);
1006
1007         if (path) {
1008                 p = strdup(path);
1009                 if (!p)
1010                         return -ENOMEM;
1011         } else
1012                 p = NULL;
1013
1014         if (streq_ptr(u->cgroup_path, p))
1015                 return 0;
1016
1017         if (p) {
1018                 r = hashmap_put(u->manager->cgroup_unit, p, u);
1019                 if (r < 0)
1020                         return r;
1021         }
1022
1023         unit_release_cgroup(u);
1024
1025         u->cgroup_path = p;
1026         p = NULL;
1027
1028         return 1;
1029 }
1030
1031 int unit_watch_cgroup(Unit *u) {
1032         _cleanup_free_ char *events = NULL;
1033         int r;
1034
1035         assert(u);
1036
1037         if (!u->cgroup_path)
1038                 return 0;
1039
1040         if (u->cgroup_inotify_wd >= 0)
1041                 return 0;
1042
1043         /* Only applies to the unified hierarchy */
1044         r = cg_unified();
1045         if (r < 0)
1046                 return log_unit_error_errno(u, r, "Failed detect wether the unified hierarchy is used: %m");
1047         if (r == 0)
1048                 return 0;
1049
1050         /* Don't watch the root slice, it's pointless. */
1051         if (unit_has_name(u, SPECIAL_ROOT_SLICE))
1052                 return 0;
1053
1054         r = hashmap_ensure_allocated(&u->manager->cgroup_inotify_wd_unit, &trivial_hash_ops);
1055         if (r < 0)
1056                 return log_oom();
1057
1058         r = cg_get_path(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path, "cgroup.events", &events);
1059         if (r < 0)
1060                 return log_oom();
1061
1062         u->cgroup_inotify_wd = inotify_add_watch(u->manager->cgroup_inotify_fd, events, IN_MODIFY);
1063         if (u->cgroup_inotify_wd < 0) {
1064
1065                 if (errno == ENOENT) /* If the directory is already
1066                                       * gone we don't need to track
1067                                       * it, so this is not an error */
1068                         return 0;
1069
1070                 return log_unit_error_errno(u, errno, "Failed to add inotify watch descriptor for control group %s: %m", u->cgroup_path);
1071         }
1072
1073         r = hashmap_put(u->manager->cgroup_inotify_wd_unit, INT_TO_PTR(u->cgroup_inotify_wd), u);
1074         if (r < 0)
1075                 return log_unit_error_errno(u, r, "Failed to add inotify watch descriptor to hash map: %m");
1076
1077         return 0;
1078 }
1079
1080 static int unit_create_cgroup(
1081                 Unit *u,
1082                 CGroupMask target_mask,
1083                 CGroupMask enable_mask) {
1084
1085         CGroupContext *c;
1086         int r;
1087
1088         assert(u);
1089
1090         c = unit_get_cgroup_context(u);
1091         if (!c)
1092                 return 0;
1093
1094         if (!u->cgroup_path) {
1095                 _cleanup_free_ char *path = NULL;
1096
1097                 path = unit_default_cgroup_path(u);
1098                 if (!path)
1099                         return log_oom();
1100
1101                 r = unit_set_cgroup_path(u, path);
1102                 if (r == -EEXIST)
1103                         return log_unit_error_errno(u, r, "Control group %s exists already.", path);
1104                 if (r < 0)
1105                         return log_unit_error_errno(u, r, "Failed to set unit's control group path to %s: %m", path);
1106         }
1107
1108         /* First, create our own group */
1109         r = cg_create_everywhere(u->manager->cgroup_supported, target_mask, u->cgroup_path);
1110         if (r < 0)
1111                 return log_unit_error_errno(u, r, "Failed to create cgroup %s: %m", u->cgroup_path);
1112
1113         /* Start watching it */
1114         (void) unit_watch_cgroup(u);
1115
1116         /* Enable all controllers we need */
1117         r = cg_enable_everywhere(u->manager->cgroup_supported, enable_mask, u->cgroup_path);
1118         if (r < 0)
1119                 log_unit_warning_errno(u, r, "Failed to enable controllers on cgroup %s, ignoring: %m", u->cgroup_path);
1120
1121         /* Keep track that this is now realized */
1122         u->cgroup_realized = true;
1123         u->cgroup_realized_mask = target_mask;
1124         u->cgroup_enabled_mask = enable_mask;
1125
1126         if (u->type != UNIT_SLICE && !c->delegate) {
1127
1128                 /* Then, possibly move things over, but not if
1129                  * subgroups may contain processes, which is the case
1130                  * for slice and delegation units. */
1131                 r = cg_migrate_everywhere(u->manager->cgroup_supported, u->cgroup_path, u->cgroup_path, migrate_callback, u);
1132                 if (r < 0)
1133                         log_unit_warning_errno(u, r, "Failed to migrate cgroup from to %s, ignoring: %m", u->cgroup_path);
1134         }
1135
1136         return 0;
1137 }
1138
1139 int unit_attach_pids_to_cgroup(Unit *u) {
1140         int r;
1141         assert(u);
1142
1143         r = unit_realize_cgroup(u);
1144         if (r < 0)
1145                 return r;
1146
1147         r = cg_attach_many_everywhere(u->manager->cgroup_supported, u->cgroup_path, u->pids, migrate_callback, u);
1148         if (r < 0)
1149                 return r;
1150
1151         return 0;
1152 }
1153
1154 static bool unit_has_mask_realized(Unit *u, CGroupMask target_mask, CGroupMask enable_mask) {
1155         assert(u);
1156
1157         return u->cgroup_realized && u->cgroup_realized_mask == target_mask && u->cgroup_enabled_mask == enable_mask;
1158 }
1159
1160 /* Check if necessary controllers and attributes for a unit are in place.
1161  *
1162  * If so, do nothing.
1163  * If not, create paths, move processes over, and set attributes.
1164  *
1165  * Returns 0 on success and < 0 on failure. */
1166 static int unit_realize_cgroup_now(Unit *u, ManagerState state) {
1167         CGroupMask target_mask, enable_mask;
1168         int r;
1169
1170         assert(u);
1171
1172         if (u->in_cgroup_queue) {
1173                 LIST_REMOVE(cgroup_queue, u->manager->cgroup_queue, u);
1174                 u->in_cgroup_queue = false;
1175         }
1176
1177         target_mask = unit_get_target_mask(u);
1178         enable_mask = unit_get_enable_mask(u);
1179
1180         if (unit_has_mask_realized(u, target_mask, enable_mask))
1181                 return 0;
1182
1183         /* First, realize parents */
1184         if (UNIT_ISSET(u->slice)) {
1185                 r = unit_realize_cgroup_now(UNIT_DEREF(u->slice), state);
1186                 if (r < 0)
1187                         return r;
1188         }
1189
1190         /* And then do the real work */
1191         r = unit_create_cgroup(u, target_mask, enable_mask);
1192         if (r < 0)
1193                 return r;
1194
1195         /* Finally, apply the necessary attributes. */
1196         cgroup_context_apply(unit_get_cgroup_context(u), target_mask, u->cgroup_path, state);
1197
1198         return 0;
1199 }
1200
1201 static void unit_add_to_cgroup_queue(Unit *u) {
1202
1203         if (u->in_cgroup_queue)
1204                 return;
1205
1206         LIST_PREPEND(cgroup_queue, u->manager->cgroup_queue, u);
1207         u->in_cgroup_queue = true;
1208 }
1209
1210 unsigned manager_dispatch_cgroup_queue(Manager *m) {
1211         ManagerState state;
1212         unsigned n = 0;
1213         Unit *i;
1214         int r;
1215
1216         state = manager_state(m);
1217
1218         while ((i = m->cgroup_queue)) {
1219                 assert(i->in_cgroup_queue);
1220
1221                 r = unit_realize_cgroup_now(i, state);
1222                 if (r < 0)
1223                         log_warning_errno(r, "Failed to realize cgroups for queued unit %s, ignoring: %m", i->id);
1224
1225                 n++;
1226         }
1227
1228         return n;
1229 }
1230
1231 static void unit_queue_siblings(Unit *u) {
1232         Unit *slice;
1233
1234         /* This adds the siblings of the specified unit and the
1235          * siblings of all parent units to the cgroup queue. (But
1236          * neither the specified unit itself nor the parents.) */
1237
1238         while ((slice = UNIT_DEREF(u->slice))) {
1239                 Iterator i;
1240                 Unit *m;
1241
1242                 SET_FOREACH(m, slice->dependencies[UNIT_BEFORE], i) {
1243                         if (m == u)
1244                                 continue;
1245
1246                         /* Skip units that have a dependency on the slice
1247                          * but aren't actually in it. */
1248                         if (UNIT_DEREF(m->slice) != slice)
1249                                 continue;
1250
1251                         /* No point in doing cgroup application for units
1252                          * without active processes. */
1253                         if (UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(m)))
1254                                 continue;
1255
1256                         /* If the unit doesn't need any new controllers
1257                          * and has current ones realized, it doesn't need
1258                          * any changes. */
1259                         if (unit_has_mask_realized(m, unit_get_target_mask(m), unit_get_enable_mask(m)))
1260                                 continue;
1261
1262                         unit_add_to_cgroup_queue(m);
1263                 }
1264
1265                 u = slice;
1266         }
1267 }
1268
1269 int unit_realize_cgroup(Unit *u) {
1270         assert(u);
1271
1272         if (!UNIT_HAS_CGROUP_CONTEXT(u))
1273                 return 0;
1274
1275         /* So, here's the deal: when realizing the cgroups for this
1276          * unit, we need to first create all parents, but there's more
1277          * actually: for the weight-based controllers we also need to
1278          * make sure that all our siblings (i.e. units that are in the
1279          * same slice as we are) have cgroups, too. Otherwise, things
1280          * would become very uneven as each of their processes would
1281          * get as much resources as all our group together. This call
1282          * will synchronously create the parent cgroups, but will
1283          * defer work on the siblings to the next event loop
1284          * iteration. */
1285
1286         /* Add all sibling slices to the cgroup queue. */
1287         unit_queue_siblings(u);
1288
1289         /* And realize this one now (and apply the values) */
1290         return unit_realize_cgroup_now(u, manager_state(u->manager));
1291 }
1292
1293 void unit_release_cgroup(Unit *u) {
1294         assert(u);
1295
1296         /* Forgets all cgroup details for this cgroup */
1297
1298         if (u->cgroup_path) {
1299                 (void) hashmap_remove(u->manager->cgroup_unit, u->cgroup_path);
1300                 u->cgroup_path = mfree(u->cgroup_path);
1301         }
1302
1303         if (u->cgroup_inotify_wd >= 0) {
1304                 if (inotify_rm_watch(u->manager->cgroup_inotify_fd, u->cgroup_inotify_wd) < 0)
1305                         log_unit_debug_errno(u, errno, "Failed to remove cgroup inotify watch %i for %s, ignoring", u->cgroup_inotify_wd, u->id);
1306
1307                 (void) hashmap_remove(u->manager->cgroup_inotify_wd_unit, INT_TO_PTR(u->cgroup_inotify_wd));
1308                 u->cgroup_inotify_wd = -1;
1309         }
1310 }
1311
1312 void unit_prune_cgroup(Unit *u) {
1313         int r;
1314         bool is_root_slice;
1315
1316         assert(u);
1317
1318         /* Removes the cgroup, if empty and possible, and stops watching it. */
1319
1320         if (!u->cgroup_path)
1321                 return;
1322
1323         is_root_slice = unit_has_name(u, SPECIAL_ROOT_SLICE);
1324
1325         r = cg_trim_everywhere(u->manager->cgroup_supported, u->cgroup_path, !is_root_slice);
1326         if (r < 0) {
1327                 log_debug_errno(r, "Failed to destroy cgroup %s, ignoring: %m", u->cgroup_path);
1328                 return;
1329         }
1330
1331         if (is_root_slice)
1332                 return;
1333
1334         unit_release_cgroup(u);
1335
1336         u->cgroup_realized = false;
1337         u->cgroup_realized_mask = 0;
1338         u->cgroup_enabled_mask = 0;
1339 }
1340
1341 int unit_search_main_pid(Unit *u, pid_t *ret) {
1342         _cleanup_fclose_ FILE *f = NULL;
1343         pid_t pid = 0, npid, mypid;
1344         int r;
1345
1346         assert(u);
1347         assert(ret);
1348
1349         if (!u->cgroup_path)
1350                 return -ENXIO;
1351
1352         r = cg_enumerate_processes(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path, &f);
1353         if (r < 0)
1354                 return r;
1355
1356         mypid = getpid();
1357         while (cg_read_pid(f, &npid) > 0)  {
1358                 pid_t ppid;
1359
1360                 if (npid == pid)
1361                         continue;
1362
1363                 /* Ignore processes that aren't our kids */
1364                 if (get_process_ppid(npid, &ppid) >= 0 && ppid != mypid)
1365                         continue;
1366
1367                 if (pid != 0)
1368                         /* Dang, there's more than one daemonized PID
1369                         in this group, so we don't know what process
1370                         is the main process. */
1371
1372                         return -ENODATA;
1373
1374                 pid = npid;
1375         }
1376
1377         *ret = pid;
1378         return 0;
1379 }
1380
1381 static int unit_watch_pids_in_path(Unit *u, const char *path) {
1382         _cleanup_closedir_ DIR *d = NULL;
1383         _cleanup_fclose_ FILE *f = NULL;
1384         int ret = 0, r;
1385
1386         assert(u);
1387         assert(path);
1388
1389         r = cg_enumerate_processes(SYSTEMD_CGROUP_CONTROLLER, path, &f);
1390         if (r < 0)
1391                 ret = r;
1392         else {
1393                 pid_t pid;
1394
1395                 while ((r = cg_read_pid(f, &pid)) > 0) {
1396                         r = unit_watch_pid(u, pid);
1397                         if (r < 0 && ret >= 0)
1398                                 ret = r;
1399                 }
1400
1401                 if (r < 0 && ret >= 0)
1402                         ret = r;
1403         }
1404
1405         r = cg_enumerate_subgroups(SYSTEMD_CGROUP_CONTROLLER, path, &d);
1406         if (r < 0) {
1407                 if (ret >= 0)
1408                         ret = r;
1409         } else {
1410                 char *fn;
1411
1412                 while ((r = cg_read_subgroup(d, &fn)) > 0) {
1413                         _cleanup_free_ char *p = NULL;
1414
1415                         p = strjoin(path, "/", fn, NULL);
1416                         free(fn);
1417
1418                         if (!p)
1419                                 return -ENOMEM;
1420
1421                         r = unit_watch_pids_in_path(u, p);
1422                         if (r < 0 && ret >= 0)
1423                                 ret = r;
1424                 }
1425
1426                 if (r < 0 && ret >= 0)
1427                         ret = r;
1428         }
1429
1430         return ret;
1431 }
1432
1433 int unit_watch_all_pids(Unit *u) {
1434         assert(u);
1435
1436         /* Adds all PIDs from our cgroup to the set of PIDs we
1437          * watch. This is a fallback logic for cases where we do not
1438          * get reliable cgroup empty notifications: we try to use
1439          * SIGCHLD as replacement. */
1440
1441         if (!u->cgroup_path)
1442                 return -ENOENT;
1443
1444         if (cg_unified() > 0) /* On unified we can use proper notifications */
1445                 return 0;
1446
1447         return unit_watch_pids_in_path(u, u->cgroup_path);
1448 }
1449
1450 int unit_notify_cgroup_empty(Unit *u) {
1451         int r;
1452
1453         assert(u);
1454
1455         if (!u->cgroup_path)
1456                 return 0;
1457
1458         r = cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path);
1459         if (r <= 0)
1460                 return r;
1461
1462         unit_add_to_gc_queue(u);
1463
1464         if (UNIT_VTABLE(u)->notify_cgroup_empty)
1465                 UNIT_VTABLE(u)->notify_cgroup_empty(u);
1466
1467         return 0;
1468 }
1469
1470 static int on_cgroup_inotify_event(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
1471         Manager *m = userdata;
1472
1473         assert(s);
1474         assert(fd >= 0);
1475         assert(m);
1476
1477         for (;;) {
1478                 union inotify_event_buffer buffer;
1479                 struct inotify_event *e;
1480                 ssize_t l;
1481
1482                 l = read(fd, &buffer, sizeof(buffer));
1483                 if (l < 0) {
1484                         if (errno == EINTR || errno == EAGAIN)
1485                                 return 0;
1486
1487                         return log_error_errno(errno, "Failed to read control group inotify events: %m");
1488                 }
1489
1490                 FOREACH_INOTIFY_EVENT(e, buffer, l) {
1491                         Unit *u;
1492
1493                         if (e->wd < 0)
1494                                 /* Queue overflow has no watch descriptor */
1495                                 continue;
1496
1497                         if (e->mask & IN_IGNORED)
1498                                 /* The watch was just removed */
1499                                 continue;
1500
1501                         u = hashmap_get(m->cgroup_inotify_wd_unit, INT_TO_PTR(e->wd));
1502                         if (!u) /* Not that inotify might deliver
1503                                  * events for a watch even after it
1504                                  * was removed, because it was queued
1505                                  * before the removal. Let's ignore
1506                                  * this here safely. */
1507                                 continue;
1508
1509                         (void) unit_notify_cgroup_empty(u);
1510                 }
1511         }
1512 }
1513 #endif // 0
1514
1515 int manager_setup_cgroup(Manager *m) {
1516         _cleanup_free_ char *path = NULL;
1517         CGroupController c;
1518         int r, unified;
1519         char *e;
1520
1521         assert(m);
1522
1523         /* 1. Determine hierarchy */
1524         m->cgroup_root = mfree(m->cgroup_root);
1525         r = cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, 0, &m->cgroup_root);
1526         if (r < 0)
1527                 return log_error_errno(r, "Cannot determine cgroup we are running in: %m");
1528
1529 #if 0 /// elogind does not support systemd scopes and slices
1530         /* Chop off the init scope, if we are already located in it */
1531         e = endswith(m->cgroup_root, "/" SPECIAL_INIT_SCOPE);
1532
1533         /* LEGACY: Also chop off the system slice if we are in
1534          * it. This is to support live upgrades from older systemd
1535          * versions where PID 1 was moved there. Also see
1536          * cg_get_root_path(). */
1537         if (!e && MANAGER_IS_SYSTEM(m)) {
1538                 e = endswith(m->cgroup_root, "/" SPECIAL_SYSTEM_SLICE);
1539                 if (!e)
1540                         e = endswith(m->cgroup_root, "/system"); /* even more legacy */
1541         }
1542         if (e)
1543                 *e = 0;
1544 #endif // 0
1545
1546         /* And make sure to store away the root value without trailing
1547          * slash, even for the root dir, so that we can easily prepend
1548          * it everywhere. */
1549         while ((e = endswith(m->cgroup_root, "/")))
1550                 *e = 0;
1551         log_debug_elogind("Cgroup Controller \"%s\" -> root \"%s\"",
1552                           SYSTEMD_CGROUP_CONTROLLER, m->cgroup_root);
1553
1554         /* 2. Show data */
1555         r = cg_get_path(SYSTEMD_CGROUP_CONTROLLER, m->cgroup_root, NULL, &path);
1556         if (r < 0)
1557                 return log_error_errno(r, "Cannot find cgroup mount point: %m");
1558
1559         unified = cg_unified();
1560         if (unified < 0)
1561                 return log_error_errno(r, "Couldn't determine if we are running in the unified hierarchy: %m");
1562         if (unified > 0)
1563                 log_debug("Unified cgroup hierarchy is located at %s.", path);
1564         else
1565                 log_debug("Using cgroup controller " SYSTEMD_CGROUP_CONTROLLER ". File system hierarchy is at %s.", path);
1566
1567         if (!m->test_run) {
1568                 const char *scope_path;
1569
1570                 /* 3. Install agent */
1571                 if (unified) {
1572
1573                         /* In the unified hierarchy we can can get
1574                          * cgroup empty notifications via inotify. */
1575
1576 #if 0 /// elogind does not support the unified hierarchy, yet.
1577                         m->cgroup_inotify_event_source = sd_event_source_unref(m->cgroup_inotify_event_source);
1578                         safe_close(m->cgroup_inotify_fd);
1579
1580                         m->cgroup_inotify_fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC);
1581                         if (m->cgroup_inotify_fd < 0)
1582                                 return log_error_errno(errno, "Failed to create control group inotify object: %m");
1583
1584                         r = sd_event_add_io(m->event, &m->cgroup_inotify_event_source, m->cgroup_inotify_fd, EPOLLIN, on_cgroup_inotify_event, m);
1585                         if (r < 0)
1586                                 return log_error_errno(r, "Failed to watch control group inotify object: %m");
1587
1588                         /* Process cgroup empty notifications early, but after service notifications and SIGCHLD. Also
1589                          * see handling of cgroup agent notifications, for the classic cgroup hierarchy support. */
1590                         r = sd_event_source_set_priority(m->cgroup_inotify_event_source, SD_EVENT_PRIORITY_NORMAL-5);
1591                         if (r < 0)
1592                                 return log_error_errno(r, "Failed to set priority of inotify event source: %m");
1593
1594                         (void) sd_event_source_set_description(m->cgroup_inotify_event_source, "cgroup-inotify");
1595
1596 #else
1597                         return log_error_errno(EOPNOTSUPP, "Unified cgroup hierarchy not supported: %m");
1598 #endif // 0
1599                 } else if (MANAGER_IS_SYSTEM(m)) {
1600
1601                         /* On the legacy hierarchy we only get
1602                          * notifications via cgroup agents. (Which
1603                          * isn't really reliable, since it does not
1604                          * generate events when control groups with
1605                          * children run empty. */
1606
1607                         r = cg_install_release_agent(SYSTEMD_CGROUP_CONTROLLER, SYSTEMD_CGROUP_AGENT_PATH);
1608                         if (r < 0)
1609                                 log_warning_errno(r, "Failed to install release agent, ignoring: %m");
1610                         else if (r > 0)
1611                                 log_debug("Installed release agent.");
1612                         else if (r == 0)
1613                                 log_debug("Release agent already installed.");
1614                 }
1615
1616 #if 0 /// elogind is not meant to run in systemd init scope
1617                 /* 4. Make sure we are in the special "init.scope" unit in the root slice. */
1618                 scope_path = strjoina(m->cgroup_root, "/" SPECIAL_INIT_SCOPE);
1619                 r = cg_create_and_attach(SYSTEMD_CGROUP_CONTROLLER, scope_path, 0);
1620 #else
1621                 if (streq(SYSTEMD_CGROUP_CONTROLLER, "name=elogind"))
1622                         // we are our own cgroup controller
1623                         scope_path = strjoina("");
1624                 else if (streq(m->cgroup_root, "/elogind"))
1625                         // root already is our cgroup
1626                         scope_path = strjoina(m->cgroup_root);
1627                 else
1628                         // we have to create our own group
1629                         scope_path = strjoina(m->cgroup_root, "/elogind");
1630                 r = cg_create_and_attach(SYSTEMD_CGROUP_CONTROLLER, scope_path, 0);
1631 #endif // 0
1632                 if (r < 0)
1633                         return log_error_errno(r, "Failed to create %s control group: %m", scope_path);
1634                 log_debug_elogind("Created control group \"%s\"", scope_path);
1635
1636                 /* also, move all other userspace processes remaining
1637                  * in the root cgroup into that scope. */
1638                 r = cg_migrate(SYSTEMD_CGROUP_CONTROLLER, m->cgroup_root, SYSTEMD_CGROUP_CONTROLLER, scope_path, false);
1639                 if (r < 0)
1640                         log_warning_errno(r, "Couldn't move remaining userspace processes, ignoring: %m");
1641
1642                 /* 5. And pin it, so that it cannot be unmounted */
1643                 safe_close(m->pin_cgroupfs_fd);
1644                 m->pin_cgroupfs_fd = open(path, O_RDONLY|O_CLOEXEC|O_DIRECTORY|O_NOCTTY|O_NONBLOCK);
1645                 if (m->pin_cgroupfs_fd < 0)
1646                         return log_error_errno(errno, "Failed to open pin file: %m");
1647
1648                 /* 6.  Always enable hierarchical support if it exists... */
1649                 if (!unified)
1650                         (void) cg_set_attribute("memory", "/", "memory.use_hierarchy", "1");
1651         }
1652
1653         /* 7. Figure out which controllers are supported */
1654         r = cg_mask_supported(&m->cgroup_supported);
1655         if (r < 0)
1656                 return log_error_errno(r, "Failed to determine supported controllers: %m");
1657
1658         for (c = 0; c < _CGROUP_CONTROLLER_MAX; c++)
1659                 log_debug("Controller '%s' supported: %s", cgroup_controller_to_string(c), yes_no(m->cgroup_supported & c));
1660
1661         return 0;
1662 }
1663
1664 void manager_shutdown_cgroup(Manager *m, bool delete) {
1665         assert(m);
1666
1667         /* We can't really delete the group, since we are in it. But
1668          * let's trim it. */
1669         if (delete && m->cgroup_root)
1670                 (void) cg_trim(SYSTEMD_CGROUP_CONTROLLER, m->cgroup_root, false);
1671
1672 #if 0 /// elogind does not support the unified hierarchy, yet.
1673         m->cgroup_inotify_wd_unit = hashmap_free(m->cgroup_inotify_wd_unit);
1674
1675         m->cgroup_inotify_event_source = sd_event_source_unref(m->cgroup_inotify_event_source);
1676         m->cgroup_inotify_fd = safe_close(m->cgroup_inotify_fd);
1677 #endif // 0
1678
1679         m->pin_cgroupfs_fd = safe_close(m->pin_cgroupfs_fd);
1680
1681         m->cgroup_root = mfree(m->cgroup_root);
1682 }
1683
1684 #if 0 /// UNNEEDED by elogind
1685 Unit* manager_get_unit_by_cgroup(Manager *m, const char *cgroup) {
1686         char *p;
1687         Unit *u;
1688
1689         assert(m);
1690         assert(cgroup);
1691
1692         u = hashmap_get(m->cgroup_unit, cgroup);
1693         if (u)
1694                 return u;
1695
1696         p = strdupa(cgroup);
1697         for (;;) {
1698                 char *e;
1699
1700                 e = strrchr(p, '/');
1701                 if (!e || e == p)
1702                         return hashmap_get(m->cgroup_unit, SPECIAL_ROOT_SLICE);
1703
1704                 *e = 0;
1705
1706                 u = hashmap_get(m->cgroup_unit, p);
1707                 if (u)
1708                         return u;
1709         }
1710 }
1711
1712 Unit *manager_get_unit_by_pid_cgroup(Manager *m, pid_t pid) {
1713         _cleanup_free_ char *cgroup = NULL;
1714         int r;
1715
1716         assert(m);
1717
1718         if (pid <= 0)
1719                 return NULL;
1720
1721         r = cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, pid, &cgroup);
1722         if (r < 0)
1723                 return NULL;
1724
1725         return manager_get_unit_by_cgroup(m, cgroup);
1726 }
1727
1728 Unit *manager_get_unit_by_pid(Manager *m, pid_t pid) {
1729         Unit *u;
1730
1731         assert(m);
1732
1733         if (pid <= 0)
1734                 return NULL;
1735
1736         if (pid == 1)
1737                 return hashmap_get(m->units, SPECIAL_INIT_SCOPE);
1738
1739         u = hashmap_get(m->watch_pids1, PID_TO_PTR(pid));
1740         if (u)
1741                 return u;
1742
1743         u = hashmap_get(m->watch_pids2, PID_TO_PTR(pid));
1744         if (u)
1745                 return u;
1746
1747         return manager_get_unit_by_pid_cgroup(m, pid);
1748 }
1749
1750 int manager_notify_cgroup_empty(Manager *m, const char *cgroup) {
1751         Unit *u;
1752
1753         assert(m);
1754         assert(cgroup);
1755
1756         log_debug("Got cgroup empty notification for: %s", cgroup);
1757
1758         u = manager_get_unit_by_cgroup(m, cgroup);
1759         if (!u)
1760                 return 0;
1761
1762         return unit_notify_cgroup_empty(u);
1763 }
1764
1765 int unit_get_memory_current(Unit *u, uint64_t *ret) {
1766         _cleanup_free_ char *v = NULL;
1767         int r;
1768
1769         assert(u);
1770         assert(ret);
1771
1772         if (!u->cgroup_path)
1773                 return -ENODATA;
1774
1775         if ((u->cgroup_realized_mask & CGROUP_MASK_MEMORY) == 0)
1776                 return -ENODATA;
1777
1778         if (cg_unified() <= 0)
1779                 r = cg_get_attribute("memory", u->cgroup_path, "memory.usage_in_bytes", &v);
1780         else
1781                 r = cg_get_attribute("memory", u->cgroup_path, "memory.current", &v);
1782         if (r == -ENOENT)
1783                 return -ENODATA;
1784         if (r < 0)
1785                 return r;
1786
1787         return safe_atou64(v, ret);
1788 }
1789
1790 int unit_get_tasks_current(Unit *u, uint64_t *ret) {
1791         _cleanup_free_ char *v = NULL;
1792         int r;
1793
1794         assert(u);
1795         assert(ret);
1796
1797         if (!u->cgroup_path)
1798                 return -ENODATA;
1799
1800         if ((u->cgroup_realized_mask & CGROUP_MASK_PIDS) == 0)
1801                 return -ENODATA;
1802
1803         r = cg_get_attribute("pids", u->cgroup_path, "pids.current", &v);
1804         if (r == -ENOENT)
1805                 return -ENODATA;
1806         if (r < 0)
1807                 return r;
1808
1809         return safe_atou64(v, ret);
1810 }
1811
1812 static int unit_get_cpu_usage_raw(Unit *u, nsec_t *ret) {
1813         _cleanup_free_ char *v = NULL;
1814         uint64_t ns;
1815         int r;
1816
1817         assert(u);
1818         assert(ret);
1819
1820         if (!u->cgroup_path)
1821                 return -ENODATA;
1822
1823         if ((u->cgroup_realized_mask & CGROUP_MASK_CPUACCT) == 0)
1824                 return -ENODATA;
1825
1826         r = cg_get_attribute("cpuacct", u->cgroup_path, "cpuacct.usage", &v);
1827         if (r == -ENOENT)
1828                 return -ENODATA;
1829         if (r < 0)
1830                 return r;
1831
1832         r = safe_atou64(v, &ns);
1833         if (r < 0)
1834                 return r;
1835
1836         *ret = ns;
1837         return 0;
1838 }
1839
1840 int unit_get_cpu_usage(Unit *u, nsec_t *ret) {
1841         nsec_t ns;
1842         int r;
1843
1844         r = unit_get_cpu_usage_raw(u, &ns);
1845         if (r < 0)
1846                 return r;
1847
1848         if (ns > u->cpuacct_usage_base)
1849                 ns -= u->cpuacct_usage_base;
1850         else
1851                 ns = 0;
1852
1853         *ret = ns;
1854         return 0;
1855 }
1856
1857 int unit_reset_cpu_usage(Unit *u) {
1858         nsec_t ns;
1859         int r;
1860
1861         assert(u);
1862
1863         r = unit_get_cpu_usage_raw(u, &ns);
1864         if (r < 0) {
1865                 u->cpuacct_usage_base = 0;
1866                 return r;
1867         }
1868
1869         u->cpuacct_usage_base = ns;
1870         return 0;
1871 }
1872
1873 bool unit_cgroup_delegate(Unit *u) {
1874         CGroupContext *c;
1875
1876         assert(u);
1877
1878         c = unit_get_cgroup_context(u);
1879         if (!c)
1880                 return false;
1881
1882         return c->delegate;
1883 }
1884
1885 void unit_invalidate_cgroup(Unit *u, CGroupMask m) {
1886         assert(u);
1887
1888         if (!UNIT_HAS_CGROUP_CONTEXT(u))
1889                 return;
1890
1891         if (m == 0)
1892                 return;
1893
1894         /* always invalidate compat pairs together */
1895         if (m & (CGROUP_MASK_IO | CGROUP_MASK_BLKIO))
1896                 m |= CGROUP_MASK_IO | CGROUP_MASK_BLKIO;
1897
1898         if ((u->cgroup_realized_mask & m) == 0)
1899                 return;
1900
1901         u->cgroup_realized_mask &= ~m;
1902         unit_add_to_cgroup_queue(u);
1903 }
1904
1905 void manager_invalidate_startup_units(Manager *m) {
1906         Iterator i;
1907         Unit *u;
1908
1909         assert(m);
1910
1911         SET_FOREACH(u, m->startup_units, i)
1912                 unit_invalidate_cgroup(u, CGROUP_MASK_CPU|CGROUP_MASK_IO|CGROUP_MASK_BLKIO);
1913 }
1914
1915 static const char* const cgroup_device_policy_table[_CGROUP_DEVICE_POLICY_MAX] = {
1916         [CGROUP_AUTO] = "auto",
1917         [CGROUP_CLOSED] = "closed",
1918         [CGROUP_STRICT] = "strict",
1919 };
1920
1921 DEFINE_STRING_TABLE_LOOKUP(cgroup_device_policy, CGroupDevicePolicy);
1922 #endif // 0