2010-12-03 05:34:57 +01:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package os
|
|
|
|
|
2018-09-24 23:46:21 +02:00
|
|
|
import (
|
|
|
|
"runtime"
|
|
|
|
"syscall"
|
|
|
|
)
|
|
|
|
|
2012-01-25 22:54:22 +01:00
|
|
|
func hostname() (name string, err error) {
|
2018-09-24 23:46:21 +02:00
|
|
|
// 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])
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2011-04-07 19:09:10 +02:00
|
|
|
f, err := Open("/proc/sys/kernel/hostname")
|
2010-12-03 05:34:57 +01:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
2018-09-24 23:46:21 +02:00
|
|
|
n, err := f.Read(buf[:])
|
2010-12-03 05:34:57 +01:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if n > 0 && buf[n-1] == '\n' {
|
|
|
|
n--
|
|
|
|
}
|
2018-09-24 23:46:21 +02:00
|
|
|
return string(buf[:n]), nil
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|