84b277
From 7664fb3e7ebba889d52c4e7ba653dbbba9035969 Mon Sep 17 00:00:00 2001
84b277
From: Michal Sekletar <msekleta@redhat.com>
84b277
Date: Wed, 24 Sep 2014 13:17:43 +0200
84b277
Subject: [PATCH] localectl: print warning when there are options given on
84b277
 kernel cmdline
84b277
84b277
Conflicts:
84b277
        src/locale/localectl.c
84b277
84b277
(cherry picked from commit a34286684ebb78dd3db0d7f34feb2c121c9d00cc)
84b277
84b277
Related: #1069420
84b277
---
84b277
 Makefile.am              |  2 ++
84b277
 src/core/locale-setup.c  | 47 +++++---------------------------------------
84b277
 src/locale/localectl.c   | 51 ++++++++++++++++++++++++++++++++++++++++++++++++
84b277
 src/shared/locale-util.c | 42 +++++++++++++++++++++++++++++++++++++++
84b277
 src/shared/locale-util.h | 47 ++++++++++++++++++++++++++++++++++++++++++++
84b277
 5 files changed, 147 insertions(+), 42 deletions(-)
84b277
 create mode 100644 src/shared/locale-util.c
84b277
 create mode 100644 src/shared/locale-util.h
84b277
84b277
diff --git a/Makefile.am b/Makefile.am
84b277
index d54a556..dab55ba 100644
84b277
--- a/Makefile.am
84b277
+++ b/Makefile.am
84b277
@@ -656,6 +656,8 @@ libsystemd_shared_la_SOURCES = \
84b277
 	src/shared/path-util.h \
84b277
 	src/shared/time-util.c \
84b277
 	src/shared/time-util.h \
84b277
+	src/shared/locale-util.c \
84b277
+	src/shared/locale-util.h \
84b277
 	src/shared/hashmap.c \
84b277
 	src/shared/hashmap.h \
84b277
 	src/shared/set.c \
84b277
diff --git a/src/core/locale-setup.c b/src/core/locale-setup.c
84b277
index 276deb9..69e9bca 100644
84b277
--- a/src/core/locale-setup.c
84b277
+++ b/src/core/locale-setup.c
84b277
@@ -30,48 +30,11 @@
84b277
 #include "fileio.h"
84b277
 #include "strv.h"
84b277
 #include "env-util.h"
84b277
-
84b277
-enum {
84b277
-        /* We don't list LC_ALL here on purpose. People should be
84b277
-         * using LANG instead. */
84b277
-
84b277
-        VARIABLE_LANG,
84b277
-        VARIABLE_LANGUAGE,
84b277
-        VARIABLE_LC_CTYPE,
84b277
-        VARIABLE_LC_NUMERIC,
84b277
-        VARIABLE_LC_TIME,
84b277
-        VARIABLE_LC_COLLATE,
84b277
-        VARIABLE_LC_MONETARY,
84b277
-        VARIABLE_LC_MESSAGES,
84b277
-        VARIABLE_LC_PAPER,
84b277
-        VARIABLE_LC_NAME,
84b277
-        VARIABLE_LC_ADDRESS,
84b277
-        VARIABLE_LC_TELEPHONE,
84b277
-        VARIABLE_LC_MEASUREMENT,
84b277
-        VARIABLE_LC_IDENTIFICATION,
84b277
-        _VARIABLE_MAX
84b277
-};
84b277
-
84b277
-static const char * const variable_names[_VARIABLE_MAX] = {
84b277
-        [VARIABLE_LANG] = "LANG",
84b277
-        [VARIABLE_LANGUAGE] = "LANGUAGE",
84b277
-        [VARIABLE_LC_CTYPE] = "LC_CTYPE",
84b277
-        [VARIABLE_LC_NUMERIC] = "LC_NUMERIC",
84b277
-        [VARIABLE_LC_TIME] = "LC_TIME",
84b277
-        [VARIABLE_LC_COLLATE] = "LC_COLLATE",
84b277
-        [VARIABLE_LC_MONETARY] = "LC_MONETARY",
84b277
-        [VARIABLE_LC_MESSAGES] = "LC_MESSAGES",
84b277
-        [VARIABLE_LC_PAPER] = "LC_PAPER",
84b277
-        [VARIABLE_LC_NAME] = "LC_NAME",
84b277
-        [VARIABLE_LC_ADDRESS] = "LC_ADDRESS",
84b277
-        [VARIABLE_LC_TELEPHONE] = "LC_TELEPHONE",
84b277
-        [VARIABLE_LC_MEASUREMENT] = "LC_MEASUREMENT",
84b277
-        [VARIABLE_LC_IDENTIFICATION] = "LC_IDENTIFICATION"
84b277
-};
84b277
+#include "locale-util.h"
84b277
 
84b277
 int locale_setup(char ***environment) {
84b277
         char **add;
84b277
-        char *variables[_VARIABLE_MAX] = {};
84b277
+        char *variables[_VARIABLE_LC_MAX] = {};
84b277
         int r = 0, i;
84b277
 
84b277
         if (detect_container(NULL) <= 0) {
84b277
@@ -121,13 +84,13 @@ int locale_setup(char ***environment) {
84b277
         }
84b277
 
84b277
         add = NULL;
84b277
-        for (i = 0; i < _VARIABLE_MAX; i++) {
84b277
+        for (i = 0; i < _VARIABLE_LC_MAX; i++) {
84b277
                 char *s;
84b277
 
84b277
                 if (!variables[i])
84b277
                         continue;
84b277
 
84b277
-                s = strjoin(variable_names[i], "=", variables[i], NULL);
84b277
+                s = strjoin(locale_variable_to_string(i), "=", variables[i], NULL);
84b277
                 if (!s) {
84b277
                         r = -ENOMEM;
84b277
                         goto finish;
84b277
@@ -158,7 +121,7 @@ int locale_setup(char ***environment) {
84b277
 finish:
84b277
         strv_free(add);
84b277
 
84b277
-        for (i = 0; i < _VARIABLE_MAX; i++)
84b277
+        for (i = 0; i < _VARIABLE_LC_MAX; i++)
84b277
                 free(variables[i]);
84b277
 
84b277
         return r;
84b277
diff --git a/src/locale/localectl.c b/src/locale/localectl.c
84b277
index d3c6152..fdef3a0 100644
84b277
--- a/src/locale/localectl.c
84b277
+++ b/src/locale/localectl.c
84b277
@@ -39,6 +39,9 @@
84b277
 #include "path-util.h"
84b277
 #include "utf8.h"
84b277
 #include "def.h"
84b277
+#include "virt.h"
84b277
+#include "fileio.h"
84b277
+#include "locale-util.h"
84b277
 
84b277
 static bool arg_no_pager = false;
84b277
 static enum transport {
84b277
@@ -79,6 +82,53 @@ typedef struct StatusInfo {
84b277
         const char *x11_options;
84b277
 } StatusInfo;
84b277
 
84b277
+static void print_overriden_variables(void) {
84b277
+        int r;
84b277
+        char *variables[_VARIABLE_LC_MAX] = {};
84b277
+        LocaleVariable j;
84b277
+        bool print_warning = true;
84b277
+
84b277
+        if (detect_container(NULL) > 0 || arg_host)
84b277
+                return;
84b277
+
84b277
+        r = parse_env_file("/proc/cmdline", WHITESPACE,
84b277
+                           "locale.LANG",              &variables[VARIABLE_LANG],
84b277
+                           "locale.LANGUAGE",          &variables[VARIABLE_LANGUAGE],
84b277
+                           "locale.LC_CTYPE",          &variables[VARIABLE_LC_CTYPE],
84b277
+                           "locale.LC_NUMERIC",        &variables[VARIABLE_LC_NUMERIC],
84b277
+                           "locale.LC_TIME",           &variables[VARIABLE_LC_TIME],
84b277
+                           "locale.LC_COLLATE",        &variables[VARIABLE_LC_COLLATE],
84b277
+                           "locale.LC_MONETARY",       &variables[VARIABLE_LC_MONETARY],
84b277
+                           "locale.LC_MESSAGES",       &variables[VARIABLE_LC_MESSAGES],
84b277
+                           "locale.LC_PAPER",          &variables[VARIABLE_LC_PAPER],
84b277
+                           "locale.LC_NAME",           &variables[VARIABLE_LC_NAME],
84b277
+                           "locale.LC_ADDRESS",        &variables[VARIABLE_LC_ADDRESS],
84b277
+                           "locale.LC_TELEPHONE",      &variables[VARIABLE_LC_TELEPHONE],
84b277
+                           "locale.LC_MEASUREMENT",    &variables[VARIABLE_LC_MEASUREMENT],
84b277
+                           "locale.LC_IDENTIFICATION", &variables[VARIABLE_LC_IDENTIFICATION],
84b277
+                           NULL);
84b277
+
84b277
+        if (r < 0 && r != -ENOENT) {
84b277
+                log_warning("Failed to read /proc/cmdline: %s", strerror(-r));
84b277
+                goto finish;
84b277
+        }
84b277
+
84b277
+        for (j = VARIABLE_LANG; j < _VARIABLE_LC_MAX; j++)
84b277
+                if (variables[j]) {
84b277
+                        if (print_warning) {
84b277
+                                printf("Warning: Settings on Kernel Command Line override system locale settings in /etc/locale.conf\n");
84b277
+                                printf("    Command Line: %s=%s\n", locale_variable_to_string(j), variables[j]);
84b277
+
84b277
+                                print_warning = false;
84b277
+                                continue;
84b277
+                        }
84b277
+                        printf("                  %s=%s\n", locale_variable_to_string(j), variables[j]);
84b277
+                }
84b277
+ finish:
84b277
+        for (j = VARIABLE_LANG; j < _VARIABLE_LC_MAX; j++)
84b277
+                free(variables[j]);
84b277
+}
84b277
+
84b277
 static void print_status_info(StatusInfo *i) {
84b277
         assert(i);
84b277
 
84b277
@@ -218,6 +268,7 @@ static int show_status(DBusConnection *bus, char **args, unsigned n) {
84b277
                 dbus_message_iter_next(&sub);
84b277
         }
84b277
 
84b277
+        print_overriden_variables();
84b277
         print_status_info(&info;;
84b277
         strv_free(info.locale);
84b277
         return 0;
84b277
diff --git a/src/shared/locale-util.c b/src/shared/locale-util.c
84b277
new file mode 100644
84b277
index 0000000..d53b16c
84b277
--- /dev/null
84b277
+++ b/src/shared/locale-util.c
84b277
@@ -0,0 +1,42 @@
84b277
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
84b277
+
84b277
+/***
84b277
+  This file is part of systemd.
84b277
+
84b277
+  Copyright 2014 Lennart Poettering
84b277
+
84b277
+  systemd is free software; you can redistribute it and/or modify it
84b277
+  under the terms of the GNU Lesser General Public License as published by
84b277
+  the Free Software Foundation; either version 2.1 of the License, or
84b277
+  (at your option) any later version.
84b277
+
84b277
+  systemd is distributed in the hope that it will be useful, but
84b277
+  WITHOUT ANY WARRANTY; without even the implied warranty of
84b277
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
84b277
+  Lesser General Public License for more details.
84b277
+
84b277
+  You should have received a copy of the GNU Lesser General Public License
84b277
+  along with systemd; If not, see <http://www.gnu.org/licenses/>.
84b277
+***/
84b277
+
84b277
+#include "util.h"
84b277
+#include "locale-util.h"
84b277
+
84b277
+static const char * const locale_variable_table[_VARIABLE_LC_MAX] = {
84b277
+        [VARIABLE_LANG] = "LANG",
84b277
+        [VARIABLE_LANGUAGE] = "LANGUAGE",
84b277
+        [VARIABLE_LC_CTYPE] = "LC_CTYPE",
84b277
+        [VARIABLE_LC_NUMERIC] = "LC_NUMERIC",
84b277
+        [VARIABLE_LC_TIME] = "LC_TIME",
84b277
+        [VARIABLE_LC_COLLATE] = "LC_COLLATE",
84b277
+        [VARIABLE_LC_MONETARY] = "LC_MONETARY",
84b277
+        [VARIABLE_LC_MESSAGES] = "LC_MESSAGES",
84b277
+        [VARIABLE_LC_PAPER] = "LC_PAPER",
84b277
+        [VARIABLE_LC_NAME] = "LC_NAME",
84b277
+        [VARIABLE_LC_ADDRESS] = "LC_ADDRESS",
84b277
+        [VARIABLE_LC_TELEPHONE] = "LC_TELEPHONE",
84b277
+        [VARIABLE_LC_MEASUREMENT] = "LC_MEASUREMENT",
84b277
+        [VARIABLE_LC_IDENTIFICATION] = "LC_IDENTIFICATION"
84b277
+};
84b277
+
84b277
+DEFINE_STRING_TABLE_LOOKUP(locale_variable, LocaleVariable);
84b277
diff --git a/src/shared/locale-util.h b/src/shared/locale-util.h
84b277
new file mode 100644
84b277
index 0000000..420a89d
84b277
--- /dev/null
84b277
+++ b/src/shared/locale-util.h
84b277
@@ -0,0 +1,47 @@
84b277
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
84b277
+
84b277
+/***
84b277
+  This file is part of systemd.
84b277
+
84b277
+  Copyright 2014 Lennart Poettering
84b277
+
84b277
+  systemd is free software; you can redistribute it and/or modify it
84b277
+  under the terms of the GNU Lesser General Public License as published by
84b277
+  the Free Software Foundation; either version 2.1 of the License, or
84b277
+  (at your option) any later version.
84b277
+
84b277
+  systemd is distributed in the hope that it will be useful, but
84b277
+  WITHOUT ANY WARRANTY; without even the implied warranty of
84b277
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
84b277
+  Lesser General Public License for more details.
84b277
+
84b277
+  You should have received a copy of the GNU Lesser General Public License
84b277
+  along with systemd; If not, see <http://www.gnu.org/licenses/>.
84b277
+***/
84b277
+
84b277
+#include "util.h"
84b277
+
84b277
+typedef enum LocaleVariable {
84b277
+        /* We don't list LC_ALL here on purpose. People should be
84b277
+         * using LANG instead. */
84b277
+
84b277
+        VARIABLE_LANG,
84b277
+        VARIABLE_LANGUAGE,
84b277
+        VARIABLE_LC_CTYPE,
84b277
+        VARIABLE_LC_NUMERIC,
84b277
+        VARIABLE_LC_TIME,
84b277
+        VARIABLE_LC_COLLATE,
84b277
+        VARIABLE_LC_MONETARY,
84b277
+        VARIABLE_LC_MESSAGES,
84b277
+        VARIABLE_LC_PAPER,
84b277
+        VARIABLE_LC_NAME,
84b277
+        VARIABLE_LC_ADDRESS,
84b277
+        VARIABLE_LC_TELEPHONE,
84b277
+        VARIABLE_LC_MEASUREMENT,
84b277
+        VARIABLE_LC_IDENTIFICATION,
84b277
+        _VARIABLE_LC_MAX,
84b277
+        _VARIABLE_LC_INVALID = -1
84b277
+} LocaleVariable;
84b277
+
84b277
+const char* locale_variable_to_string(LocaleVariable i) _const_;
84b277
+LocaleVariable locale_variable_from_string(const char *s) _pure_;