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