From 4457c2279e032832bccd6ec0895105e3d6e192cc Mon Sep 17 00:00:00 2001 From: Filipe Brandenburger Date: Sun, 30 Aug 2015 20:22:37 -0700 Subject: core: Use extract_first_word in config_parse_cpu_affinity2 Related to the TODO item to replace FOREACH_WORD_QUOTED with it. Tested by setting `CPUAfinity=0 1' (and other similar settings) in /etc/systemd/system.conf, booting the system with the patched binaries (and also using `systemctl daemon-reload` to reconfigure) and checking that /proc/1/status indicates only CPUs 0 and 1 are allowed for PID 1. No regressions observed in test cases. --- src/core/main.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'src/core') diff --git a/src/core/main.c b/src/core/main.c index e232be88c0..9c2b0c0897 100644 --- a/src/core/main.c +++ b/src/core/main.c @@ -433,8 +433,6 @@ static int config_parse_cpu_affinity2( void *data, void *userdata) { - const char *word, *state; - size_t l; cpu_set_t *c = NULL; unsigned ncpus = 0; @@ -442,16 +440,18 @@ static int config_parse_cpu_affinity2( assert(lvalue); assert(rvalue); - FOREACH_WORD_QUOTED(word, l, rvalue, state) { - char *t; - int r; + for (;;) { + _cleanup_free_ char *word = NULL; unsigned cpu; + int r; - if (!(t = strndup(word, l))) - return log_oom(); + r = extract_first_word(&rvalue, &word, WHITESPACE, EXTRACT_QUOTES); + if (r < 0) + return r; + if (r == 0) + break; - r = safe_atou(t, &cpu); - free(t); + r = safe_atou(word, &cpu); if (!c) if (!(c = cpu_set_malloc(&ncpus))) @@ -466,7 +466,7 @@ static int config_parse_cpu_affinity2( CPU_SET_S(cpu, CPU_ALLOC_SIZE(ncpus), c); } - if (!isempty(state)) + if (!isempty(rvalue)) log_syntax(unit, LOG_ERR, filename, line, EINVAL, "Trailing garbage, ignoring."); -- cgit v1.2.3-54-g00ecf From 4b40bc38b495bb0c986e7accf5897b164ccfee4d Mon Sep 17 00:00:00 2001 From: Filipe Brandenburger Date: Sun, 30 Aug 2015 20:46:27 -0700 Subject: util: Declare a cleanup routine for a cpu_set_t Make use of it in config_parse_cpu_affinity2. Tested by tweaking the `CPUAffinity' setting in /etc/systemd/system.conf and reloading the daemon to confirm it is working as expected. No regressions observed in test cases. --- src/basic/util.h | 3 +++ src/core/main.c | 8 ++------ 2 files changed, 5 insertions(+), 6 deletions(-) (limited to 'src/core') diff --git a/src/basic/util.h b/src/basic/util.h index 1484ef58e5..ff7a00e928 100644 --- a/src/basic/util.h +++ b/src/basic/util.h @@ -363,6 +363,9 @@ int fd_is_temporary_fs(int fd); int pipe_eof(int fd); +DEFINE_TRIVIAL_CLEANUP_FUNC(cpu_set_t*, CPU_FREE); +#define _cleanup_cpu_free_ _cleanup_(CPU_FREEp) + cpu_set_t* cpu_set_malloc(unsigned *ncpus); #define xsprintf(buf, fmt, ...) assert_se((size_t) snprintf(buf, ELEMENTSOF(buf), fmt, __VA_ARGS__) < ELEMENTSOF(buf)) diff --git a/src/core/main.c b/src/core/main.c index 9c2b0c0897..539c57a7b8 100644 --- a/src/core/main.c +++ b/src/core/main.c @@ -433,7 +433,7 @@ static int config_parse_cpu_affinity2( void *data, void *userdata) { - cpu_set_t *c = NULL; + _cleanup_cpu_free_ cpu_set_t *c = NULL; unsigned ncpus = 0; assert(filename); @@ -460,7 +460,6 @@ static int config_parse_cpu_affinity2( if (r < 0 || cpu >= ncpus) { log_syntax(unit, LOG_ERR, filename, line, -r, "Failed to parse CPU affinity '%s'", rvalue); - CPU_FREE(c); return -EBADMSG; } @@ -470,13 +469,10 @@ static int config_parse_cpu_affinity2( log_syntax(unit, LOG_ERR, filename, line, EINVAL, "Trailing garbage, ignoring."); - if (c) { + if (c) if (sched_setaffinity(0, CPU_ALLOC_SIZE(ncpus), c) < 0) log_warning("Failed to set CPU affinity: %m"); - CPU_FREE(c); - } - return 0; } -- cgit v1.2.3-54-g00ecf From 5cc623e644852923def55962d2ee92333af8ead7 Mon Sep 17 00:00:00 2001 From: Filipe Brandenburger Date: Tue, 1 Sep 2015 11:10:09 -0700 Subject: core: Log parse errors in config_parse_cpu_affinity2 --- src/core/main.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'src/core') diff --git a/src/core/main.c b/src/core/main.c index 539c57a7b8..8878ddc85d 100644 --- a/src/core/main.c +++ b/src/core/main.c @@ -433,6 +433,7 @@ 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; @@ -446,8 +447,10 @@ static int config_parse_cpu_affinity2( int r; r = extract_first_word(&rvalue, &word, WHITESPACE, EXTRACT_QUOTES); - if (r < 0) + 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; -- cgit v1.2.3-54-g00ecf