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);
261 log_error("Failed to issue method call: %s", strerror(-r));
268 static int add_locales_from_archive(Set *locales) {
269 /* Stolen from glibc... */
275 /* Name hash table. */
276 uint32_t namehash_offset;
277 uint32_t namehash_used;
278 uint32_t namehash_size;
280 uint32_t string_offset;
281 uint32_t string_used;
282 uint32_t string_size;
283 /* Table with locale records. */
284 uint32_t locrectab_offset;
285 uint32_t locrectab_used;
286 uint32_t locrectab_size;
287 /* MD5 sum hash table. */
288 uint32_t sumhash_offset;
289 uint32_t sumhash_used;
290 uint32_t sumhash_size;
294 /* Hash value of the name. */
296 /* Offset of the name in the string table. */
297 uint32_t name_offset;
298 /* Offset of the locale record. */
299 uint32_t locrec_offset;
302 const struct locarhead *h;
303 const struct namehashent *e;
304 const void *p = MAP_FAILED;
305 _cleanup_close_ int fd = -1;
311 fd = open("/usr/lib/locale/locale-archive", O_RDONLY|O_NOCTTY|O_CLOEXEC);
314 log_error("Failed to open locale archive: %m");
319 if (fstat(fd, &st) < 0) {
320 log_error("fstat() failed: %m");
325 if (!S_ISREG(st.st_mode)) {
326 log_error("Archive file is not regular");
331 if (st.st_size < (off_t) sizeof(struct locarhead)) {
332 log_error("Archive has invalid size");
337 p = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
338 if (p == MAP_FAILED) {
339 log_error("Failed to map archive: %m");
344 h = (const struct locarhead *) p;
345 if (h->magic != 0xde020109 ||
346 h->namehash_offset + h->namehash_size > st.st_size ||
347 h->string_offset + h->string_size > st.st_size ||
348 h->locrectab_offset + h->locrectab_size > st.st_size ||
349 h->sumhash_offset + h->sumhash_size > st.st_size) {
350 log_error("Invalid archive file.");
355 e = (const struct namehashent*) ((const uint8_t*) p + h->namehash_offset);
356 for (i = 0; i < h->namehash_size; i++) {
359 if (e[i].locrec_offset == 0)
362 if (!utf8_is_valid((char*) p + e[i].name_offset))
365 z = strdup((char*) p + e[i].name_offset);
371 r = set_consume(locales, z);
373 log_error("Failed to add locale: %s", strerror(-r));
382 munmap((void*) p, sz);
387 static int add_locales_from_libdir (Set *locales) {
388 _cleanup_closedir_ DIR *dir;
389 struct dirent *entry;
392 dir = opendir("/usr/lib/locale");
394 log_error("Failed to open locale directory: %m");
399 while ((entry = readdir(dir))) {
402 if (entry->d_type != DT_DIR)
405 if (ignore_file(entry->d_name))
408 z = strdup(entry->d_name);
412 r = set_consume(locales, z);
413 if (r < 0 && r != -EEXIST) {
414 log_error("Failed to add locale: %s", strerror(-r));
422 log_error("Failed to read locale directory: %m");
429 static int list_locales(sd_bus *bus, char **args, unsigned n) {
430 _cleanup_set_free_ Set *locales;
431 _cleanup_strv_free_ char **l = NULL;
434 locales = set_new(string_hash_func, string_compare_func);
438 r = add_locales_from_archive(locales);
439 if (r < 0 && r != -ENOENT)
442 r = add_locales_from_libdir(locales);
446 l = set_get_strv(locales);
452 pager_open_if_enabled();
459 static int set_vconsole_keymap(sd_bus *bus, char **args, unsigned n) {
460 _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
461 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
462 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 return sd_bus_call_method(bus,
478 "org.freedesktop.locale1",
479 "/org/freedesktop/locale1",
480 "org.freedesktop.locale1",
481 "SetVConsoleKeyboard",
484 "ssbb", map, toggle_map, arg_convert, arg_ask_password);
487 static Set *keymaps = NULL;
491 const struct stat *sb,
493 struct FTW *ftwbuf) {
501 if (!endswith(fpath, ".map") &&
502 !endswith(fpath, ".map.gz"))
505 p = strdup(path_get_file_name(fpath));
509 e = endswith(p, ".map");
513 e = endswith(p, ".map.gz");
517 r = set_consume(keymaps, p);
518 if (r < 0 && r != -EEXIST) {
519 log_error("Can't add keymap: %s", strerror(-r));
526 static int list_vconsole_keymaps(sd_bus *bus, char **args, unsigned n) {
527 _cleanup_strv_free_ char **l = NULL;
529 keymaps = set_new(string_hash_func, string_compare_func);
533 nftw("/usr/share/keymaps/", nftw_cb, 20, FTW_MOUNT|FTW_PHYS);
534 nftw("/usr/share/kbd/keymaps/", nftw_cb, 20, FTW_MOUNT|FTW_PHYS);
535 nftw("/usr/lib/kbd/keymaps/", nftw_cb, 20, FTW_MOUNT|FTW_PHYS);
536 nftw("/lib/kbd/keymaps/", nftw_cb, 20, FTW_MOUNT|FTW_PHYS);
538 l = set_get_strv(keymaps);
540 set_free_free(keymaps);
546 if (strv_isempty(l)) {
547 log_error("Couldn't find any console keymaps.");
553 pager_open_if_enabled();
560 static int set_x11_keymap(sd_bus *bus, char **args, unsigned n) {
561 _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
562 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
563 const char *layout, *model, *variant, *options;
569 log_error("Too many arguments.");
573 polkit_agent_open_if_enabled();
576 model = n > 2 ? args[2] : "";
577 variant = n > 3 ? args[3] : "";
578 options = n > 4 ? args[4] : "";
580 return sd_bus_call_method(bus,
581 "org.freedesktop.locale1",
582 "/org/freedesktop/locale1",
583 "org.freedesktop.locale1",
587 "ssssbb", layout, model, variant, options,
588 arg_convert, arg_ask_password);
591 static int list_x11_keymaps(sd_bus *bus, char **args, unsigned n) {
592 _cleanup_fclose_ FILE *f = NULL;
593 _cleanup_strv_free_ char **list = NULL;
601 } state = NONE, look_for;
605 log_error("Too many arguments.");
609 f = fopen("/usr/share/X11/xkb/rules/base.lst", "re");
611 log_error("Failed to open keyboard mapping list. %m");
615 if (streq(args[0], "list-x11-keymap-models"))
617 else if (streq(args[0], "list-x11-keymap-layouts"))
619 else if (streq(args[0], "list-x11-keymap-variants"))
621 else if (streq(args[0], "list-x11-keymap-options"))
624 assert_not_reached("Wrong parameter");
626 FOREACH_LINE(line, f, break) {
635 if (startswith(l, "! model"))
637 else if (startswith(l, "! layout"))
639 else if (startswith(l, "! variant"))
641 else if (startswith(l, "! option"))
649 if (state != look_for)
652 w = l + strcspn(l, WHITESPACE);
662 w += strspn(w, WHITESPACE);
670 if (!streq(w, args[1]))
675 r = strv_extend(&list, l);
680 if (strv_isempty(list)) {
681 log_error("Couldn't find any entries.");
688 pager_open_if_enabled();
694 static int help(void) {
696 printf("%s [OPTIONS...] COMMAND ...\n\n"
697 "Query or change system locale and keyboard settings.\n\n"
698 " -h --help Show this help\n"
699 " --version Show package version\n"
700 " --no-convert Don't convert keyboard mappings\n"
701 " --no-pager Do not pipe output into a pager\n"
702 " --no-ask-password Do not prompt for password\n"
703 " -H --host=[USER@]HOST Operate on remote host\n"
704 " -M --machine=CONTAINER Operate on local container\n\n"
706 " status Show current locale settings\n"
707 " set-locale LOCALE... Set system locale\n"
708 " list-locales Show known locales\n"
709 " set-keymap MAP [MAP] Set virtual console keyboard mapping\n"
710 " list-keymaps Show known virtual console keyboard mappings\n"
711 " set-x11-keymap LAYOUT [MODEL] [VARIANT] [OPTIONS]\n"
712 " Set X11 keyboard mapping\n"
713 " list-x11-keymap-models Show known X11 keyboard mapping models\n"
714 " list-x11-keymap-layouts Show known X11 keyboard mapping layouts\n"
715 " list-x11-keymap-variants [LAYOUT]\n"
716 " Show known X11 keyboard mapping variants\n"
717 " list-x11-keymap-options Show known X11 keyboard mapping options\n",
718 program_invocation_short_name);
723 static int parse_argv(int argc, char *argv[]) {
732 static const struct option options[] = {
733 { "help", no_argument, NULL, 'h' },
734 { "version", no_argument, NULL, ARG_VERSION },
735 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
736 { "host", required_argument, NULL, 'H' },
737 { "machine", required_argument, NULL, 'M' },
738 { "no-ask-password", no_argument, NULL, ARG_NO_ASK_PASSWORD },
739 { "no-convert", no_argument, NULL, ARG_NO_CONVERT },
748 while ((c = getopt_long(argc, argv, "hH:M:P", options, NULL)) >= 0) {
757 puts(PACKAGE_STRING);
758 puts(SYSTEMD_FEATURES);
769 case ARG_NO_ASK_PASSWORD:
770 arg_ask_password = false;
774 arg_transport = BUS_TRANSPORT_REMOTE;
779 arg_transport = BUS_TRANSPORT_CONTAINER;
787 log_error("Unknown option code %c", c);
795 static int localectl_main(sd_bus *bus, int argc, char *argv[]) {
797 static const struct {
805 int (* const dispatch)(sd_bus *bus, char **args, unsigned n);
807 { "status", LESS, 1, show_status },
808 { "set-locale", MORE, 2, set_locale },
809 { "list-locales", EQUAL, 1, list_locales },
810 { "set-keymap", MORE, 2, set_vconsole_keymap },
811 { "list-keymaps", EQUAL, 1, list_vconsole_keymaps },
812 { "set-x11-keymap", MORE, 2, set_x11_keymap },
813 { "list-x11-keymap-models", EQUAL, 1, list_x11_keymaps },
814 { "list-x11-keymap-layouts", EQUAL, 1, list_x11_keymaps },
815 { "list-x11-keymap-variants", LESS, 2, list_x11_keymaps },
816 { "list-x11-keymap-options", EQUAL, 1, list_x11_keymaps },
825 left = argc - optind;
828 /* Special rule: no arguments means "status" */
831 if (streq(argv[optind], "help")) {
836 for (i = 0; i < ELEMENTSOF(verbs); i++)
837 if (streq(argv[optind], verbs[i].verb))
840 if (i >= ELEMENTSOF(verbs)) {
841 log_error("Unknown operation %s", argv[optind]);
846 switch (verbs[i].argc_cmp) {
849 if (left != verbs[i].argc) {
850 log_error("Invalid number of arguments.");
857 if (left < verbs[i].argc) {
858 log_error("Too few arguments.");
865 if (left > verbs[i].argc) {
866 log_error("Too many arguments.");
873 assert_not_reached("Unknown comparison operator.");
876 return verbs[i].dispatch(bus, argv + optind, left);
879 int main(int argc, char*argv[]) {
880 int r, ret = EXIT_FAILURE;
881 _cleanup_bus_unref_ sd_bus *bus = NULL;
883 setlocale(LC_ALL, "");
884 log_parse_environment();
887 r = parse_argv(argc, argv);
895 r = bus_open_transport(arg_transport, arg_host, false, &bus);
897 log_error("Failed to create bus connection: %s", strerror(-r));
902 r = localectl_main(bus, argc, argv);
903 ret = r < 0 ? EXIT_FAILURE : r;