1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2012 Lennart Poettering
7 Copyright 2013 Kay Sievers
9 systemd is free software; you can redistribute it and/or modify it
10 under the terms of the GNU Lesser General Public License as published by
11 the Free Software Foundation; either version 2.1 of the License, or
12 (at your option) any later version.
14 systemd is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Lesser General Public License for more details.
19 You should have received a copy of the GNU Lesser General Public License
20 along with systemd; If not, see <http://www.gnu.org/licenses/>.
35 #include "bus-error.h"
36 #include "bus-message.h"
38 #include "spawn-polkit-agent.h"
43 #include "path-util.h"
46 static bool arg_no_pager = false;
47 static bool arg_ask_password = true;
48 static BusTransport arg_transport = BUS_TRANSPORT_LOCAL;
49 static char *arg_host = NULL;
50 static bool arg_convert = true;
52 static void pager_open_if_enabled(void) {
60 static void polkit_agent_open_if_enabled(void) {
62 /* Open the polkit agent as a child process if necessary */
63 if (!arg_ask_password)
69 typedef struct StatusInfo {
71 const char *vconsole_keymap;
72 const char *vconsole_keymap_toggle;
73 const char *x11_layout;
74 const char *x11_model;
75 const char *x11_variant;
76 const char *x11_options;
79 static void print_status_info(StatusInfo *i) {
82 if (strv_isempty(i->locale))
83 puts(" System Locale: n/a\n");
87 printf(" System Locale: %s\n", i->locale[0]);
88 STRV_FOREACH(j, i->locale + 1)
92 printf(" VC Keymap: %s\n", strna(i->vconsole_keymap));
93 if (!isempty(i->vconsole_keymap_toggle))
94 printf("VC Toggle Keymap: %s\n", i->vconsole_keymap_toggle);
96 printf(" X11 Layout: %s\n", strna(i->x11_layout));
97 if (!isempty(i->x11_model))
98 printf(" X11 Model: %s\n", i->x11_model);
99 if (!isempty(i->x11_variant))
100 printf(" X11 Variant: %s\n", i->x11_variant);
101 if (!isempty(i->x11_options))
102 printf(" X11 Options: %s\n", i->x11_options);
105 static int status_read_property(const char *name, sd_bus_message *property, StatusInfo *i) {
107 const char *contents;
113 r = sd_bus_message_peek_type(property, &type, &contents);
115 log_error("Could not determine type of message: %s", strerror(-r));
121 case SD_BUS_TYPE_STRING: {
124 sd_bus_message_read_basic(property, type, &s);
128 if (streq(name, "VConsoleKeymap"))
129 i->vconsole_keymap = s;
130 else if (streq(name, "VConsoleKeymapToggle"))
131 i->vconsole_keymap_toggle = s;
132 else if (streq(name, "X11Layout"))
134 else if (streq(name, "X11Model"))
136 else if (streq(name, "X11Variant"))
138 else if (streq(name, "X11Options"))
144 case SD_BUS_TYPE_ARRAY: {
145 _cleanup_strv_free_ char **l = NULL;
147 if (!streq(contents, "s"))
150 if (!streq(name, "Locale"))
153 r = bus_message_read_strv_extend(property, &l);
157 strv_free(i->locale);
168 static int show_status(sd_bus *bus, char **args, unsigned n) {
169 _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
170 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
172 StatusInfo info = {};
176 r = sd_bus_call_method( bus,
177 "org.freedesktop.locale1",
178 "/org/freedesktop/locale1",
179 "org.freedesktop.DBus.Properties",
185 log_error("Could not get properties: %s", bus_error_message(&error, -r));
189 r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "{sv}");
193 while ((r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_DICT_ENTRY, "sv")) > 0) {
195 const char *contents;
197 r = sd_bus_message_read_basic(reply, SD_BUS_TYPE_STRING, &name);
201 r = sd_bus_message_peek_type(reply, NULL, &contents);
205 r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_VARIANT, contents);
209 r = status_read_property(name, reply, &info);
211 log_error("Failed to parse reply.");
215 r = sd_bus_message_exit_container(reply);
219 r = sd_bus_message_exit_container(reply);
224 print_status_info(&info);
227 strv_free(info.locale);
231 static int set_locale(sd_bus *bus, char **args, unsigned n) {
232 _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
233 _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
234 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
240 polkit_agent_open_if_enabled();
242 r = sd_bus_message_new_method_call(bus,
243 "org.freedesktop.locale1",
244 "/org/freedesktop/locale1",
245 "org.freedesktop.locale1",
250 r = sd_bus_message_append_strv(m, args + 1);
254 r = sd_bus_message_append(m, "b", arg_ask_password);
258 r = sd_bus_send_with_reply_and_block(bus, m, 0, &error, NULL);
260 log_error("Failed to issue method call: %s", bus_error_message(&error, -r));
267 static int add_locales_from_archive(Set *locales) {
268 /* Stolen from glibc... */
274 /* Name hash table. */
275 uint32_t namehash_offset;
276 uint32_t namehash_used;
277 uint32_t namehash_size;
279 uint32_t string_offset;
280 uint32_t string_used;
281 uint32_t string_size;
282 /* Table with locale records. */
283 uint32_t locrectab_offset;
284 uint32_t locrectab_used;
285 uint32_t locrectab_size;
286 /* MD5 sum hash table. */
287 uint32_t sumhash_offset;
288 uint32_t sumhash_used;
289 uint32_t sumhash_size;
293 /* Hash value of the name. */
295 /* Offset of the name in the string table. */
296 uint32_t name_offset;
297 /* Offset of the locale record. */
298 uint32_t locrec_offset;
301 const struct locarhead *h;
302 const struct namehashent *e;
303 const void *p = MAP_FAILED;
304 _cleanup_close_ int fd = -1;
310 fd = open("/usr/lib/locale/locale-archive", O_RDONLY|O_NOCTTY|O_CLOEXEC);
313 log_error("Failed to open locale archive: %m");
318 if (fstat(fd, &st) < 0) {
319 log_error("fstat() failed: %m");
324 if (!S_ISREG(st.st_mode)) {
325 log_error("Archive file is not regular");
330 if (st.st_size < (off_t) sizeof(struct locarhead)) {
331 log_error("Archive has invalid size");
336 p = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
337 if (p == MAP_FAILED) {
338 log_error("Failed to map archive: %m");
343 h = (const struct locarhead *) p;
344 if (h->magic != 0xde020109 ||
345 h->namehash_offset + h->namehash_size > st.st_size ||
346 h->string_offset + h->string_size > st.st_size ||
347 h->locrectab_offset + h->locrectab_size > st.st_size ||
348 h->sumhash_offset + h->sumhash_size > st.st_size) {
349 log_error("Invalid archive file.");
354 e = (const struct namehashent*) ((const uint8_t*) p + h->namehash_offset);
355 for (i = 0; i < h->namehash_size; i++) {
358 if (e[i].locrec_offset == 0)
361 if (!utf8_is_valid((char*) p + e[i].name_offset))
364 z = strdup((char*) p + e[i].name_offset);
370 r = set_consume(locales, z);
372 log_error("Failed to add locale: %s", strerror(-r));
381 munmap((void*) p, sz);
386 static int add_locales_from_libdir (Set *locales) {
387 _cleanup_closedir_ DIR *dir;
388 struct dirent *entry;
391 dir = opendir("/usr/lib/locale");
393 log_error("Failed to open locale directory: %m");
398 while ((entry = readdir(dir))) {
401 if (entry->d_type != DT_DIR)
404 if (ignore_file(entry->d_name))
407 z = strdup(entry->d_name);
411 r = set_consume(locales, z);
412 if (r < 0 && r != -EEXIST) {
413 log_error("Failed to add locale: %s", strerror(-r));
421 log_error("Failed to read locale directory: %m");
428 static int list_locales(sd_bus *bus, char **args, unsigned n) {
429 _cleanup_set_free_ Set *locales;
430 _cleanup_strv_free_ char **l = NULL;
433 locales = set_new(string_hash_func, string_compare_func);
437 r = add_locales_from_archive(locales);
438 if (r < 0 && r != -ENOENT)
441 r = add_locales_from_libdir(locales);
445 l = set_get_strv(locales);
451 pager_open_if_enabled();
458 static int set_vconsole_keymap(sd_bus *bus, char **args, unsigned n) {
459 _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
460 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
461 const char *map, *toggle_map;
468 log_error("Too many arguments.");
472 polkit_agent_open_if_enabled();
475 toggle_map = n > 2 ? args[2] : "";
477 r = sd_bus_call_method(
479 "org.freedesktop.locale1",
480 "/org/freedesktop/locale1",
481 "org.freedesktop.locale1",
482 "SetVConsoleKeyboard",
485 "ssbb", map, toggle_map, arg_convert, arg_ask_password);
487 log_error("Failed to set keymap: %s", bus_error_message(&error, -r));
492 static Set *keymaps = NULL;
496 const struct stat *sb,
498 struct FTW *ftwbuf) {
506 if (!endswith(fpath, ".map") &&
507 !endswith(fpath, ".map.gz"))
510 p = strdup(path_get_file_name(fpath));
514 e = endswith(p, ".map");
518 e = endswith(p, ".map.gz");
522 r = set_consume(keymaps, p);
523 if (r < 0 && r != -EEXIST) {
524 log_error("Can't add keymap: %s", strerror(-r));
531 static int list_vconsole_keymaps(sd_bus *bus, char **args, unsigned n) {
532 _cleanup_strv_free_ char **l = NULL;
534 keymaps = set_new(string_hash_func, string_compare_func);
538 nftw("/usr/share/keymaps/", nftw_cb, 20, FTW_MOUNT|FTW_PHYS);
539 nftw("/usr/share/kbd/keymaps/", nftw_cb, 20, FTW_MOUNT|FTW_PHYS);
540 nftw("/usr/lib/kbd/keymaps/", nftw_cb, 20, FTW_MOUNT|FTW_PHYS);
541 nftw("/lib/kbd/keymaps/", nftw_cb, 20, FTW_MOUNT|FTW_PHYS);
543 l = set_get_strv(keymaps);
545 set_free_free(keymaps);
551 if (strv_isempty(l)) {
552 log_error("Couldn't find any console keymaps.");
558 pager_open_if_enabled();
565 static int set_x11_keymap(sd_bus *bus, char **args, unsigned n) {
566 _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
567 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
568 const char *layout, *model, *variant, *options;
575 log_error("Too many arguments.");
579 polkit_agent_open_if_enabled();
582 model = n > 2 ? args[2] : "";
583 variant = n > 3 ? args[3] : "";
584 options = n > 4 ? args[4] : "";
586 r = sd_bus_call_method(
588 "org.freedesktop.locale1",
589 "/org/freedesktop/locale1",
590 "org.freedesktop.locale1",
594 "ssssbb", layout, model, variant, options,
595 arg_convert, arg_ask_password);
597 log_error("Failed to set keymap: %s", bus_error_message(&error, -r));
602 static int list_x11_keymaps(sd_bus *bus, char **args, unsigned n) {
603 _cleanup_fclose_ FILE *f = NULL;
604 _cleanup_strv_free_ char **list = NULL;
612 } state = NONE, look_for;
616 log_error("Too many arguments.");
620 f = fopen("/usr/share/X11/xkb/rules/base.lst", "re");
622 log_error("Failed to open keyboard mapping list. %m");
626 if (streq(args[0], "list-x11-keymap-models"))
628 else if (streq(args[0], "list-x11-keymap-layouts"))
630 else if (streq(args[0], "list-x11-keymap-variants"))
632 else if (streq(args[0], "list-x11-keymap-options"))
635 assert_not_reached("Wrong parameter");
637 FOREACH_LINE(line, f, break) {
646 if (startswith(l, "! model"))
648 else if (startswith(l, "! layout"))
650 else if (startswith(l, "! variant"))
652 else if (startswith(l, "! option"))
660 if (state != look_for)
663 w = l + strcspn(l, WHITESPACE);
673 w += strspn(w, WHITESPACE);
681 if (!streq(w, args[1]))
686 r = strv_extend(&list, l);
691 if (strv_isempty(list)) {
692 log_error("Couldn't find any entries.");
699 pager_open_if_enabled();
705 static int help(void) {
707 printf("%s [OPTIONS...] COMMAND ...\n\n"
708 "Query or change system locale and keyboard settings.\n\n"
709 " -h --help Show this help\n"
710 " --version Show package version\n"
711 " --no-convert Don't convert keyboard mappings\n"
712 " --no-pager Do not pipe output into a pager\n"
713 " --no-ask-password Do not prompt for password\n"
714 " -H --host=[USER@]HOST Operate on remote host\n"
715 " -M --machine=CONTAINER Operate on local container\n\n"
717 " status Show current locale settings\n"
718 " set-locale LOCALE... Set system locale\n"
719 " list-locales Show known locales\n"
720 " set-keymap MAP [MAP] Set virtual console keyboard mapping\n"
721 " list-keymaps Show known virtual console keyboard mappings\n"
722 " set-x11-keymap LAYOUT [MODEL] [VARIANT] [OPTIONS]\n"
723 " Set X11 keyboard mapping\n"
724 " list-x11-keymap-models Show known X11 keyboard mapping models\n"
725 " list-x11-keymap-layouts Show known X11 keyboard mapping layouts\n"
726 " list-x11-keymap-variants [LAYOUT]\n"
727 " Show known X11 keyboard mapping variants\n"
728 " list-x11-keymap-options Show known X11 keyboard mapping options\n",
729 program_invocation_short_name);
734 static int parse_argv(int argc, char *argv[]) {
743 static const struct option options[] = {
744 { "help", no_argument, NULL, 'h' },
745 { "version", no_argument, NULL, ARG_VERSION },
746 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
747 { "host", required_argument, NULL, 'H' },
748 { "machine", required_argument, NULL, 'M' },
749 { "no-ask-password", no_argument, NULL, ARG_NO_ASK_PASSWORD },
750 { "no-convert", no_argument, NULL, ARG_NO_CONVERT },
759 while ((c = getopt_long(argc, argv, "hH:M:", options, NULL)) >= 0) {
768 puts(PACKAGE_STRING);
769 puts(SYSTEMD_FEATURES);
780 case ARG_NO_ASK_PASSWORD:
781 arg_ask_password = false;
785 arg_transport = BUS_TRANSPORT_REMOTE;
790 arg_transport = BUS_TRANSPORT_CONTAINER;
798 log_error("Unknown option code %c", c);
806 static int localectl_main(sd_bus *bus, int argc, char *argv[]) {
808 static const struct {
816 int (* const dispatch)(sd_bus *bus, char **args, unsigned n);
818 { "status", LESS, 1, show_status },
819 { "set-locale", MORE, 2, set_locale },
820 { "list-locales", EQUAL, 1, list_locales },
821 { "set-keymap", MORE, 2, set_vconsole_keymap },
822 { "list-keymaps", EQUAL, 1, list_vconsole_keymaps },
823 { "set-x11-keymap", MORE, 2, set_x11_keymap },
824 { "list-x11-keymap-models", EQUAL, 1, list_x11_keymaps },
825 { "list-x11-keymap-layouts", EQUAL, 1, list_x11_keymaps },
826 { "list-x11-keymap-variants", LESS, 2, list_x11_keymaps },
827 { "list-x11-keymap-options", EQUAL, 1, list_x11_keymaps },
836 left = argc - optind;
839 /* Special rule: no arguments means "status" */
842 if (streq(argv[optind], "help")) {
847 for (i = 0; i < ELEMENTSOF(verbs); i++)
848 if (streq(argv[optind], verbs[i].verb))
851 if (i >= ELEMENTSOF(verbs)) {
852 log_error("Unknown operation %s", argv[optind]);
857 switch (verbs[i].argc_cmp) {
860 if (left != verbs[i].argc) {
861 log_error("Invalid number of arguments.");
868 if (left < verbs[i].argc) {
869 log_error("Too few arguments.");
876 if (left > verbs[i].argc) {
877 log_error("Too many arguments.");
884 assert_not_reached("Unknown comparison operator.");
887 return verbs[i].dispatch(bus, argv + optind, left);
890 int main(int argc, char*argv[]) {
891 int r, ret = EXIT_FAILURE;
892 _cleanup_bus_unref_ sd_bus *bus = NULL;
894 setlocale(LC_ALL, "");
895 log_parse_environment();
898 r = parse_argv(argc, argv);
906 r = bus_open_transport(arg_transport, arg_host, false, &bus);
908 log_error("Failed to create bus connection: %s", strerror(-r));
913 r = localectl_main(bus, argc, argv);
914 ret = r < 0 ? EXIT_FAILURE : r;