]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
util: refactor cpu_set parsing into its own function
authorFilipe Brandenburger <filbranden@google.com>
Fri, 25 Sep 2015 00:58:49 +0000 (17:58 -0700)
committerFilipe Brandenburger <filbranden@google.com>
Fri, 25 Sep 2015 01:01:36 +0000 (18:01 -0700)
Use the new code in config_parse_cpu_affinity2.

Tested by modifying CPUAffinity=... setting in /etc/systemd/system.conf
and reloading the daemon, then checking ^Cpus_allowed in /proc/1/status
to confirm the correct CPU mask is in place.

src/basic/util.c
src/basic/util.h
src/core/main.c

index 40a4b8fbec3158b3a3a3ad3ee4e4ca4469cb2ae8..bc61ec011589c9d937924664ecc9df92a89067ee 100644 (file)
@@ -2578,6 +2578,62 @@ cpu_set_t* cpu_set_malloc(unsigned *ncpus) {
         }
 }
 
+int parse_cpu_set(
+                const char *rvalue,
+                cpu_set_t **cpu_set,
+                const char *unit,
+                const char *filename,
+                unsigned line,
+                const char *lvalue) {
+
+        const char *whole_rvalue = rvalue;
+        _cleanup_cpu_free_ cpu_set_t *c = NULL;
+        unsigned ncpus = 0;
+
+        assert(filename);
+        assert(lvalue);
+        assert(rvalue);
+
+        for (;;) {
+                _cleanup_free_ char *word = NULL;
+                unsigned cpu;
+                int r;
+
+                r = extract_first_word(&rvalue, &word, WHITESPACE, EXTRACT_QUOTES);
+                if (r < 0) {
+                        log_syntax(unit, LOG_ERR, filename, line, r,
+                                   "Invalid value for %s: %s", lvalue, whole_rvalue);
+                        return r;
+                }
+                if (r == 0)
+                        break;
+
+                r = safe_atou(word, &cpu);
+
+                if (!c)
+                        if (!(c = cpu_set_malloc(&ncpus)))
+                                return log_oom();
+
+                if (r < 0 || cpu >= ncpus) {
+                        log_syntax(unit, LOG_ERR, filename, line, -r,
+                                   "Failed to parse CPU affinity '%s'", rvalue);
+                        return -EBADMSG;
+                }
+
+                CPU_SET_S(cpu, CPU_ALLOC_SIZE(ncpus), c);
+        }
+        if (!isempty(rvalue))
+                log_syntax(unit, LOG_ERR, filename, line, EINVAL,
+                           "Trailing garbage, ignoring.");
+
+        /* On success, sets *cpu_set and returns ncpus for the system. */
+        if (c) {
+                *cpu_set = c;
+                c = NULL;
+        }
+        return (int) ncpus;
+}
+
 int files_same(const char *filea, const char *fileb) {
         struct stat a, b;
 
index d53e15e6e6ae68fd06ab1d98921a972f965fa8d9..56d9f037bf651be89344d6da4de3b162bc8b8f7b 100644 (file)
@@ -375,6 +375,7 @@ DEFINE_TRIVIAL_CLEANUP_FUNC(cpu_set_t*, CPU_FREE);
 #define _cleanup_cpu_free_ _cleanup_(CPU_FREEp)
 
 cpu_set_t* cpu_set_malloc(unsigned *ncpus);
+int parse_cpu_set(const char *rvalue, cpu_set_t **cpu_set, const char *unit, const char *filename, unsigned line, const char *lvalue);
 
 #define xsprintf(buf, fmt, ...) \
         assert_message_se((size_t) snprintf(buf, ELEMENTSOF(buf), fmt, __VA_ARGS__) < ELEMENTSOF(buf), \
index b57f4c1b7a0360b31fb921d006feec055388702c..bc72a2b00b012e6bf6edc9de25a0078179f67b43 100644 (file)
@@ -434,48 +434,16 @@ static int config_parse_cpu_affinity2(
                 void *data,
                 void *userdata) {
 
-        const char *whole_rvalue = rvalue;
         _cleanup_cpu_free_ cpu_set_t *c = NULL;
-        unsigned ncpus = 0;
+        int ncpus;
 
-        assert(filename);
-        assert(lvalue);
-        assert(rvalue);
+        ncpus = parse_cpu_set(rvalue, &c, unit, filename, line, lvalue);
 
-        for (;;) {
-                _cleanup_free_ char *word = NULL;
-                unsigned cpu;
-                int r;
-
-                r = extract_first_word(&rvalue, &word, WHITESPACE, EXTRACT_QUOTES);
-                if (r < 0) {
-                        log_syntax(unit, LOG_ERR, filename, line, r, "Invalid value for %s: %s", lvalue, whole_rvalue);
-                        return r;
-                }
-                if (r == 0)
-                        break;
-
-                r = safe_atou(word, &cpu);
-
-                if (!c)
-                        if (!(c = cpu_set_malloc(&ncpus)))
-                                return log_oom();
-
-                if (r < 0 || cpu >= ncpus) {
-                        log_syntax(unit, LOG_ERR, filename, line, -r,
-                                   "Failed to parse CPU affinity '%s'", rvalue);
-                        return -EBADMSG;
-                }
-
-                CPU_SET_S(cpu, CPU_ALLOC_SIZE(ncpus), c);
-        }
-        if (!isempty(rvalue))
-                log_syntax(unit, LOG_ERR, filename, line, EINVAL,
-                           "Trailing garbage, ignoring.");
+        if (ncpus < 0)
+                return ncpus;
 
-        if (c)
-                if (sched_setaffinity(0, CPU_ALLOC_SIZE(ncpus), c) < 0)
-                        log_warning("Failed to set CPU affinity: %m");
+        if (sched_setaffinity(0, CPU_ALLOC_SIZE(ncpus), c) < 0)
+                log_warning("Failed to set CPU affinity: %m");
 
         return 0;
 }