| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "sd-bus.h" |
| 4 |
|
| 5 |
#include "ansi-color.h" |
| 6 |
#include "alloc-util.h" |
| 7 |
#include "build.h" |
| 8 |
#include "bus-error.h" |
| 9 |
#include "bus-locator.h" |
| 10 |
#include "bus-map-properties.h" |
| 11 |
#include "bus-util.h" |
| 12 |
#include "fd-util.h" |
| 13 |
#include "fileio.h" |
| 14 |
#include "format-table.h" |
| 15 |
#include "help-util.h" |
| 16 |
#include "kbd-util.h" |
| 17 |
#include "locale-setup.h" |
| 18 |
#include "main-func.h" |
| 19 |
#include "memory-util.h" |
| 20 |
#include "options.h" |
| 21 |
#include "pager.h" |
| 22 |
#include "parse-argument.h" |
| 23 |
#include "path-util.h" |
| 24 |
#include "polkit-agent.h" |
| 25 |
#include "runtime-scope.h" |
| 26 |
#include "string-util.h" |
| 27 |
#include "strv.h" |
| 28 |
#include "time-util.h" |
| 29 |
#include "verbs.h" |
| 30 |
|
| 31 |
/* Enough time for locale-gen to finish server-side (in case it is in use) */ |
| 32 |
#define LOCALE_SLOW_BUS_CALL_TIMEOUT_USEC (2*USEC_PER_MINUTE) |
| 33 |
|
| 34 |
static PagerFlags arg_pager_flags = 0; |
| 35 |
static bool arg_ask_password = true; |
| 36 |
static BusTransport arg_transport = BUS_TRANSPORT_LOCAL; |
| 37 |
static const char *arg_host = NULL; |
| 38 |
static bool arg_convert = true; |
| 39 |
static bool arg_full = false; |
| 40 |
|
| 41 |
typedef struct StatusInfo { |
| 42 |
char **locale; |
| 43 |
const char *vconsole_keymap; |
| 44 |
const char *vconsole_keymap_toggle; |
| 45 |
const char *x11_layout; |
| 46 |
const char *x11_model; |
| 47 |
const char *x11_variant; |
| 48 |
const char *x11_options; |
| 49 |
} StatusInfo; |
| 50 |
|
| 51 |
static void status_info_clear(StatusInfo *info) { |
| 52 |
if (info) { |
| 53 |
strv_free(info->locale); |
| 54 |
zero(*info); |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
static int print_status_info(StatusInfo *i) { |
| 59 |
_cleanup_strv_free_ char **kernel_locale = NULL; |
| 60 |
_cleanup_(table_unrefp) Table *table = NULL; |
| 61 |
TableCell *cell; |
| 62 |
int r; |
| 63 |
|
| 64 |
assert(i); |
| 65 |
|
| 66 |
if (arg_transport == BUS_TRANSPORT_LOCAL) { |
| 67 |
_cleanup_(locale_context_clear) LocaleContext c = {}; |
| 68 |
|
| 69 |
r = locale_context_load(&c, LOCALE_LOAD_PROC_CMDLINE); |
| 70 |
if (r < 0) |
| 71 |
return log_error_errno(r, "Failed to read /proc/cmdline: %m"); |
| 72 |
|
| 73 |
r = locale_context_build_env(&c, &kernel_locale, NULL); |
| 74 |
if (r < 0) |
| 75 |
return log_error_errno(r, "Failed to build locale settings from kernel command line: %m"); |
| 76 |
} |
| 77 |
|
| 78 |
table = table_new_vertical(); |
| 79 |
if (!table) |
| 80 |
return log_oom(); |
| 81 |
|
| 82 |
if (arg_full) |
| 83 |
table_set_width(table, 0); |
| 84 |
|
| 85 |
assert_se(cell = table_get_cell(table, 0, 0)); |
| 86 |
(void) table_set_ellipsize_percent(table, cell, 100); |
| 87 |
|
| 88 |
table_set_ersatz_string(table, TABLE_ERSATZ_UNSET); |
| 89 |
|
| 90 |
if (!strv_isempty(kernel_locale)) { |
| 91 |
log_warning("Warning: Settings on kernel command line override system locale settings in /etc/locale.conf."); |
| 92 |
r = table_add_many(table, |
| 93 |
TABLE_FIELD, "Command Line", |
| 94 |
TABLE_SET_COLOR, ansi_highlight_yellow(), |
| 95 |
TABLE_STRV, kernel_locale, |
| 96 |
TABLE_SET_COLOR, ansi_highlight_yellow()); |
| 97 |
if (r < 0) |
| 98 |
return table_log_add_error(r); |
| 99 |
} |
| 100 |
|
| 101 |
r = table_add_many(table, |
| 102 |
TABLE_FIELD, "System Locale", |
| 103 |
TABLE_STRV, i->locale, |
| 104 |
TABLE_FIELD, "VC Keymap", |
| 105 |
TABLE_STRING, i->vconsole_keymap); |
| 106 |
if (r < 0) |
| 107 |
return table_log_add_error(r); |
| 108 |
|
| 109 |
if (!isempty(i->vconsole_keymap_toggle)) { |
| 110 |
r = table_add_many(table, |
| 111 |
TABLE_FIELD, "VC Toggle Keymap", |
| 112 |
TABLE_STRING, i->vconsole_keymap_toggle); |
| 113 |
if (r < 0) |
| 114 |
return table_log_add_error(r); |
| 115 |
} |
| 116 |
|
| 117 |
r = table_add_many(table, |
| 118 |
TABLE_FIELD, "X11 Layout", |
| 119 |
TABLE_STRING, i->x11_layout); |
| 120 |
if (r < 0) |
| 121 |
return table_log_add_error(r); |
| 122 |
|
| 123 |
if (!isempty(i->x11_model)) { |
| 124 |
r = table_add_many(table, |
| 125 |
TABLE_FIELD, "X11 Model", |
| 126 |
TABLE_STRING, i->x11_model); |
| 127 |
if (r < 0) |
| 128 |
return table_log_add_error(r); |
| 129 |
} |
| 130 |
|
| 131 |
if (!isempty(i->x11_variant)) { |
| 132 |
r = table_add_many(table, |
| 133 |
TABLE_FIELD, "X11 Variant", |
| 134 |
TABLE_STRING, i->x11_variant); |
| 135 |
if (r < 0) |
| 136 |
return table_log_add_error(r); |
| 137 |
} |
| 138 |
|
| 139 |
if (!isempty(i->x11_options)) { |
| 140 |
r = table_add_many(table, |
| 141 |
TABLE_FIELD, "X11 Options", |
| 142 |
TABLE_STRING, i->x11_options); |
| 143 |
if (r < 0) |
| 144 |
return table_log_add_error(r); |
| 145 |
} |
| 146 |
|
| 147 |
return table_print_or_warn(table); |
| 148 |
} |
| 149 |
|
| 150 |
VERB_DEFAULT_NOARG(verb_show_status, "status", "Show current locale settings"); |
| 151 |
static int verb_show_status(int argc, char *argv[], uintptr_t _data, void *userdata) { |
| 152 |
_cleanup_(status_info_clear) StatusInfo info = {}; |
| 153 |
static const struct bus_properties_map map[] = { |
| 154 |
{ "VConsoleKeymap", "s", NULL, offsetof(StatusInfo, vconsole_keymap) }, |
| 155 |
{ "VConsoleKeymapToggle", "s", NULL, offsetof(StatusInfo, vconsole_keymap_toggle) }, |
| 156 |
{ "X11Layout", "s", NULL, offsetof(StatusInfo, x11_layout) }, |
| 157 |
{ "X11Model", "s", NULL, offsetof(StatusInfo, x11_model) }, |
| 158 |
{ "X11Variant", "s", NULL, offsetof(StatusInfo, x11_variant) }, |
| 159 |
{ "X11Options", "s", NULL, offsetof(StatusInfo, x11_options) }, |
| 160 |
{ "Locale", "as", NULL, offsetof(StatusInfo, locale) }, |
| 161 |
{} |
| 162 |
}; |
| 163 |
|
| 164 |
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
| 165 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; |
| 166 |
sd_bus *bus = ASSERT_PTR(userdata); |
| 167 |
int r; |
| 168 |
|
| 169 |
r = bus_map_all_properties(bus, |
| 170 |
"org.freedesktop.locale1", |
| 171 |
"/org/freedesktop/locale1", |
| 172 |
map, |
| 173 |
0, |
| 174 |
&error, |
| 175 |
&m, |
| 176 |
&info); |
| 177 |
if (r < 0) |
| 178 |
return log_error_errno(r, "Could not get properties: %s", bus_error_message(&error, r)); |
| 179 |
|
| 180 |
return print_status_info(&info); |
| 181 |
} |
| 182 |
|
| 183 |
VERB(verb_set_locale, "set-locale", "LOCALE...", 2, VERB_ANY, 0, |
| 184 |
"Set system locale"); |
| 185 |
static int verb_set_locale(int argc, char *argv[], uintptr_t _data, void *userdata) { |
| 186 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; |
| 187 |
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
| 188 |
sd_bus *bus = ASSERT_PTR(userdata); |
| 189 |
int r; |
| 190 |
|
| 191 |
(void) polkit_agent_open_if_enabled(arg_transport, arg_ask_password); |
| 192 |
|
| 193 |
r = bus_message_new_method_call(bus, &m, bus_locale, "SetLocale"); |
| 194 |
if (r < 0) |
| 195 |
return bus_log_create_error(r); |
| 196 |
|
| 197 |
r = sd_bus_message_append_strv(m, argv + 1); |
| 198 |
if (r < 0) |
| 199 |
return bus_log_create_error(r); |
| 200 |
|
| 201 |
r = sd_bus_message_append(m, "b", arg_ask_password); |
| 202 |
if (r < 0) |
| 203 |
return bus_log_create_error(r); |
| 204 |
|
| 205 |
/* We use a longer timeout for the method call in case localed is running locale-gen */ |
| 206 |
r = sd_bus_call(bus, m, LOCALE_SLOW_BUS_CALL_TIMEOUT_USEC, &error, NULL); |
| 207 |
if (r < 0) |
| 208 |
return log_error_errno(r, "Failed to issue method call: %s", bus_error_message(&error, r)); |
| 209 |
|
| 210 |
return 0; |
| 211 |
} |
| 212 |
|
| 213 |
VERB_NOARG(verb_list_locales, "list-locales", |
| 214 |
"Show known locales"); |
| 215 |
static int verb_list_locales(int argc, char *argv[], uintptr_t _data, void *userdata) { |
| 216 |
_cleanup_strv_free_ char **l = NULL; |
| 217 |
int r; |
| 218 |
|
| 219 |
r = get_locales(&l); |
| 220 |
if (r < 0) |
| 221 |
return log_error_errno(r, "Failed to read list of locales: %m"); |
| 222 |
|
| 223 |
pager_open(arg_pager_flags); |
| 224 |
strv_print(l); |
| 225 |
|
| 226 |
return 0; |
| 227 |
} |
| 228 |
|
| 229 |
VERB(verb_set_vconsole_keymap, "set-keymap", "MAP [MAP]", 2, 3, 0, |
| 230 |
"Set console and X11 keyboard mappings"); |
| 231 |
static int verb_set_vconsole_keymap(int argc, char *argv[], uintptr_t _data, void *userdata) { |
| 232 |
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
| 233 |
const char *map, *toggle_map; |
| 234 |
sd_bus *bus = ASSERT_PTR(userdata); |
| 235 |
int r; |
| 236 |
|
| 237 |
(void) polkit_agent_open_if_enabled(arg_transport, arg_ask_password); |
| 238 |
|
| 239 |
map = argv[1]; |
| 240 |
toggle_map = argc > 2 ? argv[2] : ""; |
| 241 |
|
| 242 |
r = bus_call_method( |
| 243 |
bus, |
| 244 |
bus_locale, |
| 245 |
"SetVConsoleKeyboard", |
| 246 |
&error, |
| 247 |
NULL, |
| 248 |
"ssbb", map, toggle_map, arg_convert, arg_ask_password); |
| 249 |
if (r < 0) |
| 250 |
return log_error_errno(r, "Failed to set keymap: %s", bus_error_message(&error, r)); |
| 251 |
|
| 252 |
return 0; |
| 253 |
} |
| 254 |
|
| 255 |
VERB_NOARG(verb_list_vconsole_keymaps, "list-keymaps", |
| 256 |
"Show known virtual console keyboard mappings"); |
| 257 |
static int verb_list_vconsole_keymaps(int argc, char *argv[], uintptr_t _data, void *userdata) { |
| 258 |
_cleanup_strv_free_ char **l = NULL; |
| 259 |
int r; |
| 260 |
|
| 261 |
r = get_keymaps(&l); |
| 262 |
if (r < 0) |
| 263 |
return log_error_errno(r, "Failed to read list of keymaps: %m"); |
| 264 |
|
| 265 |
pager_open(arg_pager_flags); |
| 266 |
|
| 267 |
strv_print(l); |
| 268 |
|
| 269 |
return 0; |
| 270 |
} |
| 271 |
|
| 272 |
VERB(verb_set_x11_keymap, "set-x11-keymap", "LAYOUT [MODEL [VARIANT [OPTIONS]]]", 2, 5, 0, |
| 273 |
"Set X11 and console keyboard mappings"); |
| 274 |
static int verb_set_x11_keymap(int argc, char *argv[], uintptr_t _data, void *userdata) { |
| 275 |
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
| 276 |
const char *layout, *model, *variant, *options; |
| 277 |
sd_bus *bus = userdata; |
| 278 |
int r; |
| 279 |
|
| 280 |
(void) polkit_agent_open_if_enabled(arg_transport, arg_ask_password); |
| 281 |
|
| 282 |
layout = argv[1]; |
| 283 |
model = argc > 2 ? argv[2] : ""; |
| 284 |
variant = argc > 3 ? argv[3] : ""; |
| 285 |
options = argc > 4 ? argv[4] : ""; |
| 286 |
|
| 287 |
r = bus_call_method( |
| 288 |
bus, |
| 289 |
bus_locale, |
| 290 |
"SetX11Keyboard", |
| 291 |
&error, |
| 292 |
NULL, |
| 293 |
"ssssbb", layout, model, variant, options, |
| 294 |
arg_convert, arg_ask_password); |
| 295 |
if (r < 0) |
| 296 |
return log_error_errno(r, "Failed to set keymap: %s", bus_error_message(&error, r)); |
| 297 |
|
| 298 |
return 0; |
| 299 |
} |
| 300 |
|
| 301 |
static const char* xkb_directory(void) { |
| 302 |
static const char *cached = NULL; |
| 303 |
|
| 304 |
if (!cached) |
| 305 |
cached = secure_getenv("SYSTEMD_XKB_DIRECTORY") ?: "/usr/share/X11/xkb"; |
| 306 |
return cached; |
| 307 |
} |
| 308 |
|
| 309 |
VERB_NOARG(verb_list_x11_keymaps, "list-x11-keymap-models", |
| 310 |
"Show known X11 keyboard mapping models"); |
| 311 |
VERB_NOARG(verb_list_x11_keymaps, "list-x11-keymap-layouts", |
| 312 |
"Show known X11 keyboard mapping layouts"); |
| 313 |
VERB(verb_list_x11_keymaps, "list-x11-keymap-variants", "[LAYOUT]", VERB_ANY, 2, 0, |
| 314 |
"Show known X11 keyboard mapping variants"); |
| 315 |
VERB_NOARG(verb_list_x11_keymaps, "list-x11-keymap-options", |
| 316 |
"Show known X11 keyboard mapping options"); |
| 317 |
static int verb_list_x11_keymaps(int argc, char *argv[], uintptr_t _data, void *userdata) { |
| 318 |
_cleanup_fclose_ FILE *f = NULL; |
| 319 |
_cleanup_strv_free_ char **list = NULL; |
| 320 |
enum { |
| 321 |
NONE, |
| 322 |
MODELS, |
| 323 |
LAYOUTS, |
| 324 |
VARIANTS, |
| 325 |
OPTIONS |
| 326 |
} state = NONE, look_for; |
| 327 |
int r; |
| 328 |
|
| 329 |
_cleanup_free_ char *xkb_base = path_join(xkb_directory(), "rules/base.lst"); |
| 330 |
if (!xkb_base) |
| 331 |
return log_oom(); |
| 332 |
|
| 333 |
f = fopen(xkb_base, "re"); |
| 334 |
if (!f) |
| 335 |
return log_error_errno(errno, |
| 336 |
"Failed to open keyboard mapping list %s: %m", |
| 337 |
xkb_base); |
| 338 |
|
| 339 |
if (streq(argv[0], "list-x11-keymap-models")) |
| 340 |
look_for = MODELS; |
| 341 |
else if (streq(argv[0], "list-x11-keymap-layouts")) |
| 342 |
look_for = LAYOUTS; |
| 343 |
else if (streq(argv[0], "list-x11-keymap-variants")) |
| 344 |
look_for = VARIANTS; |
| 345 |
else if (streq(argv[0], "list-x11-keymap-options")) |
| 346 |
look_for = OPTIONS; |
| 347 |
else |
| 348 |
assert_not_reached(); |
| 349 |
|
| 350 |
for (;;) { |
| 351 |
_cleanup_free_ char *line = NULL; |
| 352 |
char *w; |
| 353 |
|
| 354 |
r = read_stripped_line(f, LONG_LINE_MAX, &line); |
| 355 |
if (r < 0) |
| 356 |
return log_error_errno(r, |
| 357 |
"Failed to read keyboard mapping list %s: %m", |
| 358 |
xkb_base); |
| 359 |
if (r == 0) |
| 360 |
break; |
| 361 |
|
| 362 |
if (isempty(line)) |
| 363 |
continue; |
| 364 |
|
| 365 |
if (line[0] == '!') { |
| 366 |
if (startswith(line, "! model")) |
| 367 |
state = MODELS; |
| 368 |
else if (startswith(line, "! layout")) |
| 369 |
state = LAYOUTS; |
| 370 |
else if (startswith(line, "! variant")) |
| 371 |
state = VARIANTS; |
| 372 |
else if (startswith(line, "! option")) |
| 373 |
state = OPTIONS; |
| 374 |
else |
| 375 |
state = NONE; |
| 376 |
|
| 377 |
continue; |
| 378 |
} |
| 379 |
|
| 380 |
if (state != look_for) |
| 381 |
continue; |
| 382 |
|
| 383 |
w = line + strcspn(line, WHITESPACE); |
| 384 |
|
| 385 |
if (argc > 1) { |
| 386 |
char *e; |
| 387 |
|
| 388 |
if (*w == 0) |
| 389 |
continue; |
| 390 |
|
| 391 |
*w = 0; |
| 392 |
w++; |
| 393 |
w += strspn(w, WHITESPACE); |
| 394 |
|
| 395 |
e = strchr(w, ':'); |
| 396 |
if (!e) |
| 397 |
continue; |
| 398 |
|
| 399 |
*e = 0; |
| 400 |
|
| 401 |
if (!streq(w, argv[1])) |
| 402 |
continue; |
| 403 |
} else |
| 404 |
*w = 0; |
| 405 |
|
| 406 |
if (strv_consume(&list, TAKE_PTR(line)) < 0) |
| 407 |
return log_oom(); |
| 408 |
} |
| 409 |
|
| 410 |
if (strv_isempty(list)) |
| 411 |
return log_error_errno(SYNTHETIC_ERRNO(ENOENT), |
| 412 |
"Couldn't find any entries in keyboard mapping list %s.", |
| 413 |
xkb_base); |
| 414 |
|
| 415 |
strv_sort_uniq(list); |
| 416 |
|
| 417 |
pager_open(arg_pager_flags); |
| 418 |
|
| 419 |
strv_print(list); |
| 420 |
return 0; |
| 421 |
} |
| 422 |
|
| 423 |
static int help(void) { |
| 424 |
_cleanup_(table_unrefp) Table *options = NULL, *verbs = NULL; |
| 425 |
int r; |
| 426 |
|
| 427 |
r = verbs_get_help_table(&verbs); |
| 428 |
if (r < 0) |
| 429 |
return r; |
| 430 |
|
| 431 |
r = option_parser_get_help_table(&options); |
| 432 |
if (r < 0) |
| 433 |
return r; |
| 434 |
|
| 435 |
(void) table_sync_column_widths(0, verbs, options); |
| 436 |
|
| 437 |
help_cmdline("[OPTIONS…] COMMAND …"); |
| 438 |
help_abstract("Query or change system locale and keyboard settings."); |
| 439 |
|
| 440 |
help_section("Commands"); |
| 441 |
r = table_print_or_warn(verbs); |
| 442 |
if (r < 0) |
| 443 |
return r; |
| 444 |
|
| 445 |
help_section("Options"); |
| 446 |
r = table_print_or_warn(options); |
| 447 |
if (r < 0) |
| 448 |
return r; |
| 449 |
|
| 450 |
help_man_page_reference("localectl", "1"); |
| 451 |
|
| 452 |
return 0; |
| 453 |
} |
| 454 |
|
| 455 |
VERB_COMMON_HELP_HIDDEN(help); |
| 456 |
|
| 457 |
static int parse_argv(int argc, char *argv[], char ***remaining_args) { |
| 458 |
assert(argc >= 0); |
| 459 |
assert(argv); |
| 460 |
assert(remaining_args); |
| 461 |
|
| 462 |
OptionParser opts = { argc, argv }; |
| 463 |
int r; |
| 464 |
|
| 465 |
FOREACH_OPTION_OR_RETURN(c, &opts) |
| 466 |
switch (c) { |
| 467 |
|
| 468 |
OPTION_COMMON_HELP: |
| 469 |
return help(); |
| 470 |
|
| 471 |
OPTION_COMMON_VERSION: |
| 472 |
return version(); |
| 473 |
|
| 474 |
OPTION('l', "full", NULL, "Do not ellipsize output"): |
| 475 |
arg_full = true; |
| 476 |
break; |
| 477 |
|
| 478 |
OPTION_COMMON_NO_PAGER: |
| 479 |
arg_pager_flags |= PAGER_DISABLE; |
| 480 |
break; |
| 481 |
|
| 482 |
OPTION_COMMON_NO_ASK_PASSWORD: |
| 483 |
arg_ask_password = false; |
| 484 |
break; |
| 485 |
|
| 486 |
OPTION_COMMON_HOST: |
| 487 |
arg_transport = BUS_TRANSPORT_REMOTE; |
| 488 |
arg_host = opts.arg; |
| 489 |
break; |
| 490 |
|
| 491 |
OPTION_COMMON_MACHINE: |
| 492 |
r = parse_machine_argument(opts.arg, &arg_host, &arg_transport); |
| 493 |
if (r < 0) |
| 494 |
return r; |
| 495 |
break; |
| 496 |
|
| 497 |
OPTION_LONG("no-convert", NULL, "Don't convert keyboard mappings"): |
| 498 |
arg_convert = false; |
| 499 |
break; |
| 500 |
} |
| 501 |
|
| 502 |
*remaining_args = option_parser_get_args(&opts); |
| 503 |
return 1; |
| 504 |
} |
| 505 |
|
| 506 |
static int run(int argc, char *argv[]) { |
| 507 |
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; |
| 508 |
int r; |
| 509 |
|
| 510 |
setlocale(LC_ALL, ""); |
| 511 |
log_setup(); |
| 512 |
|
| 513 |
char **args = NULL; |
| 514 |
r = parse_argv(argc, argv, &args); |
| 515 |
if (r <= 0) |
| 516 |
return r; |
| 517 |
|
| 518 |
r = bus_connect_transport(arg_transport, arg_host, RUNTIME_SCOPE_SYSTEM, &bus); |
| 519 |
if (r < 0) |
| 520 |
return bus_log_connect_error(r, arg_transport, RUNTIME_SCOPE_SYSTEM); |
| 521 |
|
| 522 |
return dispatch_verb(args, bus); |
| 523 |
} |
| 524 |
|
| 525 |
DEFINE_MAIN_FUNCTION(run); |