syscall_linux.go 63 KB

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