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/>.
32 #include "bus-error.h"
34 #include "spawn-polkit-agent.h"
42 #include "locale-util.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 static void pager_open_if_enabled(void) {
58 static void polkit_agent_open_if_enabled(void) {
60 /* Open the polkit agent as a child process if necessary */
61 if (!arg_ask_password
)
64 if (arg_transport
!= BUS_TRANSPORT_LOCAL
)
70 typedef struct StatusInfo
{
72 char *vconsole_keymap
;
73 char *vconsole_keymap_toggle
;
80 static void status_info_clear(StatusInfo
*info
) {
82 strv_free(info
->locale
);
83 free(info
->vconsole_keymap
);
84 free(info
->vconsole_keymap_toggle
);
85 free(info
->x11_layout
);
86 free(info
->x11_model
);
87 free(info
->x11_variant
);
88 free(info
->x11_options
);
93 static void print_overridden_variables(void) {
95 char *variables
[_VARIABLE_LC_MAX
] = {};
97 bool print_warning
= true;
99 if (detect_container(NULL
) > 0 || arg_host
)
102 r
= parse_env_file("/proc/cmdline", WHITESPACE
,
103 "locale.LANG", &variables
[VARIABLE_LANG
],
104 "locale.LANGUAGE", &variables
[VARIABLE_LANGUAGE
],
105 "locale.LC_CTYPE", &variables
[VARIABLE_LC_CTYPE
],
106 "locale.LC_NUMERIC", &variables
[VARIABLE_LC_NUMERIC
],
107 "locale.LC_TIME", &variables
[VARIABLE_LC_TIME
],
108 "locale.LC_COLLATE", &variables
[VARIABLE_LC_COLLATE
],
109 "locale.LC_MONETARY", &variables
[VARIABLE_LC_MONETARY
],
110 "locale.LC_MESSAGES", &variables
[VARIABLE_LC_MESSAGES
],
111 "locale.LC_PAPER", &variables
[VARIABLE_LC_PAPER
],
112 "locale.LC_NAME", &variables
[VARIABLE_LC_NAME
],
113 "locale.LC_ADDRESS", &variables
[VARIABLE_LC_ADDRESS
],
114 "locale.LC_TELEPHONE", &variables
[VARIABLE_LC_TELEPHONE
],
115 "locale.LC_MEASUREMENT", &variables
[VARIABLE_LC_MEASUREMENT
],
116 "locale.LC_IDENTIFICATION", &variables
[VARIABLE_LC_IDENTIFICATION
],
119 if (r
< 0 && r
!= -ENOENT
) {
120 log_warning_errno(r
, "Failed to read /proc/cmdline: %m");
124 for (j
= 0; j
< _VARIABLE_LC_MAX
; j
++)
127 log_warning("Warning: Settings on kernel command line override system locale settings in /etc/locale.conf.\n"
128 " Command Line: %s=%s", locale_variable_to_string(j
), variables
[j
]);
130 print_warning
= false;
132 log_warning(" %s=%s", locale_variable_to_string(j
), variables
[j
]);
135 for (j
= 0; j
< _VARIABLE_LC_MAX
; j
++)
139 static void print_status_info(StatusInfo
*i
) {
142 if (strv_isempty(i
->locale
))
143 puts(" System Locale: n/a\n");
147 printf(" System Locale: %s\n", i
->locale
[0]);
148 STRV_FOREACH(j
, i
->locale
+ 1)
152 printf(" VC Keymap: %s\n", strna(i
->vconsole_keymap
));
153 if (!isempty(i
->vconsole_keymap_toggle
))
154 printf("VC Toggle Keymap: %s\n", i
->vconsole_keymap_toggle
);
156 printf(" X11 Layout: %s\n", strna(i
->x11_layout
));
157 if (!isempty(i
->x11_model
))
158 printf(" X11 Model: %s\n", i
->x11_model
);
159 if (!isempty(i
->x11_variant
))
160 printf(" X11 Variant: %s\n", i
->x11_variant
);
161 if (!isempty(i
->x11_options
))
162 printf(" X11 Options: %s\n", i
->x11_options
);
165 static int show_status(sd_bus
*bus
, char **args
, unsigned n
) {
166 _cleanup_(status_info_clear
) StatusInfo info
= {};
167 static const struct bus_properties_map map
[] = {
168 { "VConsoleKeymap", "s", NULL
, offsetof(StatusInfo
, vconsole_keymap
) },
169 { "VConsoleKeymap", "s", NULL
, offsetof(StatusInfo
, vconsole_keymap
) },
170 { "VConsoleKeymapToggle", "s", NULL
, offsetof(StatusInfo
, vconsole_keymap_toggle
) },
171 { "X11Layout", "s", NULL
, offsetof(StatusInfo
, x11_layout
) },
172 { "X11Model", "s", NULL
, offsetof(StatusInfo
, x11_model
) },
173 { "X11Variant", "s", NULL
, offsetof(StatusInfo
, x11_variant
) },
174 { "X11Options", "s", NULL
, offsetof(StatusInfo
, x11_options
) },
175 { "Locale", "as", NULL
, offsetof(StatusInfo
, locale
) },
182 r
= bus_map_all_properties(bus
,
183 "org.freedesktop.locale1",
184 "/org/freedesktop/locale1",
188 return log_error_errno(r
, "Could not get properties: %m");
190 print_overridden_variables();
191 print_status_info(&info
);
196 static int set_locale(sd_bus
*bus
, char **args
, unsigned n
) {
197 _cleanup_bus_message_unref_ sd_bus_message
*m
= NULL
;
198 _cleanup_bus_error_free_ sd_bus_error error
= SD_BUS_ERROR_NULL
;
204 polkit_agent_open_if_enabled();
206 r
= sd_bus_message_new_method_call(
209 "org.freedesktop.locale1",
210 "/org/freedesktop/locale1",
211 "org.freedesktop.locale1",
214 return bus_log_create_error(r
);
216 r
= sd_bus_message_append_strv(m
, args
+ 1);
218 return bus_log_create_error(r
);
220 r
= sd_bus_message_append(m
, "b", arg_ask_password
);
222 return bus_log_create_error(r
);
224 r
= sd_bus_call(bus
, m
, 0, &error
, NULL
);
226 log_error("Failed to issue method call: %s", bus_error_message(&error
, -r
));
233 static int list_locales(sd_bus
*bus
, char **args
, unsigned n
) {
234 _cleanup_strv_free_
char **l
= NULL
;
241 return log_error_errno(r
, "Failed to read list of locales: %m");
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 return log_error_errno(r
, "Can't add keymap: %m");
319 static int list_vconsole_keymaps(sd_bus
*bus
, char **args
, unsigned n
) {
320 _cleanup_strv_free_
char **l
= NULL
;
323 keymaps
= set_new(&string_hash_ops
);
327 NULSTR_FOREACH(dir
, KBD_KEYMAP_DIRS
)
328 nftw(dir
, nftw_cb
, 20, FTW_MOUNT
|FTW_PHYS
);
330 l
= set_get_strv(keymaps
);
332 set_free_free(keymaps
);
338 if (strv_isempty(l
)) {
339 log_error("Couldn't find any console keymaps.");
345 pager_open_if_enabled();
352 static int set_x11_keymap(sd_bus
*bus
, char **args
, unsigned n
) {
353 _cleanup_bus_error_free_ sd_bus_error error
= SD_BUS_ERROR_NULL
;
354 const char *layout
, *model
, *variant
, *options
;
361 log_error("Too many arguments.");
365 polkit_agent_open_if_enabled();
368 model
= n
> 2 ? args
[2] : "";
369 variant
= n
> 3 ? args
[3] : "";
370 options
= n
> 4 ? args
[4] : "";
372 r
= sd_bus_call_method(
374 "org.freedesktop.locale1",
375 "/org/freedesktop/locale1",
376 "org.freedesktop.locale1",
380 "ssssbb", layout
, model
, variant
, options
,
381 arg_convert
, arg_ask_password
);
383 log_error("Failed to set keymap: %s", bus_error_message(&error
, -r
));
388 static int list_x11_keymaps(sd_bus
*bus
, char **args
, unsigned n
) {
389 _cleanup_fclose_
FILE *f
= NULL
;
390 _cleanup_strv_free_
char **list
= NULL
;
398 } state
= NONE
, look_for
;
402 log_error("Too many arguments.");
406 f
= fopen("/usr/share/X11/xkb/rules/base.lst", "re");
408 return log_error_errno(errno
, "Failed to open keyboard mapping list. %m");
410 if (streq(args
[0], "list-x11-keymap-models"))
412 else if (streq(args
[0], "list-x11-keymap-layouts"))
414 else if (streq(args
[0], "list-x11-keymap-variants"))
416 else if (streq(args
[0], "list-x11-keymap-options"))
419 assert_not_reached("Wrong parameter");
421 FOREACH_LINE(line
, f
, break) {
430 if (startswith(l
, "! model"))
432 else if (startswith(l
, "! layout"))
434 else if (startswith(l
, "! variant"))
436 else if (startswith(l
, "! option"))
444 if (state
!= look_for
)
447 w
= l
+ strcspn(l
, WHITESPACE
);
457 w
+= strspn(w
, WHITESPACE
);
465 if (!streq(w
, args
[1]))
470 r
= strv_extend(&list
, l
);
475 if (strv_isempty(list
)) {
476 log_error("Couldn't find any entries.");
483 pager_open_if_enabled();
489 static void help(void) {
490 printf("%s [OPTIONS...] COMMAND ...\n\n"
491 "Query or change system locale and keyboard settings.\n\n"
492 " -h --help Show this help\n"
493 " --version Show package version\n"
494 " --no-pager Do not pipe output into a pager\n"
495 " --no-ask-password Do not prompt for password\n"
496 " -H --host=[USER@]HOST Operate on remote host\n"
497 " -M --machine=CONTAINER Operate on local container\n"
498 " --no-convert Don't convert keyboard mappings\n\n"
500 " status Show current locale settings\n"
501 " set-locale LOCALE... Set system locale\n"
502 " list-locales Show known locales\n"
503 " set-keymap MAP [MAP] Set console and X11 keyboard mappings\n"
504 " list-keymaps Show known virtual console keyboard mappings\n"
505 " set-x11-keymap LAYOUT [MODEL [VARIANT [OPTIONS]]]\n"
506 " Set X11 and console keyboard mappings\n"
507 " list-x11-keymap-models Show known X11 keyboard mapping models\n"
508 " list-x11-keymap-layouts Show known X11 keyboard mapping layouts\n"
509 " list-x11-keymap-variants [LAYOUT]\n"
510 " Show known X11 keyboard mapping variants\n"
511 " list-x11-keymap-options Show known X11 keyboard mapping options\n"
512 , program_invocation_short_name
);
515 static int parse_argv(int argc
, char *argv
[]) {
524 static const struct option options
[] = {
525 { "help", no_argument
, NULL
, 'h' },
526 { "version", no_argument
, NULL
, ARG_VERSION
},
527 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
528 { "host", required_argument
, NULL
, 'H' },
529 { "machine", required_argument
, NULL
, 'M' },
530 { "no-ask-password", no_argument
, NULL
, ARG_NO_ASK_PASSWORD
},
531 { "no-convert", no_argument
, NULL
, ARG_NO_CONVERT
},
540 while ((c
= getopt_long(argc
, argv
, "hH:M:", options
, NULL
)) >= 0)
549 puts(PACKAGE_STRING
);
550 puts(SYSTEMD_FEATURES
);
561 case ARG_NO_ASK_PASSWORD
:
562 arg_ask_password
= false;
566 arg_transport
= BUS_TRANSPORT_REMOTE
;
571 arg_transport
= BUS_TRANSPORT_MACHINE
;
579 assert_not_reached("Unhandled option");
585 static int localectl_main(sd_bus
*bus
, int argc
, char *argv
[]) {
587 static const struct {
595 int (* const dispatch
)(sd_bus
*bus
, char **args
, unsigned n
);
597 { "status", LESS
, 1, show_status
},
598 { "set-locale", MORE
, 2, set_locale
},
599 { "list-locales", EQUAL
, 1, list_locales
},
600 { "set-keymap", MORE
, 2, set_vconsole_keymap
},
601 { "list-keymaps", EQUAL
, 1, list_vconsole_keymaps
},
602 { "set-x11-keymap", MORE
, 2, set_x11_keymap
},
603 { "list-x11-keymap-models", EQUAL
, 1, list_x11_keymaps
},
604 { "list-x11-keymap-layouts", EQUAL
, 1, list_x11_keymaps
},
605 { "list-x11-keymap-variants", LESS
, 2, list_x11_keymaps
},
606 { "list-x11-keymap-options", EQUAL
, 1, list_x11_keymaps
},
615 left
= argc
- optind
;
618 /* Special rule: no arguments means "status" */
621 if (streq(argv
[optind
], "help")) {
626 for (i
= 0; i
< ELEMENTSOF(verbs
); i
++)
627 if (streq(argv
[optind
], verbs
[i
].verb
))
630 if (i
>= ELEMENTSOF(verbs
)) {
631 log_error("Unknown operation %s", argv
[optind
]);
636 switch (verbs
[i
].argc_cmp
) {
639 if (left
!= verbs
[i
].argc
) {
640 log_error("Invalid number of arguments.");
647 if (left
< verbs
[i
].argc
) {
648 log_error("Too few arguments.");
655 if (left
> verbs
[i
].argc
) {
656 log_error("Too many arguments.");
663 assert_not_reached("Unknown comparison operator.");
666 return verbs
[i
].dispatch(bus
, argv
+ optind
, left
);
669 int main(int argc
, char*argv
[]) {
670 _cleanup_bus_flush_close_unref_ sd_bus
*bus
= NULL
;
673 setlocale(LC_ALL
, "");
674 log_parse_environment();
677 r
= parse_argv(argc
, argv
);
681 r
= bus_open_transport(arg_transport
, arg_host
, false, &bus
);
683 log_error_errno(r
, "Failed to create bus connection: %m");
687 r
= localectl_main(bus
, argc
, argv
);
692 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;