syscall_linux.go 49 KB

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