gopkg.in/ro-ag/posix.v1@v1.0.6/shm_open_linux.go (about)

     1  package posix
     2  
     3  import (
     4  	"strings"
     5  	"syscall"
     6  	"unsafe"
     7  )
     8  
     9  func shmOpen(name string, oflag int, mode uint32) (fd int, err error) {
    10  	fd = -1
    11  	if name, err = shmName(name); err != nil {
    12  		return
    13  	}
    14  
    15  	oflag |= O_NOFOLLOW | O_CLOEXEC
    16  
    17  	return openat(_AT_FDCWD, name, oflag, mode)
    18  }
    19  
    20  func shmUnlink(name string) (err error) {
    21  	if name, err = shmName(name); err != nil {
    22  		return
    23  	}
    24  	return unlinkat(_AT_FDCWD, name, 0)
    25  }
    26  
    27  func shmName(name string) (string, error) {
    28  
    29  	for len(name) != 0 && name[0] == '/' {
    30  		name = name[1:]
    31  	}
    32  
    33  	nameLen := len(name)
    34  
    35  	if nameLen == 0 || nameLen >= syscall.NAME_MAX || strings.Contains(name, "/") {
    36  		return "", EINVAL
    37  	}
    38  
    39  	return prefix + name, nil
    40  }
    41  
    42  func openat(dirfd int, path string, flags int, mode uint32) (fd int, err error) {
    43  	var _p0 *byte
    44  	_p0, err = syscall.BytePtrFromString(path)
    45  	if err != nil {
    46  		return
    47  	}
    48  	r0, _, e1 := _Syscall6(_SYS_OPENAT, uintptr(dirfd), uintptr(unsafe.Pointer(_p0)), uintptr(flags), uintptr(mode), 0, 0)
    49  	fd = int(r0)
    50  	if e1 != 0 {
    51  		err = errnoErr(e1)
    52  	}
    53  	return
    54  }
    55  
    56  func unlinkat(dirfd int, path string, flags int) (err error) {
    57  	var _p0 *byte
    58  	_p0, err = syscall.BytePtrFromString(path)
    59  	if err != nil {
    60  		return
    61  	}
    62  	_, _, e1 := _Syscall(_SYS_UNLINKAT, uintptr(dirfd), uintptr(unsafe.Pointer(_p0)), uintptr(flags))
    63  	if e1 != 0 {
    64  		err = errnoErr(e1)
    65  	}
    66  	return
    67  }
    68  
    69  //goland:noinspection GoSnakeCaseUsage
    70  const (
    71  	prefix        = "/dev/shm/"
    72  	_SYS_OPENAT   = 257
    73  	_SYS_UNLINKAT = 263
    74  	_AT_FDCWD     = -0x64
    75  )