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

Use POSIX-compliant uname instead of hostname #14

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

kissge
Copy link

@kissge kissge commented Dec 15, 2022

Problem

shell_intergration scripts depends on hostname(1), assuming it's available on every host including remote ones (over SSH).
However that's not the case with some Linux environments.
For example, in Arch Linux you need install inetutils package.
This leads to seeing the following error message every time I login to such hosts: shell_integration/zsh:164: command not found: hostname.

Solution

This PR aims to fix the problem by simply replacing all hostname(1) calls to uname(1).
This makes two differences:

  1. Unlike hostname(1), uname(1) is POSIX-compliant1. uname -n is available on practically every UNIX-like system including FreeBSD, NetBSD, and OpenBSD.
  2. On Linux, uname -n is faster because it's simply invoking uname(2) syscall2. On the other hand, hostname -f uses gethostbyname(3)3 which causes name server lookups every time.

Discussion

  1. I'm not 100% sure if it's safe to assume every host has uname(1). I believe so, though.
  2. If that's not the case, should we have fallbacks to hostname(1)?

This is my first contribution to your repository.
Please forgive me if it's not your taste; I'd appreciate if you can help improving this PR.

Thanks for this lovely product!

Footnotes

  1. https://pubs.opengroup.org/onlinepubs/009695299/utilities/uname.html

  2. https://github.com/coreutils/coreutils/blob/9581c4b59d2e11cd090613cd300a833b4441d2b5/src/uname.c#L299-L305

  3. https://git.savannah.gnu.org/cgit/inetutils.git/tree/src/hostname.c?id=c1b8d134d542e7e81c3dcd6adbd05305cb2c78df#n290

@gnachman
Copy link
Owner

gnachman commented Jan 2, 2023

Sorry for the delay in getting to this PR. The hostname is used by the scp feature to create a new connection, so it needs to be a fully qualified domain name. On Ubuntu 18 I get this:

bryan:/home/gnachman% hostname -f
bryan.dreamhost.com
bryan:/home/gnachman% uname -n
bryan

The problem you raise is real—especially the performance concern!—and I'm definitely interested in a solution, but I'm not sure what that might look like.

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