]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/locale/localectl.c
pkgconfig: define variables relative to ${prefix}/${rootprefix}/${sysconfdir}
[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"
3f6fd1ba 18#include "pager.h"
01771226 19#include "proc-cmdline.h"
3f6fd1ba
LP
20#include "set.h"
21#include "spawn-polkit-agent.h"
22#include "strv.h"
37ec0fdd 23#include "terminal-util.h"
3f6fd1ba 24#include "util.h"
1d4ecb98 25#include "verbs.h"
3f6fd1ba 26#include "virt.h"
2087a7af 27
0221d68a 28static PagerFlags arg_pager_flags = 0;
2087a7af 29static bool arg_ask_password = true;
4d7859d1 30static BusTransport arg_transport = BUS_TRANSPORT_LOCAL;
7085053a 31static char *arg_host = NULL;
2087a7af
LP
32static bool arg_convert = true;
33
2087a7af
LP
34typedef struct StatusInfo {
35 char **locale;
f37f8a61
YW
36 const char *vconsole_keymap;
37 const char *vconsole_keymap_toggle;
38 const char *x11_layout;
39 const char *x11_model;
40 const char *x11_variant;
41 const char *x11_options;
2087a7af
LP
42} StatusInfo;
43
e7e55dbd
DH
44static void status_info_clear(StatusInfo *info) {
45 if (info) {
46 strv_free(info->locale);
e7e55dbd
DH
47 zero(*info);
48 }
49}
50
ff9b60f3 51static void print_overridden_variables(void) {
e6755a33 52 _cleanup_(locale_variables_freep) char *variables[_VARIABLE_LC_MAX] = {};
a3428668 53 bool print_warning = true;
e6755a33
LP
54 LocaleVariable j;
55 int r;
a3428668 56
01771226 57 if (arg_transport != BUS_TRANSPORT_LOCAL)
a3428668
MS
58 return;
59
01771226
LP
60 r = proc_cmdline_get_key_many(
61 PROC_CMDLINE_STRIP_RD_PREFIX,
62 "locale.LANG", &variables[VARIABLE_LANG],
63 "locale.LANGUAGE", &variables[VARIABLE_LANGUAGE],
64 "locale.LC_CTYPE", &variables[VARIABLE_LC_CTYPE],
65 "locale.LC_NUMERIC", &variables[VARIABLE_LC_NUMERIC],
66 "locale.LC_TIME", &variables[VARIABLE_LC_TIME],
67 "locale.LC_COLLATE", &variables[VARIABLE_LC_COLLATE],
68 "locale.LC_MONETARY", &variables[VARIABLE_LC_MONETARY],
69 "locale.LC_MESSAGES", &variables[VARIABLE_LC_MESSAGES],
70 "locale.LC_PAPER", &variables[VARIABLE_LC_PAPER],
71 "locale.LC_NAME", &variables[VARIABLE_LC_NAME],
72 "locale.LC_ADDRESS", &variables[VARIABLE_LC_ADDRESS],
73 "locale.LC_TELEPHONE", &variables[VARIABLE_LC_TELEPHONE],
74 "locale.LC_MEASUREMENT", &variables[VARIABLE_LC_MEASUREMENT],
75 "locale.LC_IDENTIFICATION", &variables[VARIABLE_LC_IDENTIFICATION]);
a3428668 76 if (r < 0 && r != -ENOENT) {
da927ba9 77 log_warning_errno(r, "Failed to read /proc/cmdline: %m");
e6755a33 78 return;
a3428668
MS
79 }
80
63229aa1 81 for (j = 0; j < _VARIABLE_LC_MAX; j++)
a3428668
MS
82 if (variables[j]) {
83 if (print_warning) {
b344bcbb 84 log_warning("Warning: Settings on kernel command line override system locale settings in /etc/locale.conf.\n"
c86b2d8f 85 " Command Line: %s=%s", locale_variable_to_string(j), variables[j]);
a3428668
MS
86
87 print_warning = false;
b344bcbb 88 } else
c86b2d8f 89 log_warning(" %s=%s", locale_variable_to_string(j), variables[j]);
a3428668 90 }
a3428668
MS
91}
92
2087a7af
LP
93static void print_status_info(StatusInfo *i) {
94 assert(i);
95
96 if (strv_isempty(i->locale))
a75db59c 97 puts(" System Locale: n/a");
2087a7af
LP
98 else {
99 char **j;
100
101 printf(" System Locale: %s\n", i->locale[0]);
102 STRV_FOREACH(j, i->locale + 1)
103 printf(" %s\n", *j);
104 }
105
106 printf(" VC Keymap: %s\n", strna(i->vconsole_keymap));
107 if (!isempty(i->vconsole_keymap_toggle))
108 printf("VC Toggle Keymap: %s\n", i->vconsole_keymap_toggle);
109
110 printf(" X11 Layout: %s\n", strna(i->x11_layout));
111 if (!isempty(i->x11_model))
112 printf(" X11 Model: %s\n", i->x11_model);
113 if (!isempty(i->x11_variant))
114 printf(" X11 Variant: %s\n", i->x11_variant);
115 if (!isempty(i->x11_options))
116 printf(" X11 Options: %s\n", i->x11_options);
117}
118
1d4ecb98 119static int show_status(int argc, char **argv, void *userdata) {
e7e55dbd 120 _cleanup_(status_info_clear) StatusInfo info = {};
9f6eb1cd 121 static const struct bus_properties_map map[] = {
9f6eb1cd
KS
122 { "VConsoleKeymap", "s", NULL, offsetof(StatusInfo, vconsole_keymap) },
123 { "VConsoleKeymapToggle", "s", NULL, offsetof(StatusInfo, vconsole_keymap_toggle) },
124 { "X11Layout", "s", NULL, offsetof(StatusInfo, x11_layout) },
125 { "X11Model", "s", NULL, offsetof(StatusInfo, x11_model) },
126 { "X11Variant", "s", NULL, offsetof(StatusInfo, x11_variant) },
127 { "X11Options", "s", NULL, offsetof(StatusInfo, x11_options) },
128 { "Locale", "as", NULL, offsetof(StatusInfo, locale) },
ffc06c35
KS
129 {}
130 };
f9e0eefc
LP
131
132 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
f37f8a61 133 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
1d4ecb98 134 sd_bus *bus = userdata;
ffc06c35 135 int r;
2087a7af 136
ffc06c35 137 assert(bus);
2087a7af 138
ffc06c35
KS
139 r = bus_map_all_properties(bus,
140 "org.freedesktop.locale1",
141 "/org/freedesktop/locale1",
9f6eb1cd 142 map,
a7e4861c 143 0,
f9e0eefc 144 &error,
f37f8a61 145 &m,
9f6eb1cd 146 &info);
e7e55dbd 147 if (r < 0)
f9e0eefc 148 return log_error_errno(r, "Could not get properties: %s", bus_error_message(&error, r));
2087a7af 149
ff9b60f3 150 print_overridden_variables();
2087a7af 151 print_status_info(&info);
4d7859d1 152
4d7859d1 153 return r;
2087a7af
LP
154}
155
1d4ecb98 156static int set_locale(int argc, char **argv, void *userdata) {
4afd3348
LP
157 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
158 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1d4ecb98 159 sd_bus *bus = userdata;
2087a7af
LP
160 int r;
161
162 assert(bus);
2087a7af 163
8a4b13c5 164 polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
2087a7af 165
151b9b96
LP
166 r = sd_bus_message_new_method_call(
167 bus,
168 &m,
2087a7af
LP
169 "org.freedesktop.locale1",
170 "/org/freedesktop/locale1",
171 "org.freedesktop.locale1",
151b9b96 172 "SetLocale");
4d7859d1 173 if (r < 0)
94676f3e 174 return bus_log_create_error(r);
2087a7af 175
1d4ecb98 176 r = sd_bus_message_append_strv(m, argv + 1);
4d7859d1 177 if (r < 0)
94676f3e 178 return bus_log_create_error(r);
2087a7af 179
4d7859d1 180 r = sd_bus_message_append(m, "b", arg_ask_password);
2087a7af 181 if (r < 0)
94676f3e 182 return bus_log_create_error(r);
2087a7af 183
c49b30a2 184 r = sd_bus_call(bus, m, 0, &error, NULL);
4ae25393
YW
185 if (r < 0)
186 return log_error_errno(r, "Failed to issue method call: %s", bus_error_message(&error, -r));
2087a7af 187
4d7859d1 188 return 0;
2087a7af
LP
189}
190
1d4ecb98 191static int list_locales(int argc, char **argv, void *userdata) {
17d33cec 192 _cleanup_strv_free_ char **l = NULL;
17d33cec
GC
193 int r;
194
75683450 195 r = get_locales(&l);
f647962d
MS
196 if (r < 0)
197 return log_error_errno(r, "Failed to read list of locales: %m");
2087a7af 198
0221d68a 199 (void) pager_open(arg_pager_flags);
7c2d8094 200 strv_print(l);
2087a7af 201
bac3c8ee 202 return 0;
2087a7af
LP
203}
204
1d4ecb98 205static int set_vconsole_keymap(int argc, char **argv, void *userdata) {
4afd3348 206 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
2087a7af 207 const char *map, *toggle_map;
1d4ecb98 208 sd_bus *bus = userdata;
e1636421 209 int r;
2087a7af
LP
210
211 assert(bus);
2087a7af 212
8a4b13c5 213 polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
2087a7af 214
1d4ecb98
YW
215 map = argv[1];
216 toggle_map = argc > 2 ? argv[2] : "";
2087a7af 217
e1636421
LP
218 r = sd_bus_call_method(
219 bus,
2087a7af
LP
220 "org.freedesktop.locale1",
221 "/org/freedesktop/locale1",
222 "org.freedesktop.locale1",
223 "SetVConsoleKeyboard",
4d7859d1 224 &error,
2087a7af 225 NULL,
4d7859d1 226 "ssbb", map, toggle_map, arg_convert, arg_ask_password);
e1636421 227 if (r < 0)
4ae25393 228 return log_error_errno(r, "Failed to set keymap: %s", bus_error_message(&error, -r));
e1636421 229
4ae25393 230 return 0;
2087a7af
LP
231}
232
1d4ecb98 233static int list_vconsole_keymaps(int argc, char **argv, void *userdata) {
a017112b 234 _cleanup_strv_free_ char **l = NULL;
ed457f13 235 int r;
2087a7af 236
ed457f13
TB
237 r = get_keymaps(&l);
238 if (r < 0)
239 return log_error_errno(r, "Failed to read list of keymaps: %m");
2087a7af 240
0221d68a 241 (void) pager_open(arg_pager_flags);
2087a7af 242
7c2d8094 243 strv_print(l);
2087a7af
LP
244
245 return 0;
246}
247
1d4ecb98 248static int set_x11_keymap(int argc, char **argv, void *userdata) {
4afd3348 249 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
2087a7af 250 const char *layout, *model, *variant, *options;
1d4ecb98 251 sd_bus *bus = userdata;
e1636421 252 int r;
2087a7af 253
8a4b13c5 254 polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
2087a7af 255
1d4ecb98
YW
256 layout = argv[1];
257 model = argc > 2 ? argv[2] : "";
258 variant = argc > 3 ? argv[3] : "";
259 options = argc > 4 ? argv[4] : "";
2087a7af 260
e1636421
LP
261 r = sd_bus_call_method(
262 bus,
2087a7af
LP
263 "org.freedesktop.locale1",
264 "/org/freedesktop/locale1",
265 "org.freedesktop.locale1",
266 "SetX11Keyboard",
4d7859d1 267 &error,
2087a7af 268 NULL,
4d7859d1
KS
269 "ssssbb", layout, model, variant, options,
270 arg_convert, arg_ask_password);
e1636421 271 if (r < 0)
4ae25393 272 return log_error_errno(r, "Failed to set keymap: %s", bus_error_message(&error, -r));
e1636421 273
4ae25393 274 return 0;
2087a7af
LP
275}
276
1d4ecb98 277static int list_x11_keymaps(int argc, char **argv, void *userdata) {
50cfc579 278 _cleanup_fclose_ FILE *f = NULL;
7fd1b19b 279 _cleanup_strv_free_ char **list = NULL;
50cfc579
LP
280 enum {
281 NONE,
282 MODELS,
283 LAYOUTS,
284 VARIANTS,
285 OPTIONS
286 } state = NONE, look_for;
287 int r;
288
c62e11ce 289 f = fopen("/usr/share/X11/xkb/rules/base.lst", "re");
4a62c710
MS
290 if (!f)
291 return log_error_errno(errno, "Failed to open keyboard mapping list. %m");
50cfc579 292
1d4ecb98 293 if (streq(argv[0], "list-x11-keymap-models"))
50cfc579 294 look_for = MODELS;
1d4ecb98 295 else if (streq(argv[0], "list-x11-keymap-layouts"))
50cfc579 296 look_for = LAYOUTS;
1d4ecb98 297 else if (streq(argv[0], "list-x11-keymap-variants"))
50cfc579 298 look_for = VARIANTS;
1d4ecb98 299 else if (streq(argv[0], "list-x11-keymap-options"))
50cfc579
LP
300 look_for = OPTIONS;
301 else
302 assert_not_reached("Wrong parameter");
303
271c8ec5
LP
304 for (;;) {
305 _cleanup_free_ char *line = NULL;
50cfc579
LP
306 char *l, *w;
307
271c8ec5
LP
308 r = read_line(f, LONG_LINE_MAX, &line);
309 if (r < 0)
310 return log_error_errno(r, "Failed to read keyboard mapping list: %m");
311 if (r == 0)
312 break;
313
50cfc579
LP
314 l = strstrip(line);
315
316 if (isempty(l))
317 continue;
318
319 if (l[0] == '!') {
320 if (startswith(l, "! model"))
321 state = MODELS;
322 else if (startswith(l, "! layout"))
323 state = LAYOUTS;
324 else if (startswith(l, "! variant"))
325 state = VARIANTS;
326 else if (startswith(l, "! option"))
327 state = OPTIONS;
328 else
329 state = NONE;
330
331 continue;
332 }
333
334 if (state != look_for)
335 continue;
336
337 w = l + strcspn(l, WHITESPACE);
338
1d4ecb98 339 if (argc > 1) {
50cfc579
LP
340 char *e;
341
342 if (*w == 0)
343 continue;
344
345 *w = 0;
346 w++;
347 w += strspn(w, WHITESPACE);
348
349 e = strchr(w, ':');
350 if (!e)
351 continue;
352
353 *e = 0;
354
1d4ecb98 355 if (!streq(w, argv[1]))
50cfc579
LP
356 continue;
357 } else
358 *w = 0;
359
7d6884b6
TA
360 r = strv_extend(&list, l);
361 if (r < 0)
362 return log_oom();
50cfc579
LP
363 }
364
365 if (strv_isempty(list)) {
366 log_error("Couldn't find any entries.");
367 return -ENOENT;
368 }
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
4d7859d1 508int main(int argc, char*argv[]) {
a3c56345 509 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)
2087a7af 518 goto finish;
2087a7af 519
266f3e26 520 r = bus_connect_transport(arg_transport, arg_host, false, &bus);
4d7859d1 521 if (r < 0) {
da927ba9 522 log_error_errno(r, "Failed to create bus connection: %m");
4d7859d1 523 goto finish;
2087a7af
LP
524 }
525
4d7859d1 526 r = localectl_main(bus, argc, argv);
2087a7af 527
4d7859d1 528finish:
0a84daa5
FB
529 /* make sure we terminate the bus connection first, and then close the
530 * pager, see issue #3543 for the details. */
a3c56345 531 sd_bus_flush_close_unref(bus);
2087a7af
LP
532 pager_close();
533
84f6181c 534 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
2087a7af 535}