]>
Commit | Line | Data |
---|---|---|
1822350d KS |
1 | /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ |
2 | ||
3 | /*** | |
4 | This file is part of systemd. | |
5 | ||
6 | Copyright 2011 Lennart Poettering | |
7 | ||
8 | systemd is free software; you can redistribute it and/or modify it | |
9 | under the terms of the GNU General Public License as published by | |
10 | the Free Software Foundation; either version 2 of the License, or | |
11 | (at your option) any later version. | |
12 | ||
13 | systemd is distributed in the hope that it will be useful, but | |
14 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
19 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
20 | ***/ | |
21 | ||
22 | #include <dbus/dbus.h> | |
23 | ||
24 | #include <errno.h> | |
25 | #include <string.h> | |
26 | #include <unistd.h> | |
27 | ||
28 | #include "util.h" | |
29 | #include "strv.h" | |
30 | #include "dbus-common.h" | |
31 | #include "polkit.h" | |
32 | #include "def.h" | |
33 | ||
34 | #define INTERFACE \ | |
35 | " <interface name=\"org.freedesktop.locale1\">\n" \ | |
36 | " <property name=\"Locale\" type=\"as\" access=\"read\"/>\n" \ | |
37 | " <property name=\"VConsoleKeymap\" type=\"s\" access=\"read\"/>\n" \ | |
38 | " <property name=\"VConsoleKeymapToggle\" type=\"s\" access=\"read\"/>\n" \ | |
39 | " <property name=\"X11Layout\" type=\"s\" access=\"read\"/>\n" \ | |
40 | " <property name=\"X11Model\" type=\"s\" access=\"read\"/>\n" \ | |
41 | " <property name=\"X11Variant\" type=\"s\" access=\"read\"/>\n" \ | |
42 | " <property name=\"X11Options\" type=\"s\" access=\"read\"/>\n" \ | |
43 | " <method name=\"SetLocale\">\n" \ | |
44 | " <arg name=\"locale\" type=\"as\" direction=\"in\"/>\n" \ | |
45 | " <arg name=\"user_interaction\" type=\"b\" direction=\"in\"/>\n" \ | |
46 | " </method>\n" \ | |
47 | " <method name=\"SetVConsoleKeyboard\">\n" \ | |
48 | " <arg name=\"keymap\" type=\"s\" direction=\"in\"/>\n" \ | |
49 | " <arg name=\"keymap_toggle\" type=\"s\" direction=\"in\"/>\n" \ | |
50 | " <arg name=\"convert\" type=\"b\" direction=\"in\"/>\n" \ | |
51 | " <arg name=\"user_interaction\" type=\"b\" direction=\"in\"/>\n" \ | |
52 | " </method>\n" \ | |
53 | " <method name=\"SetX11Keyboard\">\n" \ | |
54 | " <arg name=\"layout\" type=\"s\" direction=\"in\"/>\n" \ | |
55 | " <arg name=\"model\" type=\"s\" direction=\"in\"/>\n" \ | |
56 | " <arg name=\"variant\" type=\"s\" direction=\"in\"/>\n" \ | |
57 | " <arg name=\"options\" type=\"s\" direction=\"in\"/>\n" \ | |
58 | " <arg name=\"convert\" type=\"b\" direction=\"in\"/>\n" \ | |
59 | " <arg name=\"user_interaction\" type=\"b\" direction=\"in\"/>\n" \ | |
60 | " </method>\n" \ | |
61 | " </interface>\n" | |
62 | ||
63 | #define INTROSPECTION \ | |
64 | DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE \ | |
65 | "<node>\n" \ | |
66 | INTERFACE \ | |
67 | BUS_PROPERTIES_INTERFACE \ | |
68 | BUS_INTROSPECTABLE_INTERFACE \ | |
69 | BUS_PEER_INTERFACE \ | |
70 | "</node>\n" | |
71 | ||
72 | #define INTERFACES_LIST \ | |
73 | BUS_GENERIC_INTERFACES_LIST \ | |
74 | "org.freedesktop.locale1\0" | |
75 | ||
76 | const char locale_interface[] _introspect_("locale1") = INTERFACE; | |
77 | ||
78 | enum { | |
79 | /* We don't list LC_ALL here on purpose. People should be | |
80 | * using LANG instead. */ | |
81 | ||
82 | PROP_LANG, | |
83 | PROP_LANGUAGE, | |
84 | PROP_LC_CTYPE, | |
85 | PROP_LC_NUMERIC, | |
86 | PROP_LC_TIME, | |
87 | PROP_LC_COLLATE, | |
88 | PROP_LC_MONETARY, | |
89 | PROP_LC_MESSAGES, | |
90 | PROP_LC_PAPER, | |
91 | PROP_LC_NAME, | |
92 | PROP_LC_ADDRESS, | |
93 | PROP_LC_TELEPHONE, | |
94 | PROP_LC_MEASUREMENT, | |
95 | PROP_LC_IDENTIFICATION, | |
96 | _PROP_MAX | |
97 | }; | |
98 | ||
99 | static const char * const names[_PROP_MAX] = { | |
100 | [PROP_LANG] = "LANG", | |
101 | [PROP_LANGUAGE] = "LANGUAGE", | |
102 | [PROP_LC_CTYPE] = "LC_CTYPE", | |
103 | [PROP_LC_NUMERIC] = "LC_NUMERIC", | |
104 | [PROP_LC_TIME] = "LC_TIME", | |
105 | [PROP_LC_COLLATE] = "LC_COLLATE", | |
106 | [PROP_LC_MONETARY] = "LC_MONETARY", | |
107 | [PROP_LC_MESSAGES] = "LC_MESSAGES", | |
108 | [PROP_LC_PAPER] = "LC_PAPER", | |
109 | [PROP_LC_NAME] = "LC_NAME", | |
110 | [PROP_LC_ADDRESS] = "LC_ADDRESS", | |
111 | [PROP_LC_TELEPHONE] = "LC_TELEPHONE", | |
112 | [PROP_LC_MEASUREMENT] = "LC_MEASUREMENT", | |
113 | [PROP_LC_IDENTIFICATION] = "LC_IDENTIFICATION" | |
114 | }; | |
115 | ||
116 | static char *data[_PROP_MAX] = { | |
117 | NULL, | |
118 | NULL, | |
119 | NULL, | |
120 | NULL, | |
121 | NULL, | |
122 | NULL, | |
123 | NULL, | |
124 | NULL, | |
125 | NULL, | |
126 | NULL, | |
127 | NULL, | |
128 | NULL, | |
129 | NULL | |
130 | }; | |
131 | ||
132 | static char *x11_layout = NULL, *x11_model = NULL, *x11_variant = NULL, *x11_options = NULL; | |
133 | static char *vc_keymap = NULL, *vc_keymap_toggle = NULL; | |
134 | ||
135 | static usec_t remain_until = 0; | |
136 | ||
137 | static int free_and_set(char **s, const char *v) { | |
138 | int r; | |
139 | char *t; | |
140 | ||
141 | assert(s); | |
142 | ||
143 | r = strdup_or_null(isempty(v) ? NULL : v, &t); | |
144 | if (r < 0) | |
145 | return r; | |
146 | ||
147 | free(*s); | |
148 | *s = t; | |
149 | ||
150 | return 0; | |
151 | } | |
152 | ||
153 | static void free_data_locale(void) { | |
154 | int p; | |
155 | ||
156 | for (p = 0; p < _PROP_MAX; p++) { | |
157 | free(data[p]); | |
158 | data[p] = NULL; | |
159 | } | |
160 | } | |
161 | ||
162 | static void free_data_x11(void) { | |
163 | free(x11_layout); | |
164 | free(x11_model); | |
165 | free(x11_variant); | |
166 | free(x11_options); | |
167 | ||
168 | x11_layout = x11_model = x11_variant = x11_options = NULL; | |
169 | } | |
170 | ||
171 | static void free_data_vconsole(void) { | |
172 | free(vc_keymap); | |
173 | free(vc_keymap_toggle); | |
174 | ||
175 | vc_keymap = vc_keymap_toggle = NULL; | |
176 | } | |
177 | ||
178 | static void simplify(void) { | |
179 | int p; | |
180 | ||
181 | for (p = 1; p < _PROP_MAX; p++) | |
182 | if (isempty(data[p]) || streq_ptr(data[PROP_LANG], data[p])) { | |
183 | free(data[p]); | |
184 | data[p] = NULL; | |
185 | } | |
186 | } | |
187 | ||
188 | static int read_data_locale(void) { | |
189 | int r; | |
190 | ||
191 | free_data_locale(); | |
192 | ||
193 | r = parse_env_file("/etc/locale.conf", NEWLINE, | |
194 | "LANG", &data[PROP_LANG], | |
195 | "LANGUAGE", &data[PROP_LANGUAGE], | |
196 | "LC_CTYPE", &data[PROP_LC_CTYPE], | |
197 | "LC_NUMERIC", &data[PROP_LC_NUMERIC], | |
198 | "LC_TIME", &data[PROP_LC_TIME], | |
199 | "LC_COLLATE", &data[PROP_LC_COLLATE], | |
200 | "LC_MONETARY", &data[PROP_LC_MONETARY], | |
201 | "LC_MESSAGES", &data[PROP_LC_MESSAGES], | |
202 | "LC_PAPER", &data[PROP_LC_PAPER], | |
203 | "LC_NAME", &data[PROP_LC_NAME], | |
204 | "LC_ADDRESS", &data[PROP_LC_ADDRESS], | |
205 | "LC_TELEPHONE", &data[PROP_LC_TELEPHONE], | |
206 | "LC_MEASUREMENT", &data[PROP_LC_MEASUREMENT], | |
207 | "LC_IDENTIFICATION", &data[PROP_LC_IDENTIFICATION], | |
208 | NULL); | |
209 | ||
210 | if (r == -ENOENT) { | |
211 | int p; | |
212 | ||
213 | /* Fill in what we got passed from systemd. */ | |
214 | ||
215 | for (p = 0; p < _PROP_MAX; p++) { | |
216 | char *e, *d; | |
217 | ||
218 | assert(names[p]); | |
219 | ||
220 | e = getenv(names[p]); | |
221 | if (e) { | |
222 | d = strdup(e); | |
223 | if (!d) | |
224 | return -ENOMEM; | |
225 | } else | |
226 | d = NULL; | |
227 | ||
228 | free(data[p]); | |
229 | data[p] = d; | |
230 | } | |
231 | ||
232 | r = 0; | |
233 | } | |
234 | ||
235 | simplify(); | |
236 | return r; | |
237 | } | |
238 | ||
239 | static void free_data(void) { | |
240 | free_data_locale(); | |
241 | free_data_vconsole(); | |
242 | free_data_x11(); | |
243 | } | |
244 | ||
245 | static int read_data_vconsole(void) { | |
246 | int r; | |
247 | ||
248 | free_data_vconsole(); | |
249 | ||
250 | r = parse_env_file("/etc/vconsole.conf", NEWLINE, | |
251 | "KEYMAP", &vc_keymap, | |
252 | "KEYMAP_TOGGLE", &vc_keymap_toggle, | |
253 | NULL); | |
254 | ||
255 | if (r < 0 && r != -ENOENT) | |
256 | return r; | |
257 | ||
258 | return 0; | |
259 | } | |
260 | ||
261 | static int read_data_x11(void) { | |
262 | FILE *f; | |
263 | char line[LINE_MAX]; | |
264 | bool in_section = false; | |
265 | ||
266 | free_data_x11(); | |
267 | ||
268 | f = fopen("/etc/X11/xorg.conf.d/00-keyboard.conf", "re"); | |
269 | if (!f) { | |
270 | if (errno == ENOENT) { | |
271 | ||
272 | #ifdef TARGET_FEDORA | |
273 | f = fopen("/etc/X11/xorg.conf.d/00-system-setup-keyboard.conf", "re"); | |
274 | if (!f) { | |
275 | if (errno == ENOENT) | |
276 | return 0; | |
277 | else | |
278 | return -errno; | |
279 | } | |
280 | #else | |
281 | return 0; | |
282 | #endif | |
283 | ||
284 | } else | |
285 | return -errno; | |
286 | } | |
287 | ||
288 | while (fgets(line, sizeof(line), f)) { | |
289 | char *l; | |
290 | ||
291 | char_array_0(line); | |
292 | l = strstrip(line); | |
293 | ||
294 | if (l[0] == 0 || l[0] == '#') | |
295 | continue; | |
296 | ||
297 | if (in_section && first_word(l, "Option")) { | |
298 | char **a; | |
299 | ||
300 | a = strv_split_quoted(l); | |
301 | if (!a) { | |
302 | fclose(f); | |
303 | return -ENOMEM; | |
304 | } | |
305 | ||
306 | if (strv_length(a) == 3) { | |
307 | ||
308 | if (streq(a[1], "XkbLayout")) { | |
309 | free(x11_layout); | |
310 | x11_layout = a[2]; | |
311 | a[2] = NULL; | |
312 | } else if (streq(a[1], "XkbModel")) { | |
313 | free(x11_model); | |
314 | x11_model = a[2]; | |
315 | a[2] = NULL; | |
316 | } else if (streq(a[1], "XkbVariant")) { | |
317 | free(x11_variant); | |
318 | x11_variant = a[2]; | |
319 | a[2] = NULL; | |
320 | } else if (streq(a[1], "XkbOptions")) { | |
321 | free(x11_options); | |
322 | x11_options = a[2]; | |
323 | a[2] = NULL; | |
324 | } | |
325 | } | |
326 | ||
327 | strv_free(a); | |
328 | ||
329 | } else if (!in_section && first_word(l, "Section")) { | |
330 | char **a; | |
331 | ||
332 | a = strv_split_quoted(l); | |
333 | if (!a) { | |
334 | fclose(f); | |
335 | return -ENOMEM; | |
336 | } | |
337 | ||
338 | if (strv_length(a) == 2 && streq(a[1], "InputClass")) | |
339 | in_section = true; | |
340 | ||
341 | strv_free(a); | |
342 | } else if (in_section && first_word(l, "EndSection")) | |
343 | in_section = false; | |
344 | } | |
345 | ||
346 | fclose(f); | |
347 | ||
348 | return 0; | |
349 | } | |
350 | ||
351 | static int read_data(void) { | |
352 | int r, q, p; | |
353 | ||
354 | r = read_data_locale(); | |
355 | q = read_data_vconsole(); | |
356 | p = read_data_x11(); | |
357 | ||
358 | return r < 0 ? r : q < 0 ? q : p; | |
359 | } | |
360 | ||
361 | static int write_data_locale(void) { | |
362 | int r, p; | |
363 | char **l = NULL; | |
364 | ||
365 | r = load_env_file("/etc/locale.conf", &l); | |
366 | if (r < 0 && r != -ENOENT) | |
367 | return r; | |
368 | ||
369 | for (p = 0; p < _PROP_MAX; p++) { | |
370 | char *t, **u; | |
371 | ||
372 | assert(names[p]); | |
373 | ||
374 | if (isempty(data[p])) { | |
375 | l = strv_env_unset(l, names[p]); | |
376 | continue; | |
377 | } | |
378 | ||
379 | if (asprintf(&t, "%s=%s", names[p], data[p]) < 0) { | |
380 | strv_free(l); | |
381 | return -ENOMEM; | |
382 | } | |
383 | ||
384 | u = strv_env_set(l, t); | |
385 | free(t); | |
386 | strv_free(l); | |
387 | ||
388 | if (!u) | |
389 | return -ENOMEM; | |
390 | ||
391 | l = u; | |
392 | } | |
393 | ||
394 | if (strv_isempty(l)) { | |
395 | strv_free(l); | |
396 | ||
397 | if (unlink("/etc/locale.conf") < 0) | |
398 | return errno == ENOENT ? 0 : -errno; | |
399 | ||
400 | return 0; | |
401 | } | |
402 | ||
403 | r = write_env_file("/etc/locale.conf", l); | |
404 | strv_free(l); | |
405 | ||
406 | return r; | |
407 | } | |
408 | ||
409 | static void push_data(DBusConnection *bus) { | |
410 | char **l_set = NULL, **l_unset = NULL, **t; | |
411 | int c_set = 0, c_unset = 0, p; | |
412 | DBusError error; | |
413 | DBusMessage *m = NULL, *reply = NULL; | |
414 | DBusMessageIter iter, sub; | |
415 | ||
416 | dbus_error_init(&error); | |
417 | ||
418 | assert(bus); | |
419 | ||
420 | l_set = new0(char*, _PROP_MAX); | |
421 | l_unset = new0(char*, _PROP_MAX); | |
422 | if (!l_set || !l_unset) { | |
423 | log_error("Out of memory"); | |
424 | goto finish; | |
425 | } | |
426 | ||
427 | for (p = 0; p < _PROP_MAX; p++) { | |
428 | assert(names[p]); | |
429 | ||
430 | if (isempty(data[p])) | |
431 | l_unset[c_set++] = (char*) names[p]; | |
432 | else { | |
433 | char *s; | |
434 | ||
435 | if (asprintf(&s, "%s=%s", names[p], data[p]) < 0) { | |
436 | log_error("Out of memory"); | |
437 | goto finish; | |
438 | } | |
439 | ||
440 | l_set[c_unset++] = s; | |
441 | } | |
442 | } | |
443 | ||
444 | assert(c_set + c_unset == _PROP_MAX); | |
445 | m = dbus_message_new_method_call("org.freedesktop.systemd1", "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "UnsetAndSetEnvironment"); | |
446 | if (!m) { | |
447 | log_error("Could not allocate message."); | |
448 | goto finish; | |
449 | } | |
450 | ||
451 | dbus_message_iter_init_append(m, &iter); | |
452 | ||
453 | if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub)) { | |
454 | log_error("Out of memory."); | |
455 | goto finish; | |
456 | } | |
457 | ||
458 | STRV_FOREACH(t, l_unset) | |
459 | if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, t)) { | |
460 | log_error("Out of memory."); | |
461 | goto finish; | |
462 | } | |
463 | ||
464 | if (!dbus_message_iter_close_container(&iter, &sub) || | |
465 | !dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub)) { | |
466 | log_error("Out of memory."); | |
467 | goto finish; | |
468 | } | |
469 | ||
470 | STRV_FOREACH(t, l_set) | |
471 | if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, t)) { | |
472 | log_error("Out of memory."); | |
473 | goto finish; | |
474 | } | |
475 | ||
476 | if (!dbus_message_iter_close_container(&iter, &sub)) { | |
477 | log_error("Out of memory."); | |
478 | goto finish; | |
479 | } | |
480 | ||
481 | reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error); | |
482 | if (!reply) { | |
483 | log_error("Failed to set locale information: %s", bus_error_message(&error)); | |
484 | goto finish; | |
485 | } | |
486 | ||
487 | finish: | |
488 | if (m) | |
489 | dbus_message_unref(m); | |
490 | ||
491 | if (reply) | |
492 | dbus_message_unref(reply); | |
493 | ||
494 | dbus_error_free(&error); | |
495 | ||
496 | strv_free(l_set); | |
497 | free(l_unset); | |
498 | } | |
499 | ||
500 | static int write_data_vconsole(void) { | |
501 | int r; | |
502 | char **l = NULL; | |
503 | ||
504 | r = load_env_file("/etc/vconsole.conf", &l); | |
505 | if (r < 0 && r != -ENOENT) | |
506 | return r; | |
507 | ||
508 | if (isempty(vc_keymap)) | |
509 | l = strv_env_unset(l, "KEYMAP"); | |
510 | else { | |
511 | char *s, **u; | |
512 | ||
513 | s = strappend("KEYMAP=", vc_keymap); | |
514 | if (!s) { | |
515 | strv_free(l); | |
516 | return -ENOMEM; | |
517 | } | |
518 | ||
519 | u = strv_env_set(l, s); | |
520 | free(s); | |
521 | strv_free(l); | |
522 | ||
523 | if (!u) | |
524 | return -ENOMEM; | |
525 | ||
526 | l = u; | |
527 | } | |
528 | ||
529 | if (isempty(vc_keymap_toggle)) | |
530 | l = strv_env_unset(l, "KEYMAP_TOGGLE"); | |
531 | else { | |
532 | char *s, **u; | |
533 | ||
534 | s = strappend("KEYMAP_TOGGLE=", vc_keymap_toggle); | |
535 | if (!s) { | |
536 | strv_free(l); | |
537 | return -ENOMEM; | |
538 | } | |
539 | ||
540 | u = strv_env_set(l, s); | |
541 | free(s); | |
542 | strv_free(l); | |
543 | ||
544 | if (!u) | |
545 | return -ENOMEM; | |
546 | ||
547 | l = u; | |
548 | } | |
549 | ||
550 | if (strv_isempty(l)) { | |
551 | strv_free(l); | |
552 | ||
553 | if (unlink("/etc/vconsole.conf") < 0) | |
554 | return errno == ENOENT ? 0 : -errno; | |
555 | ||
556 | return 0; | |
557 | } | |
558 | ||
559 | r = write_env_file("/etc/vconsole.conf", l); | |
560 | strv_free(l); | |
561 | ||
562 | return r; | |
563 | } | |
564 | ||
565 | static int write_data_x11(void) { | |
566 | FILE *f; | |
567 | char *temp_path; | |
568 | int r; | |
569 | ||
570 | if (isempty(x11_layout) && | |
571 | isempty(x11_model) && | |
572 | isempty(x11_variant) && | |
573 | isempty(x11_options)) { | |
574 | ||
575 | #ifdef TARGET_FEDORA | |
576 | unlink("/etc/X11/xorg.conf.d/00-system-setup-keyboard.conf"); | |
577 | ||
578 | /* Symlink this to /dev/null, so that s-s-k (if it is | |
579 | * still running) doesn't recreate this. */ | |
580 | symlink("/dev/null", "/etc/X11/xorg.conf.d/00-system-setup-keyboard.conf"); | |
581 | #endif | |
582 | ||
583 | if (unlink("/etc/X11/xorg.conf.d/00-keyboard.conf") < 0) | |
584 | return errno == ENOENT ? 0 : -errno; | |
585 | ||
586 | return 0; | |
587 | } | |
588 | ||
589 | mkdir_parents("/etc/X11/xorg.conf.d", 0755); | |
590 | ||
591 | r = fopen_temporary("/etc/X11/xorg.conf.d/00-keyboard.conf", &f, &temp_path); | |
592 | if (r < 0) | |
593 | return r; | |
594 | ||
595 | fchmod(fileno(f), 0644); | |
596 | ||
597 | fputs("# Read and parsed by systemd-localed. It's probably wise not to edit this file\n" | |
598 | "# manually too freely.\n" | |
599 | "Section \"InputClass\"\n" | |
600 | " Identifier \"system-keyboard\"\n" | |
601 | " MatchIsKeyboard \"on\"\n", f); | |
602 | ||
603 | if (!isempty(x11_layout)) | |
604 | fprintf(f, " Option \"XkbLayout\" \"%s\"\n", x11_layout); | |
605 | ||
606 | if (!isempty(x11_model)) | |
607 | fprintf(f, " Option \"XkbModel\" \"%s\"\n", x11_model); | |
608 | ||
609 | if (!isempty(x11_variant)) | |
610 | fprintf(f, " Option \"XkbVariant\" \"%s\"\n", x11_variant); | |
611 | ||
612 | if (!isempty(x11_options)) | |
613 | fprintf(f, " Option \"XkbOptions\" \"%s\"\n", x11_options); | |
614 | ||
615 | fputs("EndSection\n", f); | |
616 | fflush(f); | |
617 | ||
618 | if (ferror(f) || rename(temp_path, "/etc/X11/xorg.conf.d/00-keyboard.conf") < 0) { | |
619 | r = -errno; | |
620 | unlink("/etc/X11/xorg.conf.d/00-keyboard.conf"); | |
621 | unlink(temp_path); | |
622 | } else { | |
623 | ||
624 | #ifdef TARGET_FEDORA | |
625 | unlink("/etc/X11/xorg.conf.d/00-system-setup-keyboard.conf"); | |
626 | ||
627 | /* Symlink this to /dev/null, so that s-s-k (if it is | |
628 | * still running) doesn't recreate this. */ | |
629 | symlink("/dev/null", "/etc/X11/xorg.conf.d/00-system-setup-keyboard.conf"); | |
630 | #endif | |
631 | ||
632 | r = 0; | |
633 | } | |
634 | ||
635 | fclose(f); | |
636 | free(temp_path); | |
637 | ||
638 | return r; | |
639 | } | |
640 | ||
641 | static int load_vconsole_keymap(DBusConnection *bus, DBusError *error) { | |
642 | DBusMessage *m = NULL, *reply = NULL; | |
643 | const char *name = "systemd-vconsole-setup.service", *mode = "replace"; | |
644 | int r; | |
645 | DBusError _error; | |
646 | ||
647 | assert(bus); | |
648 | ||
649 | if (!error) { | |
650 | dbus_error_init(&_error); | |
651 | error = &_error; | |
652 | } | |
653 | ||
654 | m = dbus_message_new_method_call( | |
655 | "org.freedesktop.systemd1", | |
656 | "/org/freedesktop/systemd1", | |
657 | "org.freedesktop.systemd1.Manager", | |
658 | "RestartUnit"); | |
659 | if (!m) { | |
660 | log_error("Could not allocate message."); | |
661 | r = -ENOMEM; | |
662 | goto finish; | |
663 | } | |
664 | ||
665 | if (!dbus_message_append_args(m, | |
666 | DBUS_TYPE_STRING, &name, | |
667 | DBUS_TYPE_STRING, &mode, | |
668 | DBUS_TYPE_INVALID)) { | |
669 | log_error("Could not append arguments to message."); | |
670 | r = -ENOMEM; | |
671 | goto finish; | |
672 | } | |
673 | ||
674 | reply = dbus_connection_send_with_reply_and_block(bus, m, -1, error); | |
675 | if (!reply) { | |
676 | log_error("Failed to issue method call: %s", bus_error_message(error)); | |
677 | r = -EIO; | |
678 | goto finish; | |
679 | } | |
680 | ||
681 | r = 0; | |
682 | ||
683 | finish: | |
684 | if (m) | |
685 | dbus_message_unref(m); | |
686 | ||
687 | if (reply) | |
688 | dbus_message_unref(reply); | |
689 | ||
690 | if (error == &_error) | |
691 | dbus_error_free(error); | |
692 | ||
693 | return r; | |
694 | } | |
695 | ||
696 | static char *strnulldash(const char *s) { | |
697 | return s == NULL || *s == 0 || (s[0] == '-' && s[1] == 0) ? NULL : (char*) s; | |
698 | } | |
699 | ||
700 | static int read_next_mapping(FILE *f, unsigned *n, char ***a) { | |
701 | assert(f); | |
702 | assert(n); | |
703 | assert(a); | |
704 | ||
705 | for (;;) { | |
706 | char line[LINE_MAX]; | |
707 | char *l, **b; | |
708 | ||
709 | errno = 0; | |
710 | if (!fgets(line, sizeof(line), f)) { | |
711 | ||
712 | if (ferror(f)) | |
713 | return errno ? -errno : -EIO; | |
714 | ||
715 | return 0; | |
716 | } | |
717 | ||
718 | (*n) ++; | |
719 | ||
720 | l = strstrip(line); | |
721 | if (l[0] == 0 || l[0] == '#') | |
722 | continue; | |
723 | ||
724 | b = strv_split_quoted(l); | |
725 | if (!b) | |
726 | return -ENOMEM; | |
727 | ||
728 | if (strv_length(b) < 5) { | |
729 | log_error("Invalid line "SYSTEMD_KBD_MODEL_MAP":%u, ignoring.", *n); | |
730 | strv_free(b); | |
731 | continue; | |
732 | ||
733 | } | |
734 | ||
735 | *a = b; | |
736 | return 1; | |
737 | } | |
738 | } | |
739 | ||
740 | static int convert_vconsole_to_x11(DBusConnection *connection) { | |
741 | bool modified = false; | |
742 | ||
743 | assert(connection); | |
744 | ||
745 | if (isempty(vc_keymap)) { | |
746 | ||
747 | modified = | |
748 | !isempty(x11_layout) || | |
749 | !isempty(x11_model) || | |
750 | !isempty(x11_variant) || | |
751 | !isempty(x11_options); | |
752 | ||
753 | free_data_x11(); | |
754 | } else { | |
755 | FILE *f; | |
756 | unsigned n = 0; | |
757 | ||
758 | f = fopen(SYSTEMD_KBD_MODEL_MAP, "re"); | |
759 | if (!f) | |
760 | return -errno; | |
761 | ||
762 | for (;;) { | |
763 | char **a; | |
764 | int r; | |
765 | ||
766 | r = read_next_mapping(f, &n, &a); | |
767 | if (r < 0) { | |
768 | fclose(f); | |
769 | return r; | |
770 | } | |
771 | ||
772 | if (r == 0) | |
773 | break; | |
774 | ||
775 | if (!streq(vc_keymap, a[0])) { | |
776 | strv_free(a); | |
777 | continue; | |
778 | } | |
779 | ||
780 | if (!streq_ptr(x11_layout, strnulldash(a[1])) || | |
781 | !streq_ptr(x11_model, strnulldash(a[2])) || | |
782 | !streq_ptr(x11_variant, strnulldash(a[3])) || | |
783 | !streq_ptr(x11_options, strnulldash(a[4]))) { | |
784 | ||
785 | if (free_and_set(&x11_layout, strnulldash(a[1])) < 0 || | |
786 | free_and_set(&x11_model, strnulldash(a[2])) < 0 || | |
787 | free_and_set(&x11_variant, strnulldash(a[3])) < 0 || | |
788 | free_and_set(&x11_options, strnulldash(a[4])) < 0) { | |
789 | strv_free(a); | |
790 | fclose(f); | |
791 | return -ENOMEM; | |
792 | } | |
793 | ||
794 | modified = true; | |
795 | } | |
796 | ||
797 | strv_free(a); | |
798 | break; | |
799 | } | |
800 | ||
801 | fclose(f); | |
802 | } | |
803 | ||
804 | if (modified) { | |
805 | dbus_bool_t b; | |
806 | DBusMessage *changed; | |
807 | int r; | |
808 | ||
809 | r = write_data_x11(); | |
810 | if (r < 0) | |
811 | log_error("Failed to set X11 keyboard layout: %s", strerror(-r)); | |
812 | ||
813 | changed = bus_properties_changed_new( | |
814 | "/org/freedesktop/locale1", | |
815 | "org.freedesktop.locale1", | |
816 | "X11Layout\0" | |
817 | "X11Model\0" | |
818 | "X11Variant\0" | |
819 | "X11Options\0"); | |
820 | ||
821 | if (!changed) | |
822 | return -ENOMEM; | |
823 | ||
824 | b = dbus_connection_send(connection, changed, NULL); | |
825 | dbus_message_unref(changed); | |
826 | ||
827 | if (!b) | |
828 | return -ENOMEM; | |
829 | } | |
830 | ||
831 | return 0; | |
832 | } | |
833 | ||
834 | static int convert_x11_to_vconsole(DBusConnection *connection) { | |
835 | bool modified = false; | |
836 | ||
837 | assert(connection); | |
838 | ||
839 | if (isempty(x11_layout)) { | |
840 | ||
841 | modified = | |
842 | !isempty(vc_keymap) || | |
843 | !isempty(vc_keymap_toggle); | |
844 | ||
845 | free_data_x11(); | |
846 | } else { | |
847 | FILE *f; | |
848 | unsigned n = 0; | |
849 | unsigned best_matching = 0; | |
850 | char *new_keymap = NULL; | |
851 | ||
852 | f = fopen(SYSTEMD_KBD_MODEL_MAP, "re"); | |
853 | if (!f) | |
854 | return -errno; | |
855 | ||
856 | for (;;) { | |
857 | char **a; | |
858 | unsigned matching = 0; | |
859 | int r; | |
860 | ||
861 | r = read_next_mapping(f, &n, &a); | |
862 | if (r < 0) { | |
863 | fclose(f); | |
864 | return r; | |
865 | } | |
866 | ||
867 | if (r == 0) | |
868 | break; | |
869 | ||
870 | /* Determine how well matching this entry is */ | |
871 | if (streq_ptr(x11_layout, a[1])) | |
872 | /* If we got an exact match, this is best */ | |
873 | matching = 10; | |
874 | else { | |
875 | size_t x; | |
876 | ||
877 | x = strcspn(x11_layout, ","); | |
878 | ||
879 | /* We have multiple X layouts, look | |
880 | * for an entry that matches our key | |
881 | * with the everything but the first | |
882 | * layout stripped off. */ | |
883 | if (x > 0 && | |
884 | strlen(a[1]) == x && | |
885 | strncmp(x11_layout, a[1], x) == 0) | |
886 | matching = 5; | |
887 | else { | |
888 | size_t w; | |
889 | ||
890 | /* If that didn't work, strip | |
891 | * off the other layouts from | |
892 | * the entry, too */ | |
893 | ||
894 | w = strcspn(a[1], ","); | |
895 | ||
896 | if (x > 0 && x == w && | |
897 | memcmp(x11_layout, a[1], x) == 0) | |
898 | matching = 1; | |
899 | } | |
900 | } | |
901 | ||
902 | if (matching > 0 && | |
903 | streq_ptr(x11_model, a[2])) { | |
904 | matching++; | |
905 | ||
906 | if (streq_ptr(x11_variant, a[3])) { | |
907 | matching++; | |
908 | ||
909 | if (streq_ptr(x11_options, a[4])) | |
910 | matching++; | |
911 | } | |
912 | } | |
913 | ||
914 | /* The best matching entry so far, then let's | |
915 | * save that */ | |
916 | if (matching > best_matching) { | |
917 | best_matching = matching; | |
918 | ||
919 | free(new_keymap); | |
920 | new_keymap = strdup(a[0]); | |
921 | ||
922 | if (!new_keymap) { | |
923 | strv_free(a); | |
924 | fclose(f); | |
925 | return -ENOMEM; | |
926 | } | |
927 | } | |
928 | ||
929 | strv_free(a); | |
930 | } | |
931 | ||
932 | fclose(f); | |
933 | ||
934 | if (!streq_ptr(vc_keymap, new_keymap)) { | |
935 | free(vc_keymap); | |
936 | vc_keymap = new_keymap; | |
937 | ||
938 | free(vc_keymap_toggle); | |
939 | vc_keymap_toggle = NULL; | |
940 | ||
941 | modified = true; | |
942 | } else | |
943 | free(new_keymap); | |
944 | } | |
945 | ||
946 | if (modified) { | |
947 | dbus_bool_t b; | |
948 | DBusMessage *changed; | |
949 | int r; | |
950 | ||
951 | r = write_data_vconsole(); | |
952 | if (r < 0) | |
953 | log_error("Failed to set virtual console keymap: %s", strerror(-r)); | |
954 | ||
955 | changed = bus_properties_changed_new( | |
956 | "/org/freedesktop/locale1", | |
957 | "org.freedesktop.locale1", | |
958 | "VConsoleKeymap\0" | |
959 | "VConsoleKeymapToggle\0"); | |
960 | ||
961 | if (!changed) | |
962 | return -ENOMEM; | |
963 | ||
964 | b = dbus_connection_send(connection, changed, NULL); | |
965 | dbus_message_unref(changed); | |
966 | ||
967 | if (!b) | |
968 | return -ENOMEM; | |
969 | ||
970 | return load_vconsole_keymap(connection, NULL); | |
971 | } | |
972 | ||
973 | return 0; | |
974 | } | |
975 | ||
976 | static int append_locale(DBusMessageIter *i, const char *property, void *userdata) { | |
977 | int r, c = 0, p; | |
978 | char **l; | |
979 | ||
980 | l = new0(char*, _PROP_MAX+1); | |
981 | if (!l) | |
982 | return -ENOMEM; | |
983 | ||
984 | for (p = 0; p < _PROP_MAX; p++) { | |
985 | char *t; | |
986 | ||
987 | if (isempty(data[p])) | |
988 | continue; | |
989 | ||
990 | if (asprintf(&t, "%s=%s", names[p], data[p]) < 0) { | |
991 | strv_free(l); | |
992 | return -ENOMEM; | |
993 | } | |
994 | ||
995 | l[c++] = t; | |
996 | } | |
997 | ||
998 | r = bus_property_append_strv(i, property, (void*) l); | |
999 | strv_free(l); | |
1000 | ||
1001 | return r; | |
1002 | } | |
1003 | ||
1004 | static DBusHandlerResult locale_message_handler( | |
1005 | DBusConnection *connection, | |
1006 | DBusMessage *message, | |
1007 | void *userdata) { | |
1008 | ||
1009 | const BusProperty properties[] = { | |
1010 | { "org.freedesktop.locale1", "Locale", append_locale, "as", NULL }, | |
1011 | { "org.freedesktop.locale1", "X11Layout", bus_property_append_string, "s", x11_layout }, | |
1012 | { "org.freedesktop.locale1", "X11Model", bus_property_append_string, "s", x11_model }, | |
1013 | { "org.freedesktop.locale1", "X11Variant", bus_property_append_string, "s", x11_variant }, | |
1014 | { "org.freedesktop.locale1", "X11Options", bus_property_append_string, "s", x11_options }, | |
1015 | { "org.freedesktop.locale1", "VConsoleKeymap", bus_property_append_string, "s", vc_keymap }, | |
1016 | { "org.freedesktop.locale1", "VConsoleKeymapToggle", bus_property_append_string, "s", vc_keymap_toggle }, | |
1017 | { NULL, NULL, NULL, NULL, NULL } | |
1018 | }; | |
1019 | ||
1020 | DBusMessage *reply = NULL, *changed = NULL; | |
1021 | DBusError error; | |
1022 | int r; | |
1023 | ||
1024 | assert(connection); | |
1025 | assert(message); | |
1026 | ||
1027 | dbus_error_init(&error); | |
1028 | ||
1029 | if (dbus_message_is_method_call(message, "org.freedesktop.locale1", "SetLocale")) { | |
1030 | char **l = NULL, **i; | |
1031 | dbus_bool_t interactive; | |
1032 | DBusMessageIter iter; | |
1033 | bool modified = false; | |
1034 | bool passed[_PROP_MAX]; | |
1035 | int p; | |
1036 | ||
1037 | if (!dbus_message_iter_init(message, &iter)) | |
1038 | return bus_send_error_reply(connection, message, NULL, -EINVAL); | |
1039 | ||
1040 | r = bus_parse_strv_iter(&iter, &l); | |
1041 | if (r < 0) { | |
1042 | if (r == -ENOMEM) | |
1043 | goto oom; | |
1044 | ||
1045 | return bus_send_error_reply(connection, message, NULL, r); | |
1046 | } | |
1047 | ||
1048 | if (!dbus_message_iter_next(&iter) || | |
1049 | dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_BOOLEAN) { | |
1050 | strv_free(l); | |
1051 | return bus_send_error_reply(connection, message, NULL, -EINVAL); | |
1052 | } | |
1053 | ||
1054 | dbus_message_iter_get_basic(&iter, &interactive); | |
1055 | ||
1056 | zero(passed); | |
1057 | ||
1058 | /* Check whether a variable changed and if so valid */ | |
1059 | STRV_FOREACH(i, l) { | |
1060 | bool valid = false; | |
1061 | ||
1062 | for (p = 0; p < _PROP_MAX; p++) { | |
1063 | size_t k; | |
1064 | ||
1065 | k = strlen(names[p]); | |
1066 | if (startswith(*i, names[p]) && (*i)[k] == '=') { | |
1067 | valid = true; | |
1068 | passed[p] = true; | |
1069 | ||
1070 | if (!streq_ptr(*i + k + 1, data[p])) | |
1071 | modified = true; | |
1072 | ||
1073 | break; | |
1074 | } | |
1075 | } | |
1076 | ||
1077 | if (!valid) { | |
1078 | strv_free(l); | |
1079 | return bus_send_error_reply(connection, message, NULL, -EINVAL); | |
1080 | } | |
1081 | } | |
1082 | ||
1083 | /* Check whether a variable is unset */ | |
1084 | if (!modified) { | |
1085 | for (p = 0; p < _PROP_MAX; p++) | |
1086 | if (!isempty(data[p]) && !passed[p]) { | |
1087 | modified = true; | |
1088 | break; | |
1089 | } | |
1090 | } | |
1091 | ||
1092 | if (modified) { | |
1093 | ||
1094 | r = verify_polkit(connection, message, "org.freedesktop.locale1.set-locale", interactive, &error); | |
1095 | if (r < 0) { | |
1096 | strv_free(l); | |
1097 | return bus_send_error_reply(connection, message, &error, r); | |
1098 | } | |
1099 | ||
1100 | STRV_FOREACH(i, l) { | |
1101 | for (p = 0; p < _PROP_MAX; p++) { | |
1102 | size_t k; | |
1103 | ||
1104 | k = strlen(names[p]); | |
1105 | if (startswith(*i, names[p]) && (*i)[k] == '=') { | |
1106 | char *t; | |
1107 | ||
1108 | t = strdup(*i + k + 1); | |
1109 | if (!t) { | |
1110 | strv_free(l); | |
1111 | goto oom; | |
1112 | } | |
1113 | ||
1114 | free(data[p]); | |
1115 | data[p] = t; | |
1116 | ||
1117 | break; | |
1118 | } | |
1119 | } | |
1120 | } | |
1121 | ||
1122 | strv_free(l); | |
1123 | ||
1124 | for (p = 0; p < _PROP_MAX; p++) { | |
1125 | if (passed[p]) | |
1126 | continue; | |
1127 | ||
1128 | free(data[p]); | |
1129 | data[p] = NULL; | |
1130 | } | |
1131 | ||
1132 | simplify(); | |
1133 | ||
1134 | r = write_data_locale(); | |
1135 | if (r < 0) { | |
1136 | log_error("Failed to set locale: %s", strerror(-r)); | |
1137 | return bus_send_error_reply(connection, message, NULL, r); | |
1138 | } | |
1139 | ||
1140 | push_data(connection); | |
1141 | ||
1142 | log_info("Changed locale information."); | |
1143 | ||
1144 | changed = bus_properties_changed_new( | |
1145 | "/org/freedesktop/locale1", | |
1146 | "org.freedesktop.locale1", | |
1147 | "Locale\0"); | |
1148 | if (!changed) | |
1149 | goto oom; | |
1150 | } | |
1151 | } else if (dbus_message_is_method_call(message, "org.freedesktop.locale1", "SetVConsoleKeyboard")) { | |
1152 | ||
1153 | const char *keymap, *keymap_toggle; | |
1154 | dbus_bool_t convert, interactive; | |
1155 | ||
1156 | if (!dbus_message_get_args( | |
1157 | message, | |
1158 | &error, | |
1159 | DBUS_TYPE_STRING, &keymap, | |
1160 | DBUS_TYPE_STRING, &keymap_toggle, | |
1161 | DBUS_TYPE_BOOLEAN, &convert, | |
1162 | DBUS_TYPE_BOOLEAN, &interactive, | |
1163 | DBUS_TYPE_INVALID)) | |
1164 | return bus_send_error_reply(connection, message, &error, -EINVAL); | |
1165 | ||
1166 | if (isempty(keymap)) | |
1167 | keymap = NULL; | |
1168 | ||
1169 | if (isempty(keymap_toggle)) | |
1170 | keymap_toggle = NULL; | |
1171 | ||
1172 | if (!streq_ptr(keymap, vc_keymap) || | |
1173 | !streq_ptr(keymap_toggle, vc_keymap_toggle)) { | |
1174 | ||
1175 | r = verify_polkit(connection, message, "org.freedesktop.locale1.set-keyboard", interactive, &error); | |
1176 | if (r < 0) | |
1177 | return bus_send_error_reply(connection, message, &error, r); | |
1178 | ||
1179 | if (free_and_set(&vc_keymap, keymap) < 0 || | |
1180 | free_and_set(&vc_keymap_toggle, keymap_toggle) < 0) | |
1181 | goto oom; | |
1182 | ||
1183 | r = write_data_vconsole(); | |
1184 | if (r < 0) { | |
1185 | log_error("Failed to set virtual console keymap: %s", strerror(-r)); | |
1186 | return bus_send_error_reply(connection, message, NULL, r); | |
1187 | } | |
1188 | ||
1189 | log_info("Changed virtual console keymap to '%s'", strempty(vc_keymap)); | |
1190 | ||
1191 | r = load_vconsole_keymap(connection, NULL); | |
1192 | if (r < 0) | |
1193 | log_error("Failed to request keymap reload: %s", strerror(-r)); | |
1194 | ||
1195 | changed = bus_properties_changed_new( | |
1196 | "/org/freedesktop/locale1", | |
1197 | "org.freedesktop.locale1", | |
1198 | "VConsoleKeymap\0" | |
1199 | "VConsoleKeymapToggle\0"); | |
1200 | if (!changed) | |
1201 | goto oom; | |
1202 | ||
1203 | if (convert) { | |
1204 | r = convert_vconsole_to_x11(connection); | |
1205 | ||
1206 | if (r < 0) | |
1207 | log_error("Failed to convert keymap data: %s", strerror(-r)); | |
1208 | } | |
1209 | } | |
1210 | ||
1211 | } else if (dbus_message_is_method_call(message, "org.freedesktop.locale1", "SetX11Keyboard")) { | |
1212 | ||
1213 | const char *layout, *model, *variant, *options; | |
1214 | dbus_bool_t convert, interactive; | |
1215 | ||
1216 | if (!dbus_message_get_args( | |
1217 | message, | |
1218 | &error, | |
1219 | DBUS_TYPE_STRING, &layout, | |
1220 | DBUS_TYPE_STRING, &model, | |
1221 | DBUS_TYPE_STRING, &variant, | |
1222 | DBUS_TYPE_STRING, &options, | |
1223 | DBUS_TYPE_BOOLEAN, &convert, | |
1224 | DBUS_TYPE_BOOLEAN, &interactive, | |
1225 | DBUS_TYPE_INVALID)) | |
1226 | return bus_send_error_reply(connection, message, &error, -EINVAL); | |
1227 | ||
1228 | if (isempty(layout)) | |
1229 | layout = NULL; | |
1230 | ||
1231 | if (isempty(model)) | |
1232 | model = NULL; | |
1233 | ||
1234 | if (isempty(variant)) | |
1235 | variant = NULL; | |
1236 | ||
1237 | if (isempty(options)) | |
1238 | options = NULL; | |
1239 | ||
1240 | if (!streq_ptr(layout, x11_layout) || | |
1241 | !streq_ptr(model, x11_model) || | |
1242 | !streq_ptr(variant, x11_variant) || | |
1243 | !streq_ptr(options, x11_options)) { | |
1244 | ||
1245 | r = verify_polkit(connection, message, "org.freedesktop.locale1.set-keyboard", interactive, &error); | |
1246 | if (r < 0) | |
1247 | return bus_send_error_reply(connection, message, &error, r); | |
1248 | ||
1249 | if (free_and_set(&x11_layout, layout) < 0 || | |
1250 | free_and_set(&x11_model, model) < 0 || | |
1251 | free_and_set(&x11_variant, variant) < 0 || | |
1252 | free_and_set(&x11_options, options) < 0) | |
1253 | goto oom; | |
1254 | ||
1255 | r = write_data_x11(); | |
1256 | if (r < 0) { | |
1257 | log_error("Failed to set X11 keyboard layout: %s", strerror(-r)); | |
1258 | return bus_send_error_reply(connection, message, NULL, r); | |
1259 | } | |
1260 | ||
1261 | log_info("Changed X11 keyboard layout to '%s'", strempty(x11_layout)); | |
1262 | ||
1263 | changed = bus_properties_changed_new( | |
1264 | "/org/freedesktop/locale1", | |
1265 | "org.freedesktop.locale1", | |
1266 | "X11Layout\0" | |
1267 | "X11Model\0" | |
1268 | "X11Variant\0" | |
1269 | "X11Options\0"); | |
1270 | if (!changed) | |
1271 | goto oom; | |
1272 | ||
1273 | if (convert) { | |
1274 | r = convert_x11_to_vconsole(connection); | |
1275 | ||
1276 | if (r < 0) | |
1277 | log_error("Failed to convert keymap data: %s", strerror(-r)); | |
1278 | } | |
1279 | } | |
1280 | } else | |
1281 | return bus_default_message_handler(connection, message, INTROSPECTION, INTERFACES_LIST, properties); | |
1282 | ||
1283 | ||
1284 | if (!(reply = dbus_message_new_method_return(message))) | |
1285 | goto oom; | |
1286 | ||
1287 | if (!dbus_connection_send(connection, reply, NULL)) | |
1288 | goto oom; | |
1289 | ||
1290 | dbus_message_unref(reply); | |
1291 | reply = NULL; | |
1292 | ||
1293 | if (changed) { | |
1294 | ||
1295 | if (!dbus_connection_send(connection, changed, NULL)) | |
1296 | goto oom; | |
1297 | ||
1298 | dbus_message_unref(changed); | |
1299 | } | |
1300 | ||
1301 | return DBUS_HANDLER_RESULT_HANDLED; | |
1302 | ||
1303 | oom: | |
1304 | if (reply) | |
1305 | dbus_message_unref(reply); | |
1306 | ||
1307 | if (changed) | |
1308 | dbus_message_unref(changed); | |
1309 | ||
1310 | dbus_error_free(&error); | |
1311 | ||
1312 | return DBUS_HANDLER_RESULT_NEED_MEMORY; | |
1313 | } | |
1314 | ||
1315 | static int connect_bus(DBusConnection **_bus) { | |
1316 | static const DBusObjectPathVTable locale_vtable = { | |
1317 | .message_function = locale_message_handler | |
1318 | }; | |
1319 | DBusError error; | |
1320 | DBusConnection *bus = NULL; | |
1321 | int r; | |
1322 | ||
1323 | assert(_bus); | |
1324 | ||
1325 | dbus_error_init(&error); | |
1326 | ||
1327 | bus = dbus_bus_get_private(DBUS_BUS_SYSTEM, &error); | |
1328 | if (!bus) { | |
1329 | log_error("Failed to get system D-Bus connection: %s", bus_error_message(&error)); | |
1330 | r = -ECONNREFUSED; | |
1331 | goto fail; | |
1332 | } | |
1333 | ||
1334 | dbus_connection_set_exit_on_disconnect(bus, FALSE); | |
1335 | ||
1336 | if (!dbus_connection_register_object_path(bus, "/org/freedesktop/locale1", &locale_vtable, NULL) || | |
1337 | !dbus_connection_add_filter(bus, bus_exit_idle_filter, &remain_until, NULL)) { | |
1338 | log_error("Not enough memory"); | |
1339 | r = -ENOMEM; | |
1340 | goto fail; | |
1341 | } | |
1342 | ||
1343 | r = dbus_bus_request_name(bus, "org.freedesktop.locale1", DBUS_NAME_FLAG_DO_NOT_QUEUE, &error); | |
1344 | if (dbus_error_is_set(&error)) { | |
1345 | log_error("Failed to register name on bus: %s", bus_error_message(&error)); | |
1346 | r = -EEXIST; | |
1347 | goto fail; | |
1348 | } | |
1349 | ||
1350 | if (r != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) { | |
1351 | log_error("Failed to acquire name."); | |
1352 | r = -EEXIST; | |
1353 | goto fail; | |
1354 | } | |
1355 | ||
1356 | if (_bus) | |
1357 | *_bus = bus; | |
1358 | ||
1359 | return 0; | |
1360 | ||
1361 | fail: | |
1362 | dbus_connection_close(bus); | |
1363 | dbus_connection_unref(bus); | |
1364 | ||
1365 | dbus_error_free(&error); | |
1366 | ||
1367 | return r; | |
1368 | } | |
1369 | ||
1370 | int main(int argc, char *argv[]) { | |
1371 | int r; | |
1372 | DBusConnection *bus = NULL; | |
1373 | bool exiting = false; | |
1374 | ||
1375 | log_set_target(LOG_TARGET_AUTO); | |
1376 | log_parse_environment(); | |
1377 | log_open(); | |
1378 | ||
1379 | umask(0022); | |
1380 | ||
1381 | if (argc == 2 && streq(argv[1], "--introspect")) { | |
1382 | fputs(DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE | |
1383 | "<node>\n", stdout); | |
1384 | fputs(locale_interface, stdout); | |
1385 | fputs("</node>\n", stdout); | |
1386 | return 0; | |
1387 | } | |
1388 | ||
1389 | if (argc != 1) { | |
1390 | log_error("This program takes no arguments."); | |
1391 | r = -EINVAL; | |
1392 | goto finish; | |
1393 | } | |
1394 | ||
1395 | r = read_data(); | |
1396 | if (r < 0) { | |
1397 | log_error("Failed to read locale data: %s", strerror(-r)); | |
1398 | goto finish; | |
1399 | } | |
1400 | ||
1401 | r = connect_bus(&bus); | |
1402 | if (r < 0) | |
1403 | goto finish; | |
1404 | ||
1405 | remain_until = now(CLOCK_MONOTONIC) + DEFAULT_EXIT_USEC; | |
1406 | for (;;) { | |
1407 | ||
1408 | if (!dbus_connection_read_write_dispatch(bus, exiting ? -1 : (int) (DEFAULT_EXIT_USEC/USEC_PER_MSEC))) | |
1409 | break; | |
1410 | ||
1411 | if (!exiting && remain_until < now(CLOCK_MONOTONIC)) { | |
1412 | exiting = true; | |
1413 | bus_async_unregister_and_exit(bus, "org.freedesktop.locale1"); | |
1414 | } | |
1415 | } | |
1416 | ||
1417 | r = 0; | |
1418 | ||
1419 | finish: | |
1420 | free_data(); | |
1421 | ||
1422 | if (bus) { | |
1423 | dbus_connection_flush(bus); | |
1424 | dbus_connection_close(bus); | |
1425 | dbus_connection_unref(bus); | |
1426 | } | |
1427 | ||
1428 | return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; | |
1429 | } |