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