4fbe94
From 46b4d26c54a773f7da350e89562039ccc5157a8f Mon Sep 17 00:00:00 2001
4fbe94
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
4fbe94
Date: Sun, 19 May 2019 18:02:38 +0200
4fbe94
Subject: [PATCH] shared/cpu-set-util: move the part to print cpu-set into a
4fbe94
 separate function
4fbe94
4fbe94
Also avoid unnecessary asprintf() when we can write to the output area
4fbe94
directly.
4fbe94
4fbe94
(cherry picked from commit a832893f9c4f0a0329768e90f67e2fa24bb0008e)
4fbe94
4fbe94
Related: #1734787
4fbe94
---
4fbe94
 src/basic/cpu-set-util.c     | 21 +++++++++++++++++++++
4fbe94
 src/basic/cpu-set-util.h     |  1 +
4fbe94
 src/core/dbus-execute.c      | 29 +++++------------------------
4fbe94
 src/test/test-cpu-set-util.c | 29 +++++++++++++++++++++++++++++
4fbe94
 4 files changed, 56 insertions(+), 24 deletions(-)
4fbe94
4fbe94
diff --git a/src/basic/cpu-set-util.c b/src/basic/cpu-set-util.c
4fbe94
index b1c927bcb8..8f24a2601a 100644
4fbe94
--- a/src/basic/cpu-set-util.c
4fbe94
+++ b/src/basic/cpu-set-util.c
4fbe94
@@ -5,6 +5,7 @@
4fbe94
 
4fbe94
 #include <errno.h>
4fbe94
 #include <stddef.h>
4fbe94
+#include <stdio.h>
4fbe94
 #include <syslog.h>
4fbe94
 
4fbe94
 #include "alloc-util.h"
4fbe94
@@ -15,6 +16,26 @@
4fbe94
 #include "parse-util.h"
4fbe94
 #include "string-util.h"
4fbe94
 
4fbe94
+char* cpu_set_to_string(const cpu_set_t *set, size_t setsize) {
4fbe94
+        _cleanup_free_ char *str = NULL;
4fbe94
+        size_t allocated = 0, len = 0;
4fbe94
+        int i, r;
4fbe94
+
4fbe94
+        for (i = 0; (size_t) i < setsize * 8; i++) {
4fbe94
+                if (!CPU_ISSET_S(i, setsize, set))
4fbe94
+                        continue;
4fbe94
+
4fbe94
+                if (!GREEDY_REALLOC(str, allocated, len + 1 + DECIMAL_STR_MAX(int)))
4fbe94
+                        return NULL;
4fbe94
+
4fbe94
+                r = sprintf(str + len, len > 0 ? " %d" : "%d", i);
4fbe94
+                assert_se(r > 0);
4fbe94
+                len += r;
4fbe94
+        }
4fbe94
+
4fbe94
+        return TAKE_PTR(str) ?: strdup("");
4fbe94
+}
4fbe94
+
4fbe94
 cpu_set_t* cpu_set_malloc(unsigned *ncpus) {
4fbe94
         cpu_set_t *c;
4fbe94
         unsigned n = 1024;
4fbe94
diff --git a/src/basic/cpu-set-util.h b/src/basic/cpu-set-util.h
4fbe94
index 88470fe15a..3c546beb55 100644
4fbe94
--- a/src/basic/cpu-set-util.h
4fbe94
+++ b/src/basic/cpu-set-util.h
4fbe94
@@ -26,6 +26,7 @@ static inline cpu_set_t* cpu_set_mfree(cpu_set_t *p) {
4fbe94
 
4fbe94
 cpu_set_t* cpu_set_malloc(unsigned *ncpus);
4fbe94
 
4fbe94
+char* cpu_set_to_string(const cpu_set_t *set, size_t setsize);
4fbe94
 int parse_cpu_set_internal(const char *rvalue, cpu_set_t **cpu_set, bool warn, const char *unit, const char *filename, unsigned line, const char *lvalue);
4fbe94
 
4fbe94
 static inline int parse_cpu_set_and_warn(const char *rvalue, cpu_set_t **cpu_set, const char *unit, const char *filename, unsigned line, const char *lvalue) {
4fbe94
diff --git a/src/core/dbus-execute.c b/src/core/dbus-execute.c
4fbe94
index 5379545d57..d9f4445745 100644
4fbe94
--- a/src/core/dbus-execute.c
4fbe94
+++ b/src/core/dbus-execute.c
4fbe94
@@ -1565,32 +1565,13 @@ int bus_exec_context_set_transient_property(
4fbe94
                                 unit_write_settingf(u, flags, name, "%s=", name);
4fbe94
                         } else {
4fbe94
                                 _cleanup_free_ char *str = NULL;
4fbe94
-                                size_t allocated = 0, len = 0, i, ncpus;
4fbe94
+                                size_t ncpus;
4fbe94
 
4fbe94
-                                ncpus = CPU_SIZE_TO_NUM(n);
4fbe94
-
4fbe94
-                                for (i = 0; i < ncpus; i++) {
4fbe94
-                                        _cleanup_free_ char *p = NULL;
4fbe94
-                                        size_t add;
4fbe94
-
4fbe94
-                                        if (!CPU_ISSET_S(i, n, (cpu_set_t*) a))
4fbe94
-                                                continue;
4fbe94
-
4fbe94
-                                        r = asprintf(&p, "%zu", i);
4fbe94
-                                        if (r < 0)
4fbe94
-                                                return -ENOMEM;
4fbe94
-
4fbe94
-                                        add = strlen(p);
4fbe94
-
4fbe94
-                                        if (!GREEDY_REALLOC(str, allocated, len + add + 2))
4fbe94
-                                                return -ENOMEM;
4fbe94
-
4fbe94
-                                        strcpy(mempcpy(str + len, p, add), " ");
4fbe94
-                                        len += add + 1;
4fbe94
-                                }
4fbe94
+                                str = cpu_set_to_string(a, n);
4fbe94
+                                if (!str)
4fbe94
+                                        return -ENOMEM;
4fbe94
 
4fbe94
-                                if (len != 0)
4fbe94
-                                        str[len - 1] = '\0';
4fbe94
+                                ncpus = CPU_SIZE_TO_NUM(n);
4fbe94
 
4fbe94
                                 if (!c->cpuset || c->cpuset_ncpus < ncpus) {
4fbe94
                                         cpu_set_t *cpuset;
4fbe94
diff --git a/src/test/test-cpu-set-util.c b/src/test/test-cpu-set-util.c
4fbe94
index c9272459b4..ff5edb2a69 100644
4fbe94
--- a/src/test/test-cpu-set-util.c
4fbe94
+++ b/src/test/test-cpu-set-util.c
4fbe94
@@ -6,6 +6,7 @@
4fbe94
 
4fbe94
 static void test_parse_cpu_set(void) {
4fbe94
         cpu_set_t *c = NULL;
4fbe94
+        _cleanup_free_ char *str = NULL;
4fbe94
         int ncpus;
4fbe94
         int cpu;
4fbe94
 
4fbe94
@@ -15,6 +16,10 @@ static void test_parse_cpu_set(void) {
4fbe94
         assert_se(CPU_ISSET_S(1, CPU_ALLOC_SIZE(ncpus), c));
4fbe94
         assert_se(CPU_ISSET_S(2, CPU_ALLOC_SIZE(ncpus), c));
4fbe94
         assert_se(CPU_COUNT_S(CPU_ALLOC_SIZE(ncpus), c) == 2);
4fbe94
+
4fbe94
+        assert_se(str = cpu_set_to_string(c, CPU_ALLOC_SIZE(ncpus)));
4fbe94
+        log_info("cpu_set_to_string: %s", str);
4fbe94
+        str = mfree(str);
4fbe94
         c = cpu_set_mfree(c);
4fbe94
 
4fbe94
         /* A more interesting range */
4fbe94
@@ -25,6 +30,9 @@ static void test_parse_cpu_set(void) {
4fbe94
                 assert_se(CPU_ISSET_S(cpu, CPU_ALLOC_SIZE(ncpus), c));
4fbe94
         for (cpu = 8; cpu < 12; cpu++)
4fbe94
                 assert_se(CPU_ISSET_S(cpu, CPU_ALLOC_SIZE(ncpus), c));
4fbe94
+        assert_se(str = cpu_set_to_string(c, CPU_ALLOC_SIZE(ncpus)));
4fbe94
+        log_info("cpu_set_to_string: %s", str);
4fbe94
+        str = mfree(str);
4fbe94
         c = cpu_set_mfree(c);
4fbe94
 
4fbe94
         /* Quoted strings */
4fbe94
@@ -33,6 +41,9 @@ static void test_parse_cpu_set(void) {
4fbe94
         assert_se(CPU_COUNT_S(CPU_ALLOC_SIZE(ncpus), c) == 4);
4fbe94
         for (cpu = 8; cpu < 12; cpu++)
4fbe94
                 assert_se(CPU_ISSET_S(cpu, CPU_ALLOC_SIZE(ncpus), c));
4fbe94
+        assert_se(str = cpu_set_to_string(c, CPU_ALLOC_SIZE(ncpus)));
4fbe94
+        log_info("cpu_set_to_string: %s", str);
4fbe94
+        str = mfree(str);
4fbe94
         c = cpu_set_mfree(c);
4fbe94
 
4fbe94
         /* Use commas as separators */
4fbe94
@@ -43,6 +54,9 @@ static void test_parse_cpu_set(void) {
4fbe94
                 assert_se(CPU_ISSET_S(cpu, CPU_ALLOC_SIZE(ncpus), c));
4fbe94
         for (cpu = 8; cpu < 12; cpu++)
4fbe94
                 assert_se(CPU_ISSET_S(cpu, CPU_ALLOC_SIZE(ncpus), c));
4fbe94
+        assert_se(str = cpu_set_to_string(c, CPU_ALLOC_SIZE(ncpus)));
4fbe94
+        log_info("cpu_set_to_string: %s", str);
4fbe94
+        str = mfree(str);
4fbe94
         c = cpu_set_mfree(c);
4fbe94
 
4fbe94
         /* Commas with spaces (and trailing comma, space) */
4fbe94
@@ -51,6 +65,9 @@ static void test_parse_cpu_set(void) {
4fbe94
         assert_se(CPU_COUNT_S(CPU_ALLOC_SIZE(ncpus), c) == 8);
4fbe94
         for (cpu = 0; cpu < 8; cpu++)
4fbe94
                 assert_se(CPU_ISSET_S(cpu, CPU_ALLOC_SIZE(ncpus), c));
4fbe94
+        assert_se(str = cpu_set_to_string(c, CPU_ALLOC_SIZE(ncpus)));
4fbe94
+        log_info("cpu_set_to_string: %s", str);
4fbe94
+        str = mfree(str);
4fbe94
         c = cpu_set_mfree(c);
4fbe94
 
4fbe94
         /* Ranges */
4fbe94
@@ -61,6 +78,9 @@ static void test_parse_cpu_set(void) {
4fbe94
                 assert_se(CPU_ISSET_S(cpu, CPU_ALLOC_SIZE(ncpus), c));
4fbe94
         for (cpu = 8; cpu < 12; cpu++)
4fbe94
                 assert_se(CPU_ISSET_S(cpu, CPU_ALLOC_SIZE(ncpus), c));
4fbe94
+        assert_se(str = cpu_set_to_string(c, CPU_ALLOC_SIZE(ncpus)));
4fbe94
+        log_info("cpu_set_to_string: %s", str);
4fbe94
+        str = mfree(str);
4fbe94
         c = cpu_set_mfree(c);
4fbe94
 
4fbe94
         /* Ranges with trailing comma, space */
4fbe94
@@ -71,6 +91,9 @@ static void test_parse_cpu_set(void) {
4fbe94
                 assert_se(CPU_ISSET_S(cpu, CPU_ALLOC_SIZE(ncpus), c));
4fbe94
         for (cpu = 8; cpu < 12; cpu++)
4fbe94
                 assert_se(CPU_ISSET_S(cpu, CPU_ALLOC_SIZE(ncpus), c));
4fbe94
+        assert_se(str = cpu_set_to_string(c, CPU_ALLOC_SIZE(ncpus)));
4fbe94
+        log_info("cpu_set_to_string: %s", str);
4fbe94
+        str = mfree(str);
4fbe94
         c = cpu_set_mfree(c);
4fbe94
 
4fbe94
         /* Negative range (returns empty cpu_set) */
4fbe94
@@ -85,6 +108,9 @@ static void test_parse_cpu_set(void) {
4fbe94
         assert_se(CPU_COUNT_S(CPU_ALLOC_SIZE(ncpus), c) == 12);
4fbe94
         for (cpu = 0; cpu < 12; cpu++)
4fbe94
                 assert_se(CPU_ISSET_S(cpu, CPU_ALLOC_SIZE(ncpus), c));
4fbe94
+        assert_se(str = cpu_set_to_string(c, CPU_ALLOC_SIZE(ncpus)));
4fbe94
+        log_info("cpu_set_to_string: %s", str);
4fbe94
+        str = mfree(str);
4fbe94
         c = cpu_set_mfree(c);
4fbe94
 
4fbe94
         /* Mix ranges and individual CPUs */
4fbe94
@@ -95,6 +121,9 @@ static void test_parse_cpu_set(void) {
4fbe94
         assert_se(CPU_ISSET_S(1, CPU_ALLOC_SIZE(ncpus), c));
4fbe94
         for (cpu = 4; cpu < 12; cpu++)
4fbe94
                 assert_se(CPU_ISSET_S(cpu, CPU_ALLOC_SIZE(ncpus), c));
4fbe94
+        assert_se(str = cpu_set_to_string(c, CPU_ALLOC_SIZE(ncpus)));
4fbe94
+        log_info("cpu_set_to_string: %s", str);
4fbe94
+        str = mfree(str);
4fbe94
         c = cpu_set_mfree(c);
4fbe94
 
4fbe94
         /* Garbage */