First implementation
This commit is contained in:
100
vendor/periph.io/x/periph/host/fs/fs.go
generated
vendored
Normal file
100
vendor/periph.io/x/periph/host/fs/fs.go
generated
vendored
Normal file
@ -0,0 +1,100 @@
|
||||
// Copyright 2017 The Periph Authors. All rights reserved.
|
||||
// Use of this source code is governed under the Apache License, Version 2.0
|
||||
// that can be found in the LICENSE file.
|
||||
|
||||
// Package fs provides access to the file system on the host.
|
||||
//
|
||||
// It exposes ioctl syscall and epoll in an OS agnostic way and permits
|
||||
// completely disabling file access to lock down unit tests.
|
||||
package fs
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// Ioctler is a file handle that supports ioctl calls.
|
||||
type Ioctler interface {
|
||||
// Ioctl sends a linux ioctl on the file handle.
|
||||
//
|
||||
// op is effectively an uint32. op is expected to be encoded in the format on
|
||||
// x64. ARM happens to share the same format.
|
||||
Ioctl(op uint, data uintptr) error
|
||||
}
|
||||
|
||||
// Open opens a file.
|
||||
//
|
||||
// Returns an error if Inhibit() was called.
|
||||
func Open(path string, flag int) (*File, error) {
|
||||
mu.Lock()
|
||||
if inhibited {
|
||||
mu.Unlock()
|
||||
return nil, errors.New("file I/O is inhibited")
|
||||
}
|
||||
used = true
|
||||
mu.Unlock()
|
||||
|
||||
f, err := os.OpenFile(path, flag, 0600)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &File{f}, nil
|
||||
}
|
||||
|
||||
// Inhibit inhibits any future file I/O. It panics if any file was opened up to
|
||||
// now.
|
||||
//
|
||||
// It should only be called in unit tests.
|
||||
func Inhibit() {
|
||||
mu.Lock()
|
||||
inhibited = true
|
||||
if used {
|
||||
panic("calling Inhibit() while files were already opened")
|
||||
}
|
||||
mu.Unlock()
|
||||
}
|
||||
|
||||
// File is a superset of os.File.
|
||||
type File struct {
|
||||
*os.File
|
||||
}
|
||||
|
||||
// Ioctl sends an ioctl to the file handle.
|
||||
func (f *File) Ioctl(op uint, data uintptr) error {
|
||||
return ioctl(f.Fd(), op, data)
|
||||
}
|
||||
|
||||
// Event is a file system event.
|
||||
type Event struct {
|
||||
event
|
||||
}
|
||||
|
||||
// MakeEvent initializes an epoll *edge* triggered event on linux.
|
||||
//
|
||||
// An edge triggered event is basically an "auto-reset" event, where waiting on
|
||||
// the edge resets it. A level triggered event requires manual resetting; this
|
||||
// could be done via a Read() call but there's no need to require the user to
|
||||
// call Read(). This is particularly useless in the case of gpio.RisingEdge and
|
||||
// gpio.FallingEdge.
|
||||
//
|
||||
// As per the official doc, edge triggers is still remembered even when no
|
||||
// epoll_wait() call is running, so no edge is missed. Two edges will be
|
||||
// coallesced into one if the user mode process can't keep up. There's no
|
||||
// accumulation of edges.
|
||||
func (e *Event) MakeEvent(fd uintptr) error {
|
||||
return e.event.makeEvent(fd)
|
||||
}
|
||||
|
||||
// Wait waits for an event or the specified amount of time.
|
||||
func (e *Event) Wait(timeoutms int) (int, error) {
|
||||
return e.event.wait(timeoutms)
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
var (
|
||||
mu sync.Mutex
|
||||
inhibited bool
|
||||
used bool
|
||||
)
|
124
vendor/periph.io/x/periph/host/fs/fs_linux.go
generated
vendored
Normal file
124
vendor/periph.io/x/periph/host/fs/fs_linux.go
generated
vendored
Normal file
@ -0,0 +1,124 @@
|
||||
// Copyright 2017 The Periph Authors. All rights reserved.
|
||||
// Use of this source code is governed under the Apache License, Version 2.0
|
||||
// that can be found in the LICENSE file.
|
||||
|
||||
package fs
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"strings"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
const isLinux = true
|
||||
|
||||
// syscall.EpollCtl() commands.
|
||||
//
|
||||
// These are defined here so we don't need to import golang.org/x/sys/unix.
|
||||
//
|
||||
// http://man7.org/linux/man-pages/man2/epoll_ctl.2.html
|
||||
const (
|
||||
epollCTLAdd = 1 // EPOLL_CTL_ADD
|
||||
epollCTLDel = 2 // EPOLL_CTL_DEL
|
||||
epollCTLMod = 3 // EPOLL_CTL_MOD
|
||||
)
|
||||
|
||||
// Bitmask for field syscall.EpollEvent.Events.
|
||||
//
|
||||
// These are defined here so we don't need to import golang.org/x/sys/unix.
|
||||
//
|
||||
// http://man7.org/linux/man-pages/man2/epoll_ctl.2.html
|
||||
type epollEvent uint32
|
||||
|
||||
const (
|
||||
epollIN epollEvent = 0x1 // EPOLLIN: available for read
|
||||
epollOUT epollEvent = 0x4 // EPOLLOUT: available for write
|
||||
epollPRI epollEvent = 0x2 // EPOLLPRI: exceptional urgent condition
|
||||
epollERR epollEvent = 0x8 // EPOLLERR: error
|
||||
epollHUP epollEvent = 0x10 // EPOLLHUP: hangup
|
||||
epollET epollEvent = 0x80000000 // EPOLLET: Edge Triggered behavior
|
||||
epollONESHOT epollEvent = 0x40000000 // EPOLLONESHOT: One shot
|
||||
epollWAKEUP epollEvent = 0x20000000 // EPOLLWAKEUP: disable system sleep; kernel >=3.5
|
||||
epollEXCLUSIVE epollEvent = 0x10000000 // EPOLLEXCLUSIVE: only wake one; kernel >=4.5
|
||||
)
|
||||
|
||||
var bitmaskString = [...]struct {
|
||||
e epollEvent
|
||||
s string
|
||||
}{
|
||||
{epollIN, "IN"},
|
||||
{epollOUT, "OUT"},
|
||||
{epollPRI, "PRI"},
|
||||
{epollERR, "ERR"},
|
||||
{epollHUP, "HUP"},
|
||||
{epollET, "ET"},
|
||||
{epollONESHOT, "ONESHOT"},
|
||||
{epollWAKEUP, "WAKEUP"},
|
||||
{epollEXCLUSIVE, "EXCLUSIVE"},
|
||||
}
|
||||
|
||||
// String is useful for debugging.
|
||||
func (e epollEvent) String() string {
|
||||
var out []string
|
||||
for _, b := range bitmaskString {
|
||||
if e&b.e != 0 {
|
||||
out = append(out, b.s)
|
||||
e &^= b.e
|
||||
}
|
||||
}
|
||||
if e != 0 {
|
||||
out = append(out, "0x"+strconv.FormatUint(uint64(e), 16))
|
||||
}
|
||||
if len(out) == 0 {
|
||||
out = []string{"0"}
|
||||
}
|
||||
return strings.Join(out, "|")
|
||||
}
|
||||
|
||||
func ioctl(f uintptr, op uint, arg uintptr) error {
|
||||
if _, _, errno := syscall.Syscall(syscall.SYS_IOCTL, f, uintptr(op), arg); errno != 0 {
|
||||
return syscall.Errno(errno)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type event struct {
|
||||
event [1]syscall.EpollEvent
|
||||
epollFd int
|
||||
fd int
|
||||
}
|
||||
|
||||
// makeEvent creates an epoll *edge* triggered event.
|
||||
//
|
||||
// References:
|
||||
// behavior and flags: http://man7.org/linux/man-pages/man7/epoll.7.html
|
||||
// syscall.EpollCreate: http://man7.org/linux/man-pages/man2/epoll_create.2.html
|
||||
// syscall.EpollCtl: http://man7.org/linux/man-pages/man2/epoll_ctl.2.html
|
||||
func (e *event) makeEvent(fd uintptr) error {
|
||||
epollFd, err := syscall.EpollCreate(1)
|
||||
switch {
|
||||
case err == nil:
|
||||
break
|
||||
case err.Error() == "function not implemented":
|
||||
// Some arch (arm64) do not implement EpollCreate().
|
||||
if epollFd, err = syscall.EpollCreate1(0); err != nil {
|
||||
return err
|
||||
}
|
||||
default:
|
||||
return err
|
||||
}
|
||||
e.epollFd = epollFd
|
||||
e.fd = int(fd)
|
||||
// EPOLLWAKEUP could be used to force the system to not go do sleep while
|
||||
// waiting for an edge. This is generally a bad idea, as we'd instead have
|
||||
// the system to *wake up* when an edge is triggered. Achieving this is
|
||||
// outside the scope of this interface.
|
||||
e.event[0].Events = uint32(epollPRI | epollET)
|
||||
e.event[0].Fd = int32(e.fd)
|
||||
return syscall.EpollCtl(e.epollFd, epollCTLAdd, e.fd, &e.event[0])
|
||||
}
|
||||
|
||||
func (e *event) wait(timeoutms int) (int, error) {
|
||||
// http://man7.org/linux/man-pages/man2/epoll_wait.2.html
|
||||
return syscall.EpollWait(e.epollFd, e.event[:], timeoutms)
|
||||
}
|
25
vendor/periph.io/x/periph/host/fs/fs_other.go
generated
vendored
Normal file
25
vendor/periph.io/x/periph/host/fs/fs_other.go
generated
vendored
Normal file
@ -0,0 +1,25 @@
|
||||
// Copyright 2017 The Periph Authors. All rights reserved.
|
||||
// Use of this source code is governed under the Apache License, Version 2.0
|
||||
// that can be found in the LICENSE file.
|
||||
|
||||
// +build !linux
|
||||
|
||||
package fs
|
||||
|
||||
import "errors"
|
||||
|
||||
const isLinux = false
|
||||
|
||||
func ioctl(f uintptr, op uint, arg uintptr) error {
|
||||
return errors.New("fs: ioctl not supported on non-linux")
|
||||
}
|
||||
|
||||
type event struct{}
|
||||
|
||||
func (e *event) makeEvent(f uintptr) error {
|
||||
return errors.New("fs: unreachable code")
|
||||
}
|
||||
|
||||
func (e *event) wait(timeoutms int) (int, error) {
|
||||
return 0, errors.New("fs: unreachable code")
|
||||
}
|
51
vendor/periph.io/x/periph/host/fs/ioctl.go
generated
vendored
Normal file
51
vendor/periph.io/x/periph/host/fs/ioctl.go
generated
vendored
Normal file
@ -0,0 +1,51 @@
|
||||
// Copyright 2019 The Periph Authors. All rights reserved.
|
||||
// Use of this source code is governed under the Apache License, Version 2.0
|
||||
// that can be found in the LICENSE file.
|
||||
|
||||
package fs
|
||||
|
||||
// These constants, variables and functions are ported from the Linux userland
|
||||
// API header ioctl.h (commonly packaged at /usr/include/linux/ioctl.h which
|
||||
// includes /usr/include/asm-generic/ioctl.h).
|
||||
|
||||
const (
|
||||
iocNrbits uint = 8
|
||||
iocTypebits uint = 8
|
||||
|
||||
iocNrshift uint = 0
|
||||
|
||||
iocTypeshift = iocNrshift + iocNrbits
|
||||
iocSizeshift = iocTypeshift + iocTypebits
|
||||
iocDirshift = iocSizeshift + iocSizebits
|
||||
)
|
||||
|
||||
func ioc(dir, typ, nr, size uint) uint {
|
||||
return (dir << iocDirshift) |
|
||||
(typ << iocTypeshift) |
|
||||
(nr << iocNrshift) |
|
||||
(size << iocSizeshift)
|
||||
}
|
||||
|
||||
// IO defines an ioctl with no parameters. It corresponds to _IO in the Linux
|
||||
// userland API.
|
||||
func IO(typ, nr uint) uint {
|
||||
return ioc(iocNone, typ, nr, 0)
|
||||
}
|
||||
|
||||
// IOR defines an ioctl with read (userland perspective) parameters. It
|
||||
// corresponds to _IOR in the Linux userland API.
|
||||
func IOR(typ, nr, size uint) uint {
|
||||
return ioc(iocRead, typ, nr, size)
|
||||
}
|
||||
|
||||
// IOW defines an ioctl with write (userland perspective) parameters. It
|
||||
// corresponds to _IOW in the Linux userland API.
|
||||
func IOW(typ, nr, size uint) uint {
|
||||
return ioc(iocWrite, typ, nr, size)
|
||||
}
|
||||
|
||||
// IOWR defines an ioctl with both read and write parameters. It corresponds to
|
||||
// _IOWR in the Linux userland API.
|
||||
func IOWR(typ, nr, size uint) uint {
|
||||
return ioc(iocRead|iocWrite, typ, nr, size)
|
||||
}
|
16
vendor/periph.io/x/periph/host/fs/ioctl_mips_like.go
generated
vendored
Normal file
16
vendor/periph.io/x/periph/host/fs/ioctl_mips_like.go
generated
vendored
Normal file
@ -0,0 +1,16 @@
|
||||
// Copyright 2018 The Periph Authors. All rights reserved.
|
||||
// Use of this source code is governed under the Apache License, Version 2.0
|
||||
// that can be found in the LICENSE file.
|
||||
|
||||
// +build mips mipsle
|
||||
|
||||
package fs
|
||||
|
||||
const (
|
||||
iocNone uint = 1
|
||||
iocRead uint = 2
|
||||
iocWrite uint = 4
|
||||
|
||||
iocSizebits uint = 13
|
||||
iocDirbits uint = 3
|
||||
)
|
16
vendor/periph.io/x/periph/host/fs/ioctl_other.go
generated
vendored
Normal file
16
vendor/periph.io/x/periph/host/fs/ioctl_other.go
generated
vendored
Normal file
@ -0,0 +1,16 @@
|
||||
// Copyright 2018 The Periph Authors. All rights reserved.
|
||||
// Use of this source code is governed under the Apache License, Version 2.0
|
||||
// that can be found in the LICENSE file.
|
||||
|
||||
// +build !mips,!mipsle
|
||||
|
||||
package fs
|
||||
|
||||
const (
|
||||
iocNone uint = 0
|
||||
iocWrite uint = 1
|
||||
iocRead uint = 2
|
||||
|
||||
iocSizebits uint = 14
|
||||
iocDirbits uint = 2
|
||||
)
|
Reference in New Issue
Block a user