localed.c revision 0d0f0c50d3a1d90f03972a6abb82e6413daaa583
/*-*- 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 Lesser General Public License as published by
the Free Software Foundation; either version 2.1 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser 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 "mkdir.h"
#include "strv.h"
#include "dbus-common.h"
#include "polkit.h"
#include "def.h"
#define INTERFACE \
" <interface name=\"org.freedesktop.locale1\">\n" \
" <property name=\"Locale\" type=\"as\" access=\"read\"/>\n" \
" <property name=\"VConsoleKeymap\" type=\"s\" access=\"read\"/>\n" \
" <property name=\"VConsoleKeymapToggle\" type=\"s\" access=\"read\"/>\n" \
" <property name=\"X11Layout\" type=\"s\" access=\"read\"/>\n" \
" <property name=\"X11Model\" type=\"s\" access=\"read\"/>\n" \
" <property name=\"X11Variant\" type=\"s\" access=\"read\"/>\n" \
" <property name=\"X11Options\" type=\"s\" 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" \
" <method name=\"SetVConsoleKeyboard\">\n" \
" <arg name=\"keymap\" type=\"s\" direction=\"in\"/>\n" \
" <arg name=\"keymap_toggle\" type=\"s\" direction=\"in\"/>\n" \
" <arg name=\"convert\" type=\"b\" direction=\"in\"/>\n" \
" <arg name=\"user_interaction\" type=\"b\" direction=\"in\"/>\n" \
" </method>\n" \
" <method name=\"SetX11Keyboard\">\n" \
" <arg name=\"layout\" type=\"s\" direction=\"in\"/>\n" \
" <arg name=\"model\" type=\"s\" direction=\"in\"/>\n" \
" <arg name=\"variant\" type=\"s\" direction=\"in\"/>\n" \
" <arg name=\"options\" type=\"s\" direction=\"in\"/>\n" \
" <arg name=\"convert\" type=\"b\" direction=\"in\"/>\n" \
" <arg name=\"user_interaction\" type=\"b\" direction=\"in\"/>\n" \
" </method>\n" \
" </interface>\n"
#define INTROSPECTION \
"<node>\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_LANGUAGE] = "LANGUAGE",
[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,
};
typedef struct State {
char *vc_keymap, *vc_keymap_toggle;
} State;
static usec_t remain_until = 0;
static int free_and_set(char **s, const char *v) {
int r;
char *t;
assert(s);
if (r < 0)
return r;
free(*s);
*s = t;
return 0;
}
static void free_data_locale(void) {
int p;
for (p = 0; p < _PROP_MAX; p++) {
}
}
static void free_data_x11(void) {
}
static void free_data_vconsole(void) {
}
static void simplify(void) {
int p;
for (p = 1; p < _PROP_MAX; p++)
}
}
static int read_data_locale(void) {
int r;
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 void free_data(void) {
}
static int read_data_vconsole(void) {
int r;
NULL);
if (r < 0 && r != -ENOENT)
return r;
return 0;
}
static int read_data_x11(void) {
FILE *f;
bool in_section = false;
if (!f) {
#ifdef TARGET_FEDORA
if (!f) {
return 0;
else
return -errno;
}
#else
return 0;
#endif
} else
return -errno;
}
char *l;
if (l[0] == 0 || l[0] == '#')
continue;
char **a;
a = strv_split_quoted(l);
if (!a) {
fclose(f);
return -ENOMEM;
}
if (strv_length(a) == 3) {
a[2] = NULL;
a[2] = NULL;
a[2] = NULL;
a[2] = NULL;
}
}
strv_free(a);
char **a;
a = strv_split_quoted(l);
if (!a) {
fclose(f);
return -ENOMEM;
}
in_section = true;
strv_free(a);
in_section = false;
}
fclose(f);
return 0;
}
static int read_data(void) {
int r, q, p;
r = read_data_locale();
q = read_data_vconsole();
p = read_data_x11();
return r < 0 ? r : q < 0 ? q : p;
}
static int write_data_locale(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)) {
strv_free(l);
if (unlink("/etc/locale.conf") < 0)
return 0;
}
r = write_env_file("/etc/locale.conf", l);
strv_free(l);
return r;
}
log_oom();
goto finish;
}
for (p = 0; p < _PROP_MAX; p++) {
else {
char *s;
log_oom();
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_oom();
goto finish;
}
STRV_FOREACH(t, l_unset)
log_oom();
goto finish;
}
log_oom();
goto finish;
}
STRV_FOREACH(t, l_set)
log_oom();
goto finish;
}
log_oom();
goto finish;
}
if (!reply) {
goto finish;
}
if (m)
if (reply)
}
static int write_data_vconsole(void) {
int r;
char **l = NULL;
r = load_env_file("/etc/vconsole.conf", &l);
if (r < 0 && r != -ENOENT)
return r;
l = strv_env_unset(l, "KEYMAP");
else {
char *s, **u;
if (!s) {
strv_free(l);
return -ENOMEM;
}
u = strv_env_set(l, s);
free(s);
strv_free(l);
if (!u)
return -ENOMEM;
l = u;
}
l = strv_env_unset(l, "KEYMAP_TOGGLE");
else {
char *s, **u;
if (!s) {
strv_free(l);
return -ENOMEM;
}
u = strv_env_set(l, s);
free(s);
strv_free(l);
if (!u)
return -ENOMEM;
l = u;
}
if (strv_isempty(l)) {
strv_free(l);
if (unlink("/etc/vconsole.conf") < 0)
return 0;
}
r = write_env_file("/etc/vconsole.conf", l);
strv_free(l);
return r;
}
static int write_data_x11(void) {
FILE *f;
char *temp_path;
int r;
#ifdef TARGET_FEDORA
* still running) doesn't recreate this. */
#endif
if (unlink("/etc/X11/xorg.conf.d/00-keyboard.conf") < 0)
return 0;
}
if (r < 0)
return r;
fputs("# Read and parsed by systemd-localed. It's probably wise not to edit this file\n"
"# manually too freely.\n"
"Section \"InputClass\"\n"
" Identifier \"system-keyboard\"\n"
" MatchIsKeyboard \"on\"\n", f);
fputs("EndSection\n", f);
fflush(f);
r = -errno;
unlink("/etc/X11/xorg.conf.d/00-keyboard.conf");
} else {
#ifdef TARGET_FEDORA
* still running) doesn't recreate this. */
#endif
r = 0;
}
fclose(f);
return r;
}
int r;
if (!error) {
}
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
"RestartUnit");
if (!m) {
log_error("Could not allocate message.");
r = -ENOMEM;
goto finish;
}
if (!dbus_message_append_args(m,
log_error("Could not append arguments to message.");
r = -ENOMEM;
goto finish;
}
if (!reply) {
r = -EIO;
goto finish;
}
r = 0;
if (m)
if (reply)
return r;
}
static char *strnulldash(const char *s) {
}
static int read_next_mapping(FILE *f, unsigned *n, char ***a) {
assert(f);
assert(n);
assert(a);
for (;;) {
char *l, **b;
errno = 0;
if (ferror(f))
return 0;
}
(*n) ++;
if (l[0] == 0 || l[0] == '#')
continue;
b = strv_split_quoted(l);
if (!b)
return -ENOMEM;
if (strv_length(b) < 5) {
strv_free(b);
continue;
}
*a = b;
return 1;
}
}
bool modified = false;
modified =
} else {
FILE *f;
unsigned n = 0;
if (!f)
return -errno;
for (;;) {
char **a;
int r;
r = read_next_mapping(f, &n, &a);
if (r < 0) {
fclose(f);
return r;
}
if (r == 0)
break;
strv_free(a);
continue;
}
strv_free(a);
fclose(f);
return -ENOMEM;
}
modified = true;
}
strv_free(a);
break;
}
fclose(f);
}
if (modified) {
dbus_bool_t b;
int r;
r = write_data_x11();
if (r < 0)
"/org/freedesktop/locale1",
"org.freedesktop.locale1",
"X11Layout\0"
"X11Model\0"
"X11Variant\0"
"X11Options\0");
if (!changed)
return -ENOMEM;
if (!b)
return -ENOMEM;
}
return 0;
}
bool modified = false;
modified =
} else {
FILE *f;
unsigned n = 0;
unsigned best_matching = 0;
char *new_keymap = NULL;
if (!f)
return -errno;
for (;;) {
char **a;
unsigned matching = 0;
int r;
r = read_next_mapping(f, &n, &a);
if (r < 0) {
fclose(f);
return r;
}
if (r == 0)
break;
/* Determine how well matching this entry is */
/* If we got an exact match, this is best */
matching = 10;
else {
size_t x;
/* We have multiple X layouts, look
* for an entry that matches our key
* with the everything but the first
* layout stripped off. */
if (x > 0 &&
strlen(a[1]) == x &&
matching = 5;
else {
size_t w;
/* If that didn't work, strip
* off the other layouts from
* the entry, too */
if (x > 0 && x == w &&
matching = 1;
}
}
if (matching > 0 &&
matching++;
matching++;
matching++;
}
}
/* The best matching entry so far, then let's
* save that */
if (matching > best_matching) {
new_keymap = strdup(a[0]);
if (!new_keymap) {
strv_free(a);
fclose(f);
return -ENOMEM;
}
}
strv_free(a);
}
fclose(f);
modified = true;
} else
}
if (modified) {
dbus_bool_t b;
int r;
r = write_data_vconsole();
if (r < 0)
"/org/freedesktop/locale1",
"org.freedesktop.locale1",
"VConsoleKeymap\0"
"VConsoleKeymapToggle\0");
if (!changed)
return -ENOMEM;
if (!b)
return -ENOMEM;
}
return 0;
}
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;
}
static const BusProperty bus_locale_properties[] = {
{ "VConsoleKeymapToggle", bus_property_append_string, "s", offsetof(State, vc_keymap_toggle), true },
{ NULL, }
};
static const BusBoundProperties bps[] = {
{ NULL, }
};
void *userdata) {
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) {
r = verify_polkit(connection, message, "org.freedesktop.locale1.set-locale", interactive, NULL, &error);
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;
}
}
}
strv_free(l);
for (p = 0; p < _PROP_MAX; p++) {
if (passed[p])
continue;
}
simplify();
r = write_data_locale();
if (r < 0) {
}
log_info("Changed locale information.");
"/org/freedesktop/locale1",
"org.freedesktop.locale1",
"Locale\0");
if (!changed)
goto oom;
}
} else if (dbus_message_is_method_call(message, "org.freedesktop.locale1", "SetVConsoleKeyboard")) {
const char *keymap, *keymap_toggle;
if (!dbus_message_get_args(
&error,
if (isempty(keymap_toggle))
r = verify_polkit(connection, message, "org.freedesktop.locale1.set-keyboard", interactive, NULL, &error);
if (r < 0)
goto oom;
r = write_data_vconsole();
if (r < 0) {
}
if (r < 0)
"/org/freedesktop/locale1",
"org.freedesktop.locale1",
"VConsoleKeymap\0"
"VConsoleKeymapToggle\0");
if (!changed)
goto oom;
if (convert) {
if (r < 0)
}
}
if (!dbus_message_get_args(
&error,
r = verify_polkit(connection, message, "org.freedesktop.locale1.set-keyboard", interactive, NULL, &error);
if (r < 0)
goto oom;
r = write_data_x11();
if (r < 0) {
}
"/org/freedesktop/locale1",
"org.freedesktop.locale1",
"X11Layout\0"
"X11Model\0"
"X11Variant\0"
"X11Options\0");
if (!changed)
goto oom;
if (convert) {
if (r < 0)
}
}
} else
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;
}
r = log_oom();
goto fail;
}
if (dbus_error_is_set(&error)) {
r = -EEXIST;
goto fail;
}
if (r != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) {
log_error("Failed to acquire name.");
r = -EEXIST;
goto fail;
}
if (_bus)
return 0;
fail:
return r;
}
int r;
bool exiting = false;
log_open();
umask(0022);
"<node>\n", stdout);
return 0;
}
if (argc != 1) {
log_error("This program takes no arguments.");
r = -EINVAL;
goto finish;
}
r = read_data();
if (r < 0) {
goto finish;
}
r = connect_bus(&bus);
if (r < 0)
goto finish;
for (;;) {
if (!dbus_connection_read_write_dispatch(bus, exiting ? -1 : (int) (DEFAULT_EXIT_USEC/USEC_PER_MSEC)))
break;
exiting = true;
}
}
r = 0;
free_data();
if (bus) {
}
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}