Source file src/cmd/vendor/golang.org/x/sys/unix/syscall_linux_386.go

     1  // Copyright 2009 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 386 && linux
     6  // +build 386,linux
     7  
     8  package unix
     9  
    10  import (
    11  	"unsafe"
    12  )
    13  
    14  func setTimespec(sec, nsec int64) Timespec {
    15  	return Timespec{Sec: int32(sec), Nsec: int32(nsec)}
    16  }
    17  
    18  func setTimeval(sec, usec int64) Timeval {
    19  	return Timeval{Sec: int32(sec), Usec: int32(usec)}
    20  }
    21  
    22  // 64-bit file system and 32-bit uid calls
    23  // (386 default is 32-bit file system and 16-bit uid).
    24  //sys	EpollWait(epfd int, events []EpollEvent, msec int) (n int, err error)
    25  //sys	Fadvise(fd int, offset int64, length int64, advice int) (err error) = SYS_FADVISE64_64
    26  //sys	Fchown(fd int, uid int, gid int) (err error) = SYS_FCHOWN32
    27  //sys	Fstat(fd int, stat *Stat_t) (err error) = SYS_FSTAT64
    28  //sys	Fstatat(dirfd int, path string, stat *Stat_t, flags int) (err error) = SYS_FSTATAT64
    29  //sys	Ftruncate(fd int, length int64) (err error) = SYS_FTRUNCATE64
    30  //sysnb	Getegid() (egid int) = SYS_GETEGID32
    31  //sysnb	Geteuid() (euid int) = SYS_GETEUID32
    32  //sysnb	Getgid() (gid int) = SYS_GETGID32
    33  //sysnb	Getuid() (uid int) = SYS_GETUID32
    34  //sys	Ioperm(from int, num int, on int) (err error)
    35  //sys	Iopl(level int) (err error)
    36  //sys	Lchown(path string, uid int, gid int) (err error) = SYS_LCHOWN32
    37  //sys	Lstat(path string, stat *Stat_t) (err error) = SYS_LSTAT64
    38  //sys	Pread(fd int, p []byte, offset int64) (n int, err error) = SYS_PREAD64
    39  //sys	Pwrite(fd int, p []byte, offset int64) (n int, err error) = SYS_PWRITE64
    40  //sys	Renameat(olddirfd int, oldpath string, newdirfd int, newpath string) (err error)
    41  //sys	sendfile(outfd int, infd int, offset *int64, count int) (written int, err error) = SYS_SENDFILE64
    42  //sys	setfsgid(gid int) (prev int, err error) = SYS_SETFSGID32
    43  //sys	setfsuid(uid int) (prev int, err error) = SYS_SETFSUID32
    44  //sysnb	Setregid(rgid int, egid int) (err error) = SYS_SETREGID32
    45  //sysnb	Setresgid(rgid int, egid int, sgid int) (err error) = SYS_SETRESGID32
    46  //sysnb	Setresuid(ruid int, euid int, suid int) (err error) = SYS_SETRESUID32
    47  //sysnb	Setreuid(ruid int, euid int) (err error) = SYS_SETREUID32
    48  //sys	Splice(rfd int, roff *int64, wfd int, woff *int64, len int, flags int) (n int, err error)
    49  //sys	Stat(path string, stat *Stat_t) (err error) = SYS_STAT64
    50  //sys	SyncFileRange(fd int, off int64, n int64, flags int) (err error)
    51  //sys	Truncate(path string, length int64) (err error) = SYS_TRUNCATE64
    52  //sys	Ustat(dev int, ubuf *Ustat_t) (err error)
    53  //sysnb	getgroups(n int, list *_Gid_t) (nn int, err error) = SYS_GETGROUPS32
    54  //sysnb	setgroups(n int, list *_Gid_t) (err error) = SYS_SETGROUPS32
    55  //sys	Select(nfd int, r *FdSet, w *FdSet, e *FdSet, timeout *Timeval) (n int, err error) = SYS__NEWSELECT
    56  
    57  //sys	mmap2(addr uintptr, length uintptr, prot int, flags int, fd int, pageOffset uintptr) (xaddr uintptr, err error)
    58  //sys	Pause() (err error)
    59  
    60  func mmap(addr uintptr, length uintptr, prot int, flags int, fd int, offset int64) (xaddr uintptr, err error) {
    61  	page := uintptr(offset / 4096)
    62  	if offset != int64(page)*4096 {
    63  		return 0, EINVAL
    64  	}
    65  	return mmap2(addr, length, prot, flags, fd, page)
    66  }
    67  
    68  type rlimit32 struct {
    69  	Cur uint32
    70  	Max uint32
    71  }
    72  
    73  //sysnb	getrlimit(resource int, rlim *rlimit32) (err error) = SYS_GETRLIMIT
    74  
    75  const rlimInf32 = ^uint32(0)
    76  const rlimInf64 = ^uint64(0)
    77  
    78  func Getrlimit(resource int, rlim *Rlimit) (err error) {
    79  	err = Prlimit(0, resource, nil, rlim)
    80  	if err != ENOSYS {
    81  		return err
    82  	}
    83  
    84  	rl := rlimit32{}
    85  	err = getrlimit(resource, &rl)
    86  	if err != nil {
    87  		return
    88  	}
    89  
    90  	if rl.Cur == rlimInf32 {
    91  		rlim.Cur = rlimInf64
    92  	} else {
    93  		rlim.Cur = uint64(rl.Cur)
    94  	}
    95  
    96  	if rl.Max == rlimInf32 {
    97  		rlim.Max = rlimInf64
    98  	} else {
    99  		rlim.Max = uint64(rl.Max)
   100  	}
   101  	return
   102  }
   103  
   104  //sysnb	setrlimit(resource int, rlim *rlimit32) (err error) = SYS_SETRLIMIT
   105  
   106  func Setrlimit(resource int, rlim *Rlimit) (err error) {
   107  	err = Prlimit(0, resource, rlim, nil)
   108  	if err != ENOSYS {
   109  		return err
   110  	}
   111  
   112  	rl := rlimit32{}
   113  	if rlim.Cur == rlimInf64 {
   114  		rl.Cur = rlimInf32
   115  	} else if rlim.Cur < uint64(rlimInf32) {
   116  		rl.Cur = uint32(rlim.Cur)
   117  	} else {
   118  		return EINVAL
   119  	}
   120  	if rlim.Max == rlimInf64 {
   121  		rl.Max = rlimInf32
   122  	} else if rlim.Max < uint64(rlimInf32) {
   123  		rl.Max = uint32(rlim.Max)
   124  	} else {
   125  		return EINVAL
   126  	}
   127  
   128  	return setrlimit(resource, &rl)
   129  }
   130  
   131  func Seek(fd int, offset int64, whence int) (newoffset int64, err error) {
   132  	newoffset, errno := seek(fd, offset, whence)
   133  	if errno != 0 {
   134  		return 0, errno
   135  	}
   136  	return newoffset, nil
   137  }
   138  
   139  //sys	futimesat(dirfd int, path string, times *[2]Timeval) (err error)
   140  //sysnb	Gettimeofday(tv *Timeval) (err error)
   141  //sysnb	Time(t *Time_t) (tt Time_t, err error)
   142  //sys	Utime(path string, buf *Utimbuf) (err error)
   143  //sys	utimes(path string, times *[2]Timeval) (err error)
   144  
   145  // On x86 Linux, all the socket calls go through an extra indirection,
   146  // I think because the 5-register system call interface can't handle
   147  // the 6-argument calls like sendto and recvfrom. Instead the
   148  // arguments to the underlying system call are the number below
   149  // and a pointer to an array of uintptr. We hide the pointer in the
   150  // socketcall assembly to avoid allocation on every system call.
   151  
   152  const (
   153  	// see linux/net.h
   154  	_SOCKET      = 1
   155  	_BIND        = 2
   156  	_CONNECT     = 3
   157  	_LISTEN      = 4
   158  	_ACCEPT      = 5
   159  	_GETSOCKNAME = 6
   160  	_GETPEERNAME = 7
   161  	_SOCKETPAIR  = 8
   162  	_SEND        = 9
   163  	_RECV        = 10
   164  	_SENDTO      = 11
   165  	_RECVFROM    = 12
   166  	_SHUTDOWN    = 13
   167  	_SETSOCKOPT  = 14
   168  	_GETSOCKOPT  = 15
   169  	_SENDMSG     = 16
   170  	_RECVMSG     = 17
   171  	_ACCEPT4     = 18
   172  	_RECVMMSG    = 19
   173  	_SENDMMSG    = 20
   174  )
   175  
   176  func accept(s int, rsa *RawSockaddrAny, addrlen *_Socklen) (fd int, err error) {
   177  	fd, e := socketcall(_ACCEPT, uintptr(s), uintptr(unsafe.Pointer(rsa)), uintptr(unsafe.Pointer(addrlen)), 0, 0, 0)
   178  	if e != 0 {
   179  		err = e
   180  	}
   181  	return
   182  }
   183  
   184  func accept4(s int, rsa *RawSockaddrAny, addrlen *_Socklen, flags int) (fd int, err error) {
   185  	fd, e := socketcall(_ACCEPT4, uintptr(s), uintptr(unsafe.Pointer(rsa)), uintptr(unsafe.Pointer(addrlen)), uintptr(flags), 0, 0)
   186  	if e != 0 {
   187  		err = e
   188  	}
   189  	return
   190  }
   191  
   192  func getsockname(s int, rsa *RawSockaddrAny, addrlen *_Socklen) (err error) {
   193  	_, e := rawsocketcall(_GETSOCKNAME, uintptr(s), uintptr(unsafe.Pointer(rsa)), uintptr(unsafe.Pointer(addrlen)), 0, 0, 0)
   194  	if e != 0 {
   195  		err = e
   196  	}
   197  	return
   198  }
   199  
   200  func getpeername(s int, rsa *RawSockaddrAny, addrlen *_Socklen) (err error) {
   201  	_, e := rawsocketcall(_GETPEERNAME, uintptr(s), uintptr(unsafe.Pointer(rsa)), uintptr(unsafe.Pointer(addrlen)), 0, 0, 0)
   202  	if e != 0 {
   203  		err = e
   204  	}
   205  	return
   206  }
   207  
   208  func socketpair(domain int, typ int, flags int, fd *[2]int32) (err error) {
   209  	_, e := rawsocketcall(_SOCKETPAIR, uintptr(domain), uintptr(typ), uintptr(flags), uintptr(unsafe.Pointer(fd)), 0, 0)
   210  	if e != 0 {
   211  		err = e
   212  	}
   213  	return
   214  }
   215  
   216  func bind(s int, addr unsafe.Pointer, addrlen _Socklen) (err error) {
   217  	_, e := socketcall(_BIND, uintptr(s), uintptr(addr), uintptr(addrlen), 0, 0, 0)
   218  	if e != 0 {
   219  		err = e
   220  	}
   221  	return
   222  }
   223  
   224  func connect(s int, addr unsafe.Pointer, addrlen _Socklen) (err error) {
   225  	_, e := socketcall(_CONNECT, uintptr(s), uintptr(addr), uintptr(addrlen), 0, 0, 0)
   226  	if e != 0 {
   227  		err = e
   228  	}
   229  	return
   230  }
   231  
   232  func socket(domain int, typ int, proto int) (fd int, err error) {
   233  	fd, e := rawsocketcall(_SOCKET, uintptr(domain), uintptr(typ), uintptr(proto), 0, 0, 0)
   234  	if e != 0 {
   235  		err = e
   236  	}
   237  	return
   238  }
   239  
   240  func getsockopt(s int, level int, name int, val unsafe.Pointer, vallen *_Socklen) (err error) {
   241  	_, e := socketcall(_GETSOCKOPT, uintptr(s), uintptr(level), uintptr(name), uintptr(val), uintptr(unsafe.Pointer(vallen)), 0)
   242  	if e != 0 {
   243  		err = e
   244  	}
   245  	return
   246  }
   247  
   248  func setsockopt(s int, level int, name int, val unsafe.Pointer, vallen uintptr) (err error) {
   249  	_, e := socketcall(_SETSOCKOPT, uintptr(s), uintptr(level), uintptr(name), uintptr(val), vallen, 0)
   250  	if e != 0 {
   251  		err = e
   252  	}
   253  	return
   254  }
   255  
   256  func recvfrom(s int, p []byte, flags int, from *RawSockaddrAny, fromlen *_Socklen) (n int, err error) {
   257  	var base uintptr
   258  	if len(p) > 0 {
   259  		base = uintptr(unsafe.Pointer(&p[0]))
   260  	}
   261  	n, e := socketcall(_RECVFROM, uintptr(s), base, uintptr(len(p)), uintptr(flags), uintptr(unsafe.Pointer(from)), uintptr(unsafe.Pointer(fromlen)))
   262  	if e != 0 {
   263  		err = e
   264  	}
   265  	return
   266  }
   267  
   268  func sendto(s int, p []byte, flags int, to unsafe.Pointer, addrlen _Socklen) (err error) {
   269  	var base uintptr
   270  	if len(p) > 0 {
   271  		base = uintptr(unsafe.Pointer(&p[0]))
   272  	}
   273  	_, e := socketcall(_SENDTO, uintptr(s), base, uintptr(len(p)), uintptr(flags), uintptr(to), uintptr(addrlen))
   274  	if e != 0 {
   275  		err = e
   276  	}
   277  	return
   278  }
   279  
   280  func recvmsg(s int, msg *Msghdr, flags int) (n int, err error) {
   281  	n, e := socketcall(_RECVMSG, uintptr(s), uintptr(unsafe.Pointer(msg)), uintptr(flags), 0, 0, 0)
   282  	if e != 0 {
   283  		err = e
   284  	}
   285  	return
   286  }
   287  
   288  func sendmsg(s int, msg *Msghdr, flags int) (n int, err error) {
   289  	n, e := socketcall(_SENDMSG, uintptr(s), uintptr(unsafe.Pointer(msg)), uintptr(flags), 0, 0, 0)
   290  	if e != 0 {
   291  		err = e
   292  	}
   293  	return
   294  }
   295  
   296  func Listen(s int, n int) (err error) {
   297  	_, e := socketcall(_LISTEN, uintptr(s), uintptr(n), 0, 0, 0, 0)
   298  	if e != 0 {
   299  		err = e
   300  	}
   301  	return
   302  }
   303  
   304  func Shutdown(s, how int) (err error) {
   305  	_, e := socketcall(_SHUTDOWN, uintptr(s), uintptr(how), 0, 0, 0, 0)
   306  	if e != 0 {
   307  		err = e
   308  	}
   309  	return
   310  }
   311  
   312  func Fstatfs(fd int, buf *Statfs_t) (err error) {
   313  	_, _, e := Syscall(SYS_FSTATFS64, uintptr(fd), unsafe.Sizeof(*buf), uintptr(unsafe.Pointer(buf)))
   314  	if e != 0 {
   315  		err = e
   316  	}
   317  	return
   318  }
   319  
   320  func Statfs(path string, buf *Statfs_t) (err error) {
   321  	pathp, err := BytePtrFromString(path)
   322  	if err != nil {
   323  		return err
   324  	}
   325  	_, _, e := Syscall(SYS_STATFS64, uintptr(unsafe.Pointer(pathp)), unsafe.Sizeof(*buf), uintptr(unsafe.Pointer(buf)))
   326  	if e != 0 {
   327  		err = e
   328  	}
   329  	return
   330  }
   331  
   332  func (r *PtraceRegs) PC() uint64 { return uint64(uint32(r.Eip)) }
   333  
   334  func (r *PtraceRegs) SetPC(pc uint64) { r.Eip = int32(pc) }
   335  
   336  func (iov *Iovec) SetLen(length int) {
   337  	iov.Len = uint32(length)
   338  }
   339  
   340  func (msghdr *Msghdr) SetControllen(length int) {
   341  	msghdr.Controllen = uint32(length)
   342  }
   343  
   344  func (msghdr *Msghdr) SetIovlen(length int) {
   345  	msghdr.Iovlen = uint32(length)
   346  }
   347  
   348  func (cmsg *Cmsghdr) SetLen(length int) {
   349  	cmsg.Len = uint32(length)
   350  }
   351  
   352  func (rsa *RawSockaddrNFCLLCP) SetServiceNameLen(length int) {
   353  	rsa.Service_name_len = uint32(length)
   354  }
   355  

View as plain text