github.com/teepark/go-sysvipc@v0.0.0-20200817232735-d7ca6053ea29/common.go (about)

     1  package sysvipc
     2  
     3  /*
     4  #include <stdlib.h>
     5  #include <sys/types.h>
     6  #include <sys/ipc.h>
     7  #include <sys/msg.h>
     8  #include <sys/shm.h>
     9  key_t ftok(const char *pathname, int proj_id);
    10  */
    11  import "C"
    12  import (
    13  	"errors"
    14  	"unsafe"
    15  )
    16  
    17  // IpcPerms holds information about the permissions of a SysV IPC object.
    18  type IpcPerms struct {
    19  	OwnerUID   int
    20  	OwnerGID   int
    21  	CreatorUID int
    22  	CreatorGID int
    23  	Mode       uint16
    24  }
    25  
    26  // Ftok creates a System V IPC key suitable for msgget, semget, or shmget.
    27  // pathname must be an existing, accessible file, and projID must not be 0.
    28  func Ftok(pathname string, projID uint8) (int64, error) {
    29  	if projID == 0 {
    30  		return -1, errors.New("sysvipc: projID must be nonzero")
    31  	}
    32  
    33  	cpath := C.CString(pathname)
    34  	defer C.free(unsafe.Pointer(cpath))
    35  
    36  	rckey, err := C.ftok(cpath, C.int(projID))
    37  	rc := int64(rckey)
    38  	if rc == -1 {
    39  		return -1, err
    40  	}
    41  
    42  	return rc, nil
    43  }