Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit d110bbc
Showing
12 changed files
with
367 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
language: go | ||
|
||
go: | ||
- master | ||
|
||
script: | ||
- go test -v ./ |
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 @@ | ||
Milan Nikolic <gen2brain@gmail.com> |
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,17 @@ | ||
Copyright (C) 2018 Milan Nikolic (gen2brain) | ||
|
||
This software is provided 'as-is', without any express or implied | ||
warranty. In no event will the authors be held liable for any damages | ||
arising from the use of this software. | ||
|
||
Permission is granted to anyone to use this software for any purpose, | ||
including commercial applications, and to alter it and redistribute it | ||
freely, subject to the following restrictions: | ||
|
||
1. The origin of this software must not be misrepresented; you must not | ||
claim that you wrote the original software. If you use this software | ||
in a product, an acknowledgment in the product documentation would be | ||
appreciated but is not required. | ||
2. Altered source versions must be plainly marked as such, and must not be | ||
misrepresented as being the original software. | ||
3. This notice may not be removed or altered from any source distribution. |
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,14 @@ | ||
## shm | ||
[![TravisCI Build Status](https://travis-ci.org/gen2brain/shm.svg?branch=master)](https://travis-ci.org/gen2brain/shm) | ||
[![GoDoc](https://godoc.org/github.com/gen2brain/shm?status.svg)](https://godoc.org/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. | ||
|
||
### 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). |
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,134 @@ | ||
// +build darwin dragonfly linux netbsd openbsd | ||
|
||
// Package shm implements System V shared memory functions (shmctl, shmget, shmat, shmdt). | ||
package shm | ||
|
||
import ( | ||
"syscall" | ||
"unsafe" | ||
) | ||
|
||
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(syscall.SYS_SHMGET, 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(syscall.SYS_SHMAT, uintptr(int32(shmId)), shmAddr, uintptr(int32(shmFlg))) | ||
if int(addr) == -1 { | ||
return nil, errno | ||
} | ||
|
||
length, err := Size(shmId) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var b = struct { | ||
addr uintptr | ||
len int | ||
cap int | ||
}{addr, length, 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(syscall.SYS_SHMDT, 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(syscall.SYS_SHMCTL, 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) (int, error) { | ||
var idDs IdDs | ||
|
||
_, err := Ctl(shmId, IPC_STAT, &idDs) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
return int(idDs.SegSz), nil | ||
} |
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,43 @@ | ||
package shm | ||
|
||
// 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. | ||
Pad_cgo_0 [2]byte | ||
// Padding. | ||
Pad_cgo_1 [8]byte | ||
// Padding. | ||
Pad_cgo_2 [8]byte | ||
// Padding. | ||
Pad_cgo_3 [8]byte | ||
// Padding. | ||
Pad_cgo_4 [8]byte | ||
} |
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 @@ | ||
package shm |
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,57 @@ | ||
package shm | ||
|
||
// 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. | ||
X__pad1 uint16 | ||
// Sequence number. | ||
X__seq uint16 | ||
// Padding. | ||
X__pad2 uint16 | ||
// Reserved. | ||
X__glibc_reserved1 uint32 | ||
// Reserved. | ||
X__glibc_reserved2 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. | ||
X__glibc_reserved1 uint32 | ||
// Last detach time. | ||
Dtime int32 | ||
// Reserved. | ||
X__glibc_reserved2 uint32 | ||
// Last change time. | ||
Ctime int32 | ||
// Reserved. | ||
X__glibc_reserved3 uint32 | ||
// Pid of creator. | ||
Cpid int32 | ||
// Pid of last shmat/shmdt. | ||
Lpid int32 | ||
// Number of current attaches. | ||
Nattch uint32 | ||
// Reserved. | ||
X__glibc_reserved4 uint32 | ||
// Reserved. | ||
X__glibc_reserved5 uint32 | ||
} |
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,53 @@ | ||
package shm | ||
|
||
// 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. | ||
X__pad1 uint16 | ||
// Sequence number. | ||
Seq uint16 | ||
// Padding. | ||
X__pad2 uint16 | ||
// Padding. | ||
Pad_cgo_0 [4]byte | ||
// Reserved. | ||
X__glibc_reserved1 uint64 | ||
// Reserved. | ||
X__glibc_reserved2 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. | ||
X__glibc_reserved4 uint64 | ||
// Reserved. | ||
X__glibc_reserved5 uint64 | ||
} |
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 @@ | ||
package shm |
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 @@ | ||
package shm |
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,38 @@ | ||
package 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 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) | ||
} | ||
} |