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