]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
locale: re-read configuration files if changed
authorYu Watanabe <watanabe.yu+github@gmail.com>
Thu, 31 May 2018 07:22:05 +0000 (16:22 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Thu, 31 May 2018 07:24:45 +0000 (16:24 +0900)
Closes #8491.

src/locale/keymap-util.c
src/locale/keymap-util.h
src/locale/localed.c

index 50f7a1b7f7cb5799b9699c78fbb7d8c257437681..f1670ae87936382ddac9abca48934fd8b47ea763 100644 (file)
@@ -80,39 +80,64 @@ void context_free(Context *c) {
         context_free_vconsole(c);
 };
 
-void locale_simplify(Context *c) {
+void locale_simplify(char *locale[_VARIABLE_LC_MAX]) {
         int p;
 
         for (p = VARIABLE_LANG+1; p < _VARIABLE_LC_MAX; p++)
-                if (isempty(c->locale[p]) || streq_ptr(c->locale[VARIABLE_LANG], c->locale[p]))
-                        c->locale[p] = mfree(c->locale[p]);
+                if (isempty(locale[p]) || streq_ptr(locale[VARIABLE_LANG], locale[p]))
+                        locale[p] = mfree(locale[p]);
 }
 
-static int locale_read_data(Context *c) {
+int locale_read_data(Context *c, sd_bus_message *m) {
+        struct stat st;
         int r;
 
-        context_free_locale(c);
+        /* Do not try to re-read the file within single bus operation. */
+        if (m && m == c->locale_cache)
+                return 0;
 
-        r = parse_env_file(NULL, "/etc/locale.conf", 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);
+        /* To suppress multiple call of stat(), store the message to cache here. */
+        c->locale_cache = m;
+
+        r = stat("/etc/locale.conf", &st);
+        if (r < 0 && errno != ENOENT)
+                return -errno;
+
+        if (r >= 0) {
+                usec_t t;
+
+                /* If mtime is not changed, then we do not need to re-read the file. */
+                t = timespec_load(&st.st_mtim);
+                if (c->locale_mtime != USEC_INFINITY && t == c->locale_mtime)
+                        return 0;
 
-        if (r == -ENOENT) {
+                c->locale_mtime = t;
+                context_free_locale(c);
+
+                r = parse_env_file(NULL, "/etc/locale.conf", 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 < 0)
+                        return r;
+        } else {
                 int p;
 
+                c->locale_mtime = USEC_INFINITY;
+                context_free_locale(c);
+
                 /* Fill in what we got passed from systemd. */
                 for (p = 0; p < _VARIABLE_LC_MAX; p++) {
                         const char *name;
@@ -124,41 +149,86 @@ static int locale_read_data(Context *c) {
                         if (r < 0)
                                 return r;
                 }
-
-                r = 0;
         }
 
-        locale_simplify(c);
-        return r;
+        locale_simplify(c->locale);
+        return 0;
 }
 
-static int vconsole_read_data(Context *c) {
+int vconsole_read_data(Context *c, sd_bus_message *m) {
+        struct stat st;
+        usec_t t;
         int r;
 
+        /* Do not try to re-read the file within single bus operation. */
+        if (m && m == c->vc_cache)
+                return 0;
+
+        /* To suppress multiple call of stat(), store the message to cache here. */
+        c->vc_cache = m;
+
+        if (stat("/etc/vconsole.conf", &st) < 0) {
+                if (errno != ENOENT)
+                        return -errno;
+
+                c->vc_mtime = USEC_INFINITY;
+                context_free_vconsole(c);
+                return 0;
+        }
+
+        /* If mtime is not changed, then we do not need to re-read */
+        t = timespec_load(&st.st_mtim);
+        if (c->vc_mtime != USEC_INFINITY && t == c->vc_mtime)
+                return 0;
+
+        c->vc_mtime = t;
         context_free_vconsole(c);
 
         r = parse_env_file(NULL, "/etc/vconsole.conf", NEWLINE,
                            "KEYMAP",        &c->vc_keymap,
                            "KEYMAP_TOGGLE", &c->vc_keymap_toggle,
                            NULL);
-
-        if (r < 0 && r != -ENOENT)
+        if (r < 0)
                 return r;
 
         return 0;
 }
 
-static int x11_read_data(Context *c) {
-        _cleanup_fclose_ FILE *f;
-        char line[LINE_MAX];
+int x11_read_data(Context *c, sd_bus_message *m) {
+        _cleanup_fclose_ FILE *f = NULL;
         bool in_section = false;
+        char line[LINE_MAX];
+        struct stat st;
+        usec_t t;
         int r;
 
+        /* Do not try to re-read the file within single bus operation. */
+        if (m && m == c->x11_cache)
+                return 0;
+
+        /* To suppress multiple call of stat(), store the message to cache here. */
+        c->x11_cache = m;
+
+        if (stat("/etc/X11/xorg.conf.d/00-keyboard.conf", &st) < 0) {
+                if (errno != ENOENT)
+                        return -errno;
+
+                c->x11_mtime = USEC_INFINITY;
+                context_free_x11(c);
+                return 0;
+        }
+
+        /* If mtime is not changed, then we do not need to re-read */
+        t = timespec_load(&st.st_mtim);
+        if (c->x11_mtime != USEC_INFINITY && t == c->x11_mtime)
+                return 0;
+
+        c->x11_mtime = t;
         context_free_x11(c);
 
         f = fopen("/etc/X11/xorg.conf.d/00-keyboard.conf", "re");
         if (!f)
-                return errno == ENOENT ? 0 : -errno;
+                return -errno;
 
         while (fgets(line, sizeof(line), f)) {
                 char *l;
@@ -210,26 +280,13 @@ static int x11_read_data(Context *c) {
         return 0;
 }
 
-int context_read_data(Context *c) {
-        int r, q, p;
-
-        r = locale_read_data(c);
-        q = vconsole_read_data(c);
-        p = x11_read_data(c);
-
-        return r < 0 ? r : q < 0 ? q : p;
-}
-
 int locale_write_data(Context *c, char ***settings) {
-        int r, p;
         _cleanup_strv_free_ char **l = NULL;
+        struct stat st;
+        int r, p;
 
         /* Set values will be returned as strv in *settings on success. */
 
-        r = load_env_file(NULL, "/etc/locale.conf", NULL, &l);
-        if (r < 0 && r != -ENOENT)
-                return r;
-
         for (p = 0; p < _VARIABLE_LC_MAX; p++) {
                 _cleanup_free_ char *t = NULL;
                 char **u;
@@ -238,10 +295,8 @@ int locale_write_data(Context *c, char ***settings) {
                 name = locale_variable_to_string(p);
                 assert(name);
 
-                if (isempty(c->locale[p])) {
-                        l = strv_env_unset(l, name);
+                if (isempty(c->locale[p]))
                         continue;
-                }
 
                 if (asprintf(&t, "%s=%s", name, c->locale[p]) < 0)
                         return -ENOMEM;
@@ -257,6 +312,7 @@ int locale_write_data(Context *c, char ***settings) {
                 if (unlink("/etc/locale.conf") < 0)
                         return errno == ENOENT ? 0 : -errno;
 
+                c->locale_mtime = USEC_INFINITY;
                 return 0;
         }
 
@@ -265,12 +321,17 @@ int locale_write_data(Context *c, char ***settings) {
                 return r;
 
         *settings = TAKE_PTR(l);
+
+        if (stat("/etc/locale.conf", &st) >= 0)
+                c->locale_mtime = timespec_load(&st.st_mtim);
+
         return 0;
 }
 
 int vconsole_write_data(Context *c) {
-        int r;
         _cleanup_strv_free_ char **l = NULL;
+        struct stat st;
+        int r;
 
         r = load_env_file(NULL, "/etc/vconsole.conf", NULL, &l);
         if (r < 0 && r != -ENOENT)
@@ -314,15 +375,24 @@ int vconsole_write_data(Context *c) {
                 if (unlink("/etc/vconsole.conf") < 0)
                         return errno == ENOENT ? 0 : -errno;
 
+                c->vc_mtime = USEC_INFINITY;
                 return 0;
         }
 
-        return write_env_file_label("/etc/vconsole.conf", l);
+        r = write_env_file_label("/etc/vconsole.conf", l);
+        if (r < 0)
+                return r;
+
+        if (stat("/etc/vconsole.conf", &st) >= 0)
+                c->vc_mtime = timespec_load(&st.st_mtim);
+
+        return 0;
 }
 
 int x11_write_data(Context *c) {
         _cleanup_fclose_ FILE *f = NULL;
         _cleanup_free_ char *temp_path = NULL;
+        struct stat st;
         int r;
 
         if (isempty(c->x11_layout) &&
@@ -333,6 +403,7 @@ int x11_write_data(Context *c) {
                 if (unlink("/etc/X11/xorg.conf.d/00-keyboard.conf") < 0)
                         return errno == ENOENT ? 0 : -errno;
 
+                c->vc_mtime = USEC_INFINITY;
                 return 0;
         }
 
@@ -375,6 +446,9 @@ int x11_write_data(Context *c) {
                 goto fail;
         }
 
+        if (stat("/etc/X11/xorg.conf.d/00-keyboard.conf", &st) >= 0)
+                c->x11_mtime = timespec_load(&st.st_mtim);
+
         return 0;
 
 fail:
index 715af154f18f0527ac137b0560d9da4910f53633..41bdd02862fb8eb4a5ac697f1259efcf36cb8a79 100644 (file)
@@ -6,16 +6,25 @@
   Copyright 2013 Kay Sievers
 ***/
 
+#include "sd-bus.h"
+
 #include "locale-util.h"
+#include "time-util.h"
 
 typedef struct Context {
+        sd_bus_message *locale_cache;
+        usec_t locale_mtime;
         char *locale[_VARIABLE_LC_MAX];
 
+        sd_bus_message *x11_cache;
+        usec_t x11_mtime;
         char *x11_layout;
         char *x11_model;
         char *x11_variant;
         char *x11_options;
 
+        sd_bus_message *vc_cache;
+        usec_t vc_mtime;
         char *vc_keymap;
         char *vc_keymap_toggle;
 } Context;
@@ -24,11 +33,14 @@ int find_converted_keymap(const char *x11_layout, const char *x11_variant, char
 int find_legacy_keymap(Context *c, char **new_keymap);
 int find_language_fallback(const char *lang, char **language);
 
-int context_read_data(Context *c);
+int locale_read_data(Context *c, sd_bus_message *m);
+int vconsole_read_data(Context *c, sd_bus_message *m);
+int x11_read_data(Context *c, sd_bus_message *m);
+
 void context_free(Context *c);
 int vconsole_convert_to_x11(Context *c);
 int vconsole_write_data(Context *c);
 int x11_convert_to_vconsole(Context *c);
 int x11_write_data(Context *c);
-void locale_simplify(Context *c);
+void locale_simplify(char *locale[_VARIABLE_LC_MAX]);
 int locale_write_data(Context *c, char ***settings);
index ab16705d1d61efc2e4c9416493e40056a0965c0a..823bcd52b88fa5358ba311717190684a15a4c191 100644 (file)
@@ -88,7 +88,7 @@ static int locale_update_system_manager(Context *c, sd_bus *bus) {
 
         r = sd_bus_call(bus, m, 0, &error, NULL);
         if (r < 0)
-                log_error_errno(r, "Failed to update the manager environment: %m");
+                log_error_errno(r, "Failed to update the manager environment, ignoring: %m");
 
         return 0;
 }
@@ -113,10 +113,14 @@ static int vconsole_reload(sd_bus *bus) {
         return r;
 }
 
-static int vconsole_convert_to_x11_and_emit(Context *c, sd_bus *bus) {
+static int vconsole_convert_to_x11_and_emit(Context *c, sd_bus_message *m) {
         int r;
 
-        assert(bus);
+        assert(m);
+
+        r = x11_read_data(c, m);
+        if (r < 0)
+                return r;
 
         r = vconsole_convert_to_x11(c);
         if (r <= 0)
@@ -127,7 +131,7 @@ static int vconsole_convert_to_x11_and_emit(Context *c, sd_bus *bus) {
         if (r < 0)
                 return log_error_errno(r, "Failed to write X11 keyboard layout: %m");
 
-        sd_bus_emit_properties_changed(bus,
+        sd_bus_emit_properties_changed(sd_bus_message_get_bus(m),
                                        "/org/freedesktop/locale1",
                                        "org.freedesktop.locale1",
                                        "X11Layout", "X11Model", "X11Variant", "X11Options", NULL);
@@ -135,10 +139,14 @@ static int vconsole_convert_to_x11_and_emit(Context *c, sd_bus *bus) {
         return 1;
 }
 
-static int x11_convert_to_vconsole_and_emit(Context *c, sd_bus *bus) {
+static int x11_convert_to_vconsole_and_emit(Context *c, sd_bus_message *m) {
         int r;
 
-        assert(bus);
+        assert(m);
+
+        r = vconsole_read_data(c, m);
+        if (r < 0)
+                return r;
 
         r = x11_convert_to_vconsole(c);
         if (r <= 0)
@@ -149,12 +157,12 @@ static int x11_convert_to_vconsole_and_emit(Context *c, sd_bus *bus) {
         if (r < 0)
                 log_error_errno(r, "Failed to save virtual console keymap: %m");
 
-        sd_bus_emit_properties_changed(bus,
+        sd_bus_emit_properties_changed(sd_bus_message_get_bus(m),
                                        "/org/freedesktop/locale1",
                                        "org.freedesktop.locale1",
                                        "VConsoleKeymap", "VConsoleKeymapToggle", NULL);
 
-        return vconsole_reload(bus);
+        return vconsole_reload(sd_bus_message_get_bus(m));
 }
 
 static int property_get_locale(
@@ -168,7 +176,11 @@ static int property_get_locale(
 
         Context *c = userdata;
         _cleanup_strv_free_ char **l = NULL;
-        int p, q;
+        int p, q, r;
+
+        r = locale_read_data(c, reply);
+        if (r < 0)
+                return r;
 
         l = new0(char*, _VARIABLE_LC_MAX+1);
         if (!l)
@@ -193,16 +205,73 @@ static int property_get_locale(
         return sd_bus_message_append_strv(reply, l);
 }
 
+static int property_get_vconsole(
+                sd_bus *bus,
+                const char *path,
+                const char *interface,
+                const char *property,
+                sd_bus_message *reply,
+                void *userdata,
+                sd_bus_error *error) {
+
+        Context *c = userdata;
+        int r;
+
+        r = vconsole_read_data(c, reply);
+        if (r < 0)
+                return r;
+
+        if (streq(property, "VConsoleKeymap"))
+                return sd_bus_message_append_basic(reply, 's', c->vc_keymap);
+        else if (streq(property, "VConsoleKeymapToggle"))
+                return sd_bus_message_append_basic(reply, 's', c->vc_keymap_toggle);
+
+        return -EINVAL;
+}
+
+static int property_get_xkb(
+                sd_bus *bus,
+                const char *path,
+                const char *interface,
+                const char *property,
+                sd_bus_message *reply,
+                void *userdata,
+                sd_bus_error *error) {
+
+        Context *c = userdata;
+        int r;
+
+        r = x11_read_data(c, reply);
+        if (r < 0)
+                return r;
+
+        if (streq(property, "X11Layout"))
+                return sd_bus_message_append_basic(reply, 's', c->x11_layout);
+        else if (streq(property, "X11Model"))
+                return sd_bus_message_append_basic(reply, 's', c->x11_model);
+        else if (streq(property, "X11Variant"))
+                return sd_bus_message_append_basic(reply, 's', c->x11_variant);
+        else if (streq(property, "X11Options"))
+                return sd_bus_message_append_basic(reply, 's', c->x11_options);
+
+        return -EINVAL;
+}
+
+static void locale_free(char ***l) {
+        int p;
+
+        for (p = 0; p < _VARIABLE_LC_MAX; p++)
+                (*l)[p] = mfree((*l)[p]);
+}
+
 static int method_set_locale(sd_bus_message *m, void *userdata, sd_bus_error *error) {
         Context *c = userdata;
-        _cleanup_strv_free_ char **l = NULL;
-        char **i;
-        const char *lang = NULL;
-        int interactive;
+        _cleanup_strv_free_ char **settings = NULL, **l = NULL;
+        char *new_locale[_VARIABLE_LC_MAX] = {};
+        _cleanup_(locale_free) char **dummy = new_locale;
         bool modified = false;
-        bool have[_VARIABLE_LC_MAX] = {};
-        int p;
-        int r;
+        int interactive, p, r;
+        char **i;
 
         assert(m);
         assert(c);
@@ -215,7 +284,7 @@ static int method_set_locale(sd_bus_message *m, void *userdata, sd_bus_error *er
         if (r < 0)
                 return r;
 
-        /* Check whether a variable changed and if it is valid */
+        /* Check whether a variable is valid */
         STRV_FOREACH(i, l) {
                 bool valid = false;
 
@@ -231,13 +300,10 @@ static int method_set_locale(sd_bus_message *m, void *userdata, sd_bus_error *er
                             (*i)[k] == '=' &&
                             locale_is_valid((*i) + k + 1)) {
                                 valid = true;
-                                have[p] = true;
-
-                                if (p == VARIABLE_LANG)
-                                        lang = (*i) + k + 1;
 
-                                if (!streq_ptr(*i + k + 1, c->locale[p]))
-                                        modified = true;
+                                new_locale[p] = strdup((*i) + k + 1);
+                                if (!new_locale[p])
+                                        return -ENOMEM;
 
                                 break;
                         }
@@ -249,99 +315,82 @@ static int method_set_locale(sd_bus_message *m, void *userdata, sd_bus_error *er
 
         /* If LANG was specified, but not LANGUAGE, check if we should
          * set it based on the language fallback table. */
-        if (have[VARIABLE_LANG] && !have[VARIABLE_LANGUAGE]) {
+        if (!isempty(new_locale[VARIABLE_LANG]) &&
+            isempty(new_locale[VARIABLE_LANGUAGE])) {
                 _cleanup_free_ char *language = NULL;
 
-                assert(lang);
-
-                (void) find_language_fallback(lang, &language);
+                (void) find_language_fallback(new_locale[VARIABLE_LANG], &language);
                 if (language) {
-                        log_debug("Converted LANG=%s to LANGUAGE=%s", lang, language);
-                        if (!streq_ptr(language, c->locale[VARIABLE_LANGUAGE])) {
-                                r = strv_extendf(&l, "LANGUAGE=%s", language);
-                                if (r < 0)
-                                        return r;
-
-                                have[VARIABLE_LANGUAGE] = true;
-                                modified = true;
-                        }
+                        log_debug("Converted LANG=%s to LANGUAGE=%s", new_locale[VARIABLE_LANG], language);
+                        free_and_replace(new_locale[VARIABLE_LANGUAGE], language);
                 }
         }
 
-        /* Check whether a variable is unset */
-        if (!modified)
-                for (p = 0; p < _VARIABLE_LC_MAX; p++)
-                        if (!isempty(c->locale[p]) && !have[p]) {
-                                modified = true;
-                                break;
-                        }
+        r = locale_read_data(c, m);
+        if (r < 0) {
+                log_error_errno(r, "Failed to read locale data: %m");
+                return sd_bus_error_setf(error, SD_BUS_ERROR_FAILED, "Failed to read locale data");
+        }
 
-        if (modified) {
-                _cleanup_strv_free_ char **settings = NULL;
-
-                r = bus_verify_polkit_async(
-                                m,
-                                CAP_SYS_ADMIN,
-                                "org.freedesktop.locale1.set-locale",
-                                NULL,
-                                interactive,
-                                UID_INVALID,
-                                &polkit_registry,
-                                error);
-                if (r < 0)
-                        return r;
-                if (r == 0)
-                        return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
-
-                STRV_FOREACH(i, l)
-                        for (p = 0; p < _VARIABLE_LC_MAX; p++) {
-                                size_t k;
-                                const char *name;
-
-                                name = locale_variable_to_string(p);
-                                assert(name);
-
-                                k = strlen(name);
-                                if (startswith(*i, name) && (*i)[k] == '=') {
-                                        r = free_and_strdup(&c->locale[p], *i + k + 1);
-                                        if (r < 0)
-                                                return r;
-                                        break;
-                                }
-                        }
+        /* Merge with the current settings */
+        for (p = 0; p < _VARIABLE_LC_MAX; p++)
+                if (!isempty(c->locale[p]) && isempty(new_locale[p])) {
+                        new_locale[p] = strdup(c->locale[p]);
+                        if (!new_locale[p])
+                                return -ENOMEM;
+                }
 
-                for (p = 0; p < _VARIABLE_LC_MAX; p++) {
-                        if (have[p])
-                                continue;
+        locale_simplify(new_locale);
 
-                        c->locale[p] = mfree(c->locale[p]);
+        for (p = 0; p < _VARIABLE_LC_MAX; p++)
+                if (!streq_ptr(c->locale[p], new_locale[p])) {
+                        modified = true;
+                        break;
                 }
 
-                locale_simplify(c);
+        if (!modified) {
+                log_debug("Locale settings were not modified.");
+                return sd_bus_reply_method_return(m, NULL);
+        }
 
-                r = locale_write_data(c, &settings);
-                if (r < 0) {
-                        log_error_errno(r, "Failed to set locale: %m");
-                        return sd_bus_error_set_errnof(error, r, "Failed to set locale: %m");
-                }
+        r = bus_verify_polkit_async(
+                        m,
+                        CAP_SYS_ADMIN,
+                        "org.freedesktop.locale1.set-locale",
+                        NULL,
+                        interactive,
+                        UID_INVALID,
+                        &polkit_registry,
+                        error);
+        if (r < 0)
+                return r;
+        if (r == 0)
+                return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
 
-                locale_update_system_manager(c, sd_bus_message_get_bus(m));
+        for (p = 0; p < _VARIABLE_LC_MAX; p++)
+                free_and_replace(c->locale[p], new_locale[p]);
 
-                if (settings) {
-                        _cleanup_free_ char *line;
+        r = locale_write_data(c, &settings);
+        if (r < 0) {
+                log_error_errno(r, "Failed to set locale: %m");
+                return sd_bus_error_set_errnof(error, r, "Failed to set locale: %m");
+        }
 
-                        line = strv_join(settings, ", ");
-                        log_info("Changed locale to %s.", strnull(line));
-                } else
-                        log_info("Changed locale to unset.");
+        (void) locale_update_system_manager(c, sd_bus_message_get_bus(m));
 
-                (void) sd_bus_emit_properties_changed(
-                                sd_bus_message_get_bus(m),
-                                "/org/freedesktop/locale1",
-                                "org.freedesktop.locale1",
-                                "Locale", NULL);
+        if (settings) {
+                _cleanup_free_ char *line;
+
+                line = strv_join(settings, ", ");
+                log_info("Changed locale to %s.", strnull(line));
         } else
-                log_debug("Locale settings were not modified.");
+                log_info("Changed locale to unset.");
+
+        (void) sd_bus_emit_properties_changed(
+                        sd_bus_message_get_bus(m),
+                        "/org/freedesktop/locale1",
+                        "org.freedesktop.locale1",
+                        "Locale", NULL);
 
         return sd_bus_reply_method_return(m, NULL);
 }
@@ -361,6 +410,12 @@ static int method_set_vc_keyboard(sd_bus_message *m, void *userdata, sd_bus_erro
         keymap = empty_to_null(keymap);
         keymap_toggle = empty_to_null(keymap_toggle);
 
+        r = vconsole_read_data(c, m);
+        if (r < 0) {
+                log_error_errno(r, "Failed to read virtual console keymap data: %m");
+                return sd_bus_error_setf(error, SD_BUS_ERROR_FAILED, "Failed to read virtual console keymap data");
+        }
+
         if (streq_ptr(keymap, c->vc_keymap) &&
             streq_ptr(keymap_toggle, c->vc_keymap_toggle))
                 return sd_bus_reply_method_return(m, NULL);
@@ -407,7 +462,7 @@ static int method_set_vc_keyboard(sd_bus_message *m, void *userdata, sd_bus_erro
                         "VConsoleKeymap", "VConsoleKeymapToggle", NULL);
 
         if (convert) {
-                r = vconsole_convert_to_x11_and_emit(c, sd_bus_message_get_bus(m));
+                r = vconsole_convert_to_x11_and_emit(c, m);
                 if (r < 0)
                         log_error_errno(r, "Failed to convert keymap data: %m");
         }
@@ -535,6 +590,12 @@ static int method_set_x11_keyboard(sd_bus_message *m, void *userdata, sd_bus_err
         variant = empty_to_null(variant);
         options = empty_to_null(options);
 
+        r = x11_read_data(c, m);
+        if (r < 0) {
+                log_error_errno(r, "Failed to read x11 keyboard layout data: %m");
+                return sd_bus_error_setf(error, SD_BUS_ERROR_FAILED, "Failed to read x11 keyboard layout data");
+        }
+
         if (streq_ptr(layout, c->x11_layout) &&
             streq_ptr(model, c->x11_model) &&
             streq_ptr(variant, c->x11_variant) &&
@@ -597,7 +658,7 @@ static int method_set_x11_keyboard(sd_bus_message *m, void *userdata, sd_bus_err
                         "X11Layout", "X11Model", "X11Variant", "X11Options", NULL);
 
         if (convert) {
-                r = x11_convert_to_vconsole_and_emit(c, sd_bus_message_get_bus(m));
+                r = x11_convert_to_vconsole_and_emit(c, m);
                 if (r < 0)
                         log_error_errno(r, "Failed to convert keymap data: %m");
         }
@@ -608,12 +669,12 @@ static int method_set_x11_keyboard(sd_bus_message *m, void *userdata, sd_bus_err
 static const sd_bus_vtable locale_vtable[] = {
         SD_BUS_VTABLE_START(0),
         SD_BUS_PROPERTY("Locale", "as", property_get_locale, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
-        SD_BUS_PROPERTY("X11Layout", "s", NULL, offsetof(Context, x11_layout), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
-        SD_BUS_PROPERTY("X11Model", "s", NULL, offsetof(Context, x11_model), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
-        SD_BUS_PROPERTY("X11Variant", "s", NULL, offsetof(Context, x11_variant), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
-        SD_BUS_PROPERTY("X11Options", "s", NULL, offsetof(Context, x11_options), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
-        SD_BUS_PROPERTY("VConsoleKeymap", "s", NULL, offsetof(Context, vc_keymap), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
-        SD_BUS_PROPERTY("VConsoleKeymapToggle", "s", NULL, offsetof(Context, vc_keymap_toggle), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
+        SD_BUS_PROPERTY("X11Layout", "s", property_get_xkb, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
+        SD_BUS_PROPERTY("X11Model", "s", property_get_xkb, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
+        SD_BUS_PROPERTY("X11Variant", "s", property_get_xkb, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
+        SD_BUS_PROPERTY("X11Options", "s", property_get_xkb, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
+        SD_BUS_PROPERTY("VConsoleKeymap", "s", property_get_vconsole, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
+        SD_BUS_PROPERTY("VConsoleKeymapToggle", "s", property_get_vconsole, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
         SD_BUS_METHOD("SetLocale", "asb", NULL, method_set_locale, SD_BUS_VTABLE_UNPRIVILEGED),
         SD_BUS_METHOD("SetVConsoleKeyboard", "ssbb", NULL, method_set_vc_keyboard, SD_BUS_VTABLE_UNPRIVILEGED),
         SD_BUS_METHOD("SetX11Keyboard", "ssssbb", NULL, method_set_x11_keyboard, SD_BUS_VTABLE_UNPRIVILEGED),
@@ -650,7 +711,11 @@ static int connect_bus(Context *c, sd_event *event, sd_bus **_bus) {
 }
 
 int main(int argc, char *argv[]) {
-        _cleanup_(context_free) Context context = {};
+        _cleanup_(context_free) Context context = {
+                .locale_mtime = USEC_INFINITY,
+                .vc_mtime = USEC_INFINITY,
+                .x11_mtime = USEC_INFINITY,
+        };
         _cleanup_(sd_event_unrefp) sd_event *event = NULL;
         _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
         int r;
@@ -680,12 +745,6 @@ int main(int argc, char *argv[]) {
         if (r < 0)
                 goto finish;
 
-        r = context_read_data(&context);
-        if (r < 0) {
-                log_error_errno(r, "Failed to read locale data: %m");
-                goto finish;
-        }
-
         r = bus_event_loop_with_idle(event, bus, "org.freedesktop.locale1", DEFAULT_EXIT_USEC, NULL, NULL);
         if (r < 0)
                 log_error_errno(r, "Failed to run event loop: %m");