52b84b
From d8024b3de8ce376cdea48ffa59a44b050f215470 Mon Sep 17 00:00:00 2001
52b84b
From: Yu Watanabe <watanabe.yu+github@gmail.com>
52b84b
Date: Mon, 6 Aug 2018 13:42:14 +0900
52b84b
Subject: [PATCH] core: introduce cgroup_add_device_allow()
52b84b
52b84b
(cherry picked from commit fd870bac25c2dd36affaed0251b5a7023f635306)
52b84b
52b84b
Related: #1763435
52b84b
---
52b84b
 src/core/cgroup.c        | 29 +++++++++++++++++++++++++++++
52b84b
 src/core/cgroup.h        |  2 ++
52b84b
 src/core/load-fragment.c | 13 +------------
52b84b
 3 files changed, 32 insertions(+), 12 deletions(-)
52b84b
52b84b
diff --git a/src/core/cgroup.c b/src/core/cgroup.c
52b84b
index 9d09c65453..a17b38f914 100644
52b84b
--- a/src/core/cgroup.c
52b84b
+++ b/src/core/cgroup.c
52b84b
@@ -341,6 +341,35 @@ void cgroup_context_dump(CGroupContext *c, FILE* f, const char *prefix) {
52b84b
         }
52b84b
 }
52b84b
 
52b84b
+int cgroup_add_device_allow(CGroupContext *c, const char *dev, const char *mode) {
52b84b
+        _cleanup_free_ CGroupDeviceAllow *a = NULL;
52b84b
+        _cleanup_free_ char *d = NULL;
52b84b
+
52b84b
+        assert(c);
52b84b
+        assert(dev);
52b84b
+        assert(isempty(mode) || in_charset(mode, "rwm"));
52b84b
+
52b84b
+        a = new(CGroupDeviceAllow, 1);
52b84b
+        if (!a)
52b84b
+                return -ENOMEM;
52b84b
+
52b84b
+        d = strdup(dev);
52b84b
+        if (!d)
52b84b
+                return -ENOMEM;
52b84b
+
52b84b
+        *a = (CGroupDeviceAllow) {
52b84b
+                .path = TAKE_PTR(d),
52b84b
+                .r = isempty(mode) || !!strchr(mode, 'r'),
52b84b
+                .w = isempty(mode) || !!strchr(mode, 'w'),
52b84b
+                .m = isempty(mode) || !!strchr(mode, 'm'),
52b84b
+        };
52b84b
+
52b84b
+        LIST_PREPEND(device_allow, c->device_allow, a);
52b84b
+        TAKE_PTR(a);
52b84b
+
52b84b
+        return 0;
52b84b
+}
52b84b
+
52b84b
 static int lookup_block_device(const char *p, dev_t *ret) {
52b84b
         struct stat st;
52b84b
         int r;
52b84b
diff --git a/src/core/cgroup.h b/src/core/cgroup.h
52b84b
index 5e1be87b20..8102b442b8 100644
52b84b
--- a/src/core/cgroup.h
52b84b
+++ b/src/core/cgroup.h
52b84b
@@ -153,6 +153,8 @@ void cgroup_context_free_io_device_latency(CGroupContext *c, CGroupIODeviceLaten
52b84b
 void cgroup_context_free_blockio_device_weight(CGroupContext *c, CGroupBlockIODeviceWeight *w);
52b84b
 void cgroup_context_free_blockio_device_bandwidth(CGroupContext *c, CGroupBlockIODeviceBandwidth *b);
52b84b
 
52b84b
+int cgroup_add_device_allow(CGroupContext *c, const char *dev, const char *mode);
52b84b
+
52b84b
 CGroupMask unit_get_own_mask(Unit *u);
52b84b
 CGroupMask unit_get_delegate_mask(Unit *u);
52b84b
 CGroupMask unit_get_members_mask(Unit *u);
52b84b
diff --git a/src/core/load-fragment.c b/src/core/load-fragment.c
52b84b
index d43b0f08f9..89a3457acc 100644
52b84b
--- a/src/core/load-fragment.c
52b84b
+++ b/src/core/load-fragment.c
52b84b
@@ -3250,7 +3250,6 @@ int config_parse_device_allow(
52b84b
 
52b84b
         _cleanup_free_ char *path = NULL, *resolved = NULL;
52b84b
         CGroupContext *c = data;
52b84b
-        CGroupDeviceAllow *a;
52b84b
         const char *p = rvalue;
52b84b
         int r;
52b84b
 
52b84b
@@ -3299,17 +3298,7 @@ int config_parse_device_allow(
52b84b
                 return 0;
52b84b
         }
52b84b
 
52b84b
-        a = new0(CGroupDeviceAllow, 1);
52b84b
-        if (!a)
52b84b
-                return log_oom();
52b84b
-
52b84b
-        a->path = TAKE_PTR(resolved);
52b84b
-        a->r = isempty(p) || !!strchr(p, 'r');
52b84b
-        a->w = isempty(p) || !!strchr(p, 'w');
52b84b
-        a->m = isempty(p) || !!strchr(p, 'm');
52b84b
-
52b84b
-        LIST_PREPEND(device_allow, c->device_allow, a);
52b84b
-        return 0;
52b84b
+        return cgroup_add_device_allow(c, resolved, p);
52b84b
 }
52b84b
 
52b84b
 int config_parse_io_device_weight(