1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2012 Lennart Poettering
6 Copyright 2013 Kay Sievers
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
31 #include "bus-error.h"
36 #include "locale-util.h"
39 #include "spawn-polkit-agent.h"
44 static bool arg_no_pager
= false;
45 static bool arg_ask_password
= true;
46 static BusTransport arg_transport
= BUS_TRANSPORT_LOCAL
;
47 static char *arg_host
= NULL
;
48 static bool arg_convert
= true;
50 typedef struct StatusInfo
{
52 char *vconsole_keymap
;
53 char *vconsole_keymap_toggle
;
60 static void status_info_clear(StatusInfo
*info
) {
62 strv_free(info
->locale
);
63 free(info
->vconsole_keymap
);
64 free(info
->vconsole_keymap_toggle
);
65 free(info
->x11_layout
);
66 free(info
->x11_model
);
67 free(info
->x11_variant
);
68 free(info
->x11_options
);
73 static void print_overridden_variables(void) {
75 char *variables
[_VARIABLE_LC_MAX
] = {};
77 bool print_warning
= true;
79 if (detect_container() > 0 || arg_host
)
82 r
= parse_env_file("/proc/cmdline", WHITESPACE
,
83 "locale.LANG", &variables
[VARIABLE_LANG
],
84 "locale.LANGUAGE", &variables
[VARIABLE_LANGUAGE
],
85 "locale.LC_CTYPE", &variables
[VARIABLE_LC_CTYPE
],
86 "locale.LC_NUMERIC", &variables
[VARIABLE_LC_NUMERIC
],
87 "locale.LC_TIME", &variables
[VARIABLE_LC_TIME
],
88 "locale.LC_COLLATE", &variables
[VARIABLE_LC_COLLATE
],
89 "locale.LC_MONETARY", &variables
[VARIABLE_LC_MONETARY
],
90 "locale.LC_MESSAGES", &variables
[VARIABLE_LC_MESSAGES
],
91 "locale.LC_PAPER", &variables
[VARIABLE_LC_PAPER
],
92 "locale.LC_NAME", &variables
[VARIABLE_LC_NAME
],
93 "locale.LC_ADDRESS", &variables
[VARIABLE_LC_ADDRESS
],
94 "locale.LC_TELEPHONE", &variables
[VARIABLE_LC_TELEPHONE
],
95 "locale.LC_MEASUREMENT", &variables
[VARIABLE_LC_MEASUREMENT
],
96 "locale.LC_IDENTIFICATION", &variables
[VARIABLE_LC_IDENTIFICATION
],
99 if (r
< 0 && r
!= -ENOENT
) {
100 log_warning_errno(r
, "Failed to read /proc/cmdline: %m");
104 for (j
= 0; j
< _VARIABLE_LC_MAX
; j
++)
107 log_warning("Warning: Settings on kernel command line override system locale settings in /etc/locale.conf.\n"
108 " Command Line: %s=%s", locale_variable_to_string(j
), variables
[j
]);
110 print_warning
= false;
112 log_warning(" %s=%s", locale_variable_to_string(j
), variables
[j
]);
115 for (j
= 0; j
< _VARIABLE_LC_MAX
; j
++)
119 static void print_status_info(StatusInfo
*i
) {
122 if (strv_isempty(i
->locale
))
123 puts(" System Locale: n/a");
127 printf(" System Locale: %s\n", i
->locale
[0]);
128 STRV_FOREACH(j
, i
->locale
+ 1)
132 printf(" VC Keymap: %s\n", strna(i
->vconsole_keymap
));
133 if (!isempty(i
->vconsole_keymap_toggle
))
134 printf("VC Toggle Keymap: %s\n", i
->vconsole_keymap_toggle
);
136 printf(" X11 Layout: %s\n", strna(i
->x11_layout
));
137 if (!isempty(i
->x11_model
))
138 printf(" X11 Model: %s\n", i
->x11_model
);
139 if (!isempty(i
->x11_variant
))
140 printf(" X11 Variant: %s\n", i
->x11_variant
);
141 if (!isempty(i
->x11_options
))
142 printf(" X11 Options: %s\n", i
->x11_options
);
145 static int show_status(sd_bus
*bus
, char **args
, unsigned n
) {
146 _cleanup_(status_info_clear
) StatusInfo info
= {};
147 static const struct bus_properties_map map
[] = {
148 { "VConsoleKeymap", "s", NULL
, offsetof(StatusInfo
, vconsole_keymap
) },
149 { "VConsoleKeymap", "s", NULL
, offsetof(StatusInfo
, vconsole_keymap
) },
150 { "VConsoleKeymapToggle", "s", NULL
, offsetof(StatusInfo
, vconsole_keymap_toggle
) },
151 { "X11Layout", "s", NULL
, offsetof(StatusInfo
, x11_layout
) },
152 { "X11Model", "s", NULL
, offsetof(StatusInfo
, x11_model
) },
153 { "X11Variant", "s", NULL
, offsetof(StatusInfo
, x11_variant
) },
154 { "X11Options", "s", NULL
, offsetof(StatusInfo
, x11_options
) },
155 { "Locale", "as", NULL
, offsetof(StatusInfo
, locale
) },
159 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
164 r
= bus_map_all_properties(bus
,
165 "org.freedesktop.locale1",
166 "/org/freedesktop/locale1",
171 return log_error_errno(r
, "Could not get properties: %s", bus_error_message(&error
, r
));
173 print_overridden_variables();
174 print_status_info(&info
);
179 static int set_locale(sd_bus
*bus
, char **args
, unsigned n
) {
180 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
181 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
187 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
189 r
= sd_bus_message_new_method_call(
192 "org.freedesktop.locale1",
193 "/org/freedesktop/locale1",
194 "org.freedesktop.locale1",
197 return bus_log_create_error(r
);
199 r
= sd_bus_message_append_strv(m
, args
+ 1);
201 return bus_log_create_error(r
);
203 r
= sd_bus_message_append(m
, "b", arg_ask_password
);
205 return bus_log_create_error(r
);
207 r
= sd_bus_call(bus
, m
, 0, &error
, NULL
);
209 log_error("Failed to issue method call: %s", bus_error_message(&error
, -r
));
216 static int list_locales(sd_bus
*bus
, char **args
, unsigned n
) {
217 _cleanup_strv_free_
char **l
= NULL
;
224 return log_error_errno(r
, "Failed to read list of locales: %m");
226 pager_open(arg_no_pager
, false);
232 static int set_vconsole_keymap(sd_bus
*bus
, char **args
, unsigned n
) {
233 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
234 const char *map
, *toggle_map
;
241 log_error("Too many arguments.");
245 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
248 toggle_map
= n
> 2 ? args
[2] : "";
250 r
= sd_bus_call_method(
252 "org.freedesktop.locale1",
253 "/org/freedesktop/locale1",
254 "org.freedesktop.locale1",
255 "SetVConsoleKeyboard",
258 "ssbb", map
, toggle_map
, arg_convert
, arg_ask_password
);
260 log_error("Failed to set keymap: %s", bus_error_message(&error
, -r
));
265 static int list_vconsole_keymaps(sd_bus
*bus
, char **args
, unsigned n
) {
266 _cleanup_strv_free_
char **l
= NULL
;
273 return log_error_errno(r
, "Failed to read list of keymaps: %m");
275 pager_open(arg_no_pager
, false);
282 static int set_x11_keymap(sd_bus
*bus
, char **args
, unsigned n
) {
283 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
284 const char *layout
, *model
, *variant
, *options
;
291 log_error("Too many arguments.");
295 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
298 model
= n
> 2 ? args
[2] : "";
299 variant
= n
> 3 ? args
[3] : "";
300 options
= n
> 4 ? args
[4] : "";
302 r
= sd_bus_call_method(
304 "org.freedesktop.locale1",
305 "/org/freedesktop/locale1",
306 "org.freedesktop.locale1",
310 "ssssbb", layout
, model
, variant
, options
,
311 arg_convert
, arg_ask_password
);
313 log_error("Failed to set keymap: %s", bus_error_message(&error
, -r
));
318 static int list_x11_keymaps(sd_bus
*bus
, char **args
, unsigned n
) {
319 _cleanup_fclose_
FILE *f
= NULL
;
320 _cleanup_strv_free_
char **list
= NULL
;
328 } state
= NONE
, look_for
;
332 log_error("Too many arguments.");
336 f
= fopen("/usr/share/X11/xkb/rules/base.lst", "re");
338 return log_error_errno(errno
, "Failed to open keyboard mapping list. %m");
340 if (streq(args
[0], "list-x11-keymap-models"))
342 else if (streq(args
[0], "list-x11-keymap-layouts"))
344 else if (streq(args
[0], "list-x11-keymap-variants"))
346 else if (streq(args
[0], "list-x11-keymap-options"))
349 assert_not_reached("Wrong parameter");
351 FOREACH_LINE(line
, f
, break) {
360 if (startswith(l
, "! model"))
362 else if (startswith(l
, "! layout"))
364 else if (startswith(l
, "! variant"))
366 else if (startswith(l
, "! option"))
374 if (state
!= look_for
)
377 w
= l
+ strcspn(l
, WHITESPACE
);
387 w
+= strspn(w
, WHITESPACE
);
395 if (!streq(w
, args
[1]))
400 r
= strv_extend(&list
, l
);
405 if (strv_isempty(list
)) {
406 log_error("Couldn't find any entries.");
413 pager_open(arg_no_pager
, false);
419 static void help(void) {
420 printf("%s [OPTIONS...] COMMAND ...\n\n"
421 "Query or change system locale and keyboard settings.\n\n"
422 " -h --help Show this help\n"
423 " --version Show package version\n"
424 " --no-pager Do not pipe output into a pager\n"
425 " --no-ask-password Do not prompt for password\n"
426 " -H --host=[USER@]HOST Operate on remote host\n"
427 " -M --machine=CONTAINER Operate on local container\n"
428 " --no-convert Don't convert keyboard mappings\n\n"
430 " status Show current locale settings\n"
431 " set-locale LOCALE... Set system locale\n"
432 " list-locales Show known locales\n"
433 " set-keymap MAP [MAP] Set console and X11 keyboard mappings\n"
434 " list-keymaps Show known virtual console keyboard mappings\n"
435 " set-x11-keymap LAYOUT [MODEL [VARIANT [OPTIONS]]]\n"
436 " Set X11 and console keyboard mappings\n"
437 " list-x11-keymap-models Show known X11 keyboard mapping models\n"
438 " list-x11-keymap-layouts Show known X11 keyboard mapping layouts\n"
439 " list-x11-keymap-variants [LAYOUT]\n"
440 " Show known X11 keyboard mapping variants\n"
441 " list-x11-keymap-options Show known X11 keyboard mapping options\n"
442 , program_invocation_short_name
);
445 static int parse_argv(int argc
, char *argv
[]) {
454 static const struct option options
[] = {
455 { "help", no_argument
, NULL
, 'h' },
456 { "version", no_argument
, NULL
, ARG_VERSION
},
457 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
458 { "host", required_argument
, NULL
, 'H' },
459 { "machine", required_argument
, NULL
, 'M' },
460 { "no-ask-password", no_argument
, NULL
, ARG_NO_ASK_PASSWORD
},
461 { "no-convert", no_argument
, NULL
, ARG_NO_CONVERT
},
470 while ((c
= getopt_long(argc
, argv
, "hH:M:", options
, NULL
)) >= 0)
489 case ARG_NO_ASK_PASSWORD
:
490 arg_ask_password
= false;
494 arg_transport
= BUS_TRANSPORT_REMOTE
;
499 arg_transport
= BUS_TRANSPORT_MACHINE
;
507 assert_not_reached("Unhandled option");
513 static int localectl_main(sd_bus
*bus
, int argc
, char *argv
[]) {
515 static const struct {
523 int (* const dispatch
)(sd_bus
*bus
, char **args
, unsigned n
);
525 { "status", LESS
, 1, show_status
},
526 { "set-locale", MORE
, 2, set_locale
},
527 { "list-locales", EQUAL
, 1, list_locales
},
528 { "set-keymap", MORE
, 2, set_vconsole_keymap
},
529 { "list-keymaps", EQUAL
, 1, list_vconsole_keymaps
},
530 { "set-x11-keymap", MORE
, 2, set_x11_keymap
},
531 { "list-x11-keymap-models", EQUAL
, 1, list_x11_keymaps
},
532 { "list-x11-keymap-layouts", EQUAL
, 1, list_x11_keymaps
},
533 { "list-x11-keymap-variants", LESS
, 2, list_x11_keymaps
},
534 { "list-x11-keymap-options", EQUAL
, 1, list_x11_keymaps
},
543 left
= argc
- optind
;
546 /* Special rule: no arguments means "status" */
549 if (streq(argv
[optind
], "help")) {
554 for (i
= 0; i
< ELEMENTSOF(verbs
); i
++)
555 if (streq(argv
[optind
], verbs
[i
].verb
))
558 if (i
>= ELEMENTSOF(verbs
)) {
559 log_error("Unknown operation %s", argv
[optind
]);
564 switch (verbs
[i
].argc_cmp
) {
567 if (left
!= verbs
[i
].argc
) {
568 log_error("Invalid number of arguments.");
575 if (left
< verbs
[i
].argc
) {
576 log_error("Too few arguments.");
583 if (left
> verbs
[i
].argc
) {
584 log_error("Too many arguments.");
591 assert_not_reached("Unknown comparison operator.");
594 return verbs
[i
].dispatch(bus
, argv
+ optind
, left
);
597 int main(int argc
, char*argv
[]) {
598 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
601 setlocale(LC_ALL
, "");
602 log_parse_environment();
605 r
= parse_argv(argc
, argv
);
609 r
= bus_connect_transport(arg_transport
, arg_host
, false, &bus
);
611 log_error_errno(r
, "Failed to create bus connection: %m");
615 r
= localectl_main(bus
, argc
, argv
);
620 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;