Serge Bazanski | cc25bdf | 2018-10-25 14:02:58 +0200 | [diff] [blame] | 1 | // Copyright 2016 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 | // +build s390x,linux |
| 6 | |
| 7 | package unix |
| 8 | |
| 9 | import ( |
| 10 | "unsafe" |
| 11 | ) |
| 12 | |
| 13 | //sys Dup2(oldfd int, newfd int) (err error) |
| 14 | //sysnb EpollCreate(size int) (fd int, err error) |
| 15 | //sys EpollWait(epfd int, events []EpollEvent, msec int) (n int, err error) |
| 16 | //sys Fadvise(fd int, offset int64, length int64, advice int) (err error) = SYS_FADVISE64 |
| 17 | //sys Fchown(fd int, uid int, gid int) (err error) |
| 18 | //sys Fstat(fd int, stat *Stat_t) (err error) |
| 19 | //sys Fstatat(dirfd int, path string, stat *Stat_t, flags int) (err error) = SYS_NEWFSTATAT |
| 20 | //sys Fstatfs(fd int, buf *Statfs_t) (err error) |
| 21 | //sys Ftruncate(fd int, length int64) (err error) |
| 22 | //sysnb Getegid() (egid int) |
| 23 | //sysnb Geteuid() (euid int) |
| 24 | //sysnb Getgid() (gid int) |
| 25 | //sysnb Getrlimit(resource int, rlim *Rlimit) (err error) |
| 26 | //sysnb Getuid() (uid int) |
| 27 | //sysnb InotifyInit() (fd int, err error) |
| 28 | //sys Lchown(path string, uid int, gid int) (err error) |
| 29 | //sys Lstat(path string, stat *Stat_t) (err error) |
| 30 | //sys Pause() (err error) |
| 31 | //sys Pread(fd int, p []byte, offset int64) (n int, err error) = SYS_PREAD64 |
| 32 | //sys Pwrite(fd int, p []byte, offset int64) (n int, err error) = SYS_PWRITE64 |
| 33 | //sys Seek(fd int, offset int64, whence int) (off int64, err error) = SYS_LSEEK |
| 34 | //sys Select(nfd int, r *FdSet, w *FdSet, e *FdSet, timeout *Timeval) (n int, err error) |
| 35 | //sys sendfile(outfd int, infd int, offset *int64, count int) (written int, err error) |
| 36 | //sys Setfsgid(gid int) (err error) |
| 37 | //sys Setfsuid(uid int) (err error) |
| 38 | //sysnb Setregid(rgid int, egid int) (err error) |
| 39 | //sysnb Setresgid(rgid int, egid int, sgid int) (err error) |
| 40 | //sysnb Setresuid(ruid int, euid int, suid int) (err error) |
| 41 | //sysnb Setrlimit(resource int, rlim *Rlimit) (err error) |
| 42 | //sysnb Setreuid(ruid int, euid int) (err error) |
| 43 | //sys Splice(rfd int, roff *int64, wfd int, woff *int64, len int, flags int) (n int64, err error) |
| 44 | //sys Stat(path string, stat *Stat_t) (err error) |
| 45 | //sys Statfs(path string, buf *Statfs_t) (err error) |
| 46 | //sys SyncFileRange(fd int, off int64, n int64, flags int) (err error) |
| 47 | //sys Truncate(path string, length int64) (err error) |
| 48 | //sys Ustat(dev int, ubuf *Ustat_t) (err error) |
| 49 | //sysnb getgroups(n int, list *_Gid_t) (nn int, err error) |
| 50 | //sysnb setgroups(n int, list *_Gid_t) (err error) |
| 51 | |
| 52 | //sys futimesat(dirfd int, path string, times *[2]Timeval) (err error) |
| 53 | //sysnb Gettimeofday(tv *Timeval) (err error) |
| 54 | |
| 55 | func Time(t *Time_t) (tt Time_t, err error) { |
| 56 | var tv Timeval |
| 57 | err = Gettimeofday(&tv) |
| 58 | if err != nil { |
| 59 | return 0, err |
| 60 | } |
| 61 | if t != nil { |
| 62 | *t = Time_t(tv.Sec) |
| 63 | } |
| 64 | return Time_t(tv.Sec), nil |
| 65 | } |
| 66 | |
| 67 | //sys Utime(path string, buf *Utimbuf) (err error) |
| 68 | //sys utimes(path string, times *[2]Timeval) (err error) |
| 69 | |
| 70 | func setTimespec(sec, nsec int64) Timespec { |
| 71 | return Timespec{Sec: sec, Nsec: nsec} |
| 72 | } |
| 73 | |
| 74 | func setTimeval(sec, usec int64) Timeval { |
| 75 | return Timeval{Sec: sec, Usec: usec} |
| 76 | } |
| 77 | |
| 78 | //sysnb pipe2(p *[2]_C_int, flags int) (err error) |
| 79 | |
| 80 | func Pipe(p []int) (err error) { |
| 81 | if len(p) != 2 { |
| 82 | return EINVAL |
| 83 | } |
| 84 | var pp [2]_C_int |
| 85 | err = pipe2(&pp, 0) // pipe2 is the same as pipe when flags are set to 0. |
| 86 | p[0] = int(pp[0]) |
| 87 | p[1] = int(pp[1]) |
| 88 | return |
| 89 | } |
| 90 | |
| 91 | func Pipe2(p []int, flags int) (err error) { |
| 92 | if len(p) != 2 { |
| 93 | return EINVAL |
| 94 | } |
| 95 | var pp [2]_C_int |
| 96 | err = pipe2(&pp, flags) |
| 97 | p[0] = int(pp[0]) |
| 98 | p[1] = int(pp[1]) |
| 99 | return |
| 100 | } |
| 101 | |
| 102 | func Ioperm(from int, num int, on int) (err error) { |
| 103 | return ENOSYS |
| 104 | } |
| 105 | |
| 106 | func Iopl(level int) (err error) { |
| 107 | return ENOSYS |
| 108 | } |
| 109 | |
| 110 | func (r *PtraceRegs) PC() uint64 { return r.Psw.Addr } |
| 111 | |
| 112 | func (r *PtraceRegs) SetPC(pc uint64) { r.Psw.Addr = pc } |
| 113 | |
| 114 | func (iov *Iovec) SetLen(length int) { |
| 115 | iov.Len = uint64(length) |
| 116 | } |
| 117 | |
| 118 | func (msghdr *Msghdr) SetControllen(length int) { |
| 119 | msghdr.Controllen = uint64(length) |
| 120 | } |
| 121 | |
| 122 | func (cmsg *Cmsghdr) SetLen(length int) { |
| 123 | cmsg.Len = uint64(length) |
| 124 | } |
| 125 | |
| 126 | // Linux on s390x uses the old mmap interface, which requires arguments to be passed in a struct. |
| 127 | // mmap2 also requires arguments to be passed in a struct; it is currently not exposed in <asm/unistd.h>. |
| 128 | func mmap(addr uintptr, length uintptr, prot int, flags int, fd int, offset int64) (xaddr uintptr, err error) { |
| 129 | mmap_args := [6]uintptr{addr, length, uintptr(prot), uintptr(flags), uintptr(fd), uintptr(offset)} |
| 130 | r0, _, e1 := Syscall(SYS_MMAP, uintptr(unsafe.Pointer(&mmap_args[0])), 0, 0) |
| 131 | xaddr = uintptr(r0) |
| 132 | if e1 != 0 { |
| 133 | err = errnoErr(e1) |
| 134 | } |
| 135 | return |
| 136 | } |
| 137 | |
| 138 | // On s390x Linux, all the socket calls go through an extra indirection. |
| 139 | // The arguments to the underlying system call (SYS_SOCKETCALL) are the |
| 140 | // number below and a pointer to an array of uintptr. |
| 141 | const ( |
| 142 | // see linux/net.h |
| 143 | netSocket = 1 |
| 144 | netBind = 2 |
| 145 | netConnect = 3 |
| 146 | netListen = 4 |
| 147 | netAccept = 5 |
| 148 | netGetSockName = 6 |
| 149 | netGetPeerName = 7 |
| 150 | netSocketPair = 8 |
| 151 | netSend = 9 |
| 152 | netRecv = 10 |
| 153 | netSendTo = 11 |
| 154 | netRecvFrom = 12 |
| 155 | netShutdown = 13 |
| 156 | netSetSockOpt = 14 |
| 157 | netGetSockOpt = 15 |
| 158 | netSendMsg = 16 |
| 159 | netRecvMsg = 17 |
| 160 | netAccept4 = 18 |
| 161 | netRecvMMsg = 19 |
| 162 | netSendMMsg = 20 |
| 163 | ) |
| 164 | |
| 165 | func accept(s int, rsa *RawSockaddrAny, addrlen *_Socklen) (int, error) { |
| 166 | args := [3]uintptr{uintptr(s), uintptr(unsafe.Pointer(rsa)), uintptr(unsafe.Pointer(addrlen))} |
| 167 | fd, _, err := Syscall(SYS_SOCKETCALL, netAccept, uintptr(unsafe.Pointer(&args)), 0) |
| 168 | if err != 0 { |
| 169 | return 0, err |
| 170 | } |
| 171 | return int(fd), nil |
| 172 | } |
| 173 | |
| 174 | func accept4(s int, rsa *RawSockaddrAny, addrlen *_Socklen, flags int) (int, error) { |
| 175 | args := [4]uintptr{uintptr(s), uintptr(unsafe.Pointer(rsa)), uintptr(unsafe.Pointer(addrlen)), uintptr(flags)} |
| 176 | fd, _, err := Syscall(SYS_SOCKETCALL, netAccept4, uintptr(unsafe.Pointer(&args)), 0) |
| 177 | if err != 0 { |
| 178 | return 0, err |
| 179 | } |
| 180 | return int(fd), nil |
| 181 | } |
| 182 | |
| 183 | func getsockname(s int, rsa *RawSockaddrAny, addrlen *_Socklen) error { |
| 184 | args := [3]uintptr{uintptr(s), uintptr(unsafe.Pointer(rsa)), uintptr(unsafe.Pointer(addrlen))} |
| 185 | _, _, err := RawSyscall(SYS_SOCKETCALL, netGetSockName, uintptr(unsafe.Pointer(&args)), 0) |
| 186 | if err != 0 { |
| 187 | return err |
| 188 | } |
| 189 | return nil |
| 190 | } |
| 191 | |
| 192 | func getpeername(s int, rsa *RawSockaddrAny, addrlen *_Socklen) error { |
| 193 | args := [3]uintptr{uintptr(s), uintptr(unsafe.Pointer(rsa)), uintptr(unsafe.Pointer(addrlen))} |
| 194 | _, _, err := RawSyscall(SYS_SOCKETCALL, netGetPeerName, uintptr(unsafe.Pointer(&args)), 0) |
| 195 | if err != 0 { |
| 196 | return err |
| 197 | } |
| 198 | return nil |
| 199 | } |
| 200 | |
| 201 | func socketpair(domain int, typ int, flags int, fd *[2]int32) error { |
| 202 | args := [4]uintptr{uintptr(domain), uintptr(typ), uintptr(flags), uintptr(unsafe.Pointer(fd))} |
| 203 | _, _, err := RawSyscall(SYS_SOCKETCALL, netSocketPair, uintptr(unsafe.Pointer(&args)), 0) |
| 204 | if err != 0 { |
| 205 | return err |
| 206 | } |
| 207 | return nil |
| 208 | } |
| 209 | |
| 210 | func bind(s int, addr unsafe.Pointer, addrlen _Socklen) error { |
| 211 | args := [3]uintptr{uintptr(s), uintptr(addr), uintptr(addrlen)} |
| 212 | _, _, err := Syscall(SYS_SOCKETCALL, netBind, uintptr(unsafe.Pointer(&args)), 0) |
| 213 | if err != 0 { |
| 214 | return err |
| 215 | } |
| 216 | return nil |
| 217 | } |
| 218 | |
| 219 | func connect(s int, addr unsafe.Pointer, addrlen _Socklen) error { |
| 220 | args := [3]uintptr{uintptr(s), uintptr(addr), uintptr(addrlen)} |
| 221 | _, _, err := Syscall(SYS_SOCKETCALL, netConnect, uintptr(unsafe.Pointer(&args)), 0) |
| 222 | if err != 0 { |
| 223 | return err |
| 224 | } |
| 225 | return nil |
| 226 | } |
| 227 | |
| 228 | func socket(domain int, typ int, proto int) (int, error) { |
| 229 | args := [3]uintptr{uintptr(domain), uintptr(typ), uintptr(proto)} |
| 230 | fd, _, err := RawSyscall(SYS_SOCKETCALL, netSocket, uintptr(unsafe.Pointer(&args)), 0) |
| 231 | if err != 0 { |
| 232 | return 0, err |
| 233 | } |
| 234 | return int(fd), nil |
| 235 | } |
| 236 | |
| 237 | func getsockopt(s int, level int, name int, val unsafe.Pointer, vallen *_Socklen) error { |
| 238 | args := [5]uintptr{uintptr(s), uintptr(level), uintptr(name), uintptr(val), uintptr(unsafe.Pointer(vallen))} |
| 239 | _, _, err := Syscall(SYS_SOCKETCALL, netGetSockOpt, uintptr(unsafe.Pointer(&args)), 0) |
| 240 | if err != 0 { |
| 241 | return err |
| 242 | } |
| 243 | return nil |
| 244 | } |
| 245 | |
| 246 | func setsockopt(s int, level int, name int, val unsafe.Pointer, vallen uintptr) error { |
| 247 | args := [4]uintptr{uintptr(s), uintptr(level), uintptr(name), uintptr(val)} |
| 248 | _, _, err := Syscall(SYS_SOCKETCALL, netSetSockOpt, uintptr(unsafe.Pointer(&args)), 0) |
| 249 | if err != 0 { |
| 250 | return err |
| 251 | } |
| 252 | return nil |
| 253 | } |
| 254 | |
| 255 | func recvfrom(s int, p []byte, flags int, from *RawSockaddrAny, fromlen *_Socklen) (int, error) { |
| 256 | var base uintptr |
| 257 | if len(p) > 0 { |
| 258 | base = uintptr(unsafe.Pointer(&p[0])) |
| 259 | } |
| 260 | args := [6]uintptr{uintptr(s), base, uintptr(len(p)), uintptr(flags), uintptr(unsafe.Pointer(from)), uintptr(unsafe.Pointer(fromlen))} |
| 261 | n, _, err := Syscall(SYS_SOCKETCALL, netRecvFrom, uintptr(unsafe.Pointer(&args)), 0) |
| 262 | if err != 0 { |
| 263 | return 0, err |
| 264 | } |
| 265 | return int(n), nil |
| 266 | } |
| 267 | |
| 268 | func sendto(s int, p []byte, flags int, to unsafe.Pointer, addrlen _Socklen) error { |
| 269 | var base uintptr |
| 270 | if len(p) > 0 { |
| 271 | base = uintptr(unsafe.Pointer(&p[0])) |
| 272 | } |
| 273 | args := [6]uintptr{uintptr(s), base, uintptr(len(p)), uintptr(flags), uintptr(to), uintptr(addrlen)} |
| 274 | _, _, err := Syscall(SYS_SOCKETCALL, netSendTo, uintptr(unsafe.Pointer(&args)), 0) |
| 275 | if err != 0 { |
| 276 | return err |
| 277 | } |
| 278 | return nil |
| 279 | } |
| 280 | |
| 281 | func recvmsg(s int, msg *Msghdr, flags int) (int, error) { |
| 282 | args := [3]uintptr{uintptr(s), uintptr(unsafe.Pointer(msg)), uintptr(flags)} |
| 283 | n, _, err := Syscall(SYS_SOCKETCALL, netRecvMsg, uintptr(unsafe.Pointer(&args)), 0) |
| 284 | if err != 0 { |
| 285 | return 0, err |
| 286 | } |
| 287 | return int(n), nil |
| 288 | } |
| 289 | |
| 290 | func sendmsg(s int, msg *Msghdr, flags int) (int, error) { |
| 291 | args := [3]uintptr{uintptr(s), uintptr(unsafe.Pointer(msg)), uintptr(flags)} |
| 292 | n, _, err := Syscall(SYS_SOCKETCALL, netSendMsg, uintptr(unsafe.Pointer(&args)), 0) |
| 293 | if err != 0 { |
| 294 | return 0, err |
| 295 | } |
| 296 | return int(n), nil |
| 297 | } |
| 298 | |
| 299 | func Listen(s int, n int) error { |
| 300 | args := [2]uintptr{uintptr(s), uintptr(n)} |
| 301 | _, _, err := Syscall(SYS_SOCKETCALL, netListen, uintptr(unsafe.Pointer(&args)), 0) |
| 302 | if err != 0 { |
| 303 | return err |
| 304 | } |
| 305 | return nil |
| 306 | } |
| 307 | |
| 308 | func Shutdown(s, how int) error { |
| 309 | args := [2]uintptr{uintptr(s), uintptr(how)} |
| 310 | _, _, err := Syscall(SYS_SOCKETCALL, netShutdown, uintptr(unsafe.Pointer(&args)), 0) |
| 311 | if err != 0 { |
| 312 | return err |
| 313 | } |
| 314 | return nil |
| 315 | } |
| 316 | |
| 317 | //sys poll(fds *PollFd, nfds int, timeout int) (n int, err error) |
| 318 | |
| 319 | func Poll(fds []PollFd, timeout int) (n int, err error) { |
| 320 | if len(fds) == 0 { |
| 321 | return poll(nil, 0, timeout) |
| 322 | } |
| 323 | return poll(&fds[0], len(fds), timeout) |
| 324 | } |
| 325 | |
| 326 | //sys kexecFileLoad(kernelFd int, initrdFd int, cmdlineLen int, cmdline string, flags int) (err error) |
| 327 | |
| 328 | func KexecFileLoad(kernelFd int, initrdFd int, cmdline string, flags int) error { |
| 329 | cmdlineLen := len(cmdline) |
| 330 | if cmdlineLen > 0 { |
| 331 | // Account for the additional NULL byte added by |
| 332 | // BytePtrFromString in kexecFileLoad. The kexec_file_load |
| 333 | // syscall expects a NULL-terminated string. |
| 334 | cmdlineLen++ |
| 335 | } |
| 336 | return kexecFileLoad(kernelFd, initrdFd, cmdlineLen, cmdline, flags) |
| 337 | } |