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