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