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)) {
219 if (unlink("/etc/locale.conf") < 0)
220 return errno == ENOENT ? 0 : -errno;
225 r = write_env_file("/etc/locale.conf", l);
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;
235 DBusMessage *m = NULL, *reply = NULL;
236 DBusMessageIter iter, sub;
238 dbus_error_init(&error);
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");
249 for (p = 0; p < _PROP_MAX; p++) {
252 if (isempty(data[p]))
253 l_unset[c_set++] = (char*) names[p];
257 if (asprintf(&s, "%s=%s", names[p], data[p]) < 0) {
258 log_error("Out of memory");
262 l_set[c_unset++] = s;
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");
269 log_error("Could not allocate message.");
273 dbus_message_iter_init_append(m, &iter);
275 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub)) {
276 log_error("Out of memory.");
280 STRV_FOREACH(t, l_unset)
281 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, t)) {
282 log_error("Out of memory.");
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.");
292 STRV_FOREACH(t, l_set)
293 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, t)) {
294 log_error("Out of memory.");
298 if (!dbus_message_iter_close_container(&iter, &sub)) {
299 log_error("Out of memory.");
303 reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error);
305 log_error("Failed to set locale information: %s", bus_error_message(&error));
311 dbus_message_unref(m);
314 dbus_message_unref(reply);
316 dbus_error_free(&error);
322 static int append_locale(DBusMessageIter *i, const char *property, void *userdata) {
326 l = new0(char*, _PROP_MAX+1);
330 for (p = 0; p < _PROP_MAX; p++) {
333 if (isempty(data[p]))
336 if (asprintf(&t, "%s=%s", names[p], data[p]) < 0) {
344 r = bus_property_append_strv(i, property, (void*) l);
350 static DBusHandlerResult locale_message_handler(
351 DBusConnection *connection,
352 DBusMessage *message,
355 const BusProperty properties[] = {
356 { "org.freedesktop.locale1", "Locale", append_locale, "as", NULL},
357 { NULL, NULL, NULL, NULL, NULL }
360 DBusMessage *reply = NULL, *changed = NULL;
367 dbus_error_init(&error);
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];
377 if (!dbus_message_iter_init(message, &iter))
378 return bus_send_error_reply(connection, message, NULL, -EINVAL);
380 r = bus_parse_strv_iter(&iter, &l);
385 return bus_send_error_reply(connection, message, NULL, r);
388 if (!dbus_message_iter_next(&iter) ||
389 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_BOOLEAN) {
391 return bus_send_error_reply(connection, message, NULL, -EINVAL);
394 dbus_message_iter_get_basic(&iter, &interactive);
398 /* Check whether a variable changed and if so valid */
402 for (p = 0; p < _PROP_MAX; p++) {
405 k = strlen(names[p]);
406 if (startswith(*i, names[p]) && (*i)[k] == '=') {
410 if (!streq_ptr(*i + k + 1, data[p]))
419 return bus_send_error_reply(connection, message, NULL, -EINVAL);
423 /* Check whether a variable is unset */
425 for (p = 0; p < _PROP_MAX; p++)
426 if (!isempty(data[p]) && !passed[p]) {
434 r = verify_polkit(connection, message, "org.freedesktop.locale1.set-locale", interactive, &error);
437 return bus_send_error_reply(connection, message, &error, r);
441 for (p = 0; p < _PROP_MAX; p++) {
444 k = strlen(names[p]);
445 if (startswith(*i, names[p]) && (*i)[k] == '=') {
448 t = strdup(*i + k + 1);
462 for (p = 0; p < _PROP_MAX; p++) {
474 log_error("Failed to set locale: %s", strerror(-r));
475 return bus_send_error_reply(connection, message, NULL, r);
478 push_data(connection);
480 log_info("Changed locale information.");
482 changed = bus_properties_changed_new(
483 "/org/freedesktop/locale1",
484 "org.freedesktop.locale1",
491 return bus_default_message_handler(connection, message, INTROSPECTION, INTERFACES_LIST, properties);
493 if (!(reply = dbus_message_new_method_return(message)))
496 if (!dbus_connection_send(connection, reply, NULL))
499 dbus_message_unref(reply);
504 if (!dbus_connection_send(connection, changed, NULL))
507 dbus_message_unref(changed);
510 return DBUS_HANDLER_RESULT_HANDLED;
514 dbus_message_unref(reply);
517 dbus_message_unref(changed);
519 dbus_error_free(&error);
521 return DBUS_HANDLER_RESULT_NEED_MEMORY;
524 static int connect_bus(DBusConnection **_bus) {
525 static const DBusObjectPathVTable locale_vtable = {
526 .message_function = locale_message_handler
529 DBusConnection *bus = NULL;
534 dbus_error_init(&error);
536 bus = dbus_bus_get_private(DBUS_BUS_SYSTEM, &error);
538 log_error("Failed to get system D-Bus connection: %s", bus_error_message(&error));
543 dbus_connection_set_exit_on_disconnect(bus, FALSE);
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");
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));
559 if (r != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) {
560 log_error("Failed to acquire name.");
571 dbus_connection_close(bus);
572 dbus_connection_unref(bus);
574 dbus_error_free(&error);
579 int main(int argc, char *argv[]) {
581 DBusConnection *bus = NULL;
582 bool exiting = false;
584 log_set_target(LOG_TARGET_AUTO);
585 log_parse_environment();
590 if (argc == 2 && streq(argv[1], "--introspect")) {
591 fputs(DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE
593 fputs(locale_interface, stdout);
594 fputs("</node>\n", stdout);
599 log_error("This program takes no arguments.");
606 log_error("Failed to read locale data: %s", strerror(-r));
610 r = connect_bus(&bus);
614 remain_until = now(CLOCK_MONOTONIC) + DEFAULT_EXIT_USEC;
617 if (!dbus_connection_read_write_dispatch(bus, exiting ? -1 : (int) (DEFAULT_EXIT_USEC/USEC_PER_MSEC)))
620 if (!exiting && remain_until < now(CLOCK_MONOTONIC)) {
622 bus_async_unregister_and_exit(bus, "org.freedesktop.locale1");
632 dbus_connection_flush(bus);
633 dbus_connection_close(bus);
634 dbus_connection_unref(bus);
637 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;