From b9f111b93f9f442f00266f338b14f25ca8685352 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Sun, 11 Jan 2015 00:04:00 -0500 Subject: [PATCH] Support negated fstab options We would ignore options like "fail" and "auto", and for any option which takes a value the first assignment would win. Repeated and options equivalent to the default are rarely used, but they have been documented forever, and people might use them. Especially on the kernel command line it is easier to append a repeated or negated option at the end. --- src/cryptsetup/cryptsetup-generator.c | 4 ++-- src/cryptsetup/cryptsetup.c | 2 +- src/fstab-generator/fstab-generator.c | 21 +++++++++------------ src/shared/fstab-util.h | 14 ++++++++++++++ src/test/test-fstab-util.c | 9 +++++++++ 5 files changed, 35 insertions(+), 15 deletions(-) diff --git a/src/cryptsetup/cryptsetup-generator.c b/src/cryptsetup/cryptsetup-generator.c index 3657890df..27b2360d2 100644 --- a/src/cryptsetup/cryptsetup-generator.c +++ b/src/cryptsetup/cryptsetup-generator.c @@ -67,8 +67,8 @@ static int create_disk( assert(name); assert(device); - noauto = fstab_test_option(options, "noauto\0"); - nofail = fstab_test_option(options, "nofail\0"); + noauto = fstab_test_yes_no_option(options, "noauto\0" "auto\0"); + nofail = fstab_test_yes_no_option(options, "nofail\0" "fail\0"); tmp = fstab_test_option(options, "tmp\0"); swap = fstab_test_option(options, "swap\0"); diff --git a/src/cryptsetup/cryptsetup.c b/src/cryptsetup/cryptsetup.c index 15dea7b65..e6b37acb8 100644 --- a/src/cryptsetup/cryptsetup.c +++ b/src/cryptsetup/cryptsetup.c @@ -69,7 +69,7 @@ static int parse_one_option(const char *option) { assert(option); /* Handled outside of this tool */ - if (streq(option, "noauto") || streq(option, "nofail")) + if (STR_IN_SET(option, "noauto", "auto", "nofail", "fail")) return 0; if (startswith(option, "cipher=")) { diff --git a/src/fstab-generator/fstab-generator.c b/src/fstab-generator/fstab-generator.c index 64eed4952..bc4c1556c 100644 --- a/src/fstab-generator/fstab-generator.c +++ b/src/fstab-generator/fstab-generator.c @@ -135,17 +135,15 @@ static int add_swap( static bool mount_is_network(struct mntent *me) { assert(me); - return - hasmntopt(me, "_netdev") || - fstype_is_network(me->mnt_type); + return fstab_test_option(me->mnt_opts, "_netdev\0") || + fstype_is_network(me->mnt_type); } static bool mount_in_initrd(struct mntent *me) { assert(me); - return - hasmntopt(me, "x-initrd.mount") || - streq(me->mnt_dir, "/usr"); + return fstab_test_option(me->mnt_opts, "x-initrd.mount\0") || + streq(me->mnt_dir, "/usr"); } static int add_mount( @@ -344,8 +342,8 @@ static int parse_fstab(bool initrd) { if (is_path(where)) path_kill_slashes(where); - noauto = !!hasmntopt(me, "noauto"); - nofail = !!hasmntopt(me, "nofail"); + noauto = fstab_test_yes_no_option(me->mnt_opts, "noauto\0" "auto\0"); + nofail = fstab_test_yes_no_option(me->mnt_opts, "nofail\0" "fail\0"); log_debug("Found entry what=%s where=%s type=%s nofail=%s noauto=%s", what, where, me->mnt_type, yes_no(noauto), yes_no(nofail)); @@ -356,10 +354,9 @@ static int parse_fstab(bool initrd) { bool automount; const char *post; - automount = - hasmntopt(me, "comment=systemd.automount") || - hasmntopt(me, "x-systemd.automount"); - + automount = fstab_test_option(me->mnt_opts, + "comment=systemd.automount\0" + "x-systemd.automount\0"); if (initrd) post = SPECIAL_INITRD_FS_TARGET; else if (mount_in_initrd(me)) diff --git a/src/shared/fstab-util.h b/src/shared/fstab-util.h index 39ddb71ef..9f6b32eaf 100644 --- a/src/shared/fstab-util.h +++ b/src/shared/fstab-util.h @@ -23,6 +23,7 @@ #include #include +#include "macro.h" int fstab_filter_options(const char *opts, const char *names, const char **namefound, char **value, char **filtered); @@ -32,3 +33,16 @@ static inline bool fstab_test_option(const char *opts, const char *names) { } int fstab_find_pri(const char *options, int *ret); + +static inline bool fstab_test_yes_no_option(const char *opts, const char *yes_no) { + int r; + const char *opt; + + /* If first name given is last, return 1. + * If second name given is last or neither is found, return 0. */ + + r = fstab_filter_options(opts, yes_no, &opt, NULL, NULL); + assert(r >= 0); + + return opt == yes_no; +} diff --git a/src/test/test-fstab-util.c b/src/test/test-fstab-util.c index ddf965dde..284484d65 100644 --- a/src/test/test-fstab-util.c +++ b/src/test/test-fstab-util.c @@ -107,6 +107,15 @@ static void test_fstab_filter_options(void) { do_fstab_filter_options("", "opt\0", 0, NULL, NULL, ""); } +static void test_fstab_yes_no_option(void) { + assert_se(fstab_test_yes_no_option("nofail,fail,nofail", "nofail\0fail\0") == true); + assert_se(fstab_test_yes_no_option("nofail,nofail,fail", "nofail\0fail\0") == false); + assert_se(fstab_test_yes_no_option("abc,cde,afail", "nofail\0fail\0") == false); + assert_se(fstab_test_yes_no_option("nofail,fail=0,nofail=0", "nofail\0fail\0") == true); + assert_se(fstab_test_yes_no_option("nofail,nofail=0,fail=0", "nofail\0fail\0") == false); +} + int main(void) { test_fstab_filter_options(); + test_fstab_yes_no_option(); } -- 2.30.2