teknoraver / rpms / systemd

Forked from rpms/systemd 3 months ago
Clone
Blob Blame History Raw
From d522b56345f2fe53a5ccac396c669d8f74088a6b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
Date: Wed, 3 Sep 2014 22:55:16 -0400
Subject: [PATCH] localed: log locale/keymap changes in detail

Converting X11 to legacy keymaps and back is a fucking mess. Let's
make it at least possible to request detailed logs of what is being
changed and why (LOG_DEBUG level).

At LOG_INFO level, we would log the requested change of X11 or console
keymap, but not the resulting change after conversion to console or X11.
Make sure that every change of configuration on disk has a matching
line in the logs.

Conflicts:
        src/locale/localed.c

(cherry picked from commit 502f961425f9dea1a85239766a3189695194da63)

Related: #1109145
---
 src/locale/localed.c | 84 ++++++++++++++++++++++++++++++++++++++--------------
 1 file changed, 61 insertions(+), 23 deletions(-)

diff --git a/src/locale/localed.c b/src/locale/localed.c
index a93b309..c22e12d 100644
--- a/src/locale/localed.c
+++ b/src/locale/localed.c
@@ -337,9 +337,11 @@ static int read_data(void) {
         return r < 0 ? r : q < 0 ? q : p;
 }
 
-static int write_data_locale(void) {
+static int write_data_locale(char ***settings) {
         int r, p;
-        char **l = NULL;
+        _cleanup_strv_free_ char **l = NULL;
+
+        /* Set values will be returned as strv in *settings on success. */
 
         r = load_env_file("/etc/locale.conf", NULL, &l);
         if (r < 0 && r != -ENOENT)
@@ -355,14 +357,12 @@ static int write_data_locale(void) {
                         continue;
                 }
 
-                if (asprintf(&t, "%s=%s", names[p], data[p]) < 0) {
-                        strv_free(l);
+                if (asprintf(&t, "%s=%s", names[p], data[p]) < 0)
                         return -ENOMEM;
-                }
+
 
                 u = strv_env_set(l, t);
                 free(t);
-                strv_free(l);
 
                 if (!u)
                         return -ENOMEM;
@@ -371,8 +371,6 @@ static int write_data_locale(void) {
         }
 
         if (strv_isempty(l)) {
-                strv_free(l);
-
                 if (unlink("/etc/locale.conf") < 0)
                         return errno == ENOENT ? 0 : -errno;
 
@@ -380,7 +378,11 @@ static int write_data_locale(void) {
         }
 
         r = write_env_file_label("/etc/locale.conf", l);
-        strv_free(l);
+        if (r < 0)
+                return r;
+
+        *settings = l;
+        l = NULL;
 
         return r;
 }
@@ -771,6 +773,12 @@ static int convert_vconsole_to_x11(DBusConnection *connection) {
                 if (r < 0)
                         log_error("Failed to set X11 keyboard layout: %s", strerror(-r));
 
+                log_info("Changed X11 keyboard layout to '%s' model '%s' variant '%s' options '%s'",
+                         strempty(state.x11_layout),
+                         strempty(state.x11_model),
+                         strempty(state.x11_variant),
+                         strempty(state.x11_options));
+
                 changed = bus_properties_changed_new(
                                 "/org/freedesktop/locale1",
                                 "org.freedesktop.locale1",
@@ -787,7 +795,8 @@ static int convert_vconsole_to_x11(DBusConnection *connection) {
 
                 if (!b)
                         return -ENOMEM;
-        }
+        } else
+                log_debug("X11 keyboard layout was not modified.");
 
         return 0;
 }
@@ -805,13 +814,18 @@ static int find_converted_keymap(char **new_keymap) {
 
         NULSTR_FOREACH(dir, KBD_KEYMAP_DIRS) {
                 _cleanup_free_ char *p = NULL, *pz = NULL;
+                bool uncompressed;
 
                 p = strjoin(dir, "xkb/", n, ".map", NULL);
                 pz = strjoin(dir, "xkb/", n, ".map.gz", NULL);
                 if (!p || !pz)
                         return -ENOMEM;
 
-                if (access(p, F_OK) == 0 || access(pz, F_OK) == 0) {
+                uncompressed = access(p, F_OK) == 0;
+                if (uncompressed || access(pz, F_OK) == 0) {
+                        log_debug("Found converted keymap %s at %s",
+                                  n, uncompressed ? p : pz);
+
                         *new_keymap = n;
                         n = NULL;
                         return 1;
@@ -885,13 +899,19 @@ static int find_legacy_keymap(char **new_keymap) {
                 }
 
                 /* The best matching entry so far, then let's save that */
-                if (matching > best_matching) {
-                        best_matching = matching;
+                if (matching >= MAX(best_matching, 1u)) {
+                        log_debug("Found legacy keymap %s with score %u",
+                                  a[0], matching);
 
-                        free(*new_keymap);
-                        *new_keymap = strdup(a[0]);
-                        if (!*new_keymap)
-                                return -ENOMEM;
+                        if (matching > best_matching) {
+                                best_matching = matching;
+
+                                free(*new_keymap);
+                                *new_keymap = strdup(a[0]);
+
+                                if (!*new_keymap)
+                                        return -ENOMEM;
+                        }
                 }
         }
 
@@ -943,6 +963,9 @@ static int convert_x11_to_vconsole(DBusConnection *connection) {
                 if (r < 0)
                         log_error("Failed to set virtual console keymap: %s", strerror(-r));
 
+                log_info("Changed virtual console keymap to '%s' toggle '%s'",
+                         strempty(state.vc_keymap), strempty(state.vc_keymap_toggle));
+
                 changed = bus_properties_changed_new(
                                 "/org/freedesktop/locale1",
                                 "org.freedesktop.locale1",
@@ -959,7 +982,8 @@ static int convert_x11_to_vconsole(DBusConnection *connection) {
                         return -ENOMEM;
 
                 return load_vconsole_keymap(connection, NULL);
-        }
+        } else
+                log_debug("Virtual console keymap was not modified.");
 
         return 0;
 }
@@ -968,6 +992,7 @@ static int append_locale(DBusMessageIter *i, const char *property, void *userdat
         int r, c = 0, p;
         char **l;
 
+        /* Check whether a variable changed and if it is valid */
         l = new0(char*, _PROP_MAX+1);
         if (!l)
                 return -ENOMEM;
@@ -1086,6 +1111,7 @@ static DBusHandlerResult locale_message_handler(
                 }
 
                 if (modified) {
+                        _cleanup_strv_free_ char **settings = NULL;
 
                         r = verify_polkit(connection, message, "org.freedesktop.locale1.set-locale", interactive, NULL, &error);
                         if (r < 0) {
@@ -1127,7 +1153,7 @@ static DBusHandlerResult locale_message_handler(
 
                         simplify();
 
-                        r = write_data_locale();
+                        r = write_data_locale(&settings);
                         if (r < 0) {
                                 log_error("Failed to set locale: %s", strerror(-r));
                                 return bus_send_error_reply(connection, message, NULL, r);
@@ -1135,7 +1161,13 @@ static DBusHandlerResult locale_message_handler(
 
                         push_data(connection);
 
-                        log_info("Changed locale information.");
+                        if (settings) {
+                                _cleanup_free_ char *line;
+
+                                line = strv_join(settings, ", ");
+                                log_info("Changed locale to %s.", strnull(line));
+                        } else
+                                log_info("Changed locale to unset.");
 
                         changed = bus_properties_changed_new(
                                         "/org/freedesktop/locale1",
@@ -1143,8 +1175,10 @@ static DBusHandlerResult locale_message_handler(
                                         "Locale\0");
                         if (!changed)
                                 goto oom;
-                } else
+                } else {
+                        log_debug("Locale settings were not modified.");
                         strv_free(l);
+                }
 
         } else if (dbus_message_is_method_call(message, "org.freedesktop.locale1", "SetVConsoleKeyboard")) {
 
@@ -1188,7 +1222,8 @@ static DBusHandlerResult locale_message_handler(
                                 return bus_send_error_reply(connection, message, NULL, r);
                         }
 
-                        log_info("Changed virtual console keymap to '%s'", strempty(state.vc_keymap));
+                        log_info("Changed virtual console keymap to '%s' toggle '%s'",
+                                 strempty(state.vc_keymap), strempty(state.vc_keymap_toggle));
 
                         r = load_vconsole_keymap(connection, NULL);
                         if (r < 0)
@@ -1266,7 +1301,10 @@ static DBusHandlerResult locale_message_handler(
                                 return bus_send_error_reply(connection, message, NULL, r);
                         }
 
-                        log_info("Changed X11 keyboard layout to '%s'", strempty(state.x11_layout));
+                        log_info("Changed X11 keyboard layout to '%s' model '%s' variant '%s' options '%s'", strempty(state.x11_layout),
+                                 strempty(state.x11_model),
+                                 strempty(state.x11_variant),
+                                 strempty(state.x11_options));
 
                         changed = bus_properties_changed_new(
                                         "/org/freedesktop/locale1",