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_errno(r, "Failed to read /proc/cmdline: %m");
117 for (j = 0; j < _VARIABLE_LC_MAX; j++)
120 log_warning("Warning: Settings on kernel command line override system locale settings in /etc/locale.conf.\n"
121 " Command Line: %s=%s", locale_variable_to_string(j), variables[j]);
123 print_warning = false;
125 log_warning(" %s=%s", locale_variable_to_string(j), variables[j]);
128 for (j = 0; j < _VARIABLE_LC_MAX; j++)
132 static void print_status_info(StatusInfo *i) {
135 if (strv_isempty(i->locale))
136 puts(" System Locale: n/a\n");
140 printf(" System Locale: %s\n", i->locale[0]);
141 STRV_FOREACH(j, i->locale + 1)
145 printf(" VC Keymap: %s\n", strna(i->vconsole_keymap));
146 if (!isempty(i->vconsole_keymap_toggle))
147 printf("VC Toggle Keymap: %s\n", i->vconsole_keymap_toggle);
149 printf(" X11 Layout: %s\n", strna(i->x11_layout));
150 if (!isempty(i->x11_model))
151 printf(" X11 Model: %s\n", i->x11_model);
152 if (!isempty(i->x11_variant))
153 printf(" X11 Variant: %s\n", i->x11_variant);
154 if (!isempty(i->x11_options))
155 printf(" X11 Options: %s\n", i->x11_options);
158 static int show_status(sd_bus *bus, char **args, unsigned n) {
159 StatusInfo info = {};
160 static const struct bus_properties_map map[] = {
161 { "VConsoleKeymap", "s", NULL, offsetof(StatusInfo, vconsole_keymap) },
162 { "VConsoleKeymap", "s", NULL, offsetof(StatusInfo, vconsole_keymap) },
163 { "VConsoleKeymapToggle", "s", NULL, offsetof(StatusInfo, vconsole_keymap_toggle) },
164 { "X11Layout", "s", NULL, offsetof(StatusInfo, x11_layout) },
165 { "X11Model", "s", NULL, offsetof(StatusInfo, x11_model) },
166 { "X11Variant", "s", NULL, offsetof(StatusInfo, x11_variant) },
167 { "X11Options", "s", NULL, offsetof(StatusInfo, x11_options) },
168 { "Locale", "as", NULL, offsetof(StatusInfo, locale) },
175 r = bus_map_all_properties(bus,
176 "org.freedesktop.locale1",
177 "/org/freedesktop/locale1",
181 log_error_errno(r, "Could not get properties: %m");
185 print_overriden_variables();
186 print_status_info(&info);
189 strv_free(info.locale);
193 static int set_locale(sd_bus *bus, char **args, unsigned n) {
194 _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
195 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
201 polkit_agent_open_if_enabled();
203 r = sd_bus_message_new_method_call(
206 "org.freedesktop.locale1",
207 "/org/freedesktop/locale1",
208 "org.freedesktop.locale1",
211 return bus_log_create_error(r);
213 r = sd_bus_message_append_strv(m, args + 1);
215 return bus_log_create_error(r);
217 r = sd_bus_message_append(m, "b", arg_ask_password);
219 return bus_log_create_error(r);
221 r = sd_bus_call(bus, m, 0, &error, NULL);
223 log_error("Failed to issue method call: %s", bus_error_message(&error, -r));
230 static int list_locales(sd_bus *bus, char **args, unsigned n) {
231 _cleanup_strv_free_ char **l = NULL;
238 return log_error_errno(r, "Failed to read list of locales: %m");
240 pager_open_if_enabled();
246 static int set_vconsole_keymap(sd_bus *bus, char **args, unsigned n) {
247 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
248 const char *map, *toggle_map;
255 log_error("Too many arguments.");
259 polkit_agent_open_if_enabled();
262 toggle_map = n > 2 ? args[2] : "";
264 r = sd_bus_call_method(
266 "org.freedesktop.locale1",
267 "/org/freedesktop/locale1",
268 "org.freedesktop.locale1",
269 "SetVConsoleKeyboard",
272 "ssbb", map, toggle_map, arg_convert, arg_ask_password);
274 log_error("Failed to set keymap: %s", bus_error_message(&error, -r));
279 static Set *keymaps = NULL;
283 const struct stat *sb,
285 struct FTW *ftwbuf) {
293 if (!endswith(fpath, ".map") &&
294 !endswith(fpath, ".map.gz"))
297 p = strdup(basename(fpath));
301 e = endswith(p, ".map");
305 e = endswith(p, ".map.gz");
309 r = set_consume(keymaps, p);
310 if (r < 0 && r != -EEXIST)
311 return log_error_errno(r, "Can't add keymap: %m");
316 static int list_vconsole_keymaps(sd_bus *bus, char **args, unsigned n) {
317 _cleanup_strv_free_ char **l = NULL;
320 keymaps = set_new(&string_hash_ops);
324 NULSTR_FOREACH(dir, KBD_KEYMAP_DIRS)
325 nftw(dir, nftw_cb, 20, FTW_MOUNT|FTW_PHYS);
327 l = set_get_strv(keymaps);
329 set_free_free(keymaps);
335 if (strv_isempty(l)) {
336 log_error("Couldn't find any console keymaps.");
342 pager_open_if_enabled();
349 static int set_x11_keymap(sd_bus *bus, char **args, unsigned n) {
350 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
351 const char *layout, *model, *variant, *options;
358 log_error("Too many arguments.");
362 polkit_agent_open_if_enabled();
365 model = n > 2 ? args[2] : "";
366 variant = n > 3 ? args[3] : "";
367 options = n > 4 ? args[4] : "";
369 r = sd_bus_call_method(
371 "org.freedesktop.locale1",
372 "/org/freedesktop/locale1",
373 "org.freedesktop.locale1",
377 "ssssbb", layout, model, variant, options,
378 arg_convert, arg_ask_password);
380 log_error("Failed to set keymap: %s", bus_error_message(&error, -r));
385 static int list_x11_keymaps(sd_bus *bus, char **args, unsigned n) {
386 _cleanup_fclose_ FILE *f = NULL;
387 _cleanup_strv_free_ char **list = NULL;
395 } state = NONE, look_for;
399 log_error("Too many arguments.");
403 f = fopen("/usr/share/X11/xkb/rules/base.lst", "re");
405 return log_error_errno(errno, "Failed to open keyboard mapping list. %m");
407 if (streq(args[0], "list-x11-keymap-models"))
409 else if (streq(args[0], "list-x11-keymap-layouts"))
411 else if (streq(args[0], "list-x11-keymap-variants"))
413 else if (streq(args[0], "list-x11-keymap-options"))
416 assert_not_reached("Wrong parameter");
418 FOREACH_LINE(line, f, break) {
427 if (startswith(l, "! model"))
429 else if (startswith(l, "! layout"))
431 else if (startswith(l, "! variant"))
433 else if (startswith(l, "! option"))
441 if (state != look_for)
444 w = l + strcspn(l, WHITESPACE);
454 w += strspn(w, WHITESPACE);
462 if (!streq(w, args[1]))
467 r = strv_extend(&list, l);
472 if (strv_isempty(list)) {
473 log_error("Couldn't find any entries.");
480 pager_open_if_enabled();
486 static void help(void) {
487 printf("%s [OPTIONS...] COMMAND ...\n\n"
488 "Query or change system locale and keyboard settings.\n\n"
489 " -h --help Show this help\n"
490 " --version Show package version\n"
491 " --no-pager Do not pipe output into a pager\n"
492 " --no-ask-password Do not prompt for password\n"
493 " -H --host=[USER@]HOST Operate on remote host\n"
494 " -M --machine=CONTAINER Operate on local container\n"
495 " --no-convert Don't convert keyboard mappings\n\n"
497 " status Show current locale settings\n"
498 " set-locale LOCALE... Set system locale\n"
499 " list-locales Show known locales\n"
500 " set-keymap MAP [MAP] Set console and X11 keyboard mappings\n"
501 " list-keymaps Show known virtual console keyboard mappings\n"
502 " set-x11-keymap LAYOUT [MODEL [VARIANT [OPTIONS]]]\n"
503 " Set X11 and console keyboard mappings\n"
504 " list-x11-keymap-models Show known X11 keyboard mapping models\n"
505 " list-x11-keymap-layouts Show known X11 keyboard mapping layouts\n"
506 " list-x11-keymap-variants [LAYOUT]\n"
507 " Show known X11 keyboard mapping variants\n"
508 " list-x11-keymap-options Show known X11 keyboard mapping options\n"
509 , program_invocation_short_name);
512 static int parse_argv(int argc, char *argv[]) {
521 static const struct option options[] = {
522 { "help", no_argument, NULL, 'h' },
523 { "version", no_argument, NULL, ARG_VERSION },
524 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
525 { "host", required_argument, NULL, 'H' },
526 { "machine", required_argument, NULL, 'M' },
527 { "no-ask-password", no_argument, NULL, ARG_NO_ASK_PASSWORD },
528 { "no-convert", no_argument, NULL, ARG_NO_CONVERT },
537 while ((c = getopt_long(argc, argv, "hH:M:", options, NULL)) >= 0)
546 puts(PACKAGE_STRING);
547 puts(SYSTEMD_FEATURES);
558 case ARG_NO_ASK_PASSWORD:
559 arg_ask_password = false;
563 arg_transport = BUS_TRANSPORT_REMOTE;
568 arg_transport = BUS_TRANSPORT_MACHINE;
576 assert_not_reached("Unhandled option");
582 static int localectl_main(sd_bus *bus, int argc, char *argv[]) {
584 static const struct {
592 int (* const dispatch)(sd_bus *bus, char **args, unsigned n);
594 { "status", LESS, 1, show_status },
595 { "set-locale", MORE, 2, set_locale },
596 { "list-locales", EQUAL, 1, list_locales },
597 { "set-keymap", MORE, 2, set_vconsole_keymap },
598 { "list-keymaps", EQUAL, 1, list_vconsole_keymaps },
599 { "set-x11-keymap", MORE, 2, set_x11_keymap },
600 { "list-x11-keymap-models", EQUAL, 1, list_x11_keymaps },
601 { "list-x11-keymap-layouts", EQUAL, 1, list_x11_keymaps },
602 { "list-x11-keymap-variants", LESS, 2, list_x11_keymaps },
603 { "list-x11-keymap-options", EQUAL, 1, list_x11_keymaps },
612 left = argc - optind;
615 /* Special rule: no arguments means "status" */
618 if (streq(argv[optind], "help")) {
623 for (i = 0; i < ELEMENTSOF(verbs); i++)
624 if (streq(argv[optind], verbs[i].verb))
627 if (i >= ELEMENTSOF(verbs)) {
628 log_error("Unknown operation %s", argv[optind]);
633 switch (verbs[i].argc_cmp) {
636 if (left != verbs[i].argc) {
637 log_error("Invalid number of arguments.");
644 if (left < verbs[i].argc) {
645 log_error("Too few arguments.");
652 if (left > verbs[i].argc) {
653 log_error("Too many arguments.");
660 assert_not_reached("Unknown comparison operator.");
663 return verbs[i].dispatch(bus, argv + optind, left);
666 int main(int argc, char*argv[]) {
667 _cleanup_bus_close_unref_ sd_bus *bus = NULL;
670 setlocale(LC_ALL, "");
671 log_parse_environment();
674 r = parse_argv(argc, argv);
678 r = bus_open_transport(arg_transport, arg_host, false, &bus);
680 log_error_errno(r, "Failed to create bus connection: %m");
684 r = localectl_main(bus, argc, argv);
689 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;