richardphibel / rpms / systemd

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