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