Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/posix api #1

Merged
merged 2 commits into from
Jul 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ linters: # All available linters list: <https://golangci-lint.run/usage/linters/
enable:
- asciicheck # Simple linter to check that your code does not contain non-ASCII identifiers
- bodyclose # Checks whether HTTP response body is closed successfully
- depguard # Go linter that checks if package imports are in a list of acceptable packages
- dogsled # Checks assignments with too many blank identifiers (e.g. x, _, _, _, := f())
- errcheck # Errcheck is a program for checking for unchecked errors in go programs. These unchecked errors can be critical bugs in some cases
- exhaustive # check exhaustiveness of enum switch statements
Expand All @@ -71,4 +70,4 @@ linters: # All available linters list: <https://golangci-lint.run/usage/linters/
- tparallel # detects inappropriate usage of t.Parallel() method in your Go test codes
- unconvert # Remove unnecessary type conversions
- unused # Checks Go code for unused constants, variables, functions and types
- whitespace # Tool for detection of leading and trailing whitespace
- whitespace # Tool for detection of leading and trailing whitespace
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Linux and Windows implementation of shared memory and semaphores

<p align="center">
<a href="https://github.com/48d90782/IPC/actions"><img src="https://github.com/48d90782/IPC/workflows/CI/badge.svg" alt=""></a>
<a href="https://lgtm.com/projects/g/48d90782/IPC/alerts/"><img src="https://img.shields.io/lgtm/alerts/g/48d90782/IPC.svg?logo=lgtm&logoWidth=18"></a>
<a href="https://github.com/rustatian/IPC/actions"><img src="https://github.com/rustatian/IPC/workflows/CI/badge.svg" alt=""></a>
<a href="https://lgtm.com/projects/g/rustatian/IPC/alerts/"><img src="https://img.shields.io/lgtm/alerts/g/rustatian/IPC.svg?logo=lgtm&logoWidth=18"></a>
</p>

# How to use
Expand Down
7 changes: 5 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
module github.com/rustatian/ipc

go 1.20
go 1.22.5

require github.com/stretchr/testify v1.8.2
require (
github.com/stretchr/testify v1.9.0
golang.org/x/sys v0.22.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI=
golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Expand Down
75 changes: 72 additions & 3 deletions shm/posix_shm.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
//go:build linux
// +build linux

package shm

import (
"errors"
"fmt"
"os"
"reflect"
"syscall"
"unsafe"

"golang.org/x/sys/unix"
)

type Flag int
Expand Down Expand Up @@ -51,6 +51,75 @@ type SharedMemorySegment struct {
data []byte
}

func NewSharedMemoryPosix(name string, size uint, permission int, flags ...Flag) (*SharedMemorySegment, error) {
// OR (bitwise) flags
var flgs Flag
name = fmt.Sprintf("/dev/shm/%s", name)
for i := 0; i < len(flags); i++ {
flgs |= flags[i]
}

if permission != 0 {
flgs |= Flag(permission)
} else {
flgs |= 0600 // default permission
}

fd, err := unix.Open(name, int(flgs), uint32(permission))
if err != nil {
return nil, err
}

err = unix.Ftruncate(fd, int64(size))
if err != nil {
return nil, err
}

file := os.NewFile(uintptr(fd), name)
buff := make([]byte, 13)
_, err = file.Read(buff)
if err != nil {
return nil, err
}
fmt.Println(buff)

buffW := make([]byte, 13, 13)
buffW[0] = 1
buffW[1] = 2
buffW[2] = 3
_, err = file.WriteAt(buffW, 0)
if err != nil {
return nil, err
}
err = file.Sync()
if err != nil {
return nil, err
}

err = file.Close()
if err != nil {
return nil, err
}

// data, err := unix.Mmap(fd, 0, int(size), unix.PROT_READ|unix.PROT_WRITE, unix.MAP_SHARED)

// file := os.NewFile(uintptr(fd), "some_file")
// _, err = file.Write([]byte("foo"))
// if err != nil {
// return nil, err
// }
//
// data[1] = 1
// fmt.Println(data)
//
// err = unix.Munmap(data)
// if err != nil {
// return nil, err
// }

return nil, nil
}

/*
NewSharedMemorySegment the args are:
key - int, used as uniques identifier for the shared memory segment
Expand Down
10 changes: 10 additions & 0 deletions shm/posix_shm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,20 @@ import (

"github.com/rustatian/ipc/shm/test"
"github.com/stretchr/testify/assert"
"golang.org/x/sys/unix"
)

const testData = "hello my dear friend"

func TestNewSharedMemorySegmentPOSIX(t *testing.T) {
shms, err := NewSharedMemoryPosix("foo", 1024 /*unix.S_IRUSR|unix.S_IWUSR*/, unix.O_CREAT, unix.O_RDWR)
if err != nil {
panic(err)
}

_ = shms
}

func TestNewSharedMemorySegment(t *testing.T) {
testBuf := make([]byte, 0)
testBuf = append(testBuf, []byte(testData)...)
Expand Down
Loading