]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/locale/localectl.c
tree-wide: remove Lennart's copyright lines
[thirdparty/systemd.git] / src / locale / localectl.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
2087a7af 2/***
4d7859d1 3 Copyright 2013 Kay Sievers
2087a7af
LP
4***/
5
3f6fd1ba
LP
6#include <ftw.h>
7#include <getopt.h>
a9cdc94f 8#include <locale.h>
2087a7af 9#include <stdbool.h>
3f6fd1ba 10#include <stdlib.h>
2087a7af 11#include <string.h>
2087a7af 12
4d7859d1 13#include "sd-bus.h"
3f6fd1ba 14
4d7859d1 15#include "bus-error.h"
3f6fd1ba 16#include "bus-util.h"
0732ef7a 17#include "def.h"
3ffd4af2 18#include "fd-util.h"
a3428668 19#include "fileio.h"
75683450 20#include "locale-util.h"
3f6fd1ba
LP
21#include "pager.h"
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
LP
28
29static bool arg_no_pager = false;
2087a7af 30static bool arg_ask_password = true;
4d7859d1 31static BusTransport arg_transport = BUS_TRANSPORT_LOCAL;
7085053a 32static 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) {
a3428668
MS
53 int r;
54 char *variables[_VARIABLE_LC_MAX] = {};
55 LocaleVariable j;
56 bool print_warning = true;
57
75f86906 58 if (detect_container() > 0 || arg_host)
a3428668
MS
59 return;
60
1a5a177e 61 r = parse_env_file(NULL, "/proc/cmdline", WHITESPACE,
a3428668
MS
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],
76 NULL);
77
78 if (r < 0 && r != -ENOENT) {
da927ba9 79 log_warning_errno(r, "Failed to read /proc/cmdline: %m");
a3428668
MS
80 goto finish;
81 }
82
63229aa1 83 for (j = 0; j < _VARIABLE_LC_MAX; j++)
a3428668
MS
84 if (variables[j]) {
85 if (print_warning) {
b344bcbb 86 log_warning("Warning: Settings on kernel command line override system locale settings in /etc/locale.conf.\n"
c86b2d8f 87 " Command Line: %s=%s", locale_variable_to_string(j), variables[j]);
a3428668
MS
88
89 print_warning = false;
b344bcbb 90 } else
c86b2d8f 91 log_warning(" %s=%s", locale_variable_to_string(j), variables[j]);
a3428668
MS
92 }
93 finish:
63229aa1 94 for (j = 0; j < _VARIABLE_LC_MAX; j++)
a3428668
MS
95 free(variables[j]);
96}
97
2087a7af
LP
98static void print_status_info(StatusInfo *i) {
99 assert(i);
100
101 if (strv_isempty(i->locale))
a75db59c 102 puts(" System Locale: n/a");
2087a7af
LP
103 else {
104 char **j;
105
106 printf(" System Locale: %s\n", i->locale[0]);
107 STRV_FOREACH(j, i->locale + 1)
108 printf(" %s\n", *j);
109 }
110
111 printf(" VC Keymap: %s\n", strna(i->vconsole_keymap));
112 if (!isempty(i->vconsole_keymap_toggle))
113 printf("VC Toggle Keymap: %s\n", i->vconsole_keymap_toggle);
114
115 printf(" X11 Layout: %s\n", strna(i->x11_layout));
116 if (!isempty(i->x11_model))
117 printf(" X11 Model: %s\n", i->x11_model);
118 if (!isempty(i->x11_variant))
119 printf(" X11 Variant: %s\n", i->x11_variant);
120 if (!isempty(i->x11_options))
121 printf(" X11 Options: %s\n", i->x11_options);
122}
123
1d4ecb98 124static int show_status(int argc, char **argv, void *userdata) {
e7e55dbd 125 _cleanup_(status_info_clear) StatusInfo info = {};
9f6eb1cd 126 static const struct bus_properties_map map[] = {
9f6eb1cd
KS
127 { "VConsoleKeymap", "s", NULL, offsetof(StatusInfo, vconsole_keymap) },
128 { "VConsoleKeymapToggle", "s", NULL, offsetof(StatusInfo, vconsole_keymap_toggle) },
129 { "X11Layout", "s", NULL, offsetof(StatusInfo, x11_layout) },
130 { "X11Model", "s", NULL, offsetof(StatusInfo, x11_model) },
131 { "X11Variant", "s", NULL, offsetof(StatusInfo, x11_variant) },
132 { "X11Options", "s", NULL, offsetof(StatusInfo, x11_options) },
133 { "Locale", "as", NULL, offsetof(StatusInfo, locale) },
ffc06c35
KS
134 {}
135 };
f9e0eefc
LP
136
137 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
f37f8a61 138 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
1d4ecb98 139 sd_bus *bus = userdata;
ffc06c35 140 int r;
2087a7af 141
ffc06c35 142 assert(bus);
2087a7af 143
ffc06c35
KS
144 r = bus_map_all_properties(bus,
145 "org.freedesktop.locale1",
146 "/org/freedesktop/locale1",
9f6eb1cd 147 map,
a7e4861c 148 0,
f9e0eefc 149 &error,
f37f8a61 150 &m,
9f6eb1cd 151 &info);
e7e55dbd 152 if (r < 0)
f9e0eefc 153 return log_error_errno(r, "Could not get properties: %s", bus_error_message(&error, r));
2087a7af 154
ff9b60f3 155 print_overridden_variables();
2087a7af 156 print_status_info(&info);
4d7859d1 157
4d7859d1 158 return r;
2087a7af
LP
159}
160
1d4ecb98 161static int set_locale(int argc, char **argv, void *userdata) {
4afd3348
LP
162 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
163 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1d4ecb98 164 sd_bus *bus = userdata;
2087a7af
LP
165 int r;
166
167 assert(bus);
2087a7af 168
8a4b13c5 169 polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
2087a7af 170
151b9b96
LP
171 r = sd_bus_message_new_method_call(
172 bus,
173 &m,
2087a7af
LP
174 "org.freedesktop.locale1",
175 "/org/freedesktop/locale1",
176 "org.freedesktop.locale1",
151b9b96 177 "SetLocale");
4d7859d1 178 if (r < 0)
94676f3e 179 return bus_log_create_error(r);
2087a7af 180
1d4ecb98 181 r = sd_bus_message_append_strv(m, argv + 1);
4d7859d1 182 if (r < 0)
94676f3e 183 return bus_log_create_error(r);
2087a7af 184
4d7859d1 185 r = sd_bus_message_append(m, "b", arg_ask_password);
2087a7af 186 if (r < 0)
94676f3e 187 return bus_log_create_error(r);
2087a7af 188
c49b30a2 189 r = sd_bus_call(bus, m, 0, &error, NULL);
4d7859d1 190 if (r < 0) {
e1636421 191 log_error("Failed to issue method call: %s", bus_error_message(&error, -r));
4d7859d1 192 return r;
2087a7af
LP
193 }
194
4d7859d1 195 return 0;
2087a7af
LP
196}
197
1d4ecb98 198static int list_locales(int argc, char **argv, void *userdata) {
17d33cec 199 _cleanup_strv_free_ char **l = NULL;
17d33cec
GC
200 int r;
201
75683450 202 r = get_locales(&l);
f647962d
MS
203 if (r < 0)
204 return log_error_errno(r, "Failed to read list of locales: %m");
2087a7af 205
ee5324aa 206 (void) pager_open(arg_no_pager, false);
7c2d8094 207 strv_print(l);
2087a7af 208
bac3c8ee 209 return 0;
2087a7af
LP
210}
211
1d4ecb98 212static int set_vconsole_keymap(int argc, char **argv, void *userdata) {
4afd3348 213 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
2087a7af 214 const char *map, *toggle_map;
1d4ecb98 215 sd_bus *bus = userdata;
e1636421 216 int r;
2087a7af
LP
217
218 assert(bus);
2087a7af 219
8a4b13c5 220 polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
2087a7af 221
1d4ecb98
YW
222 map = argv[1];
223 toggle_map = argc > 2 ? argv[2] : "";
2087a7af 224
e1636421
LP
225 r = sd_bus_call_method(
226 bus,
2087a7af
LP
227 "org.freedesktop.locale1",
228 "/org/freedesktop/locale1",
229 "org.freedesktop.locale1",
230 "SetVConsoleKeyboard",
4d7859d1 231 &error,
2087a7af 232 NULL,
4d7859d1 233 "ssbb", map, toggle_map, arg_convert, arg_ask_password);
e1636421
LP
234 if (r < 0)
235 log_error("Failed to set keymap: %s", bus_error_message(&error, -r));
236
237 return r;
2087a7af
LP
238}
239
1d4ecb98 240static int list_vconsole_keymaps(int argc, char **argv, void *userdata) {
a017112b 241 _cleanup_strv_free_ char **l = NULL;
ed457f13 242 int r;
2087a7af 243
ed457f13
TB
244 r = get_keymaps(&l);
245 if (r < 0)
246 return log_error_errno(r, "Failed to read list of keymaps: %m");
2087a7af 247
ee5324aa 248 (void) pager_open(arg_no_pager, false);
2087a7af 249
7c2d8094 250 strv_print(l);
2087a7af
LP
251
252 return 0;
253}
254
1d4ecb98 255static int set_x11_keymap(int argc, char **argv, void *userdata) {
4afd3348 256 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
2087a7af 257 const char *layout, *model, *variant, *options;
1d4ecb98 258 sd_bus *bus = userdata;
e1636421 259 int r;
2087a7af 260
8a4b13c5 261 polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
2087a7af 262
1d4ecb98
YW
263 layout = argv[1];
264 model = argc > 2 ? argv[2] : "";
265 variant = argc > 3 ? argv[3] : "";
266 options = argc > 4 ? argv[4] : "";
2087a7af 267
e1636421
LP
268 r = sd_bus_call_method(
269 bus,
2087a7af
LP
270 "org.freedesktop.locale1",
271 "/org/freedesktop/locale1",
272 "org.freedesktop.locale1",
273 "SetX11Keyboard",
4d7859d1 274 &error,
2087a7af 275 NULL,
4d7859d1
KS
276 "ssssbb", layout, model, variant, options,
277 arg_convert, arg_ask_password);
e1636421
LP
278 if (r < 0)
279 log_error("Failed to set keymap: %s", bus_error_message(&error, -r));
280
281 return r;
2087a7af
LP
282}
283
1d4ecb98 284static int list_x11_keymaps(int argc, char **argv, void *userdata) {
50cfc579 285 _cleanup_fclose_ FILE *f = NULL;
7fd1b19b 286 _cleanup_strv_free_ char **list = NULL;
50cfc579
LP
287 char line[LINE_MAX];
288 enum {
289 NONE,
290 MODELS,
291 LAYOUTS,
292 VARIANTS,
293 OPTIONS
294 } state = NONE, look_for;
295 int r;
296
c62e11ce 297 f = fopen("/usr/share/X11/xkb/rules/base.lst", "re");
4a62c710
MS
298 if (!f)
299 return log_error_errno(errno, "Failed to open keyboard mapping list. %m");
50cfc579 300
1d4ecb98 301 if (streq(argv[0], "list-x11-keymap-models"))
50cfc579 302 look_for = MODELS;
1d4ecb98 303 else if (streq(argv[0], "list-x11-keymap-layouts"))
50cfc579 304 look_for = LAYOUTS;
1d4ecb98 305 else if (streq(argv[0], "list-x11-keymap-variants"))
50cfc579 306 look_for = VARIANTS;
1d4ecb98 307 else if (streq(argv[0], "list-x11-keymap-options"))
50cfc579
LP
308 look_for = OPTIONS;
309 else
310 assert_not_reached("Wrong parameter");
311
312 FOREACH_LINE(line, f, break) {
313 char *l, *w;
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
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
366 if (strv_isempty(list)) {
367 log_error("Couldn't find any entries.");
368 return -ENOENT;
369 }
370
371 strv_sort(list);
372 strv_uniq(list);
373
ee5324aa 374 (void) pager_open(arg_no_pager, false);
50cfc579
LP
375
376 strv_print(list);
377 return 0;
378}
379
1d4ecb98 380static int help(void) {
2087a7af 381 printf("%s [OPTIONS...] COMMAND ...\n\n"
82c1d8f4 382 "Query or change system locale and keyboard settings.\n\n"
50cfc579
LP
383 " -h --help Show this help\n"
384 " --version Show package version\n"
50cfc579
LP
385 " --no-pager Do not pipe output into a pager\n"
386 " --no-ask-password Do not prompt for password\n"
4d7859d1 387 " -H --host=[USER@]HOST Operate on remote host\n"
a86a47ce
LP
388 " -M --machine=CONTAINER Operate on local container\n"
389 " --no-convert Don't convert keyboard mappings\n\n"
2087a7af 390 "Commands:\n"
50cfc579
LP
391 " status Show current locale settings\n"
392 " set-locale LOCALE... Set system locale\n"
393 " list-locales Show known locales\n"
2ebcf936 394 " set-keymap MAP [MAP] Set console and X11 keyboard mappings\n"
50cfc579 395 " list-keymaps Show known virtual console keyboard mappings\n"
31cf921a 396 " set-x11-keymap LAYOUT [MODEL [VARIANT [OPTIONS]]]\n"
2ebcf936 397 " Set X11 and console keyboard mappings\n"
50cfc579
LP
398 " list-x11-keymap-models Show known X11 keyboard mapping models\n"
399 " list-x11-keymap-layouts Show known X11 keyboard mapping layouts\n"
400 " list-x11-keymap-variants [LAYOUT]\n"
401 " Show known X11 keyboard mapping variants\n"
601185b4
ZJS
402 " list-x11-keymap-options Show known X11 keyboard mapping options\n"
403 , program_invocation_short_name);
1d4ecb98
YW
404
405 return 0;
406}
407
408static int verb_help(int argc, char **argv, void *userdata) {
409 return help();
2087a7af
LP
410}
411
412static int parse_argv(int argc, char *argv[]) {
413
414 enum {
415 ARG_VERSION = 0x100,
416 ARG_NO_PAGER,
417 ARG_NO_CONVERT,
418 ARG_NO_ASK_PASSWORD
419 };
420
421 static const struct option options[] = {
4d7859d1
KS
422 { "help", no_argument, NULL, 'h' },
423 { "version", no_argument, NULL, ARG_VERSION },
424 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
425 { "host", required_argument, NULL, 'H' },
426 { "machine", required_argument, NULL, 'M' },
427 { "no-ask-password", no_argument, NULL, ARG_NO_ASK_PASSWORD },
428 { "no-convert", no_argument, NULL, ARG_NO_CONVERT },
eb9da376 429 {}
2087a7af
LP
430 };
431
432 int c;
433
434 assert(argc >= 0);
435 assert(argv);
436
601185b4 437 while ((c = getopt_long(argc, argv, "hH:M:", options, NULL)) >= 0)
2087a7af
LP
438
439 switch (c) {
440
441 case 'h':
1d4ecb98 442 return help();
2087a7af
LP
443
444 case ARG_VERSION:
3f6fd1ba 445 return version();
2087a7af 446
2087a7af
LP
447 case ARG_NO_CONVERT:
448 arg_convert = false;
449 break;
450
451 case ARG_NO_PAGER:
452 arg_no_pager = true;
453 break;
454
546158bc
JJ
455 case ARG_NO_ASK_PASSWORD:
456 arg_ask_password = false;
457 break;
458
4d7859d1
KS
459 case 'H':
460 arg_transport = BUS_TRANSPORT_REMOTE;
461 arg_host = optarg;
462 break;
463
464 case 'M':
de33fc62 465 arg_transport = BUS_TRANSPORT_MACHINE;
4d7859d1
KS
466 arg_host = optarg;
467 break;
468
2087a7af
LP
469 case '?':
470 return -EINVAL;
471
472 default:
eb9da376 473 assert_not_reached("Unhandled option");
2087a7af 474 }
2087a7af
LP
475
476 return 1;
477}
478
4d7859d1 479static int localectl_main(sd_bus *bus, int argc, char *argv[]) {
2087a7af 480
1d4ecb98
YW
481 static const Verb verbs[] = {
482 { "status", VERB_ANY, 1, VERB_DEFAULT, show_status },
483 { "set-locale", 2, VERB_ANY, 0, set_locale },
484 { "list-locales", VERB_ANY, 1, 0, list_locales },
485 { "set-keymap", 2, 3, 0, set_vconsole_keymap },
486 { "list-keymaps", VERB_ANY, 1, 0, list_vconsole_keymaps },
487 { "set-x11-keymap", 2, 5, 0, set_x11_keymap },
488 { "list-x11-keymap-models", VERB_ANY, 1, 0, list_x11_keymaps },
489 { "list-x11-keymap-layouts", VERB_ANY, 1, 0, list_x11_keymaps },
490 { "list-x11-keymap-variants", VERB_ANY, 2, 0, list_x11_keymaps },
491 { "list-x11-keymap-options", VERB_ANY, 1, 0, list_x11_keymaps },
492 { "help", VERB_ANY, VERB_ANY, 0, verb_help }, /* Not documented, but supported since it is created. */
493 {}
2087a7af
LP
494 };
495
1d4ecb98 496 return dispatch_verb(argc, argv, verbs, bus);
2087a7af
LP
497}
498
4d7859d1 499int main(int argc, char*argv[]) {
a3c56345 500 sd_bus *bus = NULL;
84f6181c 501 int r;
2087a7af 502
a9cdc94f 503 setlocale(LC_ALL, "");
2087a7af
LP
504 log_parse_environment();
505 log_open();
506
507 r = parse_argv(argc, argv);
84f6181c 508 if (r <= 0)
2087a7af 509 goto finish;
2087a7af 510
266f3e26 511 r = bus_connect_transport(arg_transport, arg_host, false, &bus);
4d7859d1 512 if (r < 0) {
da927ba9 513 log_error_errno(r, "Failed to create bus connection: %m");
4d7859d1 514 goto finish;
2087a7af
LP
515 }
516
4d7859d1 517 r = localectl_main(bus, argc, argv);
2087a7af 518
4d7859d1 519finish:
0a84daa5
FB
520 /* make sure we terminate the bus connection first, and then close the
521 * pager, see issue #3543 for the details. */
a3c56345 522 sd_bus_flush_close_unref(bus);
2087a7af
LP
523 pager_close();
524
84f6181c 525 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
2087a7af 526}