/* SPDX-License-Identifier: LGPL-2.1+ */
-/***
- This file is part of systemd.
-
- Copyright 2011 Lennart Poettering
- Copyright 2013 Kay Sievers
-***/
#include <errno.h>
-#include <stdio_ext.h>
-#include <string.h>
+#include <sys/stat.h>
+#include <sys/types.h>
#include <unistd.h>
-#include "def.h"
+#include "bus-polkit.h"
+#include "env-file-label.h"
+#include "env-file.h"
#include "env-util.h"
#include "fd-util.h"
#include "fileio-label.h"
#include "fileio.h"
+#include "kbd-util.h"
#include "keymap-util.h"
#include "locale-util.h"
#include "macro.h"
#include "mkdir.h"
+#include "nulstr-util.h"
#include "string-util.h"
#include "strv.h"
+#include "tmpfile-util.h"
static bool startswith_comma(const char *s, const char *prefix) {
s = startswith(s, prefix);
return IN_SET(*s, ',', '\0');
}
-static const char* strnulldash(const char *s) {
- return isempty(s) || streq(s, "-") ? NULL : s;
-}
-
static const char* systemd_kbd_model_map(void) {
const char* s;
c->locale[p] = mfree(c->locale[p]);
}
-void context_free(Context *c) {
+void context_clear(Context *c) {
context_free_locale(c);
context_free_x11(c);
context_free_vconsole(c);
+
+ sd_bus_message_unref(c->locale_cache);
+ sd_bus_message_unref(c->x11_cache);
+ sd_bus_message_unref(c->vc_cache);
+
+ bus_verify_polkit_async_registry_free(c->polkit_registry);
};
-void locale_simplify(Context *c) {
+void locale_simplify(char *locale[_VARIABLE_LC_MAX]) {
int p;
for (p = VARIABLE_LANG+1; p < _VARIABLE_LC_MAX; p++)
- if (isempty(c->locale[p]) || streq_ptr(c->locale[VARIABLE_LANG], c->locale[p]))
- c->locale[p] = mfree(c->locale[p]);
+ if (isempty(locale[p]) || streq_ptr(locale[VARIABLE_LANG], locale[p]))
+ locale[p] = mfree(locale[p]);
}
-static int locale_read_data(Context *c) {
+int locale_read_data(Context *c, sd_bus_message *m) {
+ struct stat st;
int r;
- context_free_locale(c);
+ /* Do not try to re-read the file within single bus operation. */
+ if (m) {
+ if (m == c->locale_cache)
+ return 0;
+
+ sd_bus_message_unref(c->locale_cache);
+ c->locale_cache = sd_bus_message_ref(m);
+ }
+
+ r = stat("/etc/locale.conf", &st);
+ if (r < 0 && errno != ENOENT)
+ return -errno;
+
+ if (r >= 0) {
+ usec_t t;
- r = parse_env_file(NULL, "/etc/locale.conf", NEWLINE,
- "LANG", &c->locale[VARIABLE_LANG],
- "LANGUAGE", &c->locale[VARIABLE_LANGUAGE],
- "LC_CTYPE", &c->locale[VARIABLE_LC_CTYPE],
- "LC_NUMERIC", &c->locale[VARIABLE_LC_NUMERIC],
- "LC_TIME", &c->locale[VARIABLE_LC_TIME],
- "LC_COLLATE", &c->locale[VARIABLE_LC_COLLATE],
- "LC_MONETARY", &c->locale[VARIABLE_LC_MONETARY],
- "LC_MESSAGES", &c->locale[VARIABLE_LC_MESSAGES],
- "LC_PAPER", &c->locale[VARIABLE_LC_PAPER],
- "LC_NAME", &c->locale[VARIABLE_LC_NAME],
- "LC_ADDRESS", &c->locale[VARIABLE_LC_ADDRESS],
- "LC_TELEPHONE", &c->locale[VARIABLE_LC_TELEPHONE],
- "LC_MEASUREMENT", &c->locale[VARIABLE_LC_MEASUREMENT],
- "LC_IDENTIFICATION", &c->locale[VARIABLE_LC_IDENTIFICATION],
- NULL);
-
- if (r == -ENOENT) {
+ /* If mtime is not changed, then we do not need to re-read the file. */
+ t = timespec_load(&st.st_mtim);
+ if (c->locale_mtime != USEC_INFINITY && t == c->locale_mtime)
+ return 0;
+
+ c->locale_mtime = t;
+ context_free_locale(c);
+
+ r = parse_env_file(NULL, "/etc/locale.conf",
+ "LANG", &c->locale[VARIABLE_LANG],
+ "LANGUAGE", &c->locale[VARIABLE_LANGUAGE],
+ "LC_CTYPE", &c->locale[VARIABLE_LC_CTYPE],
+ "LC_NUMERIC", &c->locale[VARIABLE_LC_NUMERIC],
+ "LC_TIME", &c->locale[VARIABLE_LC_TIME],
+ "LC_COLLATE", &c->locale[VARIABLE_LC_COLLATE],
+ "LC_MONETARY", &c->locale[VARIABLE_LC_MONETARY],
+ "LC_MESSAGES", &c->locale[VARIABLE_LC_MESSAGES],
+ "LC_PAPER", &c->locale[VARIABLE_LC_PAPER],
+ "LC_NAME", &c->locale[VARIABLE_LC_NAME],
+ "LC_ADDRESS", &c->locale[VARIABLE_LC_ADDRESS],
+ "LC_TELEPHONE", &c->locale[VARIABLE_LC_TELEPHONE],
+ "LC_MEASUREMENT", &c->locale[VARIABLE_LC_MEASUREMENT],
+ "LC_IDENTIFICATION", &c->locale[VARIABLE_LC_IDENTIFICATION]);
+ if (r < 0)
+ return r;
+ } else {
int p;
+ c->locale_mtime = USEC_INFINITY;
+ context_free_locale(c);
+
/* Fill in what we got passed from systemd. */
for (p = 0; p < _VARIABLE_LC_MAX; p++) {
const char *name;
if (r < 0)
return r;
}
-
- r = 0;
}
- locale_simplify(c);
- return r;
+ locale_simplify(c->locale);
+ return 0;
}
-static int vconsole_read_data(Context *c) {
+int vconsole_read_data(Context *c, sd_bus_message *m) {
+ struct stat st;
+ usec_t t;
int r;
+ /* Do not try to re-read the file within single bus operation. */
+ if (m) {
+ if (m == c->vc_cache)
+ return 0;
+
+ sd_bus_message_unref(c->vc_cache);
+ c->vc_cache = sd_bus_message_ref(m);
+ }
+
+ if (stat("/etc/vconsole.conf", &st) < 0) {
+ if (errno != ENOENT)
+ return -errno;
+
+ c->vc_mtime = USEC_INFINITY;
+ context_free_vconsole(c);
+ return 0;
+ }
+
+ /* If mtime is not changed, then we do not need to re-read */
+ t = timespec_load(&st.st_mtim);
+ if (c->vc_mtime != USEC_INFINITY && t == c->vc_mtime)
+ return 0;
+
+ c->vc_mtime = t;
context_free_vconsole(c);
- r = parse_env_file(NULL, "/etc/vconsole.conf", NEWLINE,
+ r = parse_env_file(NULL, "/etc/vconsole.conf",
"KEYMAP", &c->vc_keymap,
- "KEYMAP_TOGGLE", &c->vc_keymap_toggle,
- NULL);
-
- if (r < 0 && r != -ENOENT)
+ "KEYMAP_TOGGLE", &c->vc_keymap_toggle);
+ if (r < 0)
return r;
return 0;
}
-static int x11_read_data(Context *c) {
- _cleanup_fclose_ FILE *f;
- char line[LINE_MAX];
+int x11_read_data(Context *c, sd_bus_message *m) {
+ _cleanup_fclose_ FILE *f = NULL;
bool in_section = false;
+ struct stat st;
+ usec_t t;
int r;
+ /* Do not try to re-read the file within single bus operation. */
+ if (m) {
+ if (m == c->x11_cache)
+ return 0;
+
+ sd_bus_message_unref(c->x11_cache);
+ c->x11_cache = sd_bus_message_ref(m);
+ }
+
+ if (stat("/etc/X11/xorg.conf.d/00-keyboard.conf", &st) < 0) {
+ if (errno != ENOENT)
+ return -errno;
+
+ c->x11_mtime = USEC_INFINITY;
+ context_free_x11(c);
+ return 0;
+ }
+
+ /* If mtime is not changed, then we do not need to re-read */
+ t = timespec_load(&st.st_mtim);
+ if (c->x11_mtime != USEC_INFINITY && t == c->x11_mtime)
+ return 0;
+
+ c->x11_mtime = t;
context_free_x11(c);
f = fopen("/etc/X11/xorg.conf.d/00-keyboard.conf", "re");
if (!f)
- return errno == ENOENT ? 0 : -errno;
+ return -errno;
- while (fgets(line, sizeof(line), f)) {
+ for (;;) {
+ _cleanup_free_ char *line = NULL;
char *l;
- char_array_0(line);
- l = strstrip(line);
+ r = read_line(f, LONG_LINE_MAX, &line);
+ if (r < 0)
+ return r;
+ if (r == 0)
+ break;
+ l = strstrip(line);
if (IN_SET(l[0], 0, '#'))
continue;
if (in_section && first_word(l, "Option")) {
_cleanup_strv_free_ char **a = NULL;
- r = strv_split_extract(&a, l, WHITESPACE, EXTRACT_QUOTES);
+ r = strv_split_extract(&a, l, WHITESPACE, EXTRACT_UNQUOTE);
if (r < 0)
return r;
} else if (!in_section && first_word(l, "Section")) {
_cleanup_strv_free_ char **a = NULL;
- r = strv_split_extract(&a, l, WHITESPACE, EXTRACT_QUOTES);
+ r = strv_split_extract(&a, l, WHITESPACE, EXTRACT_UNQUOTE);
if (r < 0)
return -ENOMEM;
return 0;
}
-int context_read_data(Context *c) {
- int r, q, p;
-
- r = locale_read_data(c);
- q = vconsole_read_data(c);
- p = x11_read_data(c);
-
- return r < 0 ? r : q < 0 ? q : p;
-}
-
int locale_write_data(Context *c, char ***settings) {
- int r, p;
_cleanup_strv_free_ char **l = NULL;
+ struct stat st;
+ int r, p;
/* 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 < _VARIABLE_LC_MAX; p++) {
_cleanup_free_ char *t = NULL;
char **u;
name = locale_variable_to_string(p);
assert(name);
- if (isempty(c->locale[p])) {
- l = strv_env_unset(l, name);
+ if (isempty(c->locale[p]))
continue;
- }
if (asprintf(&t, "%s=%s", name, c->locale[p]) < 0)
return -ENOMEM;
if (unlink("/etc/locale.conf") < 0)
return errno == ENOENT ? 0 : -errno;
+ c->locale_mtime = USEC_INFINITY;
return 0;
}
return r;
*settings = TAKE_PTR(l);
+
+ if (stat("/etc/locale.conf", &st) >= 0)
+ c->locale_mtime = timespec_load(&st.st_mtim);
+
return 0;
}
int vconsole_write_data(Context *c) {
- int r;
_cleanup_strv_free_ char **l = NULL;
+ struct stat st;
+ int r;
- r = load_env_file(NULL, "/etc/vconsole.conf", NULL, &l);
+ r = load_env_file(NULL, "/etc/vconsole.conf", &l);
if (r < 0 && r != -ENOENT)
return r;
_cleanup_free_ char *s = NULL;
char **u;
- s = strappend("KEYMAP=", c->vc_keymap);
+ s = strjoin("KEYMAP=", c->vc_keymap);
if (!s)
return -ENOMEM;
_cleanup_free_ char *s = NULL;
char **u;
- s = strappend("KEYMAP_TOGGLE=", c->vc_keymap_toggle);
+ s = strjoin("KEYMAP_TOGGLE=", c->vc_keymap_toggle);
if (!s)
return -ENOMEM;
if (unlink("/etc/vconsole.conf") < 0)
return errno == ENOENT ? 0 : -errno;
+ c->vc_mtime = USEC_INFINITY;
return 0;
}
- return write_env_file_label("/etc/vconsole.conf", l);
+ r = write_env_file_label("/etc/vconsole.conf", l);
+ if (r < 0)
+ return r;
+
+ if (stat("/etc/vconsole.conf", &st) >= 0)
+ c->vc_mtime = timespec_load(&st.st_mtim);
+
+ return 0;
}
int x11_write_data(Context *c) {
_cleanup_fclose_ FILE *f = NULL;
_cleanup_free_ char *temp_path = NULL;
+ struct stat st;
int r;
if (isempty(c->x11_layout) &&
if (unlink("/etc/X11/xorg.conf.d/00-keyboard.conf") < 0)
return errno == ENOENT ? 0 : -errno;
+ c->vc_mtime = USEC_INFINITY;
return 0;
}
- mkdir_p_label("/etc/X11/xorg.conf.d", 0755);
-
+ (void) mkdir_p_label("/etc/X11/xorg.conf.d", 0755);
r = fopen_temporary("/etc/X11/xorg.conf.d/00-keyboard.conf", &f, &temp_path);
if (r < 0)
return r;
- (void) __fsetlocking(f, FSETLOCKING_BYCALLER);
(void) fchmod(fileno(f), 0644);
fputs("# Written by systemd-localed(8), read by systemd-localed and Xorg. It's\n"
goto fail;
}
+ if (stat("/etc/X11/xorg.conf.d/00-keyboard.conf", &st) >= 0)
+ c->x11_mtime = timespec_load(&st.st_mtim);
+
return 0;
fail:
assert(a);
for (;;) {
- char line[LINE_MAX];
+ _cleanup_free_ char *line = NULL;
+ size_t length;
char *l, **b;
int r;
- size_t length;
- errno = 0;
- if (!fgets(line, sizeof(line), f)) {
-
- if (ferror(f))
- return errno > 0 ? -errno : -EIO;
-
- return 0;
- }
+ r = read_line(f, LONG_LINE_MAX, &line);
+ if (r < 0)
+ return r;
+ if (r == 0)
+ break;
(*n)++;
if (IN_SET(l[0], 0, '#'))
continue;
- r = strv_split_extract(&b, l, WHITESPACE, EXTRACT_QUOTES);
+ r = strv_split_extract(&b, l, WHITESPACE, EXTRACT_UNQUOTE);
if (r < 0)
return r;
*a = b;
return 1;
}
+
+ return 0;
}
int vconsole_convert_to_x11(Context *c) {
if (!streq(c->vc_keymap, a[0]))
continue;
- if (!streq_ptr(c->x11_layout, strnulldash(a[1])) ||
- !streq_ptr(c->x11_model, strnulldash(a[2])) ||
- !streq_ptr(c->x11_variant, strnulldash(a[3])) ||
- !streq_ptr(c->x11_options, strnulldash(a[4]))) {
+ if (!streq_ptr(c->x11_layout, empty_or_dash_to_null(a[1])) ||
+ !streq_ptr(c->x11_model, empty_or_dash_to_null(a[2])) ||
+ !streq_ptr(c->x11_variant, empty_or_dash_to_null(a[3])) ||
+ !streq_ptr(c->x11_options, empty_or_dash_to_null(a[4]))) {
- 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)
+ if (free_and_strdup(&c->x11_layout, empty_or_dash_to_null(a[1])) < 0 ||
+ free_and_strdup(&c->x11_model, empty_or_dash_to_null(a[2])) < 0 ||
+ free_and_strdup(&c->x11_variant, empty_or_dash_to_null(a[3])) < 0 ||
+ free_and_strdup(&c->x11_options, empty_or_dash_to_null(a[4])) < 0)
return -ENOMEM;
modified = true;
if (startswith_comma(c->x11_layout, a[1]))
matching = 5;
else {
- char *x;
+ _cleanup_free_ char *x = NULL;
/* If that didn't work, strip off the
* other layouts from the entry, too */
- x = strndupa(a[1], strcspn(a[1], ","));
+ x = strndup(a[1], strcspn(a[1], ","));
if (startswith_comma(c->x11_layout, x))
matching = 1;
}
context_free_vconsole(c);
} else {
- char *new_keymap = NULL;
+ _cleanup_free_ char *new_keymap = NULL;
int r;
r = find_converted_keymap(c->x11_layout, c->x11_variant, &new_keymap);
c->x11_layout);
if (!streq_ptr(c->vc_keymap, new_keymap)) {
- free(c->vc_keymap);
- c->vc_keymap = new_keymap;
+ free_and_replace(c->vc_keymap, new_keymap);
c->vc_keymap_toggle = mfree(c->vc_keymap_toggle);
modified = true;
- } else
- free(new_keymap);
+ }
}
if (modified)