Skip to content

Linux: native-free NetworkParams.getHostName() diverges from JNA and reports localhost when the host name is not resolvable #3669

Closed

Description

@joshiste

LinuxNetworkParamsNF declares no getHostName(), so the native-free provider inherits AbstractNetworkParams#getHostName(), which calls InetAddress.getLocalHost(). That resolves the name rather than reading it, and diverges from the JNA/FFM backends on the same machine in two ways:

  1. it truncates at the first dot, so an FQDN kernel host name is reported short;
  2. when the host name does not resolve — routine in a container, or with no /etc/hosts entry — it degrades to getLoopbackAddress() and reports localhost. It can also block on a DNS timeout.

This is the same problem #3566 fixed for OpenBSD, whose description applies verbatim here, and the same class of gap #3480 swept for native-free Linux. LinuxNetworkParams' own javadoc already states "Subclasses provide getDomainName() and getHostName() via native calls" — which the native-free subclass structurally cannot do, so it silently gets the JDK path instead.

getDomainName() shows the same localhost degradation (last column below).

Reproducer

Repro.java, run against both providers in the same JVM:

import oshi.spi.SystemInfoProvider;

public class Repro {
    public static void main(String[] args) throws Exception {
        System.out.println("kernel host name (/proc/sys/kernel/hostname) = "
                + java.nio.file.Files.readString(java.nio.file.Path.of("/proc/sys/kernel/hostname")).strip());
        report("oshi-core     (JNA)", new oshi.SystemInfo());
        report("oshi-common   (native-free)", new oshi.nativefree.SystemInfo());
    }

    private static void report(String label, SystemInfoProvider provider) {
        var np = provider.getOperatingSystem().getNetworkParams();
        System.out.printf("%-28s getHostName() = %-20s getDomainName() = %s%n", label, np.getHostName(), np.getDomainName());
    }
}

With oshi-core 7.5.0, oshi-common 7.5.0, jna-jpms/jna-platform-jpms 5.19.1 and slf4j-api on the classpath:

# 1. FQDN kernel host name, resolvable
docker run --rm --hostname host.example.com -v "$PWD":/r:ro -w /r \
  ghcr.io/steadybit/zulu-openjdk:java-25-jre \
  sh -c 'java --enable-native-access=ALL-UNNAMED -cp "/r/libs/*" /r/Repro.java'

# 2. short kernel host name, not resolvable
docker run --rm --hostname buildbox -v "$PWD":/r:ro -w /r \
  ghcr.io/steadybit/zulu-openjdk:java-25-jre \
  sh -c 'grep -v buildbox /etc/hosts > /tmp/h; cat /tmp/h > /etc/hosts;
         java --enable-native-access=ALL-UNNAMED -cp "/r/libs/*" /r/Repro.java'

Observed

######## case 1: FQDN kernel host name, resolvable
kernel host name (/proc/sys/kernel/hostname) = host.example.com
oshi-core     (JNA)          getHostName() = host.example.com     getDomainName() = host.example.com
oshi-common   (native-free)  getHostName() = host                 getDomainName() = host.example.com

######## case 2: short kernel host name, NOT resolvable
kernel host name (/proc/sys/kernel/hostname) = buildbox
oshi-core     (JNA)          getHostName() = buildbox             getDomainName() =
oshi-common   (native-free)  getHostName() = localhost            getDomainName() = localhost

Expected

Both backends report the kernel host name — host.example.com and buildbox respectively — or at minimum agree with each other.

Case 2 is the damaging one: localhost is not a "degraded" answer but a wrong one, indistinguishable from a machine actually named localhost. We hit this using the host name as a node identifier; every node in a cluster would have collided on localhost had we not compared the two backends.

Suggested fix

Read /proc/sys/kernel/hostname. It is byte-for-byte what gethostname(2) returns — both read utsname.nodename of the caller's UTS namespace, so it stays correct inside a container — and it needs no native call, which keeps the native-free contract. oshi.util.driver.linux.proc already hosts procfs drivers such as RouteTable.

Shape would follow #3566: give LinuxNetworkParams a queryHostName() hook with the prefer-primary-fall-back-to-super override, and let LinuxNetworkParamsNF supply the procfs read while the JNA/FFM subclasses keep gethostname.

Whether the result should then be truncated at the first dot is a separate policy question — AbstractNetworkParams truncates, the Linux JNA backend does not — but the two backends should not disagree either way.

Environment

  • OSHI 7.5.0 (also reproduced on 7.4.2)
  • JDK 25 (Zulu 25.0.2), linux/aarch64 in Docker

Activity

  1. dbwiddis commented on Aug 20, 2026

    @dbwiddis
    Member

    Hmm. Seems we knew this diverged:

    // NF uses Java InetAddress (short hostname); JNA uses native gethostname (may return FQDN)
    assertThat(jna.getHostName()).startsWith(nf.getHostName());

    I agree reading procfs is the right answer, but rather than adding a hook, we should probably go the opposite direction and make the procfs call the primary one everywhere. It's slower on the scale of 2 microseconds vs. a 4 ms average for the entire call, and saves the object creation of the InetAddress object.

  2. added
    confirmed bug
    on Aug 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    confirmed bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions