sys_unix.mx raw

   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  
   5  //go:build unix || (js && wasm) || wasip1
   6  
   7  package time
   8  
   9  import (
  10  	"errors"
  11  	"runtime"
  12  	"syscall"
  13  )
  14  
  15  // for testing: whatever interrupts a sleep
  16  func interrupt() {
  17  	// There is no mechanism in wasi to interrupt the call to poll_oneoff
  18  	// used to implement runtime.usleep so this function does nothing, which
  19  	// somewhat defeats the purpose of TestSleep but we are still better off
  20  	// validating that time elapses when the process calls time.Sleep than
  21  	// skipping the test altogether.
  22  	if runtime.GOOS != "wasip1" {
  23  		syscall.Kill(syscall.Getpid(), syscall.SIGCHLD)
  24  	}
  25  }
  26  
  27  func open(name []byte) (uintptr, error) {
  28  	fd, err := syscall.Open(name, syscall.O_RDONLY, 0)
  29  	if err != nil {
  30  		return 0, err
  31  	}
  32  	return uintptr(fd), nil
  33  }
  34  
  35  func read(fd uintptr, buf []byte) (int, error) {
  36  	return syscall.Read(int(fd), buf)
  37  }
  38  
  39  func closefd(fd uintptr) {
  40  	syscall.Close(int(fd))
  41  }
  42  
  43  func preadn(fd uintptr, buf []byte, off int) error {
  44  	whence := seekStart
  45  	if off < 0 {
  46  		whence = seekEnd
  47  	}
  48  	if _, err := syscall.Seek(int(fd), int64(off), whence); err != nil {
  49  		return err
  50  	}
  51  	for len(buf) > 0 {
  52  		m, err := syscall.Read(int(fd), buf)
  53  		if m <= 0 {
  54  			if err == nil {
  55  				return errors.New("short read")
  56  			}
  57  			return err
  58  		}
  59  		buf = buf[m:]
  60  	}
  61  	return nil
  62  }
  63