Matteo Scandolo | a6a3aee | 2019-11-26 13:30:14 -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 | // +build arm,linux |
| 6 | |
| 7 | package unix |
| 8 | |
| 9 | import ( |
| 10 | "syscall" |
| 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 | //sysnb pipe(p *[2]_C_int) (err error) |
| 23 | |
| 24 | func Pipe(p []int) (err error) { |
| 25 | if len(p) != 2 { |
| 26 | return EINVAL |
| 27 | } |
| 28 | var pp [2]_C_int |
| 29 | // Try pipe2 first for Android O, then try pipe for kernel 2.6.23. |
| 30 | err = pipe2(&pp, 0) |
| 31 | if err == ENOSYS { |
| 32 | err = pipe(&pp) |
| 33 | } |
| 34 | p[0] = int(pp[0]) |
| 35 | p[1] = int(pp[1]) |
| 36 | return |
| 37 | } |
| 38 | |
| 39 | //sysnb pipe2(p *[2]_C_int, flags int) (err error) |
| 40 | |
| 41 | func Pipe2(p []int, flags int) (err error) { |
| 42 | if len(p) != 2 { |
| 43 | return EINVAL |
| 44 | } |
| 45 | var pp [2]_C_int |
| 46 | err = pipe2(&pp, flags) |
| 47 | p[0] = int(pp[0]) |
| 48 | p[1] = int(pp[1]) |
| 49 | return |
| 50 | } |
| 51 | |
| 52 | // Underlying system call writes to newoffset via pointer. |
| 53 | // Implemented in assembly to avoid allocation. |
| 54 | func seek(fd int, offset int64, whence int) (newoffset int64, err syscall.Errno) |
| 55 | |
| 56 | func Seek(fd int, offset int64, whence int) (newoffset int64, err error) { |
| 57 | newoffset, errno := seek(fd, offset, whence) |
| 58 | if errno != 0 { |
| 59 | return 0, errno |
| 60 | } |
| 61 | return newoffset, nil |
| 62 | } |
| 63 | |
| 64 | //sys accept(s int, rsa *RawSockaddrAny, addrlen *_Socklen) (fd int, err error) |
| 65 | //sys accept4(s int, rsa *RawSockaddrAny, addrlen *_Socklen, flags int) (fd int, err error) |
| 66 | //sys bind(s int, addr unsafe.Pointer, addrlen _Socklen) (err error) |
| 67 | //sys connect(s int, addr unsafe.Pointer, addrlen _Socklen) (err error) |
| 68 | //sysnb getgroups(n int, list *_Gid_t) (nn int, err error) = SYS_GETGROUPS32 |
| 69 | //sysnb setgroups(n int, list *_Gid_t) (err error) = SYS_SETGROUPS32 |
| 70 | //sys getsockopt(s int, level int, name int, val unsafe.Pointer, vallen *_Socklen) (err error) |
| 71 | //sys setsockopt(s int, level int, name int, val unsafe.Pointer, vallen uintptr) (err error) |
| 72 | //sysnb socket(domain int, typ int, proto int) (fd int, err error) |
| 73 | //sysnb getpeername(fd int, rsa *RawSockaddrAny, addrlen *_Socklen) (err error) |
| 74 | //sysnb getsockname(fd int, rsa *RawSockaddrAny, addrlen *_Socklen) (err error) |
| 75 | //sys recvfrom(fd int, p []byte, flags int, from *RawSockaddrAny, fromlen *_Socklen) (n int, err error) |
| 76 | //sys sendto(s int, buf []byte, flags int, to unsafe.Pointer, addrlen _Socklen) (err error) |
| 77 | //sysnb socketpair(domain int, typ int, flags int, fd *[2]int32) (err error) |
| 78 | //sys recvmsg(s int, msg *Msghdr, flags int) (n int, err error) |
| 79 | //sys sendmsg(s int, msg *Msghdr, flags int) (n int, err error) |
| 80 | |
| 81 | // 64-bit file system and 32-bit uid calls |
| 82 | // (16-bit uid calls are not always supported in newer kernels) |
| 83 | //sys Dup2(oldfd int, newfd int) (err error) |
| 84 | //sysnb EpollCreate(size int) (fd int, err error) |
| 85 | //sys EpollWait(epfd int, events []EpollEvent, msec int) (n int, err error) |
| 86 | //sys Fchown(fd int, uid int, gid int) (err error) = SYS_FCHOWN32 |
| 87 | //sys Fstat(fd int, stat *Stat_t) (err error) = SYS_FSTAT64 |
| 88 | //sys Fstatat(dirfd int, path string, stat *Stat_t, flags int) (err error) = SYS_FSTATAT64 |
| 89 | //sysnb Getegid() (egid int) = SYS_GETEGID32 |
| 90 | //sysnb Geteuid() (euid int) = SYS_GETEUID32 |
| 91 | //sysnb Getgid() (gid int) = SYS_GETGID32 |
| 92 | //sysnb Getuid() (uid int) = SYS_GETUID32 |
| 93 | //sysnb InotifyInit() (fd int, err error) |
| 94 | //sys Lchown(path string, uid int, gid int) (err error) = SYS_LCHOWN32 |
| 95 | //sys Listen(s int, n int) (err error) |
| 96 | //sys Lstat(path string, stat *Stat_t) (err error) = SYS_LSTAT64 |
| 97 | //sys Pause() (err error) |
| 98 | //sys Renameat(olddirfd int, oldpath string, newdirfd int, newpath string) (err error) |
| 99 | //sys sendfile(outfd int, infd int, offset *int64, count int) (written int, err error) = SYS_SENDFILE64 |
| 100 | //sys Select(nfd int, r *FdSet, w *FdSet, e *FdSet, timeout *Timeval) (n int, err error) = SYS__NEWSELECT |
Girish Gowdra | 161d27a | 2021-05-05 12:01:44 -0700 | [diff] [blame] | 101 | //sys Setfsgid(gid int) (err error) = SYS_SETFSGID32 |
| 102 | //sys Setfsuid(uid int) (err error) = SYS_SETFSUID32 |
Matteo Scandolo | a6a3aee | 2019-11-26 13:30:14 -0700 | [diff] [blame] | 103 | //sysnb Setregid(rgid int, egid int) (err error) = SYS_SETREGID32 |
| 104 | //sysnb Setresgid(rgid int, egid int, sgid int) (err error) = SYS_SETRESGID32 |
| 105 | //sysnb Setresuid(ruid int, euid int, suid int) (err error) = SYS_SETRESUID32 |
| 106 | //sysnb Setreuid(ruid int, euid int) (err error) = SYS_SETREUID32 |
| 107 | //sys Shutdown(fd int, how int) (err error) |
| 108 | //sys Splice(rfd int, roff *int64, wfd int, woff *int64, len int, flags int) (n int, err error) |
| 109 | //sys Stat(path string, stat *Stat_t) (err error) = SYS_STAT64 |
| 110 | //sys Ustat(dev int, ubuf *Ustat_t) (err error) |
| 111 | |
| 112 | //sys futimesat(dirfd int, path string, times *[2]Timeval) (err error) |
| 113 | //sysnb Gettimeofday(tv *Timeval) (err error) |
| 114 | |
| 115 | func Time(t *Time_t) (Time_t, error) { |
| 116 | var tv Timeval |
| 117 | err := Gettimeofday(&tv) |
| 118 | if err != nil { |
| 119 | return 0, err |
| 120 | } |
| 121 | if t != nil { |
| 122 | *t = Time_t(tv.Sec) |
| 123 | } |
| 124 | return Time_t(tv.Sec), nil |
| 125 | } |
| 126 | |
| 127 | func Utime(path string, buf *Utimbuf) error { |
| 128 | tv := []Timeval{ |
| 129 | {Sec: buf.Actime}, |
| 130 | {Sec: buf.Modtime}, |
| 131 | } |
| 132 | return Utimes(path, tv) |
| 133 | } |
| 134 | |
| 135 | //sys utimes(path string, times *[2]Timeval) (err error) |
| 136 | |
| 137 | //sys Pread(fd int, p []byte, offset int64) (n int, err error) = SYS_PREAD64 |
| 138 | //sys Pwrite(fd int, p []byte, offset int64) (n int, err error) = SYS_PWRITE64 |
| 139 | //sys Truncate(path string, length int64) (err error) = SYS_TRUNCATE64 |
| 140 | //sys Ftruncate(fd int, length int64) (err error) = SYS_FTRUNCATE64 |
| 141 | |
| 142 | func Fadvise(fd int, offset int64, length int64, advice int) (err error) { |
| 143 | _, _, e1 := Syscall6(SYS_ARM_FADVISE64_64, uintptr(fd), uintptr(advice), uintptr(offset), uintptr(offset>>32), uintptr(length), uintptr(length>>32)) |
| 144 | if e1 != 0 { |
| 145 | err = errnoErr(e1) |
| 146 | } |
| 147 | return |
| 148 | } |
| 149 | |
| 150 | //sys mmap2(addr uintptr, length uintptr, prot int, flags int, fd int, pageOffset uintptr) (xaddr uintptr, err error) |
| 151 | |
| 152 | func Fstatfs(fd int, buf *Statfs_t) (err error) { |
| 153 | _, _, e := Syscall(SYS_FSTATFS64, uintptr(fd), unsafe.Sizeof(*buf), uintptr(unsafe.Pointer(buf))) |
| 154 | if e != 0 { |
| 155 | err = e |
| 156 | } |
| 157 | return |
| 158 | } |
| 159 | |
| 160 | func Statfs(path string, buf *Statfs_t) (err error) { |
| 161 | pathp, err := BytePtrFromString(path) |
| 162 | if err != nil { |
| 163 | return err |
| 164 | } |
| 165 | _, _, e := Syscall(SYS_STATFS64, uintptr(unsafe.Pointer(pathp)), unsafe.Sizeof(*buf), uintptr(unsafe.Pointer(buf))) |
| 166 | if e != 0 { |
| 167 | err = e |
| 168 | } |
| 169 | return |
| 170 | } |
| 171 | |
| 172 | func mmap(addr uintptr, length uintptr, prot int, flags int, fd int, offset int64) (xaddr uintptr, err error) { |
| 173 | page := uintptr(offset / 4096) |
| 174 | if offset != int64(page)*4096 { |
| 175 | return 0, EINVAL |
| 176 | } |
| 177 | return mmap2(addr, length, prot, flags, fd, page) |
| 178 | } |
| 179 | |
| 180 | type rlimit32 struct { |
| 181 | Cur uint32 |
| 182 | Max uint32 |
| 183 | } |
| 184 | |
| 185 | //sysnb getrlimit(resource int, rlim *rlimit32) (err error) = SYS_UGETRLIMIT |
| 186 | |
| 187 | const rlimInf32 = ^uint32(0) |
| 188 | const rlimInf64 = ^uint64(0) |
| 189 | |
| 190 | func Getrlimit(resource int, rlim *Rlimit) (err error) { |
| 191 | err = prlimit(0, resource, nil, rlim) |
| 192 | if err != ENOSYS { |
| 193 | return err |
| 194 | } |
| 195 | |
| 196 | rl := rlimit32{} |
| 197 | err = getrlimit(resource, &rl) |
| 198 | if err != nil { |
| 199 | return |
| 200 | } |
| 201 | |
| 202 | if rl.Cur == rlimInf32 { |
| 203 | rlim.Cur = rlimInf64 |
| 204 | } else { |
| 205 | rlim.Cur = uint64(rl.Cur) |
| 206 | } |
| 207 | |
| 208 | if rl.Max == rlimInf32 { |
| 209 | rlim.Max = rlimInf64 |
| 210 | } else { |
| 211 | rlim.Max = uint64(rl.Max) |
| 212 | } |
| 213 | return |
| 214 | } |
| 215 | |
| 216 | //sysnb setrlimit(resource int, rlim *rlimit32) (err error) = SYS_SETRLIMIT |
| 217 | |
| 218 | func Setrlimit(resource int, rlim *Rlimit) (err error) { |
| 219 | err = prlimit(0, resource, rlim, nil) |
| 220 | if err != ENOSYS { |
| 221 | return err |
| 222 | } |
| 223 | |
| 224 | rl := rlimit32{} |
| 225 | if rlim.Cur == rlimInf64 { |
| 226 | rl.Cur = rlimInf32 |
| 227 | } else if rlim.Cur < uint64(rlimInf32) { |
| 228 | rl.Cur = uint32(rlim.Cur) |
| 229 | } else { |
| 230 | return EINVAL |
| 231 | } |
| 232 | if rlim.Max == rlimInf64 { |
| 233 | rl.Max = rlimInf32 |
| 234 | } else if rlim.Max < uint64(rlimInf32) { |
| 235 | rl.Max = uint32(rlim.Max) |
| 236 | } else { |
| 237 | return EINVAL |
| 238 | } |
| 239 | |
| 240 | return setrlimit(resource, &rl) |
| 241 | } |
| 242 | |
| 243 | func (r *PtraceRegs) PC() uint64 { return uint64(r.Uregs[15]) } |
| 244 | |
| 245 | func (r *PtraceRegs) SetPC(pc uint64) { r.Uregs[15] = uint32(pc) } |
| 246 | |
| 247 | func (iov *Iovec) SetLen(length int) { |
| 248 | iov.Len = uint32(length) |
| 249 | } |
| 250 | |
| 251 | func (msghdr *Msghdr) SetControllen(length int) { |
| 252 | msghdr.Controllen = uint32(length) |
| 253 | } |
| 254 | |
| 255 | func (cmsg *Cmsghdr) SetLen(length int) { |
| 256 | cmsg.Len = uint32(length) |
| 257 | } |
| 258 | |
| 259 | //sys poll(fds *PollFd, nfds int, timeout int) (n int, err error) |
| 260 | |
| 261 | func Poll(fds []PollFd, timeout int) (n int, err error) { |
| 262 | if len(fds) == 0 { |
| 263 | return poll(nil, 0, timeout) |
| 264 | } |
| 265 | return poll(&fds[0], len(fds), timeout) |
| 266 | } |
| 267 | |
| 268 | //sys armSyncFileRange(fd int, flags int, off int64, n int64) (err error) = SYS_ARM_SYNC_FILE_RANGE |
| 269 | |
| 270 | func SyncFileRange(fd int, off int64, n int64, flags int) error { |
| 271 | // The sync_file_range and arm_sync_file_range syscalls differ only in the |
| 272 | // order of their arguments. |
| 273 | return armSyncFileRange(fd, flags, off, n) |
| 274 | } |