]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/locale/localectl.c
tree-wide: beautify remaining copyright statements
[thirdparty/systemd.git] / src / locale / localectl.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 Copyright © 2013 Kay Sievers
4 ***/
5
6 #include <ftw.h>
7 #include <getopt.h>
8 #include <locale.h>
9 #include <stdbool.h>
10 #include <stdlib.h>
11 #include <string.h>
12
13 #include "sd-bus.h"
14
15 #include "bus-error.h"
16 #include "bus-util.h"
17 #include "def.h"
18 #include "fd-util.h"
19 #include "fileio.h"
20 #include "locale-util.h"
21 #include "pager.h"
22 #include "set.h"
23 #include "spawn-polkit-agent.h"
24 #include "strv.h"
25 #include "util.h"
26 #include "verbs.h"
27 #include "virt.h"
28
29 static bool arg_no_pager = false;
30 static bool arg_ask_password = true;
31 static BusTransport arg_transport = BUS_TRANSPORT_LOCAL;
32 static char *arg_host = NULL;
33 static bool arg_convert = true;
34
35 typedef struct StatusInfo {
36 char **locale;
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;
43 } StatusInfo;
44
45 static void status_info_clear(StatusInfo *info) {
46 if (info) {
47 strv_free(info->locale);
48 zero(*info);
49 }
50 }
51
52 static void print_overridden_variables(void) {
53 int r;
54 char *variables[_VARIABLE_LC_MAX] = {};
55 LocaleVariable j;
56 bool print_warning = true;
57
58 if (detect_container() > 0 || arg_host)
59 return;
60
61 r = parse_env_file(NULL, "/proc/cmdline", WHITESPACE,
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) {
79 log_warning_errno(r, "Failed to read /proc/cmdline: %m");
80 goto finish;
81 }
82
83 for (j = 0; j < _VARIABLE_LC_MAX; j++)
84 if (variables[j]) {
85 if (print_warning) {
86 log_warning("Warning: Settings on kernel command line override system locale settings in /etc/locale.conf.\n"
87 " Command Line: %s=%s", locale_variable_to_string(j), variables[j]);
88
89 print_warning = false;
90 } else
91 log_warning(" %s=%s", locale_variable_to_string(j), variables[j]);
92 }
93 finish:
94 for (j = 0; j < _VARIABLE_LC_MAX; j++)
95 free(variables[j]);
96 }
97
98 static void print_status_info(StatusInfo *i) {
99 assert(i);
100
101 if (strv_isempty(i->locale))
102 puts(" System Locale: n/a");
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
124 static int show_status(int argc, char **argv, void *userdata) {
125 _cleanup_(status_info_clear) StatusInfo info = {};
126 static const struct bus_properties_map map[] = {
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) },
134 {}
135 };
136
137 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
138 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
139 sd_bus *bus = userdata;
140 int r;
141
142 assert(bus);
143
144 r = bus_map_all_properties(bus,
145 "org.freedesktop.locale1",
146 "/org/freedesktop/locale1",
147 map,
148 0,
149 &error,
150 &m,
151 &info);
152 if (r < 0)
153 return log_error_errno(r, "Could not get properties: %s", bus_error_message(&error, r));
154
155 print_overridden_variables();
156 print_status_info(&info);
157
158 return r;
159 }
160
161 static int set_locale(int argc, char **argv, void *userdata) {
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;
164 sd_bus *bus = userdata;
165 int r;
166
167 assert(bus);
168
169 polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
170
171 r = sd_bus_message_new_method_call(
172 bus,
173 &m,
174 "org.freedesktop.locale1",
175 "/org/freedesktop/locale1",
176 "org.freedesktop.locale1",
177 "SetLocale");
178 if (r < 0)
179 return bus_log_create_error(r);
180
181 r = sd_bus_message_append_strv(m, argv + 1);
182 if (r < 0)
183 return bus_log_create_error(r);
184
185 r = sd_bus_message_append(m, "b", arg_ask_password);
186 if (r < 0)
187 return bus_log_create_error(r);
188
189 r = sd_bus_call(bus, m, 0, &error, NULL);
190 if (r < 0) {
191 log_error("Failed to issue method call: %s", bus_error_message(&error, -r));
192 return r;
193 }
194
195 return 0;
196 }
197
198 static int list_locales(int argc, char **argv, void *userdata) {
199 _cleanup_strv_free_ char **l = NULL;
200 int r;
201
202 r = get_locales(&l);
203 if (r < 0)
204 return log_error_errno(r, "Failed to read list of locales: %m");
205
206 (void) pager_open(arg_no_pager, false);
207 strv_print(l);
208
209 return 0;
210 }
211
212 static int set_vconsole_keymap(int argc, char **argv, void *userdata) {
213 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
214 const char *map, *toggle_map;
215 sd_bus *bus = userdata;
216 int r;
217
218 assert(bus);
219
220 polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
221
222 map = argv[1];
223 toggle_map = argc > 2 ? argv[2] : "";
224
225 r = sd_bus_call_method(
226 bus,
227 "org.freedesktop.locale1",
228 "/org/freedesktop/locale1",
229 "org.freedesktop.locale1",
230 "SetVConsoleKeyboard",
231 &error,
232 NULL,
233 "ssbb", map, toggle_map, arg_convert, arg_ask_password);
234 if (r < 0)
235 log_error("Failed to set keymap: %s", bus_error_message(&error, -r));
236
237 return r;
238 }
239
240 static int list_vconsole_keymaps(int argc, char **argv, void *userdata) {
241 _cleanup_strv_free_ char **l = NULL;
242 int r;
243
244 r = get_keymaps(&l);
245 if (r < 0)
246 return log_error_errno(r, "Failed to read list of keymaps: %m");
247
248 (void) pager_open(arg_no_pager, false);
249
250 strv_print(l);
251
252 return 0;
253 }
254
255 static int set_x11_keymap(int argc, char **argv, void *userdata) {
256 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
257 const char *layout, *model, *variant, *options;
258 sd_bus *bus = userdata;
259 int r;
260
261 polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
262
263 layout = argv[1];
264 model = argc > 2 ? argv[2] : "";
265 variant = argc > 3 ? argv[3] : "";
266 options = argc > 4 ? argv[4] : "";
267
268 r = sd_bus_call_method(
269 bus,
270 "org.freedesktop.locale1",
271 "/org/freedesktop/locale1",
272 "org.freedesktop.locale1",
273 "SetX11Keyboard",
274 &error,
275 NULL,
276 "ssssbb", layout, model, variant, options,
277 arg_convert, arg_ask_password);
278 if (r < 0)
279 log_error("Failed to set keymap: %s", bus_error_message(&error, -r));
280
281 return r;
282 }
283
284 static int list_x11_keymaps(int argc, char **argv, void *userdata) {
285 _cleanup_fclose_ FILE *f = NULL;
286 _cleanup_strv_free_ char **list = NULL;
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
297 f = fopen("/usr/share/X11/xkb/rules/base.lst", "re");
298 if (!f)
299 return log_error_errno(errno, "Failed to open keyboard mapping list. %m");
300
301 if (streq(argv[0], "list-x11-keymap-models"))
302 look_for = MODELS;
303 else if (streq(argv[0], "list-x11-keymap-layouts"))
304 look_for = LAYOUTS;
305 else if (streq(argv[0], "list-x11-keymap-variants"))
306 look_for = VARIANTS;
307 else if (streq(argv[0], "list-x11-keymap-options"))
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
340 if (argc > 1) {
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
356 if (!streq(w, argv[1]))
357 continue;
358 } else
359 *w = 0;
360
361 r = strv_extend(&list, l);
362 if (r < 0)
363 return log_oom();
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
374 (void) pager_open(arg_no_pager, false);
375
376 strv_print(list);
377 return 0;
378 }
379
380 static int help(void) {
381 printf("%s [OPTIONS...] COMMAND ...\n\n"
382 "Query or change system locale and keyboard settings.\n\n"
383 " -h --help Show this help\n"
384 " --version Show package version\n"
385 " --no-pager Do not pipe output into a pager\n"
386 " --no-ask-password Do not prompt for password\n"
387 " -H --host=[USER@]HOST Operate on remote host\n"
388 " -M --machine=CONTAINER Operate on local container\n"
389 " --no-convert Don't convert keyboard mappings\n\n"
390 "Commands:\n"
391 " status Show current locale settings\n"
392 " set-locale LOCALE... Set system locale\n"
393 " list-locales Show known locales\n"
394 " set-keymap MAP [MAP] Set console and X11 keyboard mappings\n"
395 " list-keymaps Show known virtual console keyboard mappings\n"
396 " set-x11-keymap LAYOUT [MODEL [VARIANT [OPTIONS]]]\n"
397 " Set X11 and console keyboard mappings\n"
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"
402 " list-x11-keymap-options Show known X11 keyboard mapping options\n"
403 , program_invocation_short_name);
404
405 return 0;
406 }
407
408 static int verb_help(int argc, char **argv, void *userdata) {
409 return help();
410 }
411
412 static 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[] = {
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 },
429 {}
430 };
431
432 int c;
433
434 assert(argc >= 0);
435 assert(argv);
436
437 while ((c = getopt_long(argc, argv, "hH:M:", options, NULL)) >= 0)
438
439 switch (c) {
440
441 case 'h':
442 return help();
443
444 case ARG_VERSION:
445 return version();
446
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
455 case ARG_NO_ASK_PASSWORD:
456 arg_ask_password = false;
457 break;
458
459 case 'H':
460 arg_transport = BUS_TRANSPORT_REMOTE;
461 arg_host = optarg;
462 break;
463
464 case 'M':
465 arg_transport = BUS_TRANSPORT_MACHINE;
466 arg_host = optarg;
467 break;
468
469 case '?':
470 return -EINVAL;
471
472 default:
473 assert_not_reached("Unhandled option");
474 }
475
476 return 1;
477 }
478
479 static int localectl_main(sd_bus *bus, int argc, char *argv[]) {
480
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 {}
494 };
495
496 return dispatch_verb(argc, argv, verbs, bus);
497 }
498
499 int main(int argc, char*argv[]) {
500 sd_bus *bus = NULL;
501 int r;
502
503 setlocale(LC_ALL, "");
504 log_parse_environment();
505 log_open();
506
507 r = parse_argv(argc, argv);
508 if (r <= 0)
509 goto finish;
510
511 r = bus_connect_transport(arg_transport, arg_host, false, &bus);
512 if (r < 0) {
513 log_error_errno(r, "Failed to create bus connection: %m");
514 goto finish;
515 }
516
517 r = localectl_main(bus, argc, argv);
518
519 finish:
520 /* make sure we terminate the bus connection first, and then close the
521 * pager, see issue #3543 for the details. */
522 sd_bus_flush_close_unref(bus);
523 pager_close();
524
525 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
526 }