1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
9 #include <xkbcommon/xkbcommon.h>
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"
21 #include "dlfcn-util.h"
22 #include "keymap-util.h"
23 #include "locale-util.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"
33 #include "user-util.h"
35 static int locale_update_system_manager(Context
*c
, sd_bus
*bus
) {
36 _cleanup_free_
char **l_unset
= NULL
;
37 _cleanup_strv_free_
char **l_set
= NULL
;
38 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
39 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
40 size_t c_set
, c_unset
;
46 l_unset
= new0(char*, _VARIABLE_LC_MAX
);
50 l_set
= new0(char*, _VARIABLE_LC_MAX
);
54 for (p
= 0, c_set
= 0, c_unset
= 0; p
< _VARIABLE_LC_MAX
; p
++) {
57 name
= locale_variable_to_string(p
);
60 if (isempty(c
->locale
[p
]))
61 l_unset
[c_set
++] = (char*) name
;
65 s
= strjoin(name
, "=", c
->locale
[p
]);
73 assert(c_set
+ c_unset
== _VARIABLE_LC_MAX
);
74 r
= sd_bus_message_new_method_call(bus
, &m
,
75 "org.freedesktop.systemd1",
76 "/org/freedesktop/systemd1",
77 "org.freedesktop.systemd1.Manager",
78 "UnsetAndSetEnvironment");
80 return bus_log_create_error(r
);
82 r
= sd_bus_message_append_strv(m
, l_unset
);
84 return bus_log_create_error(r
);
86 r
= sd_bus_message_append_strv(m
, l_set
);
88 return bus_log_create_error(r
);
90 r
= sd_bus_call(bus
, m
, 0, &error
, NULL
);
92 return log_error_errno(r
, "Failed to update the manager environment: %s", bus_error_message(&error
, r
));
97 static int vconsole_reload(sd_bus
*bus
) {
98 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
103 r
= sd_bus_call_method(bus
,
104 "org.freedesktop.systemd1",
105 "/org/freedesktop/systemd1",
106 "org.freedesktop.systemd1.Manager",
110 "ss", "systemd-vconsole-setup.service", "replace");
113 return log_error_errno(r
, "Failed to issue method call: %s", bus_error_message(&error
, r
));
118 static int vconsole_convert_to_x11_and_emit(Context
*c
, sd_bus_message
*m
) {
123 r
= x11_read_data(c
, m
);
127 r
= vconsole_convert_to_x11(c
);
132 r
= x11_write_data(c
);
134 return log_error_errno(r
, "Failed to write X11 keyboard layout: %m");
136 sd_bus_emit_properties_changed(sd_bus_message_get_bus(m
),
137 "/org/freedesktop/locale1",
138 "org.freedesktop.locale1",
139 "X11Layout", "X11Model", "X11Variant", "X11Options", NULL
);
144 static int x11_convert_to_vconsole_and_emit(Context
*c
, sd_bus_message
*m
) {
149 r
= vconsole_read_data(c
, m
);
153 r
= x11_convert_to_vconsole(c
);
158 r
= vconsole_write_data(c
);
160 log_error_errno(r
, "Failed to save virtual console keymap: %m");
162 sd_bus_emit_properties_changed(sd_bus_message_get_bus(m
),
163 "/org/freedesktop/locale1",
164 "org.freedesktop.locale1",
165 "VConsoleKeymap", "VConsoleKeymapToggle", NULL
);
167 return vconsole_reload(sd_bus_message_get_bus(m
));
170 static int property_get_locale(
173 const char *interface
,
174 const char *property
,
175 sd_bus_message
*reply
,
177 sd_bus_error
*error
) {
179 Context
*c
= userdata
;
180 _cleanup_strv_free_
char **l
= NULL
;
183 r
= locale_read_data(c
, reply
);
187 l
= new0(char*, _VARIABLE_LC_MAX
+1);
191 for (p
= 0, q
= 0; p
< _VARIABLE_LC_MAX
; p
++) {
195 name
= locale_variable_to_string(p
);
198 if (isempty(c
->locale
[p
]))
201 if (asprintf(&t
, "%s=%s", name
, c
->locale
[p
]) < 0)
207 return sd_bus_message_append_strv(reply
, l
);
210 static int property_get_vconsole(
213 const char *interface
,
214 const char *property
,
215 sd_bus_message
*reply
,
217 sd_bus_error
*error
) {
219 Context
*c
= userdata
;
222 r
= vconsole_read_data(c
, reply
);
226 if (streq(property
, "VConsoleKeymap"))
227 return sd_bus_message_append_basic(reply
, 's', c
->vc_keymap
);
228 else if (streq(property
, "VConsoleKeymapToggle"))
229 return sd_bus_message_append_basic(reply
, 's', c
->vc_keymap_toggle
);
234 static int property_get_xkb(
237 const char *interface
,
238 const char *property
,
239 sd_bus_message
*reply
,
241 sd_bus_error
*error
) {
243 Context
*c
= userdata
;
246 r
= x11_read_data(c
, reply
);
250 if (streq(property
, "X11Layout"))
251 return sd_bus_message_append_basic(reply
, 's', c
->x11_layout
);
252 else if (streq(property
, "X11Model"))
253 return sd_bus_message_append_basic(reply
, 's', c
->x11_model
);
254 else if (streq(property
, "X11Variant"))
255 return sd_bus_message_append_basic(reply
, 's', c
->x11_variant
);
256 else if (streq(property
, "X11Options"))
257 return sd_bus_message_append_basic(reply
, 's', c
->x11_options
);
262 static int process_locale_list_item(
263 const char *assignment
,
264 char *new_locale
[static _VARIABLE_LC_MAX
],
266 sd_bus_error
*error
) {
271 for (LocaleVariable p
= 0; p
< _VARIABLE_LC_MAX
; p
++) {
272 const char *name
, *e
;
274 assert_se(name
= locale_variable_to_string(p
));
276 e
= startswith(assignment
, name
);
285 if (!locale_is_valid(e
))
286 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Locale %s is not valid, refusing.", e
);
287 if (!use_localegen
&& locale_is_installed(e
) <= 0)
288 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Locale %s not installed, refusing.", e
);
290 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Locale variable %s set twice, refusing.", name
);
292 new_locale
[p
] = strdup(e
);
299 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Locale assignment %s not valid, refusing.", assignment
);
302 static int locale_gen_process_locale(char *new_locale
[static _VARIABLE_LC_MAX
],
303 sd_bus_error
*error
) {
307 for (LocaleVariable p
= 0; p
< _VARIABLE_LC_MAX
; p
++) {
308 if (p
== VARIABLE_LANGUAGE
)
310 if (isempty(new_locale
[p
]))
312 if (locale_is_installed(new_locale
[p
]))
315 r
= locale_gen_enable_locale(new_locale
[p
]);
317 log_error_errno(r
, "Refused to enable locale for generation: %m");
318 return sd_bus_error_setf(error
,
319 SD_BUS_ERROR_INVALID_ARGS
,
320 "Specified locale is not installed and non-UTF-8 locale will not be auto-generated: %s",
322 } else if (r
== -EINVAL
) {
323 log_error_errno(r
, "Failed to enable invalid locale %s for generation.", new_locale
[p
]);
324 return sd_bus_error_setf(error
,
325 SD_BUS_ERROR_INVALID_ARGS
,
326 "Can not enable locale generation for invalid locale: %s",
329 log_error_errno(r
, "Failed to enable locale for generation: %m");
330 return sd_bus_error_set_errnof(error
, r
, "Failed to enable locale generation: %m");
333 r
= locale_gen_run();
335 log_error_errno(r
, "Failed to generate locale: %m");
336 return sd_bus_error_set_errnof(error
, r
, "Failed to generate locale: %m");
343 static int method_set_locale(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
344 _cleanup_(locale_variables_freep
) char *new_locale
[_VARIABLE_LC_MAX
] = {};
345 _cleanup_strv_free_
char **settings
= NULL
, **l
= NULL
;
346 Context
*c
= userdata
;
347 bool modified
= false;
355 r
= sd_bus_message_read_strv(m
, &l
);
359 r
= sd_bus_message_read_basic(m
, 'b', &interactive
);
363 use_localegen
= locale_gen_check_available();
365 /* If single locale without variable name is provided, then we assume it is LANG=. */
366 if (strv_length(l
) == 1 && !strchr(l
[0], '=')) {
367 if (!locale_is_valid(l
[0]))
368 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid locale specification: %s", l
[0]);
369 if (!use_localegen
&& locale_is_installed(l
[0]) <= 0)
370 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Specified locale is not installed: %s", l
[0]);
372 new_locale
[VARIABLE_LANG
] = strdup(l
[0]);
373 if (!new_locale
[VARIABLE_LANG
])
379 /* Check whether a variable is valid */
381 r
= process_locale_list_item(*i
, new_locale
, use_localegen
, error
);
386 /* If LANG was specified, but not LANGUAGE, check if we should
387 * set it based on the language fallback table. */
388 if (!isempty(new_locale
[VARIABLE_LANG
]) &&
389 isempty(new_locale
[VARIABLE_LANGUAGE
])) {
390 _cleanup_free_
char *language
= NULL
;
392 (void) find_language_fallback(new_locale
[VARIABLE_LANG
], &language
);
394 log_debug("Converted LANG=%s to LANGUAGE=%s", new_locale
[VARIABLE_LANG
], language
);
395 free_and_replace(new_locale
[VARIABLE_LANGUAGE
], language
);
399 r
= locale_read_data(c
, m
);
401 log_error_errno(r
, "Failed to read locale data: %m");
402 return sd_bus_error_setf(error
, SD_BUS_ERROR_FAILED
, "Failed to read locale data");
405 /* Merge with the current settings */
406 for (LocaleVariable p
= 0; p
< _VARIABLE_LC_MAX
; p
++)
407 if (!isempty(c
->locale
[p
]) && isempty(new_locale
[p
])) {
408 new_locale
[p
] = strdup(c
->locale
[p
]);
413 locale_simplify(new_locale
);
415 for (LocaleVariable p
= 0; p
< _VARIABLE_LC_MAX
; p
++)
416 if (!streq_ptr(c
->locale
[p
], new_locale
[p
])) {
422 log_debug("Locale settings were not modified.");
423 return sd_bus_reply_method_return(m
, NULL
);
426 r
= bus_verify_polkit_async(
429 "org.freedesktop.locale1.set-locale",
438 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
440 /* Generate locale in case it is missing and the system is using locale-gen */
442 r
= locale_gen_process_locale(new_locale
, error
);
447 for (LocaleVariable p
= 0; p
< _VARIABLE_LC_MAX
; p
++)
448 free_and_replace(c
->locale
[p
], new_locale
[p
]);
450 /* Write locale configuration */
451 r
= locale_write_data(c
, &settings
);
453 log_error_errno(r
, "Failed to set locale: %m");
454 return sd_bus_error_set_errnof(error
, r
, "Failed to set locale: %m");
457 (void) locale_update_system_manager(c
, sd_bus_message_get_bus(m
));
460 _cleanup_free_
char *line
;
462 line
= strv_join(settings
, ", ");
463 log_info("Changed locale to %s.", strnull(line
));
465 log_info("Changed locale to unset.");
467 (void) sd_bus_emit_properties_changed(
468 sd_bus_message_get_bus(m
),
469 "/org/freedesktop/locale1",
470 "org.freedesktop.locale1",
473 return sd_bus_reply_method_return(m
, NULL
);
476 static int method_set_vc_keyboard(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
477 Context
*c
= userdata
;
478 const char *keymap
, *keymap_toggle
;
479 int convert
, interactive
, r
;
484 r
= sd_bus_message_read(m
, "ssbb", &keymap
, &keymap_toggle
, &convert
, &interactive
);
488 keymap
= empty_to_null(keymap
);
489 keymap_toggle
= empty_to_null(keymap_toggle
);
491 r
= vconsole_read_data(c
, m
);
493 log_error_errno(r
, "Failed to read virtual console keymap data: %m");
494 return sd_bus_error_setf(error
, SD_BUS_ERROR_FAILED
, "Failed to read virtual console keymap data");
497 if (streq_ptr(keymap
, c
->vc_keymap
) &&
498 streq_ptr(keymap_toggle
, c
->vc_keymap_toggle
))
499 return sd_bus_reply_method_return(m
, NULL
);
501 if ((keymap
&& (!filename_is_valid(keymap
) || !string_is_safe(keymap
))) ||
502 (keymap_toggle
&& (!filename_is_valid(keymap_toggle
) || !string_is_safe(keymap_toggle
))))
503 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Received invalid keymap data");
505 r
= bus_verify_polkit_async(
508 "org.freedesktop.locale1.set-keyboard",
517 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
519 if (free_and_strdup(&c
->vc_keymap
, keymap
) < 0 ||
520 free_and_strdup(&c
->vc_keymap_toggle
, keymap_toggle
) < 0)
523 r
= vconsole_write_data(c
);
525 log_error_errno(r
, "Failed to set virtual console keymap: %m");
526 return sd_bus_error_set_errnof(error
, r
, "Failed to set virtual console keymap: %m");
529 log_info("Changed virtual console keymap to '%s' toggle '%s'",
530 strempty(c
->vc_keymap
), strempty(c
->vc_keymap_toggle
));
532 (void) vconsole_reload(sd_bus_message_get_bus(m
));
534 (void) sd_bus_emit_properties_changed(
535 sd_bus_message_get_bus(m
),
536 "/org/freedesktop/locale1",
537 "org.freedesktop.locale1",
538 "VConsoleKeymap", "VConsoleKeymapToggle", NULL
);
541 r
= vconsole_convert_to_x11_and_emit(c
, m
);
543 log_error_errno(r
, "Failed to convert keymap data: %m");
546 return sd_bus_reply_method_return(m
, NULL
);
552 static void log_xkb(struct xkb_context
*ctx
, enum xkb_log_level lvl
, const char *format
, va_list args
) {
555 fmt
= strjoina("libxkbcommon: ", format
);
556 DISABLE_WARNING_FORMAT_NONLITERAL
;
557 log_internalv(LOG_DEBUG
, 0, __FILE__
, __LINE__
, __func__
, fmt
, args
);
561 #define LOAD_SYMBOL(symbol, dl, name) \
563 (symbol) = (typeof(symbol)) dlvsym((dl), (name), "V_0.5.0"); \
564 (symbol) ? 0 : -EOPNOTSUPP; \
567 static int verify_xkb_rmlvo(const char *model
, const char *layout
, const char *variant
, const char *options
) {
569 /* We dlopen() the library in order to make the dependency soft. The library (and what it pulls in) is huge
570 * after all, hence let's support XKB maps when the library is around, and refuse otherwise. The function
571 * pointers to the shared library are below: */
573 struct xkb_context
* (*symbol_xkb_context_new
)(enum xkb_context_flags flags
) = NULL
;
574 void (*symbol_xkb_context_unref
)(struct xkb_context
*context
) = NULL
;
575 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
;
576 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
;
577 void (*symbol_xkb_keymap_unref
)(struct xkb_keymap
*keymap
) = NULL
;
579 const struct xkb_rule_names rmlvo
= {
585 struct xkb_context
*ctx
= NULL
;
586 struct xkb_keymap
*km
= NULL
;
587 _cleanup_(dlclosep
) void *dl
= NULL
;
590 /* Compile keymap from RMLVO information to check out its validity */
592 dl
= dlopen("libxkbcommon.so.0", RTLD_LAZY
);
596 r
= LOAD_SYMBOL(symbol_xkb_context_new
, dl
, "xkb_context_new");
600 r
= LOAD_SYMBOL(symbol_xkb_context_unref
, dl
, "xkb_context_unref");
604 r
= LOAD_SYMBOL(symbol_xkb_context_set_log_fn
, dl
, "xkb_context_set_log_fn");
608 r
= LOAD_SYMBOL(symbol_xkb_keymap_new_from_names
, dl
, "xkb_keymap_new_from_names");
612 r
= LOAD_SYMBOL(symbol_xkb_keymap_unref
, dl
, "xkb_keymap_unref");
616 ctx
= symbol_xkb_context_new(XKB_CONTEXT_NO_ENVIRONMENT_NAMES
);
622 symbol_xkb_context_set_log_fn(ctx
, log_xkb
);
624 km
= symbol_xkb_keymap_new_from_names(ctx
, &rmlvo
, XKB_KEYMAP_COMPILE_NO_FLAGS
);
633 if (symbol_xkb_keymap_unref
&& km
)
634 symbol_xkb_keymap_unref(km
);
636 if (symbol_xkb_context_unref
&& ctx
)
637 symbol_xkb_context_unref(ctx
);
644 static int verify_xkb_rmlvo(const char *model
, const char *layout
, const char *variant
, const char *options
) {
650 static int method_set_x11_keyboard(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
651 Context
*c
= userdata
;
652 const char *layout
, *model
, *variant
, *options
;
653 int convert
, interactive
, r
;
658 r
= sd_bus_message_read(m
, "ssssbb", &layout
, &model
, &variant
, &options
, &convert
, &interactive
);
662 layout
= empty_to_null(layout
);
663 model
= empty_to_null(model
);
664 variant
= empty_to_null(variant
);
665 options
= empty_to_null(options
);
667 r
= x11_read_data(c
, m
);
669 log_error_errno(r
, "Failed to read x11 keyboard layout data: %m");
670 return sd_bus_error_setf(error
, SD_BUS_ERROR_FAILED
, "Failed to read x11 keyboard layout data");
673 if (streq_ptr(layout
, c
->x11_layout
) &&
674 streq_ptr(model
, c
->x11_model
) &&
675 streq_ptr(variant
, c
->x11_variant
) &&
676 streq_ptr(options
, c
->x11_options
))
677 return sd_bus_reply_method_return(m
, NULL
);
679 if ((layout
&& !string_is_safe(layout
)) ||
680 (model
&& !string_is_safe(model
)) ||
681 (variant
&& !string_is_safe(variant
)) ||
682 (options
&& !string_is_safe(options
)))
683 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Received invalid keyboard data");
685 r
= verify_xkb_rmlvo(model
, layout
, variant
, options
);
687 log_error_errno(r
, "Cannot compile XKB keymap for new x11 keyboard layout ('%s' / '%s' / '%s' / '%s'): %m",
688 strempty(model
), strempty(layout
), strempty(variant
), strempty(options
));
690 if (r
== -EOPNOTSUPP
)
691 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Local keyboard configuration not supported on this system.");
693 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Specified keymap cannot be compiled, refusing as invalid.");
696 r
= bus_verify_polkit_async(
699 "org.freedesktop.locale1.set-keyboard",
708 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
710 if (free_and_strdup(&c
->x11_layout
, layout
) < 0 ||
711 free_and_strdup(&c
->x11_model
, model
) < 0 ||
712 free_and_strdup(&c
->x11_variant
, variant
) < 0 ||
713 free_and_strdup(&c
->x11_options
, options
) < 0)
716 r
= x11_write_data(c
);
718 log_error_errno(r
, "Failed to set X11 keyboard layout: %m");
719 return sd_bus_error_set_errnof(error
, r
, "Failed to set X11 keyboard layout: %m");
722 log_info("Changed X11 keyboard layout to '%s' model '%s' variant '%s' options '%s'",
723 strempty(c
->x11_layout
),
724 strempty(c
->x11_model
),
725 strempty(c
->x11_variant
),
726 strempty(c
->x11_options
));
728 (void) sd_bus_emit_properties_changed(
729 sd_bus_message_get_bus(m
),
730 "/org/freedesktop/locale1",
731 "org.freedesktop.locale1",
732 "X11Layout", "X11Model", "X11Variant", "X11Options", NULL
);
735 r
= x11_convert_to_vconsole_and_emit(c
, m
);
737 log_error_errno(r
, "Failed to convert keymap data: %m");
740 return sd_bus_reply_method_return(m
, NULL
);
743 static const sd_bus_vtable locale_vtable
[] = {
744 SD_BUS_VTABLE_START(0),
745 SD_BUS_PROPERTY("Locale", "as", property_get_locale
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
746 SD_BUS_PROPERTY("X11Layout", "s", property_get_xkb
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
747 SD_BUS_PROPERTY("X11Model", "s", property_get_xkb
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
748 SD_BUS_PROPERTY("X11Variant", "s", property_get_xkb
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
749 SD_BUS_PROPERTY("X11Options", "s", property_get_xkb
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
750 SD_BUS_PROPERTY("VConsoleKeymap", "s", property_get_vconsole
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
751 SD_BUS_PROPERTY("VConsoleKeymapToggle", "s", property_get_vconsole
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
753 SD_BUS_METHOD_WITH_NAMES("SetLocale",
756 SD_BUS_PARAM(interactive
),
759 SD_BUS_VTABLE_UNPRIVILEGED
),
760 SD_BUS_METHOD_WITH_NAMES("SetVConsoleKeyboard",
763 SD_BUS_PARAM(keymap_toggle
)
764 SD_BUS_PARAM(convert
)
765 SD_BUS_PARAM(interactive
),
767 method_set_vc_keyboard
,
768 SD_BUS_VTABLE_UNPRIVILEGED
),
769 SD_BUS_METHOD_WITH_NAMES("SetX11Keyboard",
773 SD_BUS_PARAM(variant
)
774 SD_BUS_PARAM(options
)
775 SD_BUS_PARAM(convert
)
776 SD_BUS_PARAM(interactive
),
778 method_set_x11_keyboard
,
779 SD_BUS_VTABLE_UNPRIVILEGED
),
784 static const BusObjectImplementation manager_object
= {
785 "/org/freedesktop/locale1",
786 "org.freedesktop.locale1",
787 .vtables
= BUS_VTABLES(locale_vtable
),
790 static int connect_bus(Context
*c
, sd_event
*event
, sd_bus
**_bus
) {
791 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
798 r
= sd_bus_default_system(&bus
);
800 return log_error_errno(r
, "Failed to get system bus connection: %m");
802 r
= bus_add_implementation(bus
, &manager_object
, c
);
806 r
= bus_log_control_api_register(bus
);
810 r
= sd_bus_request_name_async(bus
, NULL
, "org.freedesktop.locale1", 0, NULL
, NULL
);
812 return log_error_errno(r
, "Failed to request name: %m");
814 r
= sd_bus_attach_event(bus
, event
, 0);
816 return log_error_errno(r
, "Failed to attach bus to event loop: %m");
818 *_bus
= TAKE_PTR(bus
);
823 static int run(int argc
, char *argv
[]) {
824 _cleanup_(context_clear
) Context context
= {
825 .locale_mtime
= USEC_INFINITY
,
826 .vc_mtime
= USEC_INFINITY
,
827 .x11_mtime
= USEC_INFINITY
,
829 _cleanup_(sd_event_unrefp
) sd_event
*event
= NULL
;
830 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
835 r
= service_parse_argv("systemd-localed.service",
836 "Manage system locale settings and key mappings.",
837 BUS_IMPLEMENTATIONS(&manager_object
,
838 &log_control_object
),
845 r
= mac_selinux_init();
849 assert_se(sigprocmask_many(SIG_BLOCK
, NULL
, SIGTERM
, SIGINT
, -1) >= 0);
851 r
= sd_event_default(&event
);
853 return log_error_errno(r
, "Failed to allocate event loop: %m");
855 (void) sd_event_set_watchdog(event
, true);
857 r
= sd_event_add_signal(event
, NULL
, SIGINT
, NULL
, NULL
);
859 return log_error_errno(r
, "Failed to install SIGINT handler: %m");
861 r
= sd_event_add_signal(event
, NULL
, SIGTERM
, NULL
, NULL
);
863 return log_error_errno(r
, "Failed to install SIGTERM handler: %m");
865 r
= connect_bus(&context
, event
, &bus
);
869 r
= bus_event_loop_with_idle(event
, bus
, "org.freedesktop.locale1", DEFAULT_EXIT_USEC
, NULL
, NULL
);
871 return log_error_errno(r
, "Failed to run event loop: %m");
876 DEFINE_MAIN_FUNCTION(run
);