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