localectl.c revision 82c1d8f4eb74ddd9be2c9b9b56d9dc564c599eff
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2012 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 <locale.h>
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
#include <getopt.h>
#include <string.h>
#include <ftw.h>
#include <fcntl.h>
#include "dbus-common.h"
#include "util.h"
#include "spawn-polkit-agent.h"
#include "build.h"
#include "strv.h"
#include "pager.h"
#include "set.h"
#include "path-util.h"
static bool arg_no_pager = false;
static enum transport {
static bool arg_ask_password = true;
static bool arg_convert = true;
static void pager_open_if_enabled(void) {
if (arg_no_pager)
return;
pager_open();
}
static void polkit_agent_open_if_enabled(void) {
/* Open the polkit agent as a child process if necessary */
if (!arg_ask_password)
return;
}
typedef struct StatusInfo {
char **locale;
const char *vconsole_keymap;
const char *vconsole_keymap_toggle;
const char *x11_layout;
const char *x11_model;
const char *x11_variant;
const char *x11_options;
} StatusInfo;
static void print_status_info(StatusInfo *i) {
assert(i);
if (strv_isempty(i->locale))
puts(" System Locale: n/a\n");
else {
char **j;
printf(" %s\n", *j);
}
if (!isempty(i->vconsole_keymap_toggle))
if (!isempty(i->x11_variant))
if (!isempty(i->x11_options))
}
int r;
switch (dbus_message_iter_get_arg_type(iter)) {
case DBUS_TYPE_STRING: {
const char *s;
if (!isempty(s)) {
i->vconsole_keymap = s;
i->vconsole_keymap_toggle = s;
i->x11_layout = s;
i->x11_model = s;
i->x11_variant = s;
i->x11_options = s;
}
break;
}
case DBUS_TYPE_ARRAY:
char **l;
r = bus_parse_strv_iter(iter, &l);
if (r < 0)
return r;
i->locale = l;
l = NULL;
}
strv_free(l);
}
}
return 0;
}
const char *interface = "";
int r;
bus,
"org.freedesktop.locale1",
"/org/freedesktop/locale1",
"org.freedesktop.DBus.Properties",
"GetAll",
&reply,
NULL,
if (r < 0)
return r;
log_error("Failed to parse reply.");
return -EIO;
}
const char *name;
log_error("Failed to parse reply.");
return -EIO;
}
log_error("Failed to parse reply.");
return -EIO;
}
log_error("Failed to parse reply.");
return -EIO;
}
if (r < 0) {
log_error("Failed to parse reply.");
return r;
}
}
return 0;
}
dbus_bool_t interactive = true;
int r;
"org.freedesktop.locale1",
"/org/freedesktop/locale1",
"org.freedesktop.locale1",
"SetLocale");
if (!m)
return log_oom();
if (r < 0)
return log_oom();
return log_oom();
if (!reply) {
r = -EIO;
goto finish;
}
r = 0;
return r;
}
/* Stolen from glibc... */
struct locarhead {
/* Serial number. */
/* Name hash table. */
/* String table. */
/* Table with locale records. */
/* MD5 sum hash table. */
};
struct namehashent {
/* Hash value of the name. */
/* Offset of the name in the string table. */
/* Offset of the locale record. */
};
const struct locarhead *h;
const struct namehashent *e;
const void *p = MAP_FAILED;
unsigned i;
int r;
if (fd < 0) {
log_error("Failed to open locale archive: %m");
r = -errno;
goto finish;
}
log_error("fstat() failed: %m");
r = -errno;
goto finish;
}
log_error("Archive file is not regular");
r = -EBADMSG;
goto finish;
}
log_error("Archive has invalid size");
r = -EBADMSG;
goto finish;
}
if (p == MAP_FAILED) {
log_error("Failed to map archive: %m");
r = -errno;
goto finish;
}
h = (const struct locarhead *) p;
if (h->magic != 0xde020109 ||
log_error("Invalid archive file.");
r = -EBADMSG;
goto finish;
}
for (i = 0; i < h->namehash_size; i++) {
char *z;
if (e[i].locrec_offset == 0)
continue;
z = strdup((char*) p + e[i].name_offset);
if (!z) {
r = log_oom();
goto finish;
}
if (r < 0) {
free(z);
goto finish;
}
}
r = 0;
if (p != MAP_FAILED)
return r;
}
int r;
if (!dir) {
log_error("Failed to open locale directory: %m");
return -errno;
}
errno = 0;
char *z;
continue;
continue;
if (!z)
return log_oom();
if (r < 0) {
free(z);
if (r != -EEXIST) {
return r;
}
}
errno = 0;
}
if (errno != 0) {
log_error("Failed to read locale directory: %m");
return -errno;
}
return 0;
}
_cleanup_strv_free_ char **l = NULL;
int r;
if (!locales)
return log_oom();
if (r < 0 && r != -ENOENT)
return r;
if (r < 0)
return r;
l = set_get_strv(locales);
if (!l)
return log_oom();
strv_sort(l);
strv_print(l);
return 0;
}
dbus_bool_t interactive = true, b;
const char *map, *toggle_map;
if (n > 3) {
log_error("Too many arguments.");
return -EINVAL;
}
b = arg_convert;
return bus_method_call_with_reply(
bus,
"org.freedesktop.locale1",
"/org/freedesktop/locale1",
"org.freedesktop.locale1",
"SetVConsoleKeyboard",
&reply,
NULL,
DBUS_TYPE_BOOLEAN, &b,
}
static int nftw_cb(
const char *fpath,
int tflag,
char *p, *e;
int r;
return 0;
return 0;
if (!p)
return log_oom();
e = endswith(p, ".map");
if (e)
*e = 0;
e = endswith(p, ".map.gz");
if (e)
*e = 0;
if (r == -EEXIST)
free(p);
else if (r < 0) {
free(p);
return r;
}
return 0;
}
char _cleanup_strv_free_ **l = NULL;
if (!keymaps)
return log_oom();
l = set_get_strv(keymaps);
if (!l) {
return log_oom();
}
if (strv_isempty(l)) {
log_error("Couldn't find any console keymaps.");
return -ENOENT;
}
strv_sort(l);
strv_print(l);
return 0;
}
dbus_bool_t interactive = true, b;
if (n > 5) {
log_error("Too many arguments.");
return -EINVAL;
}
b = arg_convert;
return bus_method_call_with_reply(
bus,
"org.freedesktop.locale1",
"/org/freedesktop/locale1",
"org.freedesktop.locale1",
"SetX11Keyboard",
&reply,
NULL,
DBUS_TYPE_BOOLEAN, &b,
}
static int help(void) {
printf("%s [OPTIONS...] COMMAND ...\n\n"
"Query or change system locale and keyboard settings.\n\n"
" -h --help Show this help\n"
" --version Show package version\n"
" --no-convert Don't convert keyboard mappings\n"
" --no-pager Do not pipe output into a pager\n"
" --no-ask-password Do not prompt for password\n"
" -H --host=[USER@]HOST Operate on remote host\n\n"
"Commands:\n"
" status Show current locale settings\n"
" set-locale LOCALE... Set system locale\n"
" list-locales Show known locales\n"
" set-keymap MAP [MAP] Set virtual console keyboard mapping\n"
" list-keymaps Show known virtual console keyboard mappings\n"
" set-x11-keymap LAYOUT [MODEL] [VARIANT] [OPTIONS]\n"
" Set X11 keyboard mapping\n",
return 0;
}
enum {
ARG_VERSION = 0x100,
};
};
int c;
switch (c) {
case 'h':
help();
return 0;
case ARG_VERSION:
return 0;
case 'P':
break;
case 'H':
break;
case ARG_NO_CONVERT:
arg_convert = false;
break;
case ARG_NO_PAGER:
arg_no_pager = true;
break;
case '?':
return -EINVAL;
default:
log_error("Unknown option code %c", c);
return -EINVAL;
}
}
return 1;
}
static const struct {
const char* verb;
const enum {
MORE,
LESS,
} argc_cmp;
const int argc;
} verbs[] = {
};
int left;
unsigned i;
if (left <= 0)
/* Special rule: no arguments means "status" */
i = 0;
else {
help();
return 0;
}
for (i = 0; i < ELEMENTSOF(verbs); i++)
break;
if (i >= ELEMENTSOF(verbs)) {
return -EINVAL;
}
}
case EQUAL:
log_error("Invalid number of arguments.");
return -EINVAL;
}
break;
case MORE:
log_error("Too few arguments.");
return -EINVAL;
}
break;
case LESS:
log_error("Too many arguments.");
return -EINVAL;
}
break;
default:
assert_not_reached("Unknown comparison operator.");
}
if (!bus) {
return -EIO;
}
}
int r, retval = EXIT_FAILURE;
log_open();
if (r < 0)
goto finish;
else if (r == 0) {
goto finish;
}
if (arg_transport == TRANSPORT_NORMAL)
else if (arg_transport == TRANSPORT_POLKIT)
else if (arg_transport == TRANSPORT_SSH)
else
assert_not_reached("Uh, invalid transport...");
retval = r < 0 ? EXIT_FAILURE : r;
if (bus) {
}
pager_close();
return retval;
}