2009-06-29 14:44:46 -06: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.
|
|
|
|
|
|
|
|
// Linux-specific
|
|
|
|
|
|
|
|
package os
|
|
|
|
|
2012-01-16 22:51:54 -07:00
|
|
|
func hostname() (name string, err error) {
|
2011-04-05 00:42:14 -06:00
|
|
|
f, err := Open("/proc/sys/kernel/hostname")
|
2009-06-29 14:44:46 -06:00
|
|
|
if err != nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
return "", err
|
2009-06-29 14:44:46 -06:00
|
|
|
}
|
2009-12-15 16:40:16 -07:00
|
|
|
defer f.Close()
|
2009-06-29 14:44:46 -06:00
|
|
|
|
2009-12-15 16:40:16 -07:00
|
|
|
var buf [512]byte // Enough for a DNS name.
|
2010-05-27 15:51:47 -06:00
|
|
|
n, err := f.Read(buf[0:])
|
2009-06-29 14:44:46 -06:00
|
|
|
if err != nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
return "", err
|
2009-06-29 14:44:46 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if n > 0 && buf[n-1] == '\n' {
|
2009-11-09 13:07:39 -07:00
|
|
|
n--
|
2009-06-29 14:44:46 -06:00
|
|
|
}
|
2009-12-15 16:40:16 -07:00
|
|
|
return string(buf[0:n]), nil
|
2009-06-29 14:44:46 -06:00
|
|
|
}
|