ryantimwilson / rpms / systemd

Forked from rpms/systemd 2 months ago
Clone
b9a53a
From daa0243fda679c8af723648b8b1e501fc55b0ada Mon Sep 17 00:00:00 2001
b9a53a
From: Michal Sekletar <msekleta@redhat.com>
b9a53a
Date: Wed, 22 May 2019 13:55:49 +0200
b9a53a
Subject: [PATCH] shared/cpu-set-util: make transfer of cpu_set_t over bus
b9a53a
 endian safe
b9a53a
b9a53a
(cherry picked from commit c367f996f5f091a63f812f0140b304c649be77fc)
b9a53a
b9a53a
Related: #1734787
b9a53a
---
b9a53a
 src/basic/cpu-set-util.c   | 38 ++++++++++++++++++++++++++++++++++++++
b9a53a
 src/basic/cpu-set-util.h   |  3 +++
b9a53a
 src/core/dbus-execute.c    |  6 +++++-
b9a53a
 src/shared/bus-unit-util.c |  8 +++++++-
b9a53a
 4 files changed, 53 insertions(+), 2 deletions(-)
b9a53a
b9a53a
diff --git a/src/basic/cpu-set-util.c b/src/basic/cpu-set-util.c
b9a53a
index c297eab032..74e35e57dd 100644
b9a53a
--- a/src/basic/cpu-set-util.c
b9a53a
+++ b/src/basic/cpu-set-util.c
b9a53a
@@ -209,3 +209,41 @@ int cpus_in_affinity_mask(void) {
b9a53a
                 n *= 2;
b9a53a
         }
b9a53a
 }
b9a53a
+
b9a53a
+int cpu_set_to_dbus(const CPUSet *set, uint8_t **ret, size_t *allocated) {
b9a53a
+        uint8_t *out;
b9a53a
+
b9a53a
+        assert(set);
b9a53a
+        assert(ret);
b9a53a
+
b9a53a
+        out = new0(uint8_t, set->allocated);
b9a53a
+        if (!out)
b9a53a
+                return -ENOMEM;
b9a53a
+
b9a53a
+        for (unsigned cpu = 0; cpu < set->allocated * 8; cpu++)
b9a53a
+                if (CPU_ISSET_S(cpu, set->allocated, set->set))
b9a53a
+                        out[cpu / 8] |= 1u << (cpu % 8);
b9a53a
+
b9a53a
+        *ret = out;
b9a53a
+        *allocated = set->allocated;
b9a53a
+        return 0;
b9a53a
+}
b9a53a
+
b9a53a
+int cpu_set_from_dbus(const uint8_t *bits, size_t size, CPUSet *set) {
b9a53a
+        _cleanup_(cpu_set_reset) CPUSet s = {};
b9a53a
+        int r;
b9a53a
+
b9a53a
+        assert(bits);
b9a53a
+        assert(set);
b9a53a
+
b9a53a
+        for (unsigned cpu = size * 8; cpu > 0; cpu--)
b9a53a
+                if (bits[(cpu - 1) / 8] & (1u << ((cpu - 1) % 8))) {
b9a53a
+                        r = cpu_set_add(&s, cpu - 1);
b9a53a
+                        if (r < 0)
b9a53a
+                                return r;
b9a53a
+                }
b9a53a
+
b9a53a
+        *set = s;
b9a53a
+        s = (CPUSet) {};
b9a53a
+        return 0;
b9a53a
+}
b9a53a
diff --git a/src/basic/cpu-set-util.h b/src/basic/cpu-set-util.h
b9a53a
index 68a73bf9f7..415c6ca295 100644
b9a53a
--- a/src/basic/cpu-set-util.h
b9a53a
+++ b/src/basic/cpu-set-util.h
b9a53a
@@ -46,4 +46,7 @@ static inline int parse_cpu_set(const char *rvalue, CPUSet *cpu_set){
b9a53a
         return parse_cpu_set_full(rvalue, cpu_set, false, NULL, NULL, 0, NULL);
b9a53a
 }
b9a53a
 
b9a53a
+int cpu_set_to_dbus(const CPUSet *set, uint8_t **ret, size_t *allocated);
b9a53a
+int cpu_set_from_dbus(const uint8_t *bits, size_t size, CPUSet *set);
b9a53a
+
b9a53a
 int cpus_in_affinity_mask(void);
b9a53a
diff --git a/src/core/dbus-execute.c b/src/core/dbus-execute.c
b9a53a
index 08946627e3..50ea71a281 100644
b9a53a
--- a/src/core/dbus-execute.c
b9a53a
+++ b/src/core/dbus-execute.c
b9a53a
@@ -1553,18 +1553,22 @@ int bus_exec_context_set_transient_property(
b9a53a
         if (streq(name, "CPUAffinity")) {
b9a53a
                 const void *a;
b9a53a
                 size_t n;
b9a53a
+                _cleanup_(cpu_set_reset) CPUSet set = {};
b9a53a
 
b9a53a
                 r = sd_bus_message_read_array(message, 'y', &a, &n);
b9a53a
                 if (r < 0)
b9a53a
                         return r;
b9a53a
 
b9a53a
+                r = cpu_set_from_dbus(a, n, &set);
b9a53a
+                if (r < 0)
b9a53a
+                        return r;
b9a53a
+
b9a53a
                 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
b9a53a
                         if (n == 0) {
b9a53a
                                 cpu_set_reset(&c->cpu_set);
b9a53a
                                 unit_write_settingf(u, flags, name, "%s=", name);
b9a53a
                         } else {
b9a53a
                                 _cleanup_free_ char *str = NULL;
b9a53a
-                                const CPUSet set = {(cpu_set_t*) a, n};
b9a53a
 
b9a53a
                                 str = cpu_set_to_string(&set);
b9a53a
                                 if (!str)
b9a53a
diff --git a/src/shared/bus-unit-util.c b/src/shared/bus-unit-util.c
b9a53a
index 75b4aace84..ec8732c226 100644
b9a53a
--- a/src/shared/bus-unit-util.c
b9a53a
+++ b/src/shared/bus-unit-util.c
b9a53a
@@ -933,12 +933,18 @@ static int bus_append_execute_property(sd_bus_message *m, const char *field, con
b9a53a
 
b9a53a
         if (streq(field, "CPUAffinity")) {
b9a53a
                 _cleanup_(cpu_set_reset) CPUSet cpuset = {};
b9a53a
+                _cleanup_free_ uint8_t *array = NULL;
b9a53a
+                size_t allocated;
b9a53a
 
b9a53a
                 r = parse_cpu_set(eq, &cpuset);
b9a53a
                 if (r < 0)
b9a53a
                         return log_error_errno(r, "Failed to parse %s value: %s", field, eq);
b9a53a
 
b9a53a
-                return bus_append_byte_array(m, field, cpuset.set, cpuset.allocated);
b9a53a
+                r = cpu_set_to_dbus(&cpuset, &array, &allocated);
b9a53a
+                if (r < 0)
b9a53a
+                        return log_error_errno(r, "Failed to serialize CPUAffinity: %m");
b9a53a
+
b9a53a
+                return bus_append_byte_array(m, field, array, allocated);
b9a53a
         }
b9a53a
 
b9a53a
         if (STR_IN_SET(field, "RestrictAddressFamilies", "SystemCallFilter")) {