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