pax_global_header00006660000000000000000000000064151530611110014504gustar00rootroot0000000000000052 comment=c1d4379d2278e80bab7d785c014e3a1315e905fc golang-github-gen2brain-shm-0.2.1/000077500000000000000000000000001515306111100166655ustar00rootroot00000000000000golang-github-gen2brain-shm-0.2.1/.github/000077500000000000000000000000001515306111100202255ustar00rootroot00000000000000golang-github-gen2brain-shm-0.2.1/.github/workflows/000077500000000000000000000000001515306111100222625ustar00rootroot00000000000000golang-github-gen2brain-shm-0.2.1/.github/workflows/test.yml000066400000000000000000000006141515306111100237650ustar00rootroot00000000000000on: [push, pull_request] name: Test jobs: test: strategy: matrix: go-version: [1.21.x] os: [ubuntu-latest, macos-latest] runs-on: ${{ matrix.os }} steps: - name: Install Go uses: actions/setup-go@v3 with: go-version: ${{ matrix.go-version }} - name: Checkout code uses: actions/checkout@v3 - name: Test run: go test golang-github-gen2brain-shm-0.2.1/AUTHORS000066400000000000000000000000441515306111100177330ustar00rootroot00000000000000Milan Nikolic golang-github-gen2brain-shm-0.2.1/LICENSE000066400000000000000000000027041515306111100176750ustar00rootroot00000000000000 Copyright (C) 2018 Milan Nikolic (gen2brain) Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. golang-github-gen2brain-shm-0.2.1/README.md000066400000000000000000000013761515306111100201530ustar00rootroot00000000000000## shm [![Status](https://github.com/gen2brain/shm/actions/workflows/test.yml/badge.svg)](https://github.com/gen2brain/shm/actions) [![Go Reference](https://pkg.go.dev/badge/github.com/gen2brain/shm.svg)](https://pkg.go.dev/github.com/gen2brain/shm) [![Go Report Card](https://goreportcard.com/badge/github.com/gen2brain/shm?branch=master)](https://goreportcard.com/report/github.com/gen2brain/shm) `shm` implements System V shared memory functions (`shmctl`, `shmget`, `shmat`, `shmdt`) in pure Go (except `solaris`, see [#24357](https://github.com/golang/go/issues/24357)). ### Installation go get -u github.com/gen2brain/shm ### More For System V Message Queue IPC (i.e. `msgctl`, `msgget`, `msgrcv`, `msgsnd`) see [ipc](https://github.com/siadat/ipc). golang-github-gen2brain-shm-0.2.1/go.mod000066400000000000000000000000511515306111100177670ustar00rootroot00000000000000module github.com/gen2brain/shm go 1.21 golang-github-gen2brain-shm-0.2.1/shm.go000066400000000000000000000066031515306111100200100ustar00rootroot00000000000000// +build darwin dragonfly freebsd linux netbsd openbsd // Package shm implements System V shared memory functions (shmctl, shmget, shmat, shmdt). package shm import ( "syscall" "unsafe" ) // Constants. const ( // Mode bits for `shmget`. // Create key if key does not exist. IPC_CREAT = 01000 // Fail if key exists. IPC_EXCL = 02000 // Return error on wait. IPC_NOWAIT = 04000 // Special key values. // Private key. IPC_PRIVATE = 0 // Flags for `shmat`. // Attach read-only access. SHM_RDONLY = 010000 // Round attach address to SHMLBA. SHM_RND = 020000 // Take-over region on attach. SHM_REMAP = 040000 // Execution access. SHM_EXEC = 0100000 // Commands for `shmctl`. // Lock segment (root only). SHM_LOCK = 1 // Unlock segment (root only). SHM_UNLOCK = 12 // Control commands for `shmctl`. // Remove identifier. IPC_RMID = 0 // Set `ipc_perm` options. IPC_SET = 1 // Get `ipc_perm' options. IPC_STAT = 2 ) // Get allocates a shared memory segment. // // Get() returns the identifier of the shared memory segment associated with the value of the argument key. // A new shared memory segment is created if key has the value IPC_PRIVATE or key isn't IPC_PRIVATE, // no shared memory segment corresponding to key exists, and IPC_CREAT is specified in shmFlg. // // If shmFlg specifies both IPC_CREAT and IPC_EXCL and a shared memory segment already exists for key, // then Get() fails with errno set to EEXIST. func Get(key int, size int, shmFlg int) (shmId int, err error) { id, _, errno := syscall.Syscall(sysShmGet, uintptr(int32(key)), uintptr(int32(size)), uintptr(int32(shmFlg))) if int(id) == -1 { return -1, errno } return int(id), nil } // At attaches the shared memory segment identified by shmId. // // Using At() with shmAddr equal to NULL is the preferred, portable way of attaching a shared memory segment. func At(shmId int, shmAddr uintptr, shmFlg int) (data []byte, err error) { addr, _, errno := syscall.Syscall(sysShmAt, uintptr(int32(shmId)), shmAddr, uintptr(int32(shmFlg))) if int(addr) == -1 { return nil, errno } length, err := Size(shmId) if err != nil { syscall.Syscall(sysShmDt, addr, 0, 0) return nil, err } var b = struct { addr uintptr len int cap int }{addr, int(length), int(length)} data = *(*[]byte)(unsafe.Pointer(&b)) return data, nil } // Dt detaches the shared memory segment. // // The to-be-detached segment must be currently attached with shmAddr equal to the value returned by the attaching At() call. func Dt(data []byte) error { result, _, errno := syscall.Syscall(sysShmDt, uintptr(unsafe.Pointer(&data[0])), 0, 0) if int(result) == -1 { return errno } return nil } // Ctl performs the control operation specified by cmd on the shared memory segment whose identifier is given in shmId. // // The buf argument is a pointer to a IdDs structure. func Ctl(shmId int, cmd int, buf *IdDs) (int, error) { result, _, errno := syscall.Syscall(sysShmCtl, uintptr(int32(shmId)), uintptr(int32(cmd)), uintptr(unsafe.Pointer(buf))) if int(result) == -1 { return -1, errno } return int(result), nil } // Rm removes the shared memory segment. func Rm(shmId int) error { _, err := Ctl(shmId, IPC_RMID, nil) return err } // Size returns size of shared memory segment. func Size(shmId int) (int64, error) { var idDs IdDs _, err := Ctl(shmId, IPC_STAT, &idDs) if err != nil { return 0, err } return int64(idDs.SegSz), nil } golang-github-gen2brain-shm-0.2.1/shm_darwin.go000066400000000000000000000016541515306111100213550ustar00rootroot00000000000000package shm import ( "syscall" ) // System call constants. const ( sysShmAt = syscall.SYS_SHMAT sysShmCtl = syscall.SYS_SHMCTL sysShmDt = syscall.SYS_SHMDT sysShmGet = syscall.SYS_SHMGET ) // Perm is used to pass permission information to IPC operations. type Perm struct { // Owner's user ID. Uid uint32 // Owner's group ID. Gid uint32 // Creator's user ID. Cuid uint32 // Creator's group ID. Cgid uint32 // Read/write permission. Mode uint16 // Sequence number. Seq uint16 // Key. Key int32 } // IdDs describes shared memory segment. type IdDs struct { // Operation permission struct. Perm Perm // Size of segment in bytes. SegSz uint64 // Pid of last shmat/shmdt. Lpid int32 // Pid of creator. Cpid int32 // Number of current attaches. Nattch uint16 // Padding. PadCgo0 [2]byte // Padding. PadCgo1 [8]byte // Padding. PadCgo2 [8]byte // Padding. PadCgo3 [8]byte // Padding. PadCgo4 [8]byte } golang-github-gen2brain-shm-0.2.1/shm_dragonfly.go000066400000000000000000000017311515306111100220520ustar00rootroot00000000000000package shm import ( "syscall" ) // System call constants. const ( sysShmAt = syscall.SYS_SHMAT sysShmCtl = syscall.SYS_SHMCTL sysShmDt = syscall.SYS_SHMDT sysShmGet = syscall.SYS_SHMGET ) // Perm is used to pass permission information to IPC operations. type Perm struct { // Creator's user ID. Cuid uint16 // Creator's group ID. Cgid uint16 // Owner's user ID. Uid uint16 // Owner's group ID. Gid uint16 // Read/write permission. Mode uint16 // Sequence number. Seq uint16 // Padding. PadCgo0 [4]byte // Key. Key int64 } // IdDs describes shared memory segment. type IdDs struct { // Operation permission struct. Perm Perm // Size of segment in bytes. SegSz uint64 // Pid of last shmat/shmdt. Lpid int32 // Pid of creator. Cpid int32 // Number of current attaches. Nattch uint32 // Padding. PadCgo0 [4]byte // Last attach time. Atime int64 // Last detach time. Dtime int64 // Last change time. Ctime int64 // Internal. Internal *byte } golang-github-gen2brain-shm-0.2.1/shm_freebsd.go000066400000000000000000000015511515306111100214770ustar00rootroot00000000000000package shm // System call constants. const ( sysShmAt = 228 sysShmCtl = 229 sysShmDt = 230 sysShmGet = 231 ) // Perm is used to pass permission information to IPC operations. type Perm struct { // Creator's user ID. Cuid uint32 // Creator's group ID. Cgid uint32 // Owner's user ID. Uid uint32 // Owner's group ID. Gid uint32 // Read/write permission. Mode uint16 // Sequence number. Seq uint16 // Padding. PadCgo0 [4]byte // Key. Key int64 } // IdDs describes shared memory segment. type IdDs struct { // Operation permission struct. Perm Perm // Size of segment in bytes. SegSz uint64 // Pid of last shmat/shmdt. Lpid int32 // Pid of creator. Cpid int32 // Number of current attaches. Nattch int32 // Padding. PadCgo0 [4]byte // Last attach time. Atime int64 // Last detach time. Dtime int64 // Last change time. Ctime int64 } golang-github-gen2brain-shm-0.2.1/shm_linux_386.go000066400000000000000000000021761515306111100216300ustar00rootroot00000000000000//go:build linux && 386 package shm // System call constants. const ( sysShmAt = 397 sysShmCtl = 396 sysShmDt = 398 sysShmGet = 395 ) // Perm is used to pass permission information to IPC operations. type Perm struct { // Key. Key int32 // Owner's user ID. Uid uint32 // Owner's group ID. Gid uint32 // Creator's user ID. Cuid uint32 // Creator's group ID. Cgid uint32 // Read/write permission. Mode uint16 // Padding. Pad1 uint16 // Sequence number. Seq uint16 // Padding. Pad2 uint16 // Reserved. GlibcReserved1 uint32 // Reserved. GlibcReserved2 uint32 } // IdDs describes shared memory segment. type IdDs struct { // Operation permission struct. Perm Perm // Size of segment in bytes. SegSz uint32 // Last attach time. Atime int32 // Reserved. GlibcReserved1 uint32 // Last detach time. Dtime int32 // Reserved. GlibcReserved2 uint32 // Last change time. Ctime int32 // Reserved. GlibcReserved3 uint32 // Pid of creator. Cpid int32 // Pid of last shmat/shmdt. Lpid int32 // Number of current attaches. Nattch uint32 // Reserved. GlibcReserved4 uint32 // Reserved. GlibcReserved5 uint32 } golang-github-gen2brain-shm-0.2.1/shm_linux_amd64.go000066400000000000000000000022001515306111100222070ustar00rootroot00000000000000//go:build linux && amd64 package shm import ( "syscall" ) // System call constants. const ( sysShmAt = syscall.SYS_SHMAT sysShmCtl = syscall.SYS_SHMCTL sysShmDt = syscall.SYS_SHMDT sysShmGet = syscall.SYS_SHMGET ) // Perm is used to pass permission information to IPC operations. type Perm struct { // Key. Key int32 // Owner's user ID. Uid uint32 // Owner's group ID. Gid uint32 // Creator's user ID. Cuid uint32 // Creator's group ID. Cgid uint32 // Read/write permission. Mode uint16 // Padding. Pad1 uint16 // Sequence number. Seq uint16 // Padding. Pad2 uint16 // Padding. PadCgo0 [4]byte // Reserved. GlibcReserved1 uint64 // Reserved. GlibcReserved2 uint64 } // IdDs describes shared memory segment. type IdDs struct { // Operation permission struct. Perm Perm // Size of segment in bytes. SegSz uint64 // Last attach time. Atime int64 // Last detach time. Dtime int64 // Last change time. Ctime int64 // Pid of creator. Cpid int32 // Pid of last shmat/shmdt. Lpid int32 // Number of current attaches. Nattch uint64 // Reserved. GlibcReserved4 uint64 // Reserved. GlibcReserved5 uint64 } golang-github-gen2brain-shm-0.2.1/shm_linux_arm.go000066400000000000000000000023171515306111100220640ustar00rootroot00000000000000//go:build linux && arm package shm import ( "syscall" ) // System call constants. const ( sysShmAt = syscall.SYS_SHMAT sysShmCtl = syscall.SYS_SHMCTL sysShmDt = syscall.SYS_SHMDT sysShmGet = syscall.SYS_SHMGET ) // Perm is used to pass permission information to IPC operations. type Perm struct { // Key. Key int32 // Owner's user ID. Uid uint32 // Owner's group ID. Gid uint32 // Creator's user ID. Cuid uint32 // Creator's group ID. Cgid uint32 // Read/write permission. Mode uint16 // Padding. Pad1 uint16 // Sequence number. Seq uint16 // Padding. Pad2 uint16 // Reserved. GlibcReserved1 uint32 // Reserved. GlibcReserved2 uint32 } // IdDs describes shared memory segment. type IdDs struct { // Operation permission struct. Perm Perm // Size of segment in bytes. SegSz uint32 // Last attach time. Atime int32 // Reserved. GlibcReserved1 uint32 // Last detach time. Dtime int32 // Reserved. GlibcReserved2 uint32 // Last change time. Ctime int32 // Reserved. GlibcReserved3 uint32 // Pid of creator. Cpid int32 // Pid of last shmat/shmdt. Lpid int32 // Number of current attaches. Nattch uint32 // Reserved. GlibcReserved4 uint32 // Reserved. GlibcReserved5 uint32 } golang-github-gen2brain-shm-0.2.1/shm_linux_arm64.go000066400000000000000000000021101515306111100222250ustar00rootroot00000000000000//go:build linux && arm64 package shm import ( "syscall" ) // System call constants. const ( sysShmAt = syscall.SYS_SHMAT sysShmCtl = syscall.SYS_SHMCTL sysShmDt = syscall.SYS_SHMDT sysShmGet = syscall.SYS_SHMGET ) // Perm is used to pass permission information to IPC operations. type Perm struct { // Key. Key int32 // Owner's user ID. Uid uint32 // Owner's group ID. Gid uint32 // Creator's user ID. Cuid uint32 // Creator's group ID. Cgid uint32 // Read/write permission. Mode uint32 // Sequence number. Seq uint16 // Padding. Pad1 uint16 // Reserved. GlibcReserved1 uint64 // Reserved. GlibcReserved2 uint64 } // IdDs describes shared memory segment. type IdDs struct { // Operation permission struct. Perm Perm // Size of segment in bytes. SegSz uint64 // Last attach time. Atime int64 // Last detach time. Dtime int64 // Last change time. Ctime int64 // Pid of creator. Cpid int32 // Pid of last shmat/shmdt. Lpid int32 // Number of current attaches. Nattch uint64 // Reserved. GlibcReserved5 uint64 // Reserved. GlibcReserved6 uint64 } golang-github-gen2brain-shm-0.2.1/shm_linux_loong64.go000066400000000000000000000021121515306111100225660ustar00rootroot00000000000000//go:build linux && loong64 package shm import ( "syscall" ) // System call constants. const ( sysShmAt = syscall.SYS_SHMAT sysShmCtl = syscall.SYS_SHMCTL sysShmDt = syscall.SYS_SHMDT sysShmGet = syscall.SYS_SHMGET ) // Perm is used to pass permission information to IPC operations. type Perm struct { // Key. Key int32 // Owner's user ID. Uid uint32 // Owner's group ID. Gid uint32 // Creator's user ID. Cuid uint32 // Creator's group ID. Cgid uint32 // Read/write permission. Mode uint32 // Sequence number. Seq uint16 // Padding. Pad1 uint16 // Reserved. GlibcReserved1 uint64 // Reserved. GlibcReserved2 uint64 } // IdDs describes shared memory segment. type IdDs struct { // Operation permission struct. Perm Perm // Size of segment in bytes. SegSz uint64 // Last attach time. Atime int64 // Last detach time. Dtime int64 // Last change time. Ctime int64 // Pid of creator. Cpid int32 // Pid of last shmat/shmdt. Lpid int32 // Number of current attaches. Nattch uint64 // Reserved. GlibcReserved5 uint64 // Reserved. GlibcReserved6 uint64 } golang-github-gen2brain-shm-0.2.1/shm_linux_mips64.go000066400000000000000000000021111515306111100224170ustar00rootroot00000000000000//go:build linux && mips64 package shm import ( "syscall" ) // System call constants. const ( sysShmAt = syscall.SYS_SHMAT sysShmCtl = syscall.SYS_SHMCTL sysShmDt = syscall.SYS_SHMDT sysShmGet = syscall.SYS_SHMGET ) // Perm is used to pass permission information to IPC operations. type Perm struct { // Key. Key int32 // Owner's user ID. Uid uint32 // Owner's group ID. Gid uint32 // Creator's user ID. Cuid uint32 // Creator's group ID. Cgid uint32 // Read/write permission. Mode uint32 // Sequence number. Seq uint16 // Padding. Pad1 uint16 // Reserved. GlibcReserved1 uint64 // Reserved. GlibcReserved2 uint64 } // IdDs describes shared memory segment. type IdDs struct { // Operation permission struct. Perm Perm // Size of segment in bytes. SegSz uint64 // Last attach time. Atime int64 // Last detach time. Dtime int64 // Last change time. Ctime int64 // Pid of creator. Cpid int32 // Pid of last shmat/shmdt. Lpid int32 // Number of current attaches. Nattch uint64 // Reserved. GlibcReserved5 uint64 // Reserved. GlibcReserved6 uint64 } golang-github-gen2brain-shm-0.2.1/shm_linux_ppc64el.go000066400000000000000000000017711515306111100225650ustar00rootroot00000000000000//go:build linux && ppc64le package shm // System call constants. const ( sysShmAt = 397 sysShmCtl = 396 sysShmDt = 398 sysShmGet = 395 ) // Perm is used to pass permission information to IPC operations. type Perm struct { // Key. Key int32 // Owner's user ID. Uid uint32 // Owner's group ID. Gid uint32 // Creator's user ID. Cuid uint32 // Creator's group ID. Cgid uint32 // Read/write permission. Mode uint32 // Sequence number. Seq uint32 // Padding. Pad1 uint32 // Reserved. GlibcReserved1 uint64 // Reserved. GlibcReserved2 uint64 } // IdDs describes shared memory segment. type IdDs struct { // Operation permission struct. Perm Perm // Last attach time. Atime int64 // Last detach time. Dtime int64 // Last change time. Ctime int64 // Size of segment in bytes. SegSz uint64 // Pid of creator. Cpid int32 // Pid of last shmat/shmdt. Lpid int32 // Number of current attaches. Nattch uint64 // Reserved. GlibcReserved5 uint64 // Reserved. GlibcReserved6 uint64 } golang-github-gen2brain-shm-0.2.1/shm_linux_riscv64.go000066400000000000000000000021121515306111100225760ustar00rootroot00000000000000//go:build linux && riscv64 package shm import ( "syscall" ) // System call constants. const ( sysShmAt = syscall.SYS_SHMAT sysShmCtl = syscall.SYS_SHMCTL sysShmDt = syscall.SYS_SHMDT sysShmGet = syscall.SYS_SHMGET ) // Perm is used to pass permission information to IPC operations. type Perm struct { // Key. Key int32 // Owner's user ID. Uid uint32 // Owner's group ID. Gid uint32 // Creator's user ID. Cuid uint32 // Creator's group ID. Cgid uint32 // Read/write permission. Mode uint32 // Sequence number. Seq uint16 // Padding. Pad1 uint16 // Reserved. GlibcReserved1 uint64 // Reserved. GlibcReserved2 uint64 } // IdDs describes shared memory segment. type IdDs struct { // Operation permission struct. Perm Perm // Size of segment in bytes. SegSz uint64 // Last attach time. Atime int64 // Last detach time. Dtime int64 // Last change time. Ctime int64 // Pid of creator. Cpid int32 // Pid of last shmat/shmdt. Lpid int32 // Number of current attaches. Nattch uint64 // Reserved. GlibcReserved5 uint64 // Reserved. GlibcReserved6 uint64 } golang-github-gen2brain-shm-0.2.1/shm_linux_s390x.go000066400000000000000000000017671515306111100222030ustar00rootroot00000000000000//go:build linux && s390x package shm // System call constants. const ( sysShmAt = 397 sysShmCtl = 396 sysShmDt = 398 sysShmGet = 395 ) // Perm is used to pass permission information to IPC operations. type Perm struct { // Key. Key int32 // Owner's user ID. Uid uint32 // Owner's group ID. Gid uint32 // Creator's user ID. Cuid uint32 // Creator's group ID. Cgid uint32 // Read/write permission. Mode uint32 // Padding. Pad1 uint16 // Sequence number. Seq uint16 // Reserved. GlibcReserved1 uint64 // Reserved. GlibcReserved2 uint64 } // IdDs describes shared memory segment. type IdDs struct { // Operation permission struct. Perm Perm // Size of segment in bytes. SegSz uint64 // Last attach time. Atime int64 // Last detach time. Dtime int64 // Last change time. Ctime int64 // Pid of creator. Cpid int32 // Pid of last shmat/shmdt. Lpid int32 // Number of current attaches. Nattch uint64 // Reserved. GlibcReserved5 uint64 // Reserved. GlibcReserved6 uint64 } golang-github-gen2brain-shm-0.2.1/shm_netbsd.go000066400000000000000000000017421515306111100213460ustar00rootroot00000000000000package shm import ( "syscall" ) // System call constants. const ( sysShmAt = syscall.SYS_SHMAT sysShmCtl = syscall.SYS_SHMCTL sysShmDt = syscall.SYS_SHMDT sysShmGet = syscall.SYS_SHMGET ) // Perm is used to pass permission information to IPC operations. type Perm struct { // Owner's user ID. Uid uint32 // Owner's group ID. Gid uint32 // Creator's user ID. Cuid uint32 // Creator's group ID. Cgid uint32 // Read/write permission. Mode uint32 // Sequence number. Seq uint16 // Padding. PadCgo0 [2]byte // Key. Key int64 } // IdDs describes shared memory segment. type IdDs struct { // Operation permission struct. Perm Perm // Size of segment in bytes. SegSz uint64 // Pid of last shmat/shmdt. Lpid int32 // Pid of creator. Cpid int32 // Number of current attaches. Nattch uint32 // Padding. PadCgo0 [4]byte // Last attach time. Atime int64 // Last detach time. Dtime int64 // Last change time. Ctime int64 // SysV stupidity XShmInternal *byte } golang-github-gen2brain-shm-0.2.1/shm_openbsd.go000066400000000000000000000021041515306111100215120ustar00rootroot00000000000000package shm import ( "syscall" ) // System call constants. const ( sysShmAt = syscall.SYS_SHMAT sysShmCtl = syscall.SYS_SHMCTL sysShmDt = syscall.SYS_SHMDT sysShmGet = syscall.SYS_SHMGET ) // Perm is used to pass permission information to IPC operations. type Perm struct { // Owner's user ID. Cuid uint32 // Owner's group ID. Cgid uint32 // Creator's user ID. Uid uint32 // Creator's group ID. Gid uint32 // Read/write permission. Mode uint32 // Sequence number. Seq uint16 // Padding. PadCgo0 [2]byte // Key. Key int64 } // IdDs describes shared memory segment. type IdDs struct { // Operation permission struct. Perm Perm // Size of segment in bytes. SegSz int32 // Pid of last shmat/shmdt. Lpid int32 // Pid of creator. Cpid int32 // Number of current attaches. Nattch int16 // Padding. PadCgo0 [2]byte // Last attach time. Atime int64 // Internal. XShmAtimensec int64 // Last detach time. Dtime int64 // Internal. XShmDtimensec int64 // Last change time. Ctime int64 // Internal. XShmCtimensec int64 // Internal. XShmInternal *byte } golang-github-gen2brain-shm-0.2.1/shm_solaris.go000066400000000000000000000102211515306111100215330ustar00rootroot00000000000000// +build solaris package shm // #include // #include // #include import "C" import ( "unsafe" ) // Perm is used to pass permission information to IPC operations. type Perm struct { // Owner's user ID. Uid uint32 // Owner's group ID. Gid uint32 // Creator's user ID. Cuid uint32 // Creator's group ID. Cgid uint32 // Read/write permission. Mode uint32 // Sequence number. Seq uint32 // Key. Key int32 } // IdDs describes shared memory segment. type IdDs struct { // Operation permission struct. Perm Perm // Padding. PadCgo0 [4]byte // Size of segment in bytes. SegSz uint64 // Flags. Flags uint64 // Internal. Lkcnt uint16 // Padding. PadCgo1 [2]byte // Pid of last shmat/shmdt. Lpid int32 // Pid of creator. Cpid int32 // Padding. PadCgo2 [4]byte // Number of current attaches. Nattch uint64 // Internal. Cnattch uint64 // Last attach time. Atime int64 // Last detach time. Dtime int64 // Last change time. Ctime int64 // Internal. Amp *byte // Internal. Gransize uint64 // Internal. Allocated uint64 // Padding. Pad4 [1]int64 } // Constants. const ( // Mode bits for `shmget`. // Create key if key does not exist. IPC_CREAT = 01000 // Fail if key exists. IPC_EXCL = 02000 // Return error on wait. IPC_NOWAIT = 04000 // Special key values. // Private key. IPC_PRIVATE = 0 // Flags for `shmat`. // Attach read-only access. SHM_RDONLY = 010000 // Round attach address to SHMLBA. SHM_RND = 020000 // Take-over region on attach. SHM_REMAP = 040000 // Execution access. SHM_EXEC = 0100000 // Commands for `shmctl`. // Lock segment (root only). SHM_LOCK = 1 // Unlock segment (root only). SHM_UNLOCK = 12 // Control commands for `shmctl`. // Remove identifier. IPC_RMID = 10 // Set `ipc_perm` options. IPC_SET = 11 // Get `ipc_perm' options. IPC_STAT = 12 ) // Get allocates a shared memory segment. // // Get() returns the identifier of the shared memory segment associated with the value of the argument key. // A new shared memory segment is created if key has the value IPC_PRIVATE or key isn't IPC_PRIVATE, // no shared memory segment corresponding to key exists, and IPC_CREAT is specified in shmFlg. // // If shmFlg specifies both IPC_CREAT and IPC_EXCL and a shared memory segment already exists for key, // then Get() fails with errno set to EEXIST. func Get(key int, size int, shmFlg int) (shmId int, err error) { id, errno := C.shmget(C.key_t(key), C.size_t(size), C.int(shmFlg)) if int(id) == -1 { return -1, errno } return int(id), nil } // At attaches the shared memory segment identified by shmId. // // Using At() with shmAddr equal to NULL is the preferred, portable way of attaching a shared memory segment. func At(shmId int, shmAddr uintptr, shmFlg int) (data []byte, err error) { addr, errno := C.shmat(C.int(shmId), unsafe.Pointer(shmAddr), C.int(shmFlg)) if int(uintptr(addr)) == -1 { return nil, errno } length, err := Size(shmId) if err != nil { return nil, err } var b = struct { addr uintptr len int cap int }{uintptr(addr), int(length), int(length)} data = *(*[]byte)(unsafe.Pointer(&b)) return data, nil } // Dt detaches the shared memory segment. // // The to-be-detached segment must be currently attached with shmAddr equal to the value returned by the attaching At() call. func Dt(data []byte) error { result, errno := C.shmdt(unsafe.Pointer(&data[0])) if int(result) == -1 { return errno } return nil } // Ctl performs the control operation specified by cmd on the shared memory segment whose identifier is given in shmId. // // The buf argument is a pointer to a IdDs structure. func Ctl(shmId int, cmd int, buf *IdDs) (int, error) { result, errno := C.shmctl(C.int(shmId), C.int(cmd), (*C.struct_shmid_ds)(unsafe.Pointer(buf))) if int(result) == -1 { return -1, errno } return int(result), nil } // Rm removes the shared memory segment. func Rm(shmId int) error { _, err := Ctl(shmId, IPC_RMID, nil) return err } // Size returns size of shared memory segment. func Size(shmId int) (int64, error) { var idDs IdDs _, err := Ctl(shmId, IPC_STAT, &idDs) if err != nil { return 0, err } return int64(idDs.SegSz), nil } golang-github-gen2brain-shm-0.2.1/shm_test.go000066400000000000000000000010571515306111100210450ustar00rootroot00000000000000package shm import ( "testing" ) func TestShm(t *testing.T) { shmSize := 65536 shmId, err := Get(IPC_PRIVATE, shmSize, IPC_CREAT|0777) if err != nil { t.Errorf("Get: %v", err) } data, err := At(shmId, 0, 0) if err != nil { t.Errorf("At: %v", err) } size, err := Size(shmId) if err != nil { t.Errorf("Size: %v", err) } if int(size) != shmSize { t.Errorf("Wrong size got %d expected %d", size, shmSize) } err = Rm(shmId) if err != nil { t.Errorf("Rm: %v", err) } err = Dt(data) if err != nil { t.Errorf("Dt: %v", err) } }