1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2011 Lennart Poettering
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.
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.
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/>.
22 #include <dbus/dbus.h>
30 #include "dbus-common.h"
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" \
43 #define INTROSPECTION \
44 DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE \
47 BUS_PROPERTIES_INTERFACE \
48 BUS_INTROSPECTABLE_INTERFACE \
52 #define INTERFACES_LIST \
53 BUS_GENERIC_INTERFACES_LIST \
54 "org.freedesktop.locale1\0"
56 const char locale_interface[] _introspect_("locale1") = INTERFACE;
59 /* We don't list LC_ALL here on purpose. People should be
60 * using LANG instead. */
75 PROP_LC_IDENTIFICATION,
79 static const char * const names[_PROP_MAX] = {
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"
96 static char *data[_PROP_MAX] = {
112 static usec_t remain_until = 0;
114 static void free_data(void) {
117 for (p = 0; p < _PROP_MAX; p++) {
123 static void simplify(void) {
126 for (p = 1; p < _PROP_MAX; p++)
127 if (isempty(data[p]) || streq_ptr(data[PROP_LANG], data[p])) {
133 static int read_data(void) {
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],
158 /* Fill in what we got passed from systemd. */
160 for (p = 0; p < _PROP_MAX; p++) {
165 e = getenv(names[p]);
184 static int write_data(void) {
188 r = load_env_file("/etc/locale.conf", &l);
189 if (r < 0 && r != -ENOENT)
192 for (p = 0; p < _PROP_MAX; p++) {
197 if (isempty(data[p])) {
198 l = strv_env_unset(l, names[p]);
202 if (asprintf(&t, "%s=%s", names[p], data[p]) < 0) {
207 u = strv_env_set(l, t);
217 if (strv_isempty(l)) {
220 if (unlink("/etc/locale.conf") < 0)
221 return errno == ENOENT ? 0 : -errno;
226 r = write_env_file("/etc/locale.conf", l);
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;
236 DBusMessage *m = NULL, *reply = NULL;
237 DBusMessageIter iter, sub;
239 dbus_error_init(&error);
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");
250 for (p = 0; p < _PROP_MAX; p++) {
253 if (isempty(data[p]))
254 l_unset[c_set++] = (char*) names[p];
258 if (asprintf(&s, "%s=%s", names[p], data[p]) < 0) {
259 log_error("Out of memory");
263 l_set[c_unset++] = s;
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");
270 log_error("Could not allocate message.");
274 dbus_message_iter_init_append(m, &iter);
276 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub)) {
277 log_error("Out of memory.");
281 STRV_FOREACH(t, l_unset)
282 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, t)) {
283 log_error("Out of memory.");
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.");
293 STRV_FOREACH(t, l_set)
294 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, t)) {
295 log_error("Out of memory.");
299 if (!dbus_message_iter_close_container(&iter, &sub)) {
300 log_error("Out of memory.");
304 reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error);
306 log_error("Failed to set locale information: %s", bus_error_message(&error));
312 dbus_message_unref(m);
315 dbus_message_unref(reply);
317 dbus_error_free(&error);
323 static int append_locale(DBusMessageIter *i, const char *property, void *userdata) {
327 l = new0(char*, _PROP_MAX+1);
331 for (p = 0; p < _PROP_MAX; p++) {
334 if (isempty(data[p]))
337 if (asprintf(&t, "%s=%s", names[p], data[p]) < 0) {
345 r = bus_property_append_strv(i, property, (void*) l);
351 static DBusHandlerResult locale_message_handler(
352 DBusConnection *connection,
353 DBusMessage *message,
356 const BusProperty properties[] = {
357 { "org.freedesktop.locale1", "Locale", append_locale, "as", NULL},
358 { NULL, NULL, NULL, NULL, NULL }
361 DBusMessage *reply = NULL, *changed = NULL;
368 dbus_error_init(&error);
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];
378 if (!dbus_message_iter_init(message, &iter))
379 return bus_send_error_reply(connection, message, NULL, -EINVAL);
381 r = bus_parse_strv_iter(&iter, &l);
386 return bus_send_error_reply(connection, message, NULL, r);
389 if (!dbus_message_iter_next(&iter) ||
390 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_BOOLEAN) {
392 return bus_send_error_reply(connection, message, NULL, -EINVAL);
395 dbus_message_iter_get_basic(&iter, &interactive);
399 /* Check whether a variable changed and if so valid */
403 for (p = 0; p < _PROP_MAX; p++) {
406 k = strlen(names[p]);
407 if (startswith(*i, names[p]) && (*i)[k] == '=') {
411 if (!streq_ptr(*i + k + 1, data[p]))
420 return bus_send_error_reply(connection, message, NULL, -EINVAL);
424 /* Check whether a variable is unset */
426 for (p = 0; p < _PROP_MAX; p++)
427 if (!isempty(data[p]) && !passed[p]) {
435 r = verify_polkit(connection, message, "org.freedesktop.locale1.set-locale", interactive, &error);
438 return bus_send_error_reply(connection, message, &error, r);
442 for (p = 0; p < _PROP_MAX; p++) {
445 k = strlen(names[p]);
446 if (startswith(*i, names[p]) && (*i)[k] == '=') {
449 t = strdup(*i + k + 1);
465 for (p = 0; p < _PROP_MAX; p++) {
477 log_error("Failed to set locale: %s", strerror(-r));
478 return bus_send_error_reply(connection, message, NULL, r);
481 push_data(connection);
483 log_info("Changed locale information.");
485 changed = bus_properties_changed_new(
486 "/org/freedesktop/locale1",
487 "org.freedesktop.locale1",
494 return bus_default_message_handler(connection, message, INTROSPECTION, INTERFACES_LIST, properties);
496 if (!(reply = dbus_message_new_method_return(message)))
499 if (!dbus_connection_send(connection, reply, NULL))
502 dbus_message_unref(reply);
507 if (!dbus_connection_send(connection, changed, NULL))
510 dbus_message_unref(changed);
513 return DBUS_HANDLER_RESULT_HANDLED;
517 dbus_message_unref(reply);
520 dbus_message_unref(changed);
522 dbus_error_free(&error);
524 return DBUS_HANDLER_RESULT_NEED_MEMORY;
527 static int connect_bus(DBusConnection **_bus) {
528 static const DBusObjectPathVTable locale_vtable = {
529 .message_function = locale_message_handler
532 DBusConnection *bus = NULL;
537 dbus_error_init(&error);
539 bus = dbus_bus_get_private(DBUS_BUS_SYSTEM, &error);
541 log_error("Failed to get system D-Bus connection: %s", bus_error_message(&error));
546 dbus_connection_set_exit_on_disconnect(bus, FALSE);
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");
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));
562 if (r != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) {
563 log_error("Failed to acquire name.");
574 dbus_connection_close(bus);
575 dbus_connection_unref(bus);
577 dbus_error_free(&error);
582 int main(int argc, char *argv[]) {
584 DBusConnection *bus = NULL;
585 bool exiting = false;
587 log_set_target(LOG_TARGET_AUTO);
588 log_parse_environment();
593 if (argc == 2 && streq(argv[1], "--introspect")) {
594 fputs(DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE
596 fputs(locale_interface, stdout);
597 fputs("</node>\n", stdout);
602 log_error("This program takes no arguments.");
609 log_error("Failed to read locale data: %s", strerror(-r));
613 r = connect_bus(&bus);
617 remain_until = now(CLOCK_MONOTONIC) + DEFAULT_EXIT_USEC;
620 if (!dbus_connection_read_write_dispatch(bus, exiting ? -1 : (int) (DEFAULT_EXIT_USEC/USEC_PER_MSEC)))
623 if (!exiting && remain_until < now(CLOCK_MONOTONIC)) {
625 bus_async_unregister_and_exit(bus, "org.freedesktop.locale1");
635 dbus_connection_flush(bus);
636 dbus_connection_close(bus);
637 dbus_connection_unref(bus);
640 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;