golang.org/x/sys@v0.20.1-0.20240517151509-673e0f94c16d/unix/sysvshm_unix_test.go (about) 1 // Copyright 2021 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 //go:build (darwin && amd64) || linux || zos 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 // create ipc 18 id, err := unix.SysvShmGet(unix.IPC_PRIVATE, 1024, unix.IPC_CREAT|unix.IPC_EXCL|0o600) 19 20 // ipc isn't implemented on android, should fail 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 // The kernel may have been built without System V IPC support. 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 // attach 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 // The minimum shared memory size is no less than units of 1M bytes in z/OS 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 // attach again 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 // The returned shared memory alligns with the pagesize. 70 // If pagesize is not 1024 bytes, the shared memory could be larger 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 // detach 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 }