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"
33 #define INTROSPECTION \
34 DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE \
36 " <interface name=\"org.freedesktop.locale1\">\n" \
37 " <property name=\"Locale\" type=\"as\" access=\"read\"/>\n" \
38 " <method name=\"SetLocale\">\n" \
39 " <arg name=\"locale\" type=\"as\" direction=\"in\"/>\n" \
40 " <arg name=\"user_interaction\" type=\"b\" direction=\"in\"/>\n" \
43 BUS_PROPERTIES_INTERFACE \
44 BUS_INTROSPECTABLE_INTERFACE \
48 #define INTERFACES_LIST \
49 BUS_GENERIC_INTERFACES_LIST \
50 "org.freedesktop.locale1\0"
53 /* We don't list LC_ALL here on purpose. People should be
54 * using LANG instead. */
68 PROP_LC_IDENTIFICATION,
72 static const char * const names[_PROP_MAX] = {
74 [PROP_LC_CTYPE] = "LC_CTYPE",
75 [PROP_LC_NUMERIC] = "LC_NUMERIC",
76 [PROP_LC_TIME] = "LC_TIME",
77 [PROP_LC_COLLATE] = "LC_COLLATE",
78 [PROP_LC_MONETARY] = "LC_MONETARY",
79 [PROP_LC_MESSAGES] = "LC_MESSAGES",
80 [PROP_LC_PAPER] = "LC_PAPER",
81 [PROP_LC_NAME] = "LC_NAME",
82 [PROP_LC_ADDRESS] = "LC_ADDRESS",
83 [PROP_LC_TELEPHONE] = "LC_TELEPHONE",
84 [PROP_LC_MEASUREMENT] = "LC_MEASUREMENT",
85 [PROP_LC_IDENTIFICATION] = "LC_IDENTIFICATION"
88 static char *data[_PROP_MAX] = {
104 static void free_data(void) {
107 for (p = 0; p < _PROP_MAX; p++) {
113 static void simplify(void) {
116 for (p = 1; p < _PROP_MAX; p++)
117 if (isempty(data[p]) || streq_ptr(data[PROP_LANG], data[p])) {
123 static int read_data(void) {
128 r = parse_env_file("/etc/locale.conf", NEWLINE,
129 "LANG", &data[PROP_LANG],
130 "LC_CTYPE", &data[PROP_LC_CTYPE],
131 "LC_NUMERIC", &data[PROP_LC_NUMERIC],
132 "LC_TIME", &data[PROP_LC_TIME],
133 "LC_COLLATE", &data[PROP_LC_COLLATE],
134 "LC_MONETARY", &data[PROP_LC_MONETARY],
135 "LC_MESSAGES", &data[PROP_LC_MESSAGES],
136 "LC_PAPER", &data[PROP_LC_PAPER],
137 "LC_NAME", &data[PROP_LC_NAME],
138 "LC_ADDRESS", &data[PROP_LC_ADDRESS],
139 "LC_TELEPHONE", &data[PROP_LC_TELEPHONE],
140 "LC_MEASUREMENT", &data[PROP_LC_MEASUREMENT],
141 "LC_IDENTIFICATION", &data[PROP_LC_IDENTIFICATION],
147 /* Fill in what we got passed from systemd. */
149 for (p = 0; p < _PROP_MAX; p++) {
154 e = getenv(names[p]);
173 static int write_data(void) {
177 r = load_env_file("/etc/locale.conf", &l);
178 if (r < 0 && r != -ENOENT)
181 for (p = 0; p < _PROP_MAX; p++) {
186 if (isempty(data[p])) {
187 l = strv_env_unset(l, names[p]);
191 if (asprintf(&t, "%s=%s", names[p], data[p]) < 0) {
196 u = strv_env_set(l, t);
206 if (strv_isempty(l)) {
208 if (unlink("/etc/locale.conf") < 0)
209 return errno == ENOENT ? 0 : -errno;
214 r = write_env_file("/etc/locale.conf", l);
220 static void push_data(DBusConnection *bus) {
221 char **l_set = NULL, **l_unset = NULL, **t;
222 int c_set = 0, c_unset = 0, p;
224 DBusMessage *m = NULL, *reply = NULL;
225 DBusMessageIter iter, sub;
227 dbus_error_init(&error);
231 l_set = new0(char*, _PROP_MAX);
232 l_unset = new0(char*, _PROP_MAX);
233 if (!l_set || !l_unset) {
234 log_error("Out of memory");
238 for (p = 0; p < _PROP_MAX; p++) {
241 if (isempty(data[p]))
242 l_unset[c_set++] = (char*) names[p];
246 if (asprintf(&s, "%s=%s", names[p], data[p]) < 0) {
247 log_error("Out of memory");
251 l_set[c_unset++] = s;
255 assert(c_set + c_unset == _PROP_MAX);
256 m = dbus_message_new_method_call("org.freedesktop.systemd1", "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "UnsetAndSetEnvironment");
258 log_error("Could not allocate message.");
262 dbus_message_iter_init_append(m, &iter);
264 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub)) {
265 log_error("Out of memory.");
269 STRV_FOREACH(t, l_unset)
270 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, t)) {
271 log_error("Out of memory.");
275 if (!dbus_message_iter_close_container(&iter, &sub) ||
276 !dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub)) {
277 log_error("Out of memory.");
281 STRV_FOREACH(t, l_set)
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 log_error("Out of memory.");
292 reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error);
294 log_error("Failed to set locale information: %s", bus_error_message(&error));
300 dbus_message_unref(m);
303 dbus_message_unref(reply);
305 dbus_error_free(&error);
311 static int append_locale(DBusMessageIter *i, const char *property, void *userdata) {
315 l = new0(char*, _PROP_MAX+1);
319 for (p = 0; p < _PROP_MAX; p++) {
322 if (isempty(data[p]))
325 if (asprintf(&t, "%s=%s", names[p], data[p]) < 0) {
333 r = bus_property_append_strv(i, property, (void*) l);
339 static DBusHandlerResult locale_message_handler(
340 DBusConnection *connection,
341 DBusMessage *message,
344 const BusProperty properties[] = {
345 { "org.freedesktop.locale1", "Locale", append_locale, "as", NULL},
346 { NULL, NULL, NULL, NULL, NULL }
349 DBusMessage *reply = NULL, *changed = NULL;
356 dbus_error_init(&error);
358 if (dbus_message_is_method_call(message, "org.freedesktop.locale1", "SetLocale")) {
359 char **l = NULL, **i;
360 dbus_bool_t interactive;
361 DBusMessageIter iter;
362 bool modified = false;
363 bool passed[_PROP_MAX];
366 if (!dbus_message_iter_init(message, &iter))
367 return bus_send_error_reply(connection, message, NULL, -EINVAL);
369 r = bus_parse_strv_iter(&iter, &l);
374 return bus_send_error_reply(connection, message, NULL, r);
377 if (!dbus_message_iter_next(&iter) ||
378 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_BOOLEAN) {
380 return bus_send_error_reply(connection, message, NULL, -EINVAL);
383 dbus_message_iter_get_basic(&iter, &interactive);
387 /* Check whether a variable changed and if so valid */
391 for (p = 0; p < _PROP_MAX; p++) {
394 k = strlen(names[p]);
395 if (startswith(*i, names[p]) && (*i)[k] == '=') {
399 if (!streq_ptr(*i + k + 1, data[p]))
408 return bus_send_error_reply(connection, message, NULL, -EINVAL);
412 /* Check whether a variable is unset */
414 for (p = 0; p < _PROP_MAX; p++)
415 if (!isempty(data[p]) && !passed[p]) {
423 r = verify_polkit(connection, message, "org.freedesktop.locale1.set-locale", interactive, &error);
426 return bus_send_error_reply(connection, message, &error, r);
430 for (p = 0; p < _PROP_MAX; p++) {
433 k = strlen(names[p]);
434 if (startswith(*i, names[p]) && (*i)[k] == '=') {
437 t = strdup(*i + k + 1);
451 for (p = 0; p < _PROP_MAX; p++) {
463 log_error("Failed to set locale: %s", strerror(-r));
464 return bus_send_error_reply(connection, message, NULL, r);
467 push_data(connection);
469 log_info("Changed locale information.");
471 changed = bus_properties_changed_new(
472 "/org/freedesktop/locale1",
473 "org.freedesktop.locale1",
480 return bus_default_message_handler(connection, message, INTROSPECTION, INTERFACES_LIST, properties);
482 if (!(reply = dbus_message_new_method_return(message)))
485 if (!dbus_connection_send(connection, reply, NULL))
488 dbus_message_unref(reply);
493 if (!dbus_connection_send(connection, changed, NULL))
496 dbus_message_unref(changed);
499 return DBUS_HANDLER_RESULT_HANDLED;
503 dbus_message_unref(reply);
506 dbus_message_unref(changed);
508 dbus_error_free(&error);
510 return DBUS_HANDLER_RESULT_NEED_MEMORY;
513 static int connect_bus(DBusConnection **_bus) {
514 static const DBusObjectPathVTable locale_vtable = {
515 .message_function = locale_message_handler
518 DBusConnection *bus = NULL;
523 dbus_error_init(&error);
525 bus = dbus_bus_get_private(DBUS_BUS_SYSTEM, &error);
527 log_error("Failed to get system D-Bus connection: %s", error.message);
532 if (!dbus_connection_register_object_path(bus, "/org/freedesktop/locale1", &locale_vtable, NULL)) {
533 log_error("Not enough memory");
538 if (dbus_bus_request_name(bus, "org.freedesktop.locale1", DBUS_NAME_FLAG_DO_NOT_QUEUE, &error) < 0) {
539 log_error("Failed to register name on bus: %s", error.message);
550 dbus_connection_close(bus);
551 dbus_connection_unref(bus);
553 dbus_error_free(&error);
558 int main(int argc, char *argv[]) {
560 DBusConnection *bus = NULL;
562 log_set_target(LOG_TARGET_AUTO);
563 log_parse_environment();
567 log_error("This program takes no arguments.");
576 log_error("Failed to read locale data: %s", strerror(-r));
580 r = connect_bus(&bus);
584 while (dbus_connection_read_write_dispatch(bus, -1))
593 dbus_connection_flush(bus);
594 dbus_connection_close(bus);
595 dbus_connection_unref(bus);
598 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;