os: stub out support for some more features

This is necessary for the following:

  - to make sure os/exec can be imported
  - to make sure internal/testenv can be imported

The internal/testenv package (which imports os/exec) is used by a lot of
tests. By adding support for it, more tests can be run.

This commit adds a bunch of new packages that now pass all tests.
This commit is contained in:
Ayke van Laethem
2020-09-25 19:59:48 +02:00
committed by Ron Evans
parent 6cbaed75c8
commit 718746dd21
12 changed files with 186 additions and 12 deletions
+32
View File
@@ -81,6 +81,12 @@ func Kill(pid int, sig Signal) (err error) {
return ENOSYS // TODO
}
type SysProcAttr struct{}
func Pipe2(p []int, flags int) (err error) {
return ENOSYS // TODO
}
func Getenv(key string) (value string, found bool) {
data := cstring(key)
raw := libc_getenv(&data[0])
@@ -115,6 +121,26 @@ func Mprotect(b []byte, prot int) (err error) {
return
}
func Environ() []string {
environ := libc_environ
var envs []string
for *environ != nil {
// Convert the C string to a Go string.
length := libc_strlen(*environ)
var envVar string
rawEnvVar := (*struct {
ptr unsafe.Pointer
length uintptr
})(unsafe.Pointer(&envVar))
rawEnvVar.ptr = *environ
rawEnvVar.length = length
envs = append(envs, envVar)
// This is the Go equivalent of "environ++" in C.
environ = (*unsafe.Pointer)(unsafe.Pointer(uintptr(unsafe.Pointer(environ)) + unsafe.Sizeof(environ)))
}
return envs
}
// cstring converts a Go string to a C string.
func cstring(s string) []byte {
data := make([]byte, len(s)+1)
@@ -128,6 +154,9 @@ func splitSlice(p []byte) (buf *byte, len uintptr) {
return slice.buf, slice.len
}
//export strlen
func libc_strlen(ptr unsafe.Pointer) uintptr
// ssize_t write(int fd, const void *buf, size_t count)
//export write
func libc_write(fd int32, buf *byte, count uint) int
@@ -167,3 +196,6 @@ func libc_rmdir(pathname *byte) int32
// int unlink(const char *pathname);
//export unlink
func libc_unlink(pathname *byte) int32
//go:extern environ
var libc_environ *unsafe.Pointer
+15 -11
View File
@@ -36,18 +36,20 @@ func (e Errno) Is(target error) bool {
return false
}
// Source: https://opensource.apple.com/source/xnu/xnu-7195.81.3/bsd/sys/errno.h.auto.html
const (
EPERM Errno = 0x1
ENOENT Errno = 0x2
EACCES Errno = 0xd
EEXIST Errno = 0x11
EINTR Errno = 0x4
ENOTDIR Errno = 0x14
EINVAL Errno = 0x16
EMFILE Errno = 0x18
EAGAIN Errno = 0x23
ETIMEDOUT Errno = 0x3c
ENOSYS Errno = 0x4e
EPERM Errno = 1
ENOENT Errno = 2
EACCES Errno = 13
EEXIST Errno = 17
EINTR Errno = 4
ENOTDIR Errno = 20
EINVAL Errno = 22
EMFILE Errno = 24
EPIPE Errno = 32
EAGAIN Errno = 35
ETIMEDOUT Errno = 60
ENOSYS Errno = 78
EWOULDBLOCK Errno = EAGAIN
)
@@ -77,6 +79,8 @@ const (
O_CREAT = 0x200
O_TRUNC = 0x400
O_EXCL = 0x800
O_CLOEXEC = 0x01000000
)
// Source: https://opensource.apple.com/source/xnu/xnu-7195.81.3/bsd/sys/mman.h.auto.html
+7
View File
@@ -67,6 +67,13 @@ func Getenv(key string) (value string, found bool) {
return "", false
}
func Environ() []string {
env := runtime_envs()
envCopy := make([]string, len(env))
copy(envCopy, env)
return envCopy
}
func Open(path string, mode int, perm uint32) (fd int, err error) {
return 0, ENOSYS
}