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