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"
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" \
42 #define INTROSPECTION \
43 DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE \
46 BUS_PROPERTIES_INTERFACE \
47 BUS_INTROSPECTABLE_INTERFACE \
51 #define INTERFACES_LIST \
52 BUS_GENERIC_INTERFACES_LIST \
53 "org.freedesktop.locale1\0"
55 const char locale_interface[] _introspect_("locale1") = INTERFACE;
58 /* We don't list LC_ALL here on purpose. People should be
59 * using LANG instead. */
73 PROP_LC_IDENTIFICATION,
77 static const char * const names[_PROP_MAX] = {
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"
93 static char *data[_PROP_MAX] = {
109 static void free_data(void) {
112 for (p = 0; p < _PROP_MAX; p++) {
118 static void simplify(void) {
121 for (p = 1; p < _PROP_MAX; p++)
122 if (isempty(data[p]) || streq_ptr(data[PROP_LANG], data[p])) {
128 static int read_data(void) {
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],
152 /* Fill in what we got passed from systemd. */
154 for (p = 0; p < _PROP_MAX; p++) {
159 e = getenv(names[p]);
178 static int write_data(void) {
182 r = load_env_file("/etc/locale.conf", &l);
183 if (r < 0 && r != -ENOENT)
186 for (p = 0; p < _PROP_MAX; p++) {
191 if (isempty(data[p])) {
192 l = strv_env_unset(l, names[p]);
196 if (asprintf(&t, "%s=%s", names[p], data[p]) < 0) {
201 u = strv_env_set(l, t);
211 if (strv_isempty(l)) {
213 if (unlink("/etc/locale.conf") < 0)
214 return errno == ENOENT ? 0 : -errno;
219 r = write_env_file("/etc/locale.conf", l);
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;
229 DBusMessage *m = NULL, *reply = NULL;
230 DBusMessageIter iter, sub;
232 dbus_error_init(&error);
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");
243 for (p = 0; p < _PROP_MAX; p++) {
246 if (isempty(data[p]))
247 l_unset[c_set++] = (char*) names[p];
251 if (asprintf(&s, "%s=%s", names[p], data[p]) < 0) {
252 log_error("Out of memory");
256 l_set[c_unset++] = s;
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");
263 log_error("Could not allocate message.");
267 dbus_message_iter_init_append(m, &iter);
269 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub)) {
270 log_error("Out of memory.");
274 STRV_FOREACH(t, l_unset)
275 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, t)) {
276 log_error("Out of memory.");
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.");
286 STRV_FOREACH(t, l_set)
287 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, t)) {
288 log_error("Out of memory.");
292 if (!dbus_message_iter_close_container(&iter, &sub)) {
293 log_error("Out of memory.");
297 reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error);
299 log_error("Failed to set locale information: %s", bus_error_message(&error));
305 dbus_message_unref(m);
308 dbus_message_unref(reply);
310 dbus_error_free(&error);
316 static int append_locale(DBusMessageIter *i, const char *property, void *userdata) {
320 l = new0(char*, _PROP_MAX+1);
324 for (p = 0; p < _PROP_MAX; p++) {
327 if (isempty(data[p]))
330 if (asprintf(&t, "%s=%s", names[p], data[p]) < 0) {
338 r = bus_property_append_strv(i, property, (void*) l);
344 static DBusHandlerResult locale_message_handler(
345 DBusConnection *connection,
346 DBusMessage *message,
349 const BusProperty properties[] = {
350 { "org.freedesktop.locale1", "Locale", append_locale, "as", NULL},
351 { NULL, NULL, NULL, NULL, NULL }
354 DBusMessage *reply = NULL, *changed = NULL;
361 dbus_error_init(&error);
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];
371 if (!dbus_message_iter_init(message, &iter))
372 return bus_send_error_reply(connection, message, NULL, -EINVAL);
374 r = bus_parse_strv_iter(&iter, &l);
379 return bus_send_error_reply(connection, message, NULL, r);
382 if (!dbus_message_iter_next(&iter) ||
383 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_BOOLEAN) {
385 return bus_send_error_reply(connection, message, NULL, -EINVAL);
388 dbus_message_iter_get_basic(&iter, &interactive);
392 /* Check whether a variable changed and if so valid */
396 for (p = 0; p < _PROP_MAX; p++) {
399 k = strlen(names[p]);
400 if (startswith(*i, names[p]) && (*i)[k] == '=') {
404 if (!streq_ptr(*i + k + 1, data[p]))
413 return bus_send_error_reply(connection, message, NULL, -EINVAL);
417 /* Check whether a variable is unset */
419 for (p = 0; p < _PROP_MAX; p++)
420 if (!isempty(data[p]) && !passed[p]) {
428 r = verify_polkit(connection, message, "org.freedesktop.locale1.set-locale", interactive, &error);
431 return bus_send_error_reply(connection, message, &error, r);
435 for (p = 0; p < _PROP_MAX; p++) {
438 k = strlen(names[p]);
439 if (startswith(*i, names[p]) && (*i)[k] == '=') {
442 t = strdup(*i + k + 1);
456 for (p = 0; p < _PROP_MAX; p++) {
468 log_error("Failed to set locale: %s", strerror(-r));
469 return bus_send_error_reply(connection, message, NULL, r);
472 push_data(connection);
474 log_info("Changed locale information.");
476 changed = bus_properties_changed_new(
477 "/org/freedesktop/locale1",
478 "org.freedesktop.locale1",
485 return bus_default_message_handler(connection, message, INTROSPECTION, INTERFACES_LIST, properties);
487 if (!(reply = dbus_message_new_method_return(message)))
490 if (!dbus_connection_send(connection, reply, NULL))
493 dbus_message_unref(reply);
498 if (!dbus_connection_send(connection, changed, NULL))
501 dbus_message_unref(changed);
504 return DBUS_HANDLER_RESULT_HANDLED;
508 dbus_message_unref(reply);
511 dbus_message_unref(changed);
513 dbus_error_free(&error);
515 return DBUS_HANDLER_RESULT_NEED_MEMORY;
518 static int connect_bus(DBusConnection **_bus) {
519 static const DBusObjectPathVTable locale_vtable = {
520 .message_function = locale_message_handler
523 DBusConnection *bus = NULL;
528 dbus_error_init(&error);
530 bus = dbus_bus_get_private(DBUS_BUS_SYSTEM, &error);
532 log_error("Failed to get system D-Bus connection: %s", error.message);
537 if (!dbus_connection_register_object_path(bus, "/org/freedesktop/locale1", &locale_vtable, NULL)) {
538 log_error("Not enough memory");
543 if (dbus_bus_request_name(bus, "org.freedesktop.locale1", DBUS_NAME_FLAG_DO_NOT_QUEUE, &error) < 0) {
544 log_error("Failed to register name on bus: %s", error.message);
555 dbus_connection_close(bus);
556 dbus_connection_unref(bus);
558 dbus_error_free(&error);
563 int main(int argc, char *argv[]) {
565 DBusConnection *bus = NULL;
567 log_set_target(LOG_TARGET_AUTO);
568 log_parse_environment();
571 if (argc == 2 && streq(argv[1], "--introspect")) {
572 fputs(DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE
574 fputs(locale_interface, stdout);
575 fputs("</node>\n", stdout);
580 log_error("This program takes no arguments.");
589 log_error("Failed to read locale data: %s", strerror(-r));
593 r = connect_bus(&bus);
597 while (dbus_connection_read_write_dispatch(bus, -1))
606 dbus_connection_flush(bus);
607 dbus_connection_close(bus);
608 dbus_connection_unref(bus);
611 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;