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