Source file src/syscall/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  package syscall
     6  
     7  import "unsafe"
     8  
     9  const (
    10  	_SYS_setgroups  = SYS_SETGROUPS32
    11  	_SYS_clone3     = 435
    12  	_SYS_faccessat2 = 439
    13  	_SYS_fchmodat2  = 452
    14  )
    15  
    16  func setTimespec(sec, nsec int64) Timespec {
    17  	return Timespec{Sec: int32(sec), Nsec: int32(nsec)}
    18  }
    19  
    20  func setTimeval(sec, usec int64) Timeval {
    21  	return Timeval{Sec: int32(sec), Usec: int32(usec)}
    22  }
    23  
    24  // 64-bit file system and 32-bit uid calls
    25  // (386 default is 32-bit file system and 16-bit uid).
    26  //sys	Dup2(oldfd int, newfd int) (err error)
    27  //sys	Fchown(fd int, uid int, gid int) (err error) = SYS_FCHOWN32
    28  //sys	Fstat(fd int, stat *Stat_t) (err error) = SYS_FSTAT64
    29  //sys	fstatat(dirfd int, path string, stat *Stat_t, flags int) (err error) = SYS_FSTATAT64
    30  //sys	Ftruncate(fd int, length int64) (err error) = SYS_FTRUNCATE64
    31  //sysnb	Getegid() (egid int) = SYS_GETEGID32
    32  //sysnb	Geteuid() (euid int) = SYS_GETEUID32
    33  //sysnb	Getgid() (gid int) = SYS_GETGID32
    34  //sysnb	Getuid() (uid int) = SYS_GETUID32
    35  //sysnb	InotifyInit() (fd int, err error)
    36  //sys	Ioperm(from int, num int, on int) (err error)
    37  //sys	Iopl(level int) (err error)
    38  //sys	Pause() (err error)
    39  //sys	pread(fd int, p []byte, offset int64) (n int, err error) = SYS_PREAD64
    40  //sys	pwrite(fd int, p []byte, offset int64) (n int, err error) = SYS_PWRITE64
    41  //sys	Renameat(olddirfd int, oldpath string, newdirfd int, newpath string) (err error)
    42  //sys	sendfile(outfd int, infd int, offset *int64, count int) (written int, err error) = SYS_SENDFILE64
    43  //sys	Setfsgid(gid int) (err error) = SYS_SETFSGID32
    44  //sys	Setfsuid(uid int) (err error) = SYS_SETFSUID32
    45  //sys	Splice(rfd int, roff *int64, wfd int, woff *int64, len int, flags int) (n int, err error)
    46  //sys	SyncFileRange(fd int, off int64, n int64, flags int) (err error)
    47  //sys	Truncate(path string, length int64) (err error) = SYS_TRUNCATE64
    48  //sys	Ustat(dev int, ubuf *Ustat_t) (err error)
    49  //sysnb	getgroups(n int, list *_Gid_t) (nn int, err error) = SYS_GETGROUPS32
    50  //sys	Select(nfd int, r *FdSet, w *FdSet, e *FdSet, timeout *Timeval) (n int, err error) = SYS__NEWSELECT
    51  
    52  //sys	mmap2(addr uintptr, length uintptr, prot int, flags int, fd int, pageOffset uintptr) (xaddr uintptr, err error)
    53  //sys	EpollWait(epfd int, events []EpollEvent, msec int) (n int, err error)
    54  
    55  func Stat(path string, stat *Stat_t) (err error) {
    56  	return fstatat(_AT_FDCWD, path, stat, 0)
    57  }
    58  
    59  func Lchown(path string, uid int, gid int) (err error) {
    60  	return Fchownat(_AT_FDCWD, path, uid, gid, _AT_SYMLINK_NOFOLLOW)
    61  }
    62  
    63  func Lstat(path string, stat *Stat_t) (err error) {
    64  	return fstatat(_AT_FDCWD, path, stat, _AT_SYMLINK_NOFOLLOW)
    65  }
    66  
    67  func mmap(addr uintptr, length uintptr, prot int, flags int, fd int, offset int64) (xaddr uintptr, err error) {
    68  	page := uintptr(offset / 4096)
    69  	if offset != int64(page)*4096 {
    70  		return 0, EINVAL
    71  	}
    72  	return mmap2(addr, length, prot, flags, fd, page)
    73  }
    74  
    75  // Underlying system call writes to newoffset via pointer.
    76  // Implemented in assembly to avoid allocation.
    77  func seek(fd int, offset int64, whence int) (newoffset int64, err Errno)
    78  
    79  func Seek(fd int, offset int64, whence int) (newoffset int64, err error) {
    80  	newoffset, errno := seek(fd, offset, whence)
    81  	if errno != 0 {
    82  		return 0, errno
    83  	}
    84  	return newoffset, nil
    85  }
    86  
    87  //sys	futimesat(dirfd int, path string, times *[2]Timeval) (err error)
    88  //sysnb	Gettimeofday(tv *Timeval) (err error)
    89  //sysnb	Time(t *Time_t) (tt Time_t, err error)
    90  //sys	Utime(path string, buf *Utimbuf) (err error)
    91  //sys	utimes(path string, times *[2]Timeval) (err error)
    92  
    93  // On x86 Linux, all the socket calls go through an extra indirection,
    94  // I think because the 5-register system call interface can't handle
    95  // the 6-argument calls like sendto and recvfrom. Instead the
    96  // arguments to the underlying system call are the number below
    97  // and a pointer to an array of uintptr. We hide the pointer in the
    98  // socketcall assembly to avoid allocation on every system call.
    99  
   100  const (
   101  	// see linux/net.h
   102  	_SOCKET      = 1
   103  	_BIND        = 2
   104  	_CONNECT     = 3
   105  	_LISTEN      = 4
   106  	_ACCEPT      = 5
   107  	_GETSOCKNAME = 6
   108  	_GETPEERNAME = 7
   109  	_SOCKETPAIR  = 8
   110  	_SEND        = 9
   111  	_RECV        = 10
   112  	_SENDTO      = 11
   113  	_RECVFROM    = 12
   114  	_SHUTDOWN    = 13
   115  	_SETSOCKOPT  = 14
   116  	_GETSOCKOPT  = 15
   117  	_SENDMSG     = 16
   118  	_RECVMSG     = 17
   119  	_ACCEPT4     = 18
   120  	_RECVMMSG    = 19
   121  	_SENDMMSG    = 20
   122  )
   123  
   124  func socketcall(call int, a0, a1, a2, a3, a4, a5 uintptr) (n int, err Errno)
   125  func rawsocketcall(call int, a0, a1, a2, a3, a4, a5 uintptr) (n int, err Errno)
   126  
   127  func accept4(s int, rsa *RawSockaddrAny, addrlen *_Socklen, flags int) (fd int, err error) {
   128  	fd, e := socketcall(_ACCEPT4, uintptr(s), uintptr(unsafe.Pointer(rsa)), uintptr(unsafe.Pointer(addrlen)), uintptr(flags), 0, 0)
   129  	if e != 0 {
   130  		err = e
   131  	}
   132  	return
   133  }
   134  
   135  func getsockname(s int, rsa *RawSockaddrAny, addrlen *_Socklen) (err error) {
   136  	_, e := rawsocketcall(_GETSOCKNAME, uintptr(s), uintptr(unsafe.Pointer(rsa)), uintptr(unsafe.Pointer(addrlen)), 0, 0, 0)
   137  	if e != 0 {
   138  		err = e
   139  	}
   140  	return
   141  }
   142  
   143  func getpeername(s int, rsa *RawSockaddrAny, addrlen *_Socklen) (err error) {
   144  	_, e := rawsocketcall(_GETPEERNAME, uintptr(s), uintptr(unsafe.Pointer(rsa)), uintptr(unsafe.Pointer(addrlen)), 0, 0, 0)
   145  	if e != 0 {
   146  		err = e
   147  	}
   148  	return
   149  }
   150  
   151  func socketpair(domain int, typ int, flags int, fd *[2]int32) (err error) {
   152  	_, e := rawsocketcall(_SOCKETPAIR, uintptr(domain), uintptr(typ), uintptr(flags), uintptr(unsafe.Pointer(fd)), 0, 0)
   153  	if e != 0 {
   154  		err = e
   155  	}
   156  	return
   157  }
   158  
   159  func bind(s int, addr unsafe.Pointer, addrlen _Socklen) (err error) {
   160  	_, e := socketcall(_BIND, uintptr(s), uintptr(addr), uintptr(addrlen), 0, 0, 0)
   161  	if e != 0 {
   162  		err = e
   163  	}
   164  	return
   165  }
   166  
   167  func connect(s int, addr unsafe.Pointer, addrlen _Socklen) (err error) {
   168  	_, e := socketcall(_CONNECT, uintptr(s), uintptr(addr), uintptr(addrlen), 0, 0, 0)
   169  	if e != 0 {
   170  		err = e
   171  	}
   172  	return
   173  }
   174  
   175  func socket(domain int, typ int, proto int) (fd int, err error) {
   176  	fd, e := rawsocketcall(_SOCKET, uintptr(domain), uintptr(typ), uintptr(proto), 0, 0, 0)
   177  	if e != 0 {
   178  		err = e
   179  	}
   180  	return
   181  }
   182  
   183  func getsockopt(s int, level int, name int, val unsafe.Pointer, vallen *_Socklen) (err error) {
   184  	_, e := socketcall(_GETSOCKOPT, uintptr(s), uintptr(level), uintptr(name), uintptr(val), uintptr(unsafe.Pointer(vallen)), 0)
   185  	if e != 0 {
   186  		err = e
   187  	}
   188  	return
   189  }
   190  
   191  func setsockopt(s int, level int, name int, val unsafe.Pointer, vallen uintptr) (err error) {
   192  	_, e := socketcall(_SETSOCKOPT, uintptr(s), uintptr(level), uintptr(name), uintptr(val), vallen, 0)
   193  	if e != 0 {
   194  		err = e
   195  	}
   196  	return
   197  }
   198  
   199  func recvfrom(s int, p []byte, flags int, from *RawSockaddrAny, fromlen *_Socklen) (n int, err error) {
   200  	var base uintptr
   201  	if len(p) > 0 {
   202  		base = uintptr(unsafe.Pointer(&p[0]))
   203  	}
   204  	n, e := socketcall(_RECVFROM, uintptr(s), base, uintptr(len(p)), uintptr(flags), uintptr(unsafe.Pointer(from)), uintptr(unsafe.Pointer(fromlen)))
   205  	if e != 0 {
   206  		err = e
   207  	}
   208  	return
   209  }
   210  
   211  func sendto(s int, p []byte, flags int, to unsafe.Pointer, addrlen _Socklen) (err error) {
   212  	var base uintptr
   213  	if len(p) > 0 {
   214  		base = uintptr(unsafe.Pointer(&p[0]))
   215  	}
   216  	_, e := socketcall(_SENDTO, uintptr(s), base, uintptr(len(p)), uintptr(flags), uintptr(to), uintptr(addrlen))
   217  	if e != 0 {
   218  		err = e
   219  	}
   220  	return
   221  }
   222  
   223  func recvmsg(s int, msg *Msghdr, flags int) (n int, err error) {
   224  	n, e := socketcall(_RECVMSG, uintptr(s), uintptr(unsafe.Pointer(msg)), uintptr(flags), 0, 0, 0)
   225  	if e != 0 {
   226  		err = e
   227  	}
   228  	return
   229  }
   230  
   231  func sendmsg(s int, msg *Msghdr, flags int) (n int, err error) {
   232  	n, e := socketcall(_SENDMSG, uintptr(s), uintptr(unsafe.Pointer(msg)), uintptr(flags), 0, 0, 0)
   233  	if e != 0 {
   234  		err = e
   235  	}
   236  	return
   237  }
   238  
   239  func Listen(s int, n int) (err error) {
   240  	_, e := socketcall(_LISTEN, uintptr(s), uintptr(n), 0, 0, 0, 0)
   241  	if e != 0 {
   242  		err = e
   243  	}
   244  	return
   245  }
   246  
   247  func Shutdown(s, how int) (err error) {
   248  	_, e := socketcall(_SHUTDOWN, uintptr(s), uintptr(how), 0, 0, 0, 0)
   249  	if e != 0 {
   250  		err = e
   251  	}
   252  	return
   253  }
   254  
   255  func Fstatfs(fd int, buf *Statfs_t) (err error) {
   256  	_, _, e := Syscall(SYS_FSTATFS64, uintptr(fd), unsafe.Sizeof(*buf), uintptr(unsafe.Pointer(buf)))
   257  	if e != 0 {
   258  		err = e
   259  	}
   260  	return
   261  }
   262  
   263  func Statfs(path string, buf *Statfs_t) (err error) {
   264  	pathp, err := BytePtrFromString(path)
   265  	if err != nil {
   266  		return err
   267  	}
   268  	_, _, e := Syscall(SYS_STATFS64, uintptr(unsafe.Pointer(pathp)), unsafe.Sizeof(*buf), uintptr(unsafe.Pointer(buf)))
   269  	if e != 0 {
   270  		err = e
   271  	}
   272  	return
   273  }
   274  
   275  func (r *PtraceRegs) PC() uint64 { return uint64(uint32(r.Eip)) }
   276  
   277  func (r *PtraceRegs) SetPC(pc uint64) { r.Eip = int32(pc) }
   278  
   279  func (iov *Iovec) SetLen(length int) {
   280  	iov.Len = uint32(length)
   281  }
   282  
   283  func (msghdr *Msghdr) SetControllen(length int) {
   284  	msghdr.Controllen = uint32(length)
   285  }
   286  
   287  func (cmsg *Cmsghdr) SetLen(length int) {
   288  	cmsg.Len = uint32(length)
   289  }
   290  

View as plain text