syscall_linux.go 27 KB

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