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)
66 if (arg_transport != BUS_TRANSPORT_LOCAL)
72 typedef struct StatusInfo {
74 const char *vconsole_keymap;
75 const char *vconsole_keymap_toggle;
76 const char *x11_layout;
77 const char *x11_model;
78 const char *x11_variant;
79 const char *x11_options;
82 static void print_status_info(StatusInfo *i) {
85 if (strv_isempty(i->locale))
86 puts(" System Locale: n/a\n");
90 printf(" System Locale: %s\n", i->locale[0]);
91 STRV_FOREACH(j, i->locale + 1)
95 printf(" VC Keymap: %s\n", strna(i->vconsole_keymap));
96 if (!isempty(i->vconsole_keymap_toggle))
97 printf("VC Toggle Keymap: %s\n", i->vconsole_keymap_toggle);
99 printf(" X11 Layout: %s\n", strna(i->x11_layout));
100 if (!isempty(i->x11_model))
101 printf(" X11 Model: %s\n", i->x11_model);
102 if (!isempty(i->x11_variant))
103 printf(" X11 Variant: %s\n", i->x11_variant);
104 if (!isempty(i->x11_options))
105 printf(" X11 Options: %s\n", i->x11_options);
108 static int show_status(sd_bus *bus, char **args, unsigned n) {
109 StatusInfo info = {};
110 static const struct bus_properties_map map[] = {
111 { "VConsoleKeymap", "s", NULL, offsetof(StatusInfo, vconsole_keymap) },
112 { "VConsoleKeymap", "s", NULL, offsetof(StatusInfo, vconsole_keymap) },
113 { "VConsoleKeymapToggle", "s", NULL, offsetof(StatusInfo, vconsole_keymap_toggle) },
114 { "X11Layout", "s", NULL, offsetof(StatusInfo, x11_layout) },
115 { "X11Model", "s", NULL, offsetof(StatusInfo, x11_model) },
116 { "X11Variant", "s", NULL, offsetof(StatusInfo, x11_variant) },
117 { "X11Options", "s", NULL, offsetof(StatusInfo, x11_options) },
118 { "Locale", "as", NULL, offsetof(StatusInfo, locale) },
125 r = bus_map_all_properties(bus,
126 "org.freedesktop.locale1",
127 "/org/freedesktop/locale1",
133 print_status_info(&info);
136 strv_free(info.locale);
140 static int set_locale(sd_bus *bus, char **args, unsigned n) {
141 _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
142 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
148 polkit_agent_open_if_enabled();
150 r = sd_bus_message_new_method_call(bus,
151 "org.freedesktop.locale1",
152 "/org/freedesktop/locale1",
153 "org.freedesktop.locale1",
156 return bus_log_create_error(r);
158 r = sd_bus_message_append_strv(m, args + 1);
160 return bus_log_create_error(r);
162 r = sd_bus_message_append(m, "b", arg_ask_password);
164 return bus_log_create_error(r);
166 r = sd_bus_call(bus, m, 0, &error, NULL);
168 log_error("Failed to issue method call: %s", bus_error_message(&error, -r));
175 static int add_locales_from_archive(Set *locales) {
176 /* Stolen from glibc... */
182 /* Name hash table. */
183 uint32_t namehash_offset;
184 uint32_t namehash_used;
185 uint32_t namehash_size;
187 uint32_t string_offset;
188 uint32_t string_used;
189 uint32_t string_size;
190 /* Table with locale records. */
191 uint32_t locrectab_offset;
192 uint32_t locrectab_used;
193 uint32_t locrectab_size;
194 /* MD5 sum hash table. */
195 uint32_t sumhash_offset;
196 uint32_t sumhash_used;
197 uint32_t sumhash_size;
201 /* Hash value of the name. */
203 /* Offset of the name in the string table. */
204 uint32_t name_offset;
205 /* Offset of the locale record. */
206 uint32_t locrec_offset;
209 const struct locarhead *h;
210 const struct namehashent *e;
211 const void *p = MAP_FAILED;
212 _cleanup_close_ int fd = -1;
218 fd = open("/usr/lib/locale/locale-archive", O_RDONLY|O_NOCTTY|O_CLOEXEC);
221 log_error("Failed to open locale archive: %m");
226 if (fstat(fd, &st) < 0) {
227 log_error("fstat() failed: %m");
232 if (!S_ISREG(st.st_mode)) {
233 log_error("Archive file is not regular");
238 if (st.st_size < (off_t) sizeof(struct locarhead)) {
239 log_error("Archive has invalid size");
244 p = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
245 if (p == MAP_FAILED) {
246 log_error("Failed to map archive: %m");
251 h = (const struct locarhead *) p;
252 if (h->magic != 0xde020109 ||
253 h->namehash_offset + h->namehash_size > st.st_size ||
254 h->string_offset + h->string_size > st.st_size ||
255 h->locrectab_offset + h->locrectab_size > st.st_size ||
256 h->sumhash_offset + h->sumhash_size > st.st_size) {
257 log_error("Invalid archive file.");
262 e = (const struct namehashent*) ((const uint8_t*) p + h->namehash_offset);
263 for (i = 0; i < h->namehash_size; i++) {
266 if (e[i].locrec_offset == 0)
269 if (!utf8_is_valid((char*) p + e[i].name_offset))
272 z = strdup((char*) p + e[i].name_offset);
278 r = set_consume(locales, z);
280 log_error("Failed to add locale: %s", strerror(-r));
289 munmap((void*) p, sz);
294 static int add_locales_from_libdir (Set *locales) {
295 _cleanup_closedir_ DIR *dir;
296 struct dirent *entry;
299 dir = opendir("/usr/lib/locale");
301 log_error("Failed to open locale directory: %m");
306 while ((entry = readdir(dir))) {
309 if (entry->d_type != DT_DIR)
312 if (ignore_file(entry->d_name))
315 z = strdup(entry->d_name);
319 r = set_consume(locales, z);
320 if (r < 0 && r != -EEXIST) {
321 log_error("Failed to add locale: %s", strerror(-r));
329 log_error("Failed to read locale directory: %m");
336 static int list_locales(sd_bus *bus, char **args, unsigned n) {
337 _cleanup_set_free_ Set *locales;
338 _cleanup_strv_free_ char **l = NULL;
341 locales = set_new(string_hash_func, string_compare_func);
345 r = add_locales_from_archive(locales);
346 if (r < 0 && r != -ENOENT)
349 r = add_locales_from_libdir(locales);
353 l = set_get_strv(locales);
359 pager_open_if_enabled();
366 static int set_vconsole_keymap(sd_bus *bus, char **args, unsigned n) {
367 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
368 const char *map, *toggle_map;
375 log_error("Too many arguments.");
379 polkit_agent_open_if_enabled();
382 toggle_map = n > 2 ? args[2] : "";
384 r = sd_bus_call_method(
386 "org.freedesktop.locale1",
387 "/org/freedesktop/locale1",
388 "org.freedesktop.locale1",
389 "SetVConsoleKeyboard",
392 "ssbb", map, toggle_map, arg_convert, arg_ask_password);
394 log_error("Failed to set keymap: %s", bus_error_message(&error, -r));
399 static Set *keymaps = NULL;
403 const struct stat *sb,
405 struct FTW *ftwbuf) {
413 if (!endswith(fpath, ".map") &&
414 !endswith(fpath, ".map.gz"))
417 p = strdup(path_get_file_name(fpath));
421 e = endswith(p, ".map");
425 e = endswith(p, ".map.gz");
429 r = set_consume(keymaps, p);
430 if (r < 0 && r != -EEXIST) {
431 log_error("Can't add keymap: %s", strerror(-r));
438 static int list_vconsole_keymaps(sd_bus *bus, char **args, unsigned n) {
439 _cleanup_strv_free_ char **l = NULL;
441 keymaps = set_new(string_hash_func, string_compare_func);
445 nftw("/usr/share/keymaps/", nftw_cb, 20, FTW_MOUNT|FTW_PHYS);
446 nftw("/usr/share/kbd/keymaps/", nftw_cb, 20, FTW_MOUNT|FTW_PHYS);
447 nftw("/usr/lib/kbd/keymaps/", nftw_cb, 20, FTW_MOUNT|FTW_PHYS);
448 nftw("/lib/kbd/keymaps/", nftw_cb, 20, FTW_MOUNT|FTW_PHYS);
450 l = set_get_strv(keymaps);
452 set_free_free(keymaps);
458 if (strv_isempty(l)) {
459 log_error("Couldn't find any console keymaps.");
465 pager_open_if_enabled();
472 static int set_x11_keymap(sd_bus *bus, char **args, unsigned n) {
473 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
474 const char *layout, *model, *variant, *options;
481 log_error("Too many arguments.");
485 polkit_agent_open_if_enabled();
488 model = n > 2 ? args[2] : "";
489 variant = n > 3 ? args[3] : "";
490 options = n > 4 ? args[4] : "";
492 r = sd_bus_call_method(
494 "org.freedesktop.locale1",
495 "/org/freedesktop/locale1",
496 "org.freedesktop.locale1",
500 "ssssbb", layout, model, variant, options,
501 arg_convert, arg_ask_password);
503 log_error("Failed to set keymap: %s", bus_error_message(&error, -r));
508 static int list_x11_keymaps(sd_bus *bus, char **args, unsigned n) {
509 _cleanup_fclose_ FILE *f = NULL;
510 _cleanup_strv_free_ char **list = NULL;
518 } state = NONE, look_for;
522 log_error("Too many arguments.");
526 f = fopen("/usr/share/X11/xkb/rules/base.lst", "re");
528 log_error("Failed to open keyboard mapping list. %m");
532 if (streq(args[0], "list-x11-keymap-models"))
534 else if (streq(args[0], "list-x11-keymap-layouts"))
536 else if (streq(args[0], "list-x11-keymap-variants"))
538 else if (streq(args[0], "list-x11-keymap-options"))
541 assert_not_reached("Wrong parameter");
543 FOREACH_LINE(line, f, break) {
552 if (startswith(l, "! model"))
554 else if (startswith(l, "! layout"))
556 else if (startswith(l, "! variant"))
558 else if (startswith(l, "! option"))
566 if (state != look_for)
569 w = l + strcspn(l, WHITESPACE);
579 w += strspn(w, WHITESPACE);
587 if (!streq(w, args[1]))
592 r = strv_extend(&list, l);
597 if (strv_isempty(list)) {
598 log_error("Couldn't find any entries.");
605 pager_open_if_enabled();
611 static int help(void) {
613 printf("%s [OPTIONS...] COMMAND ...\n\n"
614 "Query or change system locale and keyboard settings.\n\n"
615 " -h --help Show this help\n"
616 " --version Show package version\n"
617 " --no-pager Do not pipe output into a pager\n"
618 " --no-ask-password Do not prompt for password\n"
619 " -H --host=[USER@]HOST Operate on remote host\n"
620 " -M --machine=CONTAINER Operate on local container\n"
621 " --no-convert Don't convert keyboard mappings\n\n"
623 " status Show current locale settings\n"
624 " set-locale LOCALE... Set system locale\n"
625 " list-locales Show known locales\n"
626 " set-keymap MAP [MAP] Set virtual console keyboard mapping\n"
627 " list-keymaps Show known virtual console keyboard mappings\n"
628 " set-x11-keymap LAYOUT [MODEL] [VARIANT] [OPTIONS]\n"
629 " Set X11 keyboard mapping\n"
630 " list-x11-keymap-models Show known X11 keyboard mapping models\n"
631 " list-x11-keymap-layouts Show known X11 keyboard mapping layouts\n"
632 " list-x11-keymap-variants [LAYOUT]\n"
633 " Show known X11 keyboard mapping variants\n"
634 " list-x11-keymap-options Show known X11 keyboard mapping options\n",
635 program_invocation_short_name);
640 static int parse_argv(int argc, char *argv[]) {
649 static const struct option options[] = {
650 { "help", no_argument, NULL, 'h' },
651 { "version", no_argument, NULL, ARG_VERSION },
652 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
653 { "host", required_argument, NULL, 'H' },
654 { "machine", required_argument, NULL, 'M' },
655 { "no-ask-password", no_argument, NULL, ARG_NO_ASK_PASSWORD },
656 { "no-convert", no_argument, NULL, ARG_NO_CONVERT },
665 while ((c = getopt_long(argc, argv, "hH:M:", options, NULL)) >= 0) {
673 puts(PACKAGE_STRING);
674 puts(SYSTEMD_FEATURES);
685 case ARG_NO_ASK_PASSWORD:
686 arg_ask_password = false;
690 arg_transport = BUS_TRANSPORT_REMOTE;
695 arg_transport = BUS_TRANSPORT_CONTAINER;
703 assert_not_reached("Unhandled option");
710 static int localectl_main(sd_bus *bus, int argc, char *argv[]) {
712 static const struct {
720 int (* const dispatch)(sd_bus *bus, char **args, unsigned n);
722 { "status", LESS, 1, show_status },
723 { "set-locale", MORE, 2, set_locale },
724 { "list-locales", EQUAL, 1, list_locales },
725 { "set-keymap", MORE, 2, set_vconsole_keymap },
726 { "list-keymaps", EQUAL, 1, list_vconsole_keymaps },
727 { "set-x11-keymap", MORE, 2, set_x11_keymap },
728 { "list-x11-keymap-models", EQUAL, 1, list_x11_keymaps },
729 { "list-x11-keymap-layouts", EQUAL, 1, list_x11_keymaps },
730 { "list-x11-keymap-variants", LESS, 2, list_x11_keymaps },
731 { "list-x11-keymap-options", EQUAL, 1, list_x11_keymaps },
740 left = argc - optind;
743 /* Special rule: no arguments means "status" */
746 if (streq(argv[optind], "help")) {
751 for (i = 0; i < ELEMENTSOF(verbs); i++)
752 if (streq(argv[optind], verbs[i].verb))
755 if (i >= ELEMENTSOF(verbs)) {
756 log_error("Unknown operation %s", argv[optind]);
761 switch (verbs[i].argc_cmp) {
764 if (left != verbs[i].argc) {
765 log_error("Invalid number of arguments.");
772 if (left < verbs[i].argc) {
773 log_error("Too few arguments.");
780 if (left > verbs[i].argc) {
781 log_error("Too many arguments.");
788 assert_not_reached("Unknown comparison operator.");
791 return verbs[i].dispatch(bus, argv + optind, left);
794 int main(int argc, char*argv[]) {
795 _cleanup_bus_unref_ sd_bus *bus = NULL;
798 setlocale(LC_ALL, "");
799 log_parse_environment();
802 r = parse_argv(argc, argv);
806 r = bus_open_transport(arg_transport, arg_host, false, &bus);
808 log_error("Failed to create bus connection: %s", strerror(-r));
812 r = localectl_main(bus, argc, argv);
817 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;