chiark / gitweb /
systemadm: use colors for id too, remove color from fragment link
[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
219                 if (unlink("/etc/locale.conf") < 0)
220                         return errno == ENOENT ? 0 : -errno;
221
222                 return 0;
223         }
224
225         r = write_env_file("/etc/locale.conf", l);
226         strv_free(l);
227
228         return r;
229 }
230
231 static void push_data(DBusConnection *bus) {
232         char **l_set = NULL, **l_unset = NULL, **t;
233         int c_set = 0, c_unset = 0, p;
234         DBusError error;
235         DBusMessage *m = NULL, *reply = NULL;
236         DBusMessageIter iter, sub;
237
238         dbus_error_init(&error);
239
240         assert(bus);
241
242         l_set = new0(char*, _PROP_MAX);
243         l_unset = new0(char*, _PROP_MAX);
244         if (!l_set || !l_unset) {
245                 log_error("Out of memory");
246                 goto finish;
247         }
248
249         for (p = 0; p < _PROP_MAX; p++) {
250                 assert(names[p]);
251
252                 if (isempty(data[p]))
253                         l_unset[c_set++] = (char*) names[p];
254                 else {
255                         char *s;
256
257                         if (asprintf(&s, "%s=%s", names[p], data[p]) < 0) {
258                                 log_error("Out of memory");
259                                 goto finish;
260                         }
261
262                         l_set[c_unset++] = s;
263                 }
264         }
265
266         assert(c_set + c_unset == _PROP_MAX);
267         m = dbus_message_new_method_call("org.freedesktop.systemd1", "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "UnsetAndSetEnvironment");
268         if (!m) {
269                 log_error("Could not allocate message.");
270                 goto finish;
271         }
272
273         dbus_message_iter_init_append(m, &iter);
274
275         if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub)) {
276                 log_error("Out of memory.");
277                 goto finish;
278         }
279
280         STRV_FOREACH(t, l_unset)
281                 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, t)) {
282                         log_error("Out of memory.");
283                         goto finish;
284                 }
285
286         if (!dbus_message_iter_close_container(&iter, &sub) ||
287             !dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub)) {
288                 log_error("Out of memory.");
289                 goto finish;
290         }
291
292         STRV_FOREACH(t, l_set)
293                 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, t)) {
294                         log_error("Out of memory.");
295                         goto finish;
296                 }
297
298         if (!dbus_message_iter_close_container(&iter, &sub)) {
299                 log_error("Out of memory.");
300                 goto finish;
301         }
302
303         reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error);
304         if (!reply) {
305                 log_error("Failed to set locale information: %s", bus_error_message(&error));
306                 goto finish;
307         }
308
309 finish:
310         if (m)
311                 dbus_message_unref(m);
312
313         if (reply)
314                 dbus_message_unref(reply);
315
316         dbus_error_free(&error);
317
318         strv_free(l_set);
319         free(l_unset);
320 }
321
322 static int append_locale(DBusMessageIter *i, const char *property, void *userdata) {
323         int r, c = 0, p;
324         char **l;
325
326         l = new0(char*, _PROP_MAX+1);
327         if (!l)
328                 return -ENOMEM;
329
330         for (p = 0; p < _PROP_MAX; p++) {
331                 char *t;
332
333                 if (isempty(data[p]))
334                         continue;
335
336                 if (asprintf(&t, "%s=%s", names[p], data[p]) < 0) {
337                         strv_free(l);
338                         return -ENOMEM;
339                 }
340
341                 l[c++] = t;
342         }
343
344         r = bus_property_append_strv(i, property, (void*) l);
345         strv_free(l);
346
347         return r;
348 }
349
350 static DBusHandlerResult locale_message_handler(
351                 DBusConnection *connection,
352                 DBusMessage *message,
353                 void *userdata) {
354
355         const BusProperty properties[] = {
356                 { "org.freedesktop.locale1", "Locale", append_locale, "as", NULL},
357                 { NULL, NULL, NULL, NULL, NULL }
358         };
359
360         DBusMessage *reply = NULL, *changed = NULL;
361         DBusError error;
362         int r;
363
364         assert(connection);
365         assert(message);
366
367         dbus_error_init(&error);
368
369         if (dbus_message_is_method_call(message, "org.freedesktop.locale1", "SetLocale")) {
370                 char **l = NULL, **i;
371                 dbus_bool_t interactive;
372                 DBusMessageIter iter;
373                 bool modified = false;
374                 bool passed[_PROP_MAX];
375                 int p;
376
377                 if (!dbus_message_iter_init(message, &iter))
378                         return bus_send_error_reply(connection, message, NULL, -EINVAL);
379
380                 r = bus_parse_strv_iter(&iter, &l);
381                 if (r < 0) {
382                         if (r == -ENOMEM)
383                                 goto oom;
384
385                         return bus_send_error_reply(connection, message, NULL, r);
386                 }
387
388                 if (!dbus_message_iter_next(&iter) ||
389                     dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_BOOLEAN)  {
390                         strv_free(l);
391                         return bus_send_error_reply(connection, message, NULL, -EINVAL);
392                 }
393
394                 dbus_message_iter_get_basic(&iter, &interactive);
395
396                 zero(passed);
397
398                 /* Check whether a variable changed and if so valid */
399                 STRV_FOREACH(i, l) {
400                         bool valid = false;
401
402                         for (p = 0; p < _PROP_MAX; p++) {
403                                 size_t k;
404
405                                 k = strlen(names[p]);
406                                 if (startswith(*i, names[p]) && (*i)[k] == '=') {
407                                         valid = true;
408                                         passed[p] = true;
409
410                                         if (!streq_ptr(*i + k + 1, data[p]))
411                                                 modified = true;
412
413                                         break;
414                                 }
415                         }
416
417                         if (!valid) {
418                                 strv_free(l);
419                                 return bus_send_error_reply(connection, message, NULL, -EINVAL);
420                         }
421                 }
422
423                 /* Check whether a variable is unset */
424                 if (!modified)  {
425                         for (p = 0; p < _PROP_MAX; p++)
426                                 if (!isempty(data[p]) && !passed[p]) {
427                                         modified = true;
428                                         break;
429                                 }
430                 }
431
432                 if (modified) {
433
434                         r = verify_polkit(connection, message, "org.freedesktop.locale1.set-locale", interactive, &error);
435                         if (r < 0) {
436                                 strv_free(l);
437                                 return bus_send_error_reply(connection, message, &error, r);
438                         }
439
440                         STRV_FOREACH(i, l) {
441                                 for (p = 0; p < _PROP_MAX; p++) {
442                                         size_t k;
443
444                                         k = strlen(names[p]);
445                                         if (startswith(*i, names[p]) && (*i)[k] == '=') {
446                                                 char *t;
447
448                                                 t = strdup(*i + k + 1);
449                                                 if (!t) {
450                                                         strv_free(l);
451                                                         goto oom;
452                                                 }
453
454                                                 free(data[p]);
455                                                 data[p] = t;
456
457                                                 break;
458                                         }
459                                 }
460                         }
461
462                         for (p = 0; p < _PROP_MAX; p++) {
463                                 if (passed[p])
464                                         continue;
465
466                                 free(data[p]);
467                                 data[p] = NULL;
468                         }
469
470                         simplify();
471
472                         r = write_data();
473                         if (r < 0) {
474                                 log_error("Failed to set locale: %s", strerror(-r));
475                                 return bus_send_error_reply(connection, message, NULL, r);
476                         }
477
478                         push_data(connection);
479
480                         log_info("Changed locale information.");
481
482                         changed = bus_properties_changed_new(
483                                         "/org/freedesktop/locale1",
484                                         "org.freedesktop.locale1",
485                                         "Locale\0");
486                         if (!changed)
487                                 goto oom;
488                 }
489
490         } else
491                 return bus_default_message_handler(connection, message, INTROSPECTION, INTERFACES_LIST, properties);
492
493         if (!(reply = dbus_message_new_method_return(message)))
494                 goto oom;
495
496         if (!dbus_connection_send(connection, reply, NULL))
497                 goto oom;
498
499         dbus_message_unref(reply);
500         reply = NULL;
501
502         if (changed) {
503
504                 if (!dbus_connection_send(connection, changed, NULL))
505                         goto oom;
506
507                 dbus_message_unref(changed);
508         }
509
510         return DBUS_HANDLER_RESULT_HANDLED;
511
512 oom:
513         if (reply)
514                 dbus_message_unref(reply);
515
516         if (changed)
517                 dbus_message_unref(changed);
518
519         dbus_error_free(&error);
520
521         return DBUS_HANDLER_RESULT_NEED_MEMORY;
522 }
523
524 static int connect_bus(DBusConnection **_bus) {
525         static const DBusObjectPathVTable locale_vtable = {
526                 .message_function = locale_message_handler
527         };
528         DBusError error;
529         DBusConnection *bus = NULL;
530         int r;
531
532         assert(_bus);
533
534         dbus_error_init(&error);
535
536         bus = dbus_bus_get_private(DBUS_BUS_SYSTEM, &error);
537         if (!bus) {
538                 log_error("Failed to get system D-Bus connection: %s", bus_error_message(&error));
539                 r = -ECONNREFUSED;
540                 goto fail;
541         }
542
543         dbus_connection_set_exit_on_disconnect(bus, FALSE);
544
545         if (!dbus_connection_register_object_path(bus, "/org/freedesktop/locale1", &locale_vtable, NULL) ||
546             !dbus_connection_add_filter(bus, bus_exit_idle_filter, &remain_until, NULL)) {
547                 log_error("Not enough memory");
548                 r = -ENOMEM;
549                 goto fail;
550         }
551
552         r = dbus_bus_request_name(bus, "org.freedesktop.locale1", DBUS_NAME_FLAG_DO_NOT_QUEUE, &error);
553         if (dbus_error_is_set(&error)) {
554                 log_error("Failed to register name on bus: %s", bus_error_message(&error));
555                 r = -EEXIST;
556                 goto fail;
557         }
558
559         if (r != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) {
560                 log_error("Failed to acquire name.");
561                 r = -EEXIST;
562                 goto fail;
563         }
564
565         if (_bus)
566                 *_bus = bus;
567
568         return 0;
569
570 fail:
571         dbus_connection_close(bus);
572         dbus_connection_unref(bus);
573
574         dbus_error_free(&error);
575
576         return r;
577 }
578
579 int main(int argc, char *argv[]) {
580         int r;
581         DBusConnection *bus = NULL;
582         bool exiting = false;
583
584         log_set_target(LOG_TARGET_AUTO);
585         log_parse_environment();
586         log_open();
587
588         umask(0022);
589
590         if (argc == 2 && streq(argv[1], "--introspect")) {
591                 fputs(DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE
592                       "<node>\n", stdout);
593                 fputs(locale_interface, stdout);
594                 fputs("</node>\n", stdout);
595                 return 0;
596         }
597
598         if (argc != 1) {
599                 log_error("This program takes no arguments.");
600                 r = -EINVAL;
601                 goto finish;
602         }
603
604         r = read_data();
605         if (r < 0) {
606                 log_error("Failed to read locale data: %s", strerror(-r));
607                 goto finish;
608         }
609
610         r = connect_bus(&bus);
611         if (r < 0)
612                 goto finish;
613
614         remain_until = now(CLOCK_MONOTONIC) + DEFAULT_EXIT_USEC;
615         for (;;) {
616
617                 if (!dbus_connection_read_write_dispatch(bus, exiting ? -1 : (int) (DEFAULT_EXIT_USEC/USEC_PER_MSEC)))
618                         break;
619
620                 if (!exiting && remain_until < now(CLOCK_MONOTONIC)) {
621                         exiting = true;
622                         bus_async_unregister_and_exit(bus, "org.freedesktop.locale1");
623                 }
624         }
625
626         r = 0;
627
628 finish:
629         free_data();
630
631         if (bus) {
632                 dbus_connection_flush(bus);
633                 dbus_connection_close(bus);
634                 dbus_connection_unref(bus);
635         }
636
637         return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
638 }