Skip to content

Commit

Permalink
os: find Hostname using Uname to fix Android
Browse files Browse the repository at this point in the history
It's also fewer system calls. Fall back to longer read
only if it seems like the Uname result is truncated.

Fixes golang#24701

Change-Id: Ib6550acede8dddaf184e8fa9de36377e17bbddab
Reviewed-on: https://go-review.googlesource.com/110295
Reviewed-by: Ian Lance Taylor <[email protected]>
  • Loading branch information
bradfitz committed Apr 30, 2018
1 parent 4bced5e commit 3c7456c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
11 changes: 10 additions & 1 deletion src/os/os_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1544,7 +1544,16 @@ func TestHostname(t *testing.T) {
// On Plan 9 it can be taken from #c/sysname as Hostname() does.
switch runtime.GOOS {
case "android", "plan9":
t.Skipf("%s doesn't have /bin/hostname", runtime.GOOS)
// No /bin/hostname to verify against, but at least
// verify we get something back from Hostname.
hostname, err := Hostname()
if err != nil {
t.Fatal(err)
}
if hostname == "" {
t.Fatal("Hostname returned empty string and no error")
}
return
case "windows":
testWindowsHostname(t)
return
Expand Down
34 changes: 31 additions & 3 deletions src/os/sys_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,49 @@

package os

import (
"runtime"
"syscall"
)

func hostname() (name string, err error) {
// Try uname first, as it's only one system call and reading
// from /proc is not allowed on Android.
var un syscall.Utsname
err = syscall.Uname(&un)

var buf [512]byte // Enough for a DNS name.
for i, b := range un.Nodename[:] {
buf[i] = uint8(b)
if b == 0 {
name = string(buf[:i])
}
}
// If we got a name and it's not potentially truncated
// (Nodename is 65 bytes), return it.
if err == nil && len(name) > 0 && len(name) < 64 {
return name, nil
}
if runtime.GOOS == "android" {
if name != "" {
return name, nil
}
return "localhost", nil
}

f, err := Open("/proc/sys/kernel/hostname")
if err != nil {
return "", err
}
defer f.Close()

var buf [512]byte // Enough for a DNS name.
n, err := f.Read(buf[0:])
n, err := f.Read(buf[:])
if err != nil {
return "", err
}

if n > 0 && buf[n-1] == '\n' {
n--
}
return string(buf[0:n]), nil
return string(buf[:n]), nil
}

0 comments on commit 3c7456c

Please sign in to comment.