localed.c revision f401e48c2db22ff9d1a05885b5599bebf19c2707
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2011 Lennart Poettering
under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include "util.h"
#include "strv.h"
#include "dbus-common.h"
#include "polkit.h"
#define INTROSPECTION \
"<node>\n" \
" <interface name=\"org.freedesktop.locale1\">\n" \
" <property name=\"Locale\" type=\"as\" access=\"read\"/>\n" \
" <method name=\"SetLocale\">\n" \
" <arg name=\"locale\" type=\"as\" direction=\"in\"/>\n" \
" <arg name=\"user_interaction\" type=\"b\" direction=\"in\"/>\n" \
" </method>\n" \
" </interface>\n" \
"</node>\n"
#define INTERFACES_LIST \
"org.freedesktop.locale1\0"
enum {
/* We don't list LC_ALL here on purpose. People should be
* using LANG instead. */
};
[PROP_LANG] = "LANG",
[PROP_LC_CTYPE] = "LC_CTYPE",
[PROP_LC_NUMERIC] = "LC_NUMERIC",
[PROP_LC_TIME] = "LC_TIME",
[PROP_LC_COLLATE] = "LC_COLLATE",
[PROP_LC_MONETARY] = "LC_MONETARY",
[PROP_LC_MESSAGES] = "LC_MESSAGES",
[PROP_LC_PAPER] = "LC_PAPER",
[PROP_LC_NAME] = "LC_NAME",
[PROP_LC_ADDRESS] = "LC_ADDRESS",
[PROP_LC_TELEPHONE] = "LC_TELEPHONE",
[PROP_LC_MEASUREMENT] = "LC_MEASUREMENT",
[PROP_LC_IDENTIFICATION] = "LC_IDENTIFICATION"
};
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
};
static void free_data(void) {
int p;
for (p = 0; p < _PROP_MAX; p++) {
}
}
static void simplify(void) {
int p;
for (p = 1; p < _PROP_MAX; p++)
}
}
static int read_data(void) {
int r;
free_data();
NULL);
if (r == -ENOENT) {
int p;
/* Fill in what we got passed from systemd. */
for (p = 0; p < _PROP_MAX; p++) {
char *e, *d;
if (e) {
d = strdup(e);
if (!d)
return -ENOMEM;
} else
d = NULL;
data[p] = d;
}
r = 0;
}
simplify();
return r;
}
static int write_data(void) {
int r, p;
char **l = NULL;
r = load_env_file("/etc/locale.conf", &l);
if (r < 0 && r != -ENOENT)
return r;
for (p = 0; p < _PROP_MAX; p++) {
char *t, **u;
l = strv_env_unset(l, names[p]);
continue;
}
strv_free(l);
return -ENOMEM;
}
u = strv_env_set(l, t);
free(t);
strv_free(l);
if (!u)
return -ENOMEM;
l = u;
}
if (strv_isempty(l)) {
if (unlink("/etc/locale.conf") < 0)
return 0;
}
r = write_env_file("/etc/locale.conf", l);
strv_free(l);
return r;
}
log_error("Out of memory");
goto finish;
}
for (p = 0; p < _PROP_MAX; p++) {
else {
char *s;
log_error("Out of memory");
goto finish;
}
}
}
m = dbus_message_new_method_call("org.freedesktop.systemd1", "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "UnsetAndSetEnvironment");
if (!m) {
log_error("Could not allocate message.");
goto finish;
}
log_error("Out of memory.");
goto finish;
}
STRV_FOREACH(t, l_unset)
log_error("Out of memory.");
goto finish;
}
log_error("Out of memory.");
goto finish;
}
STRV_FOREACH(t, l_set)
log_error("Out of memory.");
goto finish;
}
log_error("Out of memory.");
goto finish;
}
if (!reply) {
goto finish;
}
if (m)
if (reply)
}
int r, c = 0, p;
char **l;
if (!l)
return -ENOMEM;
for (p = 0; p < _PROP_MAX; p++) {
char *t;
continue;
strv_free(l);
return -ENOMEM;
}
l[c++] = t;
}
r = bus_property_append_strv(i, property, (void*) l);
strv_free(l);
return r;
}
void *userdata) {
const BusProperty properties[] = {
};
int r;
char **l = NULL, **i;
bool modified = false;
int p;
r = bus_parse_strv_iter(&iter, &l);
if (r < 0) {
if (r == -ENOMEM)
goto oom;
}
if (!dbus_message_iter_next(&iter) ||
strv_free(l);
}
/* Check whether a variable changed and if so valid */
STRV_FOREACH(i, l) {
bool valid = false;
for (p = 0; p < _PROP_MAX; p++) {
size_t k;
valid = true;
passed[p] = true;
modified = true;
break;
}
}
if (!valid) {
strv_free(l);
}
}
/* Check whether a variable is unset */
if (!modified) {
for (p = 0; p < _PROP_MAX; p++)
modified = true;
break;
}
}
if (modified) {
if (r < 0) {
strv_free(l);
}
STRV_FOREACH(i, l) {
for (p = 0; p < _PROP_MAX; p++) {
size_t k;
char *t;
t = strdup(*i + k + 1);
if (!t) {
strv_free(l);
goto oom;
}
data[p] = t;
break;
}
}
}
for (p = 0; p < _PROP_MAX; p++) {
if (passed[p])
continue;
}
simplify();
r = write_data();
if (r < 0) {
}
log_info("Changed locale information.");
"/org/freedesktop/locale1",
"org.freedesktop.locale1",
"Locale\0");
if (!changed)
goto oom;
}
} else
return bus_default_message_handler(connection, message, INTROSPECTION, INTERFACES_LIST, properties);
goto oom;
goto oom;
if (changed) {
goto oom;
}
return DBUS_HANDLER_RESULT_HANDLED;
oom:
if (reply)
if (changed)
return DBUS_HANDLER_RESULT_NEED_MEMORY;
}
static const DBusObjectPathVTable locale_vtable = {
};
int r;
if (!bus) {
r = -ECONNREFUSED;
goto fail;
}
log_error("Not enough memory");
r = -ENOMEM;
goto fail;
}
if (dbus_bus_request_name(bus, "org.freedesktop.locale1", DBUS_NAME_FLAG_DO_NOT_QUEUE, &error) < 0) {
r = -EEXIST;
goto fail;
}
if (_bus)
return 0;
fail:
return r;
}
int r;
log_open();
if (argc != 1) {
log_error("This program takes no arguments.");
r = -EINVAL;
goto finish;
}
umask(0022);
r = read_data();
if (r < 0) {
goto finish;
}
r = connect_bus(&bus);
if (r < 0)
goto finish;
;
r = 0;
free_data();
if (bus) {
}
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}