sys_unix.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // Copyright 2011 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. // +build darwin dragonfly freebsd linux nacl netbsd openbsd solaris
  5. package time
  6. import (
  7. "errors"
  8. "syscall"
  9. )
  10. // for testing: whatever interrupts a sleep
  11. func interrupt() {
  12. syscall.Kill(syscall.Getpid(), syscall.SIGCHLD)
  13. }
  14. // readFile reads and returns the content of the named file.
  15. // It is a trivial implementation of ioutil.ReadFile, reimplemented
  16. // here to avoid depending on io/ioutil or os.
  17. func readFile(name string) ([]byte, error) {
  18. f, err := syscall.Open(name, syscall.O_RDONLY, 0)
  19. if err != nil {
  20. return nil, err
  21. }
  22. defer syscall.Close(f)
  23. var (
  24. buf [4096]byte
  25. ret []byte
  26. n int
  27. )
  28. for {
  29. n, err = syscall.Read(f, buf[:])
  30. if n > 0 {
  31. ret = append(ret, buf[:n]...)
  32. }
  33. if n == 0 || err != nil {
  34. break
  35. }
  36. }
  37. return ret, err
  38. }
  39. func open(name string) (uintptr, error) {
  40. fd, err := syscall.Open(name, syscall.O_RDONLY, 0)
  41. if err != nil {
  42. return 0, err
  43. }
  44. return uintptr(fd), nil
  45. }
  46. func closefd(fd uintptr) {
  47. syscall.Close(int(fd))
  48. }
  49. func preadn(fd uintptr, buf []byte, off int) error {
  50. whence := 0
  51. if off < 0 {
  52. whence = 2
  53. }
  54. if _, err := syscall.Seek(int(fd), int64(off), whence); err != nil {
  55. return err
  56. }
  57. for len(buf) > 0 {
  58. m, err := syscall.Read(int(fd), buf)
  59. if m <= 0 {
  60. if err == nil {
  61. return errors.New("short read")
  62. }
  63. return err
  64. }
  65. buf = buf[m:]
  66. }
  67. return nil
  68. }