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

syscall_linux.go 64KB

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