Package: systemd
Version: 231-1
Severity: normal
Tags: patch

I'm using systemd on an embedded Debian derivative that doesn't routinely
populate /etc/default/keyboard (our target devices don't normally have
keyboards, so it would be meaningless). On this system, localed doesn't
start when activated via D-Bus:

    systemd-localed[9146]: Failed to read locale data: No such file or directory

This appears to be because the upstream code to load a Red Hat-specific
X11 keyboard configuration file tolerates it not existing, but the
Debian patch to load /etc/default/keyboard instead does not tolerate
ENOENT.

To fix this, I applied the patch
0001-x11_read_data-tolerate-absence-of-etc-default-keyboa.patch
(this is a forward-port to 231, the one I initially used was for an
earlier systemd version and touched the same code in a different file)
just after Use-Debian-specific-config-files.patch in the patch
series, then squashed it into that commit to get the attached
0001-Use-Debian-specific-config-files.patch (again, this is a
forward-port).

I'm not attaching a proposed debdiff because the resulting diff between
diffs is basically impossible to review :-)

    S
>From d19610c131ba9b317f2ee3b7c142aa193529c6ee Mon Sep 17 00:00:00 2001
From: Simon McVittie <[email protected]>
Date: Mon, 8 Aug 2016 19:26:42 +0100
Subject: [PATCH] x11_read_data: tolerate absence of /etc/default/keyboard

Signed-off-by: Simon McVittie <[email protected]>
---
 src/locale/keymap-util.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/src/locale/keymap-util.c b/src/locale/keymap-util.c
index fe938c0..50ab05e 100644
--- a/src/locale/keymap-util.c
+++ b/src/locale/keymap-util.c
@@ -188,7 +188,11 @@ static int x11_read_data(Context *c) {
                            "XKBVARIANT",        &c->x11_variant,
                            "XKBOPTIONS",        &c->x11_options,
                            NULL);
-        return r;
+
+        if (r < 0 && r != -ENOENT)
+                return r;
+
+        return 0;
 }
 
 int context_read_data(Context *c) {
-- 
2.8.1

>From bae835c98361a39f64e0a481a898dc87f5d7a293 Mon Sep 17 00:00:00 2001
From: Michael Biebl <[email protected]>
Date: Thu, 18 Jul 2013 20:11:02 +0200
Subject: [PATCH] Use Debian specific config files

Use /etc/default/locale instead of /etc/locale.conf for locale settings.

Use /etc/default/keyboard instead of /etc/X11/xorg.conf.d/00-keyboard.conf for
keyboard configuration.

Read/write /etc/timezone if /etc/localtime does not exist.

[smcv: tolerate absence of /etc/default/keyboard]

Gbp-Pq: Topic debian
Gbp-Pq: Name Use-Debian-specific-config-files.patch
---
 src/basic/time-util.c    |  21 ++++-
 src/core/locale-setup.c  |  22 +++++
 src/locale/keymap-util.c | 203 +++++++++++++++++++++++++----------------------
 src/timedate/timedated.c |  10 +++
 4 files changed, 158 insertions(+), 98 deletions(-)

diff --git a/src/basic/time-util.c b/src/basic/time-util.c
index 24e681b..983a87d 100644
--- a/src/basic/time-util.c
+++ b/src/basic/time-util.c
@@ -1186,8 +1186,25 @@ int get_timezone(char **tz) {
         int r;
 
         r = readlink_malloc("/etc/localtime", &t);
-        if (r < 0)
-                return r; /* returns EINVAL if not a symlink */
+        if (r < 0) {
+                if (r != -EINVAL)
+                        return r; /* returns EINVAL if not a symlink */
+
+                r = read_one_line_file("/etc/timezone", &t);
+                if (r < 0) {
+                        if (r != -ENOENT)
+                                log_warning("Failed to read /etc/timezone: %s", strerror(-r));
+                        return -EINVAL;
+                }
+
+                if (!timezone_is_valid(t))
+                        return -EINVAL;
+                z = strdup(t);
+                if (!z)
+                        return -ENOMEM;
+                *tz = z;
+                return 0;
+        }
 
         e = path_startswith(t, "/usr/share/zoneinfo/");
         if (!e)
diff --git a/src/core/locale-setup.c b/src/core/locale-setup.c
index ccf61d2..119d284 100644
--- a/src/core/locale-setup.c
+++ b/src/core/locale-setup.c
@@ -80,6 +80,28 @@ int locale_setup(char ***environment) {
                         log_warning_errno(r, "Failed to read /etc/locale.conf: %m");
         }
 
+        if (r <= 0) {
+                r = parse_env_file("/etc/default/locale", NEWLINE,
+                                   "LANG",              &variables[VARIABLE_LANG],
+                                   "LANGUAGE",          &variables[VARIABLE_LANGUAGE],
+                                   "LC_CTYPE",          &variables[VARIABLE_LC_CTYPE],
+                                   "LC_NUMERIC",        &variables[VARIABLE_LC_NUMERIC],
+                                   "LC_TIME",           &variables[VARIABLE_LC_TIME],
+                                   "LC_COLLATE",        &variables[VARIABLE_LC_COLLATE],
+                                   "LC_MONETARY",       &variables[VARIABLE_LC_MONETARY],
+                                   "LC_MESSAGES",       &variables[VARIABLE_LC_MESSAGES],
+                                   "LC_PAPER",          &variables[VARIABLE_LC_PAPER],
+                                   "LC_NAME",           &variables[VARIABLE_LC_NAME],
+                                   "LC_ADDRESS",        &variables[VARIABLE_LC_ADDRESS],
+                                   "LC_TELEPHONE",      &variables[VARIABLE_LC_TELEPHONE],
+                                   "LC_MEASUREMENT",    &variables[VARIABLE_LC_MEASUREMENT],
+                                   "LC_IDENTIFICATION", &variables[VARIABLE_LC_IDENTIFICATION],
+                                   NULL);
+
+                if (r < 0 && r != -ENOENT)
+                        log_warning("Failed to read /etc/default/locale: %s", strerror(-r));
+        }
+
         add = NULL;
         for (i = 0; i < _VARIABLE_LC_MAX; i++) {
                 char *s;
diff --git a/src/locale/keymap-util.c b/src/locale/keymap-util.c
index a6bcd1a..50ab05e 100644
--- a/src/locale/keymap-util.c
+++ b/src/locale/keymap-util.c
@@ -121,6 +121,24 @@ static int locale_read_data(Context *c) {
                            "LC_IDENTIFICATION", &c->locale[VARIABLE_LC_IDENTIFICATION],
                            NULL);
 
+        if (r == -ENOENT)
+                r = parse_env_file("/etc/default/locale", NEWLINE,
+                                   "LANG",              &c->locale[VARIABLE_LANG],
+                                   "LANGUAGE",          &c->locale[VARIABLE_LANGUAGE],
+                                   "LC_CTYPE",          &c->locale[VARIABLE_LC_CTYPE],
+                                   "LC_NUMERIC",        &c->locale[VARIABLE_LC_NUMERIC],
+                                   "LC_TIME",           &c->locale[VARIABLE_LC_TIME],
+                                   "LC_COLLATE",        &c->locale[VARIABLE_LC_COLLATE],
+                                   "LC_MONETARY",       &c->locale[VARIABLE_LC_MONETARY],
+                                   "LC_MESSAGES",       &c->locale[VARIABLE_LC_MESSAGES],
+                                   "LC_PAPER",          &c->locale[VARIABLE_LC_PAPER],
+                                   "LC_NAME",           &c->locale[VARIABLE_LC_NAME],
+                                   "LC_ADDRESS",        &c->locale[VARIABLE_LC_ADDRESS],
+                                   "LC_TELEPHONE",      &c->locale[VARIABLE_LC_TELEPHONE],
+                                   "LC_MEASUREMENT",    &c->locale[VARIABLE_LC_MEASUREMENT],
+                                   "LC_IDENTIFICATION", &c->locale[VARIABLE_LC_IDENTIFICATION],
+                                   NULL);
+
         if (r == -ENOENT) {
                 int p;
 
@@ -160,65 +178,19 @@ static int vconsole_read_data(Context *c) {
 }
 
 static int x11_read_data(Context *c) {
-        _cleanup_fclose_ FILE *f;
-        char line[LINE_MAX];
-        bool in_section = false;
         int r;
 
         context_free_x11(c);
 
-        f = fopen("/etc/X11/xorg.conf.d/00-keyboard.conf", "re");
-        if (!f)
-                return errno == ENOENT ? 0 : -errno;
-
-        while (fgets(line, sizeof(line), f)) {
-                char *l;
-
-                char_array_0(line);
-                l = strstrip(line);
-
-                if (l[0] == 0 || l[0] == '#')
-                        continue;
-
-                if (in_section && first_word(l, "Option")) {
-                        _cleanup_strv_free_ char **a = NULL;
-
-                        r = strv_split_extract(&a, l, WHITESPACE, EXTRACT_QUOTES);
-                        if (r < 0)
-                                return r;
-
-                        if (strv_length(a) == 3) {
-                                char **p = NULL;
-
-                                if (streq(a[1], "XkbLayout"))
-                                        p = &c->x11_layout;
-                                else if (streq(a[1], "XkbModel"))
-                                        p = &c->x11_model;
-                                else if (streq(a[1], "XkbVariant"))
-                                        p = &c->x11_variant;
-                                else if (streq(a[1], "XkbOptions"))
-                                        p = &c->x11_options;
-
-                                if (p) {
-                                        free(*p);
-                                        *p = a[2];
-                                        a[2] = NULL;
-                                }
-                        }
-
-                } else if (!in_section && first_word(l, "Section")) {
-                        _cleanup_strv_free_ char **a = NULL;
-
-                        r = strv_split_extract(&a, l, WHITESPACE, EXTRACT_QUOTES);
-                        if (r < 0)
-                                return -ENOMEM;
-
-                        if (strv_length(a) == 2 && streq(a[1], "InputClass"))
-                                in_section = true;
+        r = parse_env_file("/etc/default/keyboard", NEWLINE,
+                           "XKBMODEL",          &c->x11_model,
+                           "XKBLAYOUT",         &c->x11_layout,
+                           "XKBVARIANT",        &c->x11_variant,
+                           "XKBOPTIONS",        &c->x11_options,
+                           NULL);
 
-                } else if (in_section && first_word(l, "EndSection"))
-                        in_section = false;
-        }
+        if (r < 0 && r != -ENOENT)
+                return r;
 
         return 0;
 }
@@ -236,10 +208,15 @@ int context_read_data(Context *c) {
 int locale_write_data(Context *c, char ***settings) {
         int r, p;
         _cleanup_strv_free_ char **l = NULL;
+        const char *path = "/etc/locale.conf";
 
         /* Set values will be returned as strv in *settings on success. */
 
-        r = load_env_file(NULL, "/etc/locale.conf", NULL, &l);
+        r = load_env_file(NULL, path, NULL, &l);
+        if (r < 0 && r == -ENOENT) {
+                path = "/etc/default/locale";
+                r = load_env_file(NULL, path, NULL, &l);
+        }
         if (r < 0 && r != -ENOENT)
                 return r;
 
@@ -268,13 +245,13 @@ int locale_write_data(Context *c, char ***settings) {
         }
 
         if (strv_isempty(l)) {
-                if (unlink("/etc/locale.conf") < 0)
+                if (unlink(path) < 0)
                         return errno == ENOENT ? 0 : -errno;
 
                 return 0;
         }
 
-        r = write_env_file_label("/etc/locale.conf", l);
+        r = write_env_file_label(path, l);
         if (r < 0)
                 return r;
 
@@ -338,65 +315,99 @@ int vconsole_write_data(Context *c) {
 }
 
 int x11_write_data(Context *c) {
-        _cleanup_fclose_ FILE *f = NULL;
-        _cleanup_free_ char *temp_path = NULL;
         int r;
+        char *t, **u, **l = NULL;
 
-        if (isempty(c->x11_layout) &&
-            isempty(c->x11_model) &&
-            isempty(c->x11_variant) &&
-            isempty(c->x11_options)) {
+        r = load_env_file(NULL, "/etc/default/keyboard", NULL, &l);
+        if (r < 0 && r != -ENOENT)
+                return r;
 
-                if (unlink("/etc/X11/xorg.conf.d/00-keyboard.conf") < 0)
-                        return errno == ENOENT ? 0 : -errno;
+        /* This could perhaps be done more elegantly using an array
+         * like we do for the locale, instead of struct
+         */
+        if (isempty(c->x11_layout)) {
+                l = strv_env_unset(l, "XKBLAYOUT");
+        } else {
+                if (asprintf(&t, "XKBLAYOUT=%s", c->x11_layout) < 0) {
+                        strv_free(l);
+                        return -ENOMEM;
+                }
 
-                return 0;
+                u = strv_env_set(l, t);
+                free(t);
+                strv_free(l);
+
+                if (!u)
+                        return -ENOMEM;
+
+                l = u;
         }
 
-        mkdir_p_label("/etc/X11/xorg.conf.d", 0755);
+        if (isempty(c->x11_model)) {
+                l = strv_env_unset(l, "XKBMODEL");
+        } else {
+                if (asprintf(&t, "XKBMODEL=%s", c->x11_model) < 0) {
+                        strv_free(l);
+                        return -ENOMEM;
+                }
 
-        r = fopen_temporary("/etc/X11/xorg.conf.d/00-keyboard.conf", &f, &temp_path);
-        if (r < 0)
-                return r;
+                u = strv_env_set(l, t);
+                free(t);
+                strv_free(l);
 
-        fchmod(fileno(f), 0644);
+                if (!u)
+                        return -ENOMEM;
 
-        fputs("# Read and parsed by systemd-localed. It's probably wise not to edit this file\n"
-              "# manually too freely.\n"
-              "Section \"InputClass\"\n"
-              "        Identifier \"system-keyboard\"\n"
-              "        MatchIsKeyboard \"on\"\n", f);
+                l = u;
+        }
 
-        if (!isempty(c->x11_layout))
-                fprintf(f, "        Option \"XkbLayout\" \"%s\"\n", c->x11_layout);
+        if (isempty(c->x11_variant)) {
+                l = strv_env_unset(l, "XKBVARIANT");
+        } else {
+                if (asprintf(&t, "XKBVARIANT=%s", c->x11_variant) < 0) {
+                        strv_free(l);
+                        return -ENOMEM;
+                }
 
-        if (!isempty(c->x11_model))
-                fprintf(f, "        Option \"XkbModel\" \"%s\"\n", c->x11_model);
+                u = strv_env_set(l, t);
+                free(t);
+                strv_free(l);
 
-        if (!isempty(c->x11_variant))
-                fprintf(f, "        Option \"XkbVariant\" \"%s\"\n", c->x11_variant);
+                if (!u)
+                        return -ENOMEM;
 
-        if (!isempty(c->x11_options))
-                fprintf(f, "        Option \"XkbOptions\" \"%s\"\n", c->x11_options);
+                l = u;
+        }
 
-        fputs("EndSection\n", f);
+        if (isempty(c->x11_options)) {
+                l = strv_env_unset(l, "XKBOPTIONS");
+        } else {
+                if (asprintf(&t, "XKBOPTIONS=%s", c->x11_options) < 0) {
+                        strv_free(l);
+                        return -ENOMEM;
+                }
 
-        r = fflush_and_check(f);
-        if (r < 0)
-                goto fail;
+                u = strv_env_set(l, t);
+                free(t);
+                strv_free(l);
 
-        if (rename(temp_path, "/etc/X11/xorg.conf.d/00-keyboard.conf") < 0) {
-                r = -errno;
-                goto fail;
+                if (!u)
+                        return -ENOMEM;
+
+                l = u;
         }
 
-        return 0;
+        if (strv_isempty(l)) {
+                strv_free(l);
 
-fail:
-        (void) unlink("/etc/X11/xorg.conf.d/00-keyboard.conf");
+                if (unlink("/etc/default/keyboard") < 0)
+                        return errno == ENOENT ? 0 : -errno;
+
+                return 0;
+        }
 
-        if (temp_path)
-                (void) unlink(temp_path);
+        r = write_env_file("/etc/default/keyboard", l);
+        strv_free(l);
 
         return r;
 }
diff --git a/src/timedate/timedated.c b/src/timedate/timedated.c
index ffec609..387d245 100644
--- a/src/timedate/timedated.c
+++ b/src/timedate/timedated.c
@@ -86,6 +86,7 @@ static int context_read_data(Context *c) {
 static int context_write_data_timezone(Context *c) {
         _cleanup_free_ char *p = NULL;
         int r = 0;
+        struct stat st;
 
         assert(c);
 
@@ -93,6 +94,9 @@ static int context_write_data_timezone(Context *c) {
                 if (unlink("/etc/localtime") < 0 && errno != ENOENT)
                         r = -errno;
 
+                if (unlink("/etc/timezone") < 0 && errno != ENOENT)
+                        r = -errno;
+
                 return r;
         }
 
@@ -104,6 +108,12 @@ static int context_write_data_timezone(Context *c) {
         if (r < 0)
                 return r;
 
+        if (stat("/etc/timezone", &st) == 0 && S_ISREG(st.st_mode)) {
+                r = write_string_file("/etc/timezone", c->zone, WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_ATOMIC);
+                if (r < 0)
+                        return r;
+        }
+
         return 0;
 }
 
-- 
2.8.1

Reply via email to