Add linux support, constrain build

This commit is contained in:
Dustin Spicuzza
2021-06-16 00:52:58 -04:00
parent 4aa662b50b
commit e00c4c465c
22 changed files with 524 additions and 57 deletions

View File

@@ -35,12 +35,14 @@ package unix
#include <sys/epoll.h>
#include <sys/inotify.h>
#include <sys/ioctl.h>
#include <sys/ipc.h>
#include <sys/mman.h>
#include <sys/mount.h>
#include <sys/param.h>
#include <sys/ptrace.h>
#include <sys/resource.h>
#include <sys/select.h>
#include <sys/shm.h>
#include <sys/signal.h>
#include <sys/signalfd.h>
#include <sys/statfs.h>
@@ -3835,3 +3837,26 @@ const (
const (
PIDFD_NONBLOCK = C.O_NONBLOCK
)
// shm
type SysvIpcPerm C.struct_ipc_perm
type SysvShmDesc C.struct_shmid_ds
const (
IPC_CREAT = C.IPC_CREAT
IPC_EXCL = C.IPC_EXCL
IPC_NOWAIT = C.IPC_NOWAIT
IPC_PRIVATE = C.IPC_PRIVATE
)
const (
IPC_RMID = C.IPC_RMID
IPC_SET = C.IPC_SET
IPC_STAT = C.IPC_STAT
)
const (
SHM_RDONLY = C.SHM_RDONLY
SHM_RND = C.SHM_RND
)

View File

@@ -2318,6 +2318,10 @@ type RemoteIovec struct {
//sys PidfdOpen(pid int, flags int) (fd int, err error) = SYS_PIDFD_OPEN
//sys PidfdGetfd(pidfd int, targetfd int, flags int) (fd int, err error) = SYS_PIDFD_GETFD
//sys shmat(id int, addr uintptr, flag int) (ret uintptr, err error)
//sys shmctl(id int, cmd int, buf *SysvShmDesc) (result int, err error)
//sys shmdt(addr uintptr) (err error)
//sys shmget(key int, size int, flag int) (id int, err error)
/*
* Unimplemented
@@ -2400,10 +2404,6 @@ type RemoteIovec struct {
// SetRobustList
// SetThreadArea
// SetTidAddress
// Shmat
// Shmctl
// Shmdt
// Shmget
// Sigaltstack
// Swapoff
// Swapon

View File

@@ -433,54 +433,3 @@ func Lutimes(path string, tv []Timeval) error {
}
return UtimesNanoAt(AT_FDCWD, path, ts, AT_SYMLINK_NOFOLLOW)
}
// SysvShmAttach attaches the Sysv shared memory segment associated with the
// shared memory identifier id.
func SysvShmAttach(id int, addr uintptr, flag int) ([]byte, error) {
addr, errno := shmat(id, addr, flag)
if errno != nil {
return nil, errno
}
// Retrieve the size of the shared memory to enable slice creation
var info SysvShmDesc
_, err := SysvShmCtl(id, IPC_STAT, &info)
if err != nil {
// release the shared memory if we can't find the size
// ignoring error from shmdt as there's nothing sensible to return here
shmdt(addr)
return nil, err
}
// Use unsafe to convert addr into a []byte.
var b []byte
hdr := (*unsafeheader.Slice)(unsafe.Pointer(&b))
hdr.Data = unsafe.Pointer(addr)
hdr.Cap = int(info.Segsz)
hdr.Len = int(info.Segsz)
return b, nil
}
// SysvShmCtl performs control operations on the shared memory segment
// specified by id.
func SysvShmCtl(id, cmd int, desc *SysvShmDesc) (result int, err error) {
return shmctl(id, cmd, desc)
}
// SysvShmDetach unmaps the shared memory slice returned from SysvShmAttach.
//
// It is not safe to use the slice after calling this function.
func SysvShmDetach(data []byte) error {
if data == nil {
return EINVAL
}
return shmdt(uintptr(unsafe.Pointer(&data[0])))
}
// SysvShmGet returns the Sysv shared memory identifier associated with key.
func SysvShmGet(key, size, flag int) (id int, err error) {
return shmget(key, size, flag)
}

65
unix/sysvshm_unix.go Normal file
View File

@@ -0,0 +1,65 @@
// Copyright 2021 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build (darwin && amd64) || linux
// +build darwin,amd64 linux
package unix
import (
"unsafe"
"golang.org/x/sys/internal/unsafeheader"
)
// SysvShmAttach attaches the Sysv shared memory segment associated with the
// shared memory identifier id.
func SysvShmAttach(id int, addr uintptr, flag int) ([]byte, error) {
addr, errno := shmat(id, addr, flag)
if errno != nil {
return nil, errno
}
// Retrieve the size of the shared memory to enable slice creation
var info SysvShmDesc
_, err := SysvShmCtl(id, IPC_STAT, &info)
if err != nil {
// release the shared memory if we can't find the size
// ignoring error from shmdt as there's nothing sensible to return here
shmdt(addr)
return nil, err
}
// Use unsafe to convert addr into a []byte.
var b []byte
hdr := (*unsafeheader.Slice)(unsafe.Pointer(&b))
hdr.Data = unsafe.Pointer(addr)
hdr.Cap = int(info.Segsz)
hdr.Len = int(info.Segsz)
return b, nil
}
// SysvShmCtl performs control operations on the shared memory segment
// specified by id.
func SysvShmCtl(id, cmd int, desc *SysvShmDesc) (result int, err error) {
return shmctl(id, cmd, desc)
}
// SysvShmDetach unmaps the shared memory slice returned from SysvShmAttach.
//
// It is not safe to use the slice after calling this function.
func SysvShmDetach(data []byte) error {
if data == nil {
return EINVAL
}
return shmdt(uintptr(unsafe.Pointer(&data[0])))
}
// SysvShmGet returns the Sysv shared memory identifier associated with key.
func SysvShmGet(key, size, flag int) (id int, err error) {
return shmget(key, size, flag)
}

View File

@@ -2,8 +2,8 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build darwin
// +build darwin
//go:build (darwin && amd64) || linux
// +build darwin,amd64 linux
package unix_test

View File

@@ -1397,6 +1397,8 @@ const (
MADV_NOHUGEPAGE = 0xf
MADV_NORMAL = 0x0
MADV_PAGEOUT = 0x15
MADV_POPULATE_READ = 0x16
MADV_POPULATE_WRITE = 0x17
MADV_RANDOM = 0x1
MADV_REMOVE = 0x9
MADV_SEQUENTIAL = 0x2

View File

@@ -1974,3 +1974,46 @@ func PidfdGetfd(pidfd int, targetfd int, flags int) (fd int, err error) {
}
return
}
// THIS FILE IS GENERATED BY THE COMMAND AT THE TOP; DO NOT EDIT
func shmat(id int, addr uintptr, flag int) (ret uintptr, err error) {
r0, _, e1 := Syscall(SYS_SHMAT, uintptr(id), uintptr(addr), uintptr(flag))
ret = uintptr(r0)
if e1 != 0 {
err = errnoErr(e1)
}
return
}
// THIS FILE IS GENERATED BY THE COMMAND AT THE TOP; DO NOT EDIT
func shmctl(id int, cmd int, buf *SysvShmDesc) (result int, err error) {
r0, _, e1 := Syscall(SYS_SHMCTL, uintptr(id), uintptr(cmd), uintptr(unsafe.Pointer(buf)))
result = int(r0)
if e1 != 0 {
err = errnoErr(e1)
}
return
}
// THIS FILE IS GENERATED BY THE COMMAND AT THE TOP; DO NOT EDIT
func shmdt(addr uintptr) (err error) {
_, _, e1 := Syscall(SYS_SHMDT, uintptr(addr), 0, 0)
if e1 != 0 {
err = errnoErr(e1)
}
return
}
// THIS FILE IS GENERATED BY THE COMMAND AT THE TOP; DO NOT EDIT
func shmget(key int, size int, flag int) (id int, err error) {
r0, _, e1 := Syscall(SYS_SHMGET, uintptr(key), uintptr(size), uintptr(flag))
id = int(r0)
if e1 != 0 {
err = errnoErr(e1)
}
return
}

View File

@@ -3936,3 +3936,21 @@ type LandlockPathBeneathAttr struct {
const (
LANDLOCK_RULE_PATH_BENEATH = 0x1
)
const (
IPC_CREAT = 0x200
IPC_EXCL = 0x400
IPC_NOWAIT = 0x800
IPC_PRIVATE = 0x0
)
const (
IPC_RMID = 0x0
IPC_SET = 0x1
IPC_STAT = 0x2
)
const (
SHM_RDONLY = 0x1000
SHM_RND = 0x2000
)

View File

@@ -639,3 +639,31 @@ const (
const (
PIDFD_NONBLOCK = 0x800
)
type SysvIpcPerm struct {
_ int32
Uid uint32
Gid uint32
Cuid uint32
Cgid uint32
Mode uint32
_ uint16
_ uint16
_ uint32
_ uint32
}
type SysvShmDesc struct {
Perm SysvIpcPerm
Segsz uint32
Atime int32
_ uint32
Dtime int32
_ uint32
Ctime int32
_ uint32
Cpid int32
Lpid int32
Nattch uint32
_ uint32
_ uint32
}

View File

@@ -657,3 +657,28 @@ const (
const (
PIDFD_NONBLOCK = 0x800
)
type SysvIpcPerm struct {
_ int32
Uid uint32
Gid uint32
Cuid uint32
Cgid uint32
Mode uint32
_ uint16
_ uint16
_ uint64
_ uint64
}
type SysvShmDesc struct {
Perm SysvIpcPerm
Segsz uint64
Atime int64
Dtime int64
Ctime int64
Cpid int32
Lpid int32
Nattch uint64
_ uint64
_ uint64
}

View File

@@ -634,3 +634,31 @@ const (
const (
PIDFD_NONBLOCK = 0x800
)
type SysvIpcPerm struct {
_ int32
Uid uint32
Gid uint32
Cuid uint32
Cgid uint32
Mode uint32
_ uint16
_ uint16
_ uint32
_ uint32
}
type SysvShmDesc struct {
Perm SysvIpcPerm
Segsz uint32
Atime int32
_ uint32
Dtime int32
_ uint32
Ctime int32
_ uint32
Cpid int32
Lpid int32
Nattch uint32
_ uint32
_ uint32
}

View File

@@ -636,3 +636,28 @@ const (
const (
PIDFD_NONBLOCK = 0x800
)
type SysvIpcPerm struct {
_ int32
Uid uint32
Gid uint32
Cuid uint32
Cgid uint32
Mode uint32
_ uint16
_ uint16
_ uint64
_ uint64
}
type SysvShmDesc struct {
Perm SysvIpcPerm
Segsz uint64
Atime int64
Dtime int64
Ctime int64
Cpid int32
Lpid int32
Nattch uint64
_ uint64
_ uint64
}

View File

@@ -640,3 +640,30 @@ const (
const (
PIDFD_NONBLOCK = 0x80
)
type SysvIpcPerm struct {
_ int32
Uid uint32
Gid uint32
Cuid uint32
Cgid uint32
Mode uint32
_ uint16
_ uint16
_ uint32
_ uint32
}
type SysvShmDesc struct {
Perm SysvIpcPerm
Segsz uint32
Atime int32
Dtime int32
Ctime int32
Cpid int32
Lpid int32
Nattch uint32
_ uint16
_ uint16
_ uint16
_ uint16
}

View File

@@ -639,3 +639,28 @@ const (
const (
PIDFD_NONBLOCK = 0x80
)
type SysvIpcPerm struct {
_ int32
Uid uint32
Gid uint32
Cuid uint32
Cgid uint32
Mode uint32
_ uint16
_ uint16
_ uint64
_ uint64
}
type SysvShmDesc struct {
Perm SysvIpcPerm
Segsz uint64
Atime int64
Dtime int64
Ctime int64
Cpid int32
Lpid int32
Nattch uint64
_ uint64
_ uint64
}

View File

@@ -639,3 +639,28 @@ const (
const (
PIDFD_NONBLOCK = 0x80
)
type SysvIpcPerm struct {
_ int32
Uid uint32
Gid uint32
Cuid uint32
Cgid uint32
Mode uint32
_ uint16
_ uint16
_ uint64
_ uint64
}
type SysvShmDesc struct {
Perm SysvIpcPerm
Segsz uint64
Atime int64
Dtime int64
Ctime int64
Cpid int32
Lpid int32
Nattch uint64
_ uint64
_ uint64
}

View File

@@ -640,3 +640,30 @@ const (
const (
PIDFD_NONBLOCK = 0x80
)
type SysvIpcPerm struct {
_ int32
Uid uint32
Gid uint32
Cuid uint32
Cgid uint32
Mode uint32
_ uint16
_ uint16
_ uint32
_ uint32
}
type SysvShmDesc struct {
Perm SysvIpcPerm
Segsz uint32
Atime int32
Dtime int32
Ctime int32
Cpid int32
Lpid int32
Nattch uint32
_ uint16
_ uint16
_ uint16
_ uint16
}

View File

@@ -646,3 +646,33 @@ const (
const (
PIDFD_NONBLOCK = 0x800
)
type SysvIpcPerm struct {
_ int32
Uid uint32
Gid uint32
Cuid uint32
Cgid uint32
Mode uint32
_ uint32
_ uint32
_ uint64
_ uint64
}
type SysvShmDesc struct {
Perm SysvIpcPerm
_ uint32
Atime int32
_ uint32
Dtime int32
_ uint32
Ctime int32
_ uint32
Segsz uint32
Cpid int32
Lpid int32
Nattch uint32
_ uint32
_ uint32
_ [4]byte
}

View File

@@ -646,3 +646,28 @@ const (
const (
PIDFD_NONBLOCK = 0x800
)
type SysvIpcPerm struct {
_ int32
Uid uint32
Gid uint32
Cuid uint32
Cgid uint32
Mode uint32
_ uint32
_ uint32
_ uint64
_ uint64
}
type SysvShmDesc struct {
Perm SysvIpcPerm
Atime int64
Dtime int64
Ctime int64
Segsz uint64
Cpid int32
Lpid int32
Nattch uint64
_ uint64
_ uint64
}

View File

@@ -646,3 +646,28 @@ const (
const (
PIDFD_NONBLOCK = 0x800
)
type SysvIpcPerm struct {
_ int32
Uid uint32
Gid uint32
Cuid uint32
Cgid uint32
Mode uint32
_ uint32
_ uint32
_ uint64
_ uint64
}
type SysvShmDesc struct {
Perm SysvIpcPerm
Atime int64
Dtime int64
Ctime int64
Segsz uint64
Cpid int32
Lpid int32
Nattch uint64
_ uint64
_ uint64
}

View File

@@ -664,3 +664,28 @@ const (
const (
PIDFD_NONBLOCK = 0x800
)
type SysvIpcPerm struct {
_ int32
Uid uint32
Gid uint32
Cuid uint32
Cgid uint32
Mode uint32
_ uint16
_ uint16
_ uint64
_ uint64
}
type SysvShmDesc struct {
Perm SysvIpcPerm
Segsz uint64
Atime int64
Dtime int64
Ctime int64
Cpid int32
Lpid int32
Nattch uint64
_ uint64
_ uint64
}

View File

@@ -660,3 +660,28 @@ const (
const (
PIDFD_NONBLOCK = 0x800
)
type SysvIpcPerm struct {
_ int32
Uid uint32
Gid uint32
Cuid uint32
Cgid uint32
Mode uint32
_ uint16
_ uint16
_ uint64
_ uint64
}
type SysvShmDesc struct {
Perm SysvIpcPerm
Segsz uint64
Atime int64
Dtime int64
Ctime int64
Cpid int32
Lpid int32
Nattch uint64
_ uint64
_ uint64
}

View File

@@ -641,3 +641,28 @@ const (
const (
PIDFD_NONBLOCK = 0x4000
)
type SysvIpcPerm struct {
_ int32
Uid uint32
Gid uint32
Cuid uint32
Cgid uint32
Mode uint32
_ uint16
_ uint16
_ uint64
_ uint64
}
type SysvShmDesc struct {
Perm SysvIpcPerm
Atime int64
Dtime int64
Ctime int64
Segsz uint64
Cpid int32
Lpid int32
Nattch uint64
_ uint64
_ uint64
}