2014-11-08 08:56:13 +08:00
|
|
|
package sysfs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"syscall"
|
|
|
|
)
|
|
|
|
|
2014-12-31 22:12:25 +08:00
|
|
|
// SystemCaller represents a Syscall
|
2014-11-08 08:56:13 +08:00
|
|
|
type SystemCaller interface {
|
|
|
|
Syscall(trap, a1, a2, a3 uintptr) (r1, r2 uintptr, err syscall.Errno)
|
|
|
|
}
|
|
|
|
|
2014-12-31 22:12:25 +08:00
|
|
|
// NativeSyscall represents the native Syscall
|
2014-11-08 08:56:13 +08:00
|
|
|
type NativeSyscall struct{}
|
2014-12-31 22:12:25 +08:00
|
|
|
|
|
|
|
// MockSyscall represents the mock Syscall
|
2017-02-28 06:40:14 +08:00
|
|
|
type MockSyscall struct {
|
|
|
|
Impl func(trap, a1, a2, a3 uintptr) (r1, r2 uintptr, err syscall.Errno)
|
|
|
|
}
|
2014-11-08 08:56:13 +08:00
|
|
|
|
|
|
|
var sys SystemCaller = &NativeSyscall{}
|
|
|
|
|
2014-12-31 22:12:25 +08:00
|
|
|
// SetSyscall sets the Syscall implementation
|
2014-11-08 08:56:13 +08:00
|
|
|
func SetSyscall(s SystemCaller) {
|
|
|
|
sys = s
|
|
|
|
}
|
|
|
|
|
2014-12-31 22:12:25 +08:00
|
|
|
// Syscall calls either the NativeSyscall or user defined Syscall
|
2014-11-08 08:56:13 +08:00
|
|
|
func Syscall(trap, a1, a2, a3 uintptr) (r1, r2 uintptr, err syscall.Errno) {
|
|
|
|
return sys.Syscall(trap, a1, a2, a3)
|
|
|
|
}
|
|
|
|
|
2014-12-31 22:12:25 +08:00
|
|
|
// Syscall calls syscall.Syscall
|
2014-11-08 08:56:13 +08:00
|
|
|
func (sys *NativeSyscall) Syscall(trap, a1, a2, a3 uintptr) (r1, r2 uintptr, err syscall.Errno) {
|
|
|
|
return syscall.Syscall(trap, a1, a2, a3)
|
|
|
|
}
|
|
|
|
|
2014-12-31 22:12:25 +08:00
|
|
|
// Syscall implements the SystemCaller interface
|
2014-11-08 08:56:13 +08:00
|
|
|
func (sys *MockSyscall) Syscall(trap, a1, a2, a3 uintptr) (r1, r2 uintptr, err syscall.Errno) {
|
2017-02-28 06:40:14 +08:00
|
|
|
if sys.Impl != nil {
|
|
|
|
return sys.Impl(trap, a1, a2, a3)
|
|
|
|
} else {
|
|
|
|
return 0, 0, 0
|
|
|
|
}
|
2014-11-08 08:56:13 +08:00
|
|
|
}
|