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