diff --git a/.golangci.yml b/.golangci.yml index 1be8691..255fad3 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -45,7 +45,6 @@ linters: # All available linters list: - - + +

# How to use diff --git a/go.mod b/go.mod index 570a084..b899193 100644 --- a/go.mod +++ b/go.mod @@ -1,10 +1,10 @@ module github.com/rustatian/ipc -go 1.20 +go 1.22.5 require ( - github.com/stretchr/testify v1.8.2 - golang.org/x/sys v0.8.0 + github.com/stretchr/testify v1.9.0 + golang.org/x/sys v0.22.0 ) require ( diff --git a/go.sum b/go.sum index 16e1e58..709a8b5 100644 --- a/go.sum +++ b/go.sum @@ -10,8 +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= diff --git a/shm/posix_shm.go b/shm/posix_shm.go index 442031e..c3454fb 100644 --- a/shm/posix_shm.go +++ b/shm/posix_shm.go @@ -1,10 +1,8 @@ -//go:build linux -// +build linux - package shm import ( "errors" + "fmt" "os" "reflect" "syscall" @@ -54,9 +52,9 @@ type SharedMemorySegment struct { } 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] } @@ -72,6 +70,53 @@ func NewSharedMemoryPosix(name string, size uint, permission int, flags ...Flag) 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 } diff --git a/shm/posix_shm_test.go b/shm/posix_shm_test.go index c9e7eb3..5867560 100644 --- a/shm/posix_shm_test.go +++ b/shm/posix_shm_test.go @@ -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)...)