| 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 |
// Linux system calls. |
| 6 |
// This file is compiled as ordinary Go code, |
| 7 |
// but it is also input to mksyscall, |
| 8 |
// which parses the //sys lines and generates system call stubs. |
| 9 |
// Note that sometimes we use a lowercase //sys name and |
| 10 |
// wrap it in our own nicer implementation. |
| 11 |
|
| 12 |
package unix |
| 13 |
|
| 14 |
import ( |
| 15 |
"syscall" |
| 16 |
"unsafe" |
| 17 |
) |
| 18 |
|
| 19 |
/* |
| 20 |
* Wrapped |
| 21 |
*/ |
| 22 |
|
| 23 |
func Access(path string, mode uint32) (err error) { |
| 24 |
return Faccessat(AT_FDCWD, path, mode, 0) |
| 25 |
} |
| 26 |
|
| 27 |
func Chmod(path string, mode uint32) (err error) { |
| 28 |
return Fchmodat(AT_FDCWD, path, mode, 0) |
| 29 |
} |
| 30 |
|
| 31 |
func Chown(path string, uid int, gid int) (err error) { |
| 32 |
return Fchownat(AT_FDCWD, path, uid, gid, 0) |
| 33 |
} |
| 34 |
|
| 35 |
func Creat(path string, mode uint32) (fd int, err error) { |
| 36 |
return Open(path, O_CREAT|O_WRONLY|O_TRUNC, mode) |
| 37 |
} |
| 38 |
|
| 39 |
//sys fchmodat(dirfd int, path string, mode uint32) (err error) |
| 40 |
|
| 41 |
func Fchmodat(dirfd int, path string, mode uint32, flags int) (err error) { |
| 42 |
// Linux fchmodat doesn't support the flags parameter. Mimick glibc's behavior |
| 43 |
// and check the flags. Otherwise the mode would be applied to the symlink |
| 44 |
// destination which is not what the user expects. |
| 45 |
if flags&^AT_SYMLINK_NOFOLLOW != 0 { |
| 46 |
return EINVAL |
| 47 |
} else if flags&AT_SYMLINK_NOFOLLOW != 0 { |
| 48 |
return EOPNOTSUPP |
| 49 |
} |
| 50 |
return fchmodat(dirfd, path, mode) |
| 51 |
} |
| 52 |
|
| 53 |
//sys ioctl(fd int, req uint, arg uintptr) (err error) |
| 54 |
|
| 55 |
// ioctl itself should not be exposed directly, but additional get/set |
| 56 |
// functions for specific types are permissible. |
| 57 |
|
| 58 |
// IoctlSetInt performs an ioctl operation which sets an integer value |
| 59 |
// on fd, using the specified request number. |
| 60 |
func IoctlSetInt(fd int, req uint, value int) error { |
| 61 |
return ioctl(fd, req, uintptr(value)) |
| 62 |
} |
| 63 |
|
| 64 |
func ioctlSetWinsize(fd int, req uint, value *Winsize) error { |
| 65 |
return ioctl(fd, req, uintptr(unsafe.Pointer(value))) |
| 66 |
} |
| 67 |
|
| 68 |
func ioctlSetTermios(fd int, req uint, value *Termios) error { |
| 69 |
return ioctl(fd, req, uintptr(unsafe.Pointer(value))) |
| 70 |
} |
| 71 |
|
| 72 |
// IoctlGetInt performs an ioctl operation which gets an integer value |
| 73 |
// from fd, using the specified request number. |
| 74 |
func IoctlGetInt(fd int, req uint) (int, error) { |
| 75 |
var value int |
| 76 |
err := ioctl(fd, req, uintptr(unsafe.Pointer(&value))) |
| 77 |
return value, err |
| 78 |
} |
| 79 |
|
| 80 |
func IoctlGetWinsize(fd int, req uint) (*Winsize, error) { |
| 81 |
var value Winsize |
| 82 |
err := ioctl(fd, req, uintptr(unsafe.Pointer(&value))) |
| 83 |
return &value, err |
| 84 |
} |
| 85 |
|
| 86 |
func IoctlGetTermios(fd int, req uint) (*Termios, error) { |
| 87 |
var value Termios |
| 88 |
err := ioctl(fd, req, uintptr(unsafe.Pointer(&value))) |
| 89 |
return &value, err |
| 90 |
} |
| 91 |
|
| 92 |
//sys Linkat(olddirfd int, oldpath string, newdirfd int, newpath string, flags int) (err error) |
| 93 |
|
| 94 |
func Link(oldpath string, newpath string) (err error) { |
| 95 |
return Linkat(AT_FDCWD, oldpath, AT_FDCWD, newpath, 0) |
| 96 |
} |
| 97 |
|
| 98 |
func Mkdir(path string, mode uint32) (err error) { |
| 99 |
return Mkdirat(AT_FDCWD, path, mode) |
| 100 |
} |
| 101 |
|
| 102 |
func Mknod(path string, mode uint32, dev int) (err error) { |
| 103 |
return Mknodat(AT_FDCWD, path, mode, dev) |
| 104 |
} |
| 105 |
|
| 106 |
func Open(path string, mode int, perm uint32) (fd int, err error) { |
| 107 |
return openat(AT_FDCWD, path, mode|O_LARGEFILE, perm) |
| 108 |
} |
| 109 |
|
| 110 |
//sys openat(dirfd int, path string, flags int, mode uint32) (fd int, err error) |
| 111 |
|
| 112 |
func Openat(dirfd int, path string, flags int, mode uint32) (fd int, err error) { |
| 113 |
return openat(dirfd, path, flags|O_LARGEFILE, mode) |
| 114 |
} |
| 115 |
|
| 116 |
//sys ppoll(fds *PollFd, nfds int, timeout *Timespec, sigmask *Sigset_t) (n int, err error) |
| 117 |
|
| 118 |
func Ppoll(fds []PollFd, timeout *Timespec, sigmask *Sigset_t) (n int, err error) { |
| 119 |
if len(fds) == 0 { |
| 120 |
return ppoll(nil, 0, timeout, sigmask) |
| 121 |
} |
| 122 |
return ppoll(&fds[0], len(fds), timeout, sigmask) |
| 123 |
} |
| 124 |
|
| 125 |
//sys Readlinkat(dirfd int, path string, buf []byte) (n int, err error) |
| 126 |
|
| 127 |
func Readlink(path string, buf []byte) (n int, err error) { |
| 128 |
return Readlinkat(AT_FDCWD, path, buf) |
| 129 |
} |
| 130 |
|
| 131 |
func Rename(oldpath string, newpath string) (err error) { |
| 132 |
return Renameat(AT_FDCWD, oldpath, AT_FDCWD, newpath) |
| 133 |
} |
| 134 |
|
| 135 |
func Rmdir(path string) error { |
| 136 |
return Unlinkat(AT_FDCWD, path, AT_REMOVEDIR) |
| 137 |
} |
| 138 |
|
| 139 |
//sys Symlinkat(oldpath string, newdirfd int, newpath string) (err error) |
| 140 |
|
| 141 |
func Symlink(oldpath string, newpath string) (err error) { |
| 142 |
return Symlinkat(oldpath, AT_FDCWD, newpath) |
| 143 |
} |
| 144 |
|
| 145 |
func Unlink(path string) error { |
| 146 |
return Unlinkat(AT_FDCWD, path, 0) |
| 147 |
} |
| 148 |
|
| 149 |
//sys Unlinkat(dirfd int, path string, flags int) (err error) |
| 150 |
|
| 151 |
func Utimes(path string, tv []Timeval) error { |
| 152 |
if tv == nil { |
| 153 |
err := utimensat(AT_FDCWD, path, nil, 0) |
| 154 |
if err != ENOSYS { |
| 155 |
return err |
| 156 |
} |
| 157 |
return utimes(path, nil) |
| 158 |
} |
| 159 |
if len(tv) != 2 { |
| 160 |
return EINVAL |
| 161 |
} |
| 162 |
var ts [2]Timespec |
| 163 |
ts[0] = NsecToTimespec(TimevalToNsec(tv[0])) |
| 164 |
ts[1] = NsecToTimespec(TimevalToNsec(tv[1])) |
| 165 |
err := utimensat(AT_FDCWD, path, (*[2]Timespec)(unsafe.Pointer(&ts[0])), 0) |
| 166 |
if err != ENOSYS { |
| 167 |
return err |
| 168 |
} |
| 169 |
return utimes(path, (*[2]Timeval)(unsafe.Pointer(&tv[0]))) |
| 170 |
} |
| 171 |
|
| 172 |
//sys utimensat(dirfd int, path string, times *[2]Timespec, flags int) (err error) |
| 173 |
|
| 174 |
func UtimesNano(path string, ts []Timespec) error { |
| 175 |
if ts == nil { |
| 176 |
err := utimensat(AT_FDCWD, path, nil, 0) |
| 177 |
if err != ENOSYS { |
| 178 |
return err |
| 179 |
} |
| 180 |
return utimes(path, nil) |
| 181 |
} |
| 182 |
if len(ts) != 2 { |
| 183 |
return EINVAL |
| 184 |
} |
| 185 |
err := utimensat(AT_FDCWD, path, (*[2]Timespec)(unsafe.Pointer(&ts[0])), 0) |
| 186 |
if err != ENOSYS { |
| 187 |
return err |
| 188 |
} |
| 189 |
// If the utimensat syscall isn't available (utimensat was added to Linux |
| 190 |
// in 2.6.22, Released, 8 July 2007) then fall back to utimes |
| 191 |
var tv [2]Timeval |
| 192 |
for i := 0; i < 2; i++ { |
| 193 |
tv[i] = NsecToTimeval(TimespecToNsec(ts[i])) |
| 194 |
} |
| 195 |
return utimes(path, (*[2]Timeval)(unsafe.Pointer(&tv[0]))) |
| 196 |
} |
| 197 |
|
| 198 |
func UtimesNanoAt(dirfd int, path string, ts []Timespec, flags int) error { |
| 199 |
if ts == nil { |
| 200 |
return utimensat(dirfd, path, nil, flags) |
| 201 |
} |
| 202 |
if len(ts) != 2 { |
| 203 |
return EINVAL |
| 204 |
} |
| 205 |
return utimensat(dirfd, path, (*[2]Timespec)(unsafe.Pointer(&ts[0])), flags) |
| 206 |
} |
| 207 |
|
| 208 |
func Futimesat(dirfd int, path string, tv []Timeval) error { |
| 209 |
if tv == nil { |
| 210 |
return futimesat(dirfd, path, nil) |
| 211 |
} |
| 212 |
if len(tv) != 2 { |
| 213 |
return EINVAL |
| 214 |
} |
| 215 |
return futimesat(dirfd, path, (*[2]Timeval)(unsafe.Pointer(&tv[0]))) |
| 216 |
} |
| 217 |
|
| 218 |
func Futimes(fd int, tv []Timeval) (err error) { |
| 219 |
// Believe it or not, this is the best we can do on Linux |
| 220 |
// (and is what glibc does). |
| 221 |
return Utimes("/proc/self/fd/"+itoa(fd), tv) |
| 222 |
} |
| 223 |
|
| 224 |
const ImplementsGetwd = true |
| 225 |
|
| 226 |
//sys Getcwd(buf []byte) (n int, err error) |
| 227 |
|
| 228 |
func Getwd() (wd string, err error) { |
| 229 |
var buf [PathMax]byte |
| 230 |
n, err := Getcwd(buf[0:]) |
| 231 |
if err != nil { |
| 232 |
return "", err |
| 233 |
} |
| 234 |
// Getcwd returns the number of bytes written to buf, including the NUL. |
| 235 |
if n < 1 || n > len(buf) || buf[n-1] != 0 { |
| 236 |
return "", EINVAL |
| 237 |
} |
| 238 |
return string(buf[0 : n-1]), nil |
| 239 |
} |
| 240 |
|
| 241 |
func Getgroups() (gids []int, err error) { |
| 242 |
n, err := getgroups(0, nil) |
| 243 |
if err != nil { |
| 244 |
return nil, err |
| 245 |
} |
| 246 |
if n == 0 { |
| 247 |
return nil, nil |
| 248 |
} |
| 249 |
|
| 250 |
// Sanity check group count. Max is 1<<16 on Linux. |
| 251 |
if n < 0 || n > 1<<20 { |
| 252 |
return nil, EINVAL |
| 253 |
} |
| 254 |
|
| 255 |
a := make([]_Gid_t, n) |
| 256 |
n, err = getgroups(n, &a[0]) |
| 257 |
if err != nil { |
| 258 |
return nil, err |
| 259 |
} |
| 260 |
gids = make([]int, n) |
| 261 |
for i, v := range a[0:n] { |
| 262 |
gids[i] = int(v) |
| 263 |
} |
| 264 |
return |
| 265 |
} |
| 266 |
|
| 267 |
func Setgroups(gids []int) (err error) { |
| 268 |
if len(gids) == 0 { |
| 269 |
return setgroups(0, nil) |
| 270 |
} |
| 271 |
|
| 272 |
a := make([]_Gid_t, len(gids)) |
| 273 |
for i, v := range gids { |
| 274 |
a[i] = _Gid_t(v) |
| 275 |
} |
| 276 |
return setgroups(len(a), &a[0]) |
| 277 |
} |
| 278 |
|
| 279 |
type WaitStatus uint32 |
| 280 |
|
| 281 |
// Wait status is 7 bits at bottom, either 0 (exited), |
| 282 |
// 0x7F (stopped), or a signal number that caused an exit. |
| 283 |
// The 0x80 bit is whether there was a core dump. |
| 284 |
// An extra number (exit code, signal causing a stop) |
| 285 |
// is in the high bits. At least that's the idea. |
| 286 |
// There are various irregularities. For example, the |
| 287 |
// "continued" status is 0xFFFF, distinguishing itself |
| 288 |
// from stopped via the core dump bit. |
| 289 |
|
| 290 |
const ( |
| 291 |
mask = 0x7F |
| 292 |
core = 0x80 |
| 293 |
exited = 0x00 |
| 294 |
stopped = 0x7F |
| 295 |
shift = 8 |
| 296 |
) |
| 297 |
|
| 298 |
func (w WaitStatus) Exited() bool { return w&mask == exited } |
| 299 |
|
| 300 |
func (w WaitStatus) Signaled() bool { return w&mask != stopped && w&mask != exited } |
| 301 |
|
| 302 |
func (w WaitStatus) Stopped() bool { return w&0xFF == stopped } |
| 303 |
|
| 304 |
func (w WaitStatus) Continued() bool { return w == 0xFFFF } |
| 305 |
|
| 306 |
func (w WaitStatus) CoreDump() bool { return w.Signaled() && w&core != 0 } |
| 307 |
|
| 308 |
func (w WaitStatus) ExitStatus() int { |
| 309 |
if !w.Exited() { |
| 310 |
return -1 |
| 311 |
} |
| 312 |
return int(w>>shift) & 0xFF |
| 313 |
} |
| 314 |
|
| 315 |
func (w WaitStatus) Signal() syscall.Signal { |
| 316 |
if !w.Signaled() { |
| 317 |
return -1 |
| 318 |
} |
| 319 |
return syscall.Signal(w & mask) |
| 320 |
} |
| 321 |
|
| 322 |
func (w WaitStatus) StopSignal() syscall.Signal { |
| 323 |
if !w.Stopped() { |
| 324 |
return -1 |
| 325 |
} |
| 326 |
return syscall.Signal(w>>shift) & 0xFF |
| 327 |
} |
| 328 |
|
| 329 |
func (w WaitStatus) TrapCause() int { |
| 330 |
if w.StopSignal() != SIGTRAP { |
| 331 |
return -1 |
| 332 |
} |
| 333 |
return int(w>>shift) >> 8 |
| 334 |
} |
| 335 |
|
| 336 |
//sys wait4(pid int, wstatus *_C_int, options int, rusage *Rusage) (wpid int, err error) |
| 337 |
|
| 338 |
func Wait4(pid int, wstatus *WaitStatus, options int, rusage *Rusage) (wpid int, err error) { |
| 339 |
var status _C_int |
| 340 |
wpid, err = wait4(pid, &status, options, rusage) |
| 341 |
if wstatus != nil { |
| 342 |
*wstatus = WaitStatus(status) |
| 343 |
} |
| 344 |
return |
| 345 |
} |
| 346 |
|
| 347 |
func Mkfifo(path string, mode uint32) error { |
| 348 |
return Mknod(path, mode|S_IFIFO, 0) |
| 349 |
} |
| 350 |
|
| 351 |
func Mkfifoat(dirfd int, path string, mode uint32) error { |
| 352 |
return Mknodat(dirfd, path, mode|S_IFIFO, 0) |
| 353 |
} |
| 354 |
|
| 355 |
func (sa *SockaddrInet4) sockaddr() (unsafe.Pointer, _Socklen, error) { |
| 356 |
if sa.Port < 0 || sa.Port > 0xFFFF { |
| 357 |
return nil, 0, EINVAL |
| 358 |
} |
| 359 |
sa.raw.Family = AF_INET |
| 360 |
p := (*[2]byte)(unsafe.Pointer(&sa.raw.Port)) |
| 361 |
p[0] = byte(sa.Port >> 8) |
| 362 |
p[1] = byte(sa.Port) |
| 363 |
for i := 0; i < len(sa.Addr); i++ { |
| 364 |
sa.raw.Addr[i] = sa.Addr[i] |
| 365 |
} |
| 366 |
return unsafe.Pointer(&sa.raw), SizeofSockaddrInet4, nil |
| 367 |
} |
| 368 |
|
| 369 |
func (sa *SockaddrInet6) sockaddr() (unsafe.Pointer, _Socklen, error) { |
| 370 |
if sa.Port < 0 || sa.Port > 0xFFFF { |
| 371 |
return nil, 0, EINVAL |
| 372 |
} |
| 373 |
sa.raw.Family = AF_INET6 |
| 374 |
p := (*[2]byte)(unsafe.Pointer(&sa.raw.Port)) |
| 375 |
p[0] = byte(sa.Port >> 8) |
| 376 |
p[1] = byte(sa.Port) |
| 377 |
sa.raw.Scope_id = sa.ZoneId |
| 378 |
for i := 0; i < len(sa.Addr); i++ { |
| 379 |
sa.raw.Addr[i] = sa.Addr[i] |
| 380 |
} |
| 381 |
return unsafe.Pointer(&sa.raw), SizeofSockaddrInet6, nil |
| 382 |
} |
| 383 |
|
| 384 |
func (sa *SockaddrUnix) sockaddr() (unsafe.Pointer, _Socklen, error) { |
| 385 |
name := sa.Name |
| 386 |
n := len(name) |
| 387 |
if n >= len(sa.raw.Path) { |
| 388 |
return nil, 0, EINVAL |
| 389 |
} |
| 390 |
sa.raw.Family = AF_UNIX |
| 391 |
for i := 0; i < n; i++ { |
| 392 |
sa.raw.Path[i] = int8(name[i]) |
| 393 |
} |
| 394 |
// length is family (uint16), name, NUL. |
| 395 |
sl := _Socklen(2) |
| 396 |
if n > 0 { |
| 397 |
sl += _Socklen(n) + 1 |
| 398 |
} |
| 399 |
if sa.raw.Path[0] == '@' { |
| 400 |
sa.raw.Path[0] = 0 |
| 401 |
// Don't count trailing NUL for abstract address. |
| 402 |
sl-- |
| 403 |
} |
| 404 |
|
| 405 |
return unsafe.Pointer(&sa.raw), sl, nil |
| 406 |
} |
| 407 |
|
| 408 |
// SockaddrLinklayer implements the Sockaddr interface for AF_PACKET type sockets. |
| 409 |
type SockaddrLinklayer struct { |
| 410 |
Protocol uint16 |
| 411 |
Ifindex int |
| 412 |
Hatype uint16 |
| 413 |
Pkttype uint8 |
| 414 |
Halen uint8 |
| 415 |
Addr [8]byte |
| 416 |
raw RawSockaddrLinklayer |
| 417 |
} |
| 418 |
|
| 419 |
func (sa *SockaddrLinklayer) sockaddr() (unsafe.Pointer, _Socklen, error) { |
| 420 |
if sa.Ifindex < 0 || sa.Ifindex > 0x7fffffff { |
| 421 |
return nil, 0, EINVAL |
| 422 |
} |
| 423 |
sa.raw.Family = AF_PACKET |
| 424 |
sa.raw.Protocol = sa.Protocol |
| 425 |
sa.raw.Ifindex = int32(sa.Ifindex) |
| 426 |
sa.raw.Hatype = sa.Hatype |
| 427 |
sa.raw.Pkttype = sa.Pkttype |
| 428 |
sa.raw.Halen = sa.Halen |
| 429 |
for i := 0; i < len(sa.Addr); i++ { |
| 430 |
sa.raw.Addr[i] = sa.Addr[i] |
| 431 |
} |
| 432 |
return unsafe.Pointer(&sa.raw), SizeofSockaddrLinklayer, nil |
| 433 |
} |
| 434 |
|
| 435 |
// SockaddrNetlink implements the Sockaddr interface for AF_NETLINK type sockets. |
| 436 |
type SockaddrNetlink struct { |
| 437 |
Family uint16 |
| 438 |
Pad uint16 |
| 439 |
Pid uint32 |
| 440 |
Groups uint32 |
| 441 |
raw RawSockaddrNetlink |
| 442 |
} |
| 443 |
|
| 444 |
func (sa *SockaddrNetlink) sockaddr() (unsafe.Pointer, _Socklen, error) { |
| 445 |
sa.raw.Family = AF_NETLINK |
| 446 |
sa.raw.Pad = sa.Pad |
| 447 |
sa.raw.Pid = sa.Pid |
| 448 |
sa.raw.Groups = sa.Groups |
| 449 |
return unsafe.Pointer(&sa.raw), SizeofSockaddrNetlink, nil |
| 450 |
} |
| 451 |
|
| 452 |
// SockaddrHCI implements the Sockaddr interface for AF_BLUETOOTH type sockets |
| 453 |
// using the HCI protocol. |
| 454 |
type SockaddrHCI struct { |
| 455 |
Dev uint16 |
| 456 |
Channel uint16 |
| 457 |
raw RawSockaddrHCI |
| 458 |
} |
| 459 |
|
| 460 |
func (sa *SockaddrHCI) sockaddr() (unsafe.Pointer, _Socklen, error) { |
| 461 |
sa.raw.Family = AF_BLUETOOTH |
| 462 |
sa.raw.Dev = sa.Dev |
| 463 |
sa.raw.Channel = sa.Channel |
| 464 |
return unsafe.Pointer(&sa.raw), SizeofSockaddrHCI, nil |
| 465 |
} |
| 466 |
|
| 467 |
// SockaddrL2 implements the Sockaddr interface for AF_BLUETOOTH type sockets |
| 468 |
// using the L2CAP protocol. |
| 469 |
type SockaddrL2 struct { |
| 470 |
PSM uint16 |
| 471 |
CID uint16 |
| 472 |
Addr [6]uint8 |
| 473 |
AddrType uint8 |
| 474 |
raw RawSockaddrL2 |
| 475 |
} |
| 476 |
|
| 477 |
func (sa *SockaddrL2) sockaddr() (unsafe.Pointer, _Socklen, error) { |
| 478 |
sa.raw.Family = AF_BLUETOOTH |
| 479 |
psm := (*[2]byte)(unsafe.Pointer(&sa.raw.Psm)) |
| 480 |
psm[0] = byte(sa.PSM) |
| 481 |
psm[1] = byte(sa.PSM >> 8) |
| 482 |
for i := 0; i < len(sa.Addr); i++ { |
| 483 |
sa.raw.Bdaddr[i] = sa.Addr[len(sa.Addr)-1-i] |
| 484 |
} |
| 485 |
cid := (*[2]byte)(unsafe.Pointer(&sa.raw.Cid)) |
| 486 |
cid[0] = byte(sa.CID) |
| 487 |
cid[1] = byte(sa.CID >> 8) |
| 488 |
sa.raw.Bdaddr_type = sa.AddrType |
| 489 |
return unsafe.Pointer(&sa.raw), SizeofSockaddrL2, nil |
| 490 |
} |
| 491 |
|
| 492 |
// SockaddrRFCOMM implements the Sockaddr interface for AF_BLUETOOTH type sockets |
| 493 |
// using the RFCOMM protocol. |
| 494 |
// |
| 495 |
// Server example: |
| 496 |
// |
| 497 |
// fd, _ := Socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM) |
| 498 |
// _ = unix.Bind(fd, &unix.SockaddrRFCOMM{ |
| 499 |
// Channel: 1, |
| 500 |
// Addr: [6]uint8{0, 0, 0, 0, 0, 0}, // BDADDR_ANY or 00:00:00:00:00:00 |
| 501 |
// }) |
| 502 |
// _ = Listen(fd, 1) |
| 503 |
// nfd, sa, _ := Accept(fd) |
| 504 |
// fmt.Printf("conn addr=%v fd=%d", sa.(*unix.SockaddrRFCOMM).Addr, nfd) |
| 505 |
// Read(nfd, buf) |
| 506 |
// |
| 507 |
// Client example: |
| 508 |
// |
| 509 |
// fd, _ := Socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM) |
| 510 |
// _ = Connect(fd, &SockaddrRFCOMM{ |
| 511 |
// Channel: 1, |
| 512 |
// Addr: [6]byte{0x11, 0x22, 0x33, 0xaa, 0xbb, 0xcc}, // CC:BB:AA:33:22:11 |
| 513 |
// }) |
| 514 |
// Write(fd, []byte(`hello`)) |
| 515 |
type SockaddrRFCOMM struct { |
| 516 |
// Addr represents a bluetooth address, byte ordering is little-endian. |
| 517 |
Addr [6]uint8 |
| 518 |
|
| 519 |
// Channel is a designated bluetooth channel, only 1-30 are available for use. |
| 520 |
// Since Linux 2.6.7 and further zero value is the first available channel. |
| 521 |
Channel uint8 |
| 522 |
|
| 523 |
raw RawSockaddrRFCOMM |
| 524 |
} |
| 525 |
|
| 526 |
func (sa *SockaddrRFCOMM) sockaddr() (unsafe.Pointer, _Socklen, error) { |
| 527 |
sa.raw.Family = AF_BLUETOOTH |
| 528 |
sa.raw.Channel = sa.Channel |
| 529 |
sa.raw.Bdaddr = sa.Addr |
| 530 |
return unsafe.Pointer(&sa.raw), SizeofSockaddrRFCOMM, nil |
| 531 |
} |
| 532 |
|
| 533 |
// SockaddrCAN implements the Sockaddr interface for AF_CAN type sockets. |
| 534 |
// The RxID and TxID fields are used for transport protocol addressing in |
| 535 |
// (CAN_TP16, CAN_TP20, CAN_MCNET, and CAN_ISOTP), they can be left with |
| 536 |
// zero values for CAN_RAW and CAN_BCM sockets as they have no meaning. |
| 537 |
// |
| 538 |
// The SockaddrCAN struct must be bound to the socket file descriptor |
| 539 |
// using Bind before the CAN socket can be used. |
| 540 |
// |
| 541 |
// // Read one raw CAN frame |
| 542 |
// fd, _ := Socket(AF_CAN, SOCK_RAW, CAN_RAW) |
| 543 |
// addr := &SockaddrCAN{Ifindex: index} |
| 544 |
// Bind(fd, addr) |
| 545 |
// frame := make([]byte, 16) |
| 546 |
// Read(fd, frame) |
| 547 |
// |
| 548 |
// The full SocketCAN documentation can be found in the linux kernel |
| 549 |
// archives at: https://www.kernel.org/doc/Documentation/networking/can.txt |
| 550 |
type SockaddrCAN struct { |
| 551 |
Ifindex int |
| 552 |
RxID uint32 |
| 553 |
TxID uint32 |
| 554 |
raw RawSockaddrCAN |
| 555 |
} |
| 556 |
|
| 557 |
func (sa *SockaddrCAN) sockaddr() (unsafe.Pointer, _Socklen, error) { |
| 558 |
if sa.Ifindex < 0 || sa.Ifindex > 0x7fffffff { |
| 559 |
return nil, 0, EINVAL |
| 560 |
} |
| 561 |
sa.raw.Family = AF_CAN |
| 562 |
sa.raw.Ifindex = int32(sa.Ifindex) |
| 563 |
rx := (*[4]byte)(unsafe.Pointer(&sa.RxID)) |
| 564 |
for i := 0; i < 4; i++ { |
| 565 |
sa.raw.Addr[i] = rx[i] |
| 566 |
} |
| 567 |
tx := (*[4]byte)(unsafe.Pointer(&sa.TxID)) |
| 568 |
for i := 0; i < 4; i++ { |
| 569 |
sa.raw.Addr[i+4] = tx[i] |
| 570 |
} |
| 571 |
return unsafe.Pointer(&sa.raw), SizeofSockaddrCAN, nil |
| 572 |
} |
| 573 |
|
| 574 |
// SockaddrALG implements the Sockaddr interface for AF_ALG type sockets. |
| 575 |
// SockaddrALG enables userspace access to the Linux kernel's cryptography |
| 576 |
// subsystem. The Type and Name fields specify which type of hash or cipher |
| 577 |
// should be used with a given socket. |
| 578 |
// |
| 579 |
// To create a file descriptor that provides access to a hash or cipher, both |
| 580 |
// Bind and Accept must be used. Once the setup process is complete, input |
| 581 |
// data can be written to the socket, processed by the kernel, and then read |
| 582 |
// back as hash output or ciphertext. |
| 583 |
// |
| 584 |
// Here is an example of using an AF_ALG socket with SHA1 hashing. |
| 585 |
// The initial socket setup process is as follows: |
| 586 |
// |
| 587 |
// // Open a socket to perform SHA1 hashing. |
| 588 |
// fd, _ := unix.Socket(unix.AF_ALG, unix.SOCK_SEQPACKET, 0) |
| 589 |
// addr := &unix.SockaddrALG{Type: "hash", Name: "sha1"} |
| 590 |
// unix.Bind(fd, addr) |
| 591 |
// // Note: unix.Accept does not work at this time; must invoke accept() |
| 592 |
// // manually using unix.Syscall. |
| 593 |
// hashfd, _, _ := unix.Syscall(unix.SYS_ACCEPT, uintptr(fd), 0, 0) |
| 594 |
// |
| 595 |
// Once a file descriptor has been returned from Accept, it may be used to |
| 596 |
// perform SHA1 hashing. The descriptor is not safe for concurrent use, but |
| 597 |
// may be re-used repeatedly with subsequent Write and Read operations. |
| 598 |
// |
| 599 |
// When hashing a small byte slice or string, a single Write and Read may |
| 600 |
// be used: |
| 601 |
// |
| 602 |
// // Assume hashfd is already configured using the setup process. |
| 603 |
// hash := os.NewFile(hashfd, "sha1") |
| 604 |
// // Hash an input string and read the results. Each Write discards |
| 605 |
// // previous hash state. Read always reads the current state. |
| 606 |
// b := make([]byte, 20) |
| 607 |
// for i := 0; i < 2; i++ { |
| 608 |
// io.WriteString(hash, "Hello, world.") |
| 609 |
// hash.Read(b) |
| 610 |
// fmt.Println(hex.EncodeToString(b)) |
| 611 |
// } |
| 612 |
// // Output: |
| 613 |
// // 2ae01472317d1935a84797ec1983ae243fc6aa28 |
| 614 |
// // 2ae01472317d1935a84797ec1983ae243fc6aa28 |
| 615 |
// |
| 616 |
// For hashing larger byte slices, or byte streams such as those read from |
| 617 |
// a file or socket, use Sendto with MSG_MORE to instruct the kernel to update |
| 618 |
// the hash digest instead of creating a new one for a given chunk and finalizing it. |
| 619 |
// |
| 620 |
// // Assume hashfd and addr are already configured using the setup process. |
| 621 |
// hash := os.NewFile(hashfd, "sha1") |
| 622 |
// // Hash the contents of a file. |
| 623 |
// f, _ := os.Open("/tmp/linux-4.10-rc7.tar.xz") |
| 624 |
// b := make([]byte, 4096) |
| 625 |
// for { |
| 626 |
// n, err := f.Read(b) |
| 627 |
// if err == io.EOF { |
| 628 |
// break |
| 629 |
// } |
| 630 |
// unix.Sendto(hashfd, b[:n], unix.MSG_MORE, addr) |
| 631 |
// } |
| 632 |
// hash.Read(b) |
| 633 |
// fmt.Println(hex.EncodeToString(b)) |
| 634 |
// // Output: 85cdcad0c06eef66f805ecce353bec9accbeecc5 |
| 635 |
// |
| 636 |
// For more information, see: http://www.chronox.de/crypto-API/crypto/userspace-if.html. |
| 637 |
type SockaddrALG struct { |
| 638 |
Type string |
| 639 |
Name string |
| 640 |
Feature uint32 |
| 641 |
Mask uint32 |
| 642 |
raw RawSockaddrALG |
| 643 |
} |
| 644 |
|
| 645 |
func (sa *SockaddrALG) sockaddr() (unsafe.Pointer, _Socklen, error) { |
| 646 |
// Leave room for NUL byte terminator. |
| 647 |
if len(sa.Type) > 13 { |
| 648 |
return nil, 0, EINVAL |
| 649 |
} |
| 650 |
if len(sa.Name) > 63 { |
| 651 |
return nil, 0, EINVAL |
| 652 |
} |
| 653 |
|
| 654 |
sa.raw.Family = AF_ALG |
| 655 |
sa.raw.Feat = sa.Feature |
| 656 |
sa.raw.Mask = sa.Mask |
| 657 |
|
| 658 |
typ, err := ByteSliceFromString(sa.Type) |
| 659 |
if err != nil { |
| 660 |
return nil, 0, err |
| 661 |
} |
| 662 |
name, err := ByteSliceFromString(sa.Name) |
| 663 |
if err != nil { |
| 664 |
return nil, 0, err |
| 665 |
} |
| 666 |
|
| 667 |
copy(sa.raw.Type[:], typ) |
| 668 |
copy(sa.raw.Name[:], name) |
| 669 |
|
| 670 |
return unsafe.Pointer(&sa.raw), SizeofSockaddrALG, nil |
| 671 |
} |
| 672 |
|
| 673 |
// SockaddrVM implements the Sockaddr interface for AF_VSOCK type sockets. |
| 674 |
// SockaddrVM provides access to Linux VM sockets: a mechanism that enables |
| 675 |
// bidirectional communication between a hypervisor and its guest virtual |
| 676 |
// machines. |
| 677 |
type SockaddrVM struct { |
| 678 |
// CID and Port specify a context ID and port address for a VM socket. |
| 679 |
// Guests have a unique CID, and hosts may have a well-known CID of: |
| 680 |
// - VMADDR_CID_HYPERVISOR: refers to the hypervisor process. |
| 681 |
// - VMADDR_CID_HOST: refers to other processes on the host. |
| 682 |
CID uint32 |
| 683 |
Port uint32 |
| 684 |
raw RawSockaddrVM |
| 685 |
} |
| 686 |
|
| 687 |
func (sa *SockaddrVM) sockaddr() (unsafe.Pointer, _Socklen, error) { |
| 688 |
sa.raw.Family = AF_VSOCK |
| 689 |
sa.raw.Port = sa.Port |
| 690 |
sa.raw.Cid = sa.CID |
| 691 |
|
| 692 |
return unsafe.Pointer(&sa.raw), SizeofSockaddrVM, nil |
| 693 |
} |
| 694 |
|
| 695 |
func anyToSockaddr(fd int, rsa *RawSockaddrAny) (Sockaddr, error) { |
| 696 |
switch rsa.Addr.Family { |
| 697 |
case AF_NETLINK: |
| 698 |
pp := (*RawSockaddrNetlink)(unsafe.Pointer(rsa)) |
| 699 |
sa := new(SockaddrNetlink) |
| 700 |
sa.Family = pp.Family |
| 701 |
sa.Pad = pp.Pad |
| 702 |
sa.Pid = pp.Pid |
| 703 |
sa.Groups = pp.Groups |
| 704 |
return sa, nil |
| 705 |
|
| 706 |
case AF_PACKET: |
| 707 |
pp := (*RawSockaddrLinklayer)(unsafe.Pointer(rsa)) |
| 708 |
sa := new(SockaddrLinklayer) |
| 709 |
sa.Protocol = pp.Protocol |
| 710 |
sa.Ifindex = int(pp.Ifindex) |
| 711 |
sa.Hatype = pp.Hatype |
| 712 |
sa.Pkttype = pp.Pkttype |
| 713 |
sa.Halen = pp.Halen |
| 714 |
for i := 0; i < len(sa.Addr); i++ { |
| 715 |
sa.Addr[i] = pp.Addr[i] |
| 716 |
} |
| 717 |
return sa, nil |
| 718 |
|
| 719 |
case AF_UNIX: |
| 720 |
pp := (*RawSockaddrUnix)(unsafe.Pointer(rsa)) |
| 721 |
sa := new(SockaddrUnix) |
| 722 |
if pp.Path[0] == 0 { |
| 723 |
// "Abstract" Unix domain socket. |
| 724 |
// Rewrite leading NUL as @ for textual display. |
| 725 |
// (This is the standard convention.) |
| 726 |
// Not friendly to overwrite in place, |
| 727 |
// but the callers below don't care. |
| 728 |
pp.Path[0] = '@' |
| 729 |
} |
| 730 |
|
| 731 |
// Assume path ends at NUL. |
| 732 |
// This is not technically the Linux semantics for |
| 733 |
// abstract Unix domain sockets--they are supposed |
| 734 |
// to be uninterpreted fixed-size binary blobs--but |
| 735 |
// everyone uses this convention. |
| 736 |
n := 0 |
| 737 |
for n < len(pp.Path) && pp.Path[n] != 0 { |
| 738 |
n++ |
| 739 |
} |
| 740 |
bytes := (*[10000]byte)(unsafe.Pointer(&pp.Path[0]))[0:n] |
| 741 |
sa.Name = string(bytes) |
| 742 |
return sa, nil |
| 743 |
|
| 744 |
case AF_INET: |
| 745 |
pp := (*RawSockaddrInet4)(unsafe.Pointer(rsa)) |
| 746 |
sa := new(SockaddrInet4) |
| 747 |
p := (*[2]byte)(unsafe.Pointer(&pp.Port)) |
| 748 |
sa.Port = int(p[0])<<8 + int(p[1]) |
| 749 |
for i := 0; i < len(sa.Addr); i++ { |
| 750 |
sa.Addr[i] = pp.Addr[i] |
| 751 |
} |
| 752 |
return sa, nil |
| 753 |
|
| 754 |
case AF_INET6: |
| 755 |
pp := (*RawSockaddrInet6)(unsafe.Pointer(rsa)) |
| 756 |
sa := new(SockaddrInet6) |
| 757 |
p := (*[2]byte)(unsafe.Pointer(&pp.Port)) |
| 758 |
sa.Port = int(p[0])<<8 + int(p[1]) |
| 759 |
sa.ZoneId = pp.Scope_id |
| 760 |
for i := 0; i < len(sa.Addr); i++ { |
| 761 |
sa.Addr[i] = pp.Addr[i] |
| 762 |
} |
| 763 |
return sa, nil |
| 764 |
|
| 765 |
case AF_VSOCK: |
| 766 |
pp := (*RawSockaddrVM)(unsafe.Pointer(rsa)) |
| 767 |
sa := &SockaddrVM{ |
| 768 |
CID: pp.Cid, |
| 769 |
Port: pp.Port, |
| 770 |
} |
| 771 |
return sa, nil |
| 772 |
case AF_BLUETOOTH: |
| 773 |
proto, err := GetsockoptInt(fd, SOL_SOCKET, SO_PROTOCOL) |
| 774 |
if err != nil { |
| 775 |
return nil, err |
| 776 |
} |
| 777 |
// only BTPROTO_L2CAP and BTPROTO_RFCOMM can accept connections |
| 778 |
switch proto { |
| 779 |
case BTPROTO_L2CAP: |
| 780 |
pp := (*RawSockaddrL2)(unsafe.Pointer(rsa)) |
| 781 |
sa := &SockaddrL2{ |
| 782 |
PSM: pp.Psm, |
| 783 |
CID: pp.Cid, |
| 784 |
Addr: pp.Bdaddr, |
| 785 |
AddrType: pp.Bdaddr_type, |
| 786 |
} |
| 787 |
return sa, nil |
| 788 |
case BTPROTO_RFCOMM: |
| 789 |
pp := (*RawSockaddrRFCOMM)(unsafe.Pointer(rsa)) |
| 790 |
sa := &SockaddrRFCOMM{ |
| 791 |
Channel: pp.Channel, |
| 792 |
Addr: pp.Bdaddr, |
| 793 |
} |
| 794 |
return sa, nil |
| 795 |
} |
| 796 |
} |
| 797 |
return nil, EAFNOSUPPORT |
| 798 |
} |
| 799 |
|
| 800 |
func Accept(fd int) (nfd int, sa Sockaddr, err error) { |
| 801 |
var rsa RawSockaddrAny |
| 802 |
var len _Socklen = SizeofSockaddrAny |
| 803 |
nfd, err = accept(fd, &rsa, &len) |
| 804 |
if err != nil { |
| 805 |
return |
| 806 |
} |
| 807 |
sa, err = anyToSockaddr(fd, &rsa) |
| 808 |
if err != nil { |
| 809 |
Close(nfd) |
| 810 |
nfd = 0 |
| 811 |
} |
| 812 |
return |
| 813 |
} |
| 814 |
|
| 815 |
func Accept4(fd int, flags int) (nfd int, sa Sockaddr, err error) { |
| 816 |
var rsa RawSockaddrAny |
| 817 |
var len _Socklen = SizeofSockaddrAny |
| 818 |
nfd, err = accept4(fd, &rsa, &len, flags) |
| 819 |
if err != nil { |
| 820 |
return |
| 821 |
} |
| 822 |
if len > SizeofSockaddrAny { |
| 823 |
panic("RawSockaddrAny too small") |
| 824 |
} |
| 825 |
sa, err = anyToSockaddr(fd, &rsa) |
| 826 |
if err != nil { |
| 827 |
Close(nfd) |
| 828 |
nfd = 0 |
| 829 |
} |
| 830 |
return |
| 831 |
} |
| 832 |
|
| 833 |
func Getsockname(fd int) (sa Sockaddr, err error) { |
| 834 |
var rsa RawSockaddrAny |
| 835 |
var len _Socklen = SizeofSockaddrAny |
| 836 |
if err = getsockname(fd, &rsa, &len); err != nil { |
| 837 |
return |
| 838 |
} |
| 839 |
return anyToSockaddr(fd, &rsa) |
| 840 |
} |
| 841 |
|
| 842 |
func GetsockoptIPMreqn(fd, level, opt int) (*IPMreqn, error) { |
| 843 |
var value IPMreqn |
| 844 |
vallen := _Socklen(SizeofIPMreqn) |
| 845 |
err := getsockopt(fd, level, opt, unsafe.Pointer(&value), &vallen) |
| 846 |
return &value, err |
| 847 |
} |
| 848 |
|
| 849 |
func GetsockoptUcred(fd, level, opt int) (*Ucred, error) { |
| 850 |
var value Ucred |
| 851 |
vallen := _Socklen(SizeofUcred) |
| 852 |
err := getsockopt(fd, level, opt, unsafe.Pointer(&value), &vallen) |
| 853 |
return &value, err |
| 854 |
} |
| 855 |
|
| 856 |
func GetsockoptTCPInfo(fd, level, opt int) (*TCPInfo, error) { |
| 857 |
var value TCPInfo |
| 858 |
vallen := _Socklen(SizeofTCPInfo) |
| 859 |
err := getsockopt(fd, level, opt, unsafe.Pointer(&value), &vallen) |
| 860 |
return &value, err |
| 861 |
} |
| 862 |
|
| 863 |
// GetsockoptString returns the string value of the socket option opt for the |
| 864 |
// socket associated with fd at the given socket level. |
| 865 |
func GetsockoptString(fd, level, opt int) (string, error) { |
| 866 |
buf := make([]byte, 256) |
| 867 |
vallen := _Socklen(len(buf)) |
| 868 |
err := getsockopt(fd, level, opt, unsafe.Pointer(&buf[0]), &vallen) |
| 869 |
if err != nil { |
| 870 |
if err == ERANGE { |
| 871 |
buf = make([]byte, vallen) |
| 872 |
err = getsockopt(fd, level, opt, unsafe.Pointer(&buf[0]), &vallen) |
| 873 |
} |
| 874 |
if err != nil { |
| 875 |
return "", err |
| 876 |
} |
| 877 |
} |
| 878 |
return string(buf[:vallen-1]), nil |
| 879 |
} |
| 880 |
|
| 881 |
func SetsockoptIPMreqn(fd, level, opt int, mreq *IPMreqn) (err error) { |
| 882 |
return setsockopt(fd, level, opt, unsafe.Pointer(mreq), unsafe.Sizeof(*mreq)) |
| 883 |
} |
| 884 |
|
| 885 |
// Keyctl Commands (http://man7.org/linux/man-pages/man2/keyctl.2.html) |
| 886 |
|
| 887 |
// KeyctlInt calls keyctl commands in which each argument is an int. |
| 888 |
// These commands are KEYCTL_REVOKE, KEYCTL_CHOWN, KEYCTL_CLEAR, KEYCTL_LINK, |
| 889 |
// KEYCTL_UNLINK, KEYCTL_NEGATE, KEYCTL_SET_REQKEY_KEYRING, KEYCTL_SET_TIMEOUT, |
| 890 |
// KEYCTL_ASSUME_AUTHORITY, KEYCTL_SESSION_TO_PARENT, KEYCTL_REJECT, |
| 891 |
// KEYCTL_INVALIDATE, and KEYCTL_GET_PERSISTENT. |
| 892 |
//sys KeyctlInt(cmd int, arg2 int, arg3 int, arg4 int, arg5 int) (ret int, err error) = SYS_KEYCTL |
| 893 |
|
| 894 |
// KeyctlBuffer calls keyctl commands in which the third and fourth |
| 895 |
// arguments are a buffer and its length, respectively. |
| 896 |
// These commands are KEYCTL_UPDATE, KEYCTL_READ, and KEYCTL_INSTANTIATE. |
| 897 |
//sys KeyctlBuffer(cmd int, arg2 int, buf []byte, arg5 int) (ret int, err error) = SYS_KEYCTL |
| 898 |
|
| 899 |
// KeyctlString calls keyctl commands which return a string. |
| 900 |
// These commands are KEYCTL_DESCRIBE and KEYCTL_GET_SECURITY. |
| 901 |
func KeyctlString(cmd int, id int) (string, error) { |
| 902 |
// We must loop as the string data may change in between the syscalls. |
| 903 |
// We could allocate a large buffer here to reduce the chance that the |
| 904 |
// syscall needs to be called twice; however, this is unnecessary as |
| 905 |
// the performance loss is negligible. |
| 906 |
var buffer []byte |
| 907 |
for { |
| 908 |
// Try to fill the buffer with data |
| 909 |
length, err := KeyctlBuffer(cmd, id, buffer, 0) |
| 910 |
if err != nil { |
| 911 |
return "", err |
| 912 |
} |
| 913 |
|
| 914 |
// Check if the data was written |
| 915 |
if length <= len(buffer) { |
| 916 |
// Exclude the null terminator |
| 917 |
return string(buffer[:length-1]), nil |
| 918 |
} |
| 919 |
|
| 920 |
// Make a bigger buffer if needed |
| 921 |
buffer = make([]byte, length) |
| 922 |
} |
| 923 |
} |
| 924 |
|
| 925 |
// Keyctl commands with special signatures. |
| 926 |
|
| 927 |
// KeyctlGetKeyringID implements the KEYCTL_GET_KEYRING_ID command. |
| 928 |
// See the full documentation at: |
| 929 |
// http://man7.org/linux/man-pages/man3/keyctl_get_keyring_ID.3.html |
| 930 |
func KeyctlGetKeyringID(id int, create bool) (ringid int, err error) { |
| 931 |
createInt := 0 |
| 932 |
if create { |
| 933 |
createInt = 1 |
| 934 |
} |
| 935 |
return KeyctlInt(KEYCTL_GET_KEYRING_ID, id, createInt, 0, 0) |
| 936 |
} |
| 937 |
|
| 938 |
// KeyctlSetperm implements the KEYCTL_SETPERM command. The perm value is the |
| 939 |
// key handle permission mask as described in the "keyctl setperm" section of |
| 940 |
// http://man7.org/linux/man-pages/man1/keyctl.1.html. |
| 941 |
// See the full documentation at: |
| 942 |
// http://man7.org/linux/man-pages/man3/keyctl_setperm.3.html |
| 943 |
func KeyctlSetperm(id int, perm uint32) error { |
| 944 |
_, err := KeyctlInt(KEYCTL_SETPERM, id, int(perm), 0, 0) |
| 945 |
return err |
| 946 |
} |
| 947 |
|
| 948 |
//sys keyctlJoin(cmd int, arg2 string) (ret int, err error) = SYS_KEYCTL |
| 949 |
|
| 950 |
// KeyctlJoinSessionKeyring implements the KEYCTL_JOIN_SESSION_KEYRING command. |
| 951 |
// See the full documentation at: |
| 952 |
// http://man7.org/linux/man-pages/man3/keyctl_join_session_keyring.3.html |
| 953 |
func KeyctlJoinSessionKeyring(name string) (ringid int, err error) { |
| 954 |
return keyctlJoin(KEYCTL_JOIN_SESSION_KEYRING, name) |
| 955 |
} |
| 956 |
|
| 957 |
//sys keyctlSearch(cmd int, arg2 int, arg3 string, arg4 string, arg5 int) (ret int, err error) = SYS_KEYCTL |
| 958 |
|
| 959 |
// KeyctlSearch implements the KEYCTL_SEARCH command. |
| 960 |
// See the full documentation at: |
| 961 |
// http://man7.org/linux/man-pages/man3/keyctl_search.3.html |
| 962 |
func KeyctlSearch(ringid int, keyType, description string, destRingid int) (id int, err error) { |
| 963 |
return keyctlSearch(KEYCTL_SEARCH, ringid, keyType, description, destRingid) |
| 964 |
} |
| 965 |
|
| 966 |
//sys keyctlIOV(cmd int, arg2 int, payload []Iovec, arg5 int) (err error) = SYS_KEYCTL |
| 967 |
|
| 968 |
// KeyctlInstantiateIOV implements the KEYCTL_INSTANTIATE_IOV command. This |
| 969 |
// command is similar to KEYCTL_INSTANTIATE, except that the payload is a slice |
| 970 |
// of Iovec (each of which represents a buffer) instead of a single buffer. |
| 971 |
// See the full documentation at: |
| 972 |
// http://man7.org/linux/man-pages/man3/keyctl_instantiate_iov.3.html |
| 973 |
func KeyctlInstantiateIOV(id int, payload []Iovec, ringid int) error { |
| 974 |
return keyctlIOV(KEYCTL_INSTANTIATE_IOV, id, payload, ringid) |
| 975 |
} |
| 976 |
|
| 977 |
//sys keyctlDH(cmd int, arg2 *KeyctlDHParams, buf []byte) (ret int, err error) = SYS_KEYCTL |
| 978 |
|
| 979 |
// KeyctlDHCompute implements the KEYCTL_DH_COMPUTE command. This command |
| 980 |
// computes a Diffie-Hellman shared secret based on the provide params. The |
| 981 |
// secret is written to the provided buffer and the returned size is the number |
| 982 |
// of bytes written (returning an error if there is insufficient space in the |
| 983 |
// buffer). If a nil buffer is passed in, this function returns the minimum |
| 984 |
// buffer length needed to store the appropriate data. Note that this differs |
| 985 |
// from KEYCTL_READ's behavior which always returns the requested payload size. |
| 986 |
// See the full documentation at: |
| 987 |
// http://man7.org/linux/man-pages/man3/keyctl_dh_compute.3.html |
| 988 |
func KeyctlDHCompute(params *KeyctlDHParams, buffer []byte) (size int, err error) { |
| 989 |
return keyctlDH(KEYCTL_DH_COMPUTE, params, buffer) |
| 990 |
} |
| 991 |
|
| 992 |
func Recvmsg(fd int, p, oob []byte, flags int) (n, oobn int, recvflags int, from Sockaddr, err error) { |
| 993 |
var msg Msghdr |
| 994 |
var rsa RawSockaddrAny |
| 995 |
msg.Name = (*byte)(unsafe.Pointer(&rsa)) |
| 996 |
msg.Namelen = uint32(SizeofSockaddrAny) |
| 997 |
var iov Iovec |
| 998 |
if len(p) > 0 { |
| 999 |
iov.Base = &p[0] |
| 1000 |
iov.SetLen(len(p)) |
| 1001 |
} |
| 1002 |
var dummy byte |
| 1003 |
if len(oob) > 0 { |
| 1004 |
if len(p) == 0 { |
| 1005 |
var sockType int |
| 1006 |
sockType, err = GetsockoptInt(fd, SOL_SOCKET, SO_TYPE) |
| 1007 |
if err != nil { |
| 1008 |
return |
| 1009 |
} |
| 1010 |
// receive at least one normal byte |
| 1011 |
if sockType != SOCK_DGRAM { |
| 1012 |
iov.Base = &dummy |
| 1013 |
iov.SetLen(1) |
| 1014 |
} |
| 1015 |
} |
| 1016 |
msg.Control = &oob[0] |
| 1017 |
msg.SetControllen(len(oob)) |
| 1018 |
} |
| 1019 |
msg.Iov = &iov |
| 1020 |
msg.Iovlen = 1 |
| 1021 |
if n, err = recvmsg(fd, &msg, flags); err != nil { |
| 1022 |
return |
| 1023 |
} |
| 1024 |
oobn = int(msg.Controllen) |
| 1025 |
recvflags = int(msg.Flags) |
| 1026 |
// source address is only specified if the socket is unconnected |
| 1027 |
if rsa.Addr.Family != AF_UNSPEC { |
| 1028 |
from, err = anyToSockaddr(fd, &rsa) |
| 1029 |
} |
| 1030 |
return |
| 1031 |
} |
| 1032 |
|
| 1033 |
func Sendmsg(fd int, p, oob []byte, to Sockaddr, flags int) (err error) { |
| 1034 |
_, err = SendmsgN(fd, p, oob, to, flags) |
| 1035 |
return |
| 1036 |
} |
| 1037 |
|
| 1038 |
func SendmsgN(fd int, p, oob []byte, to Sockaddr, flags int) (n int, err error) { |
| 1039 |
var ptr unsafe.Pointer |
| 1040 |
var salen _Socklen |
| 1041 |
if to != nil { |
| 1042 |
var err error |
| 1043 |
ptr, salen, err = to.sockaddr() |
| 1044 |
if err != nil { |
| 1045 |
return 0, err |
| 1046 |
} |
| 1047 |
} |
| 1048 |
var msg Msghdr |
| 1049 |
msg.Name = (*byte)(ptr) |
| 1050 |
msg.Namelen = uint32(salen) |
| 1051 |
var iov Iovec |
| 1052 |
if len(p) > 0 { |
| 1053 |
iov.Base = &p[0] |
| 1054 |
iov.SetLen(len(p)) |
| 1055 |
} |
| 1056 |
var dummy byte |
| 1057 |
if len(oob) > 0 { |
| 1058 |
if len(p) == 0 { |
| 1059 |
var sockType int |
| 1060 |
sockType, err = GetsockoptInt(fd, SOL_SOCKET, SO_TYPE) |
| 1061 |
if err != nil { |
| 1062 |
return 0, err |
| 1063 |
} |
| 1064 |
// send at least one normal byte |
| 1065 |
if sockType != SOCK_DGRAM { |
| 1066 |
iov.Base = &dummy |
| 1067 |
iov.SetLen(1) |
| 1068 |
} |
| 1069 |
} |
| 1070 |
msg.Control = &oob[0] |
| 1071 |
msg.SetControllen(len(oob)) |
| 1072 |
} |
| 1073 |
msg.Iov = &iov |
| 1074 |
msg.Iovlen = 1 |
| 1075 |
if n, err = sendmsg(fd, &msg, flags); err != nil { |
| 1076 |
return 0, err |
| 1077 |
} |
| 1078 |
if len(oob) > 0 && len(p) == 0 { |
| 1079 |
n = 0 |
| 1080 |
} |
| 1081 |
return n, nil |
| 1082 |
} |
| 1083 |
|
| 1084 |
// BindToDevice binds the socket associated with fd to device. |
| 1085 |
func BindToDevice(fd int, device string) (err error) { |
| 1086 |
return SetsockoptString(fd, SOL_SOCKET, SO_BINDTODEVICE, device) |
| 1087 |
} |
| 1088 |
|
| 1089 |
//sys ptrace(request int, pid int, addr uintptr, data uintptr) (err error) |
| 1090 |
|
| 1091 |
func ptracePeek(req int, pid int, addr uintptr, out []byte) (count int, err error) { |
| 1092 |
// The peek requests are machine-size oriented, so we wrap it |
| 1093 |
// to retrieve arbitrary-length data. |
| 1094 |
|
| 1095 |
// The ptrace syscall differs from glibc's ptrace. |
| 1096 |
// Peeks returns the word in *data, not as the return value. |
| 1097 |
|
| 1098 |
var buf [sizeofPtr]byte |
| 1099 |
|
| 1100 |
// Leading edge. PEEKTEXT/PEEKDATA don't require aligned |
| 1101 |
// access (PEEKUSER warns that it might), but if we don't |
| 1102 |
// align our reads, we might straddle an unmapped page |
| 1103 |
// boundary and not get the bytes leading up to the page |
| 1104 |
// boundary. |
| 1105 |
n := 0 |
| 1106 |
if addr%sizeofPtr != 0 { |
| 1107 |
err = ptrace(req, pid, addr-addr%sizeofPtr, uintptr(unsafe.Pointer(&buf[0]))) |
| 1108 |
if err != nil { |
| 1109 |
return 0, err |
| 1110 |
} |
| 1111 |
n += copy(out, buf[addr%sizeofPtr:]) |
| 1112 |
out = out[n:] |
| 1113 |
} |
| 1114 |
|
| 1115 |
// Remainder. |
| 1116 |
for len(out) > 0 { |
| 1117 |
// We use an internal buffer to guarantee alignment. |
| 1118 |
// It's not documented if this is necessary, but we're paranoid. |
| 1119 |
err = ptrace(req, pid, addr+uintptr(n), uintptr(unsafe.Pointer(&buf[0]))) |
| 1120 |
if err != nil { |
| 1121 |
return n, err |
| 1122 |
} |
| 1123 |
copied := copy(out, buf[0:]) |
| 1124 |
n += copied |
| 1125 |
out = out[copied:] |
| 1126 |
} |
| 1127 |
|
| 1128 |
return n, nil |
| 1129 |
} |
| 1130 |
|
| 1131 |
func PtracePeekText(pid int, addr uintptr, out []byte) (count int, err error) { |
| 1132 |
return ptracePeek(PTRACE_PEEKTEXT, pid, addr, out) |
| 1133 |
} |
| 1134 |
|
| 1135 |
func PtracePeekData(pid int, addr uintptr, out []byte) (count int, err error) { |
| 1136 |
return ptracePeek(PTRACE_PEEKDATA, pid, addr, out) |
| 1137 |
} |
| 1138 |
|
| 1139 |
func PtracePeekUser(pid int, addr uintptr, out []byte) (count int, err error) { |
| 1140 |
return ptracePeek(PTRACE_PEEKUSR, pid, addr, out) |
| 1141 |
} |
| 1142 |
|
| 1143 |
func ptracePoke(pokeReq int, peekReq int, pid int, addr uintptr, data []byte) (count int, err error) { |
| 1144 |
// As for ptracePeek, we need to align our accesses to deal |
| 1145 |
// with the possibility of straddling an invalid page. |
| 1146 |
|
| 1147 |
// Leading edge. |
| 1148 |
n := 0 |
| 1149 |
if addr%sizeofPtr != 0 { |
| 1150 |
var buf [sizeofPtr]byte |
| 1151 |
err = ptrace(peekReq, pid, addr-addr%sizeofPtr, uintptr(unsafe.Pointer(&buf[0]))) |
| 1152 |
if err != nil { |
| 1153 |
return 0, err |
| 1154 |
} |
| 1155 |
n += copy(buf[addr%sizeofPtr:], data) |
| 1156 |
word := *((*uintptr)(unsafe.Pointer(&buf[0]))) |
| 1157 |
err = ptrace(pokeReq, pid, addr-addr%sizeofPtr, word) |
| 1158 |
if err != nil { |
| 1159 |
return 0, err |
| 1160 |
} |
| 1161 |
data = data[n:] |
| 1162 |
} |
| 1163 |
|
| 1164 |
// Interior. |
| 1165 |
for len(data) > sizeofPtr { |
| 1166 |
word := *((*uintptr)(unsafe.Pointer(&data[0]))) |
| 1167 |
err = ptrace(pokeReq, pid, addr+uintptr(n), word) |
| 1168 |
if err != nil { |
| 1169 |
return n, err |
| 1170 |
} |
| 1171 |
n += sizeofPtr |
| 1172 |
data = data[sizeofPtr:] |
| 1173 |
} |
| 1174 |
|
| 1175 |
// Trailing edge. |
| 1176 |
if len(data) > 0 { |
| 1177 |
var buf [sizeofPtr]byte |
| 1178 |
err = ptrace(peekReq, pid, addr+uintptr(n), uintptr(unsafe.Pointer(&buf[0]))) |
| 1179 |
if err != nil { |
| 1180 |
return n, err |
| 1181 |
} |
| 1182 |
copy(buf[0:], data) |
| 1183 |
word := *((*uintptr)(unsafe.Pointer(&buf[0]))) |
| 1184 |
err = ptrace(pokeReq, pid, addr+uintptr(n), word) |
| 1185 |
if err != nil { |
| 1186 |
return n, err |
| 1187 |
} |
| 1188 |
n += len(data) |
| 1189 |
} |
| 1190 |
|
| 1191 |
return n, nil |
| 1192 |
} |
| 1193 |
|
| 1194 |
func PtracePokeText(pid int, addr uintptr, data []byte) (count int, err error) { |
| 1195 |
return ptracePoke(PTRACE_POKETEXT, PTRACE_PEEKTEXT, pid, addr, data) |
| 1196 |
} |
| 1197 |
|
| 1198 |
func PtracePokeData(pid int, addr uintptr, data []byte) (count int, err error) { |
| 1199 |
return ptracePoke(PTRACE_POKEDATA, PTRACE_PEEKDATA, pid, addr, data) |
| 1200 |
} |
| 1201 |
|
| 1202 |
func PtracePokeUser(pid int, addr uintptr, data []byte) (count int, err error) { |
| 1203 |
return ptracePoke(PTRACE_POKEUSR, PTRACE_PEEKUSR, pid, addr, data) |
| 1204 |
} |
| 1205 |
|
| 1206 |
func PtraceGetRegs(pid int, regsout *PtraceRegs) (err error) { |
| 1207 |
return ptrace(PTRACE_GETREGS, pid, 0, uintptr(unsafe.Pointer(regsout))) |
| 1208 |
} |
| 1209 |
|
| 1210 |
func PtraceSetRegs(pid int, regs *PtraceRegs) (err error) { |
| 1211 |
return ptrace(PTRACE_SETREGS, pid, 0, uintptr(unsafe.Pointer(regs))) |
| 1212 |
} |
| 1213 |
|
| 1214 |
func PtraceSetOptions(pid int, options int) (err error) { |
| 1215 |
return ptrace(PTRACE_SETOPTIONS, pid, 0, uintptr(options)) |
| 1216 |
} |
| 1217 |
|
| 1218 |
func PtraceGetEventMsg(pid int) (msg uint, err error) { |
| 1219 |
var data _C_long |
| 1220 |
err = ptrace(PTRACE_GETEVENTMSG, pid, 0, uintptr(unsafe.Pointer(&data))) |
| 1221 |
msg = uint(data) |
| 1222 |
return |
| 1223 |
} |
| 1224 |
|
| 1225 |
func PtraceCont(pid int, signal int) (err error) { |
| 1226 |
return ptrace(PTRACE_CONT, pid, 0, uintptr(signal)) |
| 1227 |
} |
| 1228 |
|
| 1229 |
func PtraceSyscall(pid int, signal int) (err error) { |
| 1230 |
return ptrace(PTRACE_SYSCALL, pid, 0, uintptr(signal)) |
| 1231 |
} |
| 1232 |
|
| 1233 |
func PtraceSingleStep(pid int) (err error) { return ptrace(PTRACE_SINGLESTEP, pid, 0, 0) } |
| 1234 |
|
| 1235 |
func PtraceAttach(pid int) (err error) { return ptrace(PTRACE_ATTACH, pid, 0, 0) } |
| 1236 |
|
| 1237 |
func PtraceDetach(pid int) (err error) { return ptrace(PTRACE_DETACH, pid, 0, 0) } |
| 1238 |
|
| 1239 |
//sys reboot(magic1 uint, magic2 uint, cmd int, arg string) (err error) |
| 1240 |
|
| 1241 |
func Reboot(cmd int) (err error) { |
| 1242 |
return reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, cmd, "") |
| 1243 |
} |
| 1244 |
|
| 1245 |
func ReadDirent(fd int, buf []byte) (n int, err error) { |
| 1246 |
return Getdents(fd, buf) |
| 1247 |
} |
| 1248 |
|
| 1249 |
//sys mount(source string, target string, fstype string, flags uintptr, data *byte) (err error) |
| 1250 |
|
| 1251 |
func Mount(source string, target string, fstype string, flags uintptr, data string) (err error) { |
| 1252 |
// Certain file systems get rather angry and EINVAL if you give |
| 1253 |
// them an empty string of data, rather than NULL. |
| 1254 |
if data == "" { |
| 1255 |
return mount(source, target, fstype, flags, nil) |
| 1256 |
} |
| 1257 |
datap, err := BytePtrFromString(data) |
| 1258 |
if err != nil { |
| 1259 |
return err |
| 1260 |
} |
| 1261 |
return mount(source, target, fstype, flags, datap) |
| 1262 |
} |
| 1263 |
|
| 1264 |
// Sendto |
| 1265 |
// Recvfrom |
| 1266 |
// Socketpair |
| 1267 |
|
| 1268 |
/* |
| 1269 |
* Direct access |
| 1270 |
*/ |
| 1271 |
//sys Acct(path string) (err error) |
| 1272 |
//sys AddKey(keyType string, description string, payload []byte, ringid int) (id int, err error) |
| 1273 |
//sys Adjtimex(buf *Timex) (state int, err error) |
| 1274 |
//sys Chdir(path string) (err error) |
| 1275 |
//sys Chroot(path string) (err error) |
| 1276 |
//sys ClockGettime(clockid int32, time *Timespec) (err error) |
| 1277 |
//sys Close(fd int) (err error) |
| 1278 |
//sys CopyFileRange(rfd int, roff *int64, wfd int, woff *int64, len int, flags int) (n int, err error) |
| 1279 |
//sys Dup(oldfd int) (fd int, err error) |
| 1280 |
//sys Dup3(oldfd int, newfd int, flags int) (err error) |
| 1281 |
//sysnb EpollCreate1(flag int) (fd int, err error) |
| 1282 |
//sysnb EpollCtl(epfd int, op int, fd int, event *EpollEvent) (err error) |
| 1283 |
//sys Eventfd(initval uint, flags int) (fd int, err error) = SYS_EVENTFD2 |
| 1284 |
//sys Exit(code int) = SYS_EXIT_GROUP |
| 1285 |
//sys Fallocate(fd int, mode uint32, off int64, len int64) (err error) |
| 1286 |
//sys Fchdir(fd int) (err error) |
| 1287 |
//sys Fchmod(fd int, mode uint32) (err error) |
| 1288 |
//sys Fchownat(dirfd int, path string, uid int, gid int, flags int) (err error) |
| 1289 |
//sys fcntl(fd int, cmd int, arg int) (val int, err error) |
| 1290 |
//sys Fdatasync(fd int) (err error) |
| 1291 |
//sys Fgetxattr(fd int, attr string, dest []byte) (sz int, err error) |
| 1292 |
//sys Flistxattr(fd int, dest []byte) (sz int, err error) |
| 1293 |
//sys Flock(fd int, how int) (err error) |
| 1294 |
//sys Fremovexattr(fd int, attr string) (err error) |
| 1295 |
//sys Fsetxattr(fd int, attr string, dest []byte, flags int) (err error) |
| 1296 |
//sys Fsync(fd int) (err error) |
| 1297 |
//sys Getdents(fd int, buf []byte) (n int, err error) = SYS_GETDENTS64 |
| 1298 |
//sysnb Getpgid(pid int) (pgid int, err error) |
| 1299 |
|
| 1300 |
func Getpgrp() (pid int) { |
| 1301 |
pid, _ = Getpgid(0) |
| 1302 |
return |
| 1303 |
} |
| 1304 |
|
| 1305 |
//sysnb Getpid() (pid int) |
| 1306 |
//sysnb Getppid() (ppid int) |
| 1307 |
//sys Getpriority(which int, who int) (prio int, err error) |
| 1308 |
//sys Getrandom(buf []byte, flags int) (n int, err error) |
| 1309 |
//sysnb Getrusage(who int, rusage *Rusage) (err error) |
| 1310 |
//sysnb Getsid(pid int) (sid int, err error) |
| 1311 |
//sysnb Gettid() (tid int) |
| 1312 |
//sys Getxattr(path string, attr string, dest []byte) (sz int, err error) |
| 1313 |
//sys InotifyAddWatch(fd int, pathname string, mask uint32) (watchdesc int, err error) |
| 1314 |
//sysnb InotifyInit1(flags int) (fd int, err error) |
| 1315 |
//sysnb InotifyRmWatch(fd int, watchdesc uint32) (success int, err error) |
| 1316 |
//sysnb Kill(pid int, sig syscall.Signal) (err error) |
| 1317 |
//sys Klogctl(typ int, buf []byte) (n int, err error) = SYS_SYSLOG |
| 1318 |
//sys Lgetxattr(path string, attr string, dest []byte) (sz int, err error) |
| 1319 |
//sys Listxattr(path string, dest []byte) (sz int, err error) |
| 1320 |
//sys Llistxattr(path string, dest []byte) (sz int, err error) |
| 1321 |
//sys Lremovexattr(path string, attr string) (err error) |
| 1322 |
//sys Lsetxattr(path string, attr string, data []byte, flags int) (err error) |
| 1323 |
//sys Mkdirat(dirfd int, path string, mode uint32) (err error) |
| 1324 |
//sys Mknodat(dirfd int, path string, mode uint32, dev int) (err error) |
| 1325 |
//sys Nanosleep(time *Timespec, leftover *Timespec) (err error) |
| 1326 |
//sys PerfEventOpen(attr *PerfEventAttr, pid int, cpu int, groupFd int, flags int) (fd int, err error) |
| 1327 |
//sys PivotRoot(newroot string, putold string) (err error) = SYS_PIVOT_ROOT |
| 1328 |
//sysnb prlimit(pid int, resource int, newlimit *Rlimit, old *Rlimit) (err error) = SYS_PRLIMIT64 |
| 1329 |
//sys Prctl(option int, arg2 uintptr, arg3 uintptr, arg4 uintptr, arg5 uintptr) (err error) |
| 1330 |
//sys Pselect(nfd int, r *FdSet, w *FdSet, e *FdSet, timeout *Timespec, sigmask *Sigset_t) (n int, err error) = SYS_PSELECT6 |
| 1331 |
//sys read(fd int, p []byte) (n int, err error) |
| 1332 |
//sys Removexattr(path string, attr string) (err error) |
| 1333 |
//sys Renameat(olddirfd int, oldpath string, newdirfd int, newpath string) (err error) |
| 1334 |
//sys Renameat2(olddirfd int, oldpath string, newdirfd int, newpath string, flags uint) (err error) |
| 1335 |
//sys RequestKey(keyType string, description string, callback string, destRingid int) (id int, err error) |
| 1336 |
//sys Setdomainname(p []byte) (err error) |
| 1337 |
//sys Sethostname(p []byte) (err error) |
| 1338 |
//sysnb Setpgid(pid int, pgid int) (err error) |
| 1339 |
//sysnb Setsid() (pid int, err error) |
| 1340 |
//sysnb Settimeofday(tv *Timeval) (err error) |
| 1341 |
//sys Setns(fd int, nstype int) (err error) |
| 1342 |
|
| 1343 |
// issue 1435. |
| 1344 |
// On linux Setuid and Setgid only affects the current thread, not the process. |
| 1345 |
// This does not match what most callers expect so we must return an error |
| 1346 |
// here rather than letting the caller think that the call succeeded. |
| 1347 |
|
| 1348 |
func Setuid(uid int) (err error) { |
| 1349 |
return EOPNOTSUPP |
| 1350 |
} |
| 1351 |
|
| 1352 |
func Setgid(uid int) (err error) { |
| 1353 |
return EOPNOTSUPP |
| 1354 |
} |
| 1355 |
|
| 1356 |
//sys Setpriority(which int, who int, prio int) (err error) |
| 1357 |
//sys Setxattr(path string, attr string, data []byte, flags int) (err error) |
| 1358 |
//sys Statx(dirfd int, path string, flags int, mask int, stat *Statx_t) (err error) |
| 1359 |
//sys Sync() |
| 1360 |
//sys Syncfs(fd int) (err error) |
| 1361 |
//sysnb Sysinfo(info *Sysinfo_t) (err error) |
| 1362 |
//sys Tee(rfd int, wfd int, len int, flags int) (n int64, err error) |
| 1363 |
//sysnb Tgkill(tgid int, tid int, sig syscall.Signal) (err error) |
| 1364 |
//sysnb Times(tms *Tms) (ticks uintptr, err error) |
| 1365 |
//sysnb Umask(mask int) (oldmask int) |
| 1366 |
//sysnb Uname(buf *Utsname) (err error) |
| 1367 |
//sys Unmount(target string, flags int) (err error) = SYS_UMOUNT2 |
| 1368 |
//sys Unshare(flags int) (err error) |
| 1369 |
//sys write(fd int, p []byte) (n int, err error) |
| 1370 |
//sys exitThread(code int) (err error) = SYS_EXIT |
| 1371 |
//sys readlen(fd int, p *byte, np int) (n int, err error) = SYS_READ |
| 1372 |
//sys writelen(fd int, p *byte, np int) (n int, err error) = SYS_WRITE |
| 1373 |
|
| 1374 |
// mmap varies by architecture; see syscall_linux_*.go. |
| 1375 |
//sys munmap(addr uintptr, length uintptr) (err error) |
| 1376 |
|
| 1377 |
var mapper = &mmapper{ |
| 1378 |
active: make(map[*byte][]byte), |
| 1379 |
mmap: mmap, |
| 1380 |
munmap: munmap, |
| 1381 |
} |
| 1382 |
|
| 1383 |
func Mmap(fd int, offset int64, length int, prot int, flags int) (data []byte, err error) { |
| 1384 |
return mapper.Mmap(fd, offset, length, prot, flags) |
| 1385 |
} |
| 1386 |
|
| 1387 |
func Munmap(b []byte) (err error) { |
| 1388 |
return mapper.Munmap(b) |
| 1389 |
} |
| 1390 |
|
| 1391 |
//sys Madvise(b []byte, advice int) (err error) |
| 1392 |
//sys Mprotect(b []byte, prot int) (err error) |
| 1393 |
//sys Mlock(b []byte) (err error) |
| 1394 |
//sys Mlockall(flags int) (err error) |
| 1395 |
//sys Msync(b []byte, flags int) (err error) |
| 1396 |
//sys Munlock(b []byte) (err error) |
| 1397 |
//sys Munlockall() (err error) |
| 1398 |
|
| 1399 |
// Vmsplice splices user pages from a slice of Iovecs into a pipe specified by fd, |
| 1400 |
// using the specified flags. |
| 1401 |
func Vmsplice(fd int, iovs []Iovec, flags int) (int, error) { |
| 1402 |
n, _, errno := Syscall6( |
| 1403 |
SYS_VMSPLICE, |
| 1404 |
uintptr(fd), |
| 1405 |
uintptr(unsafe.Pointer(&iovs[0])), |
| 1406 |
uintptr(len(iovs)), |
| 1407 |
uintptr(flags), |
| 1408 |
0, |
| 1409 |
0, |
| 1410 |
) |
| 1411 |
if errno != 0 { |
| 1412 |
return 0, syscall.Errno(errno) |
| 1413 |
} |
| 1414 |
|
| 1415 |
return int(n), nil |
| 1416 |
} |
| 1417 |
|
| 1418 |
//sys faccessat(dirfd int, path string, mode uint32) (err error) |
| 1419 |
|
| 1420 |
func Faccessat(dirfd int, path string, mode uint32, flags int) (err error) { |
| 1421 |
if flags & ^(AT_SYMLINK_NOFOLLOW|AT_EACCESS) != 0 { |
| 1422 |
return EINVAL |
| 1423 |
} |
| 1424 |
|
| 1425 |
// The Linux kernel faccessat system call does not take any flags. |
| 1426 |
// The glibc faccessat implements the flags itself; see |
| 1427 |
// https://sourceware.org/git/?p=glibc.git;a=blob;f=sysdeps/unix/sysv/linux/faccessat.c;hb=HEAD |
| 1428 |
// Because people naturally expect syscall.Faccessat to act |
| 1429 |
// like C faccessat, we do the same. |
| 1430 |
|
| 1431 |
if flags == 0 { |
| 1432 |
return faccessat(dirfd, path, mode) |
| 1433 |
} |
| 1434 |
|
| 1435 |
var st Stat_t |
| 1436 |
if err := Fstatat(dirfd, path, &st, flags&AT_SYMLINK_NOFOLLOW); err != nil { |
| 1437 |
return err |
| 1438 |
} |
| 1439 |
|
| 1440 |
mode &= 7 |
| 1441 |
if mode == 0 { |
| 1442 |
return nil |
| 1443 |
} |
| 1444 |
|
| 1445 |
var uid int |
| 1446 |
if flags&AT_EACCESS != 0 { |
| 1447 |
uid = Geteuid() |
| 1448 |
} else { |
| 1449 |
uid = Getuid() |
| 1450 |
} |
| 1451 |
|
| 1452 |
if uid == 0 { |
| 1453 |
if mode&1 == 0 { |
| 1454 |
// Root can read and write any file. |
| 1455 |
return nil |
| 1456 |
} |
| 1457 |
if st.Mode&0111 != 0 { |
| 1458 |
// Root can execute any file that anybody can execute. |
| 1459 |
return nil |
| 1460 |
} |
| 1461 |
return EACCES |
| 1462 |
} |
| 1463 |
|
| 1464 |
var fmode uint32 |
| 1465 |
if uint32(uid) == st.Uid { |
| 1466 |
fmode = (st.Mode >> 6) & 7 |
| 1467 |
} else { |
| 1468 |
var gid int |
| 1469 |
if flags&AT_EACCESS != 0 { |
| 1470 |
gid = Getegid() |
| 1471 |
} else { |
| 1472 |
gid = Getgid() |
| 1473 |
} |
| 1474 |
|
| 1475 |
if uint32(gid) == st.Gid { |
| 1476 |
fmode = (st.Mode >> 3) & 7 |
| 1477 |
} else { |
| 1478 |
fmode = st.Mode & 7 |
| 1479 |
} |
| 1480 |
} |
| 1481 |
|
| 1482 |
if fmode&mode == mode { |
| 1483 |
return nil |
| 1484 |
} |
| 1485 |
|
| 1486 |
return EACCES |
| 1487 |
} |
| 1488 |
|
| 1489 |
/* |
| 1490 |
* Unimplemented |
| 1491 |
*/ |
| 1492 |
// AfsSyscall |
| 1493 |
// Alarm |
| 1494 |
// ArchPrctl |
| 1495 |
// Brk |
| 1496 |
// Capget |
| 1497 |
// Capset |
| 1498 |
// ClockGetres |
| 1499 |
// ClockNanosleep |
| 1500 |
// ClockSettime |
| 1501 |
// Clone |
| 1502 |
// CreateModule |
| 1503 |
// DeleteModule |
| 1504 |
// EpollCtlOld |
| 1505 |
// EpollPwait |
| 1506 |
// EpollWaitOld |
| 1507 |
// Execve |
| 1508 |
// Fork |
| 1509 |
// Futex |
| 1510 |
// GetKernelSyms |
| 1511 |
// GetMempolicy |
| 1512 |
// GetRobustList |
| 1513 |
// GetThreadArea |
| 1514 |
// Getitimer |
| 1515 |
// Getpmsg |
| 1516 |
// IoCancel |
| 1517 |
// IoDestroy |
| 1518 |
// IoGetevents |
| 1519 |
// IoSetup |
| 1520 |
// IoSubmit |
| 1521 |
// IoprioGet |
| 1522 |
// IoprioSet |
| 1523 |
// KexecLoad |
| 1524 |
// LookupDcookie |
| 1525 |
// Mbind |
| 1526 |
// MigratePages |
| 1527 |
// Mincore |
| 1528 |
// ModifyLdt |
| 1529 |
// Mount |
| 1530 |
// MovePages |
| 1531 |
// MqGetsetattr |
| 1532 |
// MqNotify |
| 1533 |
// MqOpen |
| 1534 |
// MqTimedreceive |
| 1535 |
// MqTimedsend |
| 1536 |
// MqUnlink |
| 1537 |
// Mremap |
| 1538 |
// Msgctl |
| 1539 |
// Msgget |
| 1540 |
// Msgrcv |
| 1541 |
// Msgsnd |
| 1542 |
// Nfsservctl |
| 1543 |
// Personality |
| 1544 |
// Pselect6 |
| 1545 |
// Ptrace |
| 1546 |
// Putpmsg |
| 1547 |
// QueryModule |
| 1548 |
// Quotactl |
| 1549 |
// Readahead |
| 1550 |
// Readv |
| 1551 |
// RemapFilePages |
| 1552 |
// RestartSyscall |
| 1553 |
// RtSigaction |
| 1554 |
// RtSigpending |
| 1555 |
// RtSigprocmask |
| 1556 |
// RtSigqueueinfo |
| 1557 |
// RtSigreturn |
| 1558 |
// RtSigsuspend |
| 1559 |
// RtSigtimedwait |
| 1560 |
// SchedGetPriorityMax |
| 1561 |
// SchedGetPriorityMin |
| 1562 |
// SchedGetparam |
| 1563 |
// SchedGetscheduler |
| 1564 |
// SchedRrGetInterval |
| 1565 |
// SchedSetparam |
| 1566 |
// SchedYield |
| 1567 |
// Security |
| 1568 |
// Semctl |
| 1569 |
// Semget |
| 1570 |
// Semop |
| 1571 |
// Semtimedop |
| 1572 |
// SetMempolicy |
| 1573 |
// SetRobustList |
| 1574 |
// SetThreadArea |
| 1575 |
// SetTidAddress |
| 1576 |
// Shmat |
| 1577 |
// Shmctl |
| 1578 |
// Shmdt |
| 1579 |
// Shmget |
| 1580 |
// Sigaltstack |
| 1581 |
// Signalfd |
| 1582 |
// Swapoff |
| 1583 |
// Swapon |
| 1584 |
// Sysfs |
| 1585 |
// TimerCreate |
| 1586 |
// TimerDelete |
| 1587 |
// TimerGetoverrun |
| 1588 |
// TimerGettime |
| 1589 |
// TimerSettime |
| 1590 |
// Timerfd |
| 1591 |
// Tkill (obsolete) |
| 1592 |
// Tuxcall |
| 1593 |
// Umount2 |
| 1594 |
// Uselib |
| 1595 |
// Utimensat |
| 1596 |
// Vfork |
| 1597 |
// Vhangup |
| 1598 |
// Vserver |
| 1599 |
// Waitid |
| 1600 |
// _Sysctl |