[systemd-devel] [PATCH v2] hostname: Allow comments in /etc/hostname
Lennart Poettering
lennart at poettering.net
Mon May 18 08:12:56 PDT 2015
On Mon, 18.05.15 12:36, Martin Pitt (martin.pitt at ubuntu.com) wrote:
> 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;
This code will result in ENOENT being returned on OOM... That's not right...
Also consider using strstrip() here.
> - 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;
> + }
> + }
Given that this same code is needed twice in different components,
please add a new call read_etc_hostname() to
src/shared/hostname-util.c (which I just added to git). It should then
also do hostname_cleanup() on the name, and thus pretty much replace
read_and_strip_hostname() entirely in hostname-setup.c.
Lennart
--
Lennart Poettering, Red Hat
More information about the systemd-devel
mailing list