syscall_linux.go 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245
  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. func isGroupMember(gid int) bool {
  1713. groups, err := Getgroups()
  1714. if err != nil {
  1715. return false
  1716. }
  1717. for _, g := range groups {
  1718. if g == gid {
  1719. return true
  1720. }
  1721. }
  1722. return false
  1723. }
  1724. //sys faccessat(dirfd int, path string, mode uint32) (err error)
  1725. //sys Faccessat2(dirfd int, path string, mode uint32, flags int) (err error)
  1726. func Faccessat(dirfd int, path string, mode uint32, flags int) (err error) {
  1727. if flags == 0 {
  1728. return faccessat(dirfd, path, mode)
  1729. }
  1730. if err := Faccessat2(dirfd, path, mode, flags); err != ENOSYS {
  1731. return err
  1732. }
  1733. // The Linux kernel faccessat system call does not take any flags.
  1734. // The glibc faccessat implements the flags itself; see
  1735. // https://sourceware.org/git/?p=glibc.git;a=blob;f=sysdeps/unix/sysv/linux/faccessat.c;hb=HEAD
  1736. // Because people naturally expect syscall.Faccessat to act
  1737. // like C faccessat, we do the same.
  1738. if flags & ^(AT_SYMLINK_NOFOLLOW|AT_EACCESS) != 0 {
  1739. return EINVAL
  1740. }
  1741. var st Stat_t
  1742. if err := Fstatat(dirfd, path, &st, flags&AT_SYMLINK_NOFOLLOW); err != nil {
  1743. return err
  1744. }
  1745. mode &= 7
  1746. if mode == 0 {
  1747. return nil
  1748. }
  1749. var uid int
  1750. if flags&AT_EACCESS != 0 {
  1751. uid = Geteuid()
  1752. } else {
  1753. uid = Getuid()
  1754. }
  1755. if uid == 0 {
  1756. if mode&1 == 0 {
  1757. // Root can read and write any file.
  1758. return nil
  1759. }
  1760. if st.Mode&0111 != 0 {
  1761. // Root can execute any file that anybody can execute.
  1762. return nil
  1763. }
  1764. return EACCES
  1765. }
  1766. var fmode uint32
  1767. if uint32(uid) == st.Uid {
  1768. fmode = (st.Mode >> 6) & 7
  1769. } else {
  1770. var gid int
  1771. if flags&AT_EACCESS != 0 {
  1772. gid = Getegid()
  1773. } else {
  1774. gid = Getgid()
  1775. }
  1776. if uint32(gid) == st.Gid || isGroupMember(gid) {
  1777. fmode = (st.Mode >> 3) & 7
  1778. } else {
  1779. fmode = st.Mode & 7
  1780. }
  1781. }
  1782. if fmode&mode == mode {
  1783. return nil
  1784. }
  1785. return EACCES
  1786. }
  1787. //sys nameToHandleAt(dirFD int, pathname string, fh *fileHandle, mountID *_C_int, flags int) (err error) = SYS_NAME_TO_HANDLE_AT
  1788. //sys openByHandleAt(mountFD int, fh *fileHandle, flags int) (fd int, err error) = SYS_OPEN_BY_HANDLE_AT
  1789. // fileHandle is the argument to nameToHandleAt and openByHandleAt. We
  1790. // originally tried to generate it via unix/linux/types.go with "type
  1791. // fileHandle C.struct_file_handle" but that generated empty structs
  1792. // for mips64 and mips64le. Instead, hard code it for now (it's the
  1793. // same everywhere else) until the mips64 generator issue is fixed.
  1794. type fileHandle struct {
  1795. Bytes uint32
  1796. Type int32
  1797. }
  1798. // FileHandle represents the C struct file_handle used by
  1799. // name_to_handle_at (see NameToHandleAt) and open_by_handle_at (see
  1800. // OpenByHandleAt).
  1801. type FileHandle struct {
  1802. *fileHandle
  1803. }
  1804. // NewFileHandle constructs a FileHandle.
  1805. func NewFileHandle(handleType int32, handle []byte) FileHandle {
  1806. const hdrSize = unsafe.Sizeof(fileHandle{})
  1807. buf := make([]byte, hdrSize+uintptr(len(handle)))
  1808. copy(buf[hdrSize:], handle)
  1809. fh := (*fileHandle)(unsafe.Pointer(&buf[0]))
  1810. fh.Type = handleType
  1811. fh.Bytes = uint32(len(handle))
  1812. return FileHandle{fh}
  1813. }
  1814. func (fh *FileHandle) Size() int { return int(fh.fileHandle.Bytes) }
  1815. func (fh *FileHandle) Type() int32 { return fh.fileHandle.Type }
  1816. func (fh *FileHandle) Bytes() []byte {
  1817. n := fh.Size()
  1818. if n == 0 {
  1819. return nil
  1820. }
  1821. return (*[1 << 30]byte)(unsafe.Pointer(uintptr(unsafe.Pointer(&fh.fileHandle.Type)) + 4))[:n:n]
  1822. }
  1823. // NameToHandleAt wraps the name_to_handle_at system call; it obtains
  1824. // a handle for a path name.
  1825. func NameToHandleAt(dirfd int, path string, flags int) (handle FileHandle, mountID int, err error) {
  1826. var mid _C_int
  1827. // Try first with a small buffer, assuming the handle will
  1828. // only be 32 bytes.
  1829. size := uint32(32 + unsafe.Sizeof(fileHandle{}))
  1830. didResize := false
  1831. for {
  1832. buf := make([]byte, size)
  1833. fh := (*fileHandle)(unsafe.Pointer(&buf[0]))
  1834. fh.Bytes = size - uint32(unsafe.Sizeof(fileHandle{}))
  1835. err = nameToHandleAt(dirfd, path, fh, &mid, flags)
  1836. if err == EOVERFLOW {
  1837. if didResize {
  1838. // We shouldn't need to resize more than once
  1839. return
  1840. }
  1841. didResize = true
  1842. size = fh.Bytes + uint32(unsafe.Sizeof(fileHandle{}))
  1843. continue
  1844. }
  1845. if err != nil {
  1846. return
  1847. }
  1848. return FileHandle{fh}, int(mid), nil
  1849. }
  1850. }
  1851. // OpenByHandleAt wraps the open_by_handle_at system call; it opens a
  1852. // file via a handle as previously returned by NameToHandleAt.
  1853. func OpenByHandleAt(mountFD int, handle FileHandle, flags int) (fd int, err error) {
  1854. return openByHandleAt(mountFD, handle.fileHandle, flags)
  1855. }
  1856. // Klogset wraps the sys_syslog system call; it sets console_loglevel to
  1857. // the value specified by arg and passes a dummy pointer to bufp.
  1858. func Klogset(typ int, arg int) (err error) {
  1859. var p unsafe.Pointer
  1860. _, _, errno := Syscall(SYS_SYSLOG, uintptr(typ), uintptr(p), uintptr(arg))
  1861. if errno != 0 {
  1862. return errnoErr(errno)
  1863. }
  1864. return nil
  1865. }
  1866. // RemoteIovec is Iovec with the pointer replaced with an integer.
  1867. // It is used for ProcessVMReadv and ProcessVMWritev, where the pointer
  1868. // refers to a location in a different process' address space, which
  1869. // would confuse the Go garbage collector.
  1870. type RemoteIovec struct {
  1871. Base uintptr
  1872. Len int
  1873. }
  1874. //sys ProcessVMReadv(pid int, localIov []Iovec, remoteIov []RemoteIovec, flags uint) (n int, err error) = SYS_PROCESS_VM_READV
  1875. //sys ProcessVMWritev(pid int, localIov []Iovec, remoteIov []RemoteIovec, flags uint) (n int, err error) = SYS_PROCESS_VM_WRITEV
  1876. /*
  1877. * Unimplemented
  1878. */
  1879. // AfsSyscall
  1880. // Alarm
  1881. // ArchPrctl
  1882. // Brk
  1883. // ClockNanosleep
  1884. // ClockSettime
  1885. // Clone
  1886. // EpollCtlOld
  1887. // EpollPwait
  1888. // EpollWaitOld
  1889. // Execve
  1890. // Fork
  1891. // Futex
  1892. // GetKernelSyms
  1893. // GetMempolicy
  1894. // GetRobustList
  1895. // GetThreadArea
  1896. // Getitimer
  1897. // Getpmsg
  1898. // IoCancel
  1899. // IoDestroy
  1900. // IoGetevents
  1901. // IoSetup
  1902. // IoSubmit
  1903. // IoprioGet
  1904. // IoprioSet
  1905. // KexecLoad
  1906. // LookupDcookie
  1907. // Mbind
  1908. // MigratePages
  1909. // Mincore
  1910. // ModifyLdt
  1911. // Mount
  1912. // MovePages
  1913. // MqGetsetattr
  1914. // MqNotify
  1915. // MqOpen
  1916. // MqTimedreceive
  1917. // MqTimedsend
  1918. // MqUnlink
  1919. // Mremap
  1920. // Msgctl
  1921. // Msgget
  1922. // Msgrcv
  1923. // Msgsnd
  1924. // Nfsservctl
  1925. // Personality
  1926. // Pselect6
  1927. // Ptrace
  1928. // Putpmsg
  1929. // Quotactl
  1930. // Readahead
  1931. // Readv
  1932. // RemapFilePages
  1933. // RestartSyscall
  1934. // RtSigaction
  1935. // RtSigpending
  1936. // RtSigprocmask
  1937. // RtSigqueueinfo
  1938. // RtSigreturn
  1939. // RtSigsuspend
  1940. // RtSigtimedwait
  1941. // SchedGetPriorityMax
  1942. // SchedGetPriorityMin
  1943. // SchedGetparam
  1944. // SchedGetscheduler
  1945. // SchedRrGetInterval
  1946. // SchedSetparam
  1947. // SchedYield
  1948. // Security
  1949. // Semctl
  1950. // Semget
  1951. // Semop
  1952. // Semtimedop
  1953. // SetMempolicy
  1954. // SetRobustList
  1955. // SetThreadArea
  1956. // SetTidAddress
  1957. // Shmat
  1958. // Shmctl
  1959. // Shmdt
  1960. // Shmget
  1961. // Sigaltstack
  1962. // Swapoff
  1963. // Swapon
  1964. // Sysfs
  1965. // TimerCreate
  1966. // TimerDelete
  1967. // TimerGetoverrun
  1968. // TimerGettime
  1969. // TimerSettime
  1970. // Tkill (obsolete)
  1971. // Tuxcall
  1972. // Umount2
  1973. // Uselib
  1974. // Utimensat
  1975. // Vfork
  1976. // Vhangup
  1977. // Vserver
  1978. // Waitid
  1979. // _Sysctl