]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/locale/localectl.c
Merge pull request #11827 from keszybz/pkgconfig-variables
[thirdparty/systemd.git] / src / locale / localectl.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
2087a7af 2
3f6fd1ba
LP
3#include <ftw.h>
4#include <getopt.h>
a9cdc94f 5#include <locale.h>
2087a7af 6#include <stdbool.h>
3f6fd1ba 7#include <stdlib.h>
2087a7af 8#include <string.h>
2087a7af 9
4d7859d1 10#include "sd-bus.h"
3f6fd1ba 11
4d7859d1 12#include "bus-error.h"
3f6fd1ba 13#include "bus-util.h"
0732ef7a 14#include "def.h"
3ffd4af2 15#include "fd-util.h"
a3428668 16#include "fileio.h"
75683450 17#include "locale-util.h"
39daad0a 18#include "main-func.h"
3f6fd1ba 19#include "pager.h"
294bf0c3 20#include "pretty-print.h"
01771226 21#include "proc-cmdline.h"
3f6fd1ba
LP
22#include "set.h"
23#include "spawn-polkit-agent.h"
24#include "strv.h"
25#include "util.h"
1d4ecb98 26#include "verbs.h"
3f6fd1ba 27#include "virt.h"
2087a7af 28
0221d68a 29static PagerFlags arg_pager_flags = 0;
2087a7af 30static bool arg_ask_password = true;
4d7859d1 31static BusTransport arg_transport = BUS_TRANSPORT_LOCAL;
847da1ac 32static const char *arg_host = NULL;
2087a7af
LP
33static bool arg_convert = true;
34
2087a7af
LP
35typedef struct StatusInfo {
36 char **locale;
f37f8a61
YW
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;
2087a7af
LP
43} StatusInfo;
44
e7e55dbd
DH
45static void status_info_clear(StatusInfo *info) {
46 if (info) {
47 strv_free(info->locale);
e7e55dbd
DH
48 zero(*info);
49 }
50}
51
ff9b60f3 52static void print_overridden_variables(void) {
e6755a33 53 _cleanup_(locale_variables_freep) char *variables[_VARIABLE_LC_MAX] = {};
a3428668 54 bool print_warning = true;
e6755a33
LP
55 LocaleVariable j;
56 int r;
a3428668 57
01771226 58 if (arg_transport != BUS_TRANSPORT_LOCAL)
a3428668
MS
59 return;
60
01771226
LP
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]);
a3428668 77 if (r < 0 && r != -ENOENT) {
da927ba9 78 log_warning_errno(r, "Failed to read /proc/cmdline: %m");
e6755a33 79 return;
a3428668
MS
80 }
81
63229aa1 82 for (j = 0; j < _VARIABLE_LC_MAX; j++)
a3428668
MS
83 if (variables[j]) {
84 if (print_warning) {
b344bcbb 85 log_warning("Warning: Settings on kernel command line override system locale settings in /etc/locale.conf.\n"
c86b2d8f 86 " Command Line: %s=%s", locale_variable_to_string(j), variables[j]);
a3428668
MS
87
88 print_warning = false;
b344bcbb 89 } else
c86b2d8f 90 log_warning(" %s=%s", locale_variable_to_string(j), variables[j]);
a3428668 91 }
a3428668
MS
92}
93
2087a7af
LP
94static void print_status_info(StatusInfo *i) {
95 assert(i);
96
97 if (strv_isempty(i->locale))
a75db59c 98 puts(" System Locale: n/a");
2087a7af
LP
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
1d4ecb98 120static int show_status(int argc, char **argv, void *userdata) {
e7e55dbd 121 _cleanup_(status_info_clear) StatusInfo info = {};
9f6eb1cd 122 static const struct bus_properties_map map[] = {
9f6eb1cd
KS
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) },
ffc06c35
KS
130 {}
131 };
f9e0eefc
LP
132
133 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
f37f8a61 134 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
1d4ecb98 135 sd_bus *bus = userdata;
ffc06c35 136 int r;
2087a7af 137
ffc06c35 138 assert(bus);
2087a7af 139
ffc06c35
KS
140 r = bus_map_all_properties(bus,
141 "org.freedesktop.locale1",
142 "/org/freedesktop/locale1",
9f6eb1cd 143 map,
a7e4861c 144 0,
f9e0eefc 145 &error,
f37f8a61 146 &m,
9f6eb1cd 147 &info);
e7e55dbd 148 if (r < 0)
f9e0eefc 149 return log_error_errno(r, "Could not get properties: %s", bus_error_message(&error, r));
2087a7af 150
ff9b60f3 151 print_overridden_variables();
2087a7af 152 print_status_info(&info);
4d7859d1 153
4d7859d1 154 return r;
2087a7af
LP
155}
156
1d4ecb98 157static int set_locale(int argc, char **argv, void *userdata) {
4afd3348
LP
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;
1d4ecb98 160 sd_bus *bus = userdata;
2087a7af
LP
161 int r;
162
163 assert(bus);
2087a7af 164
8a4b13c5 165 polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
2087a7af 166
151b9b96
LP
167 r = sd_bus_message_new_method_call(
168 bus,
169 &m,
2087a7af
LP
170 "org.freedesktop.locale1",
171 "/org/freedesktop/locale1",
172 "org.freedesktop.locale1",
151b9b96 173 "SetLocale");
4d7859d1 174 if (r < 0)
94676f3e 175 return bus_log_create_error(r);
2087a7af 176
1d4ecb98 177 r = sd_bus_message_append_strv(m, argv + 1);
4d7859d1 178 if (r < 0)
94676f3e 179 return bus_log_create_error(r);
2087a7af 180
4d7859d1 181 r = sd_bus_message_append(m, "b", arg_ask_password);
2087a7af 182 if (r < 0)
94676f3e 183 return bus_log_create_error(r);
2087a7af 184
c49b30a2 185 r = sd_bus_call(bus, m, 0, &error, NULL);
4ae25393
YW
186 if (r < 0)
187 return log_error_errno(r, "Failed to issue method call: %s", bus_error_message(&error, -r));
2087a7af 188
4d7859d1 189 return 0;
2087a7af
LP
190}
191
1d4ecb98 192static int list_locales(int argc, char **argv, void *userdata) {
17d33cec 193 _cleanup_strv_free_ char **l = NULL;
17d33cec
GC
194 int r;
195
75683450 196 r = get_locales(&l);
f647962d
MS
197 if (r < 0)
198 return log_error_errno(r, "Failed to read list of locales: %m");
2087a7af 199
0221d68a 200 (void) pager_open(arg_pager_flags);
7c2d8094 201 strv_print(l);
2087a7af 202
bac3c8ee 203 return 0;
2087a7af
LP
204}
205
1d4ecb98 206static int set_vconsole_keymap(int argc, char **argv, void *userdata) {
4afd3348 207 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
2087a7af 208 const char *map, *toggle_map;
1d4ecb98 209 sd_bus *bus = userdata;
e1636421 210 int r;
2087a7af
LP
211
212 assert(bus);
2087a7af 213
8a4b13c5 214 polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
2087a7af 215
1d4ecb98
YW
216 map = argv[1];
217 toggle_map = argc > 2 ? argv[2] : "";
2087a7af 218
e1636421
LP
219 r = sd_bus_call_method(
220 bus,
2087a7af
LP
221 "org.freedesktop.locale1",
222 "/org/freedesktop/locale1",
223 "org.freedesktop.locale1",
224 "SetVConsoleKeyboard",
4d7859d1 225 &error,
2087a7af 226 NULL,
4d7859d1 227 "ssbb", map, toggle_map, arg_convert, arg_ask_password);
e1636421 228 if (r < 0)
4ae25393 229 return log_error_errno(r, "Failed to set keymap: %s", bus_error_message(&error, -r));
e1636421 230
4ae25393 231 return 0;
2087a7af
LP
232}
233
1d4ecb98 234static int list_vconsole_keymaps(int argc, char **argv, void *userdata) {
a017112b 235 _cleanup_strv_free_ char **l = NULL;
ed457f13 236 int r;
2087a7af 237
ed457f13
TB
238 r = get_keymaps(&l);
239 if (r < 0)
240 return log_error_errno(r, "Failed to read list of keymaps: %m");
2087a7af 241
0221d68a 242 (void) pager_open(arg_pager_flags);
2087a7af 243
7c2d8094 244 strv_print(l);
2087a7af
LP
245
246 return 0;
247}
248
1d4ecb98 249static int set_x11_keymap(int argc, char **argv, void *userdata) {
4afd3348 250 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
2087a7af 251 const char *layout, *model, *variant, *options;
1d4ecb98 252 sd_bus *bus = userdata;
e1636421 253 int r;
2087a7af 254
8a4b13c5 255 polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
2087a7af 256
1d4ecb98
YW
257 layout = argv[1];
258 model = argc > 2 ? argv[2] : "";
259 variant = argc > 3 ? argv[3] : "";
260 options = argc > 4 ? argv[4] : "";
2087a7af 261
e1636421
LP
262 r = sd_bus_call_method(
263 bus,
2087a7af
LP
264 "org.freedesktop.locale1",
265 "/org/freedesktop/locale1",
266 "org.freedesktop.locale1",
267 "SetX11Keyboard",
4d7859d1 268 &error,
2087a7af 269 NULL,
4d7859d1
KS
270 "ssssbb", layout, model, variant, options,
271 arg_convert, arg_ask_password);
e1636421 272 if (r < 0)
4ae25393 273 return log_error_errno(r, "Failed to set keymap: %s", bus_error_message(&error, -r));
e1636421 274
4ae25393 275 return 0;
2087a7af
LP
276}
277
1d4ecb98 278static int list_x11_keymaps(int argc, char **argv, void *userdata) {
50cfc579 279 _cleanup_fclose_ FILE *f = NULL;
7fd1b19b 280 _cleanup_strv_free_ char **list = NULL;
50cfc579
LP
281 enum {
282 NONE,
283 MODELS,
284 LAYOUTS,
285 VARIANTS,
286 OPTIONS
287 } state = NONE, look_for;
288 int r;
289
c62e11ce 290 f = fopen("/usr/share/X11/xkb/rules/base.lst", "re");
4a62c710
MS
291 if (!f)
292 return log_error_errno(errno, "Failed to open keyboard mapping list. %m");
50cfc579 293
1d4ecb98 294 if (streq(argv[0], "list-x11-keymap-models"))
50cfc579 295 look_for = MODELS;
1d4ecb98 296 else if (streq(argv[0], "list-x11-keymap-layouts"))
50cfc579 297 look_for = LAYOUTS;
1d4ecb98 298 else if (streq(argv[0], "list-x11-keymap-variants"))
50cfc579 299 look_for = VARIANTS;
1d4ecb98 300 else if (streq(argv[0], "list-x11-keymap-options"))
50cfc579
LP
301 look_for = OPTIONS;
302 else
303 assert_not_reached("Wrong parameter");
304
271c8ec5
LP
305 for (;;) {
306 _cleanup_free_ char *line = NULL;
50cfc579
LP
307 char *l, *w;
308
271c8ec5
LP
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
50cfc579
LP
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
1d4ecb98 340 if (argc > 1) {
50cfc579
LP
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
1d4ecb98 356 if (!streq(w, argv[1]))
50cfc579
LP
357 continue;
358 } else
359 *w = 0;
360
7d6884b6
TA
361 r = strv_extend(&list, l);
362 if (r < 0)
363 return log_oom();
50cfc579
LP
364 }
365
baaa35ad
ZJS
366 if (strv_isempty(list))
367 return log_error_errno(SYNTHETIC_ERRNO(ENOENT),
368 "Couldn't find any entries.");
50cfc579
LP
369
370 strv_sort(list);
371 strv_uniq(list);
372
0221d68a 373 (void) pager_open(arg_pager_flags);
50cfc579
LP
374
375 strv_print(list);
376 return 0;
377}
378
1d4ecb98 379static int help(void) {
37ec0fdd
LP
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
2087a7af 387 printf("%s [OPTIONS...] COMMAND ...\n\n"
82c1d8f4 388 "Query or change system locale and keyboard settings.\n\n"
50cfc579
LP
389 " -h --help Show this help\n"
390 " --version Show package version\n"
50cfc579
LP
391 " --no-pager Do not pipe output into a pager\n"
392 " --no-ask-password Do not prompt for password\n"
4d7859d1 393 " -H --host=[USER@]HOST Operate on remote host\n"
a86a47ce
LP
394 " -M --machine=CONTAINER Operate on local container\n"
395 " --no-convert Don't convert keyboard mappings\n\n"
2087a7af 396 "Commands:\n"
50cfc579
LP
397 " status Show current locale settings\n"
398 " set-locale LOCALE... Set system locale\n"
399 " list-locales Show known locales\n"
2ebcf936 400 " set-keymap MAP [MAP] Set console and X11 keyboard mappings\n"
50cfc579 401 " list-keymaps Show known virtual console keyboard mappings\n"
31cf921a 402 " set-x11-keymap LAYOUT [MODEL [VARIANT [OPTIONS]]]\n"
2ebcf936 403 " Set X11 and console keyboard mappings\n"
50cfc579
LP
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"
601185b4 408 " list-x11-keymap-options Show known X11 keyboard mapping options\n"
37ec0fdd
LP
409 "\nSee the %s for details.\n"
410 , program_invocation_short_name
411 , link
412 );
1d4ecb98
YW
413
414 return 0;
415}
416
417static int verb_help(int argc, char **argv, void *userdata) {
418 return help();
2087a7af
LP
419}
420
421static 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[] = {
4d7859d1
KS
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 },
eb9da376 438 {}
2087a7af
LP
439 };
440
441 int c;
442
443 assert(argc >= 0);
444 assert(argv);
445
601185b4 446 while ((c = getopt_long(argc, argv, "hH:M:", options, NULL)) >= 0)
2087a7af
LP
447
448 switch (c) {
449
450 case 'h':
1d4ecb98 451 return help();
2087a7af
LP
452
453 case ARG_VERSION:
3f6fd1ba 454 return version();
2087a7af 455
2087a7af
LP
456 case ARG_NO_CONVERT:
457 arg_convert = false;
458 break;
459
460 case ARG_NO_PAGER:
0221d68a 461 arg_pager_flags |= PAGER_DISABLE;
2087a7af
LP
462 break;
463
546158bc
JJ
464 case ARG_NO_ASK_PASSWORD:
465 arg_ask_password = false;
466 break;
467
4d7859d1
KS
468 case 'H':
469 arg_transport = BUS_TRANSPORT_REMOTE;
470 arg_host = optarg;
471 break;
472
473 case 'M':
de33fc62 474 arg_transport = BUS_TRANSPORT_MACHINE;
4d7859d1
KS
475 arg_host = optarg;
476 break;
477
2087a7af
LP
478 case '?':
479 return -EINVAL;
480
481 default:
eb9da376 482 assert_not_reached("Unhandled option");
2087a7af 483 }
2087a7af
LP
484
485 return 1;
486}
487
4d7859d1 488static int localectl_main(sd_bus *bus, int argc, char *argv[]) {
2087a7af 489
1d4ecb98
YW
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 {}
2087a7af
LP
503 };
504
1d4ecb98 505 return dispatch_verb(argc, argv, verbs, bus);
2087a7af
LP
506}
507
f2a3de01 508static int run(int argc, char *argv[]) {
39daad0a 509 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
84f6181c 510 int r;
2087a7af 511
a9cdc94f 512 setlocale(LC_ALL, "");
2087a7af
LP
513 log_parse_environment();
514 log_open();
515
516 r = parse_argv(argc, argv);
84f6181c 517 if (r <= 0)
39daad0a 518 return r;
2087a7af 519
266f3e26 520 r = bus_connect_transport(arg_transport, arg_host, false, &bus);
39daad0a
ZJS
521 if (r < 0)
522 return log_error_errno(r, "Failed to create bus connection: %m");
2087a7af 523
39daad0a 524 return localectl_main(bus, argc, argv);
2087a7af 525}
39daad0a
ZJS
526
527DEFINE_MAIN_FUNCTION(run);