]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/locale/localectl.c
Merge pull request #18007 from fw-strlen/ipv6_masq_and_dnat
[thirdparty/systemd.git] / src / locale / localectl.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <ftw.h>
4 #include <getopt.h>
5 #include <stdbool.h>
6 #include <stdlib.h>
7
8 #include "sd-bus.h"
9
10 #include "bus-error.h"
11 #include "bus-locator.h"
12 #include "bus-map-properties.h"
13 #include "fd-util.h"
14 #include "fileio.h"
15 #include "kbd-util.h"
16 #include "locale-util.h"
17 #include "main-func.h"
18 #include "memory-util.h"
19 #include "pager.h"
20 #include "pretty-print.h"
21 #include "proc-cmdline.h"
22 #include "set.h"
23 #include "spawn-polkit-agent.h"
24 #include "strv.h"
25 #include "terminal-util.h"
26 #include "verbs.h"
27 #include "virt.h"
28
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
32 static PagerFlags arg_pager_flags = 0;
33 static bool arg_ask_password = true;
34 static BusTransport arg_transport = BUS_TRANSPORT_LOCAL;
35 static const char *arg_host = NULL;
36 static bool arg_convert = true;
37
38 typedef struct StatusInfo {
39 char **locale;
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;
46 } StatusInfo;
47
48 static void status_info_clear(StatusInfo *info) {
49 if (info) {
50 strv_free(info->locale);
51 zero(*info);
52 }
53 }
54
55 static void print_overridden_variables(void) {
56 _cleanup_(locale_variables_freep) char *variables[_VARIABLE_LC_MAX] = {};
57 bool print_warning = true;
58 int r;
59
60 if (arg_transport != BUS_TRANSPORT_LOCAL)
61 return;
62
63 r = proc_cmdline_get_key_many(
64 PROC_CMDLINE_STRIP_RD_PREFIX,
65 "locale.LANG", &variables[VARIABLE_LANG],
66 "locale.LANGUAGE", &variables[VARIABLE_LANGUAGE],
67 "locale.LC_CTYPE", &variables[VARIABLE_LC_CTYPE],
68 "locale.LC_NUMERIC", &variables[VARIABLE_LC_NUMERIC],
69 "locale.LC_TIME", &variables[VARIABLE_LC_TIME],
70 "locale.LC_COLLATE", &variables[VARIABLE_LC_COLLATE],
71 "locale.LC_MONETARY", &variables[VARIABLE_LC_MONETARY],
72 "locale.LC_MESSAGES", &variables[VARIABLE_LC_MESSAGES],
73 "locale.LC_PAPER", &variables[VARIABLE_LC_PAPER],
74 "locale.LC_NAME", &variables[VARIABLE_LC_NAME],
75 "locale.LC_ADDRESS", &variables[VARIABLE_LC_ADDRESS],
76 "locale.LC_TELEPHONE", &variables[VARIABLE_LC_TELEPHONE],
77 "locale.LC_MEASUREMENT", &variables[VARIABLE_LC_MEASUREMENT],
78 "locale.LC_IDENTIFICATION", &variables[VARIABLE_LC_IDENTIFICATION]);
79 if (r < 0 && r != -ENOENT) {
80 log_warning_errno(r, "Failed to read /proc/cmdline: %m");
81 return;
82 }
83
84 for (LocaleVariable j = 0; j < _VARIABLE_LC_MAX; j++)
85 if (variables[j]) {
86 if (print_warning) {
87 log_warning("Warning: Settings on kernel command line override system locale settings in /etc/locale.conf.\n"
88 " Command Line: %s=%s", locale_variable_to_string(j), variables[j]);
89
90 print_warning = false;
91 } else
92 log_warning(" %s=%s", locale_variable_to_string(j), variables[j]);
93 }
94 }
95
96 static void print_status_info(StatusInfo *i) {
97 assert(i);
98
99 if (strv_isempty(i->locale))
100 puts(" System Locale: n/a");
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
122 static int show_status(int argc, char **argv, void *userdata) {
123 _cleanup_(status_info_clear) StatusInfo info = {};
124 static const struct bus_properties_map map[] = {
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) },
132 {}
133 };
134
135 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
136 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
137 sd_bus *bus = userdata;
138 int r;
139
140 assert(bus);
141
142 r = bus_map_all_properties(bus,
143 "org.freedesktop.locale1",
144 "/org/freedesktop/locale1",
145 map,
146 0,
147 &error,
148 &m,
149 &info);
150 if (r < 0)
151 return log_error_errno(r, "Could not get properties: %s", bus_error_message(&error, r));
152
153 print_overridden_variables();
154 print_status_info(&info);
155
156 return r;
157 }
158
159 static int set_locale(int argc, char **argv, void *userdata) {
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;
162 sd_bus *bus = userdata;
163 int r;
164
165 assert(bus);
166
167 polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
168
169 r = bus_message_new_method_call(bus, &m, bus_locale, "SetLocale");
170 if (r < 0)
171 return bus_log_create_error(r);
172
173 r = sd_bus_message_append_strv(m, argv + 1);
174 if (r < 0)
175 return bus_log_create_error(r);
176
177 r = sd_bus_message_append(m, "b", arg_ask_password);
178 if (r < 0)
179 return bus_log_create_error(r);
180
181 /* We use a longer timeout for the method call in case localed is running locale-gen */
182 r = sd_bus_call(bus, m, LOCALE_SLOW_BUS_CALL_TIMEOUT_USEC, &error, NULL);
183 if (r < 0)
184 return log_error_errno(r, "Failed to issue method call: %s", bus_error_message(&error, r));
185
186 return 0;
187 }
188
189 static int list_locales(int argc, char **argv, void *userdata) {
190 _cleanup_strv_free_ char **l = NULL;
191 int r;
192
193 r = get_locales(&l);
194 if (r < 0)
195 return log_error_errno(r, "Failed to read list of locales: %m");
196
197 (void) pager_open(arg_pager_flags);
198 strv_print(l);
199
200 return 0;
201 }
202
203 static int set_vconsole_keymap(int argc, char **argv, void *userdata) {
204 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
205 const char *map, *toggle_map;
206 sd_bus *bus = userdata;
207 int r;
208
209 assert(bus);
210
211 polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
212
213 map = argv[1];
214 toggle_map = argc > 2 ? argv[2] : "";
215
216 r = bus_call_method(
217 bus,
218 bus_locale,
219 "SetVConsoleKeyboard",
220 &error,
221 NULL,
222 "ssbb", map, toggle_map, arg_convert, arg_ask_password);
223 if (r < 0)
224 return log_error_errno(r, "Failed to set keymap: %s", bus_error_message(&error, r));
225
226 return 0;
227 }
228
229 static int list_vconsole_keymaps(int argc, char **argv, void *userdata) {
230 _cleanup_strv_free_ char **l = NULL;
231 int r;
232
233 r = get_keymaps(&l);
234 if (r < 0)
235 return log_error_errno(r, "Failed to read list of keymaps: %m");
236
237 (void) pager_open(arg_pager_flags);
238
239 strv_print(l);
240
241 return 0;
242 }
243
244 static int set_x11_keymap(int argc, char **argv, void *userdata) {
245 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
246 const char *layout, *model, *variant, *options;
247 sd_bus *bus = userdata;
248 int r;
249
250 polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
251
252 layout = argv[1];
253 model = argc > 2 ? argv[2] : "";
254 variant = argc > 3 ? argv[3] : "";
255 options = argc > 4 ? argv[4] : "";
256
257 r = bus_call_method(
258 bus,
259 bus_locale,
260 "SetX11Keyboard",
261 &error,
262 NULL,
263 "ssssbb", layout, model, variant, options,
264 arg_convert, arg_ask_password);
265 if (r < 0)
266 return log_error_errno(r, "Failed to set keymap: %s", bus_error_message(&error, r));
267
268 return 0;
269 }
270
271 static int list_x11_keymaps(int argc, char **argv, void *userdata) {
272 _cleanup_fclose_ FILE *f = NULL;
273 _cleanup_strv_free_ char **list = NULL;
274 enum {
275 NONE,
276 MODELS,
277 LAYOUTS,
278 VARIANTS,
279 OPTIONS
280 } state = NONE, look_for;
281 int r;
282
283 f = fopen("/usr/share/X11/xkb/rules/base.lst", "re");
284 if (!f)
285 return log_error_errno(errno, "Failed to open keyboard mapping list. %m");
286
287 if (streq(argv[0], "list-x11-keymap-models"))
288 look_for = MODELS;
289 else if (streq(argv[0], "list-x11-keymap-layouts"))
290 look_for = LAYOUTS;
291 else if (streq(argv[0], "list-x11-keymap-variants"))
292 look_for = VARIANTS;
293 else if (streq(argv[0], "list-x11-keymap-options"))
294 look_for = OPTIONS;
295 else
296 assert_not_reached("Wrong parameter");
297
298 for (;;) {
299 _cleanup_free_ char *line = NULL;
300 char *l, *w;
301
302 r = read_line(f, LONG_LINE_MAX, &line);
303 if (r < 0)
304 return log_error_errno(r, "Failed to read keyboard mapping list: %m");
305 if (r == 0)
306 break;
307
308 l = strstrip(line);
309
310 if (isempty(l))
311 continue;
312
313 if (l[0] == '!') {
314 if (startswith(l, "! model"))
315 state = MODELS;
316 else if (startswith(l, "! layout"))
317 state = LAYOUTS;
318 else if (startswith(l, "! variant"))
319 state = VARIANTS;
320 else if (startswith(l, "! option"))
321 state = OPTIONS;
322 else
323 state = NONE;
324
325 continue;
326 }
327
328 if (state != look_for)
329 continue;
330
331 w = l + strcspn(l, WHITESPACE);
332
333 if (argc > 1) {
334 char *e;
335
336 if (*w == 0)
337 continue;
338
339 *w = 0;
340 w++;
341 w += strspn(w, WHITESPACE);
342
343 e = strchr(w, ':');
344 if (!e)
345 continue;
346
347 *e = 0;
348
349 if (!streq(w, argv[1]))
350 continue;
351 } else
352 *w = 0;
353
354 r = strv_extend(&list, l);
355 if (r < 0)
356 return log_oom();
357 }
358
359 if (strv_isempty(list))
360 return log_error_errno(SYNTHETIC_ERRNO(ENOENT),
361 "Couldn't find any entries.");
362
363 strv_sort(list);
364 strv_uniq(list);
365
366 (void) pager_open(arg_pager_flags);
367
368 strv_print(list);
369 return 0;
370 }
371
372 static int help(void) {
373 _cleanup_free_ char *link = NULL;
374 int r;
375
376 r = terminal_urlify_man("localectl", "1", &link);
377 if (r < 0)
378 return log_oom();
379
380 printf("%s [OPTIONS...] COMMAND ...\n\n"
381 "%sQuery or change system locale and keyboard settings.%s\n"
382 "\nCommands:\n"
383 " status Show current locale settings\n"
384 " set-locale LOCALE... Set system locale\n"
385 " list-locales Show known locales\n"
386 " set-keymap MAP [MAP] Set console and X11 keyboard mappings\n"
387 " list-keymaps Show known virtual console keyboard mappings\n"
388 " set-x11-keymap LAYOUT [MODEL [VARIANT [OPTIONS]]]\n"
389 " Set X11 and console keyboard mappings\n"
390 " list-x11-keymap-models Show known X11 keyboard mapping models\n"
391 " list-x11-keymap-layouts Show known X11 keyboard mapping layouts\n"
392 " list-x11-keymap-variants [LAYOUT]\n"
393 " Show known X11 keyboard mapping variants\n"
394 " list-x11-keymap-options Show known X11 keyboard mapping options\n"
395 "\nOptions:\n"
396 " -h --help Show this help\n"
397 " --version Show package version\n"
398 " --no-pager Do not pipe output into a pager\n"
399 " --no-ask-password Do not prompt for password\n"
400 " -H --host=[USER@]HOST Operate on remote host\n"
401 " -M --machine=CONTAINER Operate on local container\n"
402 " --no-convert Don't convert keyboard mappings\n"
403 "\nSee the %s for details.\n",
404 program_invocation_short_name,
405 ansi_highlight(),
406 ansi_normal(),
407 link);
408
409 return 0;
410 }
411
412 static int verb_help(int argc, char **argv, void *userdata) {
413 return help();
414 }
415
416 static int parse_argv(int argc, char *argv[]) {
417
418 enum {
419 ARG_VERSION = 0x100,
420 ARG_NO_PAGER,
421 ARG_NO_CONVERT,
422 ARG_NO_ASK_PASSWORD
423 };
424
425 static const struct option options[] = {
426 { "help", no_argument, NULL, 'h' },
427 { "version", no_argument, NULL, ARG_VERSION },
428 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
429 { "host", required_argument, NULL, 'H' },
430 { "machine", required_argument, NULL, 'M' },
431 { "no-ask-password", no_argument, NULL, ARG_NO_ASK_PASSWORD },
432 { "no-convert", no_argument, NULL, ARG_NO_CONVERT },
433 {}
434 };
435
436 int c;
437
438 assert(argc >= 0);
439 assert(argv);
440
441 while ((c = getopt_long(argc, argv, "hH:M:", options, NULL)) >= 0)
442
443 switch (c) {
444
445 case 'h':
446 return help();
447
448 case ARG_VERSION:
449 return version();
450
451 case ARG_NO_CONVERT:
452 arg_convert = false;
453 break;
454
455 case ARG_NO_PAGER:
456 arg_pager_flags |= PAGER_DISABLE;
457 break;
458
459 case ARG_NO_ASK_PASSWORD:
460 arg_ask_password = false;
461 break;
462
463 case 'H':
464 arg_transport = BUS_TRANSPORT_REMOTE;
465 arg_host = optarg;
466 break;
467
468 case 'M':
469 arg_transport = BUS_TRANSPORT_MACHINE;
470 arg_host = optarg;
471 break;
472
473 case '?':
474 return -EINVAL;
475
476 default:
477 assert_not_reached("Unhandled option");
478 }
479
480 return 1;
481 }
482
483 static int localectl_main(sd_bus *bus, int argc, char *argv[]) {
484
485 static const Verb verbs[] = {
486 { "status", VERB_ANY, 1, VERB_DEFAULT, show_status },
487 { "set-locale", 2, VERB_ANY, 0, set_locale },
488 { "list-locales", VERB_ANY, 1, 0, list_locales },
489 { "set-keymap", 2, 3, 0, set_vconsole_keymap },
490 { "list-keymaps", VERB_ANY, 1, 0, list_vconsole_keymaps },
491 { "set-x11-keymap", 2, 5, 0, set_x11_keymap },
492 { "list-x11-keymap-models", VERB_ANY, 1, 0, list_x11_keymaps },
493 { "list-x11-keymap-layouts", VERB_ANY, 1, 0, list_x11_keymaps },
494 { "list-x11-keymap-variants", VERB_ANY, 2, 0, list_x11_keymaps },
495 { "list-x11-keymap-options", VERB_ANY, 1, 0, list_x11_keymaps },
496 { "help", VERB_ANY, VERB_ANY, 0, verb_help }, /* Not documented, but supported since it is created. */
497 {}
498 };
499
500 return dispatch_verb(argc, argv, verbs, bus);
501 }
502
503 static int run(int argc, char *argv[]) {
504 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
505 int r;
506
507 setlocale(LC_ALL, "");
508 log_setup();
509
510 r = parse_argv(argc, argv);
511 if (r <= 0)
512 return r;
513
514 r = bus_connect_transport(arg_transport, arg_host, false, &bus);
515 if (r < 0)
516 return bus_log_connect_error(r);
517
518 return localectl_main(bus, argc, argv);
519 }
520
521 DEFINE_MAIN_FUNCTION(run);