Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

base-files: don't overwrite existing locale and define default LANG #39264

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

oreo639
Copy link
Member

@oreo639 oreo639 commented Sep 13, 2022

Testing the changes

  • I tested the changes in this PR: YES

Fixes overwriting user/desktop specified locale.

Currently we always overwrite LANG on login, even if it was already set by the desktop or user which causes issues with GDM.

You can see what Arch does here:
https://bugs.archlinux.org/task/42162
https://github.com/archlinux/svntogit-packages/blob/master/filesystem/trunk/locale.sh

You can see what Fedora does here:
https://gist.github.com/oreo639/3b83b852ce0db21e21eed102a57c5148
(taken from the setup package)

Closes: #15292

@github-actions
Copy link

Pull Requests become stale 90 days after last activity and are closed 14 days after that. If this pull request is still relevant bump it or assign it.

@github-actions github-actions bot added the Stale label Dec 26, 2022
@oreo639
Copy link
Member Author

oreo639 commented Dec 26, 2022

bump

@leahneukirchen
Copy link
Member

Why would this work? Doesn't bash start with /etc/profile, which then sources /etc/profile.d/*?

In any case, setting it later in .profile overrides this.

@oreo639
Copy link
Member Author

oreo639 commented Jan 2, 2023

Because GDM sets LANG and then gnome-session launches bash as a login shell so that /etc/profile can be applied which overwrites the previous LANG preference.

This PR adds a check for if LANG is already defined (i.e. by the user/DE/etc) before applying the system-wide preference.

@oreo639 oreo639 force-pushed the files branch 10 times, most recently from 992fb1b to 7d281b0 Compare June 21, 2023 11:47
@github-actions
Copy link

Pull Requests become stale 90 days after last activity and are closed 14 days after that. If this pull request is still relevant bump it or assign it.

@github-actions github-actions bot added the Stale label Sep 20, 2023
@oreo639
Copy link
Member Author

oreo639 commented Sep 20, 2023

bump

@github-actions github-actions bot removed the Stale label Sep 21, 2023
@leahneukirchen
Copy link
Member

This feels very crude... I wonder if this would work instead:

while read -r line < /etc/locale.conf; do
	eval ": \${{$line}}"
done

@ahesford
Copy link
Member

I don't think we should be worrying about this. The stuff in /etc/profile and /etc/profile.d is supposed to be read for login shells, which means that the shell is expected to stand up the environment from scratch. It isn't really a bug that login shells overwrite whatever locale information already exists in the environment.

Hacking around the fact that GDM expects its forcing of these variables to be respected seems best resolved by looking to GDM_LANG in some gdm-specific profile.d snippet that runs after the default locale is configured.

Copy link

Pull Requests become stale 90 days after last activity and are closed 14 days after that. If this pull request is still relevant bump it or assign it.

@github-actions github-actions bot added the Stale label Dec 22, 2023
@oreo639
Copy link
Member Author

oreo639 commented Dec 22, 2023

bump

@ahesford
Copy link
Member

The parsing of locale.conf seem to be holdovers from aborted attempts to save and restore variables. Reading the lines, trying to strip out comments and then running the lines through eval doesn't really do anything different than sourcing the file, except inject some incomplete and cumbersome parsing logic here.

The locale.conf parsing should just be restored to simply sourcing the file, leavening only the default LANG and typo fix.

@oreo639
Copy link
Member Author

oreo639 commented Dec 22, 2023

Reading the lines, trying to strip out comments and then running the lines through eval doesn't really do anything different than sourcing the file, except inject some incomplete and cumbersome parsing logic here.

It prevents locale.sh from overwriting e.g. LANG if it was already defined and uses locale.conf as the default values if they are not defined.

e.g. : ${LANG=en_US.UTF-8}

@ahesford
Copy link
Member

ahesford commented Dec 22, 2023

Thanks; I wasn't reasoning through the assignment properly.

There are still problems sanitizing input. For example, if somebody has a file like

LANG=en_US.UTF-8 # English

it will contain a trailing space after the change but is currently properly parsed. I don't know whether any of the consumers of locale variables would be harmed by the addition of spurious whitespace, but I'd be surprised if this didn't introduce problems.

I have not found any documentation on locale.conf besides systemd manual pages. As I said before (apparently elsewhere; I thought I commented here earlier), we should not feel compelled to abide by the restrictions imposed by systemd against using other shell features in the configuration. Because we offer no manual page on the file, the code is the documentation; the code documents that the file is simply sourced. (The limitation in systemd, I assume, is that it parses the file directly. We don't parse the file anywhere but in shell profiles.) Users may currently be exploiting that to add extra logic to the locale configuration.

What Red Hat does to interpret the file is an ugly kludge that we should avoid. If anything, I think the Arch way is preferable; just let a pre-set LANG gate the sourcing of the file and, if some users really care about more complex preservation of the whole gamut of variables, they can implement a custom post-locale.sh to do so. However, I again question the need for this. Given we have established precedent that locale.conf is just sourced by a shell, users should feel free to do things like

: ${LANG=my-lang}

if they need to preserve a pre-set value.

Whatever the final form looks like, we should include a manual page or at least fully define on the docs web site what we accept as a valid configuration.

@github-actions github-actions bot removed the Stale label Dec 23, 2023
@oreo639
Copy link
Member Author

oreo639 commented Dec 23, 2023

There are still problems sanitizing input. For example, if somebody has a file like

Thank you for pointing that out.

I have not found any documentation on locale.conf besides systemd manual pages.

locale.conf originates from systemd, before that distributions had their own locale configuration files, e.g. /etc/sysconfig/i18n on RHEL or /etc/default/locale on debian. Arch Linux used to recommend just setting LANG in your .bashrc (or /etc/rc.conf for system wide).

I think the Arch way is preferable; just let a pre-set LANG gate the sourcing of the file

That was the original way I implemented this PR and people didn't like it.

Given we have established precedent that locale.conf is just sourced by a shell, users should feel free to do things like

Personally, I would prefer for that to continue to work as it did before to not break things for people, although I don't think it should be promoted or listed as supported.

@oreo639 oreo639 force-pushed the files branch 4 times, most recently from 1c6993b to 5351aa0 Compare March 1, 2024 05:26
@oreo639
Copy link
Member Author

oreo639 commented Mar 1, 2024

I updated this to only replace plain declarations (e.g. VAR=x, but not : VAR=x, export VAR=x, etc)
This allows everything else to work as it did before (e.g. if statements) and you can force overriding with : LANG=en_US.UTF-8

@ahesford
Copy link
Member

ahesford commented Mar 1, 2024

I think this is getting far too complicated for the simple intent of preserving variables that have already been set. I had to read that loop several times to figure out what it was doing even knowing your intent.

It's also fragile. You're effectively just sourcing the file after wrapping all simple variable assignments in : ${VAR=value} to preserve variables already set by the environment while trying to honor existing workflows that might try to inject more complicated behavior into locale.conf, but there's no way to guarantee that these kinds of modifications to what's executed during the eval won't have all sorts of unintended consequences.

I think there are four reasonable ways to solve your problem (with varying degrees of reasonability):

  1. Leave the behavior as is and let users guard variables they might not want to overwrite, knowing that locale.conf is executed by the shell.
  2. Gate the sourcing like in Arch, which you said people don't like. But do those people who objected really prefer writing trying to implement an even more complex shell parser in the shell itself?
  3. Make backup copies of all relevant variables, just source the file as we already do, and restore the backup copies if they were nonempty before the source. I think you did this, in a couple of different ways, in an earlier proposal.
  4. Properly validate that locale.conf contains nothing more than comments or simple VAR=value assignments. Reject it entirely if it does not conform, otherwise parse accordingly.

@oreo639 oreo639 force-pushed the files branch 2 times, most recently from 424cf9f to 7c2ab05 Compare March 1, 2024 22:31
@oreo639
Copy link
Member Author

oreo639 commented Mar 1, 2024

3. I think you did this, in a couple of different ways, in an earlier proposal.

Here is what I did for that: https://gist.github.com/oreo639/850ddb140df502781f1f54fc1ffaba0c

4. Properly validate that locale.conf contains nothing more than comments or simple VAR=value assignments. Reject it entirely if it does not conform, otherwise parse accordingly.

I pushed a version using that approach, although it will still accept lines that are valid (instead of rejecting the entire file)
I also updated it to use the regex from Fedora to check for a valid assignment.

@oreo639 oreo639 force-pushed the files branch 7 times, most recently from f77b269 to f599828 Compare March 2, 2024 00:14
@leahneukirchen
Copy link
Member

error messages should go to stderr

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Gnome language setting being overwritten
4 participants