sys_linux.go 517 B

123456789101112131415161718192021222324252627
  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.
  4. // Linux-specific
  5. package os
  6. func hostname() (name string, err error) {
  7. f, err := Open("/proc/sys/kernel/hostname")
  8. if err != nil {
  9. return "", err
  10. }
  11. defer f.Close()
  12. var buf [512]byte // Enough for a DNS name.
  13. n, err := f.Read(buf[0:])
  14. if err != nil {
  15. return "", err
  16. }
  17. if n > 0 && buf[n-1] == '\n' {
  18. n--
  19. }
  20. return string(buf[0:n]), nil
  21. }