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

     1  package posix
     2  
     3  import (
     4  	"syscall"
     5  	"unsafe"
     6  )
     7  
     8  func memfdCreate(name string, flags int) (fd int, err error) {
     9  	var _p0 *byte
    10  	_p0, err = syscall.BytePtrFromString(name)
    11  	if err != nil {
    12  		return
    13  	}
    14  	r0, _, e1 := _Syscall(_SYS_MEMFD_CREATE, uintptr(unsafe.Pointer(_p0)), uintptr(flags), 0)
    15  	fd = int(r0)
    16  	if e1 != 0 {
    17  		err = errnoErr(e1)
    18  	}
    19  	return
    20  }
    21  
    22  /* -------------------------------------------------------------------------------------------------------------------*/
    23  func ftruncate(fd int, length int) (err error) {
    24  	_, _, e1 := _Syscall(_SYS_FTRUNCATE, uintptr(fd), uintptr(length), 0)
    25  	if e1 != 0 {
    26  		err = errnoErr(e1)
    27  	}
    28  	return
    29  }
    30  
    31  /* -------------------------------------------------------------------------------------------------------------------*/
    32  
    33  func mmap(addr uintptr, length uintptr, prot int, flags int, fd int, offset int64) (addr2 uintptr, err error) {
    34  	r0, _, e1 := _Syscall6(_SYS_MMAP, addr, length, uintptr(prot), uintptr(flags), uintptr(fd), uintptr(offset))
    35  	addr2 = r0
    36  	if e1 != 0 {
    37  		err = errnoErr(e1)
    38  	}
    39  	return
    40  }
    41  
    42  /* -------------------------------------------------------------------------------------------------------------------*/
    43  
    44  func munmap(addr uintptr, length uintptr) (err error) {
    45  	_, _, e1 := _Syscall(_SYS_MUNMAP, addr, length, 0)
    46  	if e1 != 0 {
    47  		err = errnoErr(e1)
    48  	}
    49  	return
    50  }
    51  
    52  /*--------------------------------------------------------------------------------------------------------------------*/
    53  
    54  func madvise(b []byte, advice int) (err error) {
    55  	var _p0 unsafe.Pointer
    56  	if len(b) > 0 {
    57  		_p0 = unsafe.Pointer(&b[0])
    58  	} else {
    59  		_p0 = unsafe.Pointer(&_zero)
    60  	}
    61  	_, _, e1 := _Syscall(_SYS_MADVISE, uintptr(_p0), uintptr(len(b)), uintptr(advice))
    62  	if e1 != 0 {
    63  		err = errnoErr(e1)
    64  	}
    65  	return
    66  }
    67  
    68  /*--------------------------------------------------------------------------------------------------------------------*/
    69  func mprotect(b []byte, prot int) (err error) {
    70  	var _p0 unsafe.Pointer
    71  	if len(b) > 0 {
    72  		_p0 = unsafe.Pointer(&b[0])
    73  	} else {
    74  		_p0 = unsafe.Pointer(&_zero)
    75  	}
    76  	_, _, e1 := _Syscall(_SYS_MPROTECT, uintptr(_p0), uintptr(len(b)), uintptr(prot))
    77  	if e1 != 0 {
    78  		err = errnoErr(e1)
    79  	}
    80  	return
    81  }
    82  
    83  /*--------------------------------------------------------------------------------------------------------------------*/
    84  func mlock(b []byte, size int) (err error) {
    85  	var _p0 unsafe.Pointer
    86  	if len(b) > 0 {
    87  		_p0 = unsafe.Pointer(&b[0])
    88  	} else {
    89  		_p0 = unsafe.Pointer(&_zero)
    90  	}
    91  	_, _, e1 := _Syscall(_SYS_MLOCK, uintptr(_p0), uintptr(size), 0)
    92  	if e1 != 0 {
    93  		err = errnoErr(e1)
    94  	}
    95  	return
    96  }
    97  
    98  /*--------------------------------------------------------------------------------------------------------------------*/
    99  func mlockall(flags int) (err error) {
   100  	_, _, e1 := _Syscall(_SYS_MLOCKALL, uintptr(flags), 0, 0)
   101  	if e1 != 0 {
   102  		err = errnoErr(e1)
   103  	}
   104  	return
   105  }
   106  
   107  /*--------------------------------------------------------------------------------------------------------------------*/
   108  func munlock(b []byte, size int) (err error) {
   109  	var _p0 unsafe.Pointer
   110  	if len(b) > 0 {
   111  		_p0 = unsafe.Pointer(&b[0])
   112  	} else {
   113  		_p0 = unsafe.Pointer(&_zero)
   114  	}
   115  	_, _, e1 := _Syscall(_SYS_MUNLOCK, uintptr(_p0), uintptr(size), 0)
   116  	if e1 != 0 {
   117  		err = errnoErr(e1)
   118  	}
   119  	return
   120  }
   121  
   122  /*--------------------------------------------------------------------------------------------------------------------*/
   123  func munlockall() (err error) {
   124  	_, _, e1 := _Syscall(_SYS_MUNLOCKALL, 0, 0, 0)
   125  	if e1 != 0 {
   126  		err = errnoErr(e1)
   127  	}
   128  	return
   129  }
   130  
   131  /*--------------------------------------------------------------------------------------------------------------------*/
   132  
   133  func msync(b []byte, flags int) (err error) {
   134  	var _p0 unsafe.Pointer
   135  	if len(b) > 0 {
   136  		_p0 = unsafe.Pointer(&b[0])
   137  	} else {
   138  		_p0 = unsafe.Pointer(&_zero)
   139  	}
   140  	_, _, e1 := _Syscall(_SYS_MSYNC, uintptr(_p0), uintptr(len(b)), uintptr(flags))
   141  	if e1 != 0 {
   142  		err = errnoErr(e1)
   143  	}
   144  	return
   145  }
   146  
   147  /*--------------------------------------------------------------------------------------------------------------------*/
   148  func closeFd(fd int) (err error) {
   149  	_, _, e1 := _Syscall(_SYS_CLOSE, uintptr(fd), 0, 0)
   150  	if e1 != 0 {
   151  		err = errnoErr(e1)
   152  	}
   153  	return
   154  }
   155  
   156  /*--------------------------------------------------------------------------------------------------------------------*/
   157  func fchown(fd int, uid int, gid int) (err error) {
   158  	_, _, e1 := _Syscall(_SYS_FCHOWN, uintptr(fd), uintptr(uid), uintptr(gid))
   159  	if e1 != 0 {
   160  		err = errnoErr(e1)
   161  	}
   162  	return
   163  }
   164  
   165  /*--------------------------------------------------------------------------------------------------------------------*/
   166  func fstat(fd int, stat *Stat_t) (err error) {
   167  	_, _, e1 := _Syscall(_SYS_FSTAT, uintptr(fd), uintptr(unsafe.Pointer(stat)), 0)
   168  	if e1 != 0 {
   169  		err = errnoErr(e1)
   170  	}
   171  	return
   172  }
   173  
   174  type Timespec struct {
   175  	Sec  int64
   176  	Nsec int64
   177  }
   178  
   179  type ModeT uint32
   180  type DevT uint64
   181  
   182  //goland:noinspection GoSnakeCaseUsage
   183  type Stat_t struct {
   184  	Dev     DevT
   185  	Ino     uint64
   186  	Nlink   uint64
   187  	Mode    ModeT
   188  	Uid     uint32
   189  	Gid     uint32
   190  	_       int32
   191  	Rdev    DevT
   192  	Size    int64
   193  	Blksize int64
   194  	Blocks  int64
   195  	Atim    Timespec
   196  	Mtim    Timespec
   197  	Ctim    Timespec
   198  	_       [3]int64
   199  }
   200  
   201  /*--------------------------------------------------------------------------------------------------------------------*/
   202  func fchmod(fd int, mode int) (err error) {
   203  	_, _, e1 := _Syscall(_SYS_FCHMOD, uintptr(fd), uintptr(mode), 0)
   204  	if e1 != 0 {
   205  		err = errnoErr(e1)
   206  	}
   207  	return
   208  }
   209  
   210  /*--------------------------------------------------------------------------------------------------------------------*/
   211  
   212  // fcntl64Syscall is usually SYS_FCNTL, but is overridden on 32-bit Linux
   213  // systems by fcntl_linux_32bit.go to be SYS_FCNTL64.
   214  var fcntl64Syscall uintptr = _SYS_FCNTL
   215  
   216  func fcntl(fd int, cmd, arg int) (int, error) {
   217  	valptr, _, errno := _Syscall(fcntl64Syscall, uintptr(fd), uintptr(cmd), uintptr(arg))
   218  	var err error
   219  	if errno != 0 {
   220  		err = errno
   221  	}
   222  	return int(valptr), err
   223  }
   224  
   225  /*--------------------------------------------------------------------------------------------------------------------*/
   226  
   227  func _Syscall(trap, a1, a2, a3 uintptr) (r1, r2 uintptr, err syscall.Errno)
   228  func _Syscall6(trap, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2 uintptr, err syscall.Errno)
   229  func _RawSyscall(trap, a1, a2, a3 uintptr) (r1, r2 uintptr, err syscall.Errno)
   230  func _RawSyscall6(trap, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2 uintptr, err syscall.Errno)
   231  
   232  /*--------------------------------------------------------------------------------------------------------------------*/
   233  //goland:noinspection GoSnakeCaseUsage
   234  const (
   235  	_SYS_FTRUNCATE    = 77
   236  	_SYS_MEMFD_CREATE = 319
   237  	_SYS_MADVISE      = 28
   238  	_SYS_MMAP         = 9
   239  	_SYS_MUNMAP       = 11
   240  	_SYS_MPROTECT     = 10
   241  	_SYS_MLOCK        = 149
   242  	_SYS_MUNLOCK      = 150
   243  	_SYS_MLOCKALL     = 151
   244  	_SYS_MUNLOCKALL   = 152
   245  	_SYS_MSYNC        = 26
   246  	_SYS_CLOSE        = 3
   247  	_SYS_FCHOWN       = 93
   248  	_SYS_FSTAT        = 5
   249  	_SYS_FCHMOD       = 91
   250  	_SYS_FCNTL        = 72
   251  )