]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/locale/localectl.c
tree-wide: add clickable man page link to all --help texts
[thirdparty/systemd.git] / src / locale / localectl.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
2087a7af 2
3f6fd1ba
LP
3#include <ftw.h>
4#include <getopt.h>
a9cdc94f 5#include <locale.h>
2087a7af 6#include <stdbool.h>
3f6fd1ba 7#include <stdlib.h>
2087a7af 8#include <string.h>
2087a7af 9
4d7859d1 10#include "sd-bus.h"
3f6fd1ba 11
4d7859d1 12#include "bus-error.h"
3f6fd1ba 13#include "bus-util.h"
0732ef7a 14#include "def.h"
3ffd4af2 15#include "fd-util.h"
a3428668 16#include "fileio.h"
75683450 17#include "locale-util.h"
3f6fd1ba
LP
18#include "pager.h"
19#include "set.h"
20#include "spawn-polkit-agent.h"
21#include "strv.h"
37ec0fdd 22#include "terminal-util.h"
3f6fd1ba 23#include "util.h"
1d4ecb98 24#include "verbs.h"
3f6fd1ba 25#include "virt.h"
2087a7af
LP
26
27static bool arg_no_pager = false;
2087a7af 28static bool arg_ask_password = true;
4d7859d1 29static BusTransport arg_transport = BUS_TRANSPORT_LOCAL;
7085053a 30static char *arg_host = NULL;
2087a7af
LP
31static bool arg_convert = true;
32
2087a7af
LP
33typedef struct StatusInfo {
34 char **locale;
f37f8a61
YW
35 const char *vconsole_keymap;
36 const char *vconsole_keymap_toggle;
37 const char *x11_layout;
38 const char *x11_model;
39 const char *x11_variant;
40 const char *x11_options;
2087a7af
LP
41} StatusInfo;
42
e7e55dbd
DH
43static void status_info_clear(StatusInfo *info) {
44 if (info) {
45 strv_free(info->locale);
e7e55dbd
DH
46 zero(*info);
47 }
48}
49
ff9b60f3 50static void print_overridden_variables(void) {
a3428668
MS
51 int r;
52 char *variables[_VARIABLE_LC_MAX] = {};
53 LocaleVariable j;
54 bool print_warning = true;
55
75f86906 56 if (detect_container() > 0 || arg_host)
a3428668
MS
57 return;
58
1a5a177e 59 r = parse_env_file(NULL, "/proc/cmdline", WHITESPACE,
a3428668
MS
60 "locale.LANG", &variables[VARIABLE_LANG],
61 "locale.LANGUAGE", &variables[VARIABLE_LANGUAGE],
62 "locale.LC_CTYPE", &variables[VARIABLE_LC_CTYPE],
63 "locale.LC_NUMERIC", &variables[VARIABLE_LC_NUMERIC],
64 "locale.LC_TIME", &variables[VARIABLE_LC_TIME],
65 "locale.LC_COLLATE", &variables[VARIABLE_LC_COLLATE],
66 "locale.LC_MONETARY", &variables[VARIABLE_LC_MONETARY],
67 "locale.LC_MESSAGES", &variables[VARIABLE_LC_MESSAGES],
68 "locale.LC_PAPER", &variables[VARIABLE_LC_PAPER],
69 "locale.LC_NAME", &variables[VARIABLE_LC_NAME],
70 "locale.LC_ADDRESS", &variables[VARIABLE_LC_ADDRESS],
71 "locale.LC_TELEPHONE", &variables[VARIABLE_LC_TELEPHONE],
72 "locale.LC_MEASUREMENT", &variables[VARIABLE_LC_MEASUREMENT],
73 "locale.LC_IDENTIFICATION", &variables[VARIABLE_LC_IDENTIFICATION],
74 NULL);
75
76 if (r < 0 && r != -ENOENT) {
da927ba9 77 log_warning_errno(r, "Failed to read /proc/cmdline: %m");
a3428668
MS
78 goto finish;
79 }
80
63229aa1 81 for (j = 0; j < _VARIABLE_LC_MAX; j++)
a3428668
MS
82 if (variables[j]) {
83 if (print_warning) {
b344bcbb 84 log_warning("Warning: Settings on kernel command line override system locale settings in /etc/locale.conf.\n"
c86b2d8f 85 " Command Line: %s=%s", locale_variable_to_string(j), variables[j]);
a3428668
MS
86
87 print_warning = false;
b344bcbb 88 } else
c86b2d8f 89 log_warning(" %s=%s", locale_variable_to_string(j), variables[j]);
a3428668
MS
90 }
91 finish:
63229aa1 92 for (j = 0; j < _VARIABLE_LC_MAX; j++)
a3428668
MS
93 free(variables[j]);
94}
95
2087a7af
LP
96static void print_status_info(StatusInfo *i) {
97 assert(i);
98
99 if (strv_isempty(i->locale))
a75db59c 100 puts(" System Locale: n/a");
2087a7af
LP
101 else {
102 char **j;
103
104 printf(" System Locale: %s\n", i->locale[0]);
105 STRV_FOREACH(j, i->locale + 1)
106 printf(" %s\n", *j);
107 }
108
109 printf(" VC Keymap: %s\n", strna(i->vconsole_keymap));
110 if (!isempty(i->vconsole_keymap_toggle))
111 printf("VC Toggle Keymap: %s\n", i->vconsole_keymap_toggle);
112
113 printf(" X11 Layout: %s\n", strna(i->x11_layout));
114 if (!isempty(i->x11_model))
115 printf(" X11 Model: %s\n", i->x11_model);
116 if (!isempty(i->x11_variant))
117 printf(" X11 Variant: %s\n", i->x11_variant);
118 if (!isempty(i->x11_options))
119 printf(" X11 Options: %s\n", i->x11_options);
120}
121
1d4ecb98 122static int show_status(int argc, char **argv, void *userdata) {
e7e55dbd 123 _cleanup_(status_info_clear) StatusInfo info = {};
9f6eb1cd 124 static const struct bus_properties_map map[] = {
9f6eb1cd
KS
125 { "VConsoleKeymap", "s", NULL, offsetof(StatusInfo, vconsole_keymap) },
126 { "VConsoleKeymapToggle", "s", NULL, offsetof(StatusInfo, vconsole_keymap_toggle) },
127 { "X11Layout", "s", NULL, offsetof(StatusInfo, x11_layout) },
128 { "X11Model", "s", NULL, offsetof(StatusInfo, x11_model) },
129 { "X11Variant", "s", NULL, offsetof(StatusInfo, x11_variant) },
130 { "X11Options", "s", NULL, offsetof(StatusInfo, x11_options) },
131 { "Locale", "as", NULL, offsetof(StatusInfo, locale) },
ffc06c35
KS
132 {}
133 };
f9e0eefc
LP
134
135 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
f37f8a61 136 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
1d4ecb98 137 sd_bus *bus = userdata;
ffc06c35 138 int r;
2087a7af 139
ffc06c35 140 assert(bus);
2087a7af 141
ffc06c35
KS
142 r = bus_map_all_properties(bus,
143 "org.freedesktop.locale1",
144 "/org/freedesktop/locale1",
9f6eb1cd 145 map,
a7e4861c 146 0,
f9e0eefc 147 &error,
f37f8a61 148 &m,
9f6eb1cd 149 &info);
e7e55dbd 150 if (r < 0)
f9e0eefc 151 return log_error_errno(r, "Could not get properties: %s", bus_error_message(&error, r));
2087a7af 152
ff9b60f3 153 print_overridden_variables();
2087a7af 154 print_status_info(&info);
4d7859d1 155
4d7859d1 156 return r;
2087a7af
LP
157}
158
1d4ecb98 159static int set_locale(int argc, char **argv, void *userdata) {
4afd3348
LP
160 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
161 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1d4ecb98 162 sd_bus *bus = userdata;
2087a7af
LP
163 int r;
164
165 assert(bus);
2087a7af 166
8a4b13c5 167 polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
2087a7af 168
151b9b96
LP
169 r = sd_bus_message_new_method_call(
170 bus,
171 &m,
2087a7af
LP
172 "org.freedesktop.locale1",
173 "/org/freedesktop/locale1",
174 "org.freedesktop.locale1",
151b9b96 175 "SetLocale");
4d7859d1 176 if (r < 0)
94676f3e 177 return bus_log_create_error(r);
2087a7af 178
1d4ecb98 179 r = sd_bus_message_append_strv(m, argv + 1);
4d7859d1 180 if (r < 0)
94676f3e 181 return bus_log_create_error(r);
2087a7af 182
4d7859d1 183 r = sd_bus_message_append(m, "b", arg_ask_password);
2087a7af 184 if (r < 0)
94676f3e 185 return bus_log_create_error(r);
2087a7af 186
c49b30a2 187 r = sd_bus_call(bus, m, 0, &error, NULL);
4ae25393
YW
188 if (r < 0)
189 return log_error_errno(r, "Failed to issue method call: %s", bus_error_message(&error, -r));
2087a7af 190
4d7859d1 191 return 0;
2087a7af
LP
192}
193
1d4ecb98 194static int list_locales(int argc, char **argv, void *userdata) {
17d33cec 195 _cleanup_strv_free_ char **l = NULL;
17d33cec
GC
196 int r;
197
75683450 198 r = get_locales(&l);
f647962d
MS
199 if (r < 0)
200 return log_error_errno(r, "Failed to read list of locales: %m");
2087a7af 201
ee5324aa 202 (void) pager_open(arg_no_pager, false);
7c2d8094 203 strv_print(l);
2087a7af 204
bac3c8ee 205 return 0;
2087a7af
LP
206}
207
1d4ecb98 208static int set_vconsole_keymap(int argc, char **argv, void *userdata) {
4afd3348 209 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
2087a7af 210 const char *map, *toggle_map;
1d4ecb98 211 sd_bus *bus = userdata;
e1636421 212 int r;
2087a7af
LP
213
214 assert(bus);
2087a7af 215
8a4b13c5 216 polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
2087a7af 217
1d4ecb98
YW
218 map = argv[1];
219 toggle_map = argc > 2 ? argv[2] : "";
2087a7af 220
e1636421
LP
221 r = sd_bus_call_method(
222 bus,
2087a7af
LP
223 "org.freedesktop.locale1",
224 "/org/freedesktop/locale1",
225 "org.freedesktop.locale1",
226 "SetVConsoleKeyboard",
4d7859d1 227 &error,
2087a7af 228 NULL,
4d7859d1 229 "ssbb", map, toggle_map, arg_convert, arg_ask_password);
e1636421 230 if (r < 0)
4ae25393 231 return log_error_errno(r, "Failed to set keymap: %s", bus_error_message(&error, -r));
e1636421 232
4ae25393 233 return 0;
2087a7af
LP
234}
235
1d4ecb98 236static int list_vconsole_keymaps(int argc, char **argv, void *userdata) {
a017112b 237 _cleanup_strv_free_ char **l = NULL;
ed457f13 238 int r;
2087a7af 239
ed457f13
TB
240 r = get_keymaps(&l);
241 if (r < 0)
242 return log_error_errno(r, "Failed to read list of keymaps: %m");
2087a7af 243
ee5324aa 244 (void) pager_open(arg_no_pager, false);
2087a7af 245
7c2d8094 246 strv_print(l);
2087a7af
LP
247
248 return 0;
249}
250
1d4ecb98 251static int set_x11_keymap(int argc, char **argv, void *userdata) {
4afd3348 252 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
2087a7af 253 const char *layout, *model, *variant, *options;
1d4ecb98 254 sd_bus *bus = userdata;
e1636421 255 int r;
2087a7af 256
8a4b13c5 257 polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
2087a7af 258
1d4ecb98
YW
259 layout = argv[1];
260 model = argc > 2 ? argv[2] : "";
261 variant = argc > 3 ? argv[3] : "";
262 options = argc > 4 ? argv[4] : "";
2087a7af 263
e1636421
LP
264 r = sd_bus_call_method(
265 bus,
2087a7af
LP
266 "org.freedesktop.locale1",
267 "/org/freedesktop/locale1",
268 "org.freedesktop.locale1",
269 "SetX11Keyboard",
4d7859d1 270 &error,
2087a7af 271 NULL,
4d7859d1
KS
272 "ssssbb", layout, model, variant, options,
273 arg_convert, arg_ask_password);
e1636421 274 if (r < 0)
4ae25393 275 return log_error_errno(r, "Failed to set keymap: %s", bus_error_message(&error, -r));
e1636421 276
4ae25393 277 return 0;
2087a7af
LP
278}
279
1d4ecb98 280static int list_x11_keymaps(int argc, char **argv, void *userdata) {
50cfc579 281 _cleanup_fclose_ FILE *f = NULL;
7fd1b19b 282 _cleanup_strv_free_ char **list = NULL;
50cfc579
LP
283 char line[LINE_MAX];
284 enum {
285 NONE,
286 MODELS,
287 LAYOUTS,
288 VARIANTS,
289 OPTIONS
290 } state = NONE, look_for;
291 int r;
292
c62e11ce 293 f = fopen("/usr/share/X11/xkb/rules/base.lst", "re");
4a62c710
MS
294 if (!f)
295 return log_error_errno(errno, "Failed to open keyboard mapping list. %m");
50cfc579 296
1d4ecb98 297 if (streq(argv[0], "list-x11-keymap-models"))
50cfc579 298 look_for = MODELS;
1d4ecb98 299 else if (streq(argv[0], "list-x11-keymap-layouts"))
50cfc579 300 look_for = LAYOUTS;
1d4ecb98 301 else if (streq(argv[0], "list-x11-keymap-variants"))
50cfc579 302 look_for = VARIANTS;
1d4ecb98 303 else if (streq(argv[0], "list-x11-keymap-options"))
50cfc579
LP
304 look_for = OPTIONS;
305 else
306 assert_not_reached("Wrong parameter");
307
308 FOREACH_LINE(line, f, break) {
309 char *l, *w;
310
311 l = strstrip(line);
312
313 if (isempty(l))
314 continue;
315
316 if (l[0] == '!') {
317 if (startswith(l, "! model"))
318 state = MODELS;
319 else if (startswith(l, "! layout"))
320 state = LAYOUTS;
321 else if (startswith(l, "! variant"))
322 state = VARIANTS;
323 else if (startswith(l, "! option"))
324 state = OPTIONS;
325 else
326 state = NONE;
327
328 continue;
329 }
330
331 if (state != look_for)
332 continue;
333
334 w = l + strcspn(l, WHITESPACE);
335
1d4ecb98 336 if (argc > 1) {
50cfc579
LP
337 char *e;
338
339 if (*w == 0)
340 continue;
341
342 *w = 0;
343 w++;
344 w += strspn(w, WHITESPACE);
345
346 e = strchr(w, ':');
347 if (!e)
348 continue;
349
350 *e = 0;
351
1d4ecb98 352 if (!streq(w, argv[1]))
50cfc579
LP
353 continue;
354 } else
355 *w = 0;
356
7d6884b6
TA
357 r = strv_extend(&list, l);
358 if (r < 0)
359 return log_oom();
50cfc579
LP
360 }
361
362 if (strv_isempty(list)) {
363 log_error("Couldn't find any entries.");
364 return -ENOENT;
365 }
366
367 strv_sort(list);
368 strv_uniq(list);
369
ee5324aa 370 (void) pager_open(arg_no_pager, false);
50cfc579
LP
371
372 strv_print(list);
373 return 0;
374}
375
1d4ecb98 376static int help(void) {
37ec0fdd
LP
377 _cleanup_free_ char *link = NULL;
378 int r;
379
380 r = terminal_urlify_man("localectl", "1", &link);
381 if (r < 0)
382 return log_oom();
383
2087a7af 384 printf("%s [OPTIONS...] COMMAND ...\n\n"
82c1d8f4 385 "Query or change system locale and keyboard settings.\n\n"
50cfc579
LP
386 " -h --help Show this help\n"
387 " --version Show package version\n"
50cfc579
LP
388 " --no-pager Do not pipe output into a pager\n"
389 " --no-ask-password Do not prompt for password\n"
4d7859d1 390 " -H --host=[USER@]HOST Operate on remote host\n"
a86a47ce
LP
391 " -M --machine=CONTAINER Operate on local container\n"
392 " --no-convert Don't convert keyboard mappings\n\n"
2087a7af 393 "Commands:\n"
50cfc579
LP
394 " status Show current locale settings\n"
395 " set-locale LOCALE... Set system locale\n"
396 " list-locales Show known locales\n"
2ebcf936 397 " set-keymap MAP [MAP] Set console and X11 keyboard mappings\n"
50cfc579 398 " list-keymaps Show known virtual console keyboard mappings\n"
31cf921a 399 " set-x11-keymap LAYOUT [MODEL [VARIANT [OPTIONS]]]\n"
2ebcf936 400 " Set X11 and console keyboard mappings\n"
50cfc579
LP
401 " list-x11-keymap-models Show known X11 keyboard mapping models\n"
402 " list-x11-keymap-layouts Show known X11 keyboard mapping layouts\n"
403 " list-x11-keymap-variants [LAYOUT]\n"
404 " Show known X11 keyboard mapping variants\n"
601185b4 405 " list-x11-keymap-options Show known X11 keyboard mapping options\n"
37ec0fdd
LP
406 "\nSee the %s for details.\n"
407 , program_invocation_short_name
408 , link
409 );
1d4ecb98
YW
410
411 return 0;
412}
413
414static int verb_help(int argc, char **argv, void *userdata) {
415 return help();
2087a7af
LP
416}
417
418static int parse_argv(int argc, char *argv[]) {
419
420 enum {
421 ARG_VERSION = 0x100,
422 ARG_NO_PAGER,
423 ARG_NO_CONVERT,
424 ARG_NO_ASK_PASSWORD
425 };
426
427 static const struct option options[] = {
4d7859d1
KS
428 { "help", no_argument, NULL, 'h' },
429 { "version", no_argument, NULL, ARG_VERSION },
430 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
431 { "host", required_argument, NULL, 'H' },
432 { "machine", required_argument, NULL, 'M' },
433 { "no-ask-password", no_argument, NULL, ARG_NO_ASK_PASSWORD },
434 { "no-convert", no_argument, NULL, ARG_NO_CONVERT },
eb9da376 435 {}
2087a7af
LP
436 };
437
438 int c;
439
440 assert(argc >= 0);
441 assert(argv);
442
601185b4 443 while ((c = getopt_long(argc, argv, "hH:M:", options, NULL)) >= 0)
2087a7af
LP
444
445 switch (c) {
446
447 case 'h':
1d4ecb98 448 return help();
2087a7af
LP
449
450 case ARG_VERSION:
3f6fd1ba 451 return version();
2087a7af 452
2087a7af
LP
453 case ARG_NO_CONVERT:
454 arg_convert = false;
455 break;
456
457 case ARG_NO_PAGER:
458 arg_no_pager = true;
459 break;
460
546158bc
JJ
461 case ARG_NO_ASK_PASSWORD:
462 arg_ask_password = false;
463 break;
464
4d7859d1
KS
465 case 'H':
466 arg_transport = BUS_TRANSPORT_REMOTE;
467 arg_host = optarg;
468 break;
469
470 case 'M':
de33fc62 471 arg_transport = BUS_TRANSPORT_MACHINE;
4d7859d1
KS
472 arg_host = optarg;
473 break;
474
2087a7af
LP
475 case '?':
476 return -EINVAL;
477
478 default:
eb9da376 479 assert_not_reached("Unhandled option");
2087a7af 480 }
2087a7af
LP
481
482 return 1;
483}
484
4d7859d1 485static int localectl_main(sd_bus *bus, int argc, char *argv[]) {
2087a7af 486
1d4ecb98
YW
487 static const Verb verbs[] = {
488 { "status", VERB_ANY, 1, VERB_DEFAULT, show_status },
489 { "set-locale", 2, VERB_ANY, 0, set_locale },
490 { "list-locales", VERB_ANY, 1, 0, list_locales },
491 { "set-keymap", 2, 3, 0, set_vconsole_keymap },
492 { "list-keymaps", VERB_ANY, 1, 0, list_vconsole_keymaps },
493 { "set-x11-keymap", 2, 5, 0, set_x11_keymap },
494 { "list-x11-keymap-models", VERB_ANY, 1, 0, list_x11_keymaps },
495 { "list-x11-keymap-layouts", VERB_ANY, 1, 0, list_x11_keymaps },
496 { "list-x11-keymap-variants", VERB_ANY, 2, 0, list_x11_keymaps },
497 { "list-x11-keymap-options", VERB_ANY, 1, 0, list_x11_keymaps },
498 { "help", VERB_ANY, VERB_ANY, 0, verb_help }, /* Not documented, but supported since it is created. */
499 {}
2087a7af
LP
500 };
501
1d4ecb98 502 return dispatch_verb(argc, argv, verbs, bus);
2087a7af
LP
503}
504
4d7859d1 505int main(int argc, char*argv[]) {
a3c56345 506 sd_bus *bus = NULL;
84f6181c 507 int r;
2087a7af 508
a9cdc94f 509 setlocale(LC_ALL, "");
2087a7af
LP
510 log_parse_environment();
511 log_open();
512
513 r = parse_argv(argc, argv);
84f6181c 514 if (r <= 0)
2087a7af 515 goto finish;
2087a7af 516
266f3e26 517 r = bus_connect_transport(arg_transport, arg_host, false, &bus);
4d7859d1 518 if (r < 0) {
da927ba9 519 log_error_errno(r, "Failed to create bus connection: %m");
4d7859d1 520 goto finish;
2087a7af
LP
521 }
522
4d7859d1 523 r = localectl_main(bus, argc, argv);
2087a7af 524
4d7859d1 525finish:
0a84daa5
FB
526 /* make sure we terminate the bus connection first, and then close the
527 * pager, see issue #3543 for the details. */
a3c56345 528 sd_bus_flush_close_unref(bus);
2087a7af
LP
529 pager_close();
530
84f6181c 531 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
2087a7af 532}