syscall_linux.go 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111
  1. // Copyright 2009 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // Linux system calls.
  5. // This file is compiled as ordinary Go code,
  6. // but it is also input to mksyscall,
  7. // which parses the //sys lines and generates system call stubs.
  8. // Note that sometimes we use a lowercase //sys name and
  9. // wrap it in our own nicer implementation.
  10. package unix
  11. import (
  12. "syscall"
  13. "unsafe"
  14. )
  15. /*
  16. * Wrapped
  17. */
  18. func Access(path string, mode uint32) (err error) {
  19. return Faccessat(AT_FDCWD, path, mode, 0)
  20. }
  21. func Chmod(path string, mode uint32) (err error) {
  22. return Fchmodat(AT_FDCWD, path, mode, 0)
  23. }
  24. func Chown(path string, uid int, gid int) (err error) {
  25. return Fchownat(AT_FDCWD, path, uid, gid, 0)
  26. }
  27. func Creat(path string, mode uint32) (fd int, err error) {
  28. return Open(path, O_CREAT|O_WRONLY|O_TRUNC, mode)
  29. }
  30. //sys Linkat(olddirfd int, oldpath string, newdirfd int, newpath string, flags int) (err error)
  31. func Link(oldpath string, newpath string) (err error) {
  32. return Linkat(AT_FDCWD, oldpath, AT_FDCWD, newpath, 0)
  33. }
  34. func Mkdir(path string, mode uint32) (err error) {
  35. return Mkdirat(AT_FDCWD, path, mode)
  36. }
  37. func Mknod(path string, mode uint32, dev int) (err error) {
  38. return Mknodat(AT_FDCWD, path, mode, dev)
  39. }
  40. func Open(path string, mode int, perm uint32) (fd int, err error) {
  41. return openat(AT_FDCWD, path, mode|O_LARGEFILE, perm)
  42. }
  43. //sys openat(dirfd int, path string, flags int, mode uint32) (fd int, err error)
  44. func Openat(dirfd int, path string, flags int, mode uint32) (fd int, err error) {
  45. return openat(dirfd, path, flags|O_LARGEFILE, mode)
  46. }
  47. //sys ppoll(fds *PollFd, nfds int, timeout *Timespec, sigmask *Sigset_t) (n int, err error)
  48. func Ppoll(fds []PollFd, timeout *Timespec, sigmask *Sigset_t) (n int, err error) {
  49. if len(fds) == 0 {
  50. return ppoll(nil, 0, timeout, sigmask)
  51. }
  52. return ppoll(&fds[0], len(fds), timeout, sigmask)
  53. }
  54. //sys Readlinkat(dirfd int, path string, buf []byte) (n int, err error)
  55. func Readlink(path string, buf []byte) (n int, err error) {
  56. return Readlinkat(AT_FDCWD, path, buf)
  57. }
  58. func Rename(oldpath string, newpath string) (err error) {
  59. return Renameat(AT_FDCWD, oldpath, AT_FDCWD, newpath)
  60. }
  61. func Rmdir(path string) error {
  62. return Unlinkat(AT_FDCWD, path, AT_REMOVEDIR)
  63. }
  64. //sys Symlinkat(oldpath string, newdirfd int, newpath string) (err error)
  65. func Symlink(oldpath string, newpath string) (err error) {
  66. return Symlinkat(oldpath, AT_FDCWD, newpath)
  67. }
  68. func Unlink(path string) error {
  69. return Unlinkat(AT_FDCWD, path, 0)
  70. }
  71. //sys Unlinkat(dirfd int, path string, flags int) (err error)
  72. //sys utimes(path string, times *[2]Timeval) (err error)
  73. func Utimes(path string, tv []Timeval) error {
  74. if tv == nil {
  75. err := utimensat(AT_FDCWD, path, nil, 0)
  76. if err != ENOSYS {
  77. return err
  78. }
  79. return utimes(path, nil)
  80. }
  81. if len(tv) != 2 {
  82. return EINVAL
  83. }
  84. var ts [2]Timespec
  85. ts[0] = NsecToTimespec(TimevalToNsec(tv[0]))
  86. ts[1] = NsecToTimespec(TimevalToNsec(tv[1]))
  87. err := utimensat(AT_FDCWD, path, (*[2]Timespec)(unsafe.Pointer(&ts[0])), 0)
  88. if err != ENOSYS {
  89. return err
  90. }
  91. return utimes(path, (*[2]Timeval)(unsafe.Pointer(&tv[0])))
  92. }
  93. //sys utimensat(dirfd int, path string, times *[2]Timespec, flags int) (err error)
  94. func UtimesNano(path string, ts []Timespec) error {
  95. if ts == nil {
  96. err := utimensat(AT_FDCWD, path, nil, 0)
  97. if err != ENOSYS {
  98. return err
  99. }
  100. return utimes(path, nil)
  101. }
  102. if len(ts) != 2 {
  103. return EINVAL
  104. }
  105. err := utimensat(AT_FDCWD, path, (*[2]Timespec)(unsafe.Pointer(&ts[0])), 0)
  106. if err != ENOSYS {
  107. return err
  108. }
  109. // If the utimensat syscall isn't available (utimensat was added to Linux
  110. // in 2.6.22, Released, 8 July 2007) then fall back to utimes
  111. var tv [2]Timeval
  112. for i := 0; i < 2; i++ {
  113. tv[i] = NsecToTimeval(TimespecToNsec(ts[i]))
  114. }
  115. return utimes(path, (*[2]Timeval)(unsafe.Pointer(&tv[0])))
  116. }
  117. func UtimesNanoAt(dirfd int, path string, ts []Timespec, flags int) error {
  118. if ts == nil {
  119. return utimensat(dirfd, path, nil, flags)
  120. }
  121. if len(ts) != 2 {
  122. return EINVAL
  123. }
  124. return utimensat(dirfd, path, (*[2]Timespec)(unsafe.Pointer(&ts[0])), flags)
  125. }
  126. //sys futimesat(dirfd int, path *byte, times *[2]Timeval) (err error)
  127. func Futimesat(dirfd int, path string, tv []Timeval) error {
  128. pathp, err := BytePtrFromString(path)
  129. if err != nil {
  130. return err
  131. }
  132. if tv == nil {
  133. return futimesat(dirfd, pathp, nil)
  134. }
  135. if len(tv) != 2 {
  136. return EINVAL
  137. }
  138. return futimesat(dirfd, pathp, (*[2]Timeval)(unsafe.Pointer(&tv[0])))
  139. }
  140. func Futimes(fd int, tv []Timeval) (err error) {
  141. // Believe it or not, this is the best we can do on Linux
  142. // (and is what glibc does).
  143. return Utimes("/proc/self/fd/"+itoa(fd), tv)
  144. }
  145. const ImplementsGetwd = true
  146. //sys Getcwd(buf []byte) (n int, err error)
  147. func Getwd() (wd string, err error) {
  148. var buf [PathMax]byte
  149. n, err := Getcwd(buf[0:])
  150. if err != nil {
  151. return "", err
  152. }
  153. // Getcwd returns the number of bytes written to buf, including the NUL.
  154. if n < 1 || n > len(buf) || buf[n-1] != 0 {
  155. return "", EINVAL
  156. }
  157. return string(buf[0 : n-1]), nil
  158. }
  159. func Getgroups() (gids []int, err error) {
  160. n, err := getgroups(0, nil)
  161. if err != nil {
  162. return nil, err
  163. }
  164. if n == 0 {
  165. return nil, nil
  166. }
  167. // Sanity check group count. Max is 1<<16 on Linux.
  168. if n < 0 || n > 1<<20 {
  169. return nil, EINVAL
  170. }
  171. a := make([]_Gid_t, n)
  172. n, err = getgroups(n, &a[0])
  173. if err != nil {
  174. return nil, err
  175. }
  176. gids = make([]int, n)
  177. for i, v := range a[0:n] {
  178. gids[i] = int(v)
  179. }
  180. return
  181. }
  182. func Setgroups(gids []int) (err error) {
  183. if len(gids) == 0 {
  184. return setgroups(0, nil)
  185. }
  186. a := make([]_Gid_t, len(gids))
  187. for i, v := range gids {
  188. a[i] = _Gid_t(v)
  189. }
  190. return setgroups(len(a), &a[0])
  191. }
  192. type WaitStatus uint32
  193. // Wait status is 7 bits at bottom, either 0 (exited),
  194. // 0x7F (stopped), or a signal number that caused an exit.
  195. // The 0x80 bit is whether there was a core dump.
  196. // An extra number (exit code, signal causing a stop)
  197. // is in the high bits. At least that's the idea.
  198. // There are various irregularities. For example, the
  199. // "continued" status is 0xFFFF, distinguishing itself
  200. // from stopped via the core dump bit.
  201. const (
  202. mask = 0x7F
  203. core = 0x80
  204. exited = 0x00
  205. stopped = 0x7F
  206. shift = 8
  207. )
  208. func (w WaitStatus) Exited() bool { return w&mask == exited }
  209. func (w WaitStatus) Signaled() bool { return w&mask != stopped && w&mask != exited }
  210. func (w WaitStatus) Stopped() bool { return w&0xFF == stopped }
  211. func (w WaitStatus) Continued() bool { return w == 0xFFFF }
  212. func (w WaitStatus) CoreDump() bool { return w.Signaled() && w&core != 0 }
  213. func (w WaitStatus) ExitStatus() int {
  214. if !w.Exited() {
  215. return -1
  216. }
  217. return int(w>>shift) & 0xFF
  218. }
  219. func (w WaitStatus) Signal() syscall.Signal {
  220. if !w.Signaled() {
  221. return -1
  222. }
  223. return syscall.Signal(w & mask)
  224. }
  225. func (w WaitStatus) StopSignal() syscall.Signal {
  226. if !w.Stopped() {
  227. return -1
  228. }
  229. return syscall.Signal(w>>shift) & 0xFF
  230. }
  231. func (w WaitStatus) TrapCause() int {
  232. if w.StopSignal() != SIGTRAP {
  233. return -1
  234. }
  235. return int(w>>shift) >> 8
  236. }
  237. //sys wait4(pid int, wstatus *_C_int, options int, rusage *Rusage) (wpid int, err error)
  238. func Wait4(pid int, wstatus *WaitStatus, options int, rusage *Rusage) (wpid int, err error) {
  239. var status _C_int
  240. wpid, err = wait4(pid, &status, options, rusage)
  241. if wstatus != nil {
  242. *wstatus = WaitStatus(status)
  243. }
  244. return
  245. }
  246. func Mkfifo(path string, mode uint32) (err error) {
  247. return Mknod(path, mode|S_IFIFO, 0)
  248. }
  249. func (sa *SockaddrInet4) sockaddr() (unsafe.Pointer, _Socklen, error) {
  250. if sa.Port < 0 || sa.Port > 0xFFFF {
  251. return nil, 0, EINVAL
  252. }
  253. sa.raw.Family = AF_INET
  254. p := (*[2]byte)(unsafe.Pointer(&sa.raw.Port))
  255. p[0] = byte(sa.Port >> 8)
  256. p[1] = byte(sa.Port)
  257. for i := 0; i < len(sa.Addr); i++ {
  258. sa.raw.Addr[i] = sa.Addr[i]
  259. }
  260. return unsafe.Pointer(&sa.raw), SizeofSockaddrInet4, nil
  261. }
  262. func (sa *SockaddrInet6) sockaddr() (unsafe.Pointer, _Socklen, error) {
  263. if sa.Port < 0 || sa.Port > 0xFFFF {
  264. return nil, 0, EINVAL
  265. }
  266. sa.raw.Family = AF_INET6
  267. p := (*[2]byte)(unsafe.Pointer(&sa.raw.Port))
  268. p[0] = byte(sa.Port >> 8)
  269. p[1] = byte(sa.Port)
  270. sa.raw.Scope_id = sa.ZoneId
  271. for i := 0; i < len(sa.Addr); i++ {
  272. sa.raw.Addr[i] = sa.Addr[i]
  273. }
  274. return unsafe.Pointer(&sa.raw), SizeofSockaddrInet6, nil
  275. }
  276. func (sa *SockaddrUnix) sockaddr() (unsafe.Pointer, _Socklen, error) {
  277. name := sa.Name
  278. n := len(name)
  279. if n >= len(sa.raw.Path) {
  280. return nil, 0, EINVAL
  281. }
  282. sa.raw.Family = AF_UNIX
  283. for i := 0; i < n; i++ {
  284. sa.raw.Path[i] = int8(name[i])
  285. }
  286. // length is family (uint16), name, NUL.
  287. sl := _Socklen(2)
  288. if n > 0 {
  289. sl += _Socklen(n) + 1
  290. }
  291. if sa.raw.Path[0] == '@' {
  292. sa.raw.Path[0] = 0
  293. // Don't count trailing NUL for abstract address.
  294. sl--
  295. }
  296. return unsafe.Pointer(&sa.raw), sl, nil
  297. }
  298. type SockaddrLinklayer struct {
  299. Protocol uint16
  300. Ifindex int
  301. Hatype uint16
  302. Pkttype uint8
  303. Halen uint8
  304. Addr [8]byte
  305. raw RawSockaddrLinklayer
  306. }
  307. func (sa *SockaddrLinklayer) sockaddr() (unsafe.Pointer, _Socklen, error) {
  308. if sa.Ifindex < 0 || sa.Ifindex > 0x7fffffff {
  309. return nil, 0, EINVAL
  310. }
  311. sa.raw.Family = AF_PACKET
  312. sa.raw.Protocol = sa.Protocol
  313. sa.raw.Ifindex = int32(sa.Ifindex)
  314. sa.raw.Hatype = sa.Hatype
  315. sa.raw.Pkttype = sa.Pkttype
  316. sa.raw.Halen = sa.Halen
  317. for i := 0; i < len(sa.Addr); i++ {
  318. sa.raw.Addr[i] = sa.Addr[i]
  319. }
  320. return unsafe.Pointer(&sa.raw), SizeofSockaddrLinklayer, nil
  321. }
  322. type SockaddrNetlink struct {
  323. Family uint16
  324. Pad uint16
  325. Pid uint32
  326. Groups uint32
  327. raw RawSockaddrNetlink
  328. }
  329. func (sa *SockaddrNetlink) sockaddr() (unsafe.Pointer, _Socklen, error) {
  330. sa.raw.Family = AF_NETLINK
  331. sa.raw.Pad = sa.Pad
  332. sa.raw.Pid = sa.Pid
  333. sa.raw.Groups = sa.Groups
  334. return unsafe.Pointer(&sa.raw), SizeofSockaddrNetlink, nil
  335. }
  336. type SockaddrHCI struct {
  337. Dev uint16
  338. Channel uint16
  339. raw RawSockaddrHCI
  340. }
  341. func (sa *SockaddrHCI) sockaddr() (unsafe.Pointer, _Socklen, error) {
  342. sa.raw.Family = AF_BLUETOOTH
  343. sa.raw.Dev = sa.Dev
  344. sa.raw.Channel = sa.Channel
  345. return unsafe.Pointer(&sa.raw), SizeofSockaddrHCI, nil
  346. }
  347. func anyToSockaddr(rsa *RawSockaddrAny) (Sockaddr, error) {
  348. switch rsa.Addr.Family {
  349. case AF_NETLINK:
  350. pp := (*RawSockaddrNetlink)(unsafe.Pointer(rsa))
  351. sa := new(SockaddrNetlink)
  352. sa.Family = pp.Family
  353. sa.Pad = pp.Pad
  354. sa.Pid = pp.Pid
  355. sa.Groups = pp.Groups
  356. return sa, nil
  357. case AF_PACKET:
  358. pp := (*RawSockaddrLinklayer)(unsafe.Pointer(rsa))
  359. sa := new(SockaddrLinklayer)
  360. sa.Protocol = pp.Protocol
  361. sa.Ifindex = int(pp.Ifindex)
  362. sa.Hatype = pp.Hatype
  363. sa.Pkttype = pp.Pkttype
  364. sa.Halen = pp.Halen
  365. for i := 0; i < len(sa.Addr); i++ {
  366. sa.Addr[i] = pp.Addr[i]
  367. }
  368. return sa, nil
  369. case AF_UNIX:
  370. pp := (*RawSockaddrUnix)(unsafe.Pointer(rsa))
  371. sa := new(SockaddrUnix)
  372. if pp.Path[0] == 0 {
  373. // "Abstract" Unix domain socket.
  374. // Rewrite leading NUL as @ for textual display.
  375. // (This is the standard convention.)
  376. // Not friendly to overwrite in place,
  377. // but the callers below don't care.
  378. pp.Path[0] = '@'
  379. }
  380. // Assume path ends at NUL.
  381. // This is not technically the Linux semantics for
  382. // abstract Unix domain sockets--they are supposed
  383. // to be uninterpreted fixed-size binary blobs--but
  384. // everyone uses this convention.
  385. n := 0
  386. for n < len(pp.Path) && pp.Path[n] != 0 {
  387. n++
  388. }
  389. bytes := (*[10000]byte)(unsafe.Pointer(&pp.Path[0]))[0:n]
  390. sa.Name = string(bytes)
  391. return sa, nil
  392. case AF_INET:
  393. pp := (*RawSockaddrInet4)(unsafe.Pointer(rsa))
  394. sa := new(SockaddrInet4)
  395. p := (*[2]byte)(unsafe.Pointer(&pp.Port))
  396. sa.Port = int(p[0])<<8 + int(p[1])
  397. for i := 0; i < len(sa.Addr); i++ {
  398. sa.Addr[i] = pp.Addr[i]
  399. }
  400. return sa, nil
  401. case AF_INET6:
  402. pp := (*RawSockaddrInet6)(unsafe.Pointer(rsa))
  403. sa := new(SockaddrInet6)
  404. p := (*[2]byte)(unsafe.Pointer(&pp.Port))
  405. sa.Port = int(p[0])<<8 + int(p[1])
  406. sa.ZoneId = pp.Scope_id
  407. for i := 0; i < len(sa.Addr); i++ {
  408. sa.Addr[i] = pp.Addr[i]
  409. }
  410. return sa, nil
  411. }
  412. return nil, EAFNOSUPPORT
  413. }
  414. func Accept(fd int) (nfd int, sa Sockaddr, err error) {
  415. var rsa RawSockaddrAny
  416. var len _Socklen = SizeofSockaddrAny
  417. nfd, err = accept(fd, &rsa, &len)
  418. if err != nil {
  419. return
  420. }
  421. sa, err = anyToSockaddr(&rsa)
  422. if err != nil {
  423. Close(nfd)
  424. nfd = 0
  425. }
  426. return
  427. }
  428. func Accept4(fd int, flags int) (nfd int, sa Sockaddr, err error) {
  429. var rsa RawSockaddrAny
  430. var len _Socklen = SizeofSockaddrAny
  431. nfd, err = accept4(fd, &rsa, &len, flags)
  432. if err != nil {
  433. return
  434. }
  435. if len > SizeofSockaddrAny {
  436. panic("RawSockaddrAny too small")
  437. }
  438. sa, err = anyToSockaddr(&rsa)
  439. if err != nil {
  440. Close(nfd)
  441. nfd = 0
  442. }
  443. return
  444. }
  445. func Getsockname(fd int) (sa Sockaddr, err error) {
  446. var rsa RawSockaddrAny
  447. var len _Socklen = SizeofSockaddrAny
  448. if err = getsockname(fd, &rsa, &len); err != nil {
  449. return
  450. }
  451. return anyToSockaddr(&rsa)
  452. }
  453. func GetsockoptInet4Addr(fd, level, opt int) (value [4]byte, err error) {
  454. vallen := _Socklen(4)
  455. err = getsockopt(fd, level, opt, unsafe.Pointer(&value[0]), &vallen)
  456. return value, err
  457. }
  458. func GetsockoptIPMreq(fd, level, opt int) (*IPMreq, error) {
  459. var value IPMreq
  460. vallen := _Socklen(SizeofIPMreq)
  461. err := getsockopt(fd, level, opt, unsafe.Pointer(&value), &vallen)
  462. return &value, err
  463. }
  464. func GetsockoptIPMreqn(fd, level, opt int) (*IPMreqn, error) {
  465. var value IPMreqn
  466. vallen := _Socklen(SizeofIPMreqn)
  467. err := getsockopt(fd, level, opt, unsafe.Pointer(&value), &vallen)
  468. return &value, err
  469. }
  470. func GetsockoptIPv6Mreq(fd, level, opt int) (*IPv6Mreq, error) {
  471. var value IPv6Mreq
  472. vallen := _Socklen(SizeofIPv6Mreq)
  473. err := getsockopt(fd, level, opt, unsafe.Pointer(&value), &vallen)
  474. return &value, err
  475. }
  476. func GetsockoptIPv6MTUInfo(fd, level, opt int) (*IPv6MTUInfo, error) {
  477. var value IPv6MTUInfo
  478. vallen := _Socklen(SizeofIPv6MTUInfo)
  479. err := getsockopt(fd, level, opt, unsafe.Pointer(&value), &vallen)
  480. return &value, err
  481. }
  482. func GetsockoptICMPv6Filter(fd, level, opt int) (*ICMPv6Filter, error) {
  483. var value ICMPv6Filter
  484. vallen := _Socklen(SizeofICMPv6Filter)
  485. err := getsockopt(fd, level, opt, unsafe.Pointer(&value), &vallen)
  486. return &value, err
  487. }
  488. func GetsockoptUcred(fd, level, opt int) (*Ucred, error) {
  489. var value Ucred
  490. vallen := _Socklen(SizeofUcred)
  491. err := getsockopt(fd, level, opt, unsafe.Pointer(&value), &vallen)
  492. return &value, err
  493. }
  494. func SetsockoptIPMreqn(fd, level, opt int, mreq *IPMreqn) (err error) {
  495. return setsockopt(fd, level, opt, unsafe.Pointer(mreq), unsafe.Sizeof(*mreq))
  496. }
  497. func Recvmsg(fd int, p, oob []byte, flags int) (n, oobn int, recvflags int, from Sockaddr, err error) {
  498. var msg Msghdr
  499. var rsa RawSockaddrAny
  500. msg.Name = (*byte)(unsafe.Pointer(&rsa))
  501. msg.Namelen = uint32(SizeofSockaddrAny)
  502. var iov Iovec
  503. if len(p) > 0 {
  504. iov.Base = (*byte)(unsafe.Pointer(&p[0]))
  505. iov.SetLen(len(p))
  506. }
  507. var dummy byte
  508. if len(oob) > 0 {
  509. // receive at least one normal byte
  510. if len(p) == 0 {
  511. iov.Base = &dummy
  512. iov.SetLen(1)
  513. }
  514. msg.Control = (*byte)(unsafe.Pointer(&oob[0]))
  515. msg.SetControllen(len(oob))
  516. }
  517. msg.Iov = &iov
  518. msg.Iovlen = 1
  519. if n, err = recvmsg(fd, &msg, flags); err != nil {
  520. return
  521. }
  522. oobn = int(msg.Controllen)
  523. recvflags = int(msg.Flags)
  524. // source address is only specified if the socket is unconnected
  525. if rsa.Addr.Family != AF_UNSPEC {
  526. from, err = anyToSockaddr(&rsa)
  527. }
  528. return
  529. }
  530. func Sendmsg(fd int, p, oob []byte, to Sockaddr, flags int) (err error) {
  531. _, err = SendmsgN(fd, p, oob, to, flags)
  532. return
  533. }
  534. func SendmsgN(fd int, p, oob []byte, to Sockaddr, flags int) (n int, err error) {
  535. var ptr unsafe.Pointer
  536. var salen _Socklen
  537. if to != nil {
  538. var err error
  539. ptr, salen, err = to.sockaddr()
  540. if err != nil {
  541. return 0, err
  542. }
  543. }
  544. var msg Msghdr
  545. msg.Name = (*byte)(unsafe.Pointer(ptr))
  546. msg.Namelen = uint32(salen)
  547. var iov Iovec
  548. if len(p) > 0 {
  549. iov.Base = (*byte)(unsafe.Pointer(&p[0]))
  550. iov.SetLen(len(p))
  551. }
  552. var dummy byte
  553. if len(oob) > 0 {
  554. // send at least one normal byte
  555. if len(p) == 0 {
  556. iov.Base = &dummy
  557. iov.SetLen(1)
  558. }
  559. msg.Control = (*byte)(unsafe.Pointer(&oob[0]))
  560. msg.SetControllen(len(oob))
  561. }
  562. msg.Iov = &iov
  563. msg.Iovlen = 1
  564. if n, err = sendmsg(fd, &msg, flags); err != nil {
  565. return 0, err
  566. }
  567. if len(oob) > 0 && len(p) == 0 {
  568. n = 0
  569. }
  570. return n, nil
  571. }
  572. // BindToDevice binds the socket associated with fd to device.
  573. func BindToDevice(fd int, device string) (err error) {
  574. return SetsockoptString(fd, SOL_SOCKET, SO_BINDTODEVICE, device)
  575. }
  576. //sys ptrace(request int, pid int, addr uintptr, data uintptr) (err error)
  577. func ptracePeek(req int, pid int, addr uintptr, out []byte) (count int, err error) {
  578. // The peek requests are machine-size oriented, so we wrap it
  579. // to retrieve arbitrary-length data.
  580. // The ptrace syscall differs from glibc's ptrace.
  581. // Peeks returns the word in *data, not as the return value.
  582. var buf [sizeofPtr]byte
  583. // Leading edge. PEEKTEXT/PEEKDATA don't require aligned
  584. // access (PEEKUSER warns that it might), but if we don't
  585. // align our reads, we might straddle an unmapped page
  586. // boundary and not get the bytes leading up to the page
  587. // boundary.
  588. n := 0
  589. if addr%sizeofPtr != 0 {
  590. err = ptrace(req, pid, addr-addr%sizeofPtr, uintptr(unsafe.Pointer(&buf[0])))
  591. if err != nil {
  592. return 0, err
  593. }
  594. n += copy(out, buf[addr%sizeofPtr:])
  595. out = out[n:]
  596. }
  597. // Remainder.
  598. for len(out) > 0 {
  599. // We use an internal buffer to guarantee alignment.
  600. // It's not documented if this is necessary, but we're paranoid.
  601. err = ptrace(req, pid, addr+uintptr(n), uintptr(unsafe.Pointer(&buf[0])))
  602. if err != nil {
  603. return n, err
  604. }
  605. copied := copy(out, buf[0:])
  606. n += copied
  607. out = out[copied:]
  608. }
  609. return n, nil
  610. }
  611. func PtracePeekText(pid int, addr uintptr, out []byte) (count int, err error) {
  612. return ptracePeek(PTRACE_PEEKTEXT, pid, addr, out)
  613. }
  614. func PtracePeekData(pid int, addr uintptr, out []byte) (count int, err error) {
  615. return ptracePeek(PTRACE_PEEKDATA, pid, addr, out)
  616. }
  617. func ptracePoke(pokeReq int, peekReq int, pid int, addr uintptr, data []byte) (count int, err error) {
  618. // As for ptracePeek, we need to align our accesses to deal
  619. // with the possibility of straddling an invalid page.
  620. // Leading edge.
  621. n := 0
  622. if addr%sizeofPtr != 0 {
  623. var buf [sizeofPtr]byte
  624. err = ptrace(peekReq, pid, addr-addr%sizeofPtr, uintptr(unsafe.Pointer(&buf[0])))
  625. if err != nil {
  626. return 0, err
  627. }
  628. n += copy(buf[addr%sizeofPtr:], data)
  629. word := *((*uintptr)(unsafe.Pointer(&buf[0])))
  630. err = ptrace(pokeReq, pid, addr-addr%sizeofPtr, word)
  631. if err != nil {
  632. return 0, err
  633. }
  634. data = data[n:]
  635. }
  636. // Interior.
  637. for len(data) > sizeofPtr {
  638. word := *((*uintptr)(unsafe.Pointer(&data[0])))
  639. err = ptrace(pokeReq, pid, addr+uintptr(n), word)
  640. if err != nil {
  641. return n, err
  642. }
  643. n += sizeofPtr
  644. data = data[sizeofPtr:]
  645. }
  646. // Trailing edge.
  647. if len(data) > 0 {
  648. var buf [sizeofPtr]byte
  649. err = ptrace(peekReq, pid, addr+uintptr(n), uintptr(unsafe.Pointer(&buf[0])))
  650. if err != nil {
  651. return n, err
  652. }
  653. copy(buf[0:], data)
  654. word := *((*uintptr)(unsafe.Pointer(&buf[0])))
  655. err = ptrace(pokeReq, pid, addr+uintptr(n), word)
  656. if err != nil {
  657. return n, err
  658. }
  659. n += len(data)
  660. }
  661. return n, nil
  662. }
  663. func PtracePokeText(pid int, addr uintptr, data []byte) (count int, err error) {
  664. return ptracePoke(PTRACE_POKETEXT, PTRACE_PEEKTEXT, pid, addr, data)
  665. }
  666. func PtracePokeData(pid int, addr uintptr, data []byte) (count int, err error) {
  667. return ptracePoke(PTRACE_POKEDATA, PTRACE_PEEKDATA, pid, addr, data)
  668. }
  669. func PtraceGetRegs(pid int, regsout *PtraceRegs) (err error) {
  670. return ptrace(PTRACE_GETREGS, pid, 0, uintptr(unsafe.Pointer(regsout)))
  671. }
  672. func PtraceSetRegs(pid int, regs *PtraceRegs) (err error) {
  673. return ptrace(PTRACE_SETREGS, pid, 0, uintptr(unsafe.Pointer(regs)))
  674. }
  675. func PtraceSetOptions(pid int, options int) (err error) {
  676. return ptrace(PTRACE_SETOPTIONS, pid, 0, uintptr(options))
  677. }
  678. func PtraceGetEventMsg(pid int) (msg uint, err error) {
  679. var data _C_long
  680. err = ptrace(PTRACE_GETEVENTMSG, pid, 0, uintptr(unsafe.Pointer(&data)))
  681. msg = uint(data)
  682. return
  683. }
  684. func PtraceCont(pid int, signal int) (err error) {
  685. return ptrace(PTRACE_CONT, pid, 0, uintptr(signal))
  686. }
  687. func PtraceSyscall(pid int, signal int) (err error) {
  688. return ptrace(PTRACE_SYSCALL, pid, 0, uintptr(signal))
  689. }
  690. func PtraceSingleStep(pid int) (err error) { return ptrace(PTRACE_SINGLESTEP, pid, 0, 0) }
  691. func PtraceAttach(pid int) (err error) { return ptrace(PTRACE_ATTACH, pid, 0, 0) }
  692. func PtraceDetach(pid int) (err error) { return ptrace(PTRACE_DETACH, pid, 0, 0) }
  693. //sys reboot(magic1 uint, magic2 uint, cmd int, arg string) (err error)
  694. func Reboot(cmd int) (err error) {
  695. return reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, cmd, "")
  696. }
  697. func clen(n []byte) int {
  698. for i := 0; i < len(n); i++ {
  699. if n[i] == 0 {
  700. return i
  701. }
  702. }
  703. return len(n)
  704. }
  705. func ReadDirent(fd int, buf []byte) (n int, err error) {
  706. return Getdents(fd, buf)
  707. }
  708. func ParseDirent(buf []byte, max int, names []string) (consumed int, count int, newnames []string) {
  709. origlen := len(buf)
  710. count = 0
  711. for max != 0 && len(buf) > 0 {
  712. dirent := (*Dirent)(unsafe.Pointer(&buf[0]))
  713. buf = buf[dirent.Reclen:]
  714. if dirent.Ino == 0 { // File absent in directory.
  715. continue
  716. }
  717. bytes := (*[10000]byte)(unsafe.Pointer(&dirent.Name[0]))
  718. var name = string(bytes[0:clen(bytes[:])])
  719. if name == "." || name == ".." { // Useless names
  720. continue
  721. }
  722. max--
  723. count++
  724. names = append(names, name)
  725. }
  726. return origlen - len(buf), count, names
  727. }
  728. //sys mount(source string, target string, fstype string, flags uintptr, data *byte) (err error)
  729. func Mount(source string, target string, fstype string, flags uintptr, data string) (err error) {
  730. // Certain file systems get rather angry and EINVAL if you give
  731. // them an empty string of data, rather than NULL.
  732. if data == "" {
  733. return mount(source, target, fstype, flags, nil)
  734. }
  735. datap, err := BytePtrFromString(data)
  736. if err != nil {
  737. return err
  738. }
  739. return mount(source, target, fstype, flags, datap)
  740. }
  741. // Sendto
  742. // Recvfrom
  743. // Socketpair
  744. /*
  745. * Direct access
  746. */
  747. //sys Acct(path string) (err error)
  748. //sys Adjtimex(buf *Timex) (state int, err error)
  749. //sys Chdir(path string) (err error)
  750. //sys Chroot(path string) (err error)
  751. //sys ClockGettime(clockid int32, time *Timespec) (err error)
  752. //sys Close(fd int) (err error)
  753. //sys Dup(oldfd int) (fd int, err error)
  754. //sys Dup3(oldfd int, newfd int, flags int) (err error)
  755. //sysnb EpollCreate(size int) (fd int, err error)
  756. //sysnb EpollCreate1(flag int) (fd int, err error)
  757. //sysnb EpollCtl(epfd int, op int, fd int, event *EpollEvent) (err error)
  758. //sys Exit(code int) = SYS_EXIT_GROUP
  759. //sys Faccessat(dirfd int, path string, mode uint32, flags int) (err error)
  760. //sys Fallocate(fd int, mode uint32, off int64, len int64) (err error)
  761. //sys Fchdir(fd int) (err error)
  762. //sys Fchmod(fd int, mode uint32) (err error)
  763. //sys Fchmodat(dirfd int, path string, mode uint32, flags int) (err error)
  764. //sys Fchownat(dirfd int, path string, uid int, gid int, flags int) (err error)
  765. //sys fcntl(fd int, cmd int, arg int) (val int, err error)
  766. //sys Fdatasync(fd int) (err error)
  767. //sys Flock(fd int, how int) (err error)
  768. //sys Fsync(fd int) (err error)
  769. //sys Getdents(fd int, buf []byte) (n int, err error) = SYS_GETDENTS64
  770. //sysnb Getpgid(pid int) (pgid int, err error)
  771. func Getpgrp() (pid int) {
  772. pid, _ = Getpgid(0)
  773. return
  774. }
  775. //sysnb Getpid() (pid int)
  776. //sysnb Getppid() (ppid int)
  777. //sys Getpriority(which int, who int) (prio int, err error)
  778. //sysnb Getrusage(who int, rusage *Rusage) (err error)
  779. //sysnb Getsid(pid int) (sid int, err error)
  780. //sysnb Gettid() (tid int)
  781. //sys Getxattr(path string, attr string, dest []byte) (sz int, err error)
  782. //sys InotifyAddWatch(fd int, pathname string, mask uint32) (watchdesc int, err error)
  783. //sysnb InotifyInit1(flags int) (fd int, err error)
  784. //sysnb InotifyRmWatch(fd int, watchdesc uint32) (success int, err error)
  785. //sysnb Kill(pid int, sig syscall.Signal) (err error)
  786. //sys Klogctl(typ int, buf []byte) (n int, err error) = SYS_SYSLOG
  787. //sys Listxattr(path string, dest []byte) (sz int, err error)
  788. //sys Mkdirat(dirfd int, path string, mode uint32) (err error)
  789. //sys Mknodat(dirfd int, path string, mode uint32, dev int) (err error)
  790. //sys Nanosleep(time *Timespec, leftover *Timespec) (err error)
  791. //sys PivotRoot(newroot string, putold string) (err error) = SYS_PIVOT_ROOT
  792. //sysnb prlimit(pid int, resource int, newlimit *Rlimit, old *Rlimit) (err error) = SYS_PRLIMIT64
  793. //sys Prctl(option int, arg2 uintptr, arg3 uintptr, arg4 uintptr, arg5 uintptr) (err error)
  794. //sys read(fd int, p []byte) (n int, err error)
  795. //sys Removexattr(path string, attr string) (err error)
  796. //sys Renameat(olddirfd int, oldpath string, newdirfd int, newpath string) (err error)
  797. //sys Setdomainname(p []byte) (err error)
  798. //sys Sethostname(p []byte) (err error)
  799. //sysnb Setpgid(pid int, pgid int) (err error)
  800. //sysnb Setsid() (pid int, err error)
  801. //sysnb Settimeofday(tv *Timeval) (err error)
  802. //sys Setns(fd int, nstype int) (err error)
  803. // issue 1435.
  804. // On linux Setuid and Setgid only affects the current thread, not the process.
  805. // This does not match what most callers expect so we must return an error
  806. // here rather than letting the caller think that the call succeeded.
  807. func Setuid(uid int) (err error) {
  808. return EOPNOTSUPP
  809. }
  810. func Setgid(uid int) (err error) {
  811. return EOPNOTSUPP
  812. }
  813. //sys Setpriority(which int, who int, prio int) (err error)
  814. //sys Setxattr(path string, attr string, data []byte, flags int) (err error)
  815. //sys Sync()
  816. //sysnb Sysinfo(info *Sysinfo_t) (err error)
  817. //sys Tee(rfd int, wfd int, len int, flags int) (n int64, err error)
  818. //sysnb Tgkill(tgid int, tid int, sig syscall.Signal) (err error)
  819. //sysnb Times(tms *Tms) (ticks uintptr, err error)
  820. //sysnb Umask(mask int) (oldmask int)
  821. //sysnb Uname(buf *Utsname) (err error)
  822. //sys Unmount(target string, flags int) (err error) = SYS_UMOUNT2
  823. //sys Unshare(flags int) (err error)
  824. //sys Ustat(dev int, ubuf *Ustat_t) (err error)
  825. //sys write(fd int, p []byte) (n int, err error)
  826. //sys exitThread(code int) (err error) = SYS_EXIT
  827. //sys readlen(fd int, p *byte, np int) (n int, err error) = SYS_READ
  828. //sys writelen(fd int, p *byte, np int) (n int, err error) = SYS_WRITE
  829. // mmap varies by architecture; see syscall_linux_*.go.
  830. //sys munmap(addr uintptr, length uintptr) (err error)
  831. var mapper = &mmapper{
  832. active: make(map[*byte][]byte),
  833. mmap: mmap,
  834. munmap: munmap,
  835. }
  836. func Mmap(fd int, offset int64, length int, prot int, flags int) (data []byte, err error) {
  837. return mapper.Mmap(fd, offset, length, prot, flags)
  838. }
  839. func Munmap(b []byte) (err error) {
  840. return mapper.Munmap(b)
  841. }
  842. //sys Madvise(b []byte, advice int) (err error)
  843. //sys Mprotect(b []byte, prot int) (err error)
  844. //sys Mlock(b []byte) (err error)
  845. //sys Munlock(b []byte) (err error)
  846. //sys Mlockall(flags int) (err error)
  847. //sys Munlockall() (err error)
  848. /*
  849. * Unimplemented
  850. */
  851. // AddKey
  852. // AfsSyscall
  853. // Alarm
  854. // ArchPrctl
  855. // Brk
  856. // Capget
  857. // Capset
  858. // ClockGetres
  859. // ClockNanosleep
  860. // ClockSettime
  861. // Clone
  862. // CreateModule
  863. // DeleteModule
  864. // EpollCtlOld
  865. // EpollPwait
  866. // EpollWaitOld
  867. // Eventfd
  868. // Execve
  869. // Fgetxattr
  870. // Flistxattr
  871. // Fork
  872. // Fremovexattr
  873. // Fsetxattr
  874. // Futex
  875. // GetKernelSyms
  876. // GetMempolicy
  877. // GetRobustList
  878. // GetThreadArea
  879. // Getitimer
  880. // Getpmsg
  881. // IoCancel
  882. // IoDestroy
  883. // IoGetevents
  884. // IoSetup
  885. // IoSubmit
  886. // Ioctl
  887. // IoprioGet
  888. // IoprioSet
  889. // KexecLoad
  890. // Keyctl
  891. // Lgetxattr
  892. // Llistxattr
  893. // LookupDcookie
  894. // Lremovexattr
  895. // Lsetxattr
  896. // Mbind
  897. // MigratePages
  898. // Mincore
  899. // ModifyLdt
  900. // Mount
  901. // MovePages
  902. // Mprotect
  903. // MqGetsetattr
  904. // MqNotify
  905. // MqOpen
  906. // MqTimedreceive
  907. // MqTimedsend
  908. // MqUnlink
  909. // Mremap
  910. // Msgctl
  911. // Msgget
  912. // Msgrcv
  913. // Msgsnd
  914. // Msync
  915. // Newfstatat
  916. // Nfsservctl
  917. // Personality
  918. // Pselect6
  919. // Ptrace
  920. // Putpmsg
  921. // QueryModule
  922. // Quotactl
  923. // Readahead
  924. // Readv
  925. // RemapFilePages
  926. // RequestKey
  927. // RestartSyscall
  928. // RtSigaction
  929. // RtSigpending
  930. // RtSigprocmask
  931. // RtSigqueueinfo
  932. // RtSigreturn
  933. // RtSigsuspend
  934. // RtSigtimedwait
  935. // SchedGetPriorityMax
  936. // SchedGetPriorityMin
  937. // SchedGetaffinity
  938. // SchedGetparam
  939. // SchedGetscheduler
  940. // SchedRrGetInterval
  941. // SchedSetaffinity
  942. // SchedSetparam
  943. // SchedYield
  944. // Security
  945. // Semctl
  946. // Semget
  947. // Semop
  948. // Semtimedop
  949. // SetMempolicy
  950. // SetRobustList
  951. // SetThreadArea
  952. // SetTidAddress
  953. // Shmat
  954. // Shmctl
  955. // Shmdt
  956. // Shmget
  957. // Sigaltstack
  958. // Signalfd
  959. // Swapoff
  960. // Swapon
  961. // Sysfs
  962. // TimerCreate
  963. // TimerDelete
  964. // TimerGetoverrun
  965. // TimerGettime
  966. // TimerSettime
  967. // Timerfd
  968. // Tkill (obsolete)
  969. // Tuxcall
  970. // Umount2
  971. // Uselib
  972. // Utimensat
  973. // Vfork
  974. // Vhangup
  975. // Vmsplice
  976. // Vserver
  977. // Waitid
  978. // _Sysctl