diff --git a/unix/linux/types.go b/unix/linux/types.go index 515e3b66..7eb9a1a5 100644 --- a/unix/linux/types.go +++ b/unix/linux/types.go @@ -35,12 +35,14 @@ package unix #include #include #include +#include #include #include #include #include #include #include +#include #include #include #include @@ -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 +) diff --git a/unix/syscall_linux.go b/unix/syscall_linux.go index e253a912..9a72fab6 100644 --- a/unix/syscall_linux.go +++ b/unix/syscall_linux.go @@ -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 diff --git a/unix/syscall_unix.go b/unix/syscall_unix.go index a0bcf4f9..cf296a24 100644 --- a/unix/syscall_unix.go +++ b/unix/syscall_unix.go @@ -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) -} diff --git a/unix/sysvshm_unix.go b/unix/sysvshm_unix.go new file mode 100644 index 00000000..b968bf23 --- /dev/null +++ b/unix/sysvshm_unix.go @@ -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) +} diff --git a/unix/shm_test.go b/unix/sysvshm_unix_test.go similarity index 94% rename from unix/shm_test.go rename to unix/sysvshm_unix_test.go index b21bb439..b9ad791f 100644 --- a/unix/shm_test.go +++ b/unix/sysvshm_unix_test.go @@ -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 diff --git a/unix/zerrors_linux.go b/unix/zerrors_linux.go index b959fe19..78d4b85e 100644 --- a/unix/zerrors_linux.go +++ b/unix/zerrors_linux.go @@ -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 diff --git a/unix/zsyscall_linux.go b/unix/zsyscall_linux.go index 701f7eb8..4f5da1f5 100644 --- a/unix/zsyscall_linux.go +++ b/unix/zsyscall_linux.go @@ -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 +} diff --git a/unix/ztypes_linux.go b/unix/ztypes_linux.go index 06dcd787..0b13e079 100644 --- a/unix/ztypes_linux.go +++ b/unix/ztypes_linux.go @@ -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 +) diff --git a/unix/ztypes_linux_386.go b/unix/ztypes_linux_386.go index 3219aded..97e91240 100644 --- a/unix/ztypes_linux_386.go +++ b/unix/ztypes_linux_386.go @@ -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 +} diff --git a/unix/ztypes_linux_amd64.go b/unix/ztypes_linux_amd64.go index 16acd3bc..1544af33 100644 --- a/unix/ztypes_linux_amd64.go +++ b/unix/ztypes_linux_amd64.go @@ -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 +} diff --git a/unix/ztypes_linux_arm.go b/unix/ztypes_linux_arm.go index c4982a22..c5c5a9f5 100644 --- a/unix/ztypes_linux_arm.go +++ b/unix/ztypes_linux_arm.go @@ -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 +} diff --git a/unix/ztypes_linux_arm64.go b/unix/ztypes_linux_arm64.go index 98bb8a41..75e49fed 100644 --- a/unix/ztypes_linux_arm64.go +++ b/unix/ztypes_linux_arm64.go @@ -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 +} diff --git a/unix/ztypes_linux_mips.go b/unix/ztypes_linux_mips.go index d5bfc356..d06ac658 100644 --- a/unix/ztypes_linux_mips.go +++ b/unix/ztypes_linux_mips.go @@ -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 +} diff --git a/unix/ztypes_linux_mips64.go b/unix/ztypes_linux_mips64.go index b52c568d..bd1b3783 100644 --- a/unix/ztypes_linux_mips64.go +++ b/unix/ztypes_linux_mips64.go @@ -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 +} diff --git a/unix/ztypes_linux_mips64le.go b/unix/ztypes_linux_mips64le.go index a340b84b..b6d7cf76 100644 --- a/unix/ztypes_linux_mips64le.go +++ b/unix/ztypes_linux_mips64le.go @@ -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 +} diff --git a/unix/ztypes_linux_mipsle.go b/unix/ztypes_linux_mipsle.go index b43d8e2c..610f9e1b 100644 --- a/unix/ztypes_linux_mipsle.go +++ b/unix/ztypes_linux_mipsle.go @@ -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 +} diff --git a/unix/ztypes_linux_ppc.go b/unix/ztypes_linux_ppc.go index efd7313a..7150f710 100644 --- a/unix/ztypes_linux_ppc.go +++ b/unix/ztypes_linux_ppc.go @@ -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 +} diff --git a/unix/ztypes_linux_ppc64.go b/unix/ztypes_linux_ppc64.go index 22cedda5..f2d30f8a 100644 --- a/unix/ztypes_linux_ppc64.go +++ b/unix/ztypes_linux_ppc64.go @@ -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 +} diff --git a/unix/ztypes_linux_ppc64le.go b/unix/ztypes_linux_ppc64le.go index 452a76df..0046e24d 100644 --- a/unix/ztypes_linux_ppc64le.go +++ b/unix/ztypes_linux_ppc64le.go @@ -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 +} diff --git a/unix/ztypes_linux_riscv64.go b/unix/ztypes_linux_riscv64.go index 96c667df..645bc099 100644 --- a/unix/ztypes_linux_riscv64.go +++ b/unix/ztypes_linux_riscv64.go @@ -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 +} diff --git a/unix/ztypes_linux_s390x.go b/unix/ztypes_linux_s390x.go index af04ee17..4565589a 100644 --- a/unix/ztypes_linux_s390x.go +++ b/unix/ztypes_linux_s390x.go @@ -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 +} diff --git a/unix/ztypes_linux_sparc64.go b/unix/ztypes_linux_sparc64.go index 6f385cf6..43581ee1 100644 --- a/unix/ztypes_linux_sparc64.go +++ b/unix/ztypes_linux_sparc64.go @@ -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 +}