62fe945
From 502f961425f9dea1a85239766a3189695194da63 Mon Sep 17 00:00:00 2001
62fe945
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
62fe945
Date: Wed, 3 Sep 2014 22:55:16 -0400
62fe945
Subject: [PATCH] localed: log locale/keymap changes in detail
62fe945
62fe945
Converting X11 to legacy keymaps and back is a fucking mess. Let's
62fe945
make it at least possible to request detailed logs of what is being
62fe945
changed and why (LOG_DEBUG level).
62fe945
62fe945
At LOG_INFO level, we would log the requested change of X11 or console
62fe945
keymap, but not the resulting change after conversion to console or X11.
62fe945
Make sure that every change of configuration on disk has a matching
62fe945
line in the logs.
62fe945
---
62fe945
 src/locale/localed.c | 76 +++++++++++++++++++++++++++++++++++++++++-----------
62fe945
 1 file changed, 60 insertions(+), 16 deletions(-)
62fe945
62fe945
diff --git a/src/locale/localed.c b/src/locale/localed.c
62fe945
index ac8477ad39..777da623f6 100644
62fe945
--- a/src/locale/localed.c
62fe945
+++ b/src/locale/localed.c
62fe945
@@ -265,10 +265,12 @@ static int context_read_data(Context *c) {
62fe945
         return r < 0 ? r : q < 0 ? q : p;
62fe945
 }
62fe945
 
62fe945
-static int locale_write_data(Context *c) {
62fe945
+static int locale_write_data(Context *c, char ***settings) {
62fe945
         int r, p;
62fe945
         _cleanup_strv_free_ char **l = NULL;
62fe945
 
62fe945
+        /* Set values will be returned as strv in *settings on success. */
62fe945
+
62fe945
         r = load_env_file(NULL, "/etc/locale.conf", NULL, &l);
62fe945
         if (r < 0 && r != -ENOENT)
62fe945
                 return r;
62fe945
@@ -302,7 +304,13 @@ static int locale_write_data(Context *c) {
62fe945
                 return 0;
62fe945
         }
62fe945
 
62fe945
-        return write_env_file_label("/etc/locale.conf", l);
62fe945
+        r = write_env_file_label("/etc/locale.conf", l);
62fe945
+        if (r < 0)
62fe945
+                return r;
62fe945
+
62fe945
+        *settings = l;
62fe945
+        l = NULL;
62fe945
+        return 0;
62fe945
 }
62fe945
 
62fe945
 static int locale_update_system_manager(Context *c, sd_bus *bus) {
62fe945
@@ -595,11 +603,18 @@ static int vconsole_convert_to_x11(Context *c, sd_bus *bus) {
62fe945
                 if (r < 0)
62fe945
                         log_error("Failed to set X11 keyboard layout: %s", strerror(-r));
62fe945
 
62fe945
+                log_info("Changed X11 keyboard layout to '%s' model '%s' variant '%s' options '%s'",
62fe945
+                         strempty(c->x11_layout),
62fe945
+                         strempty(c->x11_model),
62fe945
+                         strempty(c->x11_variant),
62fe945
+                         strempty(c->x11_options));
62fe945
+
62fe945
                 sd_bus_emit_properties_changed(bus,
62fe945
                                 "/org/freedesktop/locale1",
62fe945
                                 "org.freedesktop.locale1",
62fe945
                                 "X11Layout", "X11Model", "X11Variant", "X11Options", NULL);
62fe945
-        }
62fe945
+        } else
62fe945
+                log_debug("X11 keyboard layout was not modified.");
62fe945
 
62fe945
         return 0;
62fe945
 }
62fe945
@@ -617,13 +632,18 @@ static int find_converted_keymap(Context *c, char **new_keymap) {
62fe945
 
62fe945
         NULSTR_FOREACH(dir, KBD_KEYMAP_DIRS) {
62fe945
                 _cleanup_free_ char *p = NULL, *pz = NULL;
62fe945
+                bool uncompressed;
62fe945
 
62fe945
                 p = strjoin(dir, "xkb/", n, ".map", NULL);
62fe945
                 pz = strjoin(dir, "xkb/", n, ".map.gz", NULL);
62fe945
                 if (!p || !pz)
62fe945
                         return -ENOMEM;
62fe945
 
62fe945
-                if (access(p, F_OK) == 0 || access(pz, F_OK) == 0) {
62fe945
+                uncompressed = access(p, F_OK) == 0;
62fe945
+                if (uncompressed || access(pz, F_OK) == 0) {
62fe945
+                        log_debug("Found converted keymap %s at %s",
62fe945
+                                  n, uncompressed ? p : pz);
62fe945
+
62fe945
                         *new_keymap = n;
62fe945
                         n = NULL;
62fe945
                         return 1;
62fe945
@@ -697,12 +717,17 @@ static int find_legacy_keymap(Context *c, char **new_keymap) {
62fe945
                 }
62fe945
 
62fe945
                 /* The best matching entry so far, then let's save that */
62fe945
-                if (matching > best_matching) {
62fe945
-                        best_matching = matching;
62fe945
+                if (matching >= MAX(best_matching, 1u)) {
62fe945
+                        log_debug("Found legacy keymap %s with score %u",
62fe945
+                                  a[0], matching);
62fe945
 
62fe945
-                        r = free_and_strdup(new_keymap, a[0]);
62fe945
-                        if (r < 0)
62fe945
-                                return r;
62fe945
+                        if (matching > best_matching) {
62fe945
+                                best_matching = matching;
62fe945
+
62fe945
+                                r = free_and_strdup(new_keymap, a[0]);
62fe945
+                                if (r < 0)
62fe945
+                                        return r;
62fe945
+                        }
62fe945
                 }
62fe945
         }
62fe945
 
62fe945
@@ -747,13 +772,17 @@ static int x11_convert_to_vconsole(Context *c, sd_bus *bus) {
62fe945
                 if (r < 0)
62fe945
                         log_error("Failed to set virtual console keymap: %s", strerror(-r));
62fe945
 
62fe945
+                log_info("Changed virtual console keymap to '%s' toggle '%s'",
62fe945
+                         strempty(c->vc_keymap), strempty(c->vc_keymap_toggle));
62fe945
+
62fe945
                 sd_bus_emit_properties_changed(bus,
62fe945
                                 "/org/freedesktop/locale1",
62fe945
                                 "org.freedesktop.locale1",
62fe945
                                 "VConsoleKeymap", "VConsoleKeymapToggle", NULL);
62fe945
 
62fe945
                 return vconsole_reload(bus);
62fe945
-        }
62fe945
+        } else
62fe945
+                log_debug("Virtual console keymap was not modified.");
62fe945
 
62fe945
         return 0;
62fe945
 }
62fe945
@@ -808,7 +837,7 @@ static int method_set_locale(sd_bus *bus, sd_bus_message *m, void *userdata, sd_
62fe945
         if (r < 0)
62fe945
                 return r;
62fe945
 
62fe945
-        /* Check whether a variable changed and if so valid */
62fe945
+        /* Check whether a variable changed and if it is valid */
62fe945
         STRV_FOREACH(i, l) {
62fe945
                 bool valid = false;
62fe945
 
62fe945
@@ -842,6 +871,8 @@ static int method_set_locale(sd_bus *bus, sd_bus_message *m, void *userdata, sd_
62fe945
                         }
62fe945
 
62fe945
         if (modified) {
62fe945
+                _cleanup_strv_free_ char **settings = NULL;
62fe945
+
62fe945
                 r = bus_verify_polkit_async(m, CAP_SYS_ADMIN, "org.freedesktop.locale1.set-locale", interactive, &c->polkit_registry, error);
62fe945
                 if (r < 0)
62fe945
                         return r;
62fe945
@@ -870,7 +901,7 @@ static int method_set_locale(sd_bus *bus, sd_bus_message *m, void *userdata, sd_
62fe945
 
62fe945
                 locale_simplify(c);
62fe945
 
62fe945
-                r = locale_write_data(c);
62fe945
+                r = locale_write_data(c, &settings);
62fe945
                 if (r < 0) {
62fe945
                         log_error("Failed to set locale: %s", strerror(-r));
62fe945
                         return sd_bus_error_set_errnof(error, r, "Failed to set locale: %s", strerror(-r));
62fe945
@@ -878,13 +909,21 @@ static int method_set_locale(sd_bus *bus, sd_bus_message *m, void *userdata, sd_
62fe945
 
62fe945
                 locale_update_system_manager(c, bus);
62fe945
 
62fe945
-                log_info("Changed locale information.");
62fe945
+                if (settings) {
62fe945
+                        _cleanup_free_ char *line;
62fe945
+
62fe945
+                        line = strv_join(settings, ", ");
62fe945
+                        log_info("Changed locale to %s.", strnull(line));
62fe945
+                } else
62fe945
+                        log_info("Changed locale to unset.");
62fe945
 
62fe945
                 sd_bus_emit_properties_changed(bus,
62fe945
                                 "/org/freedesktop/locale1",
62fe945
                                 "org.freedesktop.locale1",
62fe945
                                 "Locale", NULL);
62fe945
-        }
62fe945
+        } else
62fe945
+                log_debug("Locale settings were not modified.");
62fe945
+
62fe945
 
62fe945
         return sd_bus_reply_method_return(m, NULL);
62fe945
 }
62fe945
@@ -928,7 +967,8 @@ static int method_set_vc_keyboard(sd_bus *bus, sd_bus_message *m, void *userdata
62fe945
                         return sd_bus_error_set_errnof(error, r, "Failed to set virtual console keymap: %s", strerror(-r));
62fe945
                 }
62fe945
 
62fe945
-                log_info("Changed virtual console keymap to '%s'", strempty(c->vc_keymap));
62fe945
+                log_info("Changed virtual console keymap to '%s' toggle '%s'",
62fe945
+                         strempty(c->vc_keymap), strempty(c->vc_keymap_toggle));
62fe945
 
62fe945
                 r = vconsole_reload(bus);
62fe945
                 if (r < 0)
62fe945
@@ -1000,7 +1040,11 @@ static int method_set_x11_keyboard(sd_bus *bus, sd_bus_message *m, void *userdat
62fe945
                         return sd_bus_error_set_errnof(error, r, "Failed to set X11 keyboard layout: %s", strerror(-r));
62fe945
                 }
62fe945
 
62fe945
-                log_info("Changed X11 keyboard layout to '%s'", strempty(c->x11_layout));
62fe945
+                log_info("Changed X11 keyboard layout to '%s' model '%s' variant '%s' options '%s'",
62fe945
+                         strempty(c->x11_layout),
62fe945
+                         strempty(c->x11_model),
62fe945
+                         strempty(c->x11_variant),
62fe945
+                         strempty(c->x11_options));
62fe945
 
62fe945
                 sd_bus_emit_properties_changed(bus,
62fe945
                                 "/org/freedesktop/locale1",