1// Copyright 2016 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// +build linux
6// +build 386 ppc64 ppc64le s390x
7
8package x11driver
9
10import (
11	"fmt"
12	"syscall"
13	"unsafe"
14)
15
16// These constants are from /usr/include/linux/ipc.h
17const (
18	ipcPrivate = 0
19	ipcRmID    = 0
20
21	shmAt  = 21
22	shmDt  = 22
23	shmGet = 23
24	shmCtl = 24
25)
26
27func shmOpen(size int) (shmid uintptr, addr unsafe.Pointer, err error) {
28	shmid, _, errno0 := syscall.RawSyscall6(syscall.SYS_IPC, shmGet, ipcPrivate, uintptr(size), 0600, 0, 0)
29	if errno0 != 0 {
30		return 0, unsafe.Pointer(uintptr(0)), fmt.Errorf("shmget: %v", errno0)
31	}
32	_, _, errno1 := syscall.RawSyscall6(syscall.SYS_IPC, shmAt, shmid, 0, uintptr(unsafe.Pointer(&addr)), 0, 0)
33	_, _, errno2 := syscall.RawSyscall6(syscall.SYS_IPC, shmCtl, shmid, ipcRmID, 0, 0, 0)
34	if errno1 != 0 {
35		return 0, unsafe.Pointer(uintptr(0)), fmt.Errorf("shmat: %v", errno1)
36	}
37	if errno2 != 0 {
38		return 0, unsafe.Pointer(uintptr(0)), fmt.Errorf("shmctl: %v", errno2)
39	}
40	return shmid, addr, nil
41}
42
43func shmClose(p unsafe.Pointer) error {
44	_, _, errno := syscall.RawSyscall6(syscall.SYS_IPC, shmDt, 0, 0, 0, uintptr(p), 0)
45	if errno != 0 {
46		return fmt.Errorf("shmdt: %v", errno)
47	}
48	return nil
49}
50