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"
48 #include "locale-util.h"
50 static bool arg_no_pager = false;
51 static bool arg_ask_password = true;
52 static BusTransport arg_transport = BUS_TRANSPORT_LOCAL;
53 static char *arg_host = NULL;
54 static bool arg_convert = true;
56 static void pager_open_if_enabled(void) {
64 static void polkit_agent_open_if_enabled(void) {
66 /* Open the polkit agent as a child process if necessary */
67 if (!arg_ask_password)
70 if (arg_transport != BUS_TRANSPORT_LOCAL)
76 typedef struct StatusInfo {
78 const char *vconsole_keymap;
79 const char *vconsole_keymap_toggle;
80 const char *x11_layout;
81 const char *x11_model;
82 const char *x11_variant;
83 const char *x11_options;
86 static void print_overriden_variables(void) {
88 char *variables[_VARIABLE_LC_MAX] = {};
90 bool print_warning = true;
92 if (detect_container(NULL) > 0 || arg_host)
95 r = parse_env_file("/proc/cmdline", WHITESPACE,
96 "locale.LANG", &variables[VARIABLE_LANG],
97 "locale.LANGUAGE", &variables[VARIABLE_LANGUAGE],
98 "locale.LC_CTYPE", &variables[VARIABLE_LC_CTYPE],
99 "locale.LC_NUMERIC", &variables[VARIABLE_LC_NUMERIC],
100 "locale.LC_TIME", &variables[VARIABLE_LC_TIME],
101 "locale.LC_COLLATE", &variables[VARIABLE_LC_COLLATE],
102 "locale.LC_MONETARY", &variables[VARIABLE_LC_MONETARY],
103 "locale.LC_MESSAGES", &variables[VARIABLE_LC_MESSAGES],
104 "locale.LC_PAPER", &variables[VARIABLE_LC_PAPER],
105 "locale.LC_NAME", &variables[VARIABLE_LC_NAME],
106 "locale.LC_ADDRESS", &variables[VARIABLE_LC_ADDRESS],
107 "locale.LC_TELEPHONE", &variables[VARIABLE_LC_TELEPHONE],
108 "locale.LC_MEASUREMENT", &variables[VARIABLE_LC_MEASUREMENT],
109 "locale.LC_IDENTIFICATION", &variables[VARIABLE_LC_IDENTIFICATION],
112 if (r < 0 && r != -ENOENT) {
113 log_warning("Failed to read /proc/cmdline: %s", strerror(-r));
117 for (j = VARIABLE_LANG; j < _VARIABLE_LC_MAX; j++)
120 printf("Warning: Settings on Kernel Command Line override system locale settings in /etc/locale.conf\n");
121 printf(" Command Line: %s=%s\n", locale_variable_to_string(j), variables[j]);
123 print_warning = false;
126 printf(" %s=%s\n", locale_variable_to_string(j), variables[j]);
129 for (j = VARIABLE_LANG; j < _VARIABLE_LC_MAX; j++)
133 static void print_status_info(StatusInfo *i) {
136 if (strv_isempty(i->locale))
137 puts(" System Locale: n/a\n");
141 printf(" System Locale: %s\n", i->locale[0]);
142 STRV_FOREACH(j, i->locale + 1)
146 printf(" VC Keymap: %s\n", strna(i->vconsole_keymap));
147 if (!isempty(i->vconsole_keymap_toggle))
148 printf("VC Toggle Keymap: %s\n", i->vconsole_keymap_toggle);
150 printf(" X11 Layout: %s\n", strna(i->x11_layout));
151 if (!isempty(i->x11_model))
152 printf(" X11 Model: %s\n", i->x11_model);
153 if (!isempty(i->x11_variant))
154 printf(" X11 Variant: %s\n", i->x11_variant);
155 if (!isempty(i->x11_options))
156 printf(" X11 Options: %s\n", i->x11_options);
159 static int show_status(sd_bus *bus, char **args, unsigned n) {
160 StatusInfo info = {};
161 static const struct bus_properties_map map[] = {
162 { "VConsoleKeymap", "s", NULL, offsetof(StatusInfo, vconsole_keymap) },
163 { "VConsoleKeymap", "s", NULL, offsetof(StatusInfo, vconsole_keymap) },
164 { "VConsoleKeymapToggle", "s", NULL, offsetof(StatusInfo, vconsole_keymap_toggle) },
165 { "X11Layout", "s", NULL, offsetof(StatusInfo, x11_layout) },
166 { "X11Model", "s", NULL, offsetof(StatusInfo, x11_model) },
167 { "X11Variant", "s", NULL, offsetof(StatusInfo, x11_variant) },
168 { "X11Options", "s", NULL, offsetof(StatusInfo, x11_options) },
169 { "Locale", "as", NULL, offsetof(StatusInfo, locale) },
176 r = bus_map_all_properties(bus,
177 "org.freedesktop.locale1",
178 "/org/freedesktop/locale1",
182 log_error("Could not get properties: %s", strerror(-r));
186 print_overriden_variables();
187 print_status_info(&info);
190 strv_free(info.locale);
194 static int set_locale(sd_bus *bus, char **args, unsigned n) {
195 _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
196 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
202 polkit_agent_open_if_enabled();
204 r = sd_bus_message_new_method_call(
207 "org.freedesktop.locale1",
208 "/org/freedesktop/locale1",
209 "org.freedesktop.locale1",
212 return bus_log_create_error(r);
214 r = sd_bus_message_append_strv(m, args + 1);
216 return bus_log_create_error(r);
218 r = sd_bus_message_append(m, "b", arg_ask_password);
220 return bus_log_create_error(r);
222 r = sd_bus_call(bus, m, 0, &error, NULL);
224 log_error("Failed to issue method call: %s", bus_error_message(&error, -r));
231 static int list_locales(sd_bus *bus, char **args, unsigned n) {
232 _cleanup_strv_free_ char **l = NULL;
239 log_error("Failed to read list of locales: %s", strerror(-r));
243 pager_open_if_enabled();
249 static int set_vconsole_keymap(sd_bus *bus, char **args, unsigned n) {
250 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
251 const char *map, *toggle_map;
258 log_error("Too many arguments.");
262 polkit_agent_open_if_enabled();
265 toggle_map = n > 2 ? args[2] : "";
267 r = sd_bus_call_method(
269 "org.freedesktop.locale1",
270 "/org/freedesktop/locale1",
271 "org.freedesktop.locale1",
272 "SetVConsoleKeyboard",
275 "ssbb", map, toggle_map, arg_convert, arg_ask_password);
277 log_error("Failed to set keymap: %s", bus_error_message(&error, -r));
282 static Set *keymaps = NULL;
286 const struct stat *sb,
288 struct FTW *ftwbuf) {
296 if (!endswith(fpath, ".map") &&
297 !endswith(fpath, ".map.gz"))
300 p = strdup(basename(fpath));
304 e = endswith(p, ".map");
308 e = endswith(p, ".map.gz");
312 r = set_consume(keymaps, p);
313 if (r < 0 && r != -EEXIST) {
314 log_error("Can't add keymap: %s", strerror(-r));
321 static int list_vconsole_keymaps(sd_bus *bus, char **args, unsigned n) {
322 _cleanup_strv_free_ char **l = NULL;
325 keymaps = set_new(&string_hash_ops);
329 NULSTR_FOREACH(dir, KBD_KEYMAP_DIRS)
330 nftw(dir, nftw_cb, 20, FTW_MOUNT|FTW_PHYS);
332 l = set_get_strv(keymaps);
334 set_free_free(keymaps);
340 if (strv_isempty(l)) {
341 log_error("Couldn't find any console keymaps.");
347 pager_open_if_enabled();
354 static int set_x11_keymap(sd_bus *bus, char **args, unsigned n) {
355 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
356 const char *layout, *model, *variant, *options;
363 log_error("Too many arguments.");
367 polkit_agent_open_if_enabled();
370 model = n > 2 ? args[2] : "";
371 variant = n > 3 ? args[3] : "";
372 options = n > 4 ? args[4] : "";
374 r = sd_bus_call_method(
376 "org.freedesktop.locale1",
377 "/org/freedesktop/locale1",
378 "org.freedesktop.locale1",
382 "ssssbb", layout, model, variant, options,
383 arg_convert, arg_ask_password);
385 log_error("Failed to set keymap: %s", bus_error_message(&error, -r));
390 static int list_x11_keymaps(sd_bus *bus, char **args, unsigned n) {
391 _cleanup_fclose_ FILE *f = NULL;
392 _cleanup_strv_free_ char **list = NULL;
400 } state = NONE, look_for;
404 log_error("Too many arguments.");
408 f = fopen("/usr/share/X11/xkb/rules/base.lst", "re");
410 log_error("Failed to open keyboard mapping list. %m");
414 if (streq(args[0], "list-x11-keymap-models"))
416 else if (streq(args[0], "list-x11-keymap-layouts"))
418 else if (streq(args[0], "list-x11-keymap-variants"))
420 else if (streq(args[0], "list-x11-keymap-options"))
423 assert_not_reached("Wrong parameter");
425 FOREACH_LINE(line, f, break) {
434 if (startswith(l, "! model"))
436 else if (startswith(l, "! layout"))
438 else if (startswith(l, "! variant"))
440 else if (startswith(l, "! option"))
448 if (state != look_for)
451 w = l + strcspn(l, WHITESPACE);
461 w += strspn(w, WHITESPACE);
469 if (!streq(w, args[1]))
474 r = strv_extend(&list, l);
479 if (strv_isempty(list)) {
480 log_error("Couldn't find any entries.");
487 pager_open_if_enabled();
493 static void help(void) {
494 printf("%s [OPTIONS...] COMMAND ...\n\n"
495 "Query or change system locale and keyboard settings.\n\n"
496 " -h --help Show this help\n"
497 " --version Show package version\n"
498 " --no-pager Do not pipe output into a pager\n"
499 " --no-ask-password Do not prompt for password\n"
500 " -H --host=[USER@]HOST Operate on remote host\n"
501 " -M --machine=CONTAINER Operate on local container\n"
502 " --no-convert Don't convert keyboard mappings\n\n"
504 " status Show current locale settings\n"
505 " set-locale LOCALE... Set system locale\n"
506 " list-locales Show known locales\n"
507 " set-keymap MAP [MAP] Set virtual console keyboard mapping\n"
508 " list-keymaps Show known virtual console keyboard mappings\n"
509 " set-x11-keymap LAYOUT [MODEL] [VARIANT] [OPTIONS]\n"
510 " Set X11 keyboard mapping\n"
511 " list-x11-keymap-models Show known X11 keyboard mapping models\n"
512 " list-x11-keymap-layouts Show known X11 keyboard mapping layouts\n"
513 " list-x11-keymap-variants [LAYOUT]\n"
514 " Show known X11 keyboard mapping variants\n"
515 " list-x11-keymap-options Show known X11 keyboard mapping options\n"
516 , program_invocation_short_name);
519 static int parse_argv(int argc, char *argv[]) {
528 static const struct option options[] = {
529 { "help", no_argument, NULL, 'h' },
530 { "version", no_argument, NULL, ARG_VERSION },
531 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
532 { "host", required_argument, NULL, 'H' },
533 { "machine", required_argument, NULL, 'M' },
534 { "no-ask-password", no_argument, NULL, ARG_NO_ASK_PASSWORD },
535 { "no-convert", no_argument, NULL, ARG_NO_CONVERT },
544 while ((c = getopt_long(argc, argv, "hH:M:", options, NULL)) >= 0)
553 puts(PACKAGE_STRING);
554 puts(SYSTEMD_FEATURES);
565 case ARG_NO_ASK_PASSWORD:
566 arg_ask_password = false;
570 arg_transport = BUS_TRANSPORT_REMOTE;
575 arg_transport = BUS_TRANSPORT_CONTAINER;
583 assert_not_reached("Unhandled option");
589 static int localectl_main(sd_bus *bus, int argc, char *argv[]) {
591 static const struct {
599 int (* const dispatch)(sd_bus *bus, char **args, unsigned n);
601 { "status", LESS, 1, show_status },
602 { "set-locale", MORE, 2, set_locale },
603 { "list-locales", EQUAL, 1, list_locales },
604 { "set-keymap", MORE, 2, set_vconsole_keymap },
605 { "list-keymaps", EQUAL, 1, list_vconsole_keymaps },
606 { "set-x11-keymap", MORE, 2, set_x11_keymap },
607 { "list-x11-keymap-models", EQUAL, 1, list_x11_keymaps },
608 { "list-x11-keymap-layouts", EQUAL, 1, list_x11_keymaps },
609 { "list-x11-keymap-variants", LESS, 2, list_x11_keymaps },
610 { "list-x11-keymap-options", EQUAL, 1, list_x11_keymaps },
619 left = argc - optind;
622 /* Special rule: no arguments means "status" */
625 if (streq(argv[optind], "help")) {
630 for (i = 0; i < ELEMENTSOF(verbs); i++)
631 if (streq(argv[optind], verbs[i].verb))
634 if (i >= ELEMENTSOF(verbs)) {
635 log_error("Unknown operation %s", argv[optind]);
640 switch (verbs[i].argc_cmp) {
643 if (left != verbs[i].argc) {
644 log_error("Invalid number of arguments.");
651 if (left < verbs[i].argc) {
652 log_error("Too few arguments.");
659 if (left > verbs[i].argc) {
660 log_error("Too many arguments.");
667 assert_not_reached("Unknown comparison operator.");
670 return verbs[i].dispatch(bus, argv + optind, left);
673 int main(int argc, char*argv[]) {
674 _cleanup_bus_close_unref_ sd_bus *bus = NULL;
677 setlocale(LC_ALL, "");
678 log_parse_environment();
681 r = parse_argv(argc, argv);
685 r = bus_open_transport(arg_transport, arg_host, false, &bus);
687 log_error("Failed to create bus connection: %s", strerror(-r));
691 r = localectl_main(bus, argc, argv);
696 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;