sys_unix.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. //go:build aix || darwin || dragonfly || freebsd || hurd || (js && wasm) || linux || 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. func open(name string) (uintptr, error) {
  15. fd, err := syscall.Open(name, syscall.O_RDONLY, 0)
  16. if err != nil {
  17. return 0, err
  18. }
  19. return uintptr(fd), nil
  20. }
  21. func read(fd uintptr, buf []byte) (int, error) {
  22. return syscall.Read(int(fd), buf)
  23. }
  24. func closefd(fd uintptr) {
  25. syscall.Close(int(fd))
  26. }
  27. func preadn(fd uintptr, buf []byte, off int) error {
  28. whence := seekStart
  29. if off < 0 {
  30. whence = seekEnd
  31. }
  32. if _, err := syscall.Seek(int(fd), int64(off), whence); err != nil {
  33. return err
  34. }
  35. for len(buf) > 0 {
  36. m, err := syscall.Read(int(fd), buf)
  37. if m <= 0 {
  38. if err == nil {
  39. return errors.New("short read")
  40. }
  41. return err
  42. }
  43. buf = buf[m:]
  44. }
  45. return nil
  46. }