| 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 |
// GNU/Linux library calls. |
| 6 |
|
| 7 |
package syscall |
| 8 |
|
| 9 |
import ( |
| 10 |
"unsafe" |
| 11 |
) |
| 12 |
|
| 13 |
//sys ptrace(request int, pid int, addr uintptr, data uintptr) (err Errno) |
| 14 |
//__go_ptrace(request _C_int, pid Pid_t, addr *byte, data *byte) _C_long |
| 15 |
|
| 16 |
//sysnb raw_ptrace(request int, pid int, addr uintptr, data uintptr) (err Errno) |
| 17 |
//__go_ptrace(request _C_int, pid Pid_t, addr *byte, data *byte) _C_long |
| 18 |
|
| 19 |
func ptracePeek(req int, pid int, addr uintptr, out []byte) (count int, err error) { |
| 20 |
// The peek requests are machine-size oriented, so we wrap it |
| 21 |
// to retrieve arbitrary-length data. |
| 22 |
|
| 23 |
// The ptrace syscall differs from glibc's ptrace. |
| 24 |
// Peeks returns the word in *data, not as the return value. |
| 25 |
|
| 26 |
var buf [sizeofPtr]byte |
| 27 |
|
| 28 |
// Leading edge. PEEKTEXT/PEEKDATA don't require aligned |
| 29 |
// access (PEEKUSER warns that it might), but if we don't |
| 30 |
// align our reads, we might straddle an unmapped page |
| 31 |
// boundary and not get the bytes leading up to the page |
| 32 |
// boundary. |
| 33 |
n := 0 |
| 34 |
if addr%sizeofPtr != 0 { |
| 35 |
err = ptrace(req, pid, addr-addr%sizeofPtr, uintptr(unsafe.Pointer(&buf[0]))) |
| 36 |
if err != nil { |
| 37 |
return 0, err |
| 38 |
} |
| 39 |
n += copy(out, buf[addr%sizeofPtr:]) |
| 40 |
out = out[n:] |
| 41 |
} |
| 42 |
|
| 43 |
// Remainder. |
| 44 |
for len(out) > 0 { |
| 45 |
// We use an internal buffer to gaurantee alignment. |
| 46 |
// It's not documented if this is necessary, but we're paranoid. |
| 47 |
err = ptrace(req, pid, addr+uintptr(n), uintptr(unsafe.Pointer(&buf[0]))) |
| 48 |
if err != nil { |
| 49 |
return n, err |
| 50 |
} |
| 51 |
copied := copy(out, buf[0:]) |
| 52 |
n += copied |
| 53 |
out = out[copied:] |
| 54 |
} |
| 55 |
|
| 56 |
return n, nil |
| 57 |
} |
| 58 |
|
| 59 |
func PtracePeekText(pid int, addr uintptr, out []byte) (count int, err error) { |
| 60 |
return ptracePeek(PTRACE_PEEKTEXT, pid, addr, out) |
| 61 |
} |
| 62 |
|
| 63 |
func PtracePeekData(pid int, addr uintptr, out []byte) (count int, err error) { |
| 64 |
return ptracePeek(PTRACE_PEEKDATA, pid, addr, out) |
| 65 |
} |
| 66 |
|
| 67 |
func ptracePoke(pokeReq int, peekReq int, pid int, addr uintptr, data []byte) (count int, err error) { |
| 68 |
// As for ptracePeek, we need to align our accesses to deal |
| 69 |
// with the possibility of straddling an invalid page. |
| 70 |
|
| 71 |
// Leading edge. |
| 72 |
n := 0 |
| 73 |
if addr%sizeofPtr != 0 { |
| 74 |
var buf [sizeofPtr]byte |
| 75 |
err = ptrace(peekReq, pid, addr-addr%sizeofPtr, uintptr(unsafe.Pointer(&buf[0]))) |
| 76 |
if err != nil { |
| 77 |
return 0, err |
| 78 |
} |
| 79 |
n += copy(buf[addr%sizeofPtr:], data) |
| 80 |
word := *((*uintptr)(unsafe.Pointer(&buf[0]))) |
| 81 |
err = ptrace(pokeReq, pid, addr-addr%sizeofPtr, word) |
| 82 |
if err != nil { |
| 83 |
return 0, err |
| 84 |
} |
| 85 |
data = data[n:] |
| 86 |
} |
| 87 |
|
| 88 |
// Interior. |
| 89 |
for len(data) > int(sizeofPtr) { |
| 90 |
word := *((*uintptr)(unsafe.Pointer(&data[0]))) |
| 91 |
err = ptrace(pokeReq, pid, addr+uintptr(n), word) |
| 92 |
if err != nil { |
| 93 |
return n, err |
| 94 |
} |
| 95 |
n += int(sizeofPtr) |
| 96 |
data = data[sizeofPtr:] |
| 97 |
} |
| 98 |
|
| 99 |
// Trailing edge. |
| 100 |
if len(data) > 0 { |
| 101 |
var buf [sizeofPtr]byte |
| 102 |
err = ptrace(peekReq, pid, addr+uintptr(n), uintptr(unsafe.Pointer(&buf[0]))) |
| 103 |
if err != nil { |
| 104 |
return n, err |
| 105 |
} |
| 106 |
copy(buf[0:], data) |
| 107 |
word := *((*uintptr)(unsafe.Pointer(&buf[0]))) |
| 108 |
err = ptrace(pokeReq, pid, addr+uintptr(n), word) |
| 109 |
if err != nil { |
| 110 |
return n, err |
| 111 |
} |
| 112 |
n += len(data) |
| 113 |
} |
| 114 |
|
| 115 |
return n, nil |
| 116 |
} |
| 117 |
|
| 118 |
func PtracePokeText(pid int, addr uintptr, data []byte) (count int, err error) { |
| 119 |
return ptracePoke(PTRACE_POKETEXT, PTRACE_PEEKTEXT, pid, addr, data) |
| 120 |
} |
| 121 |
|
| 122 |
func PtracePokeData(pid int, addr uintptr, data []byte) (count int, err error) { |
| 123 |
return ptracePoke(PTRACE_POKEDATA, PTRACE_PEEKDATA, pid, addr, data) |
| 124 |
} |
| 125 |
|
| 126 |
func PtraceSetOptions(pid int, options int) (err error) { |
| 127 |
return ptrace(PTRACE_SETOPTIONS, pid, 0, uintptr(options)) |
| 128 |
} |
| 129 |
|
| 130 |
func PtraceGetEventMsg(pid int) (msg uint, err error) { |
| 131 |
var data _C_long |
| 132 |
err = ptrace(PTRACE_GETEVENTMSG, pid, 0, uintptr(unsafe.Pointer(&data))) |
| 133 |
msg = uint(data) |
| 134 |
return |
| 135 |
} |
| 136 |
|
| 137 |
func PtraceCont(pid int, signal int) (err error) { |
| 138 |
return ptrace(PTRACE_CONT, pid, 0, uintptr(signal)) |
| 139 |
} |
| 140 |
|
| 141 |
func PtraceSingleStep(pid int) (err error) { return ptrace(PTRACE_SINGLESTEP, pid, 0, 0) } |
| 142 |
|
| 143 |
func PtraceAttach(pid int) (err error) { return ptrace(PTRACE_ATTACH, pid, 0, 0) } |
| 144 |
|
| 145 |
func PtraceDetach(pid int) (err error) { return ptrace(PTRACE_DETACH, pid, 0, 0) } |
| 146 |
|
| 147 |
//sys reboot(magic1 uint, magic2 uint, cmd int, arg string) (err error) |
| 148 |
//reboot(magic1 _C_uint, magic2 _C_uint, cmd _C_int, arg *byte) _C_int |
| 149 |
|
| 150 |
func Reboot(cmd int) (err error) { |
| 151 |
return reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, cmd, "") |
| 152 |
} |
| 153 |
|
| 154 |
//sys Acct(path string) (err error) |
| 155 |
//acct(path *byte) _C_int |
| 156 |
|
| 157 |
//sys Adjtimex(buf *Timex) (state int, err error) |
| 158 |
//adjtimex(buf *Timex) _C_int |
| 159 |
|
| 160 |
//sys Fstatfs(fd int, buf *Statfs_t) (err error) |
| 161 |
//fstatfs64(fd _C_int, buf *Statfs_t) _C_int |
| 162 |
|
| 163 |
func Gettid() (tid int) { |
| 164 |
r1, _, _ := Syscall(SYS_GETTID, 0, 0, 0) |
| 165 |
return int(r1) |
| 166 |
} |
| 167 |
|
| 168 |
//sys Getxattr(path string, attr string, dest []byte) (sz int, err error) |
| 169 |
//getxattr(path *byte, attr *byte, buf *byte, count Size_t) Ssize_t |
| 170 |
|
| 171 |
//sys InotifyAddWatch(fd int, pathname string, mask uint32) (watchdesc int, err error) |
| 172 |
//inotify_add_watch(fd _C_int, pathname *byte, mask uint32) _C_int |
| 173 |
|
| 174 |
//sysnb InotifyInit() (fd int, err error) |
| 175 |
//inotify_init() _C_int |
| 176 |
|
| 177 |
//sysnb InotifyInit1(flags int) (fd int, err error) |
| 178 |
//inotify_init1(flags _C_int) _C_int |
| 179 |
|
| 180 |
//sysnb InotifyRmWatch(fd int, watchdesc uint32) (success int, err error) |
| 181 |
//inotify_rm_watch(fd _C_int, wd uint32) _C_int |
| 182 |
|
| 183 |
//sys Klogctl(typ int, buf []byte) (n int, err error) |
| 184 |
//klogctl(typ _C_int, bufp *byte, len _C_int) _C_int |
| 185 |
|
| 186 |
//sys Listxattr(path string, dest []byte) (sz int, err error) |
| 187 |
//listxattr(path *byte, list *byte, size Size_t) Ssize_t |
| 188 |
|
| 189 |
//sys PivotRoot(newroot string, putold string) (err error) |
| 190 |
//pivot_root(newroot *byte, putold *byte) _C_int |
| 191 |
|
| 192 |
// Used by golang.org/x/sys/unix. |
| 193 |
//sys prlimit(pid int, resource int, newlimit *Rlimit, oldlimit *Rlimit) (err error) |
| 194 |
//prlimit(pid Pid_t, resource _C_int, newlimit *Rlimit, oldlimit *Rlimit) _C_int |
| 195 |
|
| 196 |
func Prlimit(pid int, resource int, newlimit *Rlimit, oldlimit *Rlimit) error { |
| 197 |
return prlimit(pid, resource, newlimit, oldlimit) |
| 198 |
} |
| 199 |
|
| 200 |
//sys Removexattr(path string, attr string) (err error) |
| 201 |
//removexattr(path *byte, name *byte) _C_int |
| 202 |
|
| 203 |
//sys Renameat(olddirfd int, oldpath string, newdirfd int, newpath string) (err error) |
| 204 |
//renameat(olddirfd _C_int, oldpath *byte, newdirfd _C_int, newpath *byte) _C_int |
| 205 |
|
| 206 |
//sys Setfsgid(gid int) (err error) |
| 207 |
//setfsgid(gid Gid_t) _C_int |
| 208 |
|
| 209 |
//sys Setfsuid(uid int) (err error) |
| 210 |
//setfsuid(uid Uid_t) _C_int |
| 211 |
|
| 212 |
//sysnb Setresgid(rgid int, egid int, sgid int) (err error) |
| 213 |
//setresgid(rgid Gid_t, egid Gid_t, sgid Gid_t) _C_int |
| 214 |
|
| 215 |
//sysnb Setresuid(ruid int, eguid int, suid int) (err error) |
| 216 |
//setresuid(ruid Uid_t, euid Uid_t, suid Uid_t) _C_int |
| 217 |
|
| 218 |
//sys Setxattr(path string, attr string, data []byte, flags int) (err error) |
| 219 |
//setxattr(path *byte, name *byte, value *byte, size Size_t, flags _C_int) _C_int |
| 220 |
|
| 221 |
//sys splice(rfd int, roff *_libgo_loff_t_type, wfd int, woff *_libgo_loff_t_type, len int, flags int) (n int64, err error) |
| 222 |
//splice(rfd _C_int, roff *_libgo_loff_t_type, wfd _C_int, woff *_libgo_loff_t_type, len Size_t, flags _C_uint) Ssize_t |
| 223 |
|
| 224 |
func Splice(rfd int, roff *int64, wfd int, woff *int64, len int, flags int) (n int64, err error) { |
| 225 |
var lroff _libgo_loff_t_type |
| 226 |
var plroff *_libgo_loff_t_type |
| 227 |
if roff != nil { |
| 228 |
lroff = _libgo_loff_t_type(*roff) |
| 229 |
plroff = &lroff |
| 230 |
} |
| 231 |
var lwoff _libgo_loff_t_type |
| 232 |
var plwoff *_libgo_loff_t_type |
| 233 |
if woff != nil { |
| 234 |
lwoff = _libgo_loff_t_type(*woff) |
| 235 |
plwoff = &lwoff |
| 236 |
} |
| 237 |
n, err = splice(rfd, plroff, wfd, plwoff, len, flags) |
| 238 |
if roff != nil { |
| 239 |
*roff = int64(lroff) |
| 240 |
} |
| 241 |
if woff != nil { |
| 242 |
*woff = int64(lwoff) |
| 243 |
} |
| 244 |
return |
| 245 |
} |
| 246 |
|
| 247 |
//sys Statfs(path string, buf *Statfs_t) (err error) |
| 248 |
//statfs64(path *byte, buf *Statfs_t) _C_int |
| 249 |
|
| 250 |
//sysnb Sysinfo(info *Sysinfo_t) (err error) |
| 251 |
//sysinfo(info *Sysinfo_t) _C_int |
| 252 |
|
| 253 |
//sys Tee(rfd int, wfd int, len int, flags int) (n int64, err error) |
| 254 |
//tee(rfd _C_int, wfd _C_int, len Size_t, flags _C_uint) Ssize_t |
| 255 |
|
| 256 |
func Tgkill(tgid int, tid int, sig Signal) error { |
| 257 |
r1, _, errno := Syscall(SYS_TGKILL, uintptr(tgid), uintptr(tid), uintptr(sig)) |
| 258 |
if r1 < 0 { |
| 259 |
return errno |
| 260 |
} |
| 261 |
return nil |
| 262 |
} |
| 263 |
|
| 264 |
//sys unlinkat(dirfd int, path string, flags int) (err error) |
| 265 |
//unlinkat(dirfd _C_int, path *byte, flags _C_int) _C_int |
| 266 |
|
| 267 |
func Unlinkat(dirfd int, path string) (err error) { |
| 268 |
return unlinkat(dirfd, path, 0) |
| 269 |
} |
| 270 |
|
| 271 |
//sys Unmount(target string, flags int) (err error) = SYS_UMOUNT2 |
| 272 |
//umount2(target *byte, flags _C_int) _C_int |
| 273 |
|
| 274 |
//sys Unshare(flags int) (err error) |
| 275 |
//unshare(flags _C_int) _C_int |