[systemd-devel] [PATCH] hostname: Allow comments in /etc/hostname

2015-05-18 Thread Martin Pitt
Hello all,

We got a report [1] that systemd doesn't allow comments in
/etc/hostname. If there is a comment, your host name ends up being
# comment. But the original hostname(1) tool documents that comments
are allowed, thus in the sysvinit/upstart world these worked fine.
Earlier versions of cloud-init also produced a comment there.

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?
Given that host names are limited to 64 bytes (HOST_NAME_MAX) this
could be written more efficiently.

Thanks,

Martin

[1] https://launchpad.net/bugs/1053048
-- 
Martin Pitt| http://www.piware.de
Ubuntu Developer (www.ubuntu.com)  | Debian Developer  (www.debian.org)
From 0d5195a617df34abcd200a49c5560f46337e5e74 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 | 18 --
 src/hostname/hostnamed.c  | 13 -
 3 files changed, 30 insertions(+), 4 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
 citerefentryrefentrytitlesethostname/refentrytitlemanvolnum2/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..72a002a 100644
--- a/src/core/hostname-setup.c
+++ b/src/core/hostname-setup.c
@@ -28,17 +28,31 @@
 #include util.h
 #include log.h
 #include fileio.h
+#include strv.h
 
 static int read_and_strip_hostname(const char *path, char **hn) {
-char *s;
+_cleanup_free_ char *contents = NULL;
+_cleanup_strv_free_ char **lines = NULL;
+char **l;
+char *s = NULL;
 int r;
 
 assert(path);
 assert(hn);
 
-r = read_one_line_file(path, s);
+/* may have comments, ignore them */
+r = read_full_file(/etc/hostname, contents, NULL);
 if (r  0)
 return r;
+lines = strv_split_newlines(contents);
+STRV_FOREACH(l, lines) {
+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..24abf58 100644
--- a/src/hostname/hostnamed.c
+++ b/src/hostname/hostnamed.c
@@ -78,6 +78,9 @@ static void context_free(Context *c) {
 static int context_read_data(Context *c) {
 int r;
 struct utsname u;
+_cleanup_free_ char *contents = NULL;
+_cleanup_strv_free_ char **lines = NULL;
+char **l;
 
 assert(c);
 
@@ -95,9 +98,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]);
+/* may have comments, ignore them */
+r = read_full_file(/etc/hostname, contents, NULL);
 if (r  0  r != -ENOENT)
 return r;
+lines = strv_split_newlines(contents);
+STRV_FOREACH(l, lines) {
+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


Re: [systemd-devel] [PATCH] hostname: Allow comments in /etc/hostname

2015-05-18 Thread Thomas H.P. Andersen
On Mon, May 18, 2015 at 11:33 AM, Martin Pitt martin.p...@ubuntu.com wrote:
 Hello all,

 We got a report [1] that systemd doesn't allow comments in
 /etc/hostname. If there is a comment, your host name ends up being
 # comment. But the original hostname(1) tool documents that comments
 are allowed, thus in the sysvinit/upstart world these worked fine.
 Earlier versions of cloud-init also produced a comment there.

 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.

 Given that host names are limited to 64 bytes (HOST_NAME_MAX) this
 could be written more efficiently.
___
systemd-devel mailing list
systemd-devel@lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/systemd-devel


Re: [systemd-devel] [PATCH] hostname: Allow comments in /etc/hostname

2015-05-18 Thread systemd github import bot
Patchset imported to github.
Pull request:
https://github.com/systemd-devs/systemd/compare/master...systemd-mailing-devs:20150518093303.GF4392%40piware.de

--
Generated by https://github.com/haraldh/mail2git
___
systemd-devel mailing list
systemd-devel@lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/systemd-devel