diff options
author | Adrian Szyndela <adrian.s@samsung.com> | 2020-03-26 16:27:17 +0100 |
---|---|---|
committer | Adrian Szyndela <adrian.s@samsung.com> | 2020-03-26 16:27:17 +0100 |
commit | a38e2bbf80ed9d3da6f238ee87d2348b61a6e372 (patch) | |
tree | 13b15d6a3f58673b75c99355dda93e1ab8fe6f4b /src/locale | |
parent | 33b11b26145b0ab55836cda8b6492a360756febb (diff) | |
parent | de7436b02badc82200dc127ff190b8155769b8e7 (diff) | |
download | systemd-a38e2bbf80ed9d3da6f238ee87d2348b61a6e372.tar.gz systemd-a38e2bbf80ed9d3da6f238ee87d2348b61a6e372.tar.bz2 systemd-a38e2bbf80ed9d3da6f238ee87d2348b61a6e372.zip |
Merge v239 into tizen
systemd 239
Diffstat (limited to 'src/locale')
-rw-r--r-- | src/locale/keymap-util.c | 241 | ||||
-rw-r--r-- | src/locale/keymap-util.h | 33 | ||||
-rw-r--r-- | src/locale/localectl.c | 221 | ||||
-rw-r--r-- | src/locale/localed.c | 527 | ||||
-rw-r--r-- | src/locale/meson.build | 15 | ||||
-rw-r--r-- | src/locale/test-keymap-util.c | 18 |
6 files changed, 504 insertions, 551 deletions
diff --git a/src/locale/keymap-util.c b/src/locale/keymap-util.c index 2d788106bb..598b931d03 100644 --- a/src/locale/keymap-util.c +++ b/src/locale/keymap-util.c @@ -1,23 +1,4 @@ /* SPDX-License-Identifier: LGPL-2.1+ */ -/*** - This file is part of systemd. - - Copyright 2011 Lennart Poettering - Copyright 2013 Kay Sievers - - systemd is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation; either version 2.1 of the License, or - (at your option) any later version. - - systemd is distributed in the hope that it will be useful, but - WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public License - along with systemd; If not, see <http://www.gnu.org/licenses/>. -***/ #include <errno.h> #include <stdio_ext.h> @@ -93,39 +74,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("/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 == -ENOENT) { + 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; + + 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; @@ -137,41 +143,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("/etc/vconsole.conf", NEWLINE, + 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; @@ -223,26 +274,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; @@ -251,10 +289,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; @@ -263,14 +299,14 @@ int locale_write_data(Context *c, char ***settings) { if (!u) return -ENOMEM; - strv_free(l); - l = u; + strv_free_and_replace(l, u); } if (strv_isempty(l)) { if (unlink("/etc/locale.conf") < 0) return errno == ENOENT ? 0 : -errno; + c->locale_mtime = USEC_INFINITY; return 0; } @@ -278,14 +314,18 @@ int locale_write_data(Context *c, char ***settings) { if (r < 0) return r; - *settings = l; - l = NULL; + *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) @@ -305,8 +345,7 @@ int vconsole_write_data(Context *c) { if (!u) return -ENOMEM; - strv_free(l); - l = u; + strv_free_and_replace(l, u); } if (isempty(c->vc_keymap_toggle)) @@ -323,23 +362,31 @@ int vconsole_write_data(Context *c) { if (!u) return -ENOMEM; - strv_free(l); - l = u; + strv_free_and_replace(l, u); } if (strv_isempty(l)) { 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) && @@ -350,6 +397,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; } @@ -392,6 +440,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: @@ -539,8 +590,7 @@ int find_converted_keymap(const char *x11_layout, const char *x11_variant, char log_debug("Found converted keymap %s at %s", n, uncompressed ? p : pz); - *new_keymap = n; - n = NULL; + *new_keymap = TAKE_PTR(n); return 1; } } @@ -548,9 +598,10 @@ int find_converted_keymap(const char *x11_layout, const char *x11_variant, char return 0; } -int find_legacy_keymap(Context *c, char **new_keymap) { +int find_legacy_keymap(Context *c, char **ret) { const char *map; _cleanup_fclose_ FILE *f = NULL; + _cleanup_free_ char *new_keymap = NULL; unsigned n = 0; unsigned best_matching = 0; int r; @@ -615,7 +666,7 @@ int find_legacy_keymap(Context *c, char **new_keymap) { if (matching > best_matching) { best_matching = matching; - r = free_and_strdup(new_keymap, a[0]); + r = free_and_strdup(&new_keymap, a[0]); if (r < 0) return r; } @@ -635,13 +686,12 @@ int find_legacy_keymap(Context *c, char **new_keymap) { r = find_converted_keymap(l, v, &converted); if (r < 0) return r; - if (r > 0) { - free(*new_keymap); - *new_keymap = converted; - } + if (r > 0) + free_and_replace(new_keymap, converted); } - return (bool) *new_keymap; + *ret = TAKE_PTR(new_keymap); + return (bool) *ret; } int find_language_fallback(const char *lang, char **language) { @@ -668,8 +718,7 @@ int find_language_fallback(const char *lang, char **language) { if (streq(lang, a[0])) { assert(strv_length(a) == 2); - *language = a[1]; - a[1] = NULL; + *language = TAKE_PTR(a[1]); return 1; } } @@ -687,7 +736,7 @@ int x11_convert_to_vconsole(Context *c) { context_free_vconsole(c); } else { - char *new_keymap = NULL; + _cleanup_free_ char *new_keymap = NULL; int r; r = find_converted_keymap(c->x11_layout, c->x11_variant, &new_keymap); @@ -706,12 +755,10 @@ int x11_convert_to_vconsole(Context *c) { c->x11_layout); if (!streq_ptr(c->vc_keymap, new_keymap)) { - free(c->vc_keymap); - c->vc_keymap = new_keymap; + free_and_replace(c->vc_keymap, new_keymap); c->vc_keymap_toggle = mfree(c->vc_keymap_toggle); modified = true; - } else - free(new_keymap); + } } if (modified) diff --git a/src/locale/keymap-util.h b/src/locale/keymap-util.h index 371bbf2622..c55c9f92a6 100644 --- a/src/locale/keymap-util.h +++ b/src/locale/keymap-util.h @@ -1,34 +1,24 @@ /* SPDX-License-Identifier: LGPL-2.1+ */ -/*** - This file is part of systemd. - Copyright 2011 Lennart Poettering - Copyright 2013 Kay Sievers - - systemd is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation; either version 2.1 of the License, or - (at your option) any later version. - - systemd is distributed in the hope that it will be useful, but - WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public License - along with systemd; If not, see <http://www.gnu.org/licenses/>. -***/ +#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; @@ -37,11 +27,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); diff --git a/src/locale/localectl.c b/src/locale/localectl.c index af39e431f5..b3ad2820d9 100644 --- a/src/locale/localectl.c +++ b/src/locale/localectl.c @@ -1,23 +1,4 @@ /* SPDX-License-Identifier: LGPL-2.1+ */ -/*** - This file is part of systemd. - - Copyright 2012 Lennart Poettering - Copyright 2013 Kay Sievers - - systemd is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation; either version 2.1 of the License, or - (at your option) any later version. - - systemd is distributed in the hope that it will be useful, but - WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public License - along with systemd; If not, see <http://www.gnu.org/licenses/>. -***/ #include <ftw.h> #include <getopt.h> @@ -39,6 +20,7 @@ #include "spawn-polkit-agent.h" #include "strv.h" #include "util.h" +#include "verbs.h" #include "virt.h" static bool arg_no_pager = false; @@ -49,23 +31,17 @@ static bool arg_convert = true; typedef struct StatusInfo { char **locale; - char *vconsole_keymap; - char *vconsole_keymap_toggle; - char *x11_layout; - char *x11_model; - char *x11_variant; - char *x11_options; + const char *vconsole_keymap; + const char *vconsole_keymap_toggle; + const char *x11_layout; + const char *x11_model; + const char *x11_variant; + const char *x11_options; } StatusInfo; static void status_info_clear(StatusInfo *info) { if (info) { strv_free(info->locale); - free(info->vconsole_keymap); - free(info->vconsole_keymap_toggle); - free(info->x11_layout); - free(info->x11_model); - free(info->x11_variant); - free(info->x11_options); zero(*info); } } @@ -79,7 +55,7 @@ static void print_overridden_variables(void) { if (detect_container() > 0 || arg_host) return; - r = parse_env_file("/proc/cmdline", WHITESPACE, + r = parse_env_file(NULL, "/proc/cmdline", WHITESPACE, "locale.LANG", &variables[VARIABLE_LANG], "locale.LANGUAGE", &variables[VARIABLE_LANGUAGE], "locale.LC_CTYPE", &variables[VARIABLE_LC_CTYPE], @@ -142,11 +118,10 @@ static void print_status_info(StatusInfo *i) { printf(" X11 Options: %s\n", i->x11_options); } -static int show_status(sd_bus *bus, char **args, unsigned n) { +static int show_status(int argc, char **argv, void *userdata) { _cleanup_(status_info_clear) StatusInfo info = {}; static const struct bus_properties_map map[] = { { "VConsoleKeymap", "s", NULL, offsetof(StatusInfo, vconsole_keymap) }, - { "VConsoleKeymap", "s", NULL, offsetof(StatusInfo, vconsole_keymap) }, { "VConsoleKeymapToggle", "s", NULL, offsetof(StatusInfo, vconsole_keymap_toggle) }, { "X11Layout", "s", NULL, offsetof(StatusInfo, x11_layout) }, { "X11Model", "s", NULL, offsetof(StatusInfo, x11_model) }, @@ -157,6 +132,8 @@ static int show_status(sd_bus *bus, char **args, unsigned n) { }; _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; + _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; + sd_bus *bus = userdata; int r; assert(bus); @@ -165,7 +142,9 @@ static int show_status(sd_bus *bus, char **args, unsigned n) { "org.freedesktop.locale1", "/org/freedesktop/locale1", map, + 0, &error, + &m, &info); if (r < 0) return log_error_errno(r, "Could not get properties: %s", bus_error_message(&error, r)); @@ -176,13 +155,13 @@ static int show_status(sd_bus *bus, char **args, unsigned n) { return r; } -static int set_locale(sd_bus *bus, char **args, unsigned n) { +static int set_locale(int argc, char **argv, void *userdata) { _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; + sd_bus *bus = userdata; int r; assert(bus); - assert(args); polkit_agent_open_if_enabled(arg_transport, arg_ask_password); @@ -196,7 +175,7 @@ static int set_locale(sd_bus *bus, char **args, unsigned n) { if (r < 0) return bus_log_create_error(r); - r = sd_bus_message_append_strv(m, args + 1); + r = sd_bus_message_append_strv(m, argv + 1); if (r < 0) return bus_log_create_error(r); @@ -213,39 +192,32 @@ static int set_locale(sd_bus *bus, char **args, unsigned n) { return 0; } -static int list_locales(sd_bus *bus, char **args, unsigned n) { +static int list_locales(int argc, char **argv, void *userdata) { _cleanup_strv_free_ char **l = NULL; int r; - assert(args); - r = get_locales(&l); if (r < 0) return log_error_errno(r, "Failed to read list of locales: %m"); - pager_open(arg_no_pager, false); + (void) pager_open(arg_no_pager, false); strv_print(l); return 0; } -static int set_vconsole_keymap(sd_bus *bus, char **args, unsigned n) { +static int set_vconsole_keymap(int argc, char **argv, void *userdata) { _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; const char *map, *toggle_map; + sd_bus *bus = userdata; int r; assert(bus); - assert(args); - - if (n > 3) { - log_error("Too many arguments."); - return -EINVAL; - } polkit_agent_open_if_enabled(arg_transport, arg_ask_password); - map = args[1]; - toggle_map = n > 2 ? args[2] : ""; + map = argv[1]; + toggle_map = argc > 2 ? argv[2] : ""; r = sd_bus_call_method( bus, @@ -262,42 +234,33 @@ static int set_vconsole_keymap(sd_bus *bus, char **args, unsigned n) { return r; } -static int list_vconsole_keymaps(sd_bus *bus, char **args, unsigned n) { +static int list_vconsole_keymaps(int argc, char **argv, void *userdata) { _cleanup_strv_free_ char **l = NULL; int r; - assert(args); - r = get_keymaps(&l); if (r < 0) return log_error_errno(r, "Failed to read list of keymaps: %m"); - pager_open(arg_no_pager, false); + (void) pager_open(arg_no_pager, false); strv_print(l); return 0; } -static int set_x11_keymap(sd_bus *bus, char **args, unsigned n) { +static int set_x11_keymap(int argc, char **argv, void *userdata) { _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; const char *layout, *model, *variant, *options; + sd_bus *bus = userdata; int r; - assert(bus); - assert(args); - - if (n > 5) { - log_error("Too many arguments."); - return -EINVAL; - } - polkit_agent_open_if_enabled(arg_transport, arg_ask_password); - layout = args[1]; - model = n > 2 ? args[2] : ""; - variant = n > 3 ? args[3] : ""; - options = n > 4 ? args[4] : ""; + layout = argv[1]; + model = argc > 2 ? argv[2] : ""; + variant = argc > 3 ? argv[3] : ""; + options = argc > 4 ? argv[4] : ""; r = sd_bus_call_method( bus, @@ -315,7 +278,7 @@ static int set_x11_keymap(sd_bus *bus, char **args, unsigned n) { return r; } -static int list_x11_keymaps(sd_bus *bus, char **args, unsigned n) { +static int list_x11_keymaps(int argc, char **argv, void *userdata) { _cleanup_fclose_ FILE *f = NULL; _cleanup_strv_free_ char **list = NULL; char line[LINE_MAX]; @@ -328,22 +291,17 @@ static int list_x11_keymaps(sd_bus *bus, char **args, unsigned n) { } state = NONE, look_for; int r; - if (n > 2) { - log_error("Too many arguments."); - return -EINVAL; - } - f = fopen("/usr/share/X11/xkb/rules/base.lst", "re"); if (!f) return log_error_errno(errno, "Failed to open keyboard mapping list. %m"); - if (streq(args[0], "list-x11-keymap-models")) + if (streq(argv[0], "list-x11-keymap-models")) look_for = MODELS; - else if (streq(args[0], "list-x11-keymap-layouts")) + else if (streq(argv[0], "list-x11-keymap-layouts")) look_for = LAYOUTS; - else if (streq(args[0], "list-x11-keymap-variants")) + else if (streq(argv[0], "list-x11-keymap-variants")) look_for = VARIANTS; - else if (streq(args[0], "list-x11-keymap-options")) + else if (streq(argv[0], "list-x11-keymap-options")) look_for = OPTIONS; else assert_not_reached("Wrong parameter"); @@ -376,7 +334,7 @@ static int list_x11_keymaps(sd_bus *bus, char **args, unsigned n) { w = l + strcspn(l, WHITESPACE); - if (n > 1) { + if (argc > 1) { char *e; if (*w == 0) @@ -392,7 +350,7 @@ static int list_x11_keymaps(sd_bus *bus, char **args, unsigned n) { *e = 0; - if (!streq(w, args[1])) + if (!streq(w, argv[1])) continue; } else *w = 0; @@ -410,13 +368,13 @@ static int list_x11_keymaps(sd_bus *bus, char **args, unsigned n) { strv_sort(list); strv_uniq(list); - pager_open(arg_no_pager, false); + (void) pager_open(arg_no_pager, false); strv_print(list); return 0; } -static void help(void) { +static int help(void) { printf("%s [OPTIONS...] COMMAND ...\n\n" "Query or change system locale and keyboard settings.\n\n" " -h --help Show this help\n" @@ -440,6 +398,12 @@ static void help(void) { " Show known X11 keyboard mapping variants\n" " list-x11-keymap-options Show known X11 keyboard mapping options\n" , program_invocation_short_name); + + return 0; +} + +static int verb_help(int argc, char **argv, void *userdata) { + return help(); } static int parse_argv(int argc, char *argv[]) { @@ -472,8 +436,7 @@ static int parse_argv(int argc, char *argv[]) { switch (c) { case 'h': - help(); - return 0; + return help(); case ARG_VERSION: return version(); @@ -512,86 +475,22 @@ static int parse_argv(int argc, char *argv[]) { static int localectl_main(sd_bus *bus, int argc, char *argv[]) { - static const struct { - const char* verb; - const enum { - MORE, - LESS, - EQUAL - } argc_cmp; - const int argc; - int (* const dispatch)(sd_bus *bus, char **args, unsigned n); - } verbs[] = { - { "status", LESS, 1, show_status }, - { "set-locale", MORE, 2, set_locale }, - { "list-locales", EQUAL, 1, list_locales }, - { "set-keymap", MORE, 2, set_vconsole_keymap }, - { "list-keymaps", EQUAL, 1, list_vconsole_keymaps }, - { "set-x11-keymap", MORE, 2, set_x11_keymap }, - { "list-x11-keymap-models", EQUAL, 1, list_x11_keymaps }, - { "list-x11-keymap-layouts", EQUAL, 1, list_x11_keymaps }, - { "list-x11-keymap-variants", LESS, 2, list_x11_keymaps }, - { "list-x11-keymap-options", EQUAL, 1, list_x11_keymaps }, + static const Verb verbs[] = { + { "status", VERB_ANY, 1, VERB_DEFAULT, show_status }, + { "set-locale", 2, VERB_ANY, 0, set_locale }, + { "list-locales", VERB_ANY, 1, 0, list_locales }, + { "set-keymap", 2, 3, 0, set_vconsole_keymap }, + { "list-keymaps", VERB_ANY, 1, 0, list_vconsole_keymaps }, + { "set-x11-keymap", 2, 5, 0, set_x11_keymap }, + { "list-x11-keymap-models", VERB_ANY, 1, 0, list_x11_keymaps }, + { "list-x11-keymap-layouts", VERB_ANY, 1, 0, list_x11_keymaps }, + { "list-x11-keymap-variants", VERB_ANY, 2, 0, list_x11_keymaps }, + { "list-x11-keymap-options", VERB_ANY, 1, 0, list_x11_keymaps }, + { "help", VERB_ANY, VERB_ANY, 0, verb_help }, /* Not documented, but supported since it is created. */ + {} }; - int left; - unsigned i; - - assert(argc >= 0); - assert(argv); - - left = argc - optind; - - if (left <= 0) - /* Special rule: no arguments means "status" */ - i = 0; - else { - if (streq(argv[optind], "help")) { - help(); - return 0; - } - - for (i = 0; i < ELEMENTSOF(verbs); i++) - if (streq(argv[optind], verbs[i].verb)) - break; - - if (i >= ELEMENTSOF(verbs)) { - log_error("Unknown operation %s", argv[optind]); - return -EINVAL; - } - } - - switch (verbs[i].argc_cmp) { - - case EQUAL: - if (left != verbs[i].argc) { - log_error("Invalid number of arguments."); - return -EINVAL; - } - - break; - - case MORE: - if (left < verbs[i].argc) { - log_error("Too few arguments."); - return -EINVAL; - } - - break; - - case LESS: - if (left > verbs[i].argc) { - log_error("Too many arguments."); - return -EINVAL; - } - - break; - - default: - assert_not_reached("Unknown comparison operator."); - } - - return verbs[i].dispatch(bus, argv + optind, left); + return dispatch_verb(argc, argv, verbs, bus); } int main(int argc, char*argv[]) { diff --git a/src/locale/localed.c b/src/locale/localed.c index 02f5e8c656..b8f95b69a6 100644 --- a/src/locale/localed.c +++ b/src/locale/localed.c @@ -1,23 +1,4 @@ /* SPDX-License-Identifier: LGPL-2.1+ */ -/*** - This file is part of systemd. - - Copyright 2011 Lennart Poettering - Copyright 2013 Kay Sievers - - systemd is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation; either version 2.1 of the License, or - (at your option) any later version. - - systemd is distributed in the hope that it will be useful, but - WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public License - along with systemd; If not, see <http://www.gnu.org/licenses/>. -***/ #include <errno.h> #include <string.h> @@ -101,7 +82,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; } @@ -126,10 +107,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) @@ -140,7 +125,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); @@ -148,10 +133,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) @@ -162,12 +151,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( @@ -181,7 +170,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) @@ -206,16 +199,72 @@ 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] = {}, **i; + _cleanup_(locale_free) _unused_ char **dummy = new_locale; bool modified = false; - bool have[_VARIABLE_LC_MAX] = {}; - int p; - int r; + int interactive, p, r; assert(m); assert(c); @@ -228,7 +277,19 @@ 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 */ + /* If single locale without variable name is provided, then we assume it is LANG=. */ + if (strv_length(l) == 1 && !strchr(*l, '=')) { + if (!locale_is_valid(*l)) + return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid Locale data."); + + new_locale[VARIABLE_LANG] = strdup(*l); + if (!new_locale[VARIABLE_LANG]) + return -ENOMEM; + + l = strv_free(l); + } + + /* Check whether a variable is valid */ STRV_FOREACH(i, l) { bool valid = false; @@ -244,13 +305,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; } @@ -262,100 +320,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]); + + 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"); + } - if (settings) { - _cleanup_free_ char *line; + (void) locale_update_system_manager(c, sd_bus_message_get_bus(m)); - line = strv_join(settings, ", "); - log_info("Changed locale to %s.", strnull(line)); - } else - log_info("Changed locale to unset."); + if (settings) { + _cleanup_free_ char *line; - (void) sd_bus_emit_properties_changed( - sd_bus_message_get_bus(m), - "/org/freedesktop/locale1", - "org.freedesktop.locale1", - "Locale", NULL); + 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); } @@ -363,8 +403,7 @@ static int method_set_locale(sd_bus_message *m, void *userdata, sd_bus_error *er static int method_set_vc_keyboard(sd_bus_message *m, void *userdata, sd_bus_error *error) { Context *c = userdata; const char *keymap, *keymap_toggle; - int convert, interactive; - int r; + int convert, interactive, r; assert(m); assert(c); @@ -376,55 +415,61 @@ 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); - if (!streq_ptr(keymap, c->vc_keymap) || - !streq_ptr(keymap_toggle, c->vc_keymap_toggle)) { - - if ((keymap && (!filename_is_valid(keymap) || !string_is_safe(keymap))) || - (keymap_toggle && (!filename_is_valid(keymap_toggle) || !string_is_safe(keymap_toggle)))) - return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Received invalid keymap data"); - - r = bus_verify_polkit_async( - m, - CAP_SYS_ADMIN, - "org.freedesktop.locale1.set-keyboard", - 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 */ + 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 (free_and_strdup(&c->vc_keymap, keymap) < 0 || - free_and_strdup(&c->vc_keymap_toggle, keymap_toggle) < 0) - return -ENOMEM; + if (streq_ptr(keymap, c->vc_keymap) && + streq_ptr(keymap_toggle, c->vc_keymap_toggle)) + return sd_bus_reply_method_return(m, NULL); - r = vconsole_write_data(c); - if (r < 0) { - log_error_errno(r, "Failed to set virtual console keymap: %m"); - return sd_bus_error_set_errnof(error, r, "Failed to set virtual console keymap: %m"); - } + if ((keymap && (!filename_is_valid(keymap) || !string_is_safe(keymap))) || + (keymap_toggle && (!filename_is_valid(keymap_toggle) || !string_is_safe(keymap_toggle)))) + return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Received invalid keymap data"); + + r = bus_verify_polkit_async( + m, + CAP_SYS_ADMIN, + "org.freedesktop.locale1.set-keyboard", + 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 */ + + if (free_and_strdup(&c->vc_keymap, keymap) < 0 || + free_and_strdup(&c->vc_keymap_toggle, keymap_toggle) < 0) + return -ENOMEM; + + r = vconsole_write_data(c); + if (r < 0) { + log_error_errno(r, "Failed to set virtual console keymap: %m"); + return sd_bus_error_set_errnof(error, r, "Failed to set virtual console keymap: %m"); + } + + log_info("Changed virtual console keymap to '%s' toggle '%s'", + strempty(c->vc_keymap), strempty(c->vc_keymap_toggle)); - log_info("Changed virtual console keymap to '%s' toggle '%s'", - strempty(c->vc_keymap), strempty(c->vc_keymap_toggle)); + r = vconsole_reload(sd_bus_message_get_bus(m)); + if (r < 0) + log_error_errno(r, "Failed to request keymap reload: %m"); - r = vconsole_reload(sd_bus_message_get_bus(m)); + (void) sd_bus_emit_properties_changed( + sd_bus_message_get_bus(m), + "/org/freedesktop/locale1", + "org.freedesktop.locale1", + "VConsoleKeymap", "VConsoleKeymapToggle", NULL); + + if (convert) { + r = vconsole_convert_to_x11_and_emit(c, m); if (r < 0) - log_error_errno(r, "Failed to request keymap reload: %m"); - - (void) sd_bus_emit_properties_changed( - sd_bus_message_get_bus(m), - "/org/freedesktop/locale1", - "org.freedesktop.locale1", - "VConsoleKeymap", "VConsoleKeymapToggle", NULL); - - if (convert) { - r = vconsole_convert_to_x11_and_emit(c, sd_bus_message_get_bus(m)); - if (r < 0) - log_error_errno(r, "Failed to convert keymap data: %m"); - } + log_error_errno(r, "Failed to convert keymap data: %m"); } return sd_bus_reply_method_return(m, NULL); @@ -536,8 +581,7 @@ static int verify_xkb_rmlvo(const char *model, const char *layout, const char *v static int method_set_x11_keyboard(sd_bus_message *m, void *userdata, sd_bus_error *error) { Context *c = userdata; const char *layout, *model, *variant, *options; - int convert, interactive; - int r; + int convert, interactive, r; assert(m); assert(c); @@ -551,71 +595,77 @@ 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); - if (!streq_ptr(layout, c->x11_layout) || - !streq_ptr(model, c->x11_model) || - !streq_ptr(variant, c->x11_variant) || - !streq_ptr(options, c->x11_options)) { - - if ((layout && !string_is_safe(layout)) || - (model && !string_is_safe(model)) || - (variant && !string_is_safe(variant)) || - (options && !string_is_safe(options))) - return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Received invalid keyboard data"); - - r = bus_verify_polkit_async( - m, - CAP_SYS_ADMIN, - "org.freedesktop.locale1.set-keyboard", - 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 */ + 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"); + } - r = verify_xkb_rmlvo(model, layout, variant, options); - if (r < 0) { - log_error_errno(r, "Cannot compile XKB keymap for new x11 keyboard layout ('%s' / '%s' / '%s' / '%s'): %m", - strempty(model), strempty(layout), strempty(variant), strempty(options)); + if (streq_ptr(layout, c->x11_layout) && + streq_ptr(model, c->x11_model) && + streq_ptr(variant, c->x11_variant) && + streq_ptr(options, c->x11_options)) + return sd_bus_reply_method_return(m, NULL); - if (r == -EOPNOTSUPP) - return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, "Local keyboard configuration not supported on this system."); + if ((layout && !string_is_safe(layout)) || + (model && !string_is_safe(model)) || + (variant && !string_is_safe(variant)) || + (options && !string_is_safe(options))) + return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Received invalid keyboard data"); - return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Specified keymap cannot be compiled, refusing as invalid."); - } + r = verify_xkb_rmlvo(model, layout, variant, options); + if (r < 0) { + log_error_errno(r, "Cannot compile XKB keymap for new x11 keyboard layout ('%s' / '%s' / '%s' / '%s'): %m", + strempty(model), strempty(layout), strempty(variant), strempty(options)); - if (free_and_strdup(&c->x11_layout, layout) < 0 || - free_and_strdup(&c->x11_model, model) < 0 || - free_and_strdup(&c->x11_variant, variant) < 0 || - free_and_strdup(&c->x11_options, options) < 0) - return -ENOMEM; + if (r == -EOPNOTSUPP) + return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, "Local keyboard configuration not supported on this system."); - r = x11_write_data(c); - if (r < 0) { - log_error_errno(r, "Failed to set X11 keyboard layout: %m"); - return sd_bus_error_set_errnof(error, r, "Failed to set X11 keyboard layout: %m"); - } + return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Specified keymap cannot be compiled, refusing as invalid."); + } - log_info("Changed X11 keyboard layout to '%s' model '%s' variant '%s' options '%s'", - strempty(c->x11_layout), - strempty(c->x11_model), - strempty(c->x11_variant), - strempty(c->x11_options)); - - (void) sd_bus_emit_properties_changed( - sd_bus_message_get_bus(m), - "/org/freedesktop/locale1", - "org.freedesktop.locale1", - "X11Layout", "X11Model", "X11Variant", "X11Options", NULL); - - if (convert) { - r = x11_convert_to_vconsole_and_emit(c, sd_bus_message_get_bus(m)); - if (r < 0) - log_error_errno(r, "Failed to convert keymap data: %m"); - } + r = bus_verify_polkit_async( + m, + CAP_SYS_ADMIN, + "org.freedesktop.locale1.set-keyboard", + 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 */ + + if (free_and_strdup(&c->x11_layout, layout) < 0 || + free_and_strdup(&c->x11_model, model) < 0 || + free_and_strdup(&c->x11_variant, variant) < 0 || + free_and_strdup(&c->x11_options, options) < 0) + return -ENOMEM; + + r = x11_write_data(c); + if (r < 0) { + log_error_errno(r, "Failed to set X11 keyboard layout: %m"); + return sd_bus_error_set_errnof(error, r, "Failed to set X11 keyboard layout: %m"); + } + + log_info("Changed X11 keyboard layout to '%s' model '%s' variant '%s' options '%s'", + strempty(c->x11_layout), + strempty(c->x11_model), + strempty(c->x11_variant), + strempty(c->x11_options)); + + (void) sd_bus_emit_properties_changed( + sd_bus_message_get_bus(m), + "/org/freedesktop/locale1", + "org.freedesktop.locale1", + "X11Layout", "X11Model", "X11Variant", "X11Options", NULL); + + if (convert) { + r = x11_convert_to_vconsole_and_emit(c, m); + if (r < 0) + log_error_errno(r, "Failed to convert keymap data: %m"); } return sd_bus_reply_method_return(m, NULL); @@ -624,12 +674,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), @@ -660,14 +710,17 @@ static int connect_bus(Context *c, sd_event *event, sd_bus **_bus) { if (r < 0) return log_error_errno(r, "Failed to attach bus to event loop: %m"); - *_bus = bus; - bus = NULL; + *_bus = TAKE_PTR(bus); return 0; } 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; @@ -697,12 +750,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"); diff --git a/src/locale/meson.build b/src/locale/meson.build index 6b85f6bea6..e87a10ebeb 100644 --- a/src/locale/meson.build +++ b/src/locale/meson.build @@ -1,19 +1,4 @@ # SPDX-License-Identifier: LGPL-2.1+ -# -# Copyright 2017 Zbigniew Jędrzejewski-Szmek -# -# systemd is free software; you can redistribute it and/or modify it -# under the terms of the GNU Lesser General Public License as published by -# the Free Software Foundation; either version 2.1 of the License, or -# (at your option) any later version. -# -# systemd is distributed in the hope that it will be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -# Lesser General Public License for more details. -# -# You should have received a copy of the GNU Lesser General Public License -# along with systemd; If not, see <http://www.gnu.org/licenses/>. systemd_localed_sources = files(''' localed.c diff --git a/src/locale/test-keymap-util.c b/src/locale/test-keymap-util.c index fa26a0294e..e20731b253 100644 --- a/src/locale/test-keymap-util.c +++ b/src/locale/test-keymap-util.c @@ -1,22 +1,4 @@ /* SPDX-License-Identifier: LGPL-2.1+ */ -/*** - This file is part of systemd. - - Copyright 2016 Zbigniew Jędrzejewski-Szmek - - systemd is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation; either version 2.1 of the License, or - (at your option) any later version. - - systemd is distributed in the hope that it will be useful, but - WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public License - along with systemd; If not, see <http://www.gnu.org/licenses/>. -***/ #include "alloc-util.h" #include "keymap-util.h" |