richardphibel / rpms / systemd

Forked from rpms/systemd 2 years ago
Clone
594167
From cdcbd56d4eacba3b3ee4d8b0c38d6509a307a2b7 Mon Sep 17 00:00:00 2001
594167
From: Yu Watanabe <watanabe.yu+github@gmail.com>
594167
Date: Tue, 14 Jun 2022 09:07:00 +0900
594167
Subject: [PATCH] locale-setup: merge locale handling in PID1 and localed
594167
594167
Related: #2087652
594167
---
594167
 src/basic/locale-util.c   |  11 ++
594167
 src/basic/locale-util.h   |   1 +
594167
 src/core/locale-setup.c   |  95 --------------
594167
 src/core/locale-setup.h   |   4 -
594167
 src/core/meson.build      |   2 -
594167
 src/locale/keymap-util.c  | 102 +--------------
594167
 src/locale/keymap-util.h  |   7 +-
594167
 src/locale/localectl.c    |  47 +++----
594167
 src/locale/localed.c      |  87 +++----------
594167
 src/shared/locale-setup.c | 256 ++++++++++++++++++++++++++++++++++++++
594167
 src/shared/locale-setup.h |  28 +++++
594167
 src/shared/meson.build    |   2 +
594167
 12 files changed, 333 insertions(+), 309 deletions(-)
594167
 delete mode 100644 src/core/locale-setup.c
594167
 delete mode 100644 src/core/locale-setup.h
594167
 create mode 100644 src/shared/locale-setup.c
594167
 create mode 100644 src/shared/locale-setup.h
594167
594167
diff --git a/src/basic/locale-util.c b/src/basic/locale-util.c
594167
index 8098369db5..21f0982bb5 100644
594167
--- a/src/basic/locale-util.c
594167
+++ b/src/basic/locale-util.c
594167
@@ -335,6 +335,17 @@ void locale_variables_free(char *l[_VARIABLE_LC_MAX]) {
594167
                 l[i] = mfree(l[i]);
594167
 }
594167
 
594167
+void locale_variables_simplify(char *l[_VARIABLE_LC_MAX]) {
594167
+        assert(l);
594167
+
594167
+        for (LocaleVariable p = 0; p < _VARIABLE_LC_MAX; p++) {
594167
+                if (p == VARIABLE_LANG)
594167
+                        continue;
594167
+                if (isempty(l[p]) || streq_ptr(l[VARIABLE_LANG], l[p]))
594167
+                        l[p] = mfree(l[p]);
594167
+        }
594167
+}
594167
+
594167
 static const char * const locale_variable_table[_VARIABLE_LC_MAX] = {
594167
         [VARIABLE_LANG]              = "LANG",
594167
         [VARIABLE_LANGUAGE]          = "LANGUAGE",
594167
diff --git a/src/basic/locale-util.h b/src/basic/locale-util.h
594167
index bab927146b..8990cb6a75 100644
594167
--- a/src/basic/locale-util.h
594167
+++ b/src/basic/locale-util.h
594167
@@ -53,3 +53,4 @@ void locale_variables_free(char* l[_VARIABLE_LC_MAX]);
594167
 static inline void locale_variables_freep(char*(*l)[_VARIABLE_LC_MAX]) {
594167
         locale_variables_free(*l);
594167
 }
594167
+void locale_variables_simplify(char *l[_VARIABLE_LC_MAX]);
594167
diff --git a/src/core/locale-setup.c b/src/core/locale-setup.c
594167
deleted file mode 100644
594167
index 716febbefa..0000000000
594167
--- a/src/core/locale-setup.c
594167
+++ /dev/null
594167
@@ -1,95 +0,0 @@
594167
-/* SPDX-License-Identifier: LGPL-2.1-or-later */
594167
-
594167
-#include <errno.h>
594167
-#include <stdlib.h>
594167
-
594167
-#include "env-file.h"
594167
-#include "env-util.h"
594167
-#include "locale-setup.h"
594167
-#include "locale-util.h"
594167
-#include "proc-cmdline.h"
594167
-#include "string-util.h"
594167
-#include "strv.h"
594167
-#include "util.h"
594167
-#include "virt.h"
594167
-
594167
-int locale_setup(char ***environment) {
594167
-        _cleanup_(locale_variables_freep) char *variables[_VARIABLE_LC_MAX] = {};
594167
-        _cleanup_strv_free_ char **add = NULL;
594167
-        int r;
594167
-
594167
-        r = proc_cmdline_get_key_many(PROC_CMDLINE_STRIP_RD_PREFIX,
594167
-                                      "locale.LANG",              &variables[VARIABLE_LANG],
594167
-                                      "locale.LANGUAGE",          &variables[VARIABLE_LANGUAGE],
594167
-                                      "locale.LC_CTYPE",          &variables[VARIABLE_LC_CTYPE],
594167
-                                      "locale.LC_NUMERIC",        &variables[VARIABLE_LC_NUMERIC],
594167
-                                      "locale.LC_TIME",           &variables[VARIABLE_LC_TIME],
594167
-                                      "locale.LC_COLLATE",        &variables[VARIABLE_LC_COLLATE],
594167
-                                      "locale.LC_MONETARY",       &variables[VARIABLE_LC_MONETARY],
594167
-                                      "locale.LC_MESSAGES",       &variables[VARIABLE_LC_MESSAGES],
594167
-                                      "locale.LC_PAPER",          &variables[VARIABLE_LC_PAPER],
594167
-                                      "locale.LC_NAME",           &variables[VARIABLE_LC_NAME],
594167
-                                      "locale.LC_ADDRESS",        &variables[VARIABLE_LC_ADDRESS],
594167
-                                      "locale.LC_TELEPHONE",      &variables[VARIABLE_LC_TELEPHONE],
594167
-                                      "locale.LC_MEASUREMENT",    &variables[VARIABLE_LC_MEASUREMENT],
594167
-                                      "locale.LC_IDENTIFICATION", &variables[VARIABLE_LC_IDENTIFICATION]);
594167
-        if (r < 0 && r != -ENOENT)
594167
-                log_warning_errno(r, "Failed to read /proc/cmdline: %m");
594167
-
594167
-        /* Hmm, nothing set on the kernel cmd line? Then let's try /etc/locale.conf */
594167
-        if (r <= 0) {
594167
-                r = parse_env_file(NULL, "/etc/locale.conf",
594167
-                                   "LANG",              &variables[VARIABLE_LANG],
594167
-                                   "LANGUAGE",          &variables[VARIABLE_LANGUAGE],
594167
-                                   "LC_CTYPE",          &variables[VARIABLE_LC_CTYPE],
594167
-                                   "LC_NUMERIC",        &variables[VARIABLE_LC_NUMERIC],
594167
-                                   "LC_TIME",           &variables[VARIABLE_LC_TIME],
594167
-                                   "LC_COLLATE",        &variables[VARIABLE_LC_COLLATE],
594167
-                                   "LC_MONETARY",       &variables[VARIABLE_LC_MONETARY],
594167
-                                   "LC_MESSAGES",       &variables[VARIABLE_LC_MESSAGES],
594167
-                                   "LC_PAPER",          &variables[VARIABLE_LC_PAPER],
594167
-                                   "LC_NAME",           &variables[VARIABLE_LC_NAME],
594167
-                                   "LC_ADDRESS",        &variables[VARIABLE_LC_ADDRESS],
594167
-                                   "LC_TELEPHONE",      &variables[VARIABLE_LC_TELEPHONE],
594167
-                                   "LC_MEASUREMENT",    &variables[VARIABLE_LC_MEASUREMENT],
594167
-                                   "LC_IDENTIFICATION", &variables[VARIABLE_LC_IDENTIFICATION]);
594167
-                if (r < 0 && r != -ENOENT)
594167
-                        log_warning_errno(r, "Failed to read /etc/locale.conf: %m");
594167
-        }
594167
-
594167
-        for (LocaleVariable i = 0; i < _VARIABLE_LC_MAX; i++) {
594167
-                char *s;
594167
-
594167
-                if (!variables[i])
594167
-                        continue;
594167
-
594167
-                s = strjoin(locale_variable_to_string(i), "=", variables[i]);
594167
-                if (!s)
594167
-                        return -ENOMEM;
594167
-
594167
-                if (strv_consume(&add, s) < 0)
594167
-                        return -ENOMEM;
594167
-        }
594167
-
594167
-        if (strv_isempty(add)) {
594167
-                /* If no locale is configured then default to compile-time default. */
594167
-
594167
-                add = strv_new("LANG=" SYSTEMD_DEFAULT_LOCALE);
594167
-                if (!add)
594167
-                        return -ENOMEM;
594167
-        }
594167
-
594167
-        if (strv_isempty(*environment))
594167
-                strv_free_and_replace(*environment, add);
594167
-        else {
594167
-                char **merged;
594167
-
594167
-                merged = strv_env_merge(*environment, add);
594167
-                if (!merged)
594167
-                        return -ENOMEM;
594167
-
594167
-                strv_free_and_replace(*environment, merged);
594167
-        }
594167
-
594167
-        return 0;
594167
-}
594167
diff --git a/src/core/locale-setup.h b/src/core/locale-setup.h
594167
deleted file mode 100644
594167
index d554ad3060..0000000000
594167
--- a/src/core/locale-setup.h
594167
+++ /dev/null
594167
@@ -1,4 +0,0 @@
594167
-/* SPDX-License-Identifier: LGPL-2.1-or-later */
594167
-#pragma once
594167
-
594167
-int locale_setup(char ***environment);
594167
diff --git a/src/core/meson.build b/src/core/meson.build
594167
index 97ac431763..7704478d43 100644
594167
--- a/src/core/meson.build
594167
+++ b/src/core/meson.build
594167
@@ -83,8 +83,6 @@ libcore_sources = '''
594167
         load-dropin.h
594167
         load-fragment.c
594167
         load-fragment.h
594167
-        locale-setup.c
594167
-        locale-setup.h
594167
         manager-dump.c
594167
         manager-dump.h
594167
         manager-serialize.c
594167
diff --git a/src/locale/keymap-util.c b/src/locale/keymap-util.c
594167
index 10d2ed7aec..a3af396ebe 100644
594167
--- a/src/locale/keymap-util.c
594167
+++ b/src/locale/keymap-util.c
594167
@@ -65,13 +65,8 @@ static void context_free_vconsole(Context *c) {
594167
         c->vc_keymap_toggle = mfree(c->vc_keymap_toggle);
594167
 }
594167
 
594167
-static void context_free_locale(Context *c) {
594167
-        for (LocaleVariable p = 0; p < _VARIABLE_LC_MAX; p++)
594167
-                c->locale[p] = mfree(c->locale[p]);
594167
-}
594167
-
594167
 void context_clear(Context *c) {
594167
-        context_free_locale(c);
594167
+        locale_context_clear(&c->locale_context);
594167
         context_free_x11(c);
594167
         context_free_vconsole(c);
594167
 
594167
@@ -82,15 +77,8 @@ void context_clear(Context *c) {
594167
         bus_verify_polkit_async_registry_free(c->polkit_registry);
594167
 };
594167
 
594167
-void locale_simplify(char *locale[_VARIABLE_LC_MAX]) {
594167
-        for (LocaleVariable p = VARIABLE_LANG+1; p < _VARIABLE_LC_MAX; p++)
594167
-                if (isempty(locale[p]) || streq_ptr(locale[VARIABLE_LANG], locale[p]))
594167
-                        locale[p] = mfree(locale[p]);
594167
-}
594167
-
594167
 int locale_read_data(Context *c, sd_bus_message *m) {
594167
-        struct stat st;
594167
-        int r;
594167
+        assert(c);
594167
 
594167
         /* Do not try to re-read the file within single bus operation. */
594167
         if (m) {
594167
@@ -101,57 +89,7 @@ int locale_read_data(Context *c, sd_bus_message *m) {
594167
                 c->locale_cache = sd_bus_message_ref(m);
594167
         }
594167
 
594167
-        r = stat("/etc/locale.conf", &st);
594167
-        if (r < 0 && errno != ENOENT)
594167
-                return -errno;
594167
-
594167
-        if (r >= 0) {
594167
-                usec_t t;
594167
-
594167
-                /* If mtime is not changed, then we do not need to re-read the file. */
594167
-                t = timespec_load(&st.st_mtim);
594167
-                if (c->locale_mtime != USEC_INFINITY && t == c->locale_mtime)
594167
-                        return 0;
594167
-
594167
-                c->locale_mtime = t;
594167
-                context_free_locale(c);
594167
-
594167
-                r = parse_env_file(NULL, "/etc/locale.conf",
594167
-                                   "LANG",              &c->locale[VARIABLE_LANG],
594167
-                                   "LANGUAGE",          &c->locale[VARIABLE_LANGUAGE],
594167
-                                   "LC_CTYPE",          &c->locale[VARIABLE_LC_CTYPE],
594167
-                                   "LC_NUMERIC",        &c->locale[VARIABLE_LC_NUMERIC],
594167
-                                   "LC_TIME",           &c->locale[VARIABLE_LC_TIME],
594167
-                                   "LC_COLLATE",        &c->locale[VARIABLE_LC_COLLATE],
594167
-                                   "LC_MONETARY",       &c->locale[VARIABLE_LC_MONETARY],
594167
-                                   "LC_MESSAGES",       &c->locale[VARIABLE_LC_MESSAGES],
594167
-                                   "LC_PAPER",          &c->locale[VARIABLE_LC_PAPER],
594167
-                                   "LC_NAME",           &c->locale[VARIABLE_LC_NAME],
594167
-                                   "LC_ADDRESS",        &c->locale[VARIABLE_LC_ADDRESS],
594167
-                                   "LC_TELEPHONE",      &c->locale[VARIABLE_LC_TELEPHONE],
594167
-                                   "LC_MEASUREMENT",    &c->locale[VARIABLE_LC_MEASUREMENT],
594167
-                                   "LC_IDENTIFICATION", &c->locale[VARIABLE_LC_IDENTIFICATION]);
594167
-                if (r < 0)
594167
-                        return r;
594167
-        } else {
594167
-                c->locale_mtime = USEC_INFINITY;
594167
-                context_free_locale(c);
594167
-
594167
-                /* Fill in what we got passed from systemd. */
594167
-                for (LocaleVariable p = 0; p < _VARIABLE_LC_MAX; p++) {
594167
-                        const char *name;
594167
-
594167
-                        name = locale_variable_to_string(p);
594167
-                        assert(name);
594167
-
594167
-                        r = free_and_strdup(&c->locale[p], empty_to_null(getenv(name)));
594167
-                        if (r < 0)
594167
-                                return r;
594167
-                }
594167
-        }
594167
-
594167
-        locale_simplify(c->locale);
594167
-        return 0;
594167
+        return locale_context_load(&c->locale_context, LOCALE_LOAD_LOCALE_CONF | LOCALE_LOAD_ENVIRONMENT | LOCALE_LOAD_SIMPLIFY);
594167
 }
594167
 
594167
 int vconsole_read_data(Context *c, sd_bus_message *m) {
594167
@@ -285,40 +223,6 @@ int x11_read_data(Context *c, sd_bus_message *m) {
594167
         return 0;
594167
 }
594167
 
594167
-int locale_write_data(Context *c, char ***settings) {
594167
-        _cleanup_strv_free_ char **l = NULL;
594167
-        struct stat st;
594167
-        int r;
594167
-
594167
-        /* Set values will be returned as strv in *settings on success. */
594167
-
594167
-        for (LocaleVariable p = 0; p < _VARIABLE_LC_MAX; p++)
594167
-                if (!isempty(c->locale[p])) {
594167
-                        r = strv_env_assign(&l, locale_variable_to_string(p), c->locale[p]);
594167
-                        if (r < 0)
594167
-                                return r;
594167
-                }
594167
-
594167
-        if (strv_isempty(l)) {
594167
-                if (unlink("/etc/locale.conf") < 0)
594167
-                        return errno == ENOENT ? 0 : -errno;
594167
-
594167
-                c->locale_mtime = USEC_INFINITY;
594167
-                return 0;
594167
-        }
594167
-
594167
-        r = write_env_file_label("/etc/locale.conf", l);
594167
-        if (r < 0)
594167
-                return r;
594167
-
594167
-        *settings = TAKE_PTR(l);
594167
-
594167
-        if (stat("/etc/locale.conf", &st) >= 0)
594167
-                c->locale_mtime = timespec_load(&st.st_mtim);
594167
-
594167
-        return 0;
594167
-}
594167
-
594167
 int vconsole_write_data(Context *c) {
594167
         _cleanup_strv_free_ char **l = NULL;
594167
         struct stat st;
594167
diff --git a/src/locale/keymap-util.h b/src/locale/keymap-util.h
594167
index c087dbcbbe..5470d1bb9b 100644
594167
--- a/src/locale/keymap-util.h
594167
+++ b/src/locale/keymap-util.h
594167
@@ -4,13 +4,12 @@
594167
 #include "sd-bus.h"
594167
 
594167
 #include "hashmap.h"
594167
-#include "locale-util.h"
594167
+#include "locale-setup.h"
594167
 #include "time-util.h"
594167
 
594167
 typedef struct Context {
594167
         sd_bus_message *locale_cache;
594167
-        usec_t locale_mtime;
594167
-        char *locale[_VARIABLE_LC_MAX];
594167
+        LocaleContext locale_context;
594167
 
594167
         sd_bus_message *x11_cache;
594167
         usec_t x11_mtime;
594167
@@ -40,8 +39,6 @@ int vconsole_convert_to_x11(Context *c);
594167
 int vconsole_write_data(Context *c);
594167
 int x11_convert_to_vconsole(Context *c);
594167
 int x11_write_data(Context *c);
594167
-void locale_simplify(char *locale[_VARIABLE_LC_MAX]);
594167
-int locale_write_data(Context *c, char ***settings);
594167
 
594167
 bool locale_gen_check_available(void);
594167
 int locale_gen_enable_locale(const char *locale);
594167
diff --git a/src/locale/localectl.c b/src/locale/localectl.c
594167
index 661d54c27d..6bfb564f97 100644
594167
--- a/src/locale/localectl.c
594167
+++ b/src/locale/localectl.c
594167
@@ -12,7 +12,7 @@
594167
 #include "fd-util.h"
594167
 #include "fileio.h"
594167
 #include "kbd-util.h"
594167
-#include "locale-util.h"
594167
+#include "locale-setup.h"
594167
 #include "main-func.h"
594167
 #include "memory-util.h"
594167
 #include "pager.h"
594167
@@ -52,44 +52,25 @@ static void status_info_clear(StatusInfo *info) {
594167
 }
594167
 
594167
 static void print_overridden_variables(void) {
594167
-        _cleanup_(locale_variables_freep) char *variables[_VARIABLE_LC_MAX] = {};
594167
-        bool print_warning = true;
594167
+        _cleanup_(locale_context_clear) LocaleContext c = { .mtime = USEC_INFINITY };
594167
+        _cleanup_strv_free_ char **env = NULL;
594167
         int r;
594167
 
594167
         if (arg_transport != BUS_TRANSPORT_LOCAL)
594167
                 return;
594167
 
594167
-        r = proc_cmdline_get_key_many(
594167
-                        PROC_CMDLINE_STRIP_RD_PREFIX,
594167
-                        "locale.LANG",              &variables[VARIABLE_LANG],
594167
-                        "locale.LANGUAGE",          &variables[VARIABLE_LANGUAGE],
594167
-                        "locale.LC_CTYPE",          &variables[VARIABLE_LC_CTYPE],
594167
-                        "locale.LC_NUMERIC",        &variables[VARIABLE_LC_NUMERIC],
594167
-                        "locale.LC_TIME",           &variables[VARIABLE_LC_TIME],
594167
-                        "locale.LC_COLLATE",        &variables[VARIABLE_LC_COLLATE],
594167
-                        "locale.LC_MONETARY",       &variables[VARIABLE_LC_MONETARY],
594167
-                        "locale.LC_MESSAGES",       &variables[VARIABLE_LC_MESSAGES],
594167
-                        "locale.LC_PAPER",          &variables[VARIABLE_LC_PAPER],
594167
-                        "locale.LC_NAME",           &variables[VARIABLE_LC_NAME],
594167
-                        "locale.LC_ADDRESS",        &variables[VARIABLE_LC_ADDRESS],
594167
-                        "locale.LC_TELEPHONE",      &variables[VARIABLE_LC_TELEPHONE],
594167
-                        "locale.LC_MEASUREMENT",    &variables[VARIABLE_LC_MEASUREMENT],
594167
-                        "locale.LC_IDENTIFICATION", &variables[VARIABLE_LC_IDENTIFICATION]);
594167
-        if (r < 0 && r != -ENOENT) {
594167
-                log_warning_errno(r, "Failed to read /proc/cmdline: %m");
594167
-                return;
594167
-        }
594167
-
594167
-        for (LocaleVariable j = 0; j < _VARIABLE_LC_MAX; j++)
594167
-                if (variables[j]) {
594167
-                        if (print_warning) {
594167
-                                log_warning("Warning: Settings on kernel command line override system locale settings in /etc/locale.conf.\n"
594167
-                                            "    Command Line: %s=%s", locale_variable_to_string(j), variables[j]);
594167
+        (void) locale_context_load(&c, LOCALE_LOAD_PROC_CMDLINE);
594167
 
594167
-                                print_warning = false;
594167
-                        } else
594167
-                                log_warning("                  %s=%s", locale_variable_to_string(j), variables[j]);
594167
-                }
594167
+        r = locale_context_build_env(&c, &env, NULL);
594167
+        if (r < 0)
594167
+                return (void) log_warning_errno(r, "Failed to build locale settings from kernel command line, ignoring: %m");
594167
+
594167
+        STRV_FOREACH(p, env)
594167
+                if (p == env)
594167
+                        log_warning("Warning: Settings on kernel command line override system locale settings in /etc/locale.conf.\n"
594167
+                                    "    Command Line: %s", *p);
594167
+                else
594167
+                        log_warning("                  %s", *p);
594167
 }
594167
 
594167
 static void print_status_info(StatusInfo *i) {
594167
diff --git a/src/locale/localed.c b/src/locale/localed.c
594167
index 89bf9c6fba..9718c5b95f 100644
594167
--- a/src/locale/localed.c
594167
+++ b/src/locale/localed.c
594167
@@ -21,7 +21,6 @@
594167
 #include "dlfcn-util.h"
594167
 #include "kbd-util.h"
594167
 #include "keymap-util.h"
594167
-#include "locale-util.h"
594167
 #include "macro.h"
594167
 #include "main-func.h"
594167
 #include "missing_capability.h"
594167
@@ -33,44 +32,13 @@
594167
 #include "strv.h"
594167
 #include "user-util.h"
594167
 
594167
-static int locale_update_system_manager(Context *c, sd_bus *bus) {
594167
-        _cleanup_free_ char **l_unset = NULL;
594167
-        _cleanup_strv_free_ char **l_set = NULL;
594167
+static int locale_update_system_manager(sd_bus *bus, char **l_set, char **l_unset) {
594167
         _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
594167
         _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
594167
-        size_t c_set = 0, c_unset = 0;
594167
         int r;
594167
 
594167
         assert(bus);
594167
 
594167
-        l_unset = new0(char*, _VARIABLE_LC_MAX);
594167
-        if (!l_unset)
594167
-                return log_oom();
594167
-
594167
-        l_set = new0(char*, _VARIABLE_LC_MAX);
594167
-        if (!l_set)
594167
-                return log_oom();
594167
-
594167
-        for (LocaleVariable p = 0; p < _VARIABLE_LC_MAX; p++) {
594167
-                const char *name;
594167
-
594167
-                name = locale_variable_to_string(p);
594167
-                assert(name);
594167
-
594167
-                if (isempty(c->locale[p]))
594167
-                        l_unset[c_set++] = (char*) name;
594167
-                else {
594167
-                        char *s;
594167
-
594167
-                        s = strjoin(name, "=", c->locale[p]);
594167
-                        if (!s)
594167
-                                return log_oom();
594167
-
594167
-                        l_set[c_unset++] = s;
594167
-                }
594167
-        }
594167
-
594167
-        assert(c_set + c_unset == _VARIABLE_LC_MAX);
594167
         r = sd_bus_message_new_method_call(bus, &m,
594167
                         "org.freedesktop.systemd1",
594167
                         "/org/freedesktop/systemd1",
594167
@@ -188,21 +156,9 @@ static int property_get_locale(
594167
         if (!l)
594167
                 return -ENOMEM;
594167
 
594167
-        for (LocaleVariable p = 0, q = 0; p < _VARIABLE_LC_MAX; p++) {
594167
-                char *t;
594167
-                const char *name;
594167
-
594167
-                name = locale_variable_to_string(p);
594167
-                assert(name);
594167
-
594167
-                if (isempty(c->locale[p]))
594167
-                        continue;
594167
-
594167
-                if (asprintf(&t, "%s=%s", name, c->locale[p]) < 0)
594167
-                        return -ENOMEM;
594167
-
594167
-                l[q++] = t;
594167
-        }
594167
+        r = locale_context_build_env(&c->locale_context, &l, NULL);
594167
+        if (r < 0)
594167
+                return r;
594167
 
594167
         return sd_bus_message_append_strv(reply, l);
594167
 }
594167
@@ -342,9 +298,8 @@ static int locale_gen_process_locale(char *new_locale[static _VARIABLE_LC_MAX],
594167
 
594167
 static int method_set_locale(sd_bus_message *m, void *userdata, sd_bus_error *error) {
594167
         _cleanup_(locale_variables_freep) char *new_locale[_VARIABLE_LC_MAX] = {};
594167
-        _cleanup_strv_free_ char **settings = NULL, **l = NULL;
594167
+        _cleanup_strv_free_ char **l = NULL, **l_set = NULL, **l_unset = NULL;
594167
         Context *c = userdata;
594167
-        bool modified = false;
594167
         int interactive, r;
594167
         bool use_localegen;
594167
 
594167
@@ -402,22 +357,13 @@ static int method_set_locale(sd_bus_message *m, void *userdata, sd_bus_error *er
594167
         }
594167
 
594167
         /* Merge with the current settings */
594167
-        for (LocaleVariable p = 0; p < _VARIABLE_LC_MAX; p++)
594167
-                if (!isempty(c->locale[p]) && isempty(new_locale[p])) {
594167
-                        new_locale[p] = strdup(c->locale[p]);
594167
-                        if (!new_locale[p])
594167
-                                return -ENOMEM;
594167
-                }
594167
-
594167
-        locale_simplify(new_locale);
594167
+        r = locale_context_merge(&c->locale_context, new_locale);
594167
+        if (r < 0)
594167
+                return r;
594167
 
594167
-        for (LocaleVariable p = 0; p < _VARIABLE_LC_MAX; p++)
594167
-                if (!streq_ptr(c->locale[p], new_locale[p])) {
594167
-                        modified = true;
594167
-                        break;
594167
-                }
594167
+        locale_variables_simplify(new_locale);
594167
 
594167
-        if (!modified) {
594167
+        if (locale_context_equal(&c->locale_context, new_locale)) {
594167
                 log_debug("Locale settings were not modified.");
594167
                 return sd_bus_reply_method_return(m, NULL);
594167
         }
594167
@@ -443,22 +389,21 @@ static int method_set_locale(sd_bus_message *m, void *userdata, sd_bus_error *er
594167
                         return r;
594167
         }
594167
 
594167
-        for (LocaleVariable p = 0; p < _VARIABLE_LC_MAX; p++)
594167
-                free_and_replace(c->locale[p], new_locale[p]);
594167
+        locale_context_take(&c->locale_context, new_locale);
594167
 
594167
         /* Write locale configuration */
594167
-        r = locale_write_data(c, &settings);
594167
+        r = locale_context_save(&c->locale_context, &l_set, &l_unset);
594167
         if (r < 0) {
594167
                 log_error_errno(r, "Failed to set locale: %m");
594167
                 return sd_bus_error_set_errnof(error, r, "Failed to set locale: %m");
594167
         }
594167
 
594167
-        (void) locale_update_system_manager(c, sd_bus_message_get_bus(m));
594167
+        (void) locale_update_system_manager(sd_bus_message_get_bus(m), l_set, l_unset);
594167
 
594167
-        if (settings) {
594167
+        if (!strv_isempty(l_set)) {
594167
                 _cleanup_free_ char *line = NULL;
594167
 
594167
-                line = strv_join(settings, ", ");
594167
+                line = strv_join(l_set, ", ");
594167
                 log_info("Changed locale to %s.", strnull(line));
594167
         } else
594167
                 log_info("Changed locale to unset.");
594167
@@ -827,7 +772,7 @@ static int connect_bus(Context *c, sd_event *event, sd_bus **_bus) {
594167
 
594167
 static int run(int argc, char *argv[]) {
594167
         _cleanup_(context_clear) Context context = {
594167
-                .locale_mtime = USEC_INFINITY,
594167
+                .locale_context.mtime = USEC_INFINITY,
594167
                 .vc_mtime = USEC_INFINITY,
594167
                 .x11_mtime = USEC_INFINITY,
594167
         };
594167
diff --git a/src/shared/locale-setup.c b/src/shared/locale-setup.c
594167
new file mode 100644
594167
index 0000000000..b8c6647e7c
594167
--- /dev/null
594167
+++ b/src/shared/locale-setup.c
594167
@@ -0,0 +1,256 @@
594167
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
594167
+
594167
+#include <errno.h>
594167
+#include <sys/stat.h>
594167
+
594167
+#include "env-file-label.h"
594167
+#include "env-file.h"
594167
+#include "env-util.h"
594167
+#include "locale-setup.h"
594167
+#include "proc-cmdline.h"
594167
+#include "strv.h"
594167
+
594167
+void locale_context_clear(LocaleContext *c) {
594167
+        assert(c);
594167
+
594167
+        c->mtime = USEC_INFINITY;
594167
+
594167
+        for (LocaleVariable i = 0; i < _VARIABLE_LC_MAX; i++)
594167
+                c->locale[i] = mfree(c->locale[i]);
594167
+}
594167
+
594167
+int locale_context_load(LocaleContext *c, LocaleLoadFlag flag) {
594167
+        int r;
594167
+
594167
+        assert(c);
594167
+
594167
+        if (FLAGS_SET(flag, LOCALE_LOAD_PROC_CMDLINE)) {
594167
+                locale_context_clear(c);
594167
+
594167
+                r = proc_cmdline_get_key_many(PROC_CMDLINE_STRIP_RD_PREFIX,
594167
+                                              "locale.LANG",              &c->locale[VARIABLE_LANG],
594167
+                                              "locale.LANGUAGE",          &c->locale[VARIABLE_LANGUAGE],
594167
+                                              "locale.LC_CTYPE",          &c->locale[VARIABLE_LC_CTYPE],
594167
+                                              "locale.LC_NUMERIC",        &c->locale[VARIABLE_LC_NUMERIC],
594167
+                                              "locale.LC_TIME",           &c->locale[VARIABLE_LC_TIME],
594167
+                                              "locale.LC_COLLATE",        &c->locale[VARIABLE_LC_COLLATE],
594167
+                                              "locale.LC_MONETARY",       &c->locale[VARIABLE_LC_MONETARY],
594167
+                                              "locale.LC_MESSAGES",       &c->locale[VARIABLE_LC_MESSAGES],
594167
+                                              "locale.LC_PAPER",          &c->locale[VARIABLE_LC_PAPER],
594167
+                                              "locale.LC_NAME",           &c->locale[VARIABLE_LC_NAME],
594167
+                                              "locale.LC_ADDRESS",        &c->locale[VARIABLE_LC_ADDRESS],
594167
+                                              "locale.LC_TELEPHONE",      &c->locale[VARIABLE_LC_TELEPHONE],
594167
+                                              "locale.LC_MEASUREMENT",    &c->locale[VARIABLE_LC_MEASUREMENT],
594167
+                                              "locale.LC_IDENTIFICATION", &c->locale[VARIABLE_LC_IDENTIFICATION]);
594167
+                if (r < 0 && r != -ENOENT)
594167
+                        log_debug_errno(r, "Failed to read /proc/cmdline, ignoring: %m");
594167
+                if (r > 0)
594167
+                        goto finalize;
594167
+        }
594167
+
594167
+        if (FLAGS_SET(flag, LOCALE_LOAD_LOCALE_CONF)) {
594167
+                struct stat st;
594167
+                usec_t t;
594167
+
594167
+                r = stat("/etc/locale.conf", &st);
594167
+                if (r < 0 && errno != ENOENT)
594167
+                        return log_debug_errno(errno, "Failed to stat /etc/locale.conf: %m");
594167
+
594167
+                if (r >= 0) {
594167
+                        /* If mtime is not changed, then we do not need to re-read the file. */
594167
+                        t = timespec_load(&st.st_mtim);
594167
+                        if (c->mtime != USEC_INFINITY && t == c->mtime)
594167
+                                return 0;
594167
+
594167
+                        locale_context_clear(c);
594167
+                        c->mtime = t;
594167
+
594167
+                        r = parse_env_file(NULL, "/etc/locale.conf",
594167
+                                           "LANG",              &c->locale[VARIABLE_LANG],
594167
+                                           "LANGUAGE",          &c->locale[VARIABLE_LANGUAGE],
594167
+                                           "LC_CTYPE",          &c->locale[VARIABLE_LC_CTYPE],
594167
+                                           "LC_NUMERIC",        &c->locale[VARIABLE_LC_NUMERIC],
594167
+                                           "LC_TIME",           &c->locale[VARIABLE_LC_TIME],
594167
+                                           "LC_COLLATE",        &c->locale[VARIABLE_LC_COLLATE],
594167
+                                           "LC_MONETARY",       &c->locale[VARIABLE_LC_MONETARY],
594167
+                                           "LC_MESSAGES",       &c->locale[VARIABLE_LC_MESSAGES],
594167
+                                           "LC_PAPER",          &c->locale[VARIABLE_LC_PAPER],
594167
+                                           "LC_NAME",           &c->locale[VARIABLE_LC_NAME],
594167
+                                           "LC_ADDRESS",        &c->locale[VARIABLE_LC_ADDRESS],
594167
+                                           "LC_TELEPHONE",      &c->locale[VARIABLE_LC_TELEPHONE],
594167
+                                           "LC_MEASUREMENT",    &c->locale[VARIABLE_LC_MEASUREMENT],
594167
+                                           "LC_IDENTIFICATION", &c->locale[VARIABLE_LC_IDENTIFICATION]);
594167
+                        if (r < 0)
594167
+                                return log_debug_errno(r, "Failed to read /etc/locale.conf: %m");
594167
+
594167
+                        goto finalize;
594167
+                }
594167
+        }
594167
+
594167
+        if (FLAGS_SET(flag, LOCALE_LOAD_ENVIRONMENT)) {
594167
+                locale_context_clear(c);
594167
+
594167
+                /* Fill in what we got passed from systemd. */
594167
+                for (LocaleVariable p = 0; p < _VARIABLE_LC_MAX; p++) {
594167
+                        const char *name = ASSERT_PTR(locale_variable_to_string(p));
594167
+
594167
+                        r = free_and_strdup(&c->locale[p], empty_to_null(getenv(name)));
594167
+                        if (r < 0)
594167
+                                return log_oom_debug();
594167
+                }
594167
+
594167
+                goto finalize;
594167
+        }
594167
+
594167
+        /* Nothing loaded. */
594167
+        locale_context_clear(c);
594167
+        return 0;
594167
+
594167
+finalize:
594167
+        if (FLAGS_SET(flag, LOCALE_LOAD_SIMPLIFY))
594167
+                locale_variables_simplify(c->locale);
594167
+
594167
+        return 0;
594167
+}
594167
+
594167
+int locale_context_build_env(const LocaleContext *c, char ***ret_set, char ***ret_unset) {
594167
+        _cleanup_strv_free_ char **set = NULL, **unset = NULL;
594167
+        int r;
594167
+
594167
+        assert(c);
594167
+
594167
+        if (!ret_set && !ret_unset)
594167
+                return 0;
594167
+
594167
+        for (LocaleVariable p = 0; p < _VARIABLE_LC_MAX; p++) {
594167
+                const char *name = ASSERT_PTR(locale_variable_to_string(p));
594167
+
594167
+                if (isempty(c->locale[p])) {
594167
+                        if (!ret_unset)
594167
+                                continue;
594167
+                        r = strv_extend(&unset, name);
594167
+                } else {
594167
+                        if (!ret_set)
594167
+                                continue;
594167
+                        r = strv_env_assign(&set, name, c->locale[p]);
594167
+                }
594167
+                if (r < 0)
594167
+                        return r;
594167
+        }
594167
+
594167
+        if (ret_set)
594167
+                *ret_set = TAKE_PTR(set);
594167
+        if (ret_unset)
594167
+                *ret_unset = TAKE_PTR(unset);
594167
+        return 0;
594167
+}
594167
+
594167
+int locale_context_save(LocaleContext *c, char ***ret_set, char ***ret_unset) {
594167
+        _cleanup_strv_free_ char **set = NULL, **unset = NULL;
594167
+        struct stat st;
594167
+        int r;
594167
+
594167
+        assert(c);
594167
+
594167
+        /* Set values will be returned as strv in *ret on success. */
594167
+
594167
+        r = locale_context_build_env(c, &set, ret_unset ? &unset : NULL);
594167
+        if (r < 0)
594167
+                return r;
594167
+
594167
+        if (strv_isempty(set)) {
594167
+                if (unlink("/etc/locale.conf") < 0)
594167
+                        return errno == ENOENT ? 0 : -errno;
594167
+
594167
+                c->mtime = USEC_INFINITY;
594167
+                if (ret_set)
594167
+                        *ret_set = NULL;
594167
+                if (ret_unset)
594167
+                        *ret_unset = NULL;
594167
+                return 0;
594167
+        }
594167
+
594167
+        r = write_env_file_label("/etc/locale.conf", set);
594167
+        if (r < 0)
594167
+                return r;
594167
+
594167
+        if (stat("/etc/locale.conf", &st) >= 0)
594167
+                c->mtime = timespec_load(&st.st_mtim);
594167
+
594167
+        if (ret_set)
594167
+                *ret_set = TAKE_PTR(set);
594167
+        if (ret_unset)
594167
+                *ret_unset = TAKE_PTR(unset);
594167
+        return 0;
594167
+}
594167
+
594167
+int locale_context_merge(const LocaleContext *c, char *l[_VARIABLE_LC_MAX]) {
594167
+        assert(c);
594167
+        assert(l);
594167
+
594167
+        for (LocaleVariable p = 0; p < _VARIABLE_LC_MAX; p++)
594167
+                if (!isempty(c->locale[p]) && isempty(l[p])) {
594167
+                        l[p] = strdup(c->locale[p]);
594167
+                        if (!l[p])
594167
+                                return -ENOMEM;
594167
+                }
594167
+
594167
+        return 0;
594167
+}
594167
+
594167
+void locale_context_take(LocaleContext *c, char *l[_VARIABLE_LC_MAX]) {
594167
+        assert(c);
594167
+        assert(l);
594167
+
594167
+        for (LocaleVariable p = 0; p < _VARIABLE_LC_MAX; p++)
594167
+                free_and_replace(c->locale[p], l[p]);
594167
+}
594167
+
594167
+bool locale_context_equal(const LocaleContext *c, char *l[_VARIABLE_LC_MAX]) {
594167
+        assert(c);
594167
+        assert(l);
594167
+
594167
+        for (LocaleVariable p = 0; p < _VARIABLE_LC_MAX; p++)
594167
+                if (!streq_ptr(c->locale[p], l[p]))
594167
+                        return false;
594167
+
594167
+        return true;
594167
+}
594167
+
594167
+int locale_setup(char ***environment) {
594167
+        _cleanup_(locale_context_clear) LocaleContext c = { .mtime = USEC_INFINITY };
594167
+        _cleanup_strv_free_ char **add = NULL;
594167
+        int r;
594167
+
594167
+        assert(environment);
594167
+
594167
+        r = locale_context_load(&c, LOCALE_LOAD_PROC_CMDLINE | LOCALE_LOAD_LOCALE_CONF);
594167
+        if (r < 0)
594167
+                return r;
594167
+
594167
+        r = locale_context_build_env(&c, &add, NULL);
594167
+        if (r < 0)
594167
+                return r;
594167
+
594167
+        if (strv_isempty(add)) {
594167
+                /* If no locale is configured then default to compile-time default. */
594167
+
594167
+                add = strv_new("LANG=" SYSTEMD_DEFAULT_LOCALE);
594167
+                if (!add)
594167
+                        return -ENOMEM;
594167
+        }
594167
+
594167
+        if (strv_isempty(*environment))
594167
+                strv_free_and_replace(*environment, add);
594167
+        else {
594167
+                char **merged;
594167
+
594167
+                merged = strv_env_merge(*environment, add);
594167
+                if (!merged)
594167
+                        return -ENOMEM;
594167
+
594167
+                strv_free_and_replace(*environment, merged);
594167
+        }
594167
+
594167
+        return 0;
594167
+}
594167
diff --git a/src/shared/locale-setup.h b/src/shared/locale-setup.h
594167
new file mode 100644
594167
index 0000000000..ec3fc8c364
594167
--- /dev/null
594167
+++ b/src/shared/locale-setup.h
594167
@@ -0,0 +1,28 @@
594167
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
594167
+#pragma once
594167
+
594167
+#include "locale-util.h"
594167
+#include "time-util.h"
594167
+
594167
+typedef struct LocaleContext {
594167
+        usec_t mtime;
594167
+        char *locale[_VARIABLE_LC_MAX];
594167
+} LocaleContext;
594167
+
594167
+typedef enum LocaleLoadFlag {
594167
+        LOCALE_LOAD_PROC_CMDLINE = 1 << 0,
594167
+        LOCALE_LOAD_LOCALE_CONF  = 1 << 1,
594167
+        LOCALE_LOAD_ENVIRONMENT  = 1 << 2,
594167
+        LOCALE_LOAD_SIMPLIFY     = 1 << 3,
594167
+} LocaleLoadFlag;
594167
+
594167
+void locale_context_clear(LocaleContext *c);
594167
+int locale_context_load(LocaleContext *c, LocaleLoadFlag flag);
594167
+int locale_context_build_env(const LocaleContext *c, char ***ret_set, char ***ret_unset);
594167
+int locale_context_save(LocaleContext *c, char ***ret_set, char ***ret_unset);
594167
+
594167
+int locale_context_merge(const LocaleContext *c, char *l[_VARIABLE_LC_MAX]);
594167
+void locale_context_take(LocaleContext *c, char *l[_VARIABLE_LC_MAX]);
594167
+bool locale_context_equal(const LocaleContext *c, char *l[_VARIABLE_LC_MAX]);
594167
+
594167
+int locale_setup(char ***environment);
594167
diff --git a/src/shared/meson.build b/src/shared/meson.build
594167
index 006310a917..62365682cf 100644
594167
--- a/src/shared/meson.build
594167
+++ b/src/shared/meson.build
594167
@@ -195,6 +195,8 @@ shared_sources = files('''
594167
         linux/ethtool.h
594167
         local-addresses.c
594167
         local-addresses.h
594167
+        locale-setup.c
594167
+        locale-setup.h
594167
         lockfile-util.c
594167
         lockfile-util.h
594167
         log-link.h