]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/locale/localed.c
791b5e60dd8d4fb992535e36fd93fce7f79484ef
[thirdparty/systemd.git] / src / locale / localed.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <errno.h>
4 #include <sys/stat.h>
5 #include <sys/types.h>
6 #include <unistd.h>
7
8 #if HAVE_XKBCOMMON
9 #include <xkbcommon/xkbcommon.h>
10 #include <dlfcn.h>
11 #endif
12
13 #include "sd-bus.h"
14
15 #include "alloc-util.h"
16 #include "bus-error.h"
17 #include "bus-log-control-api.h"
18 #include "bus-message.h"
19 #include "bus-polkit.h"
20 #include "def.h"
21 #include "dlfcn-util.h"
22 #include "kbd-util.h"
23 #include "localed-util.h"
24 #include "macro.h"
25 #include "main-func.h"
26 #include "missing_capability.h"
27 #include "path-util.h"
28 #include "selinux-util.h"
29 #include "service-util.h"
30 #include "signal-util.h"
31 #include "string-util.h"
32 #include "strv.h"
33 #include "user-util.h"
34
35 static int locale_update_system_manager(sd_bus *bus, char **l_set, char **l_unset) {
36 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
37 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
38 int r;
39
40 assert(bus);
41
42 r = sd_bus_message_new_method_call(bus, &m,
43 "org.freedesktop.systemd1",
44 "/org/freedesktop/systemd1",
45 "org.freedesktop.systemd1.Manager",
46 "UnsetAndSetEnvironment");
47 if (r < 0)
48 return bus_log_create_error(r);
49
50 r = sd_bus_message_append_strv(m, l_unset);
51 if (r < 0)
52 return bus_log_create_error(r);
53
54 r = sd_bus_message_append_strv(m, l_set);
55 if (r < 0)
56 return bus_log_create_error(r);
57
58 r = sd_bus_call(bus, m, 0, &error, NULL);
59 if (r < 0)
60 return log_error_errno(r, "Failed to update the manager environment: %s", bus_error_message(&error, r));
61
62 return 0;
63 }
64
65 static int vconsole_reload(sd_bus *bus) {
66 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
67 int r;
68
69 assert(bus);
70
71 r = sd_bus_call_method(bus,
72 "org.freedesktop.systemd1",
73 "/org/freedesktop/systemd1",
74 "org.freedesktop.systemd1.Manager",
75 "RestartUnit",
76 &error,
77 NULL,
78 "ss", "systemd-vconsole-setup.service", "replace");
79
80 if (r < 0)
81 return log_error_errno(r, "Failed to issue method call: %s", bus_error_message(&error, r));
82
83 return 0;
84 }
85
86 static int vconsole_convert_to_x11_and_emit(Context *c, sd_bus_message *m) {
87 int r;
88
89 assert(m);
90
91 r = x11_read_data(c, m);
92 if (r < 0)
93 return r;
94
95 r = vconsole_convert_to_x11(c);
96 if (r <= 0)
97 return r;
98
99 /* modified */
100 r = x11_write_data(c);
101 if (r < 0)
102 return log_error_errno(r, "Failed to write X11 keyboard layout: %m");
103
104 sd_bus_emit_properties_changed(sd_bus_message_get_bus(m),
105 "/org/freedesktop/locale1",
106 "org.freedesktop.locale1",
107 "X11Layout", "X11Model", "X11Variant", "X11Options", NULL);
108
109 return 1;
110 }
111
112 static int x11_convert_to_vconsole_and_emit(Context *c, sd_bus_message *m) {
113 int r;
114
115 assert(m);
116
117 r = vconsole_read_data(c, m);
118 if (r < 0)
119 return r;
120
121 r = x11_convert_to_vconsole(c);
122 if (r <= 0)
123 return r;
124
125 /* modified */
126 r = vconsole_write_data(c);
127 if (r < 0)
128 log_error_errno(r, "Failed to save virtual console keymap: %m");
129
130 sd_bus_emit_properties_changed(sd_bus_message_get_bus(m),
131 "/org/freedesktop/locale1",
132 "org.freedesktop.locale1",
133 "VConsoleKeymap", "VConsoleKeymapToggle", NULL);
134
135 return vconsole_reload(sd_bus_message_get_bus(m));
136 }
137
138 static int property_get_locale(
139 sd_bus *bus,
140 const char *path,
141 const char *interface,
142 const char *property,
143 sd_bus_message *reply,
144 void *userdata,
145 sd_bus_error *error) {
146
147 Context *c = userdata;
148 _cleanup_strv_free_ char **l = NULL;
149 int r;
150
151 r = locale_read_data(c, reply);
152 if (r < 0)
153 return r;
154
155 r = locale_context_build_env(&c->locale_context, &l, NULL);
156 if (r < 0)
157 return r;
158
159 return sd_bus_message_append_strv(reply, l);
160 }
161
162 static int property_get_vconsole(
163 sd_bus *bus,
164 const char *path,
165 const char *interface,
166 const char *property,
167 sd_bus_message *reply,
168 void *userdata,
169 sd_bus_error *error) {
170
171 Context *c = userdata;
172 int r;
173
174 r = vconsole_read_data(c, reply);
175 if (r < 0)
176 return r;
177
178 if (streq(property, "VConsoleKeymap"))
179 return sd_bus_message_append_basic(reply, 's', c->vc_keymap);
180 else if (streq(property, "VConsoleKeymapToggle"))
181 return sd_bus_message_append_basic(reply, 's', c->vc_keymap_toggle);
182
183 return -EINVAL;
184 }
185
186 static int property_get_xkb(
187 sd_bus *bus,
188 const char *path,
189 const char *interface,
190 const char *property,
191 sd_bus_message *reply,
192 void *userdata,
193 sd_bus_error *error) {
194
195 Context *c = userdata;
196 int r;
197
198 r = x11_read_data(c, reply);
199 if (r < 0)
200 return r;
201
202 if (streq(property, "X11Layout"))
203 return sd_bus_message_append_basic(reply, 's', c->x11_layout);
204 else if (streq(property, "X11Model"))
205 return sd_bus_message_append_basic(reply, 's', c->x11_model);
206 else if (streq(property, "X11Variant"))
207 return sd_bus_message_append_basic(reply, 's', c->x11_variant);
208 else if (streq(property, "X11Options"))
209 return sd_bus_message_append_basic(reply, 's', c->x11_options);
210
211 return -EINVAL;
212 }
213
214 static int process_locale_list_item(
215 const char *assignment,
216 char *new_locale[static _VARIABLE_LC_MAX],
217 bool use_localegen,
218 sd_bus_error *error) {
219
220 assert(assignment);
221 assert(new_locale);
222
223 for (LocaleVariable p = 0; p < _VARIABLE_LC_MAX; p++) {
224 const char *name, *e;
225
226 assert_se(name = locale_variable_to_string(p));
227
228 e = startswith(assignment, name);
229 if (!e)
230 continue;
231
232 if (*e != '=')
233 continue;
234
235 e++;
236
237 if (!locale_is_valid(e))
238 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Locale %s is not valid, refusing.", e);
239 if (!use_localegen && locale_is_installed(e) <= 0)
240 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Locale %s not installed, refusing.", e);
241 if (new_locale[p])
242 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Locale variable %s set twice, refusing.", name);
243
244 new_locale[p] = strdup(e);
245 if (!new_locale[p])
246 return -ENOMEM;
247
248 return 0;
249 }
250
251 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Locale assignment %s not valid, refusing.", assignment);
252 }
253
254 static int locale_gen_process_locale(char *new_locale[static _VARIABLE_LC_MAX],
255 sd_bus_error *error) {
256 int r;
257 assert(new_locale);
258
259 for (LocaleVariable p = 0; p < _VARIABLE_LC_MAX; p++) {
260 if (p == VARIABLE_LANGUAGE)
261 continue;
262 if (isempty(new_locale[p]))
263 continue;
264 if (locale_is_installed(new_locale[p]))
265 continue;
266
267 r = locale_gen_enable_locale(new_locale[p]);
268 if (r == -ENOEXEC) {
269 log_error_errno(r, "Refused to enable locale for generation: %m");
270 return sd_bus_error_setf(error,
271 SD_BUS_ERROR_INVALID_ARGS,
272 "Specified locale is not installed and non-UTF-8 locale will not be auto-generated: %s",
273 new_locale[p]);
274 } else if (r == -EINVAL) {
275 log_error_errno(r, "Failed to enable invalid locale %s for generation.", new_locale[p]);
276 return sd_bus_error_setf(error,
277 SD_BUS_ERROR_INVALID_ARGS,
278 "Can not enable locale generation for invalid locale: %s",
279 new_locale[p]);
280 } else if (r < 0) {
281 log_error_errno(r, "Failed to enable locale for generation: %m");
282 return sd_bus_error_set_errnof(error, r, "Failed to enable locale generation: %m");
283 }
284
285 r = locale_gen_run();
286 if (r < 0) {
287 log_error_errno(r, "Failed to generate locale: %m");
288 return sd_bus_error_set_errnof(error, r, "Failed to generate locale: %m");
289 }
290 }
291
292 return 0;
293 }
294
295 static int method_set_locale(sd_bus_message *m, void *userdata, sd_bus_error *error) {
296 _cleanup_(locale_variables_freep) char *new_locale[_VARIABLE_LC_MAX] = {};
297 _cleanup_strv_free_ char **l = NULL, **l_set = NULL, **l_unset = NULL;
298 Context *c = userdata;
299 int interactive, r;
300 bool use_localegen;
301
302 assert(m);
303 assert(c);
304
305 r = sd_bus_message_read_strv(m, &l);
306 if (r < 0)
307 return r;
308
309 r = sd_bus_message_read_basic(m, 'b', &interactive);
310 if (r < 0)
311 return r;
312
313 use_localegen = locale_gen_check_available();
314
315 /* If single locale without variable name is provided, then we assume it is LANG=. */
316 if (strv_length(l) == 1 && !strchr(l[0], '=')) {
317 if (!locale_is_valid(l[0]))
318 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid locale specification: %s", l[0]);
319 if (!use_localegen && locale_is_installed(l[0]) <= 0)
320 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Specified locale is not installed: %s", l[0]);
321
322 new_locale[VARIABLE_LANG] = strdup(l[0]);
323 if (!new_locale[VARIABLE_LANG])
324 return -ENOMEM;
325
326 l = strv_free(l);
327 }
328
329 /* Check whether a variable is valid */
330 STRV_FOREACH(i, l) {
331 r = process_locale_list_item(*i, new_locale, use_localegen, error);
332 if (r < 0)
333 return r;
334 }
335
336 /* If LANG was specified, but not LANGUAGE, check if we should
337 * set it based on the language fallback table. */
338 if (!isempty(new_locale[VARIABLE_LANG]) &&
339 isempty(new_locale[VARIABLE_LANGUAGE])) {
340 _cleanup_free_ char *language = NULL;
341
342 (void) find_language_fallback(new_locale[VARIABLE_LANG], &language);
343 if (language) {
344 log_debug("Converted LANG=%s to LANGUAGE=%s", new_locale[VARIABLE_LANG], language);
345 free_and_replace(new_locale[VARIABLE_LANGUAGE], language);
346 }
347 }
348
349 r = locale_read_data(c, m);
350 if (r < 0) {
351 log_error_errno(r, "Failed to read locale data: %m");
352 return sd_bus_error_set(error, SD_BUS_ERROR_FAILED, "Failed to read locale data");
353 }
354
355 /* Merge with the current settings */
356 r = locale_context_merge(&c->locale_context, new_locale);
357 if (r < 0)
358 return r;
359
360 locale_variables_simplify(new_locale);
361
362 if (locale_context_equal(&c->locale_context, new_locale)) {
363 log_debug("Locale settings were not modified.");
364 return sd_bus_reply_method_return(m, NULL);
365 }
366
367 r = bus_verify_polkit_async(
368 m,
369 CAP_SYS_ADMIN,
370 "org.freedesktop.locale1.set-locale",
371 NULL,
372 interactive,
373 UID_INVALID,
374 &c->polkit_registry,
375 error);
376 if (r < 0)
377 return r;
378 if (r == 0)
379 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
380
381 /* Generate locale in case it is missing and the system is using locale-gen */
382 if (use_localegen) {
383 r = locale_gen_process_locale(new_locale, error);
384 if (r < 0)
385 return r;
386 }
387
388 locale_context_take(&c->locale_context, new_locale);
389
390 /* Write locale configuration */
391 r = locale_context_save(&c->locale_context, &l_set, &l_unset);
392 if (r < 0) {
393 log_error_errno(r, "Failed to set locale: %m");
394 return sd_bus_error_set_errnof(error, r, "Failed to set locale: %m");
395 }
396
397 (void) locale_update_system_manager(sd_bus_message_get_bus(m), l_set, l_unset);
398
399 if (!strv_isempty(l_set)) {
400 _cleanup_free_ char *line = NULL;
401
402 line = strv_join(l_set, ", ");
403 log_info("Changed locale to %s.", strnull(line));
404 } else
405 log_info("Changed locale to unset.");
406
407 (void) sd_bus_emit_properties_changed(
408 sd_bus_message_get_bus(m),
409 "/org/freedesktop/locale1",
410 "org.freedesktop.locale1",
411 "Locale", NULL);
412
413 return sd_bus_reply_method_return(m, NULL);
414 }
415
416 static int method_set_vc_keyboard(sd_bus_message *m, void *userdata, sd_bus_error *error) {
417 Context *c = userdata;
418 const char *keymap, *keymap_toggle;
419 int convert, interactive, r;
420
421 assert(m);
422 assert(c);
423
424 r = sd_bus_message_read(m, "ssbb", &keymap, &keymap_toggle, &convert, &interactive);
425 if (r < 0)
426 return r;
427
428 keymap = empty_to_null(keymap);
429 keymap_toggle = empty_to_null(keymap_toggle);
430
431 r = vconsole_read_data(c, m);
432 if (r < 0) {
433 log_error_errno(r, "Failed to read virtual console keymap data: %m");
434 return sd_bus_error_set_errnof(error, r, "Failed to read virtual console keymap data: %m");
435 }
436
437 FOREACH_STRING(name, keymap ?: keymap_toggle, keymap ? keymap_toggle : NULL) {
438 r = keymap_exists(name); /* This also verifies that the keymap name is kosher. */
439 if (r < 0) {
440 log_error_errno(r, "Failed to check keymap %s: %m", name);
441 return sd_bus_error_set_errnof(error, r, "Failed to check keymap %s: %m", name);
442 }
443 if (r == 0)
444 return sd_bus_error_setf(error, SD_BUS_ERROR_FAILED, "Keymap %s is not installed.", name);
445 }
446
447 if (streq_ptr(keymap, c->vc_keymap) &&
448 streq_ptr(keymap_toggle, c->vc_keymap_toggle))
449 return sd_bus_reply_method_return(m, NULL);
450
451 r = bus_verify_polkit_async(
452 m,
453 CAP_SYS_ADMIN,
454 "org.freedesktop.locale1.set-keyboard",
455 NULL,
456 interactive,
457 UID_INVALID,
458 &c->polkit_registry,
459 error);
460 if (r < 0)
461 return r;
462 if (r == 0)
463 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
464
465 if (free_and_strdup(&c->vc_keymap, keymap) < 0 ||
466 free_and_strdup(&c->vc_keymap_toggle, keymap_toggle) < 0)
467 return -ENOMEM;
468
469 r = vconsole_write_data(c);
470 if (r < 0) {
471 log_error_errno(r, "Failed to set virtual console keymap: %m");
472 return sd_bus_error_set_errnof(error, r, "Failed to set virtual console keymap: %m");
473 }
474
475 log_info("Changed virtual console keymap to '%s' toggle '%s'",
476 strempty(c->vc_keymap), strempty(c->vc_keymap_toggle));
477
478 (void) vconsole_reload(sd_bus_message_get_bus(m));
479
480 (void) sd_bus_emit_properties_changed(
481 sd_bus_message_get_bus(m),
482 "/org/freedesktop/locale1",
483 "org.freedesktop.locale1",
484 "VConsoleKeymap", "VConsoleKeymapToggle", NULL);
485
486 if (convert) {
487 r = vconsole_convert_to_x11_and_emit(c, m);
488 if (r < 0)
489 log_error_errno(r, "Failed to convert keymap data: %m");
490 }
491
492 return sd_bus_reply_method_return(m, NULL);
493 }
494
495 #if HAVE_XKBCOMMON
496
497 _printf_(3, 0)
498 static void log_xkb(struct xkb_context *ctx, enum xkb_log_level lvl, const char *format, va_list args) {
499 const char *fmt;
500
501 fmt = strjoina("libxkbcommon: ", format);
502 DISABLE_WARNING_FORMAT_NONLITERAL;
503 log_internalv(LOG_DEBUG, 0, PROJECT_FILE, __LINE__, __func__, fmt, args);
504 REENABLE_WARNING;
505 }
506
507 #define LOAD_SYMBOL(symbol, dl, name) \
508 ({ \
509 (symbol) = (typeof(symbol)) dlvsym((dl), (name), "V_0.5.0"); \
510 (symbol) ? 0 : -EOPNOTSUPP; \
511 })
512
513 static int verify_xkb_rmlvo(const char *model, const char *layout, const char *variant, const char *options) {
514
515 /* We dlopen() the library in order to make the dependency soft. The library (and what it pulls in) is huge
516 * after all, hence let's support XKB maps when the library is around, and refuse otherwise. The function
517 * pointers to the shared library are below: */
518
519 struct xkb_context* (*symbol_xkb_context_new)(enum xkb_context_flags flags) = NULL;
520 void (*symbol_xkb_context_unref)(struct xkb_context *context) = NULL;
521 void (*symbol_xkb_context_set_log_fn)(struct xkb_context *context, void (*log_fn)(struct xkb_context *context, enum xkb_log_level level, const char *format, va_list args)) = NULL;
522 struct xkb_keymap* (*symbol_xkb_keymap_new_from_names)(struct xkb_context *context, const struct xkb_rule_names *names, enum xkb_keymap_compile_flags flags) = NULL;
523 void (*symbol_xkb_keymap_unref)(struct xkb_keymap *keymap) = NULL;
524
525 const struct xkb_rule_names rmlvo = {
526 .model = model,
527 .layout = layout,
528 .variant = variant,
529 .options = options,
530 };
531 struct xkb_context *ctx = NULL;
532 struct xkb_keymap *km = NULL;
533 _cleanup_(dlclosep) void *dl = NULL;
534 int r;
535
536 /* Compile keymap from RMLVO information to check out its validity */
537
538 dl = dlopen("libxkbcommon.so.0", RTLD_LAZY);
539 if (!dl)
540 return -EOPNOTSUPP;
541
542 r = LOAD_SYMBOL(symbol_xkb_context_new, dl, "xkb_context_new");
543 if (r < 0)
544 goto finish;
545
546 r = LOAD_SYMBOL(symbol_xkb_context_unref, dl, "xkb_context_unref");
547 if (r < 0)
548 goto finish;
549
550 r = LOAD_SYMBOL(symbol_xkb_context_set_log_fn, dl, "xkb_context_set_log_fn");
551 if (r < 0)
552 goto finish;
553
554 r = LOAD_SYMBOL(symbol_xkb_keymap_new_from_names, dl, "xkb_keymap_new_from_names");
555 if (r < 0)
556 goto finish;
557
558 r = LOAD_SYMBOL(symbol_xkb_keymap_unref, dl, "xkb_keymap_unref");
559 if (r < 0)
560 goto finish;
561
562 ctx = symbol_xkb_context_new(XKB_CONTEXT_NO_ENVIRONMENT_NAMES);
563 if (!ctx) {
564 r = -ENOMEM;
565 goto finish;
566 }
567
568 symbol_xkb_context_set_log_fn(ctx, log_xkb);
569
570 km = symbol_xkb_keymap_new_from_names(ctx, &rmlvo, XKB_KEYMAP_COMPILE_NO_FLAGS);
571 if (!km) {
572 r = -EINVAL;
573 goto finish;
574 }
575
576 r = 0;
577
578 finish:
579 if (symbol_xkb_keymap_unref && km)
580 symbol_xkb_keymap_unref(km);
581
582 if (symbol_xkb_context_unref && ctx)
583 symbol_xkb_context_unref(ctx);
584
585 return r;
586 }
587
588 #else
589
590 static int verify_xkb_rmlvo(const char *model, const char *layout, const char *variant, const char *options) {
591 return 0;
592 }
593
594 #endif
595
596 static int method_set_x11_keyboard(sd_bus_message *m, void *userdata, sd_bus_error *error) {
597 Context *c = userdata;
598 const char *layout, *model, *variant, *options;
599 int convert, interactive, r;
600
601 assert(m);
602 assert(c);
603
604 r = sd_bus_message_read(m, "ssssbb", &layout, &model, &variant, &options, &convert, &interactive);
605 if (r < 0)
606 return r;
607
608 layout = empty_to_null(layout);
609 model = empty_to_null(model);
610 variant = empty_to_null(variant);
611 options = empty_to_null(options);
612
613 r = x11_read_data(c, m);
614 if (r < 0) {
615 log_error_errno(r, "Failed to read x11 keyboard layout data: %m");
616 return sd_bus_error_set(error, SD_BUS_ERROR_FAILED, "Failed to read x11 keyboard layout data");
617 }
618
619 if (streq_ptr(layout, c->x11_layout) &&
620 streq_ptr(model, c->x11_model) &&
621 streq_ptr(variant, c->x11_variant) &&
622 streq_ptr(options, c->x11_options))
623 return sd_bus_reply_method_return(m, NULL);
624
625 if ((layout && !string_is_safe(layout)) ||
626 (model && !string_is_safe(model)) ||
627 (variant && !string_is_safe(variant)) ||
628 (options && !string_is_safe(options)))
629 return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Received invalid keyboard data");
630
631 r = verify_xkb_rmlvo(model, layout, variant, options);
632 if (r < 0) {
633 log_error_errno(r, "Cannot compile XKB keymap for new x11 keyboard layout ('%s' / '%s' / '%s' / '%s'): %m",
634 strempty(model), strempty(layout), strempty(variant), strempty(options));
635
636 if (r == -EOPNOTSUPP)
637 return sd_bus_error_set(error, SD_BUS_ERROR_NOT_SUPPORTED, "Local keyboard configuration not supported on this system.");
638
639 return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Specified keymap cannot be compiled, refusing as invalid.");
640 }
641
642 r = bus_verify_polkit_async(
643 m,
644 CAP_SYS_ADMIN,
645 "org.freedesktop.locale1.set-keyboard",
646 NULL,
647 interactive,
648 UID_INVALID,
649 &c->polkit_registry,
650 error);
651 if (r < 0)
652 return r;
653 if (r == 0)
654 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
655
656 if (free_and_strdup(&c->x11_layout, layout) < 0 ||
657 free_and_strdup(&c->x11_model, model) < 0 ||
658 free_and_strdup(&c->x11_variant, variant) < 0 ||
659 free_and_strdup(&c->x11_options, options) < 0)
660 return -ENOMEM;
661
662 r = x11_write_data(c);
663 if (r < 0) {
664 log_error_errno(r, "Failed to set X11 keyboard layout: %m");
665 return sd_bus_error_set_errnof(error, r, "Failed to set X11 keyboard layout: %m");
666 }
667
668 log_info("Changed X11 keyboard layout to '%s' model '%s' variant '%s' options '%s'",
669 strempty(c->x11_layout),
670 strempty(c->x11_model),
671 strempty(c->x11_variant),
672 strempty(c->x11_options));
673
674 (void) sd_bus_emit_properties_changed(
675 sd_bus_message_get_bus(m),
676 "/org/freedesktop/locale1",
677 "org.freedesktop.locale1",
678 "X11Layout", "X11Model", "X11Variant", "X11Options", NULL);
679
680 if (convert) {
681 r = x11_convert_to_vconsole_and_emit(c, m);
682 if (r < 0)
683 log_error_errno(r, "Failed to convert keymap data: %m");
684 }
685
686 return sd_bus_reply_method_return(m, NULL);
687 }
688
689 static const sd_bus_vtable locale_vtable[] = {
690 SD_BUS_VTABLE_START(0),
691 SD_BUS_PROPERTY("Locale", "as", property_get_locale, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
692 SD_BUS_PROPERTY("X11Layout", "s", property_get_xkb, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
693 SD_BUS_PROPERTY("X11Model", "s", property_get_xkb, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
694 SD_BUS_PROPERTY("X11Variant", "s", property_get_xkb, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
695 SD_BUS_PROPERTY("X11Options", "s", property_get_xkb, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
696 SD_BUS_PROPERTY("VConsoleKeymap", "s", property_get_vconsole, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
697 SD_BUS_PROPERTY("VConsoleKeymapToggle", "s", property_get_vconsole, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
698
699 SD_BUS_METHOD_WITH_NAMES("SetLocale",
700 "asb",
701 SD_BUS_PARAM(locale)
702 SD_BUS_PARAM(interactive),
703 NULL,,
704 method_set_locale,
705 SD_BUS_VTABLE_UNPRIVILEGED),
706 SD_BUS_METHOD_WITH_NAMES("SetVConsoleKeyboard",
707 "ssbb",
708 SD_BUS_PARAM(keymap)
709 SD_BUS_PARAM(keymap_toggle)
710 SD_BUS_PARAM(convert)
711 SD_BUS_PARAM(interactive),
712 NULL,,
713 method_set_vc_keyboard,
714 SD_BUS_VTABLE_UNPRIVILEGED),
715 SD_BUS_METHOD_WITH_NAMES("SetX11Keyboard",
716 "ssssbb",
717 SD_BUS_PARAM(layout)
718 SD_BUS_PARAM(model)
719 SD_BUS_PARAM(variant)
720 SD_BUS_PARAM(options)
721 SD_BUS_PARAM(convert)
722 SD_BUS_PARAM(interactive),
723 NULL,,
724 method_set_x11_keyboard,
725 SD_BUS_VTABLE_UNPRIVILEGED),
726
727 SD_BUS_VTABLE_END
728 };
729
730 static const BusObjectImplementation manager_object = {
731 "/org/freedesktop/locale1",
732 "org.freedesktop.locale1",
733 .vtables = BUS_VTABLES(locale_vtable),
734 };
735
736 static int connect_bus(Context *c, sd_event *event, sd_bus **_bus) {
737 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
738 int r;
739
740 assert(c);
741 assert(event);
742 assert(_bus);
743
744 r = sd_bus_default_system(&bus);
745 if (r < 0)
746 return log_error_errno(r, "Failed to get system bus connection: %m");
747
748 r = bus_add_implementation(bus, &manager_object, c);
749 if (r < 0)
750 return r;
751
752 r = bus_log_control_api_register(bus);
753 if (r < 0)
754 return r;
755
756 r = sd_bus_request_name_async(bus, NULL, "org.freedesktop.locale1", 0, NULL, NULL);
757 if (r < 0)
758 return log_error_errno(r, "Failed to request name: %m");
759
760 r = sd_bus_attach_event(bus, event, 0);
761 if (r < 0)
762 return log_error_errno(r, "Failed to attach bus to event loop: %m");
763
764 *_bus = TAKE_PTR(bus);
765
766 return 0;
767 }
768
769 static int run(int argc, char *argv[]) {
770 _cleanup_(context_clear) Context context = {
771 .locale_context.mtime = USEC_INFINITY,
772 .vc_mtime = USEC_INFINITY,
773 .x11_mtime = USEC_INFINITY,
774 };
775 _cleanup_(sd_event_unrefp) sd_event *event = NULL;
776 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
777 int r;
778
779 log_setup();
780
781 r = service_parse_argv("systemd-localed.service",
782 "Manage system locale settings and key mappings.",
783 BUS_IMPLEMENTATIONS(&manager_object,
784 &log_control_object),
785 argc, argv);
786 if (r <= 0)
787 return r;
788
789 umask(0022);
790
791 r = mac_selinux_init();
792 if (r < 0)
793 return r;
794
795 assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGTERM, SIGINT, -1) >= 0);
796
797 r = sd_event_default(&event);
798 if (r < 0)
799 return log_error_errno(r, "Failed to allocate event loop: %m");
800
801 (void) sd_event_set_watchdog(event, true);
802
803 r = sd_event_add_signal(event, NULL, SIGINT, NULL, NULL);
804 if (r < 0)
805 return log_error_errno(r, "Failed to install SIGINT handler: %m");
806
807 r = sd_event_add_signal(event, NULL, SIGTERM, NULL, NULL);
808 if (r < 0)
809 return log_error_errno(r, "Failed to install SIGTERM handler: %m");
810
811 r = connect_bus(&context, event, &bus);
812 if (r < 0)
813 return r;
814
815 r = bus_event_loop_with_idle(event, bus, "org.freedesktop.locale1", DEFAULT_EXIT_USEC, NULL, NULL);
816 if (r < 0)
817 return log_error_errno(r, "Failed to run event loop: %m");
818
819 return 0;
820 }
821
822 DEFINE_MAIN_FUNCTION(run);