Scott Baker | 1fe7273 | 2019-10-21 10:58:51 -0700 | [diff] [blame] | 1 | // Copyright 2015 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 arm64,linux |
| 6 | |
| 7 | package unix |
| 8 | |
| 9 | import "unsafe" |
| 10 | |
| 11 | func EpollCreate(size int) (fd int, err error) { |
| 12 | if size <= 0 { |
| 13 | return -1, EINVAL |
| 14 | } |
| 15 | return EpollCreate1(0) |
| 16 | } |
| 17 | |
| 18 | //sys EpollWait(epfd int, events []EpollEvent, msec int) (n int, err error) = SYS_EPOLL_PWAIT |
| 19 | //sys Fadvise(fd int, offset int64, length int64, advice int) (err error) = SYS_FADVISE64 |
| 20 | //sys Fchown(fd int, uid int, gid int) (err error) |
| 21 | //sys Fstat(fd int, stat *Stat_t) (err error) |
| 22 | //sys Fstatat(fd int, path string, stat *Stat_t, flags int) (err error) |
| 23 | //sys Fstatfs(fd int, buf *Statfs_t) (err error) |
| 24 | //sys Ftruncate(fd int, length int64) (err error) |
| 25 | //sysnb Getegid() (egid int) |
| 26 | //sysnb Geteuid() (euid int) |
| 27 | //sysnb Getgid() (gid int) |
| 28 | //sysnb Getrlimit(resource int, rlim *Rlimit) (err error) |
| 29 | //sysnb Getuid() (uid int) |
| 30 | //sys Listen(s int, n int) (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 Renameat(olddirfd int, oldpath string, newdirfd int, newpath string) (err error) |
| 34 | //sys Seek(fd int, offset int64, whence int) (off int64, err error) = SYS_LSEEK |
| 35 | |
| 36 | func Select(nfd int, r *FdSet, w *FdSet, e *FdSet, timeout *Timeval) (n int, err error) { |
| 37 | var ts *Timespec |
| 38 | if timeout != nil { |
| 39 | ts = &Timespec{Sec: timeout.Sec, Nsec: timeout.Usec * 1000} |
| 40 | } |
| 41 | return Pselect(nfd, r, w, e, ts, nil) |
| 42 | } |
| 43 | |
| 44 | //sys sendfile(outfd int, infd int, offset *int64, count int) (written int, err error) |
| 45 | //sys Setfsgid(gid int) (err error) |
| 46 | //sys Setfsuid(uid int) (err error) |
| 47 | //sysnb Setregid(rgid int, egid int) (err error) |
| 48 | //sysnb Setresgid(rgid int, egid int, sgid int) (err error) |
| 49 | //sysnb Setresuid(ruid int, euid int, suid int) (err error) |
| 50 | //sysnb Setrlimit(resource int, rlim *Rlimit) (err error) |
| 51 | //sysnb Setreuid(ruid int, euid int) (err error) |
| 52 | //sys Shutdown(fd int, how int) (err error) |
| 53 | //sys Splice(rfd int, roff *int64, wfd int, woff *int64, len int, flags int) (n int64, 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 | //sys Statfs(path string, buf *Statfs_t) (err error) |
| 68 | //sys SyncFileRange(fd int, off int64, n int64, flags int) (err error) |
| 69 | //sys Truncate(path string, length int64) (err error) |
| 70 | |
| 71 | func Ustat(dev int, ubuf *Ustat_t) (err error) { |
| 72 | return ENOSYS |
| 73 | } |
| 74 | |
| 75 | //sys accept(s int, rsa *RawSockaddrAny, addrlen *_Socklen) (fd int, err error) |
| 76 | //sys accept4(s int, rsa *RawSockaddrAny, addrlen *_Socklen, flags int) (fd int, err error) |
| 77 | //sys bind(s int, addr unsafe.Pointer, addrlen _Socklen) (err error) |
| 78 | //sys connect(s int, addr unsafe.Pointer, addrlen _Socklen) (err error) |
| 79 | //sysnb getgroups(n int, list *_Gid_t) (nn int, err error) |
| 80 | //sysnb setgroups(n int, list *_Gid_t) (err error) |
| 81 | //sys getsockopt(s int, level int, name int, val unsafe.Pointer, vallen *_Socklen) (err error) |
| 82 | //sys setsockopt(s int, level int, name int, val unsafe.Pointer, vallen uintptr) (err error) |
| 83 | //sysnb socket(domain int, typ int, proto int) (fd int, err error) |
| 84 | //sysnb socketpair(domain int, typ int, proto int, fd *[2]int32) (err error) |
| 85 | //sysnb getpeername(fd int, rsa *RawSockaddrAny, addrlen *_Socklen) (err error) |
| 86 | //sysnb getsockname(fd int, rsa *RawSockaddrAny, addrlen *_Socklen) (err error) |
| 87 | //sys recvfrom(fd int, p []byte, flags int, from *RawSockaddrAny, fromlen *_Socklen) (n int, err error) |
| 88 | //sys sendto(s int, buf []byte, flags int, to unsafe.Pointer, addrlen _Socklen) (err error) |
| 89 | //sys recvmsg(s int, msg *Msghdr, flags int) (n int, err error) |
| 90 | //sys sendmsg(s int, msg *Msghdr, flags int) (n int, err error) |
| 91 | //sys mmap(addr uintptr, length uintptr, prot int, flags int, fd int, offset int64) (xaddr uintptr, err error) |
| 92 | |
| 93 | //sysnb Gettimeofday(tv *Timeval) (err error) |
| 94 | |
| 95 | func setTimespec(sec, nsec int64) Timespec { |
| 96 | return Timespec{Sec: sec, Nsec: nsec} |
| 97 | } |
| 98 | |
| 99 | func setTimeval(sec, usec int64) Timeval { |
| 100 | return Timeval{Sec: sec, Usec: usec} |
| 101 | } |
| 102 | |
| 103 | func futimesat(dirfd int, path string, tv *[2]Timeval) (err error) { |
| 104 | if tv == nil { |
| 105 | return utimensat(dirfd, path, nil, 0) |
| 106 | } |
| 107 | |
| 108 | ts := []Timespec{ |
| 109 | NsecToTimespec(TimevalToNsec(tv[0])), |
| 110 | NsecToTimespec(TimevalToNsec(tv[1])), |
| 111 | } |
| 112 | return utimensat(dirfd, path, (*[2]Timespec)(unsafe.Pointer(&ts[0])), 0) |
| 113 | } |
| 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 | func utimes(path string, tv *[2]Timeval) (err error) { |
| 136 | if tv == nil { |
| 137 | return utimensat(AT_FDCWD, path, nil, 0) |
| 138 | } |
| 139 | |
| 140 | ts := []Timespec{ |
| 141 | NsecToTimespec(TimevalToNsec(tv[0])), |
| 142 | NsecToTimespec(TimevalToNsec(tv[1])), |
| 143 | } |
| 144 | return utimensat(AT_FDCWD, path, (*[2]Timespec)(unsafe.Pointer(&ts[0])), 0) |
| 145 | } |
| 146 | |
| 147 | func Pipe(p []int) (err error) { |
| 148 | if len(p) != 2 { |
| 149 | return EINVAL |
| 150 | } |
| 151 | var pp [2]_C_int |
| 152 | err = pipe2(&pp, 0) |
| 153 | p[0] = int(pp[0]) |
| 154 | p[1] = int(pp[1]) |
| 155 | return |
| 156 | } |
| 157 | |
| 158 | //sysnb pipe2(p *[2]_C_int, flags int) (err error) |
| 159 | |
| 160 | func Pipe2(p []int, flags int) (err error) { |
| 161 | if len(p) != 2 { |
| 162 | return EINVAL |
| 163 | } |
| 164 | var pp [2]_C_int |
| 165 | err = pipe2(&pp, flags) |
| 166 | p[0] = int(pp[0]) |
| 167 | p[1] = int(pp[1]) |
| 168 | return |
| 169 | } |
| 170 | |
| 171 | func (r *PtraceRegs) PC() uint64 { return r.Pc } |
| 172 | |
| 173 | func (r *PtraceRegs) SetPC(pc uint64) { r.Pc = pc } |
| 174 | |
| 175 | func (iov *Iovec) SetLen(length int) { |
| 176 | iov.Len = uint64(length) |
| 177 | } |
| 178 | |
| 179 | func (msghdr *Msghdr) SetControllen(length int) { |
| 180 | msghdr.Controllen = uint64(length) |
| 181 | } |
| 182 | |
| 183 | func (cmsg *Cmsghdr) SetLen(length int) { |
| 184 | cmsg.Len = uint64(length) |
| 185 | } |
| 186 | |
| 187 | func InotifyInit() (fd int, err error) { |
| 188 | return InotifyInit1(0) |
| 189 | } |
| 190 | |
| 191 | func Dup2(oldfd int, newfd int) (err error) { |
| 192 | return Dup3(oldfd, newfd, 0) |
| 193 | } |
| 194 | |
| 195 | func Pause() error { |
| 196 | _, err := ppoll(nil, 0, nil, nil) |
| 197 | return err |
| 198 | } |
| 199 | |
| 200 | func Poll(fds []PollFd, timeout int) (n int, err error) { |
| 201 | var ts *Timespec |
| 202 | if timeout >= 0 { |
| 203 | ts = new(Timespec) |
| 204 | *ts = NsecToTimespec(int64(timeout) * 1e6) |
| 205 | } |
| 206 | if len(fds) == 0 { |
| 207 | return ppoll(nil, 0, ts, nil) |
| 208 | } |
| 209 | return ppoll(&fds[0], len(fds), ts, nil) |
| 210 | } |