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