diff options
Diffstat (limited to 'src/locale/keymap-util.c')
-rw-r--r-- | src/locale/keymap-util.c | 85 |
1 files changed, 51 insertions, 34 deletions
diff --git a/src/locale/keymap-util.c b/src/locale/keymap-util.c index 598b931d03..6b6b32a591 100644 --- a/src/locale/keymap-util.c +++ b/src/locale/keymap-util.c @@ -5,7 +5,10 @@ #include <string.h> #include <unistd.h> +#include "bus-util.h" #include "def.h" +#include "env-file.h" +#include "env-file-label.h" #include "env-util.h" #include "fd-util.h" #include "fileio-label.h" @@ -16,6 +19,7 @@ #include "mkdir.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); @@ -68,10 +72,16 @@ static void context_free_locale(Context *c) { 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(char *locale[_VARIABLE_LC_MAX]) { @@ -87,11 +97,13 @@ int locale_read_data(Context *c, sd_bus_message *m) { int r; /* Do not try to re-read the file within single bus operation. */ - if (m && m == c->locale_cache) - return 0; + if (m) { + if (m == c->locale_cache) + return 0; - /* To suppress multiple call of stat(), store the message to cache here. */ - c->locale_cache = m; + 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) @@ -108,7 +120,7 @@ int locale_read_data(Context *c, sd_bus_message *m) { c->locale_mtime = t; context_free_locale(c); - r = parse_env_file(NULL, "/etc/locale.conf", NEWLINE, + 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], @@ -122,8 +134,7 @@ int locale_read_data(Context *c, sd_bus_message *m) { "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); + "LC_IDENTIFICATION", &c->locale[VARIABLE_LC_IDENTIFICATION]); if (r < 0) return r; } else { @@ -155,11 +166,13 @@ int vconsole_read_data(Context *c, sd_bus_message *m) { int r; /* Do not try to re-read the file within single bus operation. */ - if (m && m == c->vc_cache) - return 0; + if (m) { + if (m == c->vc_cache) + return 0; - /* To suppress multiple call of stat(), store the message to cache here. */ - c->vc_cache = m; + 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) @@ -178,10 +191,9 @@ int vconsole_read_data(Context *c, sd_bus_message *m) { 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); + "KEYMAP_TOGGLE", &c->vc_keymap_toggle); if (r < 0) return r; @@ -191,17 +203,18 @@ int vconsole_read_data(Context *c, sd_bus_message *m) { int x11_read_data(Context *c, sd_bus_message *m) { _cleanup_fclose_ FILE *f = NULL; bool in_section = false; - char line[LINE_MAX]; struct stat st; usec_t t; int r; /* Do not try to re-read the file within single bus operation. */ - if (m && m == c->x11_cache) - return 0; + if (m) { + if (m == c->x11_cache) + return 0; - /* To suppress multiple call of stat(), store the message to cache here. */ - c->x11_cache = m; + 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) @@ -224,12 +237,17 @@ int x11_read_data(Context *c, sd_bus_message *m) { if (!f) 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; @@ -327,7 +345,7 @@ int vconsole_write_data(Context *c) { 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; @@ -460,19 +478,16 @@ static int read_next_mapping(const char* filename, 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)++; @@ -495,6 +510,8 @@ static int read_next_mapping(const char* filename, *a = b; return 1; } + + return 0; } int vconsole_convert_to_x11(Context *c) { |