From d5cc4be28f9d678dab441e7cf332fedf47917ea5 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Tue, 7 Feb 2017 13:08:56 +0100 Subject: [PATCH 1/2] fstab-generator: Options= applies specifier expansion Let's document that this is the case, and properly escape % when we generate Options= in the generator. Fixes: #5086 --- man/systemd.mount.xml | 6 +++--- man/systemd.swap.xml | 9 ++++----- src/fstab-generator/fstab-generator.c | 27 +++++++++++++++++++++++---- 3 files changed, 30 insertions(+), 12 deletions(-) diff --git a/man/systemd.mount.xml b/man/systemd.mount.xml index 2117433bf0..8d9517a913 100644 --- a/man/systemd.mount.xml +++ b/man/systemd.mount.xml @@ -366,9 +366,9 @@ Options= - Mount options to use when mounting. This takes - a comma-separated list of options. This setting is - optional. + Mount options to use when mounting. This takes a comma-separated list of options. This setting + is optional. Note that the usual specifier expansion is applied to this setting, literal percent characters + should hence be written as %%. diff --git a/man/systemd.swap.xml b/man/systemd.swap.xml index cf4e1ba839..7f82d69725 100644 --- a/man/systemd.swap.xml +++ b/man/systemd.swap.xml @@ -195,12 +195,11 @@ Options= - May contain an option string for the swap - device. This may be used for controlling discard options among - other functionality, if the swap backing device supports the - discard or trim operation. (See + May contain an option string for the swap device. This may be used for controlling discard + options among other functionality, if the swap backing device supports the discard or trim operation. (See swapon8 - for more information.) + for more information.) Note that the usual specifier expansion is applied to this setting, literal percent + characters should hence be written as %%. diff --git a/src/fstab-generator/fstab-generator.c b/src/fstab-generator/fstab-generator.c index 3c601a63e2..00c6b2d37b 100644 --- a/src/fstab-generator/fstab-generator.c +++ b/src/fstab-generator/fstab-generator.c @@ -57,6 +57,23 @@ static char *arg_usr_fstype = NULL; static char *arg_usr_options = NULL; static VolatileMode arg_volatile_mode = _VOLATILE_MODE_INVALID; +static int write_options(FILE *f, const char *options) { + _cleanup_free_ char *o = NULL; + + if (isempty(options)) + return 0; + + if (streq(options, "defaults")) + return 0; + + o = strreplace(options, "%", "%%"); + if (!o) + return log_oom(); + + fprintf(f, "Options=%s\n", o); + return 1; +} + static int add_swap( const char *what, struct mntent *me, @@ -105,8 +122,9 @@ static int add_swap( "What=%s\n", what); - if (!isempty(me->mnt_opts) && !streq(me->mnt_opts, "defaults")) - fprintf(f, "Options=%s\n", me->mnt_opts); + r = write_options(f, me->mnt_opts); + if (r < 0) + return r; r = fflush_and_check(f); if (r < 0) @@ -347,8 +365,9 @@ static int add_mount( if (r < 0) return r; - if (!isempty(filtered) && !streq(filtered, "defaults")) - fprintf(f, "Options=%s\n", filtered); + r = write_options(f, filtered); + if (r < 0) + return r; r = fflush_and_check(f); if (r < 0) From 19d0833bead317dc099c10b472c23e665ff76cfd Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Wed, 8 Feb 2017 17:14:58 +0100 Subject: [PATCH 2/2] =?UTF-8?q?fstab-generator:=20also=20convert=20%=20?= =?UTF-8?q?=E2=86=92=20%%=20for=20What=3D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Same reasons as the previous patch. --- man/systemd.mount.xml | 14 +++++------ man/systemd.swap.xml | 20 +++++++--------- src/fstab-generator/fstab-generator.c | 34 +++++++++++++++++++-------- 3 files changed, 39 insertions(+), 29 deletions(-) diff --git a/man/systemd.mount.xml b/man/systemd.mount.xml index 8d9517a913..bb372d788a 100644 --- a/man/systemd.mount.xml +++ b/man/systemd.mount.xml @@ -337,14 +337,12 @@ What= - Takes an absolute path of a device node, file - or other resource to mount. See - mount8 - for details. If this refers to a device node, a dependency on - the respective device unit is automatically created. (See - systemd.device5 - for more information.) This option is - mandatory. + Takes an absolute path of a device node, file or other resource to mount. See mount8 for details. If + this refers to a device node, a dependency on the respective device unit is automatically created. (See + systemd.device5 for more + information.) This option is mandatory. Note that the usual specifier expansion is applied to this setting, + literal percent characters should hence be written as %%. diff --git a/man/systemd.swap.xml b/man/systemd.swap.xml index 7f82d69725..d2c2027228 100644 --- a/man/systemd.swap.xml +++ b/man/systemd.swap.xml @@ -170,17 +170,15 @@ What= - Takes an absolute path of a device node or - file to use for paging. See - swapon8 - for details. If this refers to a device node, a dependency on - the respective device unit is automatically created. (See - systemd.device5 - for more information.) If this refers to a file, a dependency - on the respective mount unit is automatically created. (See - systemd.mount5 - for more information.) This option is - mandatory. + Takes an absolute path of a device node or file to use for paging. See swapon8 for + details. If this refers to a device node, a dependency on the respective device unit is automatically + created. (See + systemd.device5 for more + information.) If this refers to a file, a dependency on the respective mount unit is automatically + created. (See systemd.mount5 + for more information.) This option is mandatory. Note that the usual specifier expansion is applied to this + setting, literal percent characters should hence be written as %%. diff --git a/src/fstab-generator/fstab-generator.c b/src/fstab-generator/fstab-generator.c index 00c6b2d37b..d97bafd1fb 100644 --- a/src/fstab-generator/fstab-generator.c +++ b/src/fstab-generator/fstab-generator.c @@ -74,6 +74,17 @@ static int write_options(FILE *f, const char *options) { return 1; } +static int write_what(FILE *f, const char *what) { + _cleanup_free_ char *w = NULL; + + w = strreplace(what, "%", "%%"); + if (!w) + return log_oom(); + + fprintf(f, "What=%s\n", w); + return 1; +} + static int add_swap( const char *what, struct mntent *me, @@ -113,14 +124,15 @@ static int add_swap( "Failed to create unit file %s: %m", unit); - fprintf(f, - "# Automatically generated by systemd-fstab-generator\n\n" - "[Unit]\n" - "SourcePath=/etc/fstab\n" - "Documentation=man:fstab(5) man:systemd-fstab-generator(8)\n\n" - "[Swap]\n" - "What=%s\n", - what); + fputs("# Automatically generated by systemd-fstab-generator\n\n" + "[Unit]\n" + "SourcePath=/etc/fstab\n" + "Documentation=man:fstab(5) man:systemd-fstab-generator(8)\n\n" + "[Swap]\n", f); + + r = write_what(f, what); + if (r < 0) + return r; r = write_options(f, me->mnt_opts); if (r < 0) @@ -349,11 +361,13 @@ static int add_mount( fprintf(f, "\n" "[Mount]\n" - "What=%s\n" "Where=%s\n", - what, where); + r = write_what(f, what); + if (r < 0) + return r; + if (!isempty(fstype) && !streq(fstype, "auto")) fprintf(f, "Type=%s\n", fstype);