├── LICENSE ├── README.md ├── go.mod ├── go.sum ├── shm_linux.go └── shm_other.go /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2016, Tom Thorogood. 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | * Redistributions of source code must retain the above copyright 7 | notice, this list of conditions and the following disclaimer. 8 | * Redistributions in binary form must reproduce the above copyright 9 | notice, this list of conditions and the following disclaimer in the 10 | documentation and/or other materials provided with the distribution. 11 | * Neither the name of the Tom Thorogood nor the 12 | names of its contributors may be used to endorse or promote products 13 | derived from this software without specific prior written permission. 14 | 15 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 16 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY 19 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 22 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # go-shm 2 | 3 | [![GoDoc](https://godoc.org/github.com/tmthrgd/go-shm?status.svg)](https://godoc.org/github.com/tmthrgd/go-shm) 4 | 5 | go-shm provides functions to open and unlink shared memory without relying on cgo (on Linux). 6 | It provides an implementation of [`shm_open`](https://linux.die.net/man/3/shm_open) and 7 | [`shm_unlink`](https://linux.die.net/man/3/shm_unlink) from `sys/mman.h`. 8 | 9 | ## Download 10 | 11 | ``` 12 | go get github.com/tmthrgd/go-shm 13 | ``` 14 | 15 | ## License 16 | 17 | Unless otherwise noted, the go-shm source files are distributed under the Modified BSD License 18 | found in the LICENSE file. 19 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/tmthrgd/go-shm 2 | 3 | go 1.19 4 | 5 | require golang.org/x/sys v0.4.0 6 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | golang.org/x/sys v0.4.0 h1:Zr2JFtRQNX3BCZ8YtxRE9hNJYC8J6I1MVbMg6owUp18= 2 | golang.org/x/sys v0.4.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 3 | -------------------------------------------------------------------------------- /shm_linux.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 Tom Thorogood. All rights reserved. 2 | // Use of this source code is governed by a 3 | // Modified BSD License license that can be found in 4 | // the LICENSE file. 5 | 6 | // Package shm provides functions to open and unlink shared memory. 7 | package shm 8 | 9 | import ( 10 | "os" 11 | 12 | "golang.org/x/sys/unix" 13 | ) 14 | 15 | const devShm = "/dev/shm/" 16 | 17 | // Taken from shm_open(3): 18 | // shm_open() creates and opens a new, or opens an existing, POSIX shared 19 | // memory object. A POSIX shared memory object is in effect a handle which 20 | // can be used by unrelated processes to mmap(2) the same region of shared 21 | // memory. The shm_unlink() function performs the converse operation, 22 | // removing an object previously created by shm_open(). 23 | // 24 | // The operation of shm_open() is analogous to that of open(2). name 25 | // specifies the shared memory object to be created or opened. For 26 | // portable use, a shared memory object should be identified by a name of 27 | // the form /somename; that is, a null-terminated string of up to NAME_MAX 28 | // (i.e., 255) characters consisting of an initial slash, followed by one 29 | // or more characters, none of which are slashes. 30 | func Open(name string, flag int, perm os.FileMode) (*os.File, error) { 31 | fileName := name 32 | 33 | for len(name) != 0 && name[0] == '/' { 34 | name = name[1:] 35 | } 36 | 37 | if len(name) == 0 { 38 | return nil, &os.PathError{Op: "open", Path: fileName, Err: unix.EINVAL} 39 | } 40 | 41 | o := uint32(perm.Perm()) 42 | if perm&os.ModeSetuid != 0 { 43 | o |= unix.S_ISUID 44 | } 45 | if perm&os.ModeSetgid != 0 { 46 | o |= unix.S_ISGID 47 | } 48 | if perm&os.ModeSticky != 0 { 49 | o |= unix.S_ISVTX 50 | } 51 | 52 | fd, err := unix.Open(devShm+name, flag|unix.O_CLOEXEC, o) 53 | if err != nil { 54 | return nil, &os.PathError{Op: "open", Path: fileName, Err: err} 55 | } 56 | 57 | return os.NewFile(uintptr(fd), fileName), nil 58 | } 59 | 60 | // Taken from shm_unlink(3): 61 | // The operation of shm_unlink() is analogous to unlink(2): it removes a 62 | // shared memory object name, and, once all processes have unmapped the 63 | // object, de-allocates and destroys the contents of the associated memory 64 | // region. After a successful shm_unlink(), attempts to shm_open() an 65 | // object with the same name will fail (unless O_CREAT was specified, in 66 | // which case a new, distinct object is created). 67 | func Unlink(name string) error { 68 | fileName := name 69 | 70 | for len(name) != 0 && name[0] == '/' { 71 | name = name[1:] 72 | } 73 | 74 | if len(name) == 0 { 75 | return &os.PathError{Op: "unlink", Path: fileName, Err: unix.EINVAL} 76 | } 77 | 78 | if err := unix.Unlink(devShm + name); err != nil { 79 | return &os.PathError{Op: "unlink", Path: fileName, Err: err} 80 | } 81 | 82 | return nil 83 | } 84 | -------------------------------------------------------------------------------- /shm_other.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 Tom Thorogood. All rights reserved. 2 | // Use of this source code is governed by a 3 | // Modified BSD License license that can be found in 4 | // the LICENSE file. 5 | 6 | // +build !linux 7 | 8 | // Package shm provides functions to open and unlink shared memory. 9 | package shm 10 | 11 | /* 12 | #cgo LDFLAGS: -lrt 13 | 14 | #include // For free 15 | #include // For shm_* 16 | */ 17 | import "C" 18 | 19 | import ( 20 | "os" 21 | "unsafe" 22 | ) 23 | 24 | // Taken from shm_open(3): 25 | // shm_open() creates and opens a new, or opens an existing, POSIX shared 26 | // memory object. A POSIX shared memory object is in effect a handle which 27 | // can be used by unrelated processes to mmap(2) the same region of shared 28 | // memory. The shm_unlink() function performs the converse operation, 29 | // removing an object previously created by shm_open(). 30 | // 31 | // The operation of shm_open() is analogous to that of open(2). name 32 | // specifies the shared memory object to be created or opened. For 33 | // portable use, a shared memory object should be identified by a name of 34 | // the form /somename; that is, a null-terminated string of up to NAME_MAX 35 | // (i.e., 255) characters consisting of an initial slash, followed by one 36 | // or more characters, none of which are slashes. 37 | func Open(name string, flag int, perm os.FileMode) (*os.File, error) { 38 | nameC := C.CString(name) 39 | defer C.free(unsafe.Pointer(nameC)) 40 | 41 | fd, err := C.shm_open(nameC, C.int(flag), C.mode_t(perm)) 42 | if err != nil { 43 | return nil, &os.PathError{Op: "open", Path: name, Err: err} 44 | } 45 | 46 | return os.NewFile(uintptr(fd), name), nil 47 | } 48 | 49 | // Taken from shm_unlink(3): 50 | // The operation of shm_unlink() is analogous to unlink(2): it removes a 51 | // shared memory object name, and, once all processes have unmapped the 52 | // object, de-allocates and destroys the contents of the associated memory 53 | // region. After a successful shm_unlink(), attempts to shm_open() an 54 | // object with the same name will fail (unless O_CREAT was specified, in 55 | // which case a new, distinct object is created). 56 | func Unlink(name string) error { 57 | nameC := C.CString(name) 58 | defer C.free(unsafe.Pointer(nameC)) 59 | 60 | if _, err := C.shm_unlink(nameC); err != nil { 61 | return &os.PathError{Op: "unlink", Path: name, Err: err} 62 | } 63 | 64 | return nil 65 | } 66 | --------------------------------------------------------------------------------