blob: 85cd97da094901dbffbdab48127e34fb5d2445c9 [file] [log] [blame]
William Kurkianea869482019-04-09 15:16:11 -04001// 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
khenaidoo106c61a2021-08-11 18:05:46 -04005//go:build amd64 && linux
William Kurkianea869482019-04-09 15:16:11 -04006// +build amd64,linux
7
8package unix
9
khenaidoo106c61a2021-08-11 18:05:46 -040010//sys dup2(oldfd int, newfd int) (err error)
William Kurkianea869482019-04-09 15:16:11 -040011//sysnb EpollCreate(size int) (fd int, err error)
12//sys EpollWait(epfd int, events []EpollEvent, msec int) (n int, err error)
13//sys Fadvise(fd int, offset int64, length int64, advice int) (err error) = SYS_FADVISE64
14//sys Fchown(fd int, uid int, gid int) (err error)
15//sys Fstat(fd int, stat *Stat_t) (err error)
16//sys Fstatat(dirfd int, path string, stat *Stat_t, flags int) (err error) = SYS_NEWFSTATAT
17//sys Fstatfs(fd int, buf *Statfs_t) (err error)
18//sys Ftruncate(fd int, length int64) (err error)
19//sysnb Getegid() (egid int)
20//sysnb Geteuid() (euid int)
21//sysnb Getgid() (gid int)
22//sysnb Getrlimit(resource int, rlim *Rlimit) (err error)
23//sysnb Getuid() (uid int)
24//sysnb inotifyInit() (fd int, err error)
25
26func InotifyInit() (fd int, err error) {
27 // First try inotify_init1, because Android's seccomp policy blocks the latter.
28 fd, err = InotifyInit1(0)
29 if err == ENOSYS {
30 fd, err = inotifyInit()
31 }
32 return
33}
34
35//sys Ioperm(from int, num int, on int) (err error)
36//sys Iopl(level int) (err error)
37//sys Lchown(path string, uid int, gid int) (err error)
38//sys Listen(s int, n int) (err error)
39
40func Lstat(path string, stat *Stat_t) (err error) {
41 return Fstatat(AT_FDCWD, path, stat, AT_SYMLINK_NOFOLLOW)
42}
43
44//sys Pause() (err error)
45//sys Pread(fd int, p []byte, offset int64) (n int, err error) = SYS_PREAD64
46//sys Pwrite(fd int, p []byte, offset int64) (n int, err error) = SYS_PWRITE64
47//sys Renameat(olddirfd int, oldpath string, newdirfd int, newpath string) (err error)
48//sys Seek(fd int, offset int64, whence int) (off int64, err error) = SYS_LSEEK
49
50func Select(nfd int, r *FdSet, w *FdSet, e *FdSet, timeout *Timeval) (n int, err error) {
51 var ts *Timespec
52 if timeout != nil {
53 ts = &Timespec{Sec: timeout.Sec, Nsec: timeout.Usec * 1000}
54 }
55 return Pselect(nfd, r, w, e, ts, nil)
56}
57
58//sys sendfile(outfd int, infd int, offset *int64, count int) (written int, err error)
khenaidoo106c61a2021-08-11 18:05:46 -040059//sys setfsgid(gid int) (prev int, err error)
60//sys setfsuid(uid int) (prev int, err error)
William Kurkianea869482019-04-09 15:16:11 -040061//sysnb Setregid(rgid int, egid int) (err error)
62//sysnb Setresgid(rgid int, egid int, sgid int) (err error)
63//sysnb Setresuid(ruid int, euid int, suid int) (err error)
64//sysnb Setrlimit(resource int, rlim *Rlimit) (err error)
65//sysnb Setreuid(ruid int, euid int) (err error)
66//sys Shutdown(fd int, how int) (err error)
67//sys Splice(rfd int, roff *int64, wfd int, woff *int64, len int, flags int) (n int64, err error)
68
69func Stat(path string, stat *Stat_t) (err error) {
70 // Use fstatat, because Android's seccomp policy blocks stat.
71 return Fstatat(AT_FDCWD, path, stat, 0)
72}
73
74//sys Statfs(path string, buf *Statfs_t) (err error)
75//sys SyncFileRange(fd int, off int64, n int64, flags int) (err error)
76//sys Truncate(path string, length int64) (err error)
77//sys Ustat(dev int, ubuf *Ustat_t) (err error)
78//sys accept(s int, rsa *RawSockaddrAny, addrlen *_Socklen) (fd int, err error)
79//sys accept4(s int, rsa *RawSockaddrAny, addrlen *_Socklen, flags int) (fd int, err error)
80//sys bind(s int, addr unsafe.Pointer, addrlen _Socklen) (err error)
81//sys connect(s int, addr unsafe.Pointer, addrlen _Socklen) (err error)
82//sysnb getgroups(n int, list *_Gid_t) (nn int, err error)
83//sysnb setgroups(n int, list *_Gid_t) (err error)
84//sys getsockopt(s int, level int, name int, val unsafe.Pointer, vallen *_Socklen) (err error)
85//sys setsockopt(s int, level int, name int, val unsafe.Pointer, vallen uintptr) (err error)
86//sysnb socket(domain int, typ int, proto int) (fd int, err error)
87//sysnb socketpair(domain int, typ int, proto int, fd *[2]int32) (err error)
88//sysnb getpeername(fd int, rsa *RawSockaddrAny, addrlen *_Socklen) (err error)
89//sysnb getsockname(fd int, rsa *RawSockaddrAny, addrlen *_Socklen) (err error)
90//sys recvfrom(fd int, p []byte, flags int, from *RawSockaddrAny, fromlen *_Socklen) (n int, err error)
91//sys sendto(s int, buf []byte, flags int, to unsafe.Pointer, addrlen _Socklen) (err error)
92//sys recvmsg(s int, msg *Msghdr, flags int) (n int, err error)
93//sys sendmsg(s int, msg *Msghdr, flags int) (n int, err error)
94//sys mmap(addr uintptr, length uintptr, prot int, flags int, fd int, offset int64) (xaddr uintptr, err error)
95
96//sys futimesat(dirfd int, path string, times *[2]Timeval) (err error)
97
98func Gettimeofday(tv *Timeval) (err error) {
99 errno := gettimeofday(tv)
100 if errno != 0 {
101 return errno
102 }
103 return nil
104}
105
106func Time(t *Time_t) (tt Time_t, err error) {
107 var tv Timeval
108 errno := gettimeofday(&tv)
109 if errno != 0 {
110 return 0, errno
111 }
112 if t != nil {
113 *t = Time_t(tv.Sec)
114 }
115 return Time_t(tv.Sec), nil
116}
117
118//sys Utime(path string, buf *Utimbuf) (err error)
119//sys utimes(path string, times *[2]Timeval) (err error)
120
121func setTimespec(sec, nsec int64) Timespec {
122 return Timespec{Sec: sec, Nsec: nsec}
123}
124
125func setTimeval(sec, usec int64) Timeval {
126 return Timeval{Sec: sec, Usec: usec}
127}
128
129//sysnb pipe(p *[2]_C_int) (err error)
130
131func Pipe(p []int) (err error) {
132 if len(p) != 2 {
133 return EINVAL
134 }
135 var pp [2]_C_int
136 err = pipe(&pp)
137 p[0] = int(pp[0])
138 p[1] = int(pp[1])
139 return
140}
141
khenaidoo106c61a2021-08-11 18:05:46 -0400142//sysnb pipe2(p *[2]_C_int, flags int) (err error)
William Kurkianea869482019-04-09 15:16:11 -0400143
144func Pipe2(p []int, flags int) (err error) {
145 if len(p) != 2 {
146 return EINVAL
147 }
148 var pp [2]_C_int
149 err = pipe2(&pp, flags)
150 p[0] = int(pp[0])
151 p[1] = int(pp[1])
152 return
153}
154
155func (r *PtraceRegs) PC() uint64 { return r.Rip }
156
157func (r *PtraceRegs) SetPC(pc uint64) { r.Rip = pc }
158
159func (iov *Iovec) SetLen(length int) {
160 iov.Len = uint64(length)
161}
162
163func (msghdr *Msghdr) SetControllen(length int) {
164 msghdr.Controllen = uint64(length)
165}
166
David Bainbridge788e5202019-10-21 18:49:40 +0000167func (msghdr *Msghdr) SetIovlen(length int) {
168 msghdr.Iovlen = uint64(length)
169}
170
William Kurkianea869482019-04-09 15:16:11 -0400171func (cmsg *Cmsghdr) SetLen(length int) {
172 cmsg.Len = uint64(length)
173}
174
khenaidoo106c61a2021-08-11 18:05:46 -0400175func (rsa *RawSockaddrNFCLLCP) SetServiceNameLen(length int) {
176 rsa.Service_name_len = uint64(length)
177}
178
William Kurkianea869482019-04-09 15:16:11 -0400179//sys poll(fds *PollFd, nfds int, timeout int) (n int, err error)
180
181func Poll(fds []PollFd, timeout int) (n int, err error) {
182 if len(fds) == 0 {
183 return poll(nil, 0, timeout)
184 }
185 return poll(&fds[0], len(fds), timeout)
186}
187
188//sys kexecFileLoad(kernelFd int, initrdFd int, cmdlineLen int, cmdline string, flags int) (err error)
189
190func KexecFileLoad(kernelFd int, initrdFd int, cmdline string, flags int) error {
191 cmdlineLen := len(cmdline)
192 if cmdlineLen > 0 {
193 // Account for the additional NULL byte added by
194 // BytePtrFromString in kexecFileLoad. The kexec_file_load
195 // syscall expects a NULL-terminated string.
196 cmdlineLen++
197 }
198 return kexecFileLoad(kernelFd, initrdFd, cmdlineLen, cmdline, flags)
199}