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. */
74 PROP_LC_IDENTIFICATION,
78 static const char * const names[_PROP_MAX] = {
80 [PROP_LANGUAGE] = "LANGUAGE",
81 [PROP_LC_CTYPE] = "LC_CTYPE",
82 [PROP_LC_NUMERIC] = "LC_NUMERIC",
83 [PROP_LC_TIME] = "LC_TIME",
84 [PROP_LC_COLLATE] = "LC_COLLATE",
85 [PROP_LC_MONETARY] = "LC_MONETARY",
86 [PROP_LC_MESSAGES] = "LC_MESSAGES",
87 [PROP_LC_PAPER] = "LC_PAPER",
88 [PROP_LC_NAME] = "LC_NAME",
89 [PROP_LC_ADDRESS] = "LC_ADDRESS",
90 [PROP_LC_TELEPHONE] = "LC_TELEPHONE",
91 [PROP_LC_MEASUREMENT] = "LC_MEASUREMENT",
92 [PROP_LC_IDENTIFICATION] = "LC_IDENTIFICATION"
95 static char *data[_PROP_MAX] = {
111 static void free_data(void) {
114 for (p = 0; p < _PROP_MAX; p++) {
120 static void simplify(void) {
123 for (p = 1; p < _PROP_MAX; p++)
124 if (isempty(data[p]) || streq_ptr(data[PROP_LANG], data[p])) {
130 static int read_data(void) {
135 r = parse_env_file("/etc/locale.conf", NEWLINE,
136 "LANG", &data[PROP_LANG],
137 "LANGUAGE", &data[PROP_LANGUAGE],
138 "LC_CTYPE", &data[PROP_LC_CTYPE],
139 "LC_NUMERIC", &data[PROP_LC_NUMERIC],
140 "LC_TIME", &data[PROP_LC_TIME],
141 "LC_COLLATE", &data[PROP_LC_COLLATE],
142 "LC_MONETARY", &data[PROP_LC_MONETARY],
143 "LC_MESSAGES", &data[PROP_LC_MESSAGES],
144 "LC_PAPER", &data[PROP_LC_PAPER],
145 "LC_NAME", &data[PROP_LC_NAME],
146 "LC_ADDRESS", &data[PROP_LC_ADDRESS],
147 "LC_TELEPHONE", &data[PROP_LC_TELEPHONE],
148 "LC_MEASUREMENT", &data[PROP_LC_MEASUREMENT],
149 "LC_IDENTIFICATION", &data[PROP_LC_IDENTIFICATION],
155 /* Fill in what we got passed from systemd. */
157 for (p = 0; p < _PROP_MAX; p++) {
162 e = getenv(names[p]);
181 static int write_data(void) {
185 r = load_env_file("/etc/locale.conf", &l);
186 if (r < 0 && r != -ENOENT)
189 for (p = 0; p < _PROP_MAX; p++) {
194 if (isempty(data[p])) {
195 l = strv_env_unset(l, names[p]);
199 if (asprintf(&t, "%s=%s", names[p], data[p]) < 0) {
204 u = strv_env_set(l, t);
214 if (strv_isempty(l)) {
216 if (unlink("/etc/locale.conf") < 0)
217 return errno == ENOENT ? 0 : -errno;
222 r = write_env_file("/etc/locale.conf", l);
228 static void push_data(DBusConnection *bus) {
229 char **l_set = NULL, **l_unset = NULL, **t;
230 int c_set = 0, c_unset = 0, p;
232 DBusMessage *m = NULL, *reply = NULL;
233 DBusMessageIter iter, sub;
235 dbus_error_init(&error);
239 l_set = new0(char*, _PROP_MAX);
240 l_unset = new0(char*, _PROP_MAX);
241 if (!l_set || !l_unset) {
242 log_error("Out of memory");
246 for (p = 0; p < _PROP_MAX; p++) {
249 if (isempty(data[p]))
250 l_unset[c_set++] = (char*) names[p];
254 if (asprintf(&s, "%s=%s", names[p], data[p]) < 0) {
255 log_error("Out of memory");
259 l_set[c_unset++] = s;
263 assert(c_set + c_unset == _PROP_MAX);
264 m = dbus_message_new_method_call("org.freedesktop.systemd1", "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "UnsetAndSetEnvironment");
266 log_error("Could not allocate message.");
270 dbus_message_iter_init_append(m, &iter);
272 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub)) {
273 log_error("Out of memory.");
277 STRV_FOREACH(t, l_unset)
278 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, t)) {
279 log_error("Out of memory.");
283 if (!dbus_message_iter_close_container(&iter, &sub) ||
284 !dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub)) {
285 log_error("Out of memory.");
289 STRV_FOREACH(t, l_set)
290 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, t)) {
291 log_error("Out of memory.");
295 if (!dbus_message_iter_close_container(&iter, &sub)) {
296 log_error("Out of memory.");
300 reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error);
302 log_error("Failed to set locale information: %s", bus_error_message(&error));
308 dbus_message_unref(m);
311 dbus_message_unref(reply);
313 dbus_error_free(&error);
319 static int append_locale(DBusMessageIter *i, const char *property, void *userdata) {
323 l = new0(char*, _PROP_MAX+1);
327 for (p = 0; p < _PROP_MAX; p++) {
330 if (isempty(data[p]))
333 if (asprintf(&t, "%s=%s", names[p], data[p]) < 0) {
341 r = bus_property_append_strv(i, property, (void*) l);
347 static DBusHandlerResult locale_message_handler(
348 DBusConnection *connection,
349 DBusMessage *message,
352 const BusProperty properties[] = {
353 { "org.freedesktop.locale1", "Locale", append_locale, "as", NULL},
354 { NULL, NULL, NULL, NULL, NULL }
357 DBusMessage *reply = NULL, *changed = NULL;
364 dbus_error_init(&error);
366 if (dbus_message_is_method_call(message, "org.freedesktop.locale1", "SetLocale")) {
367 char **l = NULL, **i;
368 dbus_bool_t interactive;
369 DBusMessageIter iter;
370 bool modified = false;
371 bool passed[_PROP_MAX];
374 if (!dbus_message_iter_init(message, &iter))
375 return bus_send_error_reply(connection, message, NULL, -EINVAL);
377 r = bus_parse_strv_iter(&iter, &l);
382 return bus_send_error_reply(connection, message, NULL, r);
385 if (!dbus_message_iter_next(&iter) ||
386 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_BOOLEAN) {
388 return bus_send_error_reply(connection, message, NULL, -EINVAL);
391 dbus_message_iter_get_basic(&iter, &interactive);
395 /* Check whether a variable changed and if so valid */
399 for (p = 0; p < _PROP_MAX; p++) {
402 k = strlen(names[p]);
403 if (startswith(*i, names[p]) && (*i)[k] == '=') {
407 if (!streq_ptr(*i + k + 1, data[p]))
416 return bus_send_error_reply(connection, message, NULL, -EINVAL);
420 /* Check whether a variable is unset */
422 for (p = 0; p < _PROP_MAX; p++)
423 if (!isempty(data[p]) && !passed[p]) {
431 r = verify_polkit(connection, message, "org.freedesktop.locale1.set-locale", interactive, &error);
434 return bus_send_error_reply(connection, message, &error, r);
438 for (p = 0; p < _PROP_MAX; p++) {
441 k = strlen(names[p]);
442 if (startswith(*i, names[p]) && (*i)[k] == '=') {
445 t = strdup(*i + k + 1);
459 for (p = 0; p < _PROP_MAX; p++) {
471 log_error("Failed to set locale: %s", strerror(-r));
472 return bus_send_error_reply(connection, message, NULL, r);
475 push_data(connection);
477 log_info("Changed locale information.");
479 changed = bus_properties_changed_new(
480 "/org/freedesktop/locale1",
481 "org.freedesktop.locale1",
488 return bus_default_message_handler(connection, message, INTROSPECTION, INTERFACES_LIST, properties);
490 if (!(reply = dbus_message_new_method_return(message)))
493 if (!dbus_connection_send(connection, reply, NULL))
496 dbus_message_unref(reply);
501 if (!dbus_connection_send(connection, changed, NULL))
504 dbus_message_unref(changed);
507 return DBUS_HANDLER_RESULT_HANDLED;
511 dbus_message_unref(reply);
514 dbus_message_unref(changed);
516 dbus_error_free(&error);
518 return DBUS_HANDLER_RESULT_NEED_MEMORY;
521 static int connect_bus(DBusConnection **_bus) {
522 static const DBusObjectPathVTable locale_vtable = {
523 .message_function = locale_message_handler
526 DBusConnection *bus = NULL;
531 dbus_error_init(&error);
533 bus = dbus_bus_get_private(DBUS_BUS_SYSTEM, &error);
535 log_error("Failed to get system D-Bus connection: %s", bus_error_message(&error));
540 if (!dbus_connection_register_object_path(bus, "/org/freedesktop/locale1", &locale_vtable, NULL)) {
541 log_error("Not enough memory");
546 r = dbus_bus_request_name(bus, "org.freedesktop.locale1", DBUS_NAME_FLAG_DO_NOT_QUEUE, &error);
547 if (dbus_error_is_set(&error)) {
548 log_error("Failed to register name on bus: %s", bus_error_message(&error));
553 if (r != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) {
554 log_error("Failed to acquire name.");
565 dbus_connection_close(bus);
566 dbus_connection_unref(bus);
568 dbus_error_free(&error);
573 int main(int argc, char *argv[]) {
575 DBusConnection *bus = NULL;
577 log_set_target(LOG_TARGET_AUTO);
578 log_parse_environment();
583 if (argc == 2 && streq(argv[1], "--introspect")) {
584 fputs(DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE
586 fputs(locale_interface, stdout);
587 fputs("</node>\n", stdout);
592 log_error("This program takes no arguments.");
599 log_error("Failed to read locale data: %s", strerror(-r));
603 r = connect_bus(&bus);
607 while (dbus_connection_read_write_dispatch(bus, -1))
616 dbus_connection_flush(bus);
617 dbus_connection_close(bus);
618 dbus_connection_unref(bus);
621 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;