Hashmap *polkit_registry;
} Context;
-static int free_and_copy(char **s, const char *v) {
- int r;
- char *t;
-
- assert(s);
-
- r = strdup_or_null(isempty(v) ? NULL : v, &t);
- if (r < 0)
- return r;
-
- free(*s);
- *s = t;
-
- return 0;
+static const char* nonempty(const char *s) {
+ return isempty(s) ? NULL : s;
}
static void free_and_replace(char **s, char *v) {
int p;
for (p = LOCALE_LANG+1; p < _LOCALE_MAX; p++)
- if (isempty(c->locale[p]) || streq_ptr(c->locale[LOCALE_LANG], c->locale[p])) {
- free(c->locale[p]);
- c->locale[p] = NULL;
- }
+ if (isempty(c->locale[p]) || streq_ptr(c->locale[LOCALE_LANG], c->locale[p]))
+ free_and_replace(&c->locale[p], NULL);
}
static int locale_read_data(Context *c) {
for (p = 0; p < _LOCALE_MAX; p++) {
assert(names[p]);
- r = free_and_copy(&c->locale[p], getenv(names[p]));
+ r = free_and_strdup(&c->locale[p],
+ nonempty(getenv(names[p])));
if (r < 0)
return r;
}
}
static int x11_read_data(Context *c) {
- FILE *f;
+ _cleanup_fclose_ FILE *f;
char line[LINE_MAX];
bool in_section = false;
int r;
continue;
if (in_section && first_word(l, "Option")) {
- char **a;
+ _cleanup_strv_free_ char **a = NULL;
r = strv_split_quoted(&a, l);
- if (r < 0) {
- fclose(f);
+ if (r < 0)
return r;
- }
if (strv_length(a) == 3) {
if (streq(a[1], "XkbLayout")) {
}
}
- strv_free(a);
-
} else if (!in_section && first_word(l, "Section")) {
- char **a;
+ _cleanup_strv_free_ char **a = NULL;
r = strv_split_quoted(&a, l);
- if (r < 0) {
- fclose(f);
+ if (r < 0)
return -ENOMEM;
- }
if (strv_length(a) == 2 && streq(a[1], "InputClass"))
in_section = true;
- strv_free(a);
} else if (in_section && first_word(l, "EndSection"))
in_section = false;
}
- fclose(f);
-
return 0;
}
return r < 0 ? r : q < 0 ? q : p;
}
-static int locale_write_data(Context *c) {
+static int locale_write_data(Context *c, char ***settings) {
int r, p;
- char **l = NULL;
+ _cleanup_strv_free_ char **l = NULL;
+
+ /* Set values will be returned as strv in *settings on success. */
r = load_env_file(NULL, "/etc/locale.conf", NULL, &l);
if (r < 0 && r != -ENOENT)
return r;
for (p = 0; p < _LOCALE_MAX; p++) {
- char *t, **u;
+ _cleanup_free_ char *t = NULL;
+ char **u;
assert(names[p]);
continue;
}
- if (asprintf(&t, "%s=%s", names[p], c->locale[p]) < 0) {
- strv_free(l);
+ if (asprintf(&t, "%s=%s", names[p], c->locale[p]) < 0)
return -ENOMEM;
- }
u = strv_env_set(l, t);
- free(t);
- strv_free(l);
-
if (!u)
return -ENOMEM;
+ strv_free(l);
l = u;
}
if (strv_isempty(l)) {
- strv_free(l);
-
if (unlink("/etc/locale.conf") < 0)
return errno == ENOENT ? 0 : -errno;
}
r = write_env_file_label("/etc/locale.conf", l);
- strv_free(l);
+ if (r < 0)
+ return r;
- return r;
+ *settings = l;
+ l = NULL;
+ return 0;
}
static int locale_update_system_manager(Context *c, sd_bus *bus) {
if (isempty(c->vc_keymap))
l = strv_env_unset(l, "KEYMAP");
else {
- char *s, **u;
+ _cleanup_free_ char *s = NULL;
+ char **u;
s = strappend("KEYMAP=", c->vc_keymap);
if (!s)
return -ENOMEM;
u = strv_env_set(l, s);
- free(s);
- strv_free(l);
-
if (!u)
return -ENOMEM;
+ strv_free(l);
l = u;
}
if (isempty(c->vc_keymap_toggle))
l = strv_env_unset(l, "KEYMAP_TOGGLE");
else {
- char *s, **u;
+ _cleanup_free_ char *s = NULL;
+ char **u;
s = strappend("KEYMAP_TOGGLE=", c->vc_keymap_toggle);
if (!s)
return -ENOMEM;
u = strv_env_set(l, s);
- free(s);
- strv_free(l);
-
if (!u)
return -ENOMEM;
+ strv_free(l);
l = u;
}
return 0;
}
- r = write_env_file_label("/etc/vconsole.conf", l);
- return r;
+ return write_env_file_label("/etc/vconsole.conf", l);
}
static int write_data_x11(Context *c) {
return r;
}
-static char *strnulldash(const char *s) {
- return s == NULL || *s == 0 || (s[0] == '-' && s[1] == 0) ? NULL : (char*) s;
+static const char* strnulldash(const char *s) {
+ return isempty(s) || streq(s, "-") ? NULL : s;
}
static int read_next_mapping(FILE *f, unsigned *n, char ***a) {
!streq_ptr(c->x11_variant, strnulldash(a[3])) ||
!streq_ptr(c->x11_options, strnulldash(a[4]))) {
- if (free_and_copy(&c->x11_layout, strnulldash(a[1])) < 0 ||
- free_and_copy(&c->x11_model, strnulldash(a[2])) < 0 ||
- free_and_copy(&c->x11_variant, strnulldash(a[3])) < 0 ||
- free_and_copy(&c->x11_options, strnulldash(a[4])) < 0)
+ if (free_and_strdup(&c->x11_layout, strnulldash(a[1])) < 0 ||
+ free_and_strdup(&c->x11_model, strnulldash(a[2])) < 0 ||
+ free_and_strdup(&c->x11_variant, strnulldash(a[3])) < 0 ||
+ free_and_strdup(&c->x11_options, strnulldash(a[4])) < 0)
return -ENOMEM;
modified = true;
if (r < 0)
log_error("Failed to set X11 keyboard layout: %s", strerror(-r));
+ log_info("Changed X11 keyboard layout to '%s' model '%s' variant '%s' options '%s'",
+ strempty(c->x11_layout),
+ strempty(c->x11_model),
+ strempty(c->x11_variant),
+ strempty(c->x11_options));
+
sd_bus_emit_properties_changed(bus,
"/org/freedesktop/locale1",
"org.freedesktop.locale1",
"X11Layout", "X11Model", "X11Variant", "X11Options", NULL);
- }
+ } else
+ log_debug("X11 keyboard layout was not modified.");
return 0;
}
NULSTR_FOREACH(dir, KBD_KEYMAP_DIRS) {
_cleanup_free_ char *p = NULL, *pz = NULL;
+ bool uncompressed;
p = strjoin(dir, "xkb/", n, ".map", NULL);
pz = strjoin(dir, "xkb/", n, ".map.gz", NULL);
if (!p || !pz)
return -ENOMEM;
- if (access(p, F_OK) == 0 || access(pz, F_OK) == 0) {
+ uncompressed = access(p, F_OK) == 0;
+ if (uncompressed || access(pz, F_OK) == 0) {
+ log_debug("Found converted keymap %s at %s",
+ n, uncompressed ? p : pz);
+
*new_keymap = n;
n = NULL;
return 1;
}
/* The best matching entry so far, then let's save that */
- if (matching > best_matching) {
- best_matching = matching;
+ if (matching >= MAX(best_matching, 1u)) {
+ log_debug("Found legacy keymap %s with score %u",
+ a[0], matching);
- free(*new_keymap);
- *new_keymap = strdup(a[0]);
- if (!*new_keymap)
- return -ENOMEM;
+ if (matching > best_matching) {
+ best_matching = matching;
+
+ r = free_and_strdup(new_keymap, a[0]);
+ if (r < 0)
+ return r;
+ }
}
}
if (r < 0)
log_error("Failed to set virtual console keymap: %s", strerror(-r));
+ log_info("Changed virtual console keymap to '%s' toggle '%s'",
+ strempty(c->vc_keymap), strempty(c->vc_keymap_toggle));
+
sd_bus_emit_properties_changed(bus,
"/org/freedesktop/locale1",
"org.freedesktop.locale1",
"VConsoleKeymap", "VConsoleKeymapToggle", NULL);
return vconsole_reload(bus);
- }
+ } else
+ log_debug("Virtual console keymap was not modified.");
return 0;
}
if (r < 0)
return r;
- /* Check whether a variable changed and if so valid */
+ /* Check whether a variable changed and if it is valid */
STRV_FOREACH(i, l) {
bool valid = false;
}
/* Check whether a variable is unset */
- if (!modified) {
+ if (!modified)
for (p = 0; p < _LOCALE_MAX; p++)
if (!isempty(c->locale[p]) && !passed[p]) {
modified = true;
break;
}
- }
if (modified) {
+ _cleanup_strv_free_ char **settings = NULL;
+
r = bus_verify_polkit_async(m, CAP_SYS_ADMIN, "org.freedesktop.locale1.set-locale", interactive, &c->polkit_registry, error);
if (r < 0)
return r;
if (r == 0)
return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
- STRV_FOREACH(i, l) {
+ STRV_FOREACH(i, l)
for (p = 0; p < _LOCALE_MAX; p++) {
size_t k;
k = strlen(names[p]);
if (startswith(*i, names[p]) && (*i)[k] == '=') {
- char *t;
-
- t = strdup(*i + k + 1);
- if (!t)
- return -ENOMEM;
-
- free(c->locale[p]);
- c->locale[p] = t;
+ r = free_and_strdup(&c->locale[p], *i + k + 1);
+ if (r < 0)
+ return r;
break;
}
}
- }
for (p = 0; p < _LOCALE_MAX; p++) {
if (passed[p])
locale_simplify(c);
- r = locale_write_data(c);
+ r = locale_write_data(c, &settings);
if (r < 0) {
log_error("Failed to set locale: %s", strerror(-r));
return sd_bus_error_set_errnof(error, r, "Failed to set locale: %s", strerror(-r));
locale_update_system_manager(c, bus);
- log_info("Changed locale information.");
+ if (settings) {
+ _cleanup_free_ char *line;
+
+ line = strv_join(settings, ", ");
+ log_info("Changed locale to %s.", strnull(line));
+ } else
+ log_info("Changed locale to unset.");
sd_bus_emit_properties_changed(bus,
"/org/freedesktop/locale1",
"org.freedesktop.locale1",
"Locale", NULL);
- }
+ } else
+ log_debug("Locale settings were not modified.");
+
return sd_bus_reply_method_return(m, NULL);
}
if (r == 0)
return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
- if (free_and_copy(&c->vc_keymap, keymap) < 0 ||
- free_and_copy(&c->vc_keymap_toggle, keymap_toggle) < 0)
+ if (free_and_strdup(&c->vc_keymap, keymap) < 0 ||
+ free_and_strdup(&c->vc_keymap_toggle, keymap_toggle) < 0)
return -ENOMEM;
r = vconsole_write_data(c);
return sd_bus_error_set_errnof(error, r, "Failed to set virtual console keymap: %s", strerror(-r));
}
- log_info("Changed virtual console keymap to '%s'", strempty(c->vc_keymap));
+ log_info("Changed virtual console keymap to '%s' toggle '%s'",
+ strempty(c->vc_keymap), strempty(c->vc_keymap_toggle));
r = vconsole_reload(bus);
if (r < 0)
if (r == 0)
return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
- if (free_and_copy(&c->x11_layout, layout) < 0 ||
- free_and_copy(&c->x11_model, model) < 0 ||
- free_and_copy(&c->x11_variant, variant) < 0 ||
- free_and_copy(&c->x11_options, options) < 0)
+ if (free_and_strdup(&c->x11_layout, layout) < 0 ||
+ free_and_strdup(&c->x11_model, model) < 0 ||
+ free_and_strdup(&c->x11_variant, variant) < 0 ||
+ free_and_strdup(&c->x11_options, options) < 0)
return -ENOMEM;
r = write_data_x11(c);
return sd_bus_error_set_errnof(error, r, "Failed to set X11 keyboard layout: %s", strerror(-r));
}
- log_info("Changed X11 keyboard layout to '%s'", strempty(c->x11_layout));
+ log_info("Changed X11 keyboard layout to '%s' model '%s' variant '%s' options '%s'",
+ strempty(c->x11_layout),
+ strempty(c->x11_model),
+ strempty(c->x11_variant),
+ strempty(c->x11_options));
sd_bus_emit_properties_changed(bus,
"/org/freedesktop/locale1",
"org.freedesktop.locale1",
- "X11Layout" "X11Model" "X11Variant" "X11Options", NULL);
+ "X11Layout", "X11Model", "X11Variant", "X11Options", NULL);
if (convert) {
r = x11_convert_to_vconsole(c, bus);
}
int main(int argc, char *argv[]) {
- Context context = {};
+ _cleanup_(context_free) Context context = {};
_cleanup_event_unref_ sd_event *event = NULL;
_cleanup_bus_close_unref_ sd_bus *bus = NULL;
int r;
}
finish:
- context_free(&context);
-
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}