Skip to content

Comments

filesystem: skip hostname exec when HOSTNAME is already set#6053

Open
dand-oss wants to merge 1 commit intomsys2:masterfrom
dand-oss:guard-hostname-in-profile
Open

filesystem: skip hostname exec when HOSTNAME is already set#6053
dand-oss wants to merge 1 commit intomsys2:masterfrom
dand-oss:guard-hostname-in-profile

Conversation

@dand-oss
Copy link

@dand-oss dand-oss commented Feb 19, 2026

Summary

Guard the HOSTNAME="$(exec /usr/bin/hostname)" assignments in /etc/profile with [ -z "${HOSTNAME}" ] so the redundant fork/exec is skipped.

Problem

Bash sets $HOSTNAME as a builtin shell variable at startup — before /etc/profile runs. The explicit exec /usr/bin/hostname in /etc/profile unconditionally overwrites it with the same value, making the call 100% redundant for bash users.

This matters because on MSYS2/Cygwin, every fork+exec costs ~50-130ms due to Windows lacking copy-on-write fork. The hostname call alone adds ~127ms to every login shell startup — for a value bash already has.

Demonstration that the builtin is identical to the external command:

$ bash --noprofile --norc -c 'echo $HOSTNAME'
bugger-wg1
$ /usr/bin/hostname
bugger-wg1

Fix

Add [ -z "${HOSTNAME}" ] guard before each HOSTNAME assignment. This is consistent with the existing pattern used by other profile.d scripts:

  • profile.lang.sh guards with test -z "${LC_ALL}"
  • profile.tzset.sh guards with test -z "$TZ"
  • profile.000-msys2.sh guards with __MSYS2_WINDOWS_VERSION_WARNING_DONE

The guard also benefits ksh/zsh/posh users who pre-set HOSTNAME via their environment, while preserving the fallback for shells that don't set it as a builtin.

Impact

Saves ~127ms per login shell startup on Windows — a ~25% reduction in /etc/profile execution time.

Bash, zsh, and other shells set HOSTNAME as a builtin variable at
startup. The explicit `exec /usr/bin/hostname` in /etc/profile is
redundant and costs ~127ms per login on Windows due to Cygwin's
expensive fork/exec. Guard the assignment with `[ -z "${HOSTNAME}" ]`
so it only runs when HOSTNAME is not already set.

This is consistent with the existing guards in profile.d scripts
(lang.sh guards LC_ALL, tzset.sh guards TZ, 000-msys2.sh guards
__MSYS2_WINDOWS_VERSION_WARNING_DONE).
@lazka
Copy link
Member

lazka commented Feb 20, 2026

Sounds reasonable, thank you

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.

2 participants