summaryrefslogtreecommitdiff
path: root/src/core/swap.c
diff options
context:
space:
mode:
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2015-01-09 16:58:29 -0500
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2015-01-11 23:41:41 -0500
commitd15d0333be6a1ca7fdd99a1881d967b6be8f387a (patch)
tree488ce214314613658c5eda3b0a94deec31353de7 /src/core/swap.c
parent1e39ff926f815d241721ed9486945cf599ef8c2f (diff)
Add new function to filter fstab options
This fixes parsing of options in shared/generator.c. Existing code had some issues: - it would treate whitespace and semicolons as seperators. fstab(5) is pretty clear that only commas matter. And the syntax does not allow for spaces to be inserted in the field in fstab. Whitespace might be escaped, but then it should not seperate options. Treat whitespace and semicolons as any other character. - it assumed that x-systemd.device-timeout would always be followed by "=". But this is not guaranteed, hasmntopt will return this option even if there's no value. Uninitialized memory could be read. - some error paths would log, and inconsistently, some would just return an error code. Filtering is split out to a separate function and tests are added. Similar code paths in other places are adjusted to use the new function.
Diffstat (limited to 'src/core/swap.c')
-rw-r--r--src/core/swap.c71
1 files changed, 4 insertions, 67 deletions
diff --git a/src/core/swap.c b/src/core/swap.c
index cb19d0f123..b2ebed4b72 100644
--- a/src/core/swap.c
+++ b/src/core/swap.c
@@ -41,6 +41,7 @@
#include "path-util.h"
#include "virt.h"
#include "udev-util.h"
+#include "fstab-util.h"
static const UnitActiveState state_translation_table[_SWAP_STATE_MAX] = {
[SWAP_DEAD] = UNIT_INACTIVE,
@@ -704,71 +705,6 @@ fail:
swap_enter_dead(s, SWAP_FAILURE_RESOURCES);
}
-static int mount_find_pri(const char *options, int *ret) {
- const char *opt;
- char *end;
- unsigned long r;
-
- assert(ret);
-
- if (!options)
- return 0;
-
- opt = mount_test_option(options, "pri");
- if (!opt)
- return 0;
-
- opt += strlen("pri");
- if (*opt != '=')
- return -EINVAL;
-
- errno = 0;
- r = strtoul(opt + 1, &end, 10);
- if (errno > 0)
- return -errno;
-
- if (end == opt + 1 || (*end != ',' && *end != 0))
- return -EINVAL;
-
- *ret = (int) r;
- return 1;
-}
-
-static int mount_find_discard(const char *options, char **ret) {
- const char *opt;
- char *ans;
- size_t len;
-
- assert(ret);
-
- if (!options)
- return 0;
-
- opt = mount_test_option(options, "discard");
- if (!opt)
- return 0;
-
- opt += strlen("discard");
- if (*opt == ',' || *opt == '\0')
- ans = strdup("all");
- else {
- if (*opt != '=')
- return -EINVAL;
-
- len = strcspn(opt + 1, ",");
- if (len == 0)
- return -EINVAL;
-
- ans = strndup(opt + 1, len);
- }
-
- if (!ans)
- return -ENOMEM;
-
- *ret = ans;
- return 1;
-}
-
static void swap_enter_activating(Swap *s) {
_cleanup_free_ char *discard = NULL;
int r, priority = -1;
@@ -779,11 +715,12 @@ static void swap_enter_activating(Swap *s) {
s->control_command = s->exec_command + SWAP_EXEC_ACTIVATE;
if (s->from_fragment) {
- mount_find_discard(s->parameters_fragment.options, &discard);
+ fstab_filter_options(s->parameters_fragment.options, "discard\0",
+ NULL, &discard, NULL);
priority = s->parameters_fragment.priority;
if (priority < 0)
- mount_find_pri(s->parameters_fragment.options, &priority);
+ fstab_find_pri(s->parameters_fragment.options, &priority);
}
r = exec_command_set(s->control_command, "/sbin/swapon", NULL);