]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/locale/localectl.c
Merge pull request #11827 from keszybz/pkgconfig-variables
[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 "main-func.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 "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 = 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 if (r < 0)
175 return bus_log_create_error(r);
176
177 r = sd_bus_message_append_strv(m, argv + 1);
178 if (r < 0)
179 return bus_log_create_error(r);
180
181 r = sd_bus_message_append(m, "b", arg_ask_password);
182 if (r < 0)
183 return bus_log_create_error(r);
184
185 r = sd_bus_call(bus, m, 0, &error, NULL);
186 if (r < 0)
187 return log_error_errno(r, "Failed to issue method call: %s", bus_error_message(&error, -r));
188
189 return 0;
190 }
191
192 static int list_locales(int argc, char **argv, void *userdata) {
193 _cleanup_strv_free_ char **l = NULL;
194 int r;
195
196 r = get_locales(&l);
197 if (r < 0)
198 return log_error_errno(r, "Failed to read list of locales: %m");
199
200 (void) pager_open(arg_pager_flags);
201 strv_print(l);
202
203 return 0;
204 }
205
206 static int set_vconsole_keymap(int argc, char **argv, void *userdata) {
207 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
208 const char *map, *toggle_map;
209 sd_bus *bus = userdata;
210 int r;
211
212 assert(bus);
213
214 polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
215
216 map = argv[1];
217 toggle_map = argc > 2 ? argv[2] : "";
218
219 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 if (r < 0)
229 return log_error_errno(r, "Failed to set keymap: %s", bus_error_message(&error, -r));
230
231 return 0;
232 }
233
234 static int list_vconsole_keymaps(int argc, char **argv, void *userdata) {
235 _cleanup_strv_free_ char **l = NULL;
236 int r;
237
238 r = get_keymaps(&l);
239 if (r < 0)
240 return log_error_errno(r, "Failed to read list of keymaps: %m");
241
242 (void) pager_open(arg_pager_flags);
243
244 strv_print(l);
245
246 return 0;
247 }
248
249 static int set_x11_keymap(int argc, char **argv, void *userdata) {
250 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
251 const char *layout, *model, *variant, *options;
252 sd_bus *bus = userdata;
253 int r;
254
255 polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
256
257 layout = argv[1];
258 model = argc > 2 ? argv[2] : "";
259 variant = argc > 3 ? argv[3] : "";
260 options = argc > 4 ? argv[4] : "";
261
262 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 if (r < 0)
273 return log_error_errno(r, "Failed to set keymap: %s", bus_error_message(&error, -r));
274
275 return 0;
276 }
277
278 static int list_x11_keymaps(int argc, char **argv, void *userdata) {
279 _cleanup_fclose_ FILE *f = NULL;
280 _cleanup_strv_free_ char **list = NULL;
281 enum {
282 NONE,
283 MODELS,
284 LAYOUTS,
285 VARIANTS,
286 OPTIONS
287 } state = NONE, look_for;
288 int r;
289
290 f = fopen("/usr/share/X11/xkb/rules/base.lst", "re");
291 if (!f)
292 return log_error_errno(errno, "Failed to open keyboard mapping list. %m");
293
294 if (streq(argv[0], "list-x11-keymap-models"))
295 look_for = MODELS;
296 else if (streq(argv[0], "list-x11-keymap-layouts"))
297 look_for = LAYOUTS;
298 else if (streq(argv[0], "list-x11-keymap-variants"))
299 look_for = VARIANTS;
300 else if (streq(argv[0], "list-x11-keymap-options"))
301 look_for = OPTIONS;
302 else
303 assert_not_reached("Wrong parameter");
304
305 for (;;) {
306 _cleanup_free_ char *line = NULL;
307 char *l, *w;
308
309 r = read_line(f, LONG_LINE_MAX, &line);
310 if (r < 0)
311 return log_error_errno(r, "Failed to read keyboard mapping list: %m");
312 if (r == 0)
313 break;
314
315 l = strstrip(line);
316
317 if (isempty(l))
318 continue;
319
320 if (l[0] == '!') {
321 if (startswith(l, "! model"))
322 state = MODELS;
323 else if (startswith(l, "! layout"))
324 state = LAYOUTS;
325 else if (startswith(l, "! variant"))
326 state = VARIANTS;
327 else if (startswith(l, "! option"))
328 state = OPTIONS;
329 else
330 state = NONE;
331
332 continue;
333 }
334
335 if (state != look_for)
336 continue;
337
338 w = l + strcspn(l, WHITESPACE);
339
340 if (argc > 1) {
341 char *e;
342
343 if (*w == 0)
344 continue;
345
346 *w = 0;
347 w++;
348 w += strspn(w, WHITESPACE);
349
350 e = strchr(w, ':');
351 if (!e)
352 continue;
353
354 *e = 0;
355
356 if (!streq(w, argv[1]))
357 continue;
358 } else
359 *w = 0;
360
361 r = strv_extend(&list, l);
362 if (r < 0)
363 return log_oom();
364 }
365
366 if (strv_isempty(list))
367 return log_error_errno(SYNTHETIC_ERRNO(ENOENT),
368 "Couldn't find any entries.");
369
370 strv_sort(list);
371 strv_uniq(list);
372
373 (void) pager_open(arg_pager_flags);
374
375 strv_print(list);
376 return 0;
377 }
378
379 static int help(void) {
380 _cleanup_free_ char *link = NULL;
381 int r;
382
383 r = terminal_urlify_man("localectl", "1", &link);
384 if (r < 0)
385 return log_oom();
386
387 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 return 0;
415 }
416
417 static int verb_help(int argc, char **argv, void *userdata) {
418 return help();
419 }
420
421 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 assert(argc >= 0);
444 assert(argv);
445
446 while ((c = getopt_long(argc, argv, "hH:M:", options, NULL)) >= 0)
447
448 switch (c) {
449
450 case 'h':
451 return help();
452
453 case ARG_VERSION:
454 return version();
455
456 case ARG_NO_CONVERT:
457 arg_convert = false;
458 break;
459
460 case ARG_NO_PAGER:
461 arg_pager_flags |= PAGER_DISABLE;
462 break;
463
464 case ARG_NO_ASK_PASSWORD:
465 arg_ask_password = false;
466 break;
467
468 case 'H':
469 arg_transport = BUS_TRANSPORT_REMOTE;
470 arg_host = optarg;
471 break;
472
473 case 'M':
474 arg_transport = BUS_TRANSPORT_MACHINE;
475 arg_host = optarg;
476 break;
477
478 case '?':
479 return -EINVAL;
480
481 default:
482 assert_not_reached("Unhandled option");
483 }
484
485 return 1;
486 }
487
488 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 return dispatch_verb(argc, argv, verbs, bus);
506 }
507
508 static int run(int argc, char *argv[]) {
509 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
510 int r;
511
512 setlocale(LC_ALL, "");
513 log_parse_environment();
514 log_open();
515
516 r = parse_argv(argc, argv);
517 if (r <= 0)
518 return r;
519
520 r = bus_connect_transport(arg_transport, arg_host, false, &bus);
521 if (r < 0)
522 return log_error_errno(r, "Failed to create bus connection: %m");
523
524 return localectl_main(bus, argc, argv);
525 }
526
527 DEFINE_MAIN_FUNCTION(run);