]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/locale/localed.c
locale: rename context_free_vconsole() -> context_clear_vconsole()
[thirdparty/systemd.git] / src / locale / localed.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
1822350d 2
1822350d 3#include <errno.h>
ca78ad1d
ZJS
4#include <sys/stat.h>
5#include <sys/types.h>
1822350d
KS
6#include <unistd.h>
7
349cc4a5 8#if HAVE_XKBCOMMON
3ffd4af2 9#include <xkbcommon/xkbcommon.h>
5de34470 10#include <dlfcn.h>
3ffd4af2
LP
11#endif
12
8d451309
KS
13#include "sd-bus.h"
14
b5efdb8a 15#include "alloc-util.h"
8d451309 16#include "bus-error.h"
ac9f55ed 17#include "bus-log-control-api.h"
8d451309 18#include "bus-message.h"
269e4d2d 19#include "bus-polkit.h"
28db6fbf 20#include "constants.h"
88d775b7 21#include "dlfcn-util.h"
9ef70c06 22#include "kbd-util.h"
3e5203b3 23#include "localed-util.h"
4897d1dc 24#include "macro.h"
c6f09e6a 25#include "main-func.h"
36dd5ffd 26#include "missing_capability.h"
bb15fafe 27#include "path-util.h"
d7b8eec7 28#include "selinux-util.h"
fc021a5b 29#include "service-util.h"
50008ae4 30#include "signal-util.h"
4897d1dc 31#include "string-util.h"
bb15fafe 32#include "strv.h"
ee104e11 33#include "user-util.h"
d4f5a1f4 34
c8966e81 35static int reload_system_manager(sd_bus *bus) {
4afd3348 36 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
7e284b05 37 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
8d451309 38 int r;
1822350d
KS
39
40 assert(bus);
41
151b9b96 42 r = sd_bus_message_new_method_call(bus, &m,
8d451309
KS
43 "org.freedesktop.systemd1",
44 "/org/freedesktop/systemd1",
45 "org.freedesktop.systemd1.Manager",
c8966e81 46 "Reload");
8d451309 47 if (r < 0)
46e8b947 48 return bus_log_create_error(r);
1822350d 49
c49b30a2 50 r = sd_bus_call(bus, m, 0, &error, NULL);
8d451309 51 if (r < 0)
c8966e81 52 return log_error_errno(r, "Failed to reload system manager: %s", bus_error_message(&error, r));
1822350d 53
8d451309 54 return 0;
1822350d
KS
55}
56
8d451309 57static int vconsole_reload(sd_bus *bus) {
4afd3348 58 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1822350d 59 int r;
1822350d
KS
60
61 assert(bus);
62
8d451309 63 r = sd_bus_call_method(bus,
1822350d
KS
64 "org.freedesktop.systemd1",
65 "/org/freedesktop/systemd1",
66 "org.freedesktop.systemd1.Manager",
8d451309
KS
67 "RestartUnit",
68 &error,
69 NULL,
70 "ss", "systemd-vconsole-setup.service", "replace");
1822350d 71
8d451309 72 if (r < 0)
26a93376 73 return log_error_errno(r, "Failed to issue method call: %s", bus_error_message(&error, r));
4ae25393
YW
74
75 return 0;
1822350d
KS
76}
77
df4fd2c7 78static int vconsole_convert_to_x11_and_emit(Context *c, sd_bus_message *m) {
78bd12a0 79 int r;
0732ef7a 80
76400a62 81 assert(c);
df4fd2c7
YW
82 assert(m);
83
84 r = x11_read_data(c, m);
85 if (r < 0)
86 return r;
4e829d21 87
4897d1dc
ZJS
88 r = vconsole_convert_to_x11(c);
89 if (r <= 0)
90 return r;
4e829d21 91
4897d1dc
ZJS
92 /* modified */
93 r = x11_write_data(c);
94 if (r < 0)
95 return log_error_errno(r, "Failed to write X11 keyboard layout: %m");
4e829d21 96
df4fd2c7 97 sd_bus_emit_properties_changed(sd_bus_message_get_bus(m),
4897d1dc
ZJS
98 "/org/freedesktop/locale1",
99 "org.freedesktop.locale1",
100 "X11Layout", "X11Model", "X11Variant", "X11Options", NULL);
4e829d21 101
4897d1dc 102 return 1;
4e829d21
ZJS
103}
104
df4fd2c7 105static int x11_convert_to_vconsole_and_emit(Context *c, sd_bus_message *m) {
0732ef7a 106 int r;
1822350d 107
76400a62 108 assert(c);
df4fd2c7
YW
109 assert(m);
110
111 r = vconsole_read_data(c, m);
112 if (r < 0)
113 return r;
1822350d 114
4897d1dc
ZJS
115 r = x11_convert_to_vconsole(c);
116 if (r <= 0)
117 return r;
502f9614 118
4897d1dc
ZJS
119 /* modified */
120 r = vconsole_write_data(c);
121 if (r < 0)
122 log_error_errno(r, "Failed to save virtual console keymap: %m");
1822350d 123
df4fd2c7 124 sd_bus_emit_properties_changed(sd_bus_message_get_bus(m),
4897d1dc
ZJS
125 "/org/freedesktop/locale1",
126 "org.freedesktop.locale1",
127 "VConsoleKeymap", "VConsoleKeymapToggle", NULL);
1822350d 128
df4fd2c7 129 return vconsole_reload(sd_bus_message_get_bus(m));
1822350d
KS
130}
131
ebcf1f97
LP
132static int property_get_locale(
133 sd_bus *bus,
134 const char *path,
135 const char *interface,
136 const char *property,
137 sd_bus_message *reply,
138 void *userdata,
139 sd_bus_error *error) {
140
76400a62 141 Context *c = ASSERT_PTR(userdata);
b47d419c 142 _cleanup_strv_free_ char **l = NULL;
ab4ab13c 143 int r;
df4fd2c7
YW
144
145 r = locale_read_data(c, reply);
146 if (r < 0)
147 return r;
1822350d 148
3d36b5d7
YW
149 r = locale_context_build_env(&c->locale_context, &l, NULL);
150 if (r < 0)
151 return r;
1822350d 152
8d451309 153 return sd_bus_message_append_strv(reply, l);
1822350d
KS
154}
155
df4fd2c7
YW
156static int property_get_vconsole(
157 sd_bus *bus,
158 const char *path,
159 const char *interface,
160 const char *property,
161 sd_bus_message *reply,
162 void *userdata,
163 sd_bus_error *error) {
164
76400a62 165 Context *c = ASSERT_PTR(userdata);
df4fd2c7
YW
166 int r;
167
76400a62
YW
168 assert(property);
169
df4fd2c7
YW
170 r = vconsole_read_data(c, reply);
171 if (r < 0)
172 return r;
173
174 if (streq(property, "VConsoleKeymap"))
175 return sd_bus_message_append_basic(reply, 's', c->vc_keymap);
76400a62 176 if (streq(property, "VConsoleKeymapToggle"))
df4fd2c7
YW
177 return sd_bus_message_append_basic(reply, 's', c->vc_keymap_toggle);
178
179 return -EINVAL;
180}
181
182static int property_get_xkb(
183 sd_bus *bus,
184 const char *path,
185 const char *interface,
186 const char *property,
187 sd_bus_message *reply,
188 void *userdata,
189 sd_bus_error *error) {
190
76400a62 191 Context *c = ASSERT_PTR(userdata);
df4fd2c7
YW
192 int r;
193
76400a62
YW
194 assert(property);
195
df4fd2c7
YW
196 r = x11_read_data(c, reply);
197 if (r < 0)
198 return r;
199
200 if (streq(property, "X11Layout"))
201 return sd_bus_message_append_basic(reply, 's', c->x11_layout);
76400a62 202 if (streq(property, "X11Model"))
df4fd2c7 203 return sd_bus_message_append_basic(reply, 's', c->x11_model);
76400a62 204 if (streq(property, "X11Variant"))
df4fd2c7 205 return sd_bus_message_append_basic(reply, 's', c->x11_variant);
76400a62 206 if (streq(property, "X11Options"))
df4fd2c7
YW
207 return sd_bus_message_append_basic(reply, 's', c->x11_options);
208
209 return -EINVAL;
210}
211
a00a78b8
LP
212static int process_locale_list_item(
213 const char *assignment,
214 char *new_locale[static _VARIABLE_LC_MAX],
8f20232f 215 bool use_localegen,
a00a78b8
LP
216 sd_bus_error *error) {
217
218 assert(assignment);
219 assert(new_locale);
220
221 for (LocaleVariable p = 0; p < _VARIABLE_LC_MAX; p++) {
222 const char *name, *e;
223
224 assert_se(name = locale_variable_to_string(p));
225
226 e = startswith(assignment, name);
227 if (!e)
228 continue;
229
230 if (*e != '=')
231 continue;
232
233 e++;
234
235 if (!locale_is_valid(e))
236 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Locale %s is not valid, refusing.", e);
8f20232f 237 if (!use_localegen && locale_is_installed(e) <= 0)
a00a78b8
LP
238 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Locale %s not installed, refusing.", e);
239 if (new_locale[p])
240 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Locale variable %s set twice, refusing.", name);
241
242 new_locale[p] = strdup(e);
243 if (!new_locale[p])
244 return -ENOMEM;
245
246 return 0;
247 }
248
249 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Locale assignment %s not valid, refusing.", assignment);
250}
251
76400a62 252static int locale_gen_process_locale(char *new_locale[static _VARIABLE_LC_MAX], sd_bus_error *error) {
8f20232f 253 int r;
76400a62 254
8f20232f
MK
255 assert(new_locale);
256
257 for (LocaleVariable p = 0; p < _VARIABLE_LC_MAX; p++) {
258 if (p == VARIABLE_LANGUAGE)
259 continue;
260 if (isempty(new_locale[p]))
261 continue;
262 if (locale_is_installed(new_locale[p]))
263 continue;
264
265 r = locale_gen_enable_locale(new_locale[p]);
266 if (r == -ENOEXEC) {
267 log_error_errno(r, "Refused to enable locale for generation: %m");
268 return sd_bus_error_setf(error,
269 SD_BUS_ERROR_INVALID_ARGS,
270 "Specified locale is not installed and non-UTF-8 locale will not be auto-generated: %s",
271 new_locale[p]);
76400a62
YW
272 }
273 if (r == -EINVAL) {
8f20232f
MK
274 log_error_errno(r, "Failed to enable invalid locale %s for generation.", new_locale[p]);
275 return sd_bus_error_setf(error,
276 SD_BUS_ERROR_INVALID_ARGS,
277 "Can not enable locale generation for invalid locale: %s",
278 new_locale[p]);
76400a62
YW
279 }
280 if (r < 0) {
8f20232f
MK
281 log_error_errno(r, "Failed to enable locale for generation: %m");
282 return sd_bus_error_set_errnof(error, r, "Failed to enable locale generation: %m");
283 }
284
285 r = locale_gen_run();
286 if (r < 0) {
287 log_error_errno(r, "Failed to generate locale: %m");
288 return sd_bus_error_set_errnof(error, r, "Failed to generate locale: %m");
289 }
290 }
291
292 return 0;
293}
294
19070062 295static int method_set_locale(sd_bus_message *m, void *userdata, sd_bus_error *error) {
e6755a33 296 _cleanup_(locale_variables_freep) char *new_locale[_VARIABLE_LC_MAX] = {};
3d36b5d7 297 _cleanup_strv_free_ char **l = NULL, **l_set = NULL, **l_unset = NULL;
99534007 298 Context *c = ASSERT_PTR(userdata);
a00a78b8 299 int interactive, r;
8f20232f 300 bool use_localegen;
1822350d 301
19070062 302 assert(m);
19070062 303
a00a78b8 304 r = sd_bus_message_read_strv(m, &l);
8d451309 305 if (r < 0)
ebcf1f97 306 return r;
1822350d 307
8d451309
KS
308 r = sd_bus_message_read_basic(m, 'b', &interactive);
309 if (r < 0)
ebcf1f97 310 return r;
1822350d 311
8f20232f
MK
312 use_localegen = locale_gen_check_available();
313
4156e767 314 /* If single locale without variable name is provided, then we assume it is LANG=. */
a00a78b8
LP
315 if (strv_length(l) == 1 && !strchr(l[0], '=')) {
316 if (!locale_is_valid(l[0]))
317 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid locale specification: %s", l[0]);
8f20232f 318 if (!use_localegen && locale_is_installed(l[0]) <= 0)
a00a78b8 319 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Specified locale is not installed: %s", l[0]);
4156e767 320
a00a78b8 321 new_locale[VARIABLE_LANG] = strdup(l[0]);
4156e767
YW
322 if (!new_locale[VARIABLE_LANG])
323 return -ENOMEM;
324
325 l = strv_free(l);
326 }
327
df4fd2c7 328 /* Check whether a variable is valid */
8d451309 329 STRV_FOREACH(i, l) {
8f20232f 330 r = process_locale_list_item(*i, new_locale, use_localegen, error);
a00a78b8
LP
331 if (r < 0)
332 return r;
8d451309 333 }
1822350d 334
4e829d21
ZJS
335 /* If LANG was specified, but not LANGUAGE, check if we should
336 * set it based on the language fallback table. */
df4fd2c7
YW
337 if (!isempty(new_locale[VARIABLE_LANG]) &&
338 isempty(new_locale[VARIABLE_LANGUAGE])) {
4e829d21
ZJS
339 _cleanup_free_ char *language = NULL;
340
df4fd2c7 341 (void) find_language_fallback(new_locale[VARIABLE_LANG], &language);
4e829d21 342 if (language) {
df4fd2c7
YW
343 log_debug("Converted LANG=%s to LANGUAGE=%s", new_locale[VARIABLE_LANG], language);
344 free_and_replace(new_locale[VARIABLE_LANGUAGE], language);
4e829d21
ZJS
345 }
346 }
347
df4fd2c7
YW
348 r = locale_read_data(c, m);
349 if (r < 0) {
350 log_error_errno(r, "Failed to read locale data: %m");
1b09b81c 351 return sd_bus_error_set(error, SD_BUS_ERROR_FAILED, "Failed to read locale data");
df4fd2c7 352 }
8d451309 353
df4fd2c7 354 /* Merge with the current settings */
3d36b5d7
YW
355 r = locale_context_merge(&c->locale_context, new_locale);
356 if (r < 0)
357 return r;
1822350d 358
3d36b5d7 359 locale_variables_simplify(new_locale);
1822350d 360
3d36b5d7 361 if (locale_context_equal(&c->locale_context, new_locale)) {
df4fd2c7
YW
362 log_debug("Locale settings were not modified.");
363 return sd_bus_reply_method_return(m, NULL);
364 }
1822350d 365
df4fd2c7
YW
366 r = bus_verify_polkit_async(
367 m,
368 CAP_SYS_ADMIN,
369 "org.freedesktop.locale1.set-locale",
370 NULL,
371 interactive,
372 UID_INVALID,
af7865c1 373 &c->polkit_registry,
df4fd2c7
YW
374 error);
375 if (r < 0)
376 return r;
377 if (r == 0)
378 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1822350d 379
8f20232f
MK
380 /* Generate locale in case it is missing and the system is using locale-gen */
381 if (use_localegen) {
382 r = locale_gen_process_locale(new_locale, error);
383 if (r < 0)
384 return r;
385 }
386
3d36b5d7 387 locale_context_take(&c->locale_context, new_locale);
1822350d 388
8f20232f 389 /* Write locale configuration */
3d36b5d7 390 r = locale_context_save(&c->locale_context, &l_set, &l_unset);
df4fd2c7
YW
391 if (r < 0) {
392 log_error_errno(r, "Failed to set locale: %m");
393 return sd_bus_error_set_errnof(error, r, "Failed to set locale: %m");
394 }
502f9614 395
c8966e81
FB
396 /* Since we just updated the locale configuration file, ask the system manager to read it again to
397 * update its default locale settings. It's important to not use UnsetAndSetEnvironment or a similar
398 * method because in this case unsetting variables means restoring them to PID1 default values, which
399 * may be outdated, since locale.conf has just changed and PID1 hasn't read it */
400 (void) reload_system_manager(sd_bus_message_get_bus(m));
1822350d 401
3d36b5d7 402 if (!strv_isempty(l_set)) {
c2b2df60 403 _cleanup_free_ char *line = NULL;
df4fd2c7 404
3d36b5d7 405 line = strv_join(l_set, ", ");
df4fd2c7 406 log_info("Changed locale to %s.", strnull(line));
502f9614 407 } else
df4fd2c7
YW
408 log_info("Changed locale to unset.");
409
410 (void) sd_bus_emit_properties_changed(
411 sd_bus_message_get_bus(m),
412 "/org/freedesktop/locale1",
413 "org.freedesktop.locale1",
414 "Locale", NULL);
502f9614 415
df2d202e 416 return sd_bus_reply_method_return(m, NULL);
8d451309 417}
1822350d 418
19070062 419static int method_set_vc_keyboard(sd_bus_message *m, void *userdata, sd_bus_error *error) {
99534007 420 Context *c = ASSERT_PTR(userdata);
5980d463 421 const char *keymap, *keymap_toggle;
b47ff73b 422 int convert, interactive, r;
f2cc3753 423
19070062 424 assert(m);
19070062 425
8d451309
KS
426 r = sd_bus_message_read(m, "ssbb", &keymap, &keymap_toggle, &convert, &interactive);
427 if (r < 0)
ebcf1f97 428 return r;
1822350d 429
3c6f7c34
LP
430 keymap = empty_to_null(keymap);
431 keymap_toggle = empty_to_null(keymap_toggle);
1822350d 432
df4fd2c7
YW
433 r = vconsole_read_data(c, m);
434 if (r < 0) {
435 log_error_errno(r, "Failed to read virtual console keymap data: %m");
9ef70c06
ZJS
436 return sd_bus_error_set_errnof(error, r, "Failed to read virtual console keymap data: %m");
437 }
438
439 FOREACH_STRING(name, keymap ?: keymap_toggle, keymap ? keymap_toggle : NULL) {
440 r = keymap_exists(name); /* This also verifies that the keymap name is kosher. */
441 if (r < 0) {
442 log_error_errno(r, "Failed to check keymap %s: %m", name);
443 return sd_bus_error_set_errnof(error, r, "Failed to check keymap %s: %m", name);
444 }
445 if (r == 0)
446 return sd_bus_error_setf(error, SD_BUS_ERROR_FAILED, "Keymap %s is not installed.", name);
df4fd2c7
YW
447 }
448
b47ff73b
YW
449 if (streq_ptr(keymap, c->vc_keymap) &&
450 streq_ptr(keymap_toggle, c->vc_keymap_toggle))
451 return sd_bus_reply_method_return(m, NULL);
1822350d 452
b47ff73b
YW
453 r = bus_verify_polkit_async(
454 m,
455 CAP_SYS_ADMIN,
456 "org.freedesktop.locale1.set-keyboard",
457 NULL,
458 interactive,
459 UID_INVALID,
af7865c1 460 &c->polkit_registry,
b47ff73b
YW
461 error);
462 if (r < 0)
463 return r;
464 if (r == 0)
465 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1822350d 466
b47ff73b
YW
467 if (free_and_strdup(&c->vc_keymap, keymap) < 0 ||
468 free_and_strdup(&c->vc_keymap_toggle, keymap_toggle) < 0)
469 return -ENOMEM;
0b507b17 470
b47ff73b
YW
471 r = vconsole_write_data(c);
472 if (r < 0) {
473 log_error_errno(r, "Failed to set virtual console keymap: %m");
474 return sd_bus_error_set_errnof(error, r, "Failed to set virtual console keymap: %m");
475 }
1822350d 476
b47ff73b
YW
477 log_info("Changed virtual console keymap to '%s' toggle '%s'",
478 strempty(c->vc_keymap), strempty(c->vc_keymap_toggle));
1822350d 479
26a93376 480 (void) vconsole_reload(sd_bus_message_get_bus(m));
1822350d 481
b47ff73b
YW
482 (void) sd_bus_emit_properties_changed(
483 sd_bus_message_get_bus(m),
484 "/org/freedesktop/locale1",
485 "org.freedesktop.locale1",
486 "VConsoleKeymap", "VConsoleKeymapToggle", NULL);
1822350d 487
b47ff73b 488 if (convert) {
df4fd2c7 489 r = vconsole_convert_to_x11_and_emit(c, m);
b47ff73b
YW
490 if (r < 0)
491 log_error_errno(r, "Failed to convert keymap data: %m");
8d451309 492 }
1822350d 493
df2d202e 494 return sd_bus_reply_method_return(m, NULL);
8d451309 495}
1822350d 496
349cc4a5 497#if HAVE_XKBCOMMON
5de34470 498
3cb063fd 499_printf_(3, 0)
d4f5a1f4 500static void log_xkb(struct xkb_context *ctx, enum xkb_log_level lvl, const char *format, va_list args) {
8433e339
JS
501 const char *fmt;
502
63c372cb 503 fmt = strjoina("libxkbcommon: ", format);
56e577c6 504 DISABLE_WARNING_FORMAT_NONLITERAL;
11c8b1f1 505 log_internalv(LOG_DEBUG, 0, PROJECT_FILE, __LINE__, __func__, fmt, args);
56e577c6 506 REENABLE_WARNING;
d4f5a1f4
DH
507}
508
5de34470
LP
509#define LOAD_SYMBOL(symbol, dl, name) \
510 ({ \
511 (symbol) = (typeof(symbol)) dlvsym((dl), (name), "V_0.5.0"); \
512 (symbol) ? 0 : -EOPNOTSUPP; \
513 })
514
d4f5a1f4 515static int verify_xkb_rmlvo(const char *model, const char *layout, const char *variant, const char *options) {
5de34470
LP
516
517 /* We dlopen() the library in order to make the dependency soft. The library (and what it pulls in) is huge
518 * after all, hence let's support XKB maps when the library is around, and refuse otherwise. The function
519 * pointers to the shared library are below: */
520
521 struct xkb_context* (*symbol_xkb_context_new)(enum xkb_context_flags flags) = NULL;
522 void (*symbol_xkb_context_unref)(struct xkb_context *context) = NULL;
523 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;
524 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;
525 void (*symbol_xkb_keymap_unref)(struct xkb_keymap *keymap) = NULL;
526
d4f5a1f4
DH
527 const struct xkb_rule_names rmlvo = {
528 .model = model,
529 .layout = layout,
530 .variant = variant,
531 .options = options,
532 };
533 struct xkb_context *ctx = NULL;
534 struct xkb_keymap *km = NULL;
88d775b7 535 _cleanup_(dlclosep) void *dl = NULL;
d4f5a1f4
DH
536 int r;
537
5de34470
LP
538 /* Compile keymap from RMLVO information to check out its validity */
539
540 dl = dlopen("libxkbcommon.so.0", RTLD_LAZY);
541 if (!dl)
542 return -EOPNOTSUPP;
543
544 r = LOAD_SYMBOL(symbol_xkb_context_new, dl, "xkb_context_new");
545 if (r < 0)
546 goto finish;
547
548 r = LOAD_SYMBOL(symbol_xkb_context_unref, dl, "xkb_context_unref");
549 if (r < 0)
550 goto finish;
551
552 r = LOAD_SYMBOL(symbol_xkb_context_set_log_fn, dl, "xkb_context_set_log_fn");
553 if (r < 0)
554 goto finish;
d4f5a1f4 555
5de34470
LP
556 r = LOAD_SYMBOL(symbol_xkb_keymap_new_from_names, dl, "xkb_keymap_new_from_names");
557 if (r < 0)
558 goto finish;
559
560 r = LOAD_SYMBOL(symbol_xkb_keymap_unref, dl, "xkb_keymap_unref");
561 if (r < 0)
562 goto finish;
563
564 ctx = symbol_xkb_context_new(XKB_CONTEXT_NO_ENVIRONMENT_NAMES);
d4f5a1f4
DH
565 if (!ctx) {
566 r = -ENOMEM;
5de34470 567 goto finish;
d4f5a1f4
DH
568 }
569
5de34470 570 symbol_xkb_context_set_log_fn(ctx, log_xkb);
d4f5a1f4 571
5de34470 572 km = symbol_xkb_keymap_new_from_names(ctx, &rmlvo, XKB_KEYMAP_COMPILE_NO_FLAGS);
d4f5a1f4
DH
573 if (!km) {
574 r = -EINVAL;
5de34470 575 goto finish;
d4f5a1f4
DH
576 }
577
578 r = 0;
579
5de34470
LP
580finish:
581 if (symbol_xkb_keymap_unref && km)
582 symbol_xkb_keymap_unref(km);
583
584 if (symbol_xkb_context_unref && ctx)
585 symbol_xkb_context_unref(ctx);
586
d4f5a1f4
DH
587 return r;
588}
5de34470 589
d4f5a1f4 590#else
5de34470 591
d4f5a1f4
DH
592static int verify_xkb_rmlvo(const char *model, const char *layout, const char *variant, const char *options) {
593 return 0;
594}
5de34470 595
d4f5a1f4
DH
596#endif
597
19070062 598static int method_set_x11_keyboard(sd_bus_message *m, void *userdata, sd_bus_error *error) {
99534007 599 Context *c = ASSERT_PTR(userdata);
8d451309 600 const char *layout, *model, *variant, *options;
b47ff73b 601 int convert, interactive, r;
1822350d 602
19070062 603 assert(m);
19070062 604
8d451309
KS
605 r = sd_bus_message_read(m, "ssssbb", &layout, &model, &variant, &options, &convert, &interactive);
606 if (r < 0)
ebcf1f97 607 return r;
1822350d 608
3c6f7c34
LP
609 layout = empty_to_null(layout);
610 model = empty_to_null(model);
611 variant = empty_to_null(variant);
612 options = empty_to_null(options);
1822350d 613
df4fd2c7
YW
614 r = x11_read_data(c, m);
615 if (r < 0) {
616 log_error_errno(r, "Failed to read x11 keyboard layout data: %m");
1b09b81c 617 return sd_bus_error_set(error, SD_BUS_ERROR_FAILED, "Failed to read x11 keyboard layout data");
df4fd2c7
YW
618 }
619
b47ff73b
YW
620 if (streq_ptr(layout, c->x11_layout) &&
621 streq_ptr(model, c->x11_model) &&
622 streq_ptr(variant, c->x11_variant) &&
623 streq_ptr(options, c->x11_options))
624 return sd_bus_reply_method_return(m, NULL);
625
626 if ((layout && !string_is_safe(layout)) ||
627 (model && !string_is_safe(model)) ||
628 (variant && !string_is_safe(variant)) ||
629 (options && !string_is_safe(options)))
1b09b81c 630 return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Received invalid keyboard data");
b47ff73b 631
fe28d887
YW
632 r = verify_xkb_rmlvo(model, layout, variant, options);
633 if (r < 0) {
634 log_error_errno(r, "Cannot compile XKB keymap for new x11 keyboard layout ('%s' / '%s' / '%s' / '%s'): %m",
635 strempty(model), strempty(layout), strempty(variant), strempty(options));
636
637 if (r == -EOPNOTSUPP)
1b09b81c 638 return sd_bus_error_set(error, SD_BUS_ERROR_NOT_SUPPORTED, "Local keyboard configuration not supported on this system.");
fe28d887
YW
639
640 return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Specified keymap cannot be compiled, refusing as invalid.");
641 }
642
b47ff73b
YW
643 r = bus_verify_polkit_async(
644 m,
645 CAP_SYS_ADMIN,
646 "org.freedesktop.locale1.set-keyboard",
647 NULL,
648 interactive,
649 UID_INVALID,
af7865c1 650 &c->polkit_registry,
b47ff73b
YW
651 error);
652 if (r < 0)
653 return r;
654 if (r == 0)
655 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1822350d 656
b47ff73b
YW
657 if (free_and_strdup(&c->x11_layout, layout) < 0 ||
658 free_and_strdup(&c->x11_model, model) < 0 ||
659 free_and_strdup(&c->x11_variant, variant) < 0 ||
660 free_and_strdup(&c->x11_options, options) < 0)
661 return -ENOMEM;
1822350d 662
b47ff73b
YW
663 r = x11_write_data(c);
664 if (r < 0) {
665 log_error_errno(r, "Failed to set X11 keyboard layout: %m");
666 return sd_bus_error_set_errnof(error, r, "Failed to set X11 keyboard layout: %m");
667 }
1822350d 668
b47ff73b
YW
669 log_info("Changed X11 keyboard layout to '%s' model '%s' variant '%s' options '%s'",
670 strempty(c->x11_layout),
671 strempty(c->x11_model),
672 strempty(c->x11_variant),
673 strempty(c->x11_options));
1822350d 674
b47ff73b
YW
675 (void) sd_bus_emit_properties_changed(
676 sd_bus_message_get_bus(m),
677 "/org/freedesktop/locale1",
678 "org.freedesktop.locale1",
679 "X11Layout", "X11Model", "X11Variant", "X11Options", NULL);
1822350d 680
b47ff73b 681 if (convert) {
df4fd2c7 682 r = x11_convert_to_vconsole_and_emit(c, m);
b47ff73b
YW
683 if (r < 0)
684 log_error_errno(r, "Failed to convert keymap data: %m");
1822350d
KS
685 }
686
df2d202e 687 return sd_bus_reply_method_return(m, NULL);
1822350d
KS
688}
689
8d451309
KS
690static const sd_bus_vtable locale_vtable[] = {
691 SD_BUS_VTABLE_START(0),
6d1bd3b2 692 SD_BUS_PROPERTY("Locale", "as", property_get_locale, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
df4fd2c7
YW
693 SD_BUS_PROPERTY("X11Layout", "s", property_get_xkb, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
694 SD_BUS_PROPERTY("X11Model", "s", property_get_xkb, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
695 SD_BUS_PROPERTY("X11Variant", "s", property_get_xkb, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
696 SD_BUS_PROPERTY("X11Options", "s", property_get_xkb, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
697 SD_BUS_PROPERTY("VConsoleKeymap", "s", property_get_vconsole, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
698 SD_BUS_PROPERTY("VConsoleKeymapToggle", "s", property_get_vconsole, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
5cc34d6d
ZJS
699
700 SD_BUS_METHOD_WITH_NAMES("SetLocale",
701 "asb",
702 SD_BUS_PARAM(locale)
703 SD_BUS_PARAM(interactive),
704 NULL,,
705 method_set_locale,
706 SD_BUS_VTABLE_UNPRIVILEGED),
707 SD_BUS_METHOD_WITH_NAMES("SetVConsoleKeyboard",
708 "ssbb",
709 SD_BUS_PARAM(keymap)
710 SD_BUS_PARAM(keymap_toggle)
711 SD_BUS_PARAM(convert)
712 SD_BUS_PARAM(interactive),
713 NULL,,
714 method_set_vc_keyboard,
715 SD_BUS_VTABLE_UNPRIVILEGED),
716 SD_BUS_METHOD_WITH_NAMES("SetX11Keyboard",
717 "ssssbb",
718 SD_BUS_PARAM(layout)
719 SD_BUS_PARAM(model)
720 SD_BUS_PARAM(variant)
721 SD_BUS_PARAM(options)
722 SD_BUS_PARAM(convert)
723 SD_BUS_PARAM(interactive),
724 NULL,,
725 method_set_x11_keyboard,
726 SD_BUS_VTABLE_UNPRIVILEGED),
727
8d451309
KS
728 SD_BUS_VTABLE_END
729};
730
5ceceff1
ZJS
731static const BusObjectImplementation manager_object = {
732 "/org/freedesktop/locale1",
733 "org.freedesktop.locale1",
734 .vtables = BUS_VTABLES(locale_vtable),
735};
736
8d451309 737static int connect_bus(Context *c, sd_event *event, sd_bus **_bus) {
4afd3348 738 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
1822350d
KS
739 int r;
740
8d451309
KS
741 assert(c);
742 assert(event);
1822350d
KS
743 assert(_bus);
744
76b54375 745 r = sd_bus_default_system(&bus);
f647962d
MS
746 if (r < 0)
747 return log_error_errno(r, "Failed to get system bus connection: %m");
1822350d 748
5ceceff1 749 r = bus_add_implementation(bus, &manager_object, c);
f647962d 750 if (r < 0)
5ceceff1 751 return r;
1822350d 752
ac9f55ed
LP
753 r = bus_log_control_api_register(bus);
754 if (r < 0)
755 return r;
756
0c0b9306 757 r = sd_bus_request_name_async(bus, NULL, "org.freedesktop.locale1", 0, NULL, NULL);
f647962d 758 if (r < 0)
0c0b9306 759 return log_error_errno(r, "Failed to request name: %m");
1822350d 760
8d451309 761 r = sd_bus_attach_event(bus, event, 0);
f647962d
MS
762 if (r < 0)
763 return log_error_errno(r, "Failed to attach bus to event loop: %m");
1822350d 764
1cc6c93a 765 *_bus = TAKE_PTR(bus);
1822350d 766
8d451309 767 return 0;
1822350d
KS
768}
769
c6f09e6a 770static int run(int argc, char *argv[]) {
56b7f112 771 _cleanup_(context_clear) Context context = {};
4afd3348
LP
772 _cleanup_(sd_event_unrefp) sd_event *event = NULL;
773 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
1822350d 774 int r;
1822350d 775
d2acb93d 776 log_setup();
8d451309 777
fc021a5b
ZJS
778 r = service_parse_argv("systemd-localed.service",
779 "Manage system locale settings and key mappings.",
5ceceff1
ZJS
780 BUS_IMPLEMENTATIONS(&manager_object,
781 &log_control_object),
fc021a5b
ZJS
782 argc, argv);
783 if (r <= 0)
784 return r;
785
1822350d 786 umask(0022);
a9ba0e32
CG
787
788 r = mac_selinux_init();
789 if (r < 0)
790 return r;
1822350d 791
50008ae4
YW
792 assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGTERM, SIGINT, -1) >= 0);
793
afc6adb5 794 r = sd_event_default(&event);
c6f09e6a
YW
795 if (r < 0)
796 return log_error_errno(r, "Failed to allocate event loop: %m");
1822350d 797
50008ae4
YW
798 (void) sd_event_set_watchdog(event, true);
799
800 r = sd_event_add_signal(event, NULL, SIGINT, NULL, NULL);
c6f09e6a
YW
801 if (r < 0)
802 return log_error_errno(r, "Failed to install SIGINT handler: %m");
50008ae4
YW
803
804 r = sd_event_add_signal(event, NULL, SIGTERM, NULL, NULL);
c6f09e6a
YW
805 if (r < 0)
806 return log_error_errno(r, "Failed to install SIGTERM handler: %m");
cde93897 807
8d451309 808 r = connect_bus(&context, event, &bus);
1822350d 809 if (r < 0)
c6f09e6a 810 return r;
1822350d 811
37224a5f 812 r = bus_event_loop_with_idle(event, bus, "org.freedesktop.locale1", DEFAULT_EXIT_USEC, NULL, NULL);
4897d1dc 813 if (r < 0)
c6f09e6a 814 return log_error_errno(r, "Failed to run event loop: %m");
1822350d 815
c6f09e6a 816 return 0;
1822350d 817}
c6f09e6a
YW
818
819DEFINE_MAIN_FUNCTION(run);