]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/locale/localectl.c
b60da006b3f7cd50ce039f2da5befda473e824c8
[thirdparty/systemd.git] / src / locale / localectl.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <getopt.h>
4
5 #include "sd-bus.h"
6
7 #include "alloc-util.h"
8 #include "build.h"
9 #include "bus-error.h"
10 #include "bus-locator.h"
11 #include "bus-map-properties.h"
12 #include "bus-util.h"
13 #include "fd-util.h"
14 #include "fileio.h"
15 #include "format-table.h"
16 #include "kbd-util.h"
17 #include "locale-setup.h"
18 #include "main-func.h"
19 #include "memory-util.h"
20 #include "pager.h"
21 #include "parse-argument.h"
22 #include "polkit-agent.h"
23 #include "pretty-print.h"
24 #include "runtime-scope.h"
25 #include "string-util.h"
26 #include "strv.h"
27 #include "time-util.h"
28 #include "verbs.h"
29
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
33 static PagerFlags arg_pager_flags = 0;
34 static bool arg_ask_password = true;
35 static BusTransport arg_transport = BUS_TRANSPORT_LOCAL;
36 static const char *arg_host = NULL;
37 static bool arg_convert = true;
38 static bool arg_full = false;
39
40 typedef struct StatusInfo {
41 char **locale;
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;
48 } StatusInfo;
49
50 static void status_info_clear(StatusInfo *info) {
51 if (info) {
52 strv_free(info->locale);
53 zero(*info);
54 }
55 }
56
57 static int print_status_info(StatusInfo *i) {
58 _cleanup_strv_free_ char **kernel_locale = NULL;
59 _cleanup_(table_unrefp) Table *table = NULL;
60 TableCell *cell;
61 int r;
62
63 assert(i);
64
65 if (arg_transport == BUS_TRANSPORT_LOCAL) {
66 _cleanup_(locale_context_clear) LocaleContext c = {};
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 }
76
77 table = table_new_vertical();
78 if (!table)
79 return log_oom();
80
81 if (arg_full)
82 table_set_width(table, 0);
83
84 assert_se(cell = table_get_cell(table, 0, 0));
85 (void) table_set_ellipsize_percent(table, cell, 100);
86
87 table_set_ersatz_string(table, TABLE_ERSATZ_UNSET);
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,
92 TABLE_FIELD, "Command Line",
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 }
99
100 r = table_add_many(table,
101 TABLE_FIELD, "System Locale",
102 TABLE_STRV, i->locale,
103 TABLE_FIELD, "VC Keymap",
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,
110 TABLE_FIELD, "VC Toggle Keymap",
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,
117 TABLE_FIELD, "X11 Layout",
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,
124 TABLE_FIELD, "X11 Model",
125 TABLE_STRING, i->x11_model);
126 if (r < 0)
127 return table_log_add_error(r);
128 }
129
130 if (!isempty(i->x11_variant)) {
131 r = table_add_many(table,
132 TABLE_FIELD, "X11 Variant",
133 TABLE_STRING, i->x11_variant);
134 if (r < 0)
135 return table_log_add_error(r);
136 }
137
138 if (!isempty(i->x11_options)) {
139 r = table_add_many(table,
140 TABLE_FIELD, "X11 Options",
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;
151 }
152
153 static int show_status(int argc, char **argv, void *userdata) {
154 _cleanup_(status_info_clear) StatusInfo info = {};
155 static const struct bus_properties_map map[] = {
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) },
163 {}
164 };
165
166 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
167 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
168 sd_bus *bus = ASSERT_PTR(userdata);
169 int r;
170
171 r = bus_map_all_properties(bus,
172 "org.freedesktop.locale1",
173 "/org/freedesktop/locale1",
174 map,
175 0,
176 &error,
177 &m,
178 &info);
179 if (r < 0)
180 return log_error_errno(r, "Could not get properties: %s", bus_error_message(&error, r));
181
182 return print_status_info(&info);
183 }
184
185 static int set_locale(int argc, char **argv, void *userdata) {
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;
188 sd_bus *bus = ASSERT_PTR(userdata);
189 int r;
190
191 (void) polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
192
193 r = bus_message_new_method_call(bus, &m, bus_locale, "SetLocale");
194 if (r < 0)
195 return bus_log_create_error(r);
196
197 r = sd_bus_message_append_strv(m, argv + 1);
198 if (r < 0)
199 return bus_log_create_error(r);
200
201 r = sd_bus_message_append(m, "b", arg_ask_password);
202 if (r < 0)
203 return bus_log_create_error(r);
204
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);
207 if (r < 0)
208 return log_error_errno(r, "Failed to issue method call: %s", bus_error_message(&error, r));
209
210 return 0;
211 }
212
213 static int list_locales(int argc, char **argv, void *userdata) {
214 _cleanup_strv_free_ char **l = NULL;
215 int r;
216
217 r = get_locales(&l);
218 if (r < 0)
219 return log_error_errno(r, "Failed to read list of locales: %m");
220
221 pager_open(arg_pager_flags);
222 strv_print(l);
223
224 return 0;
225 }
226
227 static int set_vconsole_keymap(int argc, char **argv, void *userdata) {
228 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
229 const char *map, *toggle_map;
230 sd_bus *bus = ASSERT_PTR(userdata);
231 int r;
232
233 (void) polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
234
235 map = argv[1];
236 toggle_map = argc > 2 ? argv[2] : "";
237
238 r = bus_call_method(
239 bus,
240 bus_locale,
241 "SetVConsoleKeyboard",
242 &error,
243 NULL,
244 "ssbb", map, toggle_map, arg_convert, arg_ask_password);
245 if (r < 0)
246 return log_error_errno(r, "Failed to set keymap: %s", bus_error_message(&error, r));
247
248 return 0;
249 }
250
251 static int list_vconsole_keymaps(int argc, char **argv, void *userdata) {
252 _cleanup_strv_free_ char **l = NULL;
253 int r;
254
255 r = get_keymaps(&l);
256 if (r < 0)
257 return log_error_errno(r, "Failed to read list of keymaps: %m");
258
259 pager_open(arg_pager_flags);
260
261 strv_print(l);
262
263 return 0;
264 }
265
266 static int set_x11_keymap(int argc, char **argv, void *userdata) {
267 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
268 const char *layout, *model, *variant, *options;
269 sd_bus *bus = userdata;
270 int r;
271
272 (void) polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
273
274 layout = argv[1];
275 model = argc > 2 ? argv[2] : "";
276 variant = argc > 3 ? argv[3] : "";
277 options = argc > 4 ? argv[4] : "";
278
279 r = bus_call_method(
280 bus,
281 bus_locale,
282 "SetX11Keyboard",
283 &error,
284 NULL,
285 "ssssbb", layout, model, variant, options,
286 arg_convert, arg_ask_password);
287 if (r < 0)
288 return log_error_errno(r, "Failed to set keymap: %s", bus_error_message(&error, r));
289
290 return 0;
291 }
292
293 static int list_x11_keymaps(int argc, char **argv, void *userdata) {
294 _cleanup_fclose_ FILE *f = NULL;
295 _cleanup_strv_free_ char **list = NULL;
296 enum {
297 NONE,
298 MODELS,
299 LAYOUTS,
300 VARIANTS,
301 OPTIONS
302 } state = NONE, look_for;
303 int r;
304
305 f = fopen("/usr/share/X11/xkb/rules/base.lst", "re");
306 if (!f)
307 return log_error_errno(errno, "Failed to open keyboard mapping list. %m");
308
309 if (streq(argv[0], "list-x11-keymap-models"))
310 look_for = MODELS;
311 else if (streq(argv[0], "list-x11-keymap-layouts"))
312 look_for = LAYOUTS;
313 else if (streq(argv[0], "list-x11-keymap-variants"))
314 look_for = VARIANTS;
315 else if (streq(argv[0], "list-x11-keymap-options"))
316 look_for = OPTIONS;
317 else
318 assert_not_reached();
319
320 for (;;) {
321 _cleanup_free_ char *line = NULL;
322 char *w;
323
324 r = read_stripped_line(f, LONG_LINE_MAX, &line);
325 if (r < 0)
326 return log_error_errno(r, "Failed to read keyboard mapping list: %m");
327 if (r == 0)
328 break;
329
330 if (isempty(line))
331 continue;
332
333 if (line[0] == '!') {
334 if (startswith(line, "! model"))
335 state = MODELS;
336 else if (startswith(line, "! layout"))
337 state = LAYOUTS;
338 else if (startswith(line, "! variant"))
339 state = VARIANTS;
340 else if (startswith(line, "! option"))
341 state = OPTIONS;
342 else
343 state = NONE;
344
345 continue;
346 }
347
348 if (state != look_for)
349 continue;
350
351 w = line + strcspn(line, WHITESPACE);
352
353 if (argc > 1) {
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
369 if (!streq(w, argv[1]))
370 continue;
371 } else
372 *w = 0;
373
374 if (strv_consume(&list, TAKE_PTR(line)) < 0)
375 return log_oom();
376 }
377
378 if (strv_isempty(list))
379 return log_error_errno(SYNTHETIC_ERRNO(ENOENT),
380 "Couldn't find any entries.");
381
382 strv_sort_uniq(list);
383
384 pager_open(arg_pager_flags);
385
386 strv_print(list);
387 return 0;
388 }
389
390 static int help(void) {
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
398 printf("%s [OPTIONS...] COMMAND ...\n\n"
399 "%sQuery or change system locale and keyboard settings.%s\n"
400 "\nCommands:\n"
401 " status Show current locale settings\n"
402 " set-locale LOCALE... Set system locale\n"
403 " list-locales Show known locales\n"
404 " set-keymap MAP [MAP] Set console and X11 keyboard mappings\n"
405 " list-keymaps Show known virtual console keyboard mappings\n"
406 " set-x11-keymap LAYOUT [MODEL [VARIANT [OPTIONS]]]\n"
407 " Set X11 and console keyboard mappings\n"
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"
412 " list-x11-keymap-options Show known X11 keyboard mapping options\n"
413 "\nOptions:\n"
414 " -h --help Show this help\n"
415 " --version Show package version\n"
416 " -l --full Do not ellipsize output\n"
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"
422 "\nSee the %s for details.\n",
423 program_invocation_short_name,
424 ansi_highlight(),
425 ansi_normal(),
426 link);
427
428 return 0;
429 }
430
431 static int verb_help(int argc, char **argv, void *userdata) {
432 return help();
433 }
434
435 static 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[] = {
445 { "help", no_argument, NULL, 'h' },
446 { "version", no_argument, NULL, ARG_VERSION },
447 { "full", no_argument, NULL, 'l' },
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 },
453 {}
454 };
455
456 int r, c;
457
458 assert(argc >= 0);
459 assert(argv);
460
461 while ((c = getopt_long(argc, argv, "hlH:M:", options, NULL)) >= 0)
462
463 switch (c) {
464
465 case 'h':
466 return help();
467
468 case ARG_VERSION:
469 return version();
470
471 case 'l':
472 arg_full = true;
473 break;
474
475 case ARG_NO_CONVERT:
476 arg_convert = false;
477 break;
478
479 case ARG_NO_PAGER:
480 arg_pager_flags |= PAGER_DISABLE;
481 break;
482
483 case ARG_NO_ASK_PASSWORD:
484 arg_ask_password = false;
485 break;
486
487 case 'H':
488 arg_transport = BUS_TRANSPORT_REMOTE;
489 arg_host = optarg;
490 break;
491
492 case 'M':
493 r = parse_machine_argument(optarg, &arg_host, &arg_transport);
494 if (r < 0)
495 return r;
496 break;
497
498 case '?':
499 return -EINVAL;
500
501 default:
502 assert_not_reached();
503 }
504
505 return 1;
506 }
507
508 static int localectl_main(sd_bus *bus, int argc, char *argv[]) {
509
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 {}
523 };
524
525 return dispatch_verb(argc, argv, verbs, bus);
526 }
527
528 static int run(int argc, char *argv[]) {
529 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
530 int r;
531
532 setlocale(LC_ALL, "");
533 log_setup();
534
535 r = parse_argv(argc, argv);
536 if (r <= 0)
537 return r;
538
539 r = bus_connect_transport(arg_transport, arg_host, RUNTIME_SCOPE_SYSTEM, &bus);
540 if (r < 0)
541 return bus_log_connect_error(r, arg_transport, RUNTIME_SCOPE_SYSTEM);
542
543 return localectl_main(bus, argc, argv);
544 }
545
546 DEFINE_MAIN_FUNCTION(run);