From 6d9e0ca400133aeffa4a53c707db43b3e6c98c7b Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 9 Jan 2020 17:01:14 +0100 Subject: [PATCH 1/5] core: expose swap priority value via dbus only if it is set --- src/core/dbus-swap.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/core/dbus-swap.c b/src/core/dbus-swap.c index 353fa201321..57c8c42091f 100644 --- a/src/core/dbus-swap.c +++ b/src/core/dbus-swap.c @@ -12,16 +12,23 @@ #include "unit.h" static int swap_get_priority(Swap *s) { - if (s->from_proc_swaps) + assert(s); + + if (s->from_proc_swaps && s->parameters_proc_swaps.priority_set) return s->parameters_proc_swaps.priority; - if (s->from_fragment) + + if (s->from_fragment && s->parameters_fragment.priority_set) return s->parameters_fragment.priority; + return -1; } static const char *swap_get_options(Swap *s) { + assert(s); + if (s->from_fragment) return s->parameters_fragment.options; + return NULL; } From 6afc31615e63b7db941684be84da82a06373a778 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 9 Jan 2020 17:01:46 +0100 Subject: [PATCH 2/5] core: no need to initialize swap structure fields if all zeroes anyway --- src/core/swap.c | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/core/swap.c b/src/core/swap.c index 03f443daecf..6caf20ea664 100644 --- a/src/core/swap.c +++ b/src/core/swap.c @@ -63,7 +63,6 @@ static void swap_unset_proc_swaps(Swap *s) { return; s->parameters_proc_swaps.what = mfree(s->parameters_proc_swaps.what); - s->from_proc_swaps = false; } @@ -117,9 +116,6 @@ static void swap_init(Unit *u) { s->exec_context.std_output = u->manager->default_std_output; s->exec_context.std_error = u->manager->default_std_error; - s->parameters_proc_swaps.priority = s->parameters_fragment.priority = 0; - s->parameters_fragment.priority_set = false; - s->control_command_id = _SWAP_EXEC_COMMAND_INVALID; u->ignore_on_isolate = true; From eb34a981d67165ec346c69aba53168facc556b64 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 9 Jan 2020 17:02:56 +0100 Subject: [PATCH 3/5] core: initialize priority_set when parsing swap unit files Fixes: #14524 --- src/core/load-fragment-gperf.gperf.m4 | 2 +- src/core/load-fragment.c | 48 +++++++++++++++++++++++++++ src/core/load-fragment.h | 1 + 3 files changed, 50 insertions(+), 1 deletion(-) diff --git a/src/core/load-fragment-gperf.gperf.m4 b/src/core/load-fragment-gperf.gperf.m4 index de08f7d0676..c1f8ac7bb24 100644 --- a/src/core/load-fragment-gperf.gperf.m4 +++ b/src/core/load-fragment-gperf.gperf.m4 @@ -435,7 +435,7 @@ Automount.DirectoryMode, config_parse_mode, 0, Automount.TimeoutIdleSec, config_parse_sec_fix_0, 0, offsetof(Automount, timeout_idle_usec) m4_dnl Swap.What, config_parse_unit_path_printf, 0, offsetof(Swap, parameters_fragment.what) -Swap.Priority, config_parse_int, 0, offsetof(Swap, parameters_fragment.priority) +Swap.Priority, config_parse_swap_priority, 0, 0 Swap.Options, config_parse_unit_string_printf, 0, offsetof(Swap, parameters_fragment.options) Swap.TimeoutSec, config_parse_sec_fix_0, 0, offsetof(Swap, timeout_usec) EXEC_CONTEXT_CONFIG_ITEMS(Swap)m4_dnl diff --git a/src/core/load-fragment.c b/src/core/load-fragment.c index 1679e047dd7..8f9a2f64dbe 100644 --- a/src/core/load-fragment.c +++ b/src/core/load-fragment.c @@ -5002,3 +5002,51 @@ int config_parse_crash_chvt( return 0; } + +int config_parse_swap_priority( + const char *unit, + const char *filename, + unsigned line, + const char *section, + unsigned section_line, + const char *lvalue, + int ltype, + const char *rvalue, + void *data, + void *userdata) { + + Swap *s = userdata; + int r, priority; + + assert(s); + assert(filename); + assert(lvalue); + assert(rvalue); + assert(data); + + if (isempty(rvalue)) { + s->parameters_fragment.priority = -1; + s->parameters_fragment.priority_set = false; + return 0; + } + + r = safe_atoi(rvalue, &priority); + if (r < 0) { + log_syntax(unit, LOG_ERR, filename, line, r, "Invalid swap pririty '%s', ignoring.", rvalue); + return 0; + } + + if (priority < -1) { + log_syntax(unit, LOG_ERR, filename, line, 0, "Sorry, swap priorities smaller than -1 may only be assigned by the kernel itself, ignoring: %s", rvalue); + return 0; + } + + if (priority > 32767) { + log_syntax(unit, LOG_ERR, filename, line, 0, "Swap priority out of range, ignoring: %s", rvalue); + return 0; + } + + s->parameters_fragment.priority = priority; + s->parameters_fragment.priority_set = true; + return 0; +} diff --git a/src/core/load-fragment.h b/src/core/load-fragment.h index b81887d5104..28613ef5b38 100644 --- a/src/core/load-fragment.h +++ b/src/core/load-fragment.h @@ -121,6 +121,7 @@ CONFIG_PARSER_PROTOTYPE(config_parse_status_unit_format); CONFIG_PARSER_PROTOTYPE(config_parse_output_restricted); CONFIG_PARSER_PROTOTYPE(config_parse_crash_chvt); CONFIG_PARSER_PROTOTYPE(config_parse_timeout_abort); +CONFIG_PARSER_PROTOTYPE(config_parse_swap_priority); /* gperf prototypes */ const struct ConfigPerfItem* load_fragment_gperf_lookup(const char *key, GPERF_LEN_TYPE length); From af4454cb17da6727e490522afb7d4bddf8dae7fd Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 9 Jan 2020 17:03:57 +0100 Subject: [PATCH 4/5] core: use unit-based logging instead of generic logging where appropriate --- src/core/swap.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/core/swap.c b/src/core/swap.c index 6caf20ea664..d4f6db6ddce 100644 --- a/src/core/swap.c +++ b/src/core/swap.c @@ -768,9 +768,9 @@ static void swap_enter_activating(Swap *s) { r = fstab_find_pri(s->parameters_fragment.options, &priority); if (r < 0) - log_warning_errno(r, "Failed to parse swap priority \"%s\", ignoring: %m", s->parameters_fragment.options); - else if (r == 1 && s->parameters_fragment.priority_set) - log_warning("Duplicate swap priority configuration by Priority and Options fields."); + log_unit_warning_errno(UNIT(s), r, "Failed to parse swap priority \"%s\", ignoring: %m", s->parameters_fragment.options); + else if (r > 0 && s->parameters_fragment.priority_set) + log_unit_warning(UNIT(s), "Duplicate swap priority configuration by Priority= and Options= fields."); if (r <= 0 && s->parameters_fragment.priority_set) { if (s->parameters_fragment.options) @@ -788,7 +788,7 @@ static void swap_enter_activating(Swap *s) { if (s->parameters_fragment.options || opts) { r = exec_command_append(s->control_command, "-o", - opts ? : s->parameters_fragment.options, NULL); + opts ?: s->parameters_fragment.options, NULL); if (r < 0) goto fail; } @@ -804,7 +804,6 @@ static void swap_enter_activating(Swap *s) { goto fail; swap_set_state(s, SWAP_ACTIVATING); - return; fail: From 6fca66a7f125607864850ac9a4d6cc56a27594dd Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 9 Jan 2020 17:04:25 +0100 Subject: [PATCH 5/5] core: set error value correctly --- src/core/swap.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/core/swap.c b/src/core/swap.c index d4f6db6ddce..225488282e8 100644 --- a/src/core/swap.c +++ b/src/core/swap.c @@ -777,8 +777,10 @@ static void swap_enter_activating(Swap *s) { r = asprintf(&opts, "%s,pri=%i", s->parameters_fragment.options, s->parameters_fragment.priority); else r = asprintf(&opts, "pri=%i", s->parameters_fragment.priority); - if (r < 0) + if (r < 0) { + r = -ENOMEM; goto fail; + } } }