Russ Cox | 802d6d4 | 2009-06-04 13:33:40 -0700 | [diff] [blame] | 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 | |
Russ Cox | b04ac10 | 2009-08-12 13:19:17 -0700 | [diff] [blame] | 7 | import "unsafe" |
Russ Cox | 802d6d4 | 2009-06-04 13:33:40 -0700 | [diff] [blame] | 8 | |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 9 | func Getpagesize() int { return 4096 } |
Russ Cox | 802d6d4 | 2009-06-04 13:33:40 -0700 | [diff] [blame] | 10 | |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 11 | func TimespecToNsec(ts Timespec) int64 { return int64(ts.Sec)*1e9 + int64(ts.Nsec) } |
Russ Cox | 802d6d4 | 2009-06-04 13:33:40 -0700 | [diff] [blame] | 12 | |
| 13 | func NsecToTimespec(nsec int64) (ts Timespec) { |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 14 | ts.Sec = int32(nsec / 1e9) |
| 15 | ts.Nsec = int32(nsec % 1e9) |
| 16 | return |
Russ Cox | 802d6d4 | 2009-06-04 13:33:40 -0700 | [diff] [blame] | 17 | } |
| 18 | |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 19 | func TimevalToNsec(tv Timeval) int64 { return int64(tv.Sec)*1e9 + int64(tv.Usec)*1e3 } |
Russ Cox | 802d6d4 | 2009-06-04 13:33:40 -0700 | [diff] [blame] | 20 | |
| 21 | func NsecToTimeval(nsec int64) (tv Timeval) { |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 22 | nsec += 999 // round up to microsecond |
| 23 | tv.Sec = int32(nsec / 1e9) |
| 24 | tv.Usec = int32(nsec % 1e9 / 1e3) |
| 25 | return |
Russ Cox | 802d6d4 | 2009-06-04 13:33:40 -0700 | [diff] [blame] | 26 | } |
| 27 | |
Russ Cox | 76c87d5 | 2009-06-16 17:17:02 -0700 | [diff] [blame] | 28 | // 64-bit file system and 32-bit uid calls |
| 29 | // (386 default is 32-bit file system and 16-bit uid). |
| 30 | //sys Chown(path string, uid int, gid int) (errno int) = SYS_CHOWN32 |
| 31 | //sys Fchown(fd int, uid int, gid int) (errno int) = SYS_FCHOWN32 |
| 32 | //sys Fstat(fd int, stat *Stat_t) (errno int) = SYS_FSTAT64 |
Russ Cox | 76c87d5 | 2009-06-16 17:17:02 -0700 | [diff] [blame] | 33 | //sys Getegid() (egid int) = SYS_GETEGID32 |
| 34 | //sys Geteuid() (euid int) = SYS_GETEUID32 |
| 35 | //sys Getgid() (gid int) = SYS_GETGID32 |
| 36 | //sys Getuid() (uid int) = SYS_GETUID32 |
Kai Backman | 116beb2 | 2009-10-06 16:39:38 -0700 | [diff] [blame] | 37 | //sys Ioperm(from int, num int, on int) (errno int) |
| 38 | //sys Iopl(level int) (errno int) |
Russ Cox | 76c87d5 | 2009-06-16 17:17:02 -0700 | [diff] [blame] | 39 | //sys Lchown(path string, uid int, gid int) (errno int) = SYS_LCHOWN32 |
| 40 | //sys Lstat(path string, stat *Stat_t) (errno int) = SYS_LSTAT64 |
| 41 | //sys Setfsgid(gid int) (errno int) = SYS_SETFSGID32 |
| 42 | //sys Setfsuid(uid int) (errno int) = SYS_SETFSUID32 |
| 43 | //sys Setgid(gid int) (errno int) = SYS_SETGID32 |
| 44 | //sys Setregid(rgid int, egid int) (errno int) = SYS_SETREGID32 |
| 45 | //sys Setresgid(rgid int, egid int, sgid int) (errno int) = SYS_SETRESGID32 |
| 46 | //sys Setresuid(ruid int, euid int, suid int) (errno int) = SYS_SETRESUID32 |
| 47 | //sys Setreuid(ruid int, euid int) (errno int) = SYS_SETREUID32 |
| 48 | //sys Stat(path string, stat *Stat_t) (errno int) = SYS_STAT64 |
Kai Backman | 116beb2 | 2009-10-06 16:39:38 -0700 | [diff] [blame] | 49 | //sys SyncFileRange(fd int, off int64, n int64, flags int) (errno int) |
Russ Cox | 76c87d5 | 2009-06-16 17:17:02 -0700 | [diff] [blame] | 50 | //sys getgroups(n int, list *_Gid_t) (nn int, errno int) = SYS_GETGROUPS32 |
| 51 | //sys setgroups(n int, list *_Gid_t) (errno int) = SYS_SETGROUPS32 |
Russ Cox | da5e962 | 2009-06-17 15:16:06 -0700 | [diff] [blame] | 52 | //sys Select(nfd int, r *FdSet, w *FdSet, e *FdSet, timeout *Timeval) (n int, errno int) = SYS__NEWSELECT |
| 53 | |
Russ Cox | 76c87d5 | 2009-06-16 17:17:02 -0700 | [diff] [blame] | 54 | // Underlying system call writes to newoffset via pointer. |
| 55 | // Implemented in assembly to avoid allocation. |
| 56 | func Seek(fd int, offset int64, whence int) (newoffset int64, errno int) |
| 57 | |
Russ Cox | 802d6d4 | 2009-06-04 13:33:40 -0700 | [diff] [blame] | 58 | // On x86 Linux, all the socket calls go through an extra indirection, |
| 59 | // I think because the 5-register system call interface can't handle |
| 60 | // the 6-argument calls like sendto and recvfrom. Instead the |
| 61 | // arguments to the underlying system call are the number below |
| 62 | // and a pointer to an array of uintptr. We hide the pointer in the |
| 63 | // socketcall assembly to avoid allocation on every system call. |
| 64 | |
| 65 | const ( |
| 66 | // see linux/net.h |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 67 | _SOCKET = 1 |
| 68 | _BIND = 2 |
| 69 | _CONNECT = 3 |
| 70 | _LISTEN = 4 |
| 71 | _ACCEPT = 5 |
| 72 | _GETSOCKNAME = 6 |
| 73 | _GETPEERNAME = 7 |
| 74 | _SOCKETPAIR = 8 |
| 75 | _SEND = 9 |
| 76 | _RECV = 10 |
| 77 | _SENDTO = 11 |
| 78 | _RECVFROM = 12 |
| 79 | _SHUTDOWN = 13 |
| 80 | _SETSOCKOPT = 14 |
| 81 | _GETSOCKOPT = 15 |
| 82 | _SENDMSG = 16 |
| 83 | _RECVMSG = 17 |
Russ Cox | 802d6d4 | 2009-06-04 13:33:40 -0700 | [diff] [blame] | 84 | ) |
| 85 | |
| 86 | func socketcall(call int, a0, a1, a2, a3, a4, a5 uintptr) (n int, errno int) |
| 87 | |
| 88 | func accept(s int, rsa *RawSockaddrAny, addrlen *_Socklen) (fd int, errno int) { |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 89 | fd, errno = socketcall(_ACCEPT, uintptr(s), uintptr(unsafe.Pointer(rsa)), uintptr(unsafe.Pointer(addrlen)), 0, 0, 0) |
| 90 | return |
Russ Cox | 802d6d4 | 2009-06-04 13:33:40 -0700 | [diff] [blame] | 91 | } |
| 92 | |
Russ Cox | 5d2ee9d | 2009-06-17 21:44:26 -0700 | [diff] [blame] | 93 | func getsockname(s int, rsa *RawSockaddrAny, addrlen *_Socklen) (errno int) { |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 94 | _, errno = socketcall(_GETSOCKNAME, uintptr(s), uintptr(unsafe.Pointer(rsa)), uintptr(unsafe.Pointer(addrlen)), 0, 0, 0) |
| 95 | return |
Russ Cox | 5d2ee9d | 2009-06-17 21:44:26 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | func getpeername(s int, rsa *RawSockaddrAny, addrlen *_Socklen) (errno int) { |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 99 | _, errno = socketcall(_GETPEERNAME, uintptr(s), uintptr(unsafe.Pointer(rsa)), uintptr(unsafe.Pointer(addrlen)), 0, 0, 0) |
| 100 | return |
Russ Cox | 5d2ee9d | 2009-06-17 21:44:26 -0700 | [diff] [blame] | 101 | } |
| 102 | |
Ivan Krasin | 400f7a6 | 2010-06-30 14:58:21 -0700 | [diff] [blame] | 103 | func socketpair(domain int, typ int, proto int) (fd [2]int, errno int) { |
| 104 | var f [2]int |
| 105 | _, errno = socketcall(_SOCKETPAIR, uintptr(domain), uintptr(typ), uintptr(proto), uintptr(unsafe.Pointer(&f)), 0, 0) |
| 106 | fd = f |
| 107 | return |
| 108 | } |
| 109 | |
Russ Cox | 802d6d4 | 2009-06-04 13:33:40 -0700 | [diff] [blame] | 110 | func bind(s int, addr uintptr, addrlen _Socklen) (errno int) { |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 111 | _, errno = socketcall(_BIND, uintptr(s), uintptr(addr), uintptr(addrlen), 0, 0, 0) |
| 112 | return |
Russ Cox | 802d6d4 | 2009-06-04 13:33:40 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | func connect(s int, addr uintptr, addrlen _Socklen) (errno int) { |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 116 | _, errno = socketcall(_CONNECT, uintptr(s), uintptr(addr), uintptr(addrlen), 0, 0, 0) |
| 117 | return |
Russ Cox | 802d6d4 | 2009-06-04 13:33:40 -0700 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | func socket(domain int, typ int, proto int) (fd int, errno int) { |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 121 | fd, errno = socketcall(_SOCKET, uintptr(domain), uintptr(typ), uintptr(proto), 0, 0, 0) |
| 122 | return |
Russ Cox | 802d6d4 | 2009-06-04 13:33:40 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | func setsockopt(s int, level int, name int, val uintptr, vallen int) (errno int) { |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 126 | _, errno = socketcall(_SETSOCKOPT, uintptr(s), uintptr(level), uintptr(name), uintptr(val), uintptr(vallen), 0) |
| 127 | return |
Russ Cox | 802d6d4 | 2009-06-04 13:33:40 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Russ Cox | fd1add2 | 2009-11-01 11:13:27 -0800 | [diff] [blame] | 130 | func recvfrom(s int, p []byte, flags int, from *RawSockaddrAny, fromlen *_Socklen) (n int, errno int) { |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 131 | var base uintptr |
Russ Cox | fd1add2 | 2009-11-01 11:13:27 -0800 | [diff] [blame] | 132 | if len(p) > 0 { |
Russ Cox | a65bf95 | 2009-11-17 08:39:04 -0800 | [diff] [blame] | 133 | base = uintptr(unsafe.Pointer(&p[0])) |
Russ Cox | fd1add2 | 2009-11-01 11:13:27 -0800 | [diff] [blame] | 134 | } |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 135 | n, errno = socketcall(_RECVFROM, uintptr(s), base, uintptr(len(p)), uintptr(flags), uintptr(unsafe.Pointer(from)), uintptr(unsafe.Pointer(fromlen))) |
| 136 | return |
Russ Cox | fd1add2 | 2009-11-01 11:13:27 -0800 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | func sendto(s int, p []byte, flags int, to uintptr, addrlen _Socklen) (errno int) { |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 140 | var base uintptr |
Russ Cox | fd1add2 | 2009-11-01 11:13:27 -0800 | [diff] [blame] | 141 | if len(p) > 0 { |
Russ Cox | a65bf95 | 2009-11-17 08:39:04 -0800 | [diff] [blame] | 142 | base = uintptr(unsafe.Pointer(&p[0])) |
Russ Cox | fd1add2 | 2009-11-01 11:13:27 -0800 | [diff] [blame] | 143 | } |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 144 | _, errno = socketcall(_SENDTO, uintptr(s), base, uintptr(len(p)), uintptr(flags), to, uintptr(addrlen)) |
| 145 | return |
Robert Griesemer | bd4f940 | 2009-11-05 10:55:57 -0800 | [diff] [blame] | 146 | } |
Russ Cox | fd1add2 | 2009-11-01 11:13:27 -0800 | [diff] [blame] | 147 | |
Russ Cox | 802d6d4 | 2009-06-04 13:33:40 -0700 | [diff] [blame] | 148 | func Listen(s int, n int) (errno int) { |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 149 | _, errno = socketcall(_LISTEN, uintptr(s), uintptr(n), 0, 0, 0, 0) |
| 150 | return |
Russ Cox | 802d6d4 | 2009-06-04 13:33:40 -0700 | [diff] [blame] | 151 | } |
Russ Cox | 74bb34c | 2009-09-03 16:17:21 -0700 | [diff] [blame] | 152 | |
Ian Lance Taylor | 952b91e | 2009-12-02 08:24:14 -0800 | [diff] [blame] | 153 | func Shutdown(s, how int) (errno int) { |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 154 | _, errno = socketcall(_SHUTDOWN, uintptr(s), uintptr(how), 0, 0, 0, 0) |
| 155 | return |
Ian Lance Taylor | 952b91e | 2009-12-02 08:24:14 -0800 | [diff] [blame] | 156 | } |
| 157 | |
Ian Lance Taylor | 44c1eb6 | 2009-12-04 21:58:32 -0800 | [diff] [blame] | 158 | func Fstatfs(fd int, buf *Statfs_t) (errno int) { |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 159 | _, _, e1 := Syscall(SYS_FSTATFS64, uintptr(fd), uintptr(unsafe.Sizeof(*buf)), uintptr(unsafe.Pointer(buf))) |
| 160 | errno = int(e1) |
| 161 | return |
Ian Lance Taylor | 44c1eb6 | 2009-12-04 21:58:32 -0800 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | func Statfs(path string, buf *Statfs_t) (errno int) { |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 165 | _, _, e1 := Syscall(SYS_STATFS64, uintptr(unsafe.Pointer(StringBytePtr(path))), uintptr(unsafe.Sizeof(*buf)), uintptr(unsafe.Pointer(buf))) |
| 166 | errno = int(e1) |
| 167 | return |
Ian Lance Taylor | 44c1eb6 | 2009-12-04 21:58:32 -0800 | [diff] [blame] | 168 | } |
| 169 | |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 170 | func (r *PtraceRegs) PC() uint64 { return uint64(uint32(r.Eip)) } |
Russ Cox | 74bb34c | 2009-09-03 16:17:21 -0700 | [diff] [blame] | 171 | |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 172 | func (r *PtraceRegs) SetPC(pc uint64) { r.Eip = int32(pc) } |