You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1491 lines
42 KiB

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