-
Notifications
You must be signed in to change notification settings - Fork 714
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
internal/unix: add Errno wrapper for Windows
eBPF for Windows has historically aimed to be source compatible with libbpf API. As part of this, POSIX error codes are found in errno.h are used to indicate errors of all kinds. eBPF for Windows so far aimed to be source compatible with libbpf. This includes using the same error constants (EINVAL, etc.) as libbpf. It does this using the errno.h header supplied by the Microsoft C runtime. Unfortunately, Windows error codes do not always use the same value as Linux does. Introduce a dedicated Errno type on Windows which maps errors to the correct constant for the platform. This means that syscall.Errno is not an alias for unix.Errno any more. Signed-off-by: Lorenz Bauer <[email protected]>
- Loading branch information
Showing
9 changed files
with
221 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package unix | ||
|
||
import ( | ||
"syscall" | ||
|
||
linux "golang.org/x/sys/unix" | ||
) | ||
|
||
type Errno = syscall.Errno | ||
|
||
const ( | ||
E2BIG = linux.E2BIG | ||
EACCES = linux.EACCES | ||
EAGAIN = linux.EAGAIN | ||
EBADF = linux.EBADF | ||
EEXIST = linux.EEXIST | ||
EFAULT = linux.EFAULT | ||
EILSEQ = linux.EILSEQ | ||
EINTR = linux.EINTR | ||
EINVAL = linux.EINVAL | ||
ENODEV = linux.ENODEV | ||
ENOENT = linux.ENOENT | ||
ENOSPC = linux.ENOSPC | ||
EOPNOTSUPP = linux.EOPNOTSUPP | ||
EPERM = linux.EPERM | ||
EPOLLIN = linux.EPOLLIN | ||
ESRCH = linux.ESRCH | ||
ESTALE = linux.ESTALE | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package unix | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/go-quicktest/qt" | ||
"golang.org/x/sys/unix" | ||
) | ||
|
||
func TestErrnoIsUnix(t *testing.T) { | ||
qt.Assert(t, qt.ErrorIs(EPERM, unix.EPERM)) | ||
qt.Assert(t, qt.ErrorIs(ENOENT, unix.ENOENT)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
//go:build !linux && !windows | ||
|
||
package unix | ||
|
||
import "syscall" | ||
|
||
type Errno = syscall.Errno | ||
|
||
// Errnos are distinct and non-zero. | ||
const ( | ||
E2BIG Errno = iota + 1 | ||
EACCES | ||
EAGAIN | ||
EBADF | ||
EEXIST | ||
EFAULT | ||
EILSEQ | ||
EINTR | ||
EINVAL | ||
ENODEV | ||
ENOENT | ||
ENOSPC | ||
ENOTSUP | ||
ENOTSUPP | ||
EOPNOTSUPP | ||
EPERM | ||
ESRCH | ||
ESTALE | ||
) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package unix | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/go-quicktest/qt" | ||
) | ||
|
||
func TestErrno(t *testing.T) { | ||
qt.Assert(t, qt.ErrorIs(ENOENT, os.ErrNotExist)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package unix | ||
|
||
// The code in this file is derived from syscall_unix.go in the Go source code, | ||
// licensed under the MIT license. | ||
|
||
import ( | ||
"errors" | ||
"os" | ||
"syscall" | ||
) | ||
|
||
//go:generate go run golang.org/x/tools/cmd/stringer@latest -type=Errno -tags=windows -output=errno_string_windows.go | ||
|
||
// Windows specific constants for Unix errnos. | ||
// | ||
// The values do not always match Linux, for example EILSEQ and EOPNOTSUPP. | ||
// | ||
// See https://learn.microsoft.com/en-us/cpp/c-runtime-library/errno-constants?view=msvc-170 | ||
const ( | ||
EPERM Errno = 1 | ||
ENOENT Errno = 2 | ||
ESRCH Errno = 3 | ||
EINTR Errno = 4 | ||
E2BIG Errno = 7 | ||
EBADF Errno = 9 | ||
EAGAIN Errno = 11 | ||
EACCES Errno = 13 | ||
EFAULT Errno = 14 | ||
EEXIST Errno = 17 | ||
ENODEV Errno = 19 | ||
EINVAL Errno = 22 | ||
ENOSPC Errno = 28 | ||
EILSEQ Errno = 42 | ||
ENOTSUP Errno = 129 | ||
EOPNOTSUPP Errno = 130 | ||
) | ||
|
||
// These constants do not exist on Windows and therefore have a non-zero | ||
// dummy value. | ||
const ( | ||
ENOTSUPP Errno = Errno(syscall.APPLICATION_ERROR) + iota | ||
ESTALE | ||
) | ||
|
||
// Errno is a Windows compatibility shim for Unix errnos. | ||
type Errno uintptr | ||
|
||
func (e Errno) Error() string { | ||
return e.String() | ||
} | ||
|
||
func (e Errno) Is(target error) bool { | ||
switch target { | ||
case os.ErrPermission: | ||
return e == EACCES || e == EPERM | ||
case os.ErrExist: | ||
return e == EEXIST /* || e == ENOTEMPTY */ | ||
case os.ErrNotExist: | ||
return e == ENOENT | ||
case errors.ErrUnsupported: | ||
return /* e == ENOSYS || e == ENOTSUP || */ e == EOPNOTSUPP | ||
} | ||
return false | ||
} | ||
|
||
func (e Errno) Temporary() bool { | ||
return e == EINTR || /* e == EMFILE || e == ENFILE || */ e.Timeout() | ||
} | ||
|
||
func (e Errno) Timeout() bool { | ||
return e == EAGAIN /* || e == EWOULDBLOCK || e == ETIMEDOUT */ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters