]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/locale/localectl.c
Merge pull request #32588 from CodethinkLabs/mkosi-selinux
[thirdparty/systemd.git] / src / locale / localectl.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
2087a7af 2
3f6fd1ba 3#include <getopt.h>
2087a7af 4#include <stdbool.h>
2087a7af 5
4d7859d1 6#include "sd-bus.h"
3f6fd1ba 7
d6b4d1c7 8#include "build.h"
4d7859d1 9#include "bus-error.h"
9b71e4ab 10#include "bus-locator.h"
807542be 11#include "bus-map-properties.h"
3ffd4af2 12#include "fd-util.h"
a3428668 13#include "fileio.h"
2b1eb5f8 14#include "format-table.h"
f05e1d0d 15#include "kbd-util.h"
3d36b5d7 16#include "locale-setup.h"
39daad0a 17#include "main-func.h"
0a970718 18#include "memory-util.h"
3f6fd1ba 19#include "pager.h"
294bf0c3 20#include "pretty-print.h"
01771226 21#include "proc-cmdline.h"
3f6fd1ba
LP
22#include "set.h"
23#include "spawn-polkit-agent.h"
24#include "strv.h"
ce2529b4 25#include "terminal-util.h"
1d4ecb98 26#include "verbs.h"
3f6fd1ba 27#include "virt.h"
2087a7af 28
8f20232f
MK
29/* Enough time for locale-gen to finish server-side (in case it is in use) */
30#define LOCALE_SLOW_BUS_CALL_TIMEOUT_USEC (2*USEC_PER_MINUTE)
31
0221d68a 32static PagerFlags arg_pager_flags = 0;
2087a7af 33static bool arg_ask_password = true;
4d7859d1 34static BusTransport arg_transport = BUS_TRANSPORT_LOCAL;
847da1ac 35static const char *arg_host = NULL;
2087a7af
LP
36static bool arg_convert = true;
37
2087a7af
LP
38typedef struct StatusInfo {
39 char **locale;
f37f8a61
YW
40 const char *vconsole_keymap;
41 const char *vconsole_keymap_toggle;
42 const char *x11_layout;
43 const char *x11_model;
44 const char *x11_variant;
45 const char *x11_options;
2087a7af
LP
46} StatusInfo;
47
e7e55dbd
DH
48static void status_info_clear(StatusInfo *info) {
49 if (info) {
50 strv_free(info->locale);
e7e55dbd
DH
51 zero(*info);
52 }
53}
54
2b1eb5f8
YW
55static int print_status_info(StatusInfo *i) {
56 _cleanup_strv_free_ char **kernel_locale = NULL;
57 _cleanup_(table_unrefp) Table *table = NULL;
58 TableCell *cell;
e6755a33 59 int r;
a3428668 60
2b1eb5f8
YW
61 assert(i);
62
63 if (arg_transport == BUS_TRANSPORT_LOCAL) {
56b7f112 64 _cleanup_(locale_context_clear) LocaleContext c = {};
2b1eb5f8
YW
65
66 r = locale_context_load(&c, LOCALE_LOAD_PROC_CMDLINE);
67 if (r < 0)
68 return log_error_errno(r, "Failed to read /proc/cmdline: %m");
69
70 r = locale_context_build_env(&c, &kernel_locale, NULL);
71 if (r < 0)
72 return log_error_errno(r, "Failed to build locale settings from kernel command line: %m");
73 }
a3428668 74
c62ca826 75 table = table_new_vertical();
2b1eb5f8
YW
76 if (!table)
77 return log_oom();
a3428668 78
2b1eb5f8
YW
79 assert_se(cell = table_get_cell(table, 0, 0));
80 (void) table_set_ellipsize_percent(table, cell, 100);
2b1eb5f8 81
c8b62cf6 82 table_set_ersatz_string(table, TABLE_ERSATZ_UNSET);
2b1eb5f8
YW
83
84 if (!strv_isempty(kernel_locale)) {
85 log_warning("Warning: Settings on kernel command line override system locale settings in /etc/locale.conf.");
86 r = table_add_many(table,
f0385769 87 TABLE_FIELD, "Command Line",
2b1eb5f8
YW
88 TABLE_SET_COLOR, ansi_highlight_yellow(),
89 TABLE_STRV, kernel_locale,
90 TABLE_SET_COLOR, ansi_highlight_yellow());
91 if (r < 0)
92 return table_log_add_error(r);
93 }
a3428668 94
2b1eb5f8 95 r = table_add_many(table,
c62ca826 96 TABLE_FIELD, "System Locale",
2b1eb5f8 97 TABLE_STRV, i->locale,
c62ca826 98 TABLE_FIELD, "VC Keymap",
2b1eb5f8
YW
99 TABLE_STRING, i->vconsole_keymap);
100 if (r < 0)
101 return table_log_add_error(r);
102
103 if (!isempty(i->vconsole_keymap_toggle)) {
104 r = table_add_many(table,
c62ca826 105 TABLE_FIELD, "VC Toggle Keymap",
2b1eb5f8
YW
106 TABLE_STRING, i->vconsole_keymap_toggle);
107 if (r < 0)
108 return table_log_add_error(r);
109 }
110
111 r = table_add_many(table,
c62ca826 112 TABLE_FIELD, "X11 Layout",
2b1eb5f8
YW
113 TABLE_STRING, i->x11_layout);
114 if (r < 0)
115 return table_log_add_error(r);
116
117 if (!isempty(i->x11_model)) {
118 r = table_add_many(table,
c62ca826 119 TABLE_FIELD, "X11 Model",
2b1eb5f8
YW
120 TABLE_STRING, i->x11_model);
121 if (r < 0)
122 return table_log_add_error(r);
123 }
2087a7af 124
2b1eb5f8
YW
125 if (!isempty(i->x11_variant)) {
126 r = table_add_many(table,
c62ca826 127 TABLE_FIELD, "X11 Variant",
2b1eb5f8
YW
128 TABLE_STRING, i->x11_variant);
129 if (r < 0)
130 return table_log_add_error(r);
2087a7af
LP
131 }
132
2b1eb5f8
YW
133 if (!isempty(i->x11_options)) {
134 r = table_add_many(table,
c62ca826 135 TABLE_FIELD, "X11 Options",
2b1eb5f8
YW
136 TABLE_STRING, i->x11_options);
137 if (r < 0)
138 return table_log_add_error(r);
139 }
140
141 r = table_print(table, NULL);
142 if (r < 0)
143 return table_log_print_error(r);
144
145 return 0;
2087a7af
LP
146}
147
1d4ecb98 148static int show_status(int argc, char **argv, void *userdata) {
e7e55dbd 149 _cleanup_(status_info_clear) StatusInfo info = {};
9f6eb1cd 150 static const struct bus_properties_map map[] = {
9f6eb1cd
KS
151 { "VConsoleKeymap", "s", NULL, offsetof(StatusInfo, vconsole_keymap) },
152 { "VConsoleKeymapToggle", "s", NULL, offsetof(StatusInfo, vconsole_keymap_toggle) },
153 { "X11Layout", "s", NULL, offsetof(StatusInfo, x11_layout) },
154 { "X11Model", "s", NULL, offsetof(StatusInfo, x11_model) },
155 { "X11Variant", "s", NULL, offsetof(StatusInfo, x11_variant) },
156 { "X11Options", "s", NULL, offsetof(StatusInfo, x11_options) },
157 { "Locale", "as", NULL, offsetof(StatusInfo, locale) },
ffc06c35
KS
158 {}
159 };
f9e0eefc
LP
160
161 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
f37f8a61 162 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
99534007 163 sd_bus *bus = ASSERT_PTR(userdata);
ffc06c35 164 int r;
2087a7af 165
ffc06c35
KS
166 r = bus_map_all_properties(bus,
167 "org.freedesktop.locale1",
168 "/org/freedesktop/locale1",
9f6eb1cd 169 map,
a7e4861c 170 0,
f9e0eefc 171 &error,
f37f8a61 172 &m,
9f6eb1cd 173 &info);
e7e55dbd 174 if (r < 0)
f9e0eefc 175 return log_error_errno(r, "Could not get properties: %s", bus_error_message(&error, r));
2087a7af 176
2b1eb5f8 177 return print_status_info(&info);
2087a7af
LP
178}
179
1d4ecb98 180static int set_locale(int argc, char **argv, void *userdata) {
4afd3348
LP
181 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
182 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
99534007 183 sd_bus *bus = ASSERT_PTR(userdata);
2087a7af
LP
184 int r;
185
8a4b13c5 186 polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
2087a7af 187
e3c8ec3b 188 r = bus_message_new_method_call(bus, &m, bus_locale, "SetLocale");
4d7859d1 189 if (r < 0)
94676f3e 190 return bus_log_create_error(r);
2087a7af 191
1d4ecb98 192 r = sd_bus_message_append_strv(m, argv + 1);
4d7859d1 193 if (r < 0)
94676f3e 194 return bus_log_create_error(r);
2087a7af 195
4d7859d1 196 r = sd_bus_message_append(m, "b", arg_ask_password);
2087a7af 197 if (r < 0)
94676f3e 198 return bus_log_create_error(r);
2087a7af 199
8f20232f
MK
200 /* We use a longer timeout for the method call in case localed is running locale-gen */
201 r = sd_bus_call(bus, m, LOCALE_SLOW_BUS_CALL_TIMEOUT_USEC, &error, NULL);
4ae25393 202 if (r < 0)
2a03b9ed 203 return log_error_errno(r, "Failed to issue method call: %s", bus_error_message(&error, r));
2087a7af 204
4d7859d1 205 return 0;
2087a7af
LP
206}
207
1d4ecb98 208static int list_locales(int argc, char **argv, void *userdata) {
17d33cec 209 _cleanup_strv_free_ char **l = NULL;
17d33cec
GC
210 int r;
211
75683450 212 r = get_locales(&l);
f647962d
MS
213 if (r < 0)
214 return log_error_errno(r, "Failed to read list of locales: %m");
2087a7af 215
384c2c32 216 pager_open(arg_pager_flags);
7c2d8094 217 strv_print(l);
2087a7af 218
bac3c8ee 219 return 0;
2087a7af
LP
220}
221
1d4ecb98 222static int set_vconsole_keymap(int argc, char **argv, void *userdata) {
4afd3348 223 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
2087a7af 224 const char *map, *toggle_map;
99534007 225 sd_bus *bus = ASSERT_PTR(userdata);
e1636421 226 int r;
2087a7af 227
8a4b13c5 228 polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
2087a7af 229
1d4ecb98
YW
230 map = argv[1];
231 toggle_map = argc > 2 ? argv[2] : "";
2087a7af 232
e3c8ec3b 233 r = bus_call_method(
e1636421 234 bus,
e3c8ec3b 235 bus_locale,
2087a7af 236 "SetVConsoleKeyboard",
4d7859d1 237 &error,
2087a7af 238 NULL,
4d7859d1 239 "ssbb", map, toggle_map, arg_convert, arg_ask_password);
e1636421 240 if (r < 0)
2a03b9ed 241 return log_error_errno(r, "Failed to set keymap: %s", bus_error_message(&error, r));
e1636421 242
4ae25393 243 return 0;
2087a7af
LP
244}
245
1d4ecb98 246static int list_vconsole_keymaps(int argc, char **argv, void *userdata) {
a017112b 247 _cleanup_strv_free_ char **l = NULL;
ed457f13 248 int r;
2087a7af 249
ed457f13
TB
250 r = get_keymaps(&l);
251 if (r < 0)
252 return log_error_errno(r, "Failed to read list of keymaps: %m");
2087a7af 253
384c2c32 254 pager_open(arg_pager_flags);
2087a7af 255
7c2d8094 256 strv_print(l);
2087a7af
LP
257
258 return 0;
259}
260
1d4ecb98 261static int set_x11_keymap(int argc, char **argv, void *userdata) {
4afd3348 262 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
2087a7af 263 const char *layout, *model, *variant, *options;
1d4ecb98 264 sd_bus *bus = userdata;
e1636421 265 int r;
2087a7af 266
8a4b13c5 267 polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
2087a7af 268
1d4ecb98
YW
269 layout = argv[1];
270 model = argc > 2 ? argv[2] : "";
271 variant = argc > 3 ? argv[3] : "";
272 options = argc > 4 ? argv[4] : "";
2087a7af 273
e3c8ec3b 274 r = bus_call_method(
e1636421 275 bus,
e3c8ec3b 276 bus_locale,
2087a7af 277 "SetX11Keyboard",
4d7859d1 278 &error,
2087a7af 279 NULL,
4d7859d1
KS
280 "ssssbb", layout, model, variant, options,
281 arg_convert, arg_ask_password);
e1636421 282 if (r < 0)
2a03b9ed 283 return log_error_errno(r, "Failed to set keymap: %s", bus_error_message(&error, r));
e1636421 284
4ae25393 285 return 0;
2087a7af
LP
286}
287
1d4ecb98 288static int list_x11_keymaps(int argc, char **argv, void *userdata) {
50cfc579 289 _cleanup_fclose_ FILE *f = NULL;
7fd1b19b 290 _cleanup_strv_free_ char **list = NULL;
50cfc579
LP
291 enum {
292 NONE,
293 MODELS,
294 LAYOUTS,
295 VARIANTS,
296 OPTIONS
297 } state = NONE, look_for;
298 int r;
299
c62e11ce 300 f = fopen("/usr/share/X11/xkb/rules/base.lst", "re");
4a62c710
MS
301 if (!f)
302 return log_error_errno(errno, "Failed to open keyboard mapping list. %m");
50cfc579 303
1d4ecb98 304 if (streq(argv[0], "list-x11-keymap-models"))
50cfc579 305 look_for = MODELS;
1d4ecb98 306 else if (streq(argv[0], "list-x11-keymap-layouts"))
50cfc579 307 look_for = LAYOUTS;
1d4ecb98 308 else if (streq(argv[0], "list-x11-keymap-variants"))
50cfc579 309 look_for = VARIANTS;
1d4ecb98 310 else if (streq(argv[0], "list-x11-keymap-options"))
50cfc579
LP
311 look_for = OPTIONS;
312 else
04499a70 313 assert_not_reached();
50cfc579 314
271c8ec5
LP
315 for (;;) {
316 _cleanup_free_ char *line = NULL;
0ff6ff2b 317 char *w;
50cfc579 318
0ff6ff2b 319 r = read_stripped_line(f, LONG_LINE_MAX, &line);
271c8ec5
LP
320 if (r < 0)
321 return log_error_errno(r, "Failed to read keyboard mapping list: %m");
322 if (r == 0)
323 break;
324
0ff6ff2b 325 if (isempty(line))
50cfc579
LP
326 continue;
327
0ff6ff2b
LP
328 if (line[0] == '!') {
329 if (startswith(line, "! model"))
50cfc579 330 state = MODELS;
0ff6ff2b 331 else if (startswith(line, "! layout"))
50cfc579 332 state = LAYOUTS;
0ff6ff2b 333 else if (startswith(line, "! variant"))
50cfc579 334 state = VARIANTS;
0ff6ff2b 335 else if (startswith(line, "! option"))
50cfc579
LP
336 state = OPTIONS;
337 else
338 state = NONE;
339
340 continue;
341 }
342
343 if (state != look_for)
344 continue;
345
0ff6ff2b 346 w = line + strcspn(line, WHITESPACE);
50cfc579 347
1d4ecb98 348 if (argc > 1) {
50cfc579
LP
349 char *e;
350
351 if (*w == 0)
352 continue;
353
354 *w = 0;
355 w++;
356 w += strspn(w, WHITESPACE);
357
358 e = strchr(w, ':');
359 if (!e)
360 continue;
361
362 *e = 0;
363
1d4ecb98 364 if (!streq(w, argv[1]))
50cfc579
LP
365 continue;
366 } else
367 *w = 0;
368
0ff6ff2b 369 if (strv_consume(&list, TAKE_PTR(line)) < 0)
7d6884b6 370 return log_oom();
50cfc579
LP
371 }
372
baaa35ad
ZJS
373 if (strv_isempty(list))
374 return log_error_errno(SYNTHETIC_ERRNO(ENOENT),
375 "Couldn't find any entries.");
50cfc579
LP
376
377 strv_sort(list);
378 strv_uniq(list);
379
384c2c32 380 pager_open(arg_pager_flags);
50cfc579
LP
381
382 strv_print(list);
383 return 0;
384}
385
1d4ecb98 386static int help(void) {
37ec0fdd
LP
387 _cleanup_free_ char *link = NULL;
388 int r;
389
390 r = terminal_urlify_man("localectl", "1", &link);
391 if (r < 0)
392 return log_oom();
393
353b2baa
LP
394 printf("%s [OPTIONS...] COMMAND ...\n\n"
395 "%sQuery or change system locale and keyboard settings.%s\n"
396 "\nCommands:\n"
50cfc579
LP
397 " status Show current locale settings\n"
398 " set-locale LOCALE... Set system locale\n"
399 " list-locales Show known locales\n"
2ebcf936 400 " set-keymap MAP [MAP] Set console and X11 keyboard mappings\n"
50cfc579 401 " list-keymaps Show known virtual console keyboard mappings\n"
31cf921a 402 " set-x11-keymap LAYOUT [MODEL [VARIANT [OPTIONS]]]\n"
2ebcf936 403 " Set X11 and console keyboard mappings\n"
50cfc579
LP
404 " list-x11-keymap-models Show known X11 keyboard mapping models\n"
405 " list-x11-keymap-layouts Show known X11 keyboard mapping layouts\n"
406 " list-x11-keymap-variants [LAYOUT]\n"
407 " Show known X11 keyboard mapping variants\n"
601185b4 408 " list-x11-keymap-options Show known X11 keyboard mapping options\n"
353b2baa
LP
409 "\nOptions:\n"
410 " -h --help Show this help\n"
411 " --version Show package version\n"
412 " --no-pager Do not pipe output into a pager\n"
413 " --no-ask-password Do not prompt for password\n"
414 " -H --host=[USER@]HOST Operate on remote host\n"
415 " -M --machine=CONTAINER Operate on local container\n"
416 " --no-convert Don't convert keyboard mappings\n"
bc556335
DDM
417 "\nSee the %s for details.\n",
418 program_invocation_short_name,
419 ansi_highlight(),
420 ansi_normal(),
421 link);
1d4ecb98
YW
422
423 return 0;
424}
425
426static int verb_help(int argc, char **argv, void *userdata) {
427 return help();
2087a7af
LP
428}
429
430static int parse_argv(int argc, char *argv[]) {
431
432 enum {
433 ARG_VERSION = 0x100,
434 ARG_NO_PAGER,
435 ARG_NO_CONVERT,
436 ARG_NO_ASK_PASSWORD
437 };
438
439 static const struct option options[] = {
4d7859d1
KS
440 { "help", no_argument, NULL, 'h' },
441 { "version", no_argument, NULL, ARG_VERSION },
442 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
443 { "host", required_argument, NULL, 'H' },
444 { "machine", required_argument, NULL, 'M' },
445 { "no-ask-password", no_argument, NULL, ARG_NO_ASK_PASSWORD },
446 { "no-convert", no_argument, NULL, ARG_NO_CONVERT },
eb9da376 447 {}
2087a7af
LP
448 };
449
450 int c;
451
452 assert(argc >= 0);
453 assert(argv);
454
601185b4 455 while ((c = getopt_long(argc, argv, "hH:M:", options, NULL)) >= 0)
2087a7af
LP
456
457 switch (c) {
458
459 case 'h':
1d4ecb98 460 return help();
2087a7af
LP
461
462 case ARG_VERSION:
3f6fd1ba 463 return version();
2087a7af 464
2087a7af
LP
465 case ARG_NO_CONVERT:
466 arg_convert = false;
467 break;
468
469 case ARG_NO_PAGER:
0221d68a 470 arg_pager_flags |= PAGER_DISABLE;
2087a7af
LP
471 break;
472
546158bc
JJ
473 case ARG_NO_ASK_PASSWORD:
474 arg_ask_password = false;
475 break;
476
4d7859d1
KS
477 case 'H':
478 arg_transport = BUS_TRANSPORT_REMOTE;
479 arg_host = optarg;
480 break;
481
482 case 'M':
de33fc62 483 arg_transport = BUS_TRANSPORT_MACHINE;
4d7859d1
KS
484 arg_host = optarg;
485 break;
486
2087a7af
LP
487 case '?':
488 return -EINVAL;
489
490 default:
04499a70 491 assert_not_reached();
2087a7af 492 }
2087a7af
LP
493
494 return 1;
495}
496
4d7859d1 497static int localectl_main(sd_bus *bus, int argc, char *argv[]) {
2087a7af 498
1d4ecb98
YW
499 static const Verb verbs[] = {
500 { "status", VERB_ANY, 1, VERB_DEFAULT, show_status },
501 { "set-locale", 2, VERB_ANY, 0, set_locale },
502 { "list-locales", VERB_ANY, 1, 0, list_locales },
503 { "set-keymap", 2, 3, 0, set_vconsole_keymap },
504 { "list-keymaps", VERB_ANY, 1, 0, list_vconsole_keymaps },
505 { "set-x11-keymap", 2, 5, 0, set_x11_keymap },
506 { "list-x11-keymap-models", VERB_ANY, 1, 0, list_x11_keymaps },
507 { "list-x11-keymap-layouts", VERB_ANY, 1, 0, list_x11_keymaps },
508 { "list-x11-keymap-variants", VERB_ANY, 2, 0, list_x11_keymaps },
509 { "list-x11-keymap-options", VERB_ANY, 1, 0, list_x11_keymaps },
510 { "help", VERB_ANY, VERB_ANY, 0, verb_help }, /* Not documented, but supported since it is created. */
511 {}
2087a7af
LP
512 };
513
1d4ecb98 514 return dispatch_verb(argc, argv, verbs, bus);
2087a7af
LP
515}
516
f2a3de01 517static int run(int argc, char *argv[]) {
39daad0a 518 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
84f6181c 519 int r;
2087a7af 520
a9cdc94f 521 setlocale(LC_ALL, "");
d2acb93d 522 log_setup();
2087a7af
LP
523
524 r = parse_argv(argc, argv);
84f6181c 525 if (r <= 0)
39daad0a 526 return r;
2087a7af 527
4870133b 528 r = bus_connect_transport(arg_transport, arg_host, RUNTIME_SCOPE_SYSTEM, &bus);
39daad0a 529 if (r < 0)
10a7340a 530 return bus_log_connect_error(r, arg_transport);
2087a7af 531
39daad0a 532 return localectl_main(bus, argc, argv);
2087a7af 533}
39daad0a
ZJS
534
535DEFINE_MAIN_FUNCTION(run);