...
1
2
3
4
5
6
7 package unix_test
8
9 import (
10 "runtime"
11 "testing"
12
13 "golang.org/x/sys/unix"
14 )
15
16 func TestSysvSharedMemory(t *testing.T) {
17
18 id, err := unix.SysvShmGet(unix.IPC_PRIVATE, 1024, unix.IPC_CREAT|unix.IPC_EXCL|0o600)
19
20
21 if runtime.GOOS == "android" {
22 if err != unix.ENOSYS {
23 t.Fatalf("expected android to fail, but it didn't")
24 }
25 return
26 }
27
28
29 if err == unix.ENOSYS {
30 t.Skip("shmget not supported")
31 }
32
33 if err != nil {
34 t.Fatalf("SysvShmGet: %v", err)
35 }
36 defer func() {
37 _, err := unix.SysvShmCtl(id, unix.IPC_RMID, nil)
38 if err != nil {
39 t.Errorf("Remove failed: %v", err)
40 }
41 }()
42
43
44 b1, err := unix.SysvShmAttach(id, 0, 0)
45 if err != nil {
46 t.Fatalf("Attach: %v", err)
47 }
48
49 if runtime.GOOS == "zos" {
50
51 if len(b1) < 1024 {
52 t.Fatalf("b1 len = %v, less than 1024", len(b1))
53 }
54 } else {
55 if len(b1) != 1024 {
56 t.Fatalf("b1 len = %v, want 1024", len(b1))
57 }
58 }
59
60 b1[42] = 'x'
61
62
63 b2, err := unix.SysvShmAttach(id, 0, 0)
64 if err != nil {
65 t.Fatalf("Attach: %v", err)
66 }
67
68 if runtime.GOOS == "zos" {
69
70
71 if len(b2) < 1024 {
72 t.Fatalf("b1 len = %v, less than 1024", len(b2))
73 }
74 } else {
75 if len(b2) != 1024 {
76 t.Fatalf("b1 len = %v, want 1024", len(b2))
77 }
78 }
79
80 b2[43] = 'y'
81 if b2[42] != 'x' || b1[43] != 'y' {
82 t.Fatalf("shared memory isn't shared")
83 }
84
85
86 if err = unix.SysvShmDetach(b2); err != nil {
87 t.Fatalf("Detach: %v", err)
88 }
89
90 if b1[42] != 'x' || b1[43] != 'y' {
91 t.Fatalf("shared memory was invalidated")
92 }
93 }
94
View as plain text