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.
5 // GNU/Linux library calls.
11 //sys Openat(dirfd int, path string, flags int, mode uint32) (fd int, err error)
12 //__go_openat(dirfd _C_int, path *byte, flags _C_int, mode Mode_t) _C_int
14 //sys futimesat(dirfd int, path *byte, times *[2]Timeval) (err error)
15 //futimesat(dirfd _C_int, path *byte, times *[2]Timeval) _C_int
16 func Futimesat(dirfd
int, path
string, tv
[]Timeval
) (err error
) {
20 return futimesat(dirfd
, StringBytePtr(path
), (*[2]Timeval
)(unsafe
.Pointer(&tv
[0])))
23 func Futimes(fd
int, tv
[]Timeval
) (err error
) {
24 // Believe it or not, this is the best we can do on GNU/Linux
25 // (and is what glibc does).
26 return Utimes("/proc/self/fd/"+itoa(fd
), tv
)
29 //sys ptrace(request int, pid int, addr uintptr, data uintptr) (err error)
30 //ptrace(request _C_int, pid Pid_t, addr *byte, data *byte) _C_long
32 //sysnb raw_ptrace(request int, pid int, addr *byte, data *byte) (err Errno)
33 //ptrace(request _C_int, pid Pid_t, addr *byte, data *byte) _C_long
35 func ptracePeek(req
int, pid
int, addr
uintptr, out
[]byte) (count
int, err error
) {
36 // The peek requests are machine-size oriented, so we wrap it
37 // to retrieve arbitrary-length data.
39 // The ptrace syscall differs from glibc's ptrace.
40 // Peeks returns the word in *data, not as the return value.
42 var buf
[sizeofPtr
]byte
44 // Leading edge. PEEKTEXT/PEEKDATA don't require aligned
45 // access (PEEKUSER warns that it might), but if we don't
46 // align our reads, we might straddle an unmapped page
47 // boundary and not get the bytes leading up to the page
50 if addr%sizeofPtr
!= 0 {
51 err
= ptrace(req
, pid
, addr
-addr%sizeofPtr
, uintptr(unsafe
.Pointer(&buf
[0])))
55 n
+= copy(out
, buf
[addr%sizeofPtr
:])
61 // We use an internal buffer to gaurantee alignment.
62 // It's not documented if this is necessary, but we're paranoid.
63 err
= ptrace(req
, pid
, addr
+uintptr(n
), uintptr(unsafe
.Pointer(&buf
[0])))
67 copied
:= copy(out
, buf
[0:])
75 func PtracePeekText(pid
int, addr
uintptr, out
[]byte) (count
int, err error
) {
76 return ptracePeek(PTRACE_PEEKTEXT
, pid
, addr
, out
)
79 func PtracePeekData(pid
int, addr
uintptr, out
[]byte) (count
int, err error
) {
80 return ptracePeek(PTRACE_PEEKDATA
, pid
, addr
, out
)
83 func ptracePoke(pokeReq
int, peekReq
int, pid
int, addr
uintptr, data
[]byte) (count
int, err error
) {
84 // As for ptracePeek, we need to align our accesses to deal
85 // with the possibility of straddling an invalid page.
89 if addr%sizeofPtr
!= 0 {
90 var buf
[sizeofPtr
]byte
91 err
= ptrace(peekReq
, pid
, addr
-addr%sizeofPtr
, uintptr(unsafe
.Pointer(&buf
[0])))
95 n
+= copy(buf
[addr%sizeofPtr
:], data
)
96 word
:= *((*uintptr)(unsafe
.Pointer(&buf
[0])))
97 err
= ptrace(pokeReq
, pid
, addr
-addr%sizeofPtr
, word
)
105 for len(data
) > int(sizeofPtr
) {
106 word
:= *((*uintptr)(unsafe
.Pointer(&data
[0])))
107 err
= ptrace(pokeReq
, pid
, addr
+uintptr(n
), word
)
112 data
= data
[sizeofPtr
:]
117 var buf
[sizeofPtr
]byte
118 err
= ptrace(peekReq
, pid
, addr
+uintptr(n
), uintptr(unsafe
.Pointer(&buf
[0])))
123 word
:= *((*uintptr)(unsafe
.Pointer(&buf
[0])))
124 err
= ptrace(pokeReq
, pid
, addr
+uintptr(n
), word
)
134 func PtracePokeText(pid
int, addr
uintptr, data
[]byte) (count
int, err error
) {
135 return ptracePoke(PTRACE_POKETEXT
, PTRACE_PEEKTEXT
, pid
, addr
, data
)
138 func PtracePokeData(pid
int, addr
uintptr, data
[]byte) (count
int, err error
) {
139 return ptracePoke(PTRACE_POKEDATA
, PTRACE_PEEKDATA
, pid
, addr
, data
)
142 func PtraceSetOptions(pid
int, options
int) (err error
) {
143 return ptrace(PTRACE_SETOPTIONS
, pid
, 0, uintptr(options
))
146 func PtraceGetEventMsg(pid
int) (msg
uint, err error
) {
148 err
= ptrace(PTRACE_GETEVENTMSG
, pid
, 0, uintptr(unsafe
.Pointer(&data
)))
153 func PtraceCont(pid
int, signal
int) (err error
) {
154 return ptrace(PTRACE_CONT
, pid
, 0, uintptr(signal
))
157 func PtraceSingleStep(pid
int) (err error
) { return ptrace(PTRACE_SINGLESTEP
, pid
, 0, 0) }
159 func PtraceAttach(pid
int) (err error
) { return ptrace(PTRACE_ATTACH
, pid
, 0, 0) }
161 func PtraceDetach(pid
int) (err error
) { return ptrace(PTRACE_DETACH
, pid
, 0, 0) }
163 //sys reboot(magic1 uint, magic2 uint, cmd int, arg string) (err error)
164 //reboot(magic1 _C_uint, magic2 _C_uint, cmd _C_int, arg *byte) _C_int
165 func Reboot(cmd
int) (err error
) {
166 return reboot(LINUX_REBOOT_MAGIC1
, LINUX_REBOOT_MAGIC2
, cmd
, "")
169 //sys accept4(fd int, sa *RawSockaddrAny, len *Socklen_t, flags int) (nfd int, err error)
170 //accept4(fd _C_int, sa *RawSockaddrAny, len *Socklen_t, flags _C_int) _C_int
172 func Accept4(fd
int, flags
int) (nfd
int, sa Sockaddr
, err error
) {
173 var rsa RawSockaddrAny
174 var len Socklen_t
= SizeofSockaddrAny
175 nfd
, err
= accept4(fd
, &rsa
, &len, flags
)
179 sa
, err
= anyToSockaddr(&rsa
)
187 //sys Acct(path string) (err error)
188 //acct(path *byte) _C_int
190 //sys Adjtimex(buf *Timex) (state int, err error)
191 //adjtimex(buf *Timex) _C_int
193 //sysnb Dup3(oldfd int, newfd int, flags int) (err error)
194 //dup3(oldfd _C_int, newfd _C_int, flags _C_int) _C_int
196 //sys Faccessat(dirfd int, path string, mode uint32, flags int) (err error)
197 //faccessat(dirfd _C_int, pathname *byte, mode _C_int, flags _C_int) _C_int
199 //sys Fallocate(fd int, mode uint32, off int64, len int64) (err error)
200 //fallocate(fd _C_int, mode _C_int, offset Offset_t, len Offset_t) _C_int
202 //sys Fchmodat(dirfd int, path string, mode uint32, flags int) (err error)
203 //fchmodat(dirfd _C_int, pathname *byte, mode Mode_t, flags _C_int) _C_int
205 //sys Fchownat(dirfd int, path string, uid int, gid int, flags int) (err error)
206 //fchownat(dirfd _C_int, path *byte, owner Uid_t, group Gid_t, flags _C_int) _C_int
208 //sys Flock(fd int, how int) (err error)
209 //flock(fd _C_int, how _C_int) _C_int
211 //sys Fstatfs(fd int, buf *Statfs_t) (err error)
212 //fstatfs(fd _C_int, buf *Statfs_t) _C_int
214 func Gettid() (tid
int) {
215 r1
, _
, _
:= Syscall(SYS_GETTID
, 0, 0, 0)
219 func Getdents(fd
int, buf
[]byte) (n
int, err error
) {
224 p
= (*byte)(unsafe
.Pointer(&_zero
))
231 r1
, _
, errno
:= Syscall(uintptr(s
), uintptr(fd
), uintptr(unsafe
.Pointer(p
)), uintptr(len(buf
)))
240 func clen(n
[]byte) int {
241 for i
:= 0; i
< len(n
); i
++ {
249 func ReadDirent(fd
int, buf
[]byte) (n
int, err error
) {
250 return Getdents(fd
, buf
)
253 func ParseDirent(buf
[]byte, max
int, names
[]string) (consumed
int, count
int, newnames
[]string) {
256 for max
!= 0 && len(buf
) > 0 {
257 dirent
:= (*Dirent
)(unsafe
.Pointer(&buf
[0]))
258 buf
= buf
[dirent
.Reclen
:]
259 if dirent
.Ino
== 0 { // File absent in directory.
262 bytes
:= (*[10000]byte)(unsafe
.Pointer(&dirent
.Name
[0]))
263 var name
= string(bytes
[0:clen(bytes
[:])])
264 if name
== "." || name
== ".." { // Useless names
269 names
= append(names
, name
)
271 return origlen
- len(buf
), count
, names
274 //sys Getxattr(path string, attr string, dest []byte) (sz int, err error)
275 //getxattr(path *byte, attr *byte, buf *byte, count Size_t) Ssize_t
277 //sys InotifyAddWatch(fd int, pathname string, mask uint32) (watchdesc int, err error)
278 //inotify_add_watch(fd _C_int, pathname *byte, mask uint32) _C_int
280 //sysnb InotifyInit() (fd int, err error)
281 //inotify_init() _C_int
283 //sysnb InotifyInit1(flags int) (fd int, err error)
284 //inotify_init1(flags _C_int) _C_int
286 //sysnb InotifyRmWatch(fd int, watchdesc uint32) (success int, err error)
287 //inotify_rm_watch(fd _C_int, wd uint32) _C_int
289 //sys Klogctl(typ int, buf []byte) (n int, err error)
290 //klogctl(typ _C_int, bufp *byte, len _C_int) _C_int
292 //sys Listxattr(path string, dest []byte) (sz int, err error)
293 //listxattr(path *byte, list *byte, size Size_t) Ssize_t
295 //sys Mkdirat(dirfd int, path string, mode uint32) (err error)
296 //mkdirat(dirfd _C_int, path *byte, mode Mode_t) _C_int
298 //sys Mknodat(dirfd int, path string, mode uint32, dev int) (err error)
299 //mknodat(dirfd _C_int, path *byte, mode Mode_t, dev _dev_t) _C_int
301 //sysnb pipe2(p *[2]_C_int, flags int) (err error)
302 //pipe2(p *[2]_C_int, flags _C_int) _C_int
303 func Pipe2(p
[]int, flags
int) (err error
) {
308 err
= pipe2(&pp
, flags
)
314 //sys PivotRoot(newroot string, putold string) (err error)
315 //pivot_root(newroot *byte, putold *byte) _C_int
317 //sys Removexattr(path string, attr string) (err error)
318 //removexattr(path *byte, name *byte) _C_int
320 //sys Renameat(olddirfd int, oldpath string, newdirfd int, newpath string) (err error)
321 //renameat(olddirfd _C_int, oldpath *byte, newdirfd _C_int, newpath *byte) _C_int
323 //sys sendfile(outfd int, infd int, offset *Offset_t, count int) (written int, err error)
324 //sendfile64(outfd _C_int, infd _C_int, offset *Offset_t, count Size_t) Ssize_t
325 func Sendfile(outfd
int, infd
int, offset
*int64, count
int) (written
int, err error
) {
327 raceReleaseMerge(unsafe
.Pointer(&ioSync
))
334 written
, err
= sendfile(outfd
, infd
, psoff
, count
)
336 *offset
= int64(soff
)
341 //sys Setfsgid(gid int) (err error)
342 //setfsgid(gid Gid_t) _C_int
344 //sys Setfsuid(uid int) (err error)
345 //setfsuid(uid Uid_t) _C_int
347 //sysnb Setresgid(rgid int, egid int, sgid int) (err error)
348 //setresgid(rgid Gid_t, egid Gid_t, sgid Gid_t) _C_int
350 //sysnb Setresuid(ruid int, eguid int, suid int) (err error)
351 //setresuid(ruid Uid_t, euid Uid_t, suid Uid_t) _C_int
353 //sys Setxattr(path string, attr string, data []byte, flags int) (err error)
354 //setxattr(path *byte, name *byte, value *byte, size Size_t, flags _C_int) _C_int
356 //sys splice(rfd int, roff *_loff_t, wfd int, woff *_loff_t, len int, flags int) (n int64, err error)
357 //splice(rfd _C_int, roff *_loff_t, wfd _C_int, woff *_loff_t, len Size_t, flags _C_uint) Ssize_t
358 func Splice(rfd
int, roff
*int64, wfd
int, woff
*int64, len int, flags
int) (n
int64, err error
) {
362 lroff
= _loff_t(*roff
)
368 lwoff
= _loff_t(*woff
)
371 n
, err
= splice(rfd
, plroff
, wfd
, plwoff
, len, flags
)
381 //sys Statfs(path string, buf *Statfs_t) (err error)
382 //statfs(path *byte, buf *Statfs_t) _C_int
384 //sys SyncFileRange(fd int, off int64, n int64, flags int) (err error)
385 //sync_file_range(fd _C_int, off Offset_t, n Offset_t, flags _C_uint) _C_int
387 //sysnb Sysinfo(info *Sysinfo_t) (err error)
388 //sysinfo(info *Sysinfo_t) _C_int
390 //sys Tee(rfd int, wfd int, len int, flags int) (n int64, err error)
391 //tee(rfd _C_int, wfd _C_int, len Size_t, flags _C_uint) Ssize_t
393 func Tgkill(tgid
int, tid
int, sig Signal
) error
{
394 r1
, _
, errno
:= Syscall(SYS_TGKILL
, uintptr(tgid
), uintptr(tid
), uintptr(sig
))
401 //sys unlinkat(dirfd int, path string, flags int) (err error)
402 //unlinkat(dirfd _C_int, path *byte, flags _C_int) _C_int
404 func Unlinkat(dirfd
int, path
string) (err error
) {
405 return unlinkat(dirfd
, path
, 0)
408 //sys Unmount(target string, flags int) (err error) = SYS_UMOUNT2
409 //umount2(target *byte, flags _C_int) _C_int
411 //sys Unshare(flags int) (err error)
412 //unshare(flags _C_int) _C_int
414 //sys Ustat(dev int, ubuf *Ustat_t) (err error)
415 //ustat(dev _dev_t, ubuf *Ustat_t) _C_int