]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/locale/localectl.c
Make pager_open() return void
[thirdparty/systemd.git] / src / locale / localectl.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
2087a7af 2
3f6fd1ba 3#include <getopt.h>
2087a7af 4#include <stdbool.h>
3f6fd1ba 5#include <stdlib.h>
2087a7af 6
4d7859d1 7#include "sd-bus.h"
3f6fd1ba 8
4d7859d1 9#include "bus-error.h"
9b71e4ab 10#include "bus-locator.h"
807542be 11#include "bus-map-properties.h"
3ffd4af2 12#include "fd-util.h"
a3428668 13#include "fileio.h"
f05e1d0d 14#include "kbd-util.h"
75683450 15#include "locale-util.h"
39daad0a 16#include "main-func.h"
0a970718 17#include "memory-util.h"
3f6fd1ba 18#include "pager.h"
294bf0c3 19#include "pretty-print.h"
01771226 20#include "proc-cmdline.h"
3f6fd1ba
LP
21#include "set.h"
22#include "spawn-polkit-agent.h"
23#include "strv.h"
ce2529b4 24#include "terminal-util.h"
1d4ecb98 25#include "verbs.h"
3f6fd1ba 26#include "virt.h"
2087a7af 27
8f20232f
MK
28/* Enough time for locale-gen to finish server-side (in case it is in use) */
29#define LOCALE_SLOW_BUS_CALL_TIMEOUT_USEC (2*USEC_PER_MINUTE)
30
0221d68a 31static PagerFlags arg_pager_flags = 0;
2087a7af 32static bool arg_ask_password = true;
4d7859d1 33static BusTransport arg_transport = BUS_TRANSPORT_LOCAL;
847da1ac 34static const char *arg_host = NULL;
2087a7af
LP
35static bool arg_convert = true;
36
2087a7af
LP
37typedef struct StatusInfo {
38 char **locale;
f37f8a61
YW
39 const char *vconsole_keymap;
40 const char *vconsole_keymap_toggle;
41 const char *x11_layout;
42 const char *x11_model;
43 const char *x11_variant;
44 const char *x11_options;
2087a7af
LP
45} StatusInfo;
46
e7e55dbd
DH
47static void status_info_clear(StatusInfo *info) {
48 if (info) {
49 strv_free(info->locale);
e7e55dbd
DH
50 zero(*info);
51 }
52}
53
ff9b60f3 54static void print_overridden_variables(void) {
e6755a33 55 _cleanup_(locale_variables_freep) char *variables[_VARIABLE_LC_MAX] = {};
a3428668 56 bool print_warning = true;
e6755a33 57 int r;
a3428668 58
01771226 59 if (arg_transport != BUS_TRANSPORT_LOCAL)
a3428668
MS
60 return;
61
01771226
LP
62 r = proc_cmdline_get_key_many(
63 PROC_CMDLINE_STRIP_RD_PREFIX,
64 "locale.LANG", &variables[VARIABLE_LANG],
65 "locale.LANGUAGE", &variables[VARIABLE_LANGUAGE],
66 "locale.LC_CTYPE", &variables[VARIABLE_LC_CTYPE],
67 "locale.LC_NUMERIC", &variables[VARIABLE_LC_NUMERIC],
68 "locale.LC_TIME", &variables[VARIABLE_LC_TIME],
69 "locale.LC_COLLATE", &variables[VARIABLE_LC_COLLATE],
70 "locale.LC_MONETARY", &variables[VARIABLE_LC_MONETARY],
71 "locale.LC_MESSAGES", &variables[VARIABLE_LC_MESSAGES],
72 "locale.LC_PAPER", &variables[VARIABLE_LC_PAPER],
73 "locale.LC_NAME", &variables[VARIABLE_LC_NAME],
74 "locale.LC_ADDRESS", &variables[VARIABLE_LC_ADDRESS],
75 "locale.LC_TELEPHONE", &variables[VARIABLE_LC_TELEPHONE],
76 "locale.LC_MEASUREMENT", &variables[VARIABLE_LC_MEASUREMENT],
77 "locale.LC_IDENTIFICATION", &variables[VARIABLE_LC_IDENTIFICATION]);
a3428668 78 if (r < 0 && r != -ENOENT) {
da927ba9 79 log_warning_errno(r, "Failed to read /proc/cmdline: %m");
e6755a33 80 return;
a3428668
MS
81 }
82
ab4ab13c 83 for (LocaleVariable 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 92 }
a3428668
MS
93}
94
2087a7af
LP
95static void print_status_info(StatusInfo *i) {
96 assert(i);
97
98 if (strv_isempty(i->locale))
a75db59c 99 puts(" System Locale: n/a");
2087a7af
LP
100 else {
101 char **j;
102
103 printf(" System Locale: %s\n", i->locale[0]);
104 STRV_FOREACH(j, i->locale + 1)
105 printf(" %s\n", *j);
106 }
107
108 printf(" VC Keymap: %s\n", strna(i->vconsole_keymap));
109 if (!isempty(i->vconsole_keymap_toggle))
110 printf("VC Toggle Keymap: %s\n", i->vconsole_keymap_toggle);
111
112 printf(" X11 Layout: %s\n", strna(i->x11_layout));
113 if (!isempty(i->x11_model))
114 printf(" X11 Model: %s\n", i->x11_model);
115 if (!isempty(i->x11_variant))
116 printf(" X11 Variant: %s\n", i->x11_variant);
117 if (!isempty(i->x11_options))
118 printf(" X11 Options: %s\n", i->x11_options);
119}
120
1d4ecb98 121static int show_status(int argc, char **argv, void *userdata) {
e7e55dbd 122 _cleanup_(status_info_clear) StatusInfo info = {};
9f6eb1cd 123 static const struct bus_properties_map map[] = {
9f6eb1cd
KS
124 { "VConsoleKeymap", "s", NULL, offsetof(StatusInfo, vconsole_keymap) },
125 { "VConsoleKeymapToggle", "s", NULL, offsetof(StatusInfo, vconsole_keymap_toggle) },
126 { "X11Layout", "s", NULL, offsetof(StatusInfo, x11_layout) },
127 { "X11Model", "s", NULL, offsetof(StatusInfo, x11_model) },
128 { "X11Variant", "s", NULL, offsetof(StatusInfo, x11_variant) },
129 { "X11Options", "s", NULL, offsetof(StatusInfo, x11_options) },
130 { "Locale", "as", NULL, offsetof(StatusInfo, locale) },
ffc06c35
KS
131 {}
132 };
f9e0eefc
LP
133
134 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
f37f8a61 135 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
1d4ecb98 136 sd_bus *bus = userdata;
ffc06c35 137 int r;
2087a7af 138
ffc06c35 139 assert(bus);
2087a7af 140
ffc06c35
KS
141 r = bus_map_all_properties(bus,
142 "org.freedesktop.locale1",
143 "/org/freedesktop/locale1",
9f6eb1cd 144 map,
a7e4861c 145 0,
f9e0eefc 146 &error,
f37f8a61 147 &m,
9f6eb1cd 148 &info);
e7e55dbd 149 if (r < 0)
f9e0eefc 150 return log_error_errno(r, "Could not get properties: %s", bus_error_message(&error, r));
2087a7af 151
ff9b60f3 152 print_overridden_variables();
2087a7af 153 print_status_info(&info);
4d7859d1 154
4d7859d1 155 return r;
2087a7af
LP
156}
157
1d4ecb98 158static int set_locale(int argc, char **argv, void *userdata) {
4afd3348
LP
159 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
160 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1d4ecb98 161 sd_bus *bus = userdata;
2087a7af
LP
162 int r;
163
164 assert(bus);
2087a7af 165
8a4b13c5 166 polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
2087a7af 167
e3c8ec3b 168 r = bus_message_new_method_call(bus, &m, bus_locale, "SetLocale");
4d7859d1 169 if (r < 0)
94676f3e 170 return bus_log_create_error(r);
2087a7af 171
1d4ecb98 172 r = sd_bus_message_append_strv(m, argv + 1);
4d7859d1 173 if (r < 0)
94676f3e 174 return bus_log_create_error(r);
2087a7af 175
4d7859d1 176 r = sd_bus_message_append(m, "b", arg_ask_password);
2087a7af 177 if (r < 0)
94676f3e 178 return bus_log_create_error(r);
2087a7af 179
8f20232f
MK
180 /* We use a longer timeout for the method call in case localed is running locale-gen */
181 r = sd_bus_call(bus, m, LOCALE_SLOW_BUS_CALL_TIMEOUT_USEC, &error, NULL);
4ae25393 182 if (r < 0)
2a03b9ed 183 return log_error_errno(r, "Failed to issue method call: %s", bus_error_message(&error, r));
2087a7af 184
4d7859d1 185 return 0;
2087a7af
LP
186}
187
1d4ecb98 188static int list_locales(int argc, char **argv, void *userdata) {
17d33cec 189 _cleanup_strv_free_ char **l = NULL;
17d33cec
GC
190 int r;
191
75683450 192 r = get_locales(&l);
f647962d
MS
193 if (r < 0)
194 return log_error_errno(r, "Failed to read list of locales: %m");
2087a7af 195
384c2c32 196 pager_open(arg_pager_flags);
7c2d8094 197 strv_print(l);
2087a7af 198
bac3c8ee 199 return 0;
2087a7af
LP
200}
201
1d4ecb98 202static int set_vconsole_keymap(int argc, char **argv, void *userdata) {
4afd3348 203 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
2087a7af 204 const char *map, *toggle_map;
1d4ecb98 205 sd_bus *bus = userdata;
e1636421 206 int r;
2087a7af
LP
207
208 assert(bus);
2087a7af 209
8a4b13c5 210 polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
2087a7af 211
1d4ecb98
YW
212 map = argv[1];
213 toggle_map = argc > 2 ? argv[2] : "";
2087a7af 214
e3c8ec3b 215 r = bus_call_method(
e1636421 216 bus,
e3c8ec3b 217 bus_locale,
2087a7af 218 "SetVConsoleKeyboard",
4d7859d1 219 &error,
2087a7af 220 NULL,
4d7859d1 221 "ssbb", map, toggle_map, arg_convert, arg_ask_password);
e1636421 222 if (r < 0)
2a03b9ed 223 return log_error_errno(r, "Failed to set keymap: %s", bus_error_message(&error, r));
e1636421 224
4ae25393 225 return 0;
2087a7af
LP
226}
227
1d4ecb98 228static int list_vconsole_keymaps(int argc, char **argv, void *userdata) {
a017112b 229 _cleanup_strv_free_ char **l = NULL;
ed457f13 230 int r;
2087a7af 231
ed457f13
TB
232 r = get_keymaps(&l);
233 if (r < 0)
234 return log_error_errno(r, "Failed to read list of keymaps: %m");
2087a7af 235
384c2c32 236 pager_open(arg_pager_flags);
2087a7af 237
7c2d8094 238 strv_print(l);
2087a7af
LP
239
240 return 0;
241}
242
1d4ecb98 243static int set_x11_keymap(int argc, char **argv, void *userdata) {
4afd3348 244 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
2087a7af 245 const char *layout, *model, *variant, *options;
1d4ecb98 246 sd_bus *bus = userdata;
e1636421 247 int r;
2087a7af 248
8a4b13c5 249 polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
2087a7af 250
1d4ecb98
YW
251 layout = argv[1];
252 model = argc > 2 ? argv[2] : "";
253 variant = argc > 3 ? argv[3] : "";
254 options = argc > 4 ? argv[4] : "";
2087a7af 255
e3c8ec3b 256 r = bus_call_method(
e1636421 257 bus,
e3c8ec3b 258 bus_locale,
2087a7af 259 "SetX11Keyboard",
4d7859d1 260 &error,
2087a7af 261 NULL,
4d7859d1
KS
262 "ssssbb", layout, model, variant, options,
263 arg_convert, arg_ask_password);
e1636421 264 if (r < 0)
2a03b9ed 265 return log_error_errno(r, "Failed to set keymap: %s", bus_error_message(&error, r));
e1636421 266
4ae25393 267 return 0;
2087a7af
LP
268}
269
1d4ecb98 270static int list_x11_keymaps(int argc, char **argv, void *userdata) {
50cfc579 271 _cleanup_fclose_ FILE *f = NULL;
7fd1b19b 272 _cleanup_strv_free_ char **list = NULL;
50cfc579
LP
273 enum {
274 NONE,
275 MODELS,
276 LAYOUTS,
277 VARIANTS,
278 OPTIONS
279 } state = NONE, look_for;
280 int r;
281
c62e11ce 282 f = fopen("/usr/share/X11/xkb/rules/base.lst", "re");
4a62c710
MS
283 if (!f)
284 return log_error_errno(errno, "Failed to open keyboard mapping list. %m");
50cfc579 285
1d4ecb98 286 if (streq(argv[0], "list-x11-keymap-models"))
50cfc579 287 look_for = MODELS;
1d4ecb98 288 else if (streq(argv[0], "list-x11-keymap-layouts"))
50cfc579 289 look_for = LAYOUTS;
1d4ecb98 290 else if (streq(argv[0], "list-x11-keymap-variants"))
50cfc579 291 look_for = VARIANTS;
1d4ecb98 292 else if (streq(argv[0], "list-x11-keymap-options"))
50cfc579
LP
293 look_for = OPTIONS;
294 else
04499a70 295 assert_not_reached();
50cfc579 296
271c8ec5
LP
297 for (;;) {
298 _cleanup_free_ char *line = NULL;
50cfc579
LP
299 char *l, *w;
300
271c8ec5
LP
301 r = read_line(f, LONG_LINE_MAX, &line);
302 if (r < 0)
303 return log_error_errno(r, "Failed to read keyboard mapping list: %m");
304 if (r == 0)
305 break;
306
50cfc579
LP
307 l = strstrip(line);
308
309 if (isempty(l))
310 continue;
311
312 if (l[0] == '!') {
313 if (startswith(l, "! model"))
314 state = MODELS;
315 else if (startswith(l, "! layout"))
316 state = LAYOUTS;
317 else if (startswith(l, "! variant"))
318 state = VARIANTS;
319 else if (startswith(l, "! option"))
320 state = OPTIONS;
321 else
322 state = NONE;
323
324 continue;
325 }
326
327 if (state != look_for)
328 continue;
329
330 w = l + strcspn(l, WHITESPACE);
331
1d4ecb98 332 if (argc > 1) {
50cfc579
LP
333 char *e;
334
335 if (*w == 0)
336 continue;
337
338 *w = 0;
339 w++;
340 w += strspn(w, WHITESPACE);
341
342 e = strchr(w, ':');
343 if (!e)
344 continue;
345
346 *e = 0;
347
1d4ecb98 348 if (!streq(w, argv[1]))
50cfc579
LP
349 continue;
350 } else
351 *w = 0;
352
7d6884b6
TA
353 r = strv_extend(&list, l);
354 if (r < 0)
355 return log_oom();
50cfc579
LP
356 }
357
baaa35ad
ZJS
358 if (strv_isempty(list))
359 return log_error_errno(SYNTHETIC_ERRNO(ENOENT),
360 "Couldn't find any entries.");
50cfc579
LP
361
362 strv_sort(list);
363 strv_uniq(list);
364
384c2c32 365 pager_open(arg_pager_flags);
50cfc579
LP
366
367 strv_print(list);
368 return 0;
369}
370
1d4ecb98 371static int help(void) {
37ec0fdd
LP
372 _cleanup_free_ char *link = NULL;
373 int r;
374
375 r = terminal_urlify_man("localectl", "1", &link);
376 if (r < 0)
377 return log_oom();
378
353b2baa
LP
379 printf("%s [OPTIONS...] COMMAND ...\n\n"
380 "%sQuery or change system locale and keyboard settings.%s\n"
381 "\nCommands:\n"
50cfc579
LP
382 " status Show current locale settings\n"
383 " set-locale LOCALE... Set system locale\n"
384 " list-locales Show known locales\n"
2ebcf936 385 " set-keymap MAP [MAP] Set console and X11 keyboard mappings\n"
50cfc579 386 " list-keymaps Show known virtual console keyboard mappings\n"
31cf921a 387 " set-x11-keymap LAYOUT [MODEL [VARIANT [OPTIONS]]]\n"
2ebcf936 388 " Set X11 and console keyboard mappings\n"
50cfc579
LP
389 " list-x11-keymap-models Show known X11 keyboard mapping models\n"
390 " list-x11-keymap-layouts Show known X11 keyboard mapping layouts\n"
391 " list-x11-keymap-variants [LAYOUT]\n"
392 " Show known X11 keyboard mapping variants\n"
601185b4 393 " list-x11-keymap-options Show known X11 keyboard mapping options\n"
353b2baa
LP
394 "\nOptions:\n"
395 " -h --help Show this help\n"
396 " --version Show package version\n"
397 " --no-pager Do not pipe output into a pager\n"
398 " --no-ask-password Do not prompt for password\n"
399 " -H --host=[USER@]HOST Operate on remote host\n"
400 " -M --machine=CONTAINER Operate on local container\n"
401 " --no-convert Don't convert keyboard mappings\n"
bc556335
DDM
402 "\nSee the %s for details.\n",
403 program_invocation_short_name,
404 ansi_highlight(),
405 ansi_normal(),
406 link);
1d4ecb98
YW
407
408 return 0;
409}
410
411static int verb_help(int argc, char **argv, void *userdata) {
412 return help();
2087a7af
LP
413}
414
415static int parse_argv(int argc, char *argv[]) {
416
417 enum {
418 ARG_VERSION = 0x100,
419 ARG_NO_PAGER,
420 ARG_NO_CONVERT,
421 ARG_NO_ASK_PASSWORD
422 };
423
424 static const struct option options[] = {
4d7859d1
KS
425 { "help", no_argument, NULL, 'h' },
426 { "version", no_argument, NULL, ARG_VERSION },
427 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
428 { "host", required_argument, NULL, 'H' },
429 { "machine", required_argument, NULL, 'M' },
430 { "no-ask-password", no_argument, NULL, ARG_NO_ASK_PASSWORD },
431 { "no-convert", no_argument, NULL, ARG_NO_CONVERT },
eb9da376 432 {}
2087a7af
LP
433 };
434
435 int c;
436
437 assert(argc >= 0);
438 assert(argv);
439
601185b4 440 while ((c = getopt_long(argc, argv, "hH:M:", options, NULL)) >= 0)
2087a7af
LP
441
442 switch (c) {
443
444 case 'h':
1d4ecb98 445 return help();
2087a7af
LP
446
447 case ARG_VERSION:
3f6fd1ba 448 return version();
2087a7af 449
2087a7af
LP
450 case ARG_NO_CONVERT:
451 arg_convert = false;
452 break;
453
454 case ARG_NO_PAGER:
0221d68a 455 arg_pager_flags |= PAGER_DISABLE;
2087a7af
LP
456 break;
457
546158bc
JJ
458 case ARG_NO_ASK_PASSWORD:
459 arg_ask_password = false;
460 break;
461
4d7859d1
KS
462 case 'H':
463 arg_transport = BUS_TRANSPORT_REMOTE;
464 arg_host = optarg;
465 break;
466
467 case 'M':
de33fc62 468 arg_transport = BUS_TRANSPORT_MACHINE;
4d7859d1
KS
469 arg_host = optarg;
470 break;
471
2087a7af
LP
472 case '?':
473 return -EINVAL;
474
475 default:
04499a70 476 assert_not_reached();
2087a7af 477 }
2087a7af
LP
478
479 return 1;
480}
481
4d7859d1 482static int localectl_main(sd_bus *bus, int argc, char *argv[]) {
2087a7af 483
1d4ecb98
YW
484 static const Verb verbs[] = {
485 { "status", VERB_ANY, 1, VERB_DEFAULT, show_status },
486 { "set-locale", 2, VERB_ANY, 0, set_locale },
487 { "list-locales", VERB_ANY, 1, 0, list_locales },
488 { "set-keymap", 2, 3, 0, set_vconsole_keymap },
489 { "list-keymaps", VERB_ANY, 1, 0, list_vconsole_keymaps },
490 { "set-x11-keymap", 2, 5, 0, set_x11_keymap },
491 { "list-x11-keymap-models", VERB_ANY, 1, 0, list_x11_keymaps },
492 { "list-x11-keymap-layouts", VERB_ANY, 1, 0, list_x11_keymaps },
493 { "list-x11-keymap-variants", VERB_ANY, 2, 0, list_x11_keymaps },
494 { "list-x11-keymap-options", VERB_ANY, 1, 0, list_x11_keymaps },
495 { "help", VERB_ANY, VERB_ANY, 0, verb_help }, /* Not documented, but supported since it is created. */
496 {}
2087a7af
LP
497 };
498
1d4ecb98 499 return dispatch_verb(argc, argv, verbs, bus);
2087a7af
LP
500}
501
f2a3de01 502static int run(int argc, char *argv[]) {
39daad0a 503 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
84f6181c 504 int r;
2087a7af 505
a9cdc94f 506 setlocale(LC_ALL, "");
d2acb93d 507 log_setup();
2087a7af
LP
508
509 r = parse_argv(argc, argv);
84f6181c 510 if (r <= 0)
39daad0a 511 return r;
2087a7af 512
266f3e26 513 r = bus_connect_transport(arg_transport, arg_host, false, &bus);
39daad0a 514 if (r < 0)
ddbab78f 515 return bus_log_connect_error(r);
2087a7af 516
39daad0a 517 return localectl_main(bus, argc, argv);
2087a7af 518}
39daad0a
ZJS
519
520DEFINE_MAIN_FUNCTION(run);