chiark / gitweb /
main: fix reexec
[elogind.git] / src / localed.c
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
33 #define INTERFACE                                                       \
34         " <interface name=\"org.freedesktop.locale1\">\n"               \
35         "  <property name=\"Locale\" type=\"as\" access=\"read\"/>\n"   \
36         "  <method name=\"SetLocale\">\n"                               \
37         "   <arg name=\"locale\" type=\"as\" direction=\"in\"/>\n"      \
38         "   <arg name=\"user_interaction\" type=\"b\" direction=\"in\"/>\n" \
39         "  </method>\n"                                                 \
40         " </interface>\n"
41
42 #define INTROSPECTION                                                   \
43         DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE                       \
44         "<node>\n"                                                      \
45         INTERFACE                                                       \
46         BUS_PROPERTIES_INTERFACE                                        \
47         BUS_INTROSPECTABLE_INTERFACE                                    \
48         BUS_PEER_INTERFACE                                              \
49         "</node>\n"
50
51 #define INTERFACES_LIST                         \
52         BUS_GENERIC_INTERFACES_LIST             \
53         "org.freedesktop.locale1\0"
54
55 const char locale_interface[] _introspect_("locale1") = INTERFACE;
56
57 enum {
58         /* We don't list LC_ALL here on purpose. People should be
59          * using LANG instead. */
60
61         PROP_LANG,
62         PROP_LC_CTYPE,
63         PROP_LC_NUMERIC,
64         PROP_LC_TIME,
65         PROP_LC_COLLATE,
66         PROP_LC_MONETARY,
67         PROP_LC_MESSAGES,
68         PROP_LC_PAPER,
69         PROP_LC_NAME,
70         PROP_LC_ADDRESS,
71         PROP_LC_TELEPHONE,
72         PROP_LC_MEASUREMENT,
73         PROP_LC_IDENTIFICATION,
74         _PROP_MAX
75 };
76
77 static const char * const names[_PROP_MAX] = {
78         [PROP_LANG] = "LANG",
79         [PROP_LC_CTYPE] = "LC_CTYPE",
80         [PROP_LC_NUMERIC] = "LC_NUMERIC",
81         [PROP_LC_TIME] = "LC_TIME",
82         [PROP_LC_COLLATE] = "LC_COLLATE",
83         [PROP_LC_MONETARY] = "LC_MONETARY",
84         [PROP_LC_MESSAGES] = "LC_MESSAGES",
85         [PROP_LC_PAPER] = "LC_PAPER",
86         [PROP_LC_NAME] = "LC_NAME",
87         [PROP_LC_ADDRESS] = "LC_ADDRESS",
88         [PROP_LC_TELEPHONE] = "LC_TELEPHONE",
89         [PROP_LC_MEASUREMENT] = "LC_MEASUREMENT",
90         [PROP_LC_IDENTIFICATION] = "LC_IDENTIFICATION"
91 };
92
93 static char *data[_PROP_MAX] = {
94         NULL,
95         NULL,
96         NULL,
97         NULL,
98         NULL,
99         NULL,
100         NULL,
101         NULL,
102         NULL,
103         NULL,
104         NULL,
105         NULL,
106         NULL
107 };
108
109 static void free_data(void) {
110         int p;
111
112         for (p = 0; p < _PROP_MAX; p++) {
113                 free(data[p]);
114                 data[p] = NULL;
115         }
116 }
117
118 static void simplify(void) {
119         int p;
120
121         for (p = 1; p < _PROP_MAX; p++)
122                 if (isempty(data[p]) || streq_ptr(data[PROP_LANG], data[p])) {
123                         free(data[p]);
124                         data[p] = NULL;
125                 }
126 }
127
128 static int read_data(void) {
129         int r;
130
131         free_data();
132
133         r = parse_env_file("/etc/locale.conf", NEWLINE,
134                            "LANG",              &data[PROP_LANG],
135                            "LC_CTYPE",          &data[PROP_LC_CTYPE],
136                            "LC_NUMERIC",        &data[PROP_LC_NUMERIC],
137                            "LC_TIME",           &data[PROP_LC_TIME],
138                            "LC_COLLATE",        &data[PROP_LC_COLLATE],
139                            "LC_MONETARY",       &data[PROP_LC_MONETARY],
140                            "LC_MESSAGES",       &data[PROP_LC_MESSAGES],
141                            "LC_PAPER",          &data[PROP_LC_PAPER],
142                            "LC_NAME",           &data[PROP_LC_NAME],
143                            "LC_ADDRESS",        &data[PROP_LC_ADDRESS],
144                            "LC_TELEPHONE",      &data[PROP_LC_TELEPHONE],
145                            "LC_MEASUREMENT",    &data[PROP_LC_MEASUREMENT],
146                            "LC_IDENTIFICATION", &data[PROP_LC_IDENTIFICATION],
147                            NULL);
148
149         if (r == -ENOENT) {
150                 int p;
151
152                 /* Fill in what we got passed from systemd. */
153
154                 for (p = 0; p < _PROP_MAX; p++) {
155                         char *e, *d;
156
157                         assert(names[p]);
158
159                         e = getenv(names[p]);
160                         if (e) {
161                                 d = strdup(e);
162                                 if (!d)
163                                         return -ENOMEM;
164                         } else
165                                 d = NULL;
166
167                         free(data[p]);
168                         data[p] = d;
169                 }
170
171                 r = 0;
172         }
173
174         simplify();
175         return r;
176 }
177
178 static int write_data(void) {
179         int r, p;
180         char **l = NULL;
181
182         r = load_env_file("/etc/locale.conf", &l);
183         if (r < 0 && r != -ENOENT)
184                 return r;
185
186         for (p = 0; p < _PROP_MAX; p++) {
187                 char *t, **u;
188
189                 assert(names[p]);
190
191                 if (isempty(data[p])) {
192                         l = strv_env_unset(l, names[p]);
193                         continue;
194                 }
195
196                 if (asprintf(&t, "%s=%s", names[p], data[p]) < 0) {
197                         strv_free(l);
198                         return -ENOMEM;
199                 }
200
201                 u = strv_env_set(l, t);
202                 free(t);
203                 strv_free(l);
204
205                 if (!u)
206                         return -ENOMEM;
207
208                 l = u;
209         }
210
211         if (strv_isempty(l)) {
212
213                 if (unlink("/etc/locale.conf") < 0)
214                         return errno == ENOENT ? 0 : -errno;
215
216                 return 0;
217         }
218
219         r = write_env_file("/etc/locale.conf", l);
220         strv_free(l);
221
222         return r;
223 }
224
225 static void push_data(DBusConnection *bus) {
226         char **l_set = NULL, **l_unset = NULL, **t;
227         int c_set = 0, c_unset = 0, p;
228         DBusError error;
229         DBusMessage *m = NULL, *reply = NULL;
230         DBusMessageIter iter, sub;
231
232         dbus_error_init(&error);
233
234         assert(bus);
235
236         l_set = new0(char*, _PROP_MAX);
237         l_unset = new0(char*, _PROP_MAX);
238         if (!l_set || !l_unset) {
239                 log_error("Out of memory");
240                 goto finish;
241         }
242
243         for (p = 0; p < _PROP_MAX; p++) {
244                 assert(names[p]);
245
246                 if (isempty(data[p]))
247                         l_unset[c_set++] = (char*) names[p];
248                 else {
249                         char *s;
250
251                         if (asprintf(&s, "%s=%s", names[p], data[p]) < 0) {
252                                 log_error("Out of memory");
253                                 goto finish;
254                         }
255
256                         l_set[c_unset++] = s;
257                 }
258         }
259
260         assert(c_set + c_unset == _PROP_MAX);
261         m = dbus_message_new_method_call("org.freedesktop.systemd1", "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "UnsetAndSetEnvironment");
262         if (!m) {
263                 log_error("Could not allocate message.");
264                 goto finish;
265         }
266
267         dbus_message_iter_init_append(m, &iter);
268
269         if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub)) {
270                 log_error("Out of memory.");
271                 goto finish;
272         }
273
274         STRV_FOREACH(t, l_unset)
275                 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, t)) {
276                         log_error("Out of memory.");
277                         goto finish;
278                 }
279
280         if (!dbus_message_iter_close_container(&iter, &sub) ||
281             !dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub)) {
282                 log_error("Out of memory.");
283                 goto finish;
284         }
285
286         STRV_FOREACH(t, l_set)
287                 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, t)) {
288                         log_error("Out of memory.");
289                         goto finish;
290                 }
291
292         if (!dbus_message_iter_close_container(&iter, &sub)) {
293                 log_error("Out of memory.");
294                 goto finish;
295         }
296
297         reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error);
298         if (!reply) {
299                 log_error("Failed to set locale information: %s", bus_error_message(&error));
300                 goto finish;
301         }
302
303 finish:
304         if (m)
305                 dbus_message_unref(m);
306
307         if (reply)
308                 dbus_message_unref(reply);
309
310         dbus_error_free(&error);
311
312         strv_free(l_set);
313         free(l_unset);
314 }
315
316 static int append_locale(DBusMessageIter *i, const char *property, void *userdata) {
317         int r, c = 0, p;
318         char **l;
319
320         l = new0(char*, _PROP_MAX+1);
321         if (!l)
322                 return -ENOMEM;
323
324         for (p = 0; p < _PROP_MAX; p++) {
325                 char *t;
326
327                 if (isempty(data[p]))
328                         continue;
329
330                 if (asprintf(&t, "%s=%s", names[p], data[p]) < 0) {
331                         strv_free(l);
332                         return -ENOMEM;
333                 }
334
335                 l[c++] = t;
336         }
337
338         r = bus_property_append_strv(i, property, (void*) l);
339         strv_free(l);
340
341         return r;
342 }
343
344 static DBusHandlerResult locale_message_handler(
345                 DBusConnection *connection,
346                 DBusMessage *message,
347                 void *userdata) {
348
349         const BusProperty properties[] = {
350                 { "org.freedesktop.locale1", "Locale", append_locale, "as", NULL},
351                 { NULL, NULL, NULL, NULL, NULL }
352         };
353
354         DBusMessage *reply = NULL, *changed = NULL;
355         DBusError error;
356         int r;
357
358         assert(connection);
359         assert(message);
360
361         dbus_error_init(&error);
362
363         if (dbus_message_is_method_call(message, "org.freedesktop.locale1", "SetLocale")) {
364                 char **l = NULL, **i;
365                 dbus_bool_t interactive;
366                 DBusMessageIter iter;
367                 bool modified = false;
368                 bool passed[_PROP_MAX];
369                 int p;
370
371                 if (!dbus_message_iter_init(message, &iter))
372                         return bus_send_error_reply(connection, message, NULL, -EINVAL);
373
374                 r = bus_parse_strv_iter(&iter, &l);
375                 if (r < 0) {
376                         if (r == -ENOMEM)
377                                 goto oom;
378
379                         return bus_send_error_reply(connection, message, NULL, r);
380                 }
381
382                 if (!dbus_message_iter_next(&iter) ||
383                     dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_BOOLEAN)  {
384                         strv_free(l);
385                         return bus_send_error_reply(connection, message, NULL, -EINVAL);
386                 }
387
388                 dbus_message_iter_get_basic(&iter, &interactive);
389
390                 zero(passed);
391
392                 /* Check whether a variable changed and if so valid */
393                 STRV_FOREACH(i, l) {
394                         bool valid = false;
395
396                         for (p = 0; p < _PROP_MAX; p++) {
397                                 size_t k;
398
399                                 k = strlen(names[p]);
400                                 if (startswith(*i, names[p]) && (*i)[k] == '=') {
401                                         valid = true;
402                                         passed[p] = true;
403
404                                         if (!streq_ptr(*i + k + 1, data[p]))
405                                                 modified = true;
406
407                                         break;
408                                 }
409                         }
410
411                         if (!valid) {
412                                 strv_free(l);
413                                 return bus_send_error_reply(connection, message, NULL, -EINVAL);
414                         }
415                 }
416
417                 /* Check whether a variable is unset */
418                 if (!modified)  {
419                         for (p = 0; p < _PROP_MAX; p++)
420                                 if (!isempty(data[p]) && !passed[p]) {
421                                         modified = true;
422                                         break;
423                                 }
424                 }
425
426                 if (modified) {
427
428                         r = verify_polkit(connection, message, "org.freedesktop.locale1.set-locale", interactive, &error);
429                         if (r < 0) {
430                                 strv_free(l);
431                                 return bus_send_error_reply(connection, message, &error, r);
432                         }
433
434                         STRV_FOREACH(i, l) {
435                                 for (p = 0; p < _PROP_MAX; p++) {
436                                         size_t k;
437
438                                         k = strlen(names[p]);
439                                         if (startswith(*i, names[p]) && (*i)[k] == '=') {
440                                                 char *t;
441
442                                                 t = strdup(*i + k + 1);
443                                                 if (!t) {
444                                                         strv_free(l);
445                                                         goto oom;
446                                                 }
447
448                                                 free(data[p]);
449                                                 data[p] = t;
450
451                                                 break;
452                                         }
453                                 }
454                         }
455
456                         for (p = 0; p < _PROP_MAX; p++) {
457                                 if (passed[p])
458                                         continue;
459
460                                 free(data[p]);
461                                 data[p] = NULL;
462                         }
463
464                         simplify();
465
466                         r = write_data();
467                         if (r < 0) {
468                                 log_error("Failed to set locale: %s", strerror(-r));
469                                 return bus_send_error_reply(connection, message, NULL, r);
470                         }
471
472                         push_data(connection);
473
474                         log_info("Changed locale information.");
475
476                         changed = bus_properties_changed_new(
477                                         "/org/freedesktop/locale1",
478                                         "org.freedesktop.locale1",
479                                         "Locale\0");
480                         if (!changed)
481                                 goto oom;
482                 }
483
484         } else
485                 return bus_default_message_handler(connection, message, INTROSPECTION, INTERFACES_LIST, properties);
486
487         if (!(reply = dbus_message_new_method_return(message)))
488                 goto oom;
489
490         if (!dbus_connection_send(connection, reply, NULL))
491                 goto oom;
492
493         dbus_message_unref(reply);
494         reply = NULL;
495
496         if (changed) {
497
498                 if (!dbus_connection_send(connection, changed, NULL))
499                         goto oom;
500
501                 dbus_message_unref(changed);
502         }
503
504         return DBUS_HANDLER_RESULT_HANDLED;
505
506 oom:
507         if (reply)
508                 dbus_message_unref(reply);
509
510         if (changed)
511                 dbus_message_unref(changed);
512
513         dbus_error_free(&error);
514
515         return DBUS_HANDLER_RESULT_NEED_MEMORY;
516 }
517
518 static int connect_bus(DBusConnection **_bus) {
519         static const DBusObjectPathVTable locale_vtable = {
520                 .message_function = locale_message_handler
521         };
522         DBusError error;
523         DBusConnection *bus = NULL;
524         int r;
525
526         assert(_bus);
527
528         dbus_error_init(&error);
529
530         bus = dbus_bus_get_private(DBUS_BUS_SYSTEM, &error);
531         if (!bus) {
532                 log_error("Failed to get system D-Bus connection: %s", bus_error_message(&error));
533                 r = -ECONNREFUSED;
534                 goto fail;
535         }
536
537         if (!dbus_connection_register_object_path(bus, "/org/freedesktop/locale1", &locale_vtable, NULL)) {
538                 log_error("Not enough memory");
539                 r = -ENOMEM;
540                 goto fail;
541         }
542
543         r = dbus_bus_request_name(bus, "org.freedesktop.locale1", DBUS_NAME_FLAG_DO_NOT_QUEUE, &error);
544         if (dbus_error_is_set(&error)) {
545                 log_error("Failed to register name on bus: %s", bus_error_message(&error));
546                 r = -EEXIST;
547                 goto fail;
548         }
549
550         if (r != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) {
551                 log_error("Failed to acquire name.");
552                 r = -EEXIST;
553                 goto fail;
554         }
555
556         if (_bus)
557                 *_bus = bus;
558
559         return 0;
560
561 fail:
562         dbus_connection_close(bus);
563         dbus_connection_unref(bus);
564
565         dbus_error_free(&error);
566
567         return r;
568 }
569
570 int main(int argc, char *argv[]) {
571         int r;
572         DBusConnection *bus = NULL;
573
574         log_set_target(LOG_TARGET_AUTO);
575         log_parse_environment();
576         log_open();
577
578         umask(0022);
579
580         if (argc == 2 && streq(argv[1], "--introspect")) {
581                 fputs(DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE
582                       "<node>\n", stdout);
583                 fputs(locale_interface, stdout);
584                 fputs("</node>\n", stdout);
585                 return 0;
586         }
587
588         if (argc != 1) {
589                 log_error("This program takes no arguments.");
590                 r = -EINVAL;
591                 goto finish;
592         }
593
594         r = read_data();
595         if (r < 0) {
596                 log_error("Failed to read locale data: %s", strerror(-r));
597                 goto finish;
598         }
599
600         r = connect_bus(&bus);
601         if (r < 0)
602                 goto finish;
603
604         while (dbus_connection_read_write_dispatch(bus, -1))
605                 ;
606
607         r = 0;
608
609 finish:
610         free_data();
611
612         if (bus) {
613                 dbus_connection_flush(bus);
614                 dbus_connection_close(bus);
615                 dbus_connection_unref(bus);
616         }
617
618         return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
619 }