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