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