Hey Thomas, Thomas H.P. Andersen [2015-05-18 11:49 +0200]: > On Mon, May 18, 2015 at 11:33 AM, Martin Pitt <martin.p...@ubuntu.com> wrote: > > This currently uses read_full_file() and strv_split_newlines() which > > is relatively expensive (although, in most cases there will only be > > one line). In cases like these should we rather use an fgets() loop? > > I think FOREACH_LINE would make this more readable.
Indeed, thanks for pointing out! Updated patch attached. Martin -- Martin Pitt | http://www.piware.de Ubuntu Developer (www.ubuntu.com) | Debian Developer (www.debian.org)
From d485e7ea74f531b1fbd95ecaeab545f4bd2c10c2 Mon Sep 17 00:00:00 2001 From: Martin Pitt <martin.p...@ubuntu.com> Date: Mon, 18 May 2015 11:25:20 +0200 Subject: [PATCH] hostname: Allow comments in /etc/hostname The hostname(1) tool allows comments in /etc/hostname. For compatibility, allow them in hostname-setup.c and hostnamed. https://launchpad.net/bugs/1053048 --- man/hostname.xml | 3 ++- src/core/hostname-setup.c | 21 ++++++++++++++++----- src/hostname/hostnamed.c | 16 +++++++++++++--- 3 files changed, 31 insertions(+), 9 deletions(-) diff --git a/man/hostname.xml b/man/hostname.xml index 5d3d46d..5c68d3d 100644 --- a/man/hostname.xml +++ b/man/hostname.xml @@ -57,7 +57,8 @@ name of the local system that is set during boot using the <citerefentry><refentrytitle>sethostname</refentrytitle><manvolnum>2</manvolnum></citerefentry> system call. It should contain a single newline-terminated - hostname string. The hostname may be a free-form string up to 64 + hostname string. Comments (lines starting with a `#') are ignored. + The hostname may be a free-form string up to 64 characters in length; however, it is recommended that it consists only of 7-bit ASCII lower-case characters and no spaces or dots, and limits itself to the format allowed for DNS domain name diff --git a/src/core/hostname-setup.c b/src/core/hostname-setup.c index 03b0ce3..9b4bfd1 100644 --- a/src/core/hostname-setup.c +++ b/src/core/hostname-setup.c @@ -30,15 +30,26 @@ #include "fileio.h" static int read_and_strip_hostname(const char *path, char **hn) { - char *s; - int r; + _cleanup_fclose_ FILE *f = NULL; + char l[LINE_MAX]; + char *s = NULL; assert(path); assert(hn); - r = read_one_line_file(path, &s); - if (r < 0) - return r; + /* may have comments, ignore them */ + f = fopen("/etc/hostname", "re"); + if (!f) + return -errno; + FOREACH_LINE(l, f, return -errno) { + truncate_nl(l); + if (l[0] != '\0' && l[0] != '#') { + s = strdup(l); + break; + } + } + if (!s) + return -ENOENT; hostname_cleanup(s, false); diff --git a/src/hostname/hostnamed.c b/src/hostname/hostnamed.c index ddf7b8f..029f1fe 100644 --- a/src/hostname/hostnamed.c +++ b/src/hostname/hostnamed.c @@ -78,6 +78,8 @@ static void context_free(Context *c) { static int context_read_data(Context *c) { int r; struct utsname u; + _cleanup_fclose_ FILE *f = NULL; + char l[LINE_MAX]; assert(c); @@ -95,9 +97,17 @@ static int context_read_data(Context *c) { if (!c->data[PROP_HOSTNAME]) return -ENOMEM; - r = read_one_line_file("/etc/hostname", &c->data[PROP_STATIC_HOSTNAME]); - if (r < 0 && r != -ENOENT) - return r; + /* /etc/hostname may have comments, ignore them */ + f = fopen("/etc/hostname", "re"); + if (!f) + return -errno; + FOREACH_LINE(l, f, return -errno) { + truncate_nl(l); + if (l[0] != '\0' && l[0] != '#') { + c->data[PROP_STATIC_HOSTNAME] = strdup(l); + break; + } + } r = parse_env_file("/etc/machine-info", NEWLINE, "PRETTY_HOSTNAME", &c->data[PROP_PRETTY_HOSTNAME], -- 2.1.4
signature.asc
Description: Digital signature
_______________________________________________ systemd-devel mailing list systemd-devel@lists.freedesktop.org http://lists.freedesktop.org/mailman/listinfo/systemd-devel