1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2011 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 #include <dbus/dbus.h>
30 #include "dbus-common.h"
35 " <interface name=\"org.freedesktop.locale1\">\n" \
36 " <property name=\"Locale\" type=\"as\" access=\"read\"/>\n" \
37 " <property name=\"VConsoleKeymap\" type=\"s\" access=\"read\"/>\n" \
38 " <property name=\"VConsoleKeymapToggle\" type=\"s\" access=\"read\"/>\n" \
39 " <property name=\"X11Layout\" type=\"s\" access=\"read\"/>\n" \
40 " <property name=\"X11Model\" type=\"s\" access=\"read\"/>\n" \
41 " <property name=\"X11Variant\" type=\"s\" access=\"read\"/>\n" \
42 " <property name=\"X11Options\" type=\"s\" access=\"read\"/>\n" \
43 " <method name=\"SetLocale\">\n" \
44 " <arg name=\"locale\" type=\"as\" direction=\"in\"/>\n" \
45 " <arg name=\"user_interaction\" type=\"b\" direction=\"in\"/>\n" \
47 " <method name=\"SetVConsoleKeyboard\">\n" \
48 " <arg name=\"keymap\" type=\"s\" direction=\"in\"/>\n" \
49 " <arg name=\"keymap_toggle\" type=\"s\" direction=\"in\"/>\n" \
50 " <arg name=\"convert\" type=\"b\" direction=\"in\"/>\n" \
51 " <arg name=\"user_interaction\" type=\"b\" direction=\"in\"/>\n" \
53 " <method name=\"SetX11Keyboard\">\n" \
54 " <arg name=\"layout\" type=\"s\" direction=\"in\"/>\n" \
55 " <arg name=\"model\" type=\"s\" direction=\"in\"/>\n" \
56 " <arg name=\"variant\" type=\"s\" direction=\"in\"/>\n" \
57 " <arg name=\"options\" type=\"s\" direction=\"in\"/>\n" \
58 " <arg name=\"convert\" type=\"b\" direction=\"in\"/>\n" \
59 " <arg name=\"user_interaction\" type=\"b\" direction=\"in\"/>\n" \
63 #define INTROSPECTION \
64 DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE \
67 BUS_PROPERTIES_INTERFACE \
68 BUS_INTROSPECTABLE_INTERFACE \
72 #define INTERFACES_LIST \
73 BUS_GENERIC_INTERFACES_LIST \
74 "org.freedesktop.locale1\0"
76 const char locale_interface[] _introspect_("locale1") = INTERFACE;
79 /* We don't list LC_ALL here on purpose. People should be
80 * using LANG instead. */
95 PROP_LC_IDENTIFICATION,
99 static const char * const names[_PROP_MAX] = {
100 [PROP_LANG] = "LANG",
101 [PROP_LANGUAGE] = "LANGUAGE",
102 [PROP_LC_CTYPE] = "LC_CTYPE",
103 [PROP_LC_NUMERIC] = "LC_NUMERIC",
104 [PROP_LC_TIME] = "LC_TIME",
105 [PROP_LC_COLLATE] = "LC_COLLATE",
106 [PROP_LC_MONETARY] = "LC_MONETARY",
107 [PROP_LC_MESSAGES] = "LC_MESSAGES",
108 [PROP_LC_PAPER] = "LC_PAPER",
109 [PROP_LC_NAME] = "LC_NAME",
110 [PROP_LC_ADDRESS] = "LC_ADDRESS",
111 [PROP_LC_TELEPHONE] = "LC_TELEPHONE",
112 [PROP_LC_MEASUREMENT] = "LC_MEASUREMENT",
113 [PROP_LC_IDENTIFICATION] = "LC_IDENTIFICATION"
116 static char *data[_PROP_MAX] = {
132 static char *x11_layout = NULL, *x11_model = NULL, *x11_variant = NULL, *x11_options = NULL;
133 static char *vc_keymap = NULL, *vc_keymap_toggle = NULL;
135 static usec_t remain_until = 0;
137 static int free_and_set(char **s, const char *v) {
143 r = strdup_or_null(isempty(v) ? NULL : v, &t);
153 static void free_data_locale(void) {
156 for (p = 0; p < _PROP_MAX; p++) {
162 static void free_data_x11(void) {
168 x11_layout = x11_model = x11_variant = x11_options = NULL;
171 static void free_data_vconsole(void) {
173 free(vc_keymap_toggle);
175 vc_keymap = vc_keymap_toggle = NULL;
178 static void simplify(void) {
181 for (p = 1; p < _PROP_MAX; p++)
182 if (isempty(data[p]) || streq_ptr(data[PROP_LANG], data[p])) {
188 static int read_data_locale(void) {
193 r = parse_env_file("/etc/locale.conf", NEWLINE,
194 "LANG", &data[PROP_LANG],
195 "LANGUAGE", &data[PROP_LANGUAGE],
196 "LC_CTYPE", &data[PROP_LC_CTYPE],
197 "LC_NUMERIC", &data[PROP_LC_NUMERIC],
198 "LC_TIME", &data[PROP_LC_TIME],
199 "LC_COLLATE", &data[PROP_LC_COLLATE],
200 "LC_MONETARY", &data[PROP_LC_MONETARY],
201 "LC_MESSAGES", &data[PROP_LC_MESSAGES],
202 "LC_PAPER", &data[PROP_LC_PAPER],
203 "LC_NAME", &data[PROP_LC_NAME],
204 "LC_ADDRESS", &data[PROP_LC_ADDRESS],
205 "LC_TELEPHONE", &data[PROP_LC_TELEPHONE],
206 "LC_MEASUREMENT", &data[PROP_LC_MEASUREMENT],
207 "LC_IDENTIFICATION", &data[PROP_LC_IDENTIFICATION],
213 /* Fill in what we got passed from systemd. */
215 for (p = 0; p < _PROP_MAX; p++) {
220 e = getenv(names[p]);
239 static void free_data(void) {
241 free_data_vconsole();
245 static int read_data_vconsole(void) {
248 free_data_vconsole();
250 r = parse_env_file("/etc/vconsole.conf", NEWLINE,
251 "KEYMAP", &vc_keymap,
252 "KEYMAP_TOGGLE", &vc_keymap_toggle,
255 if (r < 0 && r != -ENOENT)
261 static int read_data_x11(void) {
264 bool in_section = false;
268 f = fopen("/etc/X11/xorg.conf.d/00-keyboard.conf", "re");
270 if (errno == ENOENT) {
273 f = fopen("/etc/X11/xorg.conf.d/00-system-setup-keyboard.conf", "re");
288 while (fgets(line, sizeof(line), f)) {
294 if (l[0] == 0 || l[0] == '#')
297 if (in_section && first_word(l, "Option")) {
300 a = strv_split_quoted(l);
306 if (strv_length(a) == 3) {
308 if (streq(a[1], "XkbLayout")) {
312 } else if (streq(a[1], "XkbModel")) {
316 } else if (streq(a[1], "XkbVariant")) {
320 } else if (streq(a[1], "XkbOptions")) {
329 } else if (!in_section && first_word(l, "Section")) {
332 a = strv_split_quoted(l);
338 if (strv_length(a) == 2 && streq(a[1], "InputClass"))
342 } else if (in_section && first_word(l, "EndSection"))
351 static int read_data(void) {
354 r = read_data_locale();
355 q = read_data_vconsole();
358 return r < 0 ? r : q < 0 ? q : p;
361 static int write_data_locale(void) {
365 r = load_env_file("/etc/locale.conf", &l);
366 if (r < 0 && r != -ENOENT)
369 for (p = 0; p < _PROP_MAX; p++) {
374 if (isempty(data[p])) {
375 l = strv_env_unset(l, names[p]);
379 if (asprintf(&t, "%s=%s", names[p], data[p]) < 0) {
384 u = strv_env_set(l, t);
394 if (strv_isempty(l)) {
397 if (unlink("/etc/locale.conf") < 0)
398 return errno == ENOENT ? 0 : -errno;
403 r = write_env_file("/etc/locale.conf", l);
409 static void push_data(DBusConnection *bus) {
410 char **l_set = NULL, **l_unset = NULL, **t;
411 int c_set = 0, c_unset = 0, p;
413 DBusMessage *m = NULL, *reply = NULL;
414 DBusMessageIter iter, sub;
416 dbus_error_init(&error);
420 l_set = new0(char*, _PROP_MAX);
421 l_unset = new0(char*, _PROP_MAX);
422 if (!l_set || !l_unset) {
423 log_error("Out of memory");
427 for (p = 0; p < _PROP_MAX; p++) {
430 if (isempty(data[p]))
431 l_unset[c_set++] = (char*) names[p];
435 if (asprintf(&s, "%s=%s", names[p], data[p]) < 0) {
436 log_error("Out of memory");
440 l_set[c_unset++] = s;
444 assert(c_set + c_unset == _PROP_MAX);
445 m = dbus_message_new_method_call("org.freedesktop.systemd1", "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "UnsetAndSetEnvironment");
447 log_error("Could not allocate message.");
451 dbus_message_iter_init_append(m, &iter);
453 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub)) {
454 log_error("Out of memory.");
458 STRV_FOREACH(t, l_unset)
459 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, t)) {
460 log_error("Out of memory.");
464 if (!dbus_message_iter_close_container(&iter, &sub) ||
465 !dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub)) {
466 log_error("Out of memory.");
470 STRV_FOREACH(t, l_set)
471 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, t)) {
472 log_error("Out of memory.");
476 if (!dbus_message_iter_close_container(&iter, &sub)) {
477 log_error("Out of memory.");
481 reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error);
483 log_error("Failed to set locale information: %s", bus_error_message(&error));
489 dbus_message_unref(m);
492 dbus_message_unref(reply);
494 dbus_error_free(&error);
500 static int write_data_vconsole(void) {
504 r = load_env_file("/etc/vconsole.conf", &l);
505 if (r < 0 && r != -ENOENT)
508 if (isempty(vc_keymap))
509 l = strv_env_unset(l, "KEYMAP");
513 s = strappend("KEYMAP=", vc_keymap);
519 u = strv_env_set(l, s);
529 if (isempty(vc_keymap_toggle))
530 l = strv_env_unset(l, "KEYMAP_TOGGLE");
534 s = strappend("KEYMAP_TOGGLE=", vc_keymap_toggle);
540 u = strv_env_set(l, s);
550 if (strv_isempty(l)) {
553 if (unlink("/etc/vconsole.conf") < 0)
554 return errno == ENOENT ? 0 : -errno;
559 r = write_env_file("/etc/vconsole.conf", l);
565 static int write_data_x11(void) {
570 if (isempty(x11_layout) &&
571 isempty(x11_model) &&
572 isempty(x11_variant) &&
573 isempty(x11_options)) {
576 unlink("/etc/X11/xorg.conf.d/00-system-setup-keyboard.conf");
579 if (unlink("/etc/X11/xorg.conf.d/00-keyboard.conf") < 0)
580 return errno == ENOENT ? 0 : -errno;
585 mkdir_parents("/etc/X11/xorg.conf.d", 0755);
587 r = fopen_temporary("/etc/X11/xorg.conf.d/00-keyboard.conf", &f, &temp_path);
591 fchmod(fileno(f), 0644);
593 fputs("# Read and parsed by systemd-localed. It's probably wise not to edit this file\n"
594 "# manually too freely.\n"
595 "Section \"InputClass\"\n"
596 " Identifier \"system-keyboard\"\n"
597 " MatchIsKeyboard \"on\"\n", f);
599 if (!isempty(x11_layout))
600 fprintf(f, " Option \"XkbLayout\" \"%s\"\n", x11_layout);
602 if (!isempty(x11_model))
603 fprintf(f, " Option \"XkbModel\" \"%s\"\n", x11_model);
605 if (!isempty(x11_variant))
606 fprintf(f, " Option \"XkbVariant\" \"%s\"\n", x11_variant);
608 if (!isempty(x11_options))
609 fprintf(f, " Option \"XkbOptions\" \"%s\"\n", x11_options);
611 fputs("EndSection\n", f);
614 if (ferror(f) || rename(temp_path, "/etc/X11/xorg.conf.d/00-keyboard.conf") < 0) {
616 unlink("/etc/X11/xorg.conf.d/00-keyboard.conf");
621 unlink("/etc/X11/xorg.conf.d/00-system-setup-keyboard.conf");
633 static int load_vconsole_keymap(DBusConnection *bus, DBusError *error) {
634 DBusMessage *m = NULL, *reply = NULL;
635 const char *name = "systemd-vconsole-setup.service", *mode = "replace";
642 dbus_error_init(&_error);
646 m = dbus_message_new_method_call(
647 "org.freedesktop.systemd1",
648 "/org/freedesktop/systemd1",
649 "org.freedesktop.systemd1.Manager",
652 log_error("Could not allocate message.");
657 if (!dbus_message_append_args(m,
658 DBUS_TYPE_STRING, &name,
659 DBUS_TYPE_STRING, &mode,
660 DBUS_TYPE_INVALID)) {
661 log_error("Could not append arguments to message.");
666 reply = dbus_connection_send_with_reply_and_block(bus, m, -1, error);
668 log_error("Failed to issue method call: %s", bus_error_message(error));
677 dbus_message_unref(m);
680 dbus_message_unref(reply);
682 if (error == &_error)
683 dbus_error_free(error);
688 static char *strnulldash(const char *s) {
689 return s == NULL || *s == 0 || (s[0] == '-' && s[1] == 0) ? NULL : (char*) s;
692 static int read_next_mapping(FILE *f, unsigned *n, char ***a) {
702 if (!fgets(line, sizeof(line), f)) {
705 return errno ? -errno : -EIO;
713 if (l[0] == 0 || l[0] == '#')
716 b = strv_split_quoted(l);
720 if (strv_length(b) < 5) {
721 log_error("Invalid line "SYSTEMD_KBD_MODEL_MAP":%u, ignoring.", *n);
732 static int convert_vconsole_to_x11(DBusConnection *connection) {
733 bool modified = false;
737 if (isempty(vc_keymap)) {
740 !isempty(x11_layout) ||
741 !isempty(x11_model) ||
742 !isempty(x11_variant) ||
743 !isempty(x11_options);
750 f = fopen(SYSTEMD_KBD_MODEL_MAP, "re");
758 r = read_next_mapping(f, &n, &a);
767 if (!streq(vc_keymap, a[0])) {
772 if (!streq_ptr(x11_layout, strnulldash(a[1])) ||
773 !streq_ptr(x11_model, strnulldash(a[2])) ||
774 !streq_ptr(x11_variant, strnulldash(a[3])) ||
775 !streq_ptr(x11_options, strnulldash(a[4]))) {
777 if (free_and_set(&x11_layout, strnulldash(a[1])) < 0 ||
778 free_and_set(&x11_model, strnulldash(a[2])) < 0 ||
779 free_and_set(&x11_variant, strnulldash(a[3])) < 0 ||
780 free_and_set(&x11_options, strnulldash(a[4])) < 0) {
798 DBusMessage *changed;
801 r = write_data_x11();
803 log_error("Failed to set X11 keyboard layout: %s", strerror(-r));
805 changed = bus_properties_changed_new(
806 "/org/freedesktop/locale1",
807 "org.freedesktop.locale1",
816 b = dbus_connection_send(connection, changed, NULL);
817 dbus_message_unref(changed);
826 static int convert_x11_to_vconsole(DBusConnection *connection) {
827 bool modified = false;
831 if (isempty(x11_layout)) {
834 !isempty(vc_keymap) ||
835 !isempty(vc_keymap_toggle);
841 unsigned best_matching = 0;
842 char *new_keymap = NULL;
844 f = fopen(SYSTEMD_KBD_MODEL_MAP, "re");
850 unsigned matching = 0;
853 r = read_next_mapping(f, &n, &a);
862 /* Determine how well matching this entry is */
863 if (streq_ptr(x11_layout, a[1]))
864 /* If we got an exact match, this is best */
869 x = strcspn(x11_layout, ",");
871 /* We have multiple X layouts, look
872 * for an entry that matches our key
873 * with the everything but the first
874 * layout stripped off. */
877 strncmp(x11_layout, a[1], x) == 0)
882 /* If that didn't work, strip
883 * off the other layouts from
886 w = strcspn(a[1], ",");
888 if (x > 0 && x == w &&
889 memcmp(x11_layout, a[1], x) == 0)
895 streq_ptr(x11_model, a[2])) {
898 if (streq_ptr(x11_variant, a[3])) {
901 if (streq_ptr(x11_options, a[4]))
906 /* The best matching entry so far, then let's
908 if (matching > best_matching) {
909 best_matching = matching;
912 new_keymap = strdup(a[0]);
926 if (!streq_ptr(vc_keymap, new_keymap)) {
928 vc_keymap = new_keymap;
930 free(vc_keymap_toggle);
931 vc_keymap_toggle = NULL;
940 DBusMessage *changed;
943 r = write_data_vconsole();
945 log_error("Failed to set virtual console keymap: %s", strerror(-r));
947 changed = bus_properties_changed_new(
948 "/org/freedesktop/locale1",
949 "org.freedesktop.locale1",
951 "VConsoleKeymapToggle\0");
956 b = dbus_connection_send(connection, changed, NULL);
957 dbus_message_unref(changed);
962 return load_vconsole_keymap(connection, NULL);
968 static int append_locale(DBusMessageIter *i, const char *property, void *userdata) {
972 l = new0(char*, _PROP_MAX+1);
976 for (p = 0; p < _PROP_MAX; p++) {
979 if (isempty(data[p]))
982 if (asprintf(&t, "%s=%s", names[p], data[p]) < 0) {
990 r = bus_property_append_strv(i, property, (void*) l);
996 static DBusHandlerResult locale_message_handler(
997 DBusConnection *connection,
998 DBusMessage *message,
1001 const BusProperty properties[] = {
1002 { "org.freedesktop.locale1", "Locale", append_locale, "as", NULL },
1003 { "org.freedesktop.locale1", "X11Layout", bus_property_append_string, "s", x11_layout },
1004 { "org.freedesktop.locale1", "X11Model", bus_property_append_string, "s", x11_model },
1005 { "org.freedesktop.locale1", "X11Variant", bus_property_append_string, "s", x11_variant },
1006 { "org.freedesktop.locale1", "X11Options", bus_property_append_string, "s", x11_options },
1007 { "org.freedesktop.locale1", "VConsoleKeymap", bus_property_append_string, "s", vc_keymap },
1008 { "org.freedesktop.locale1", "VConsoleKeymapToggle", bus_property_append_string, "s", vc_keymap_toggle },
1009 { NULL, NULL, NULL, NULL, NULL }
1012 DBusMessage *reply = NULL, *changed = NULL;
1019 dbus_error_init(&error);
1021 if (dbus_message_is_method_call(message, "org.freedesktop.locale1", "SetLocale")) {
1022 char **l = NULL, **i;
1023 dbus_bool_t interactive;
1024 DBusMessageIter iter;
1025 bool modified = false;
1026 bool passed[_PROP_MAX];
1029 if (!dbus_message_iter_init(message, &iter))
1030 return bus_send_error_reply(connection, message, NULL, -EINVAL);
1032 r = bus_parse_strv_iter(&iter, &l);
1037 return bus_send_error_reply(connection, message, NULL, r);
1040 if (!dbus_message_iter_next(&iter) ||
1041 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_BOOLEAN) {
1043 return bus_send_error_reply(connection, message, NULL, -EINVAL);
1046 dbus_message_iter_get_basic(&iter, &interactive);
1050 /* Check whether a variable changed and if so valid */
1051 STRV_FOREACH(i, l) {
1054 for (p = 0; p < _PROP_MAX; p++) {
1057 k = strlen(names[p]);
1058 if (startswith(*i, names[p]) && (*i)[k] == '=') {
1062 if (!streq_ptr(*i + k + 1, data[p]))
1071 return bus_send_error_reply(connection, message, NULL, -EINVAL);
1075 /* Check whether a variable is unset */
1077 for (p = 0; p < _PROP_MAX; p++)
1078 if (!isempty(data[p]) && !passed[p]) {
1086 r = verify_polkit(connection, message, "org.freedesktop.locale1.set-locale", interactive, &error);
1089 return bus_send_error_reply(connection, message, &error, r);
1092 STRV_FOREACH(i, l) {
1093 for (p = 0; p < _PROP_MAX; p++) {
1096 k = strlen(names[p]);
1097 if (startswith(*i, names[p]) && (*i)[k] == '=') {
1100 t = strdup(*i + k + 1);
1116 for (p = 0; p < _PROP_MAX; p++) {
1126 r = write_data_locale();
1128 log_error("Failed to set locale: %s", strerror(-r));
1129 return bus_send_error_reply(connection, message, NULL, r);
1132 push_data(connection);
1134 log_info("Changed locale information.");
1136 changed = bus_properties_changed_new(
1137 "/org/freedesktop/locale1",
1138 "org.freedesktop.locale1",
1143 } else if (dbus_message_is_method_call(message, "org.freedesktop.locale1", "SetVConsoleKeyboard")) {
1145 const char *keymap, *keymap_toggle;
1146 dbus_bool_t convert, interactive;
1148 if (!dbus_message_get_args(
1151 DBUS_TYPE_STRING, &keymap,
1152 DBUS_TYPE_STRING, &keymap_toggle,
1153 DBUS_TYPE_BOOLEAN, &convert,
1154 DBUS_TYPE_BOOLEAN, &interactive,
1156 return bus_send_error_reply(connection, message, &error, -EINVAL);
1158 if (isempty(keymap))
1161 if (isempty(keymap_toggle))
1162 keymap_toggle = NULL;
1164 if (!streq_ptr(keymap, vc_keymap) ||
1165 !streq_ptr(keymap_toggle, vc_keymap_toggle)) {
1167 r = verify_polkit(connection, message, "org.freedesktop.locale1.set-keyboard", interactive, &error);
1169 return bus_send_error_reply(connection, message, &error, r);
1171 if (free_and_set(&vc_keymap, keymap) < 0 ||
1172 free_and_set(&vc_keymap_toggle, keymap_toggle) < 0)
1175 r = write_data_vconsole();
1177 log_error("Failed to set virtual console keymap: %s", strerror(-r));
1178 return bus_send_error_reply(connection, message, NULL, r);
1181 log_info("Changed virtual console keymap to '%s'", strempty(vc_keymap));
1183 r = load_vconsole_keymap(connection, NULL);
1185 log_error("Failed to request keymap reload: %s", strerror(-r));
1187 changed = bus_properties_changed_new(
1188 "/org/freedesktop/locale1",
1189 "org.freedesktop.locale1",
1191 "VConsoleKeymapToggle\0");
1196 r = convert_vconsole_to_x11(connection);
1199 log_error("Failed to convert keymap data: %s", strerror(-r));
1203 } else if (dbus_message_is_method_call(message, "org.freedesktop.locale1", "SetX11Keyboard")) {
1205 const char *layout, *model, *variant, *options;
1206 dbus_bool_t convert, interactive;
1208 if (!dbus_message_get_args(
1211 DBUS_TYPE_STRING, &layout,
1212 DBUS_TYPE_STRING, &model,
1213 DBUS_TYPE_STRING, &variant,
1214 DBUS_TYPE_STRING, &options,
1215 DBUS_TYPE_BOOLEAN, &convert,
1216 DBUS_TYPE_BOOLEAN, &interactive,
1218 return bus_send_error_reply(connection, message, &error, -EINVAL);
1220 if (isempty(layout))
1226 if (isempty(variant))
1229 if (isempty(options))
1232 if (!streq_ptr(layout, x11_layout) ||
1233 !streq_ptr(model, x11_model) ||
1234 !streq_ptr(variant, x11_variant) ||
1235 !streq_ptr(options, x11_options)) {
1237 r = verify_polkit(connection, message, "org.freedesktop.locale1.set-keyboard", interactive, &error);
1239 return bus_send_error_reply(connection, message, &error, r);
1241 if (free_and_set(&x11_layout, layout) < 0 ||
1242 free_and_set(&x11_model, model) < 0 ||
1243 free_and_set(&x11_variant, variant) < 0 ||
1244 free_and_set(&x11_options, options) < 0)
1247 r = write_data_x11();
1249 log_error("Failed to set X11 keyboard layout: %s", strerror(-r));
1250 return bus_send_error_reply(connection, message, NULL, r);
1253 log_info("Changed X11 keyboard layout to '%s'", strempty(x11_layout));
1255 changed = bus_properties_changed_new(
1256 "/org/freedesktop/locale1",
1257 "org.freedesktop.locale1",
1266 r = convert_x11_to_vconsole(connection);
1269 log_error("Failed to convert keymap data: %s", strerror(-r));
1273 return bus_default_message_handler(connection, message, INTROSPECTION, INTERFACES_LIST, properties);
1276 if (!(reply = dbus_message_new_method_return(message)))
1279 if (!dbus_connection_send(connection, reply, NULL))
1282 dbus_message_unref(reply);
1287 if (!dbus_connection_send(connection, changed, NULL))
1290 dbus_message_unref(changed);
1293 return DBUS_HANDLER_RESULT_HANDLED;
1297 dbus_message_unref(reply);
1300 dbus_message_unref(changed);
1302 dbus_error_free(&error);
1304 return DBUS_HANDLER_RESULT_NEED_MEMORY;
1307 static int connect_bus(DBusConnection **_bus) {
1308 static const DBusObjectPathVTable locale_vtable = {
1309 .message_function = locale_message_handler
1312 DBusConnection *bus = NULL;
1317 dbus_error_init(&error);
1319 bus = dbus_bus_get_private(DBUS_BUS_SYSTEM, &error);
1321 log_error("Failed to get system D-Bus connection: %s", bus_error_message(&error));
1326 dbus_connection_set_exit_on_disconnect(bus, FALSE);
1328 if (!dbus_connection_register_object_path(bus, "/org/freedesktop/locale1", &locale_vtable, NULL) ||
1329 !dbus_connection_add_filter(bus, bus_exit_idle_filter, &remain_until, NULL)) {
1330 log_error("Not enough memory");
1335 r = dbus_bus_request_name(bus, "org.freedesktop.locale1", DBUS_NAME_FLAG_DO_NOT_QUEUE, &error);
1336 if (dbus_error_is_set(&error)) {
1337 log_error("Failed to register name on bus: %s", bus_error_message(&error));
1342 if (r != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) {
1343 log_error("Failed to acquire name.");
1354 dbus_connection_close(bus);
1355 dbus_connection_unref(bus);
1357 dbus_error_free(&error);
1362 int main(int argc, char *argv[]) {
1364 DBusConnection *bus = NULL;
1365 bool exiting = false;
1367 log_set_target(LOG_TARGET_AUTO);
1368 log_parse_environment();
1373 if (argc == 2 && streq(argv[1], "--introspect")) {
1374 fputs(DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE
1375 "<node>\n", stdout);
1376 fputs(locale_interface, stdout);
1377 fputs("</node>\n", stdout);
1382 log_error("This program takes no arguments.");
1389 log_error("Failed to read locale data: %s", strerror(-r));
1393 r = connect_bus(&bus);
1397 remain_until = now(CLOCK_MONOTONIC) + DEFAULT_EXIT_USEC;
1400 if (!dbus_connection_read_write_dispatch(bus, exiting ? -1 : (int) (DEFAULT_EXIT_USEC/USEC_PER_MSEC)))
1403 if (!exiting && remain_until < now(CLOCK_MONOTONIC)) {
1405 bus_async_unregister_and_exit(bus, "org.freedesktop.locale1");
1415 dbus_connection_flush(bus);
1416 dbus_connection_close(bus);
1417 dbus_connection_unref(bus);
1420 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;