1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
12 func hostname() (name
string, err error
) {
13 // Try uname first, as it's only one system call and reading
14 // from /proc is not allowed on Android.
15 var un syscall
.Utsname
16 err
= syscall
.Uname(&un
)
18 var buf
[512]byte // Enough for a DNS name.
19 for i
, b
:= range un
.Nodename
[:] {
22 name
= string(buf
[:i
])
26 // If we got a name and it's not potentially truncated
27 // (Nodename is 65 bytes), return it.
28 if err
== nil && len(name
) > 0 && len(name
) < 64 {
31 if runtime
.GOOS
== "android" {
35 return "localhost", nil
38 f
, err
:= Open("/proc/sys/kernel/hostname")
44 n
, err
:= f
.Read(buf
[:])
49 if n
> 0 && buf
[n
-1] == '\n' {
52 return string(buf
[:n
]), nil