A lightweight mechanism to provide an *instant kickstart* to a Go web server instance upon changing any Go source files under the project directory (and its subdirectories).

syscall_linux.go 40KB

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