syscall_zos_s390x.go 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829
  1. // Copyright 2020 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. //go:build zos && s390x
  5. // +build zos,s390x
  6. package unix
  7. import (
  8. "bytes"
  9. "runtime"
  10. "sort"
  11. "sync"
  12. "syscall"
  13. "unsafe"
  14. )
  15. const (
  16. O_CLOEXEC = 0 // Dummy value (not supported).
  17. AF_LOCAL = AF_UNIX // AF_LOCAL is an alias for AF_UNIX
  18. )
  19. func syscall_syscall(trap, a1, a2, a3 uintptr) (r1, r2 uintptr, err Errno)
  20. func syscall_rawsyscall(trap, a1, a2, a3 uintptr) (r1, r2 uintptr, err Errno)
  21. func syscall_syscall6(trap, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2 uintptr, err Errno)
  22. func syscall_rawsyscall6(trap, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2 uintptr, err Errno)
  23. func syscall_syscall9(trap, a1, a2, a3, a4, a5, a6, a7, a8, a9 uintptr) (r1, r2 uintptr, err Errno)
  24. func syscall_rawsyscall9(trap, a1, a2, a3, a4, a5, a6, a7, a8, a9 uintptr) (r1, r2 uintptr, err Errno)
  25. func copyStat(stat *Stat_t, statLE *Stat_LE_t) {
  26. stat.Dev = uint64(statLE.Dev)
  27. stat.Ino = uint64(statLE.Ino)
  28. stat.Nlink = uint64(statLE.Nlink)
  29. stat.Mode = uint32(statLE.Mode)
  30. stat.Uid = uint32(statLE.Uid)
  31. stat.Gid = uint32(statLE.Gid)
  32. stat.Rdev = uint64(statLE.Rdev)
  33. stat.Size = statLE.Size
  34. stat.Atim.Sec = int64(statLE.Atim)
  35. stat.Atim.Nsec = 0 //zos doesn't return nanoseconds
  36. stat.Mtim.Sec = int64(statLE.Mtim)
  37. stat.Mtim.Nsec = 0 //zos doesn't return nanoseconds
  38. stat.Ctim.Sec = int64(statLE.Ctim)
  39. stat.Ctim.Nsec = 0 //zos doesn't return nanoseconds
  40. stat.Blksize = int64(statLE.Blksize)
  41. stat.Blocks = statLE.Blocks
  42. }
  43. func svcCall(fnptr unsafe.Pointer, argv *unsafe.Pointer, dsa *uint64)
  44. func svcLoad(name *byte) unsafe.Pointer
  45. func svcUnload(name *byte, fnptr unsafe.Pointer) int64
  46. func (d *Dirent) NameString() string {
  47. if d == nil {
  48. return ""
  49. }
  50. return string(d.Name[:d.Namlen])
  51. }
  52. func (sa *SockaddrInet4) sockaddr() (unsafe.Pointer, _Socklen, error) {
  53. if sa.Port < 0 || sa.Port > 0xFFFF {
  54. return nil, 0, EINVAL
  55. }
  56. sa.raw.Len = SizeofSockaddrInet4
  57. sa.raw.Family = AF_INET
  58. p := (*[2]byte)(unsafe.Pointer(&sa.raw.Port))
  59. p[0] = byte(sa.Port >> 8)
  60. p[1] = byte(sa.Port)
  61. for i := 0; i < len(sa.Addr); i++ {
  62. sa.raw.Addr[i] = sa.Addr[i]
  63. }
  64. return unsafe.Pointer(&sa.raw), _Socklen(sa.raw.Len), nil
  65. }
  66. func (sa *SockaddrInet6) sockaddr() (unsafe.Pointer, _Socklen, error) {
  67. if sa.Port < 0 || sa.Port > 0xFFFF {
  68. return nil, 0, EINVAL
  69. }
  70. sa.raw.Len = SizeofSockaddrInet6
  71. sa.raw.Family = AF_INET6
  72. p := (*[2]byte)(unsafe.Pointer(&sa.raw.Port))
  73. p[0] = byte(sa.Port >> 8)
  74. p[1] = byte(sa.Port)
  75. sa.raw.Scope_id = sa.ZoneId
  76. for i := 0; i < len(sa.Addr); i++ {
  77. sa.raw.Addr[i] = sa.Addr[i]
  78. }
  79. return unsafe.Pointer(&sa.raw), _Socklen(sa.raw.Len), nil
  80. }
  81. func (sa *SockaddrUnix) sockaddr() (unsafe.Pointer, _Socklen, error) {
  82. name := sa.Name
  83. n := len(name)
  84. if n >= len(sa.raw.Path) || n == 0 {
  85. return nil, 0, EINVAL
  86. }
  87. sa.raw.Len = byte(3 + n) // 2 for Family, Len; 1 for NUL
  88. sa.raw.Family = AF_UNIX
  89. for i := 0; i < n; i++ {
  90. sa.raw.Path[i] = int8(name[i])
  91. }
  92. return unsafe.Pointer(&sa.raw), _Socklen(sa.raw.Len), nil
  93. }
  94. func anyToSockaddr(_ int, rsa *RawSockaddrAny) (Sockaddr, error) {
  95. // TODO(neeilan): Implement use of first param (fd)
  96. switch rsa.Addr.Family {
  97. case AF_UNIX:
  98. pp := (*RawSockaddrUnix)(unsafe.Pointer(rsa))
  99. sa := new(SockaddrUnix)
  100. // For z/OS, only replace NUL with @ when the
  101. // length is not zero.
  102. if pp.Len != 0 && pp.Path[0] == 0 {
  103. // "Abstract" Unix domain socket.
  104. // Rewrite leading NUL as @ for textual display.
  105. // (This is the standard convention.)
  106. // Not friendly to overwrite in place,
  107. // but the callers below don't care.
  108. pp.Path[0] = '@'
  109. }
  110. // Assume path ends at NUL.
  111. //
  112. // For z/OS, the length of the name is a field
  113. // in the structure. To be on the safe side, we
  114. // will still scan the name for a NUL but only
  115. // to the length provided in the structure.
  116. //
  117. // This is not technically the Linux semantics for
  118. // abstract Unix domain sockets--they are supposed
  119. // to be uninterpreted fixed-size binary blobs--but
  120. // everyone uses this convention.
  121. n := 0
  122. for n < int(pp.Len) && pp.Path[n] != 0 {
  123. n++
  124. }
  125. bytes := (*[len(pp.Path)]byte)(unsafe.Pointer(&pp.Path[0]))[0:n]
  126. sa.Name = string(bytes)
  127. return sa, nil
  128. case AF_INET:
  129. pp := (*RawSockaddrInet4)(unsafe.Pointer(rsa))
  130. sa := new(SockaddrInet4)
  131. p := (*[2]byte)(unsafe.Pointer(&pp.Port))
  132. sa.Port = int(p[0])<<8 + int(p[1])
  133. for i := 0; i < len(sa.Addr); i++ {
  134. sa.Addr[i] = pp.Addr[i]
  135. }
  136. return sa, nil
  137. case AF_INET6:
  138. pp := (*RawSockaddrInet6)(unsafe.Pointer(rsa))
  139. sa := new(SockaddrInet6)
  140. p := (*[2]byte)(unsafe.Pointer(&pp.Port))
  141. sa.Port = int(p[0])<<8 + int(p[1])
  142. sa.ZoneId = pp.Scope_id
  143. for i := 0; i < len(sa.Addr); i++ {
  144. sa.Addr[i] = pp.Addr[i]
  145. }
  146. return sa, nil
  147. }
  148. return nil, EAFNOSUPPORT
  149. }
  150. func Accept(fd int) (nfd int, sa Sockaddr, err error) {
  151. var rsa RawSockaddrAny
  152. var len _Socklen = SizeofSockaddrAny
  153. nfd, err = accept(fd, &rsa, &len)
  154. if err != nil {
  155. return
  156. }
  157. // TODO(neeilan): Remove 0 in call
  158. sa, err = anyToSockaddr(0, &rsa)
  159. if err != nil {
  160. Close(nfd)
  161. nfd = 0
  162. }
  163. return
  164. }
  165. func (iov *Iovec) SetLen(length int) {
  166. iov.Len = uint64(length)
  167. }
  168. func (msghdr *Msghdr) SetControllen(length int) {
  169. msghdr.Controllen = int32(length)
  170. }
  171. func (cmsg *Cmsghdr) SetLen(length int) {
  172. cmsg.Len = int32(length)
  173. }
  174. //sys fcntl(fd int, cmd int, arg int) (val int, err error)
  175. //sys read(fd int, p []byte) (n int, err error)
  176. //sys readlen(fd int, buf *byte, nbuf int) (n int, err error) = SYS_READ
  177. //sys write(fd int, p []byte) (n int, err error)
  178. //sys accept(s int, rsa *RawSockaddrAny, addrlen *_Socklen) (fd int, err error) = SYS___ACCEPT_A
  179. //sys bind(s int, addr unsafe.Pointer, addrlen _Socklen) (err error) = SYS___BIND_A
  180. //sys connect(s int, addr unsafe.Pointer, addrlen _Socklen) (err error) = SYS___CONNECT_A
  181. //sysnb getgroups(n int, list *_Gid_t) (nn int, err error)
  182. //sysnb setgroups(n int, list *_Gid_t) (err error)
  183. //sys getsockopt(s int, level int, name int, val unsafe.Pointer, vallen *_Socklen) (err error)
  184. //sys setsockopt(s int, level int, name int, val unsafe.Pointer, vallen uintptr) (err error)
  185. //sysnb socket(domain int, typ int, proto int) (fd int, err error)
  186. //sysnb socketpair(domain int, typ int, proto int, fd *[2]int32) (err error)
  187. //sysnb getpeername(fd int, rsa *RawSockaddrAny, addrlen *_Socklen) (err error) = SYS___GETPEERNAME_A
  188. //sysnb getsockname(fd int, rsa *RawSockaddrAny, addrlen *_Socklen) (err error) = SYS___GETSOCKNAME_A
  189. //sys recvfrom(fd int, p []byte, flags int, from *RawSockaddrAny, fromlen *_Socklen) (n int, err error) = SYS___RECVFROM_A
  190. //sys sendto(s int, buf []byte, flags int, to unsafe.Pointer, addrlen _Socklen) (err error) = SYS___SENDTO_A
  191. //sys recvmsg(s int, msg *Msghdr, flags int) (n int, err error) = SYS___RECVMSG_A
  192. //sys sendmsg(s int, msg *Msghdr, flags int) (n int, err error) = SYS___SENDMSG_A
  193. //sys mmap(addr uintptr, length uintptr, prot int, flag int, fd int, pos int64) (ret uintptr, err error) = SYS_MMAP
  194. //sys munmap(addr uintptr, length uintptr) (err error) = SYS_MUNMAP
  195. //sys ioctl(fd int, req uint, arg uintptr) (err error) = SYS_IOCTL
  196. //sys Access(path string, mode uint32) (err error) = SYS___ACCESS_A
  197. //sys Chdir(path string) (err error) = SYS___CHDIR_A
  198. //sys Chown(path string, uid int, gid int) (err error) = SYS___CHOWN_A
  199. //sys Chmod(path string, mode uint32) (err error) = SYS___CHMOD_A
  200. //sys Creat(path string, mode uint32) (fd int, err error) = SYS___CREAT_A
  201. //sys Dup(oldfd int) (fd int, err error)
  202. //sys Dup2(oldfd int, newfd int) (err error)
  203. //sys Errno2() (er2 int) = SYS___ERRNO2
  204. //sys Err2ad() (eadd *int) = SYS___ERR2AD
  205. //sys Exit(code int)
  206. //sys Fchdir(fd int) (err error)
  207. //sys Fchmod(fd int, mode uint32) (err error)
  208. //sys Fchown(fd int, uid int, gid int) (err error)
  209. //sys FcntlInt(fd uintptr, cmd int, arg int) (retval int, err error) = SYS_FCNTL
  210. //sys fstat(fd int, stat *Stat_LE_t) (err error)
  211. func Fstat(fd int, stat *Stat_t) (err error) {
  212. var statLE Stat_LE_t
  213. err = fstat(fd, &statLE)
  214. copyStat(stat, &statLE)
  215. return
  216. }
  217. //sys Fstatvfs(fd int, stat *Statvfs_t) (err error) = SYS_FSTATVFS
  218. //sys Fsync(fd int) (err error)
  219. //sys Ftruncate(fd int, length int64) (err error)
  220. //sys Getpagesize() (pgsize int) = SYS_GETPAGESIZE
  221. //sys Mprotect(b []byte, prot int) (err error) = SYS_MPROTECT
  222. //sys Msync(b []byte, flags int) (err error) = SYS_MSYNC
  223. //sys Poll(fds []PollFd, timeout int) (n int, err error) = SYS_POLL
  224. //sys Times(tms *Tms) (ticks uintptr, err error) = SYS_TIMES
  225. //sys W_Getmntent(buff *byte, size int) (lastsys int, err error) = SYS_W_GETMNTENT
  226. //sys W_Getmntent_A(buff *byte, size int) (lastsys int, err error) = SYS___W_GETMNTENT_A
  227. //sys mount_LE(path string, filesystem string, fstype string, mtm uint32, parmlen int32, parm string) (err error) = SYS___MOUNT_A
  228. //sys unmount(filesystem string, mtm int) (err error) = SYS___UMOUNT_A
  229. //sys Chroot(path string) (err error) = SYS___CHROOT_A
  230. //sys Select(nmsgsfds int, r *FdSet, w *FdSet, e *FdSet, timeout *Timeval) (ret int, err error) = SYS_SELECT
  231. //sysnb Uname(buf *Utsname) (err error) = SYS___UNAME_A
  232. func Ptsname(fd int) (name string, err error) {
  233. r0, _, e1 := syscall_syscall(SYS___PTSNAME_A, uintptr(fd), 0, 0)
  234. name = u2s(unsafe.Pointer(r0))
  235. if e1 != 0 {
  236. err = errnoErr(e1)
  237. }
  238. return
  239. }
  240. func u2s(cstr unsafe.Pointer) string {
  241. str := (*[1024]uint8)(cstr)
  242. i := 0
  243. for str[i] != 0 {
  244. i++
  245. }
  246. return string(str[:i])
  247. }
  248. func Close(fd int) (err error) {
  249. _, _, e1 := syscall_syscall(SYS_CLOSE, uintptr(fd), 0, 0)
  250. for i := 0; e1 == EAGAIN && i < 10; i++ {
  251. _, _, _ = syscall_syscall(SYS_USLEEP, uintptr(10), 0, 0)
  252. _, _, e1 = syscall_syscall(SYS_CLOSE, uintptr(fd), 0, 0)
  253. }
  254. if e1 != 0 {
  255. err = errnoErr(e1)
  256. }
  257. return
  258. }
  259. var mapper = &mmapper{
  260. active: make(map[*byte][]byte),
  261. mmap: mmap,
  262. munmap: munmap,
  263. }
  264. // Dummy function: there are no semantics for Madvise on z/OS
  265. func Madvise(b []byte, advice int) (err error) {
  266. return
  267. }
  268. func Mmap(fd int, offset int64, length int, prot int, flags int) (data []byte, err error) {
  269. return mapper.Mmap(fd, offset, length, prot, flags)
  270. }
  271. func Munmap(b []byte) (err error) {
  272. return mapper.Munmap(b)
  273. }
  274. //sys Gethostname(buf []byte) (err error) = SYS___GETHOSTNAME_A
  275. //sysnb Getegid() (egid int)
  276. //sysnb Geteuid() (uid int)
  277. //sysnb Getgid() (gid int)
  278. //sysnb Getpid() (pid int)
  279. //sysnb Getpgid(pid int) (pgid int, err error) = SYS_GETPGID
  280. func Getpgrp() (pid int) {
  281. pid, _ = Getpgid(0)
  282. return
  283. }
  284. //sysnb Getppid() (pid int)
  285. //sys Getpriority(which int, who int) (prio int, err error)
  286. //sysnb Getrlimit(resource int, rlim *Rlimit) (err error) = SYS_GETRLIMIT
  287. //sysnb getrusage(who int, rusage *rusage_zos) (err error) = SYS_GETRUSAGE
  288. func Getrusage(who int, rusage *Rusage) (err error) {
  289. var ruz rusage_zos
  290. err = getrusage(who, &ruz)
  291. //Only the first two fields of Rusage are set
  292. rusage.Utime.Sec = ruz.Utime.Sec
  293. rusage.Utime.Usec = int64(ruz.Utime.Usec)
  294. rusage.Stime.Sec = ruz.Stime.Sec
  295. rusage.Stime.Usec = int64(ruz.Stime.Usec)
  296. return
  297. }
  298. //sysnb Getsid(pid int) (sid int, err error) = SYS_GETSID
  299. //sysnb Getuid() (uid int)
  300. //sysnb Kill(pid int, sig Signal) (err error)
  301. //sys Lchown(path string, uid int, gid int) (err error) = SYS___LCHOWN_A
  302. //sys Link(path string, link string) (err error) = SYS___LINK_A
  303. //sys Listen(s int, n int) (err error)
  304. //sys lstat(path string, stat *Stat_LE_t) (err error) = SYS___LSTAT_A
  305. func Lstat(path string, stat *Stat_t) (err error) {
  306. var statLE Stat_LE_t
  307. err = lstat(path, &statLE)
  308. copyStat(stat, &statLE)
  309. return
  310. }
  311. //sys Mkdir(path string, mode uint32) (err error) = SYS___MKDIR_A
  312. //sys Mkfifo(path string, mode uint32) (err error) = SYS___MKFIFO_A
  313. //sys Mknod(path string, mode uint32, dev int) (err error) = SYS___MKNOD_A
  314. //sys Pread(fd int, p []byte, offset int64) (n int, err error)
  315. //sys Pwrite(fd int, p []byte, offset int64) (n int, err error)
  316. //sys Readlink(path string, buf []byte) (n int, err error) = SYS___READLINK_A
  317. //sys Rename(from string, to string) (err error) = SYS___RENAME_A
  318. //sys Rmdir(path string) (err error) = SYS___RMDIR_A
  319. //sys Seek(fd int, offset int64, whence int) (off int64, err error) = SYS_LSEEK
  320. //sys Setpriority(which int, who int, prio int) (err error)
  321. //sysnb Setpgid(pid int, pgid int) (err error) = SYS_SETPGID
  322. //sysnb Setrlimit(resource int, lim *Rlimit) (err error)
  323. //sysnb Setregid(rgid int, egid int) (err error) = SYS_SETREGID
  324. //sysnb Setreuid(ruid int, euid int) (err error) = SYS_SETREUID
  325. //sysnb Setsid() (pid int, err error) = SYS_SETSID
  326. //sys Setuid(uid int) (err error) = SYS_SETUID
  327. //sys Setgid(uid int) (err error) = SYS_SETGID
  328. //sys Shutdown(fd int, how int) (err error)
  329. //sys stat(path string, statLE *Stat_LE_t) (err error) = SYS___STAT_A
  330. func Stat(path string, sta *Stat_t) (err error) {
  331. var statLE Stat_LE_t
  332. err = stat(path, &statLE)
  333. copyStat(sta, &statLE)
  334. return
  335. }
  336. //sys Symlink(path string, link string) (err error) = SYS___SYMLINK_A
  337. //sys Sync() = SYS_SYNC
  338. //sys Truncate(path string, length int64) (err error) = SYS___TRUNCATE_A
  339. //sys Tcgetattr(fildes int, termptr *Termios) (err error) = SYS_TCGETATTR
  340. //sys Tcsetattr(fildes int, when int, termptr *Termios) (err error) = SYS_TCSETATTR
  341. //sys Umask(mask int) (oldmask int)
  342. //sys Unlink(path string) (err error) = SYS___UNLINK_A
  343. //sys Utime(path string, utim *Utimbuf) (err error) = SYS___UTIME_A
  344. //sys open(path string, mode int, perm uint32) (fd int, err error) = SYS___OPEN_A
  345. func Open(path string, mode int, perm uint32) (fd int, err error) {
  346. return open(path, mode, perm)
  347. }
  348. func Mkfifoat(dirfd int, path string, mode uint32) (err error) {
  349. wd, err := Getwd()
  350. if err != nil {
  351. return err
  352. }
  353. if err := Fchdir(dirfd); err != nil {
  354. return err
  355. }
  356. defer Chdir(wd)
  357. return Mkfifo(path, mode)
  358. }
  359. //sys remove(path string) (err error)
  360. func Remove(path string) error {
  361. return remove(path)
  362. }
  363. const ImplementsGetwd = true
  364. func Getcwd(buf []byte) (n int, err error) {
  365. var p unsafe.Pointer
  366. if len(buf) > 0 {
  367. p = unsafe.Pointer(&buf[0])
  368. } else {
  369. p = unsafe.Pointer(&_zero)
  370. }
  371. _, _, e := syscall_syscall(SYS___GETCWD_A, uintptr(p), uintptr(len(buf)), 0)
  372. n = clen(buf) + 1
  373. if e != 0 {
  374. err = errnoErr(e)
  375. }
  376. return
  377. }
  378. func Getwd() (wd string, err error) {
  379. var buf [PathMax]byte
  380. n, err := Getcwd(buf[0:])
  381. if err != nil {
  382. return "", err
  383. }
  384. // Getcwd returns the number of bytes written to buf, including the NUL.
  385. if n < 1 || n > len(buf) || buf[n-1] != 0 {
  386. return "", EINVAL
  387. }
  388. return string(buf[0 : n-1]), nil
  389. }
  390. func Getgroups() (gids []int, err error) {
  391. n, err := getgroups(0, nil)
  392. if err != nil {
  393. return nil, err
  394. }
  395. if n == 0 {
  396. return nil, nil
  397. }
  398. // Sanity check group count. Max is 1<<16 on Linux.
  399. if n < 0 || n > 1<<20 {
  400. return nil, EINVAL
  401. }
  402. a := make([]_Gid_t, n)
  403. n, err = getgroups(n, &a[0])
  404. if err != nil {
  405. return nil, err
  406. }
  407. gids = make([]int, n)
  408. for i, v := range a[0:n] {
  409. gids[i] = int(v)
  410. }
  411. return
  412. }
  413. func Setgroups(gids []int) (err error) {
  414. if len(gids) == 0 {
  415. return setgroups(0, nil)
  416. }
  417. a := make([]_Gid_t, len(gids))
  418. for i, v := range gids {
  419. a[i] = _Gid_t(v)
  420. }
  421. return setgroups(len(a), &a[0])
  422. }
  423. func gettid() uint64
  424. func Gettid() (tid int) {
  425. return int(gettid())
  426. }
  427. type WaitStatus uint32
  428. // Wait status is 7 bits at bottom, either 0 (exited),
  429. // 0x7F (stopped), or a signal number that caused an exit.
  430. // The 0x80 bit is whether there was a core dump.
  431. // An extra number (exit code, signal causing a stop)
  432. // is in the high bits. At least that's the idea.
  433. // There are various irregularities. For example, the
  434. // "continued" status is 0xFFFF, distinguishing itself
  435. // from stopped via the core dump bit.
  436. const (
  437. mask = 0x7F
  438. core = 0x80
  439. exited = 0x00
  440. stopped = 0x7F
  441. shift = 8
  442. )
  443. func (w WaitStatus) Exited() bool { return w&mask == exited }
  444. func (w WaitStatus) Signaled() bool { return w&mask != stopped && w&mask != exited }
  445. func (w WaitStatus) Stopped() bool { return w&0xFF == stopped }
  446. func (w WaitStatus) Continued() bool { return w == 0xFFFF }
  447. func (w WaitStatus) CoreDump() bool { return w.Signaled() && w&core != 0 }
  448. func (w WaitStatus) ExitStatus() int {
  449. if !w.Exited() {
  450. return -1
  451. }
  452. return int(w>>shift) & 0xFF
  453. }
  454. func (w WaitStatus) Signal() Signal {
  455. if !w.Signaled() {
  456. return -1
  457. }
  458. return Signal(w & mask)
  459. }
  460. func (w WaitStatus) StopSignal() Signal {
  461. if !w.Stopped() {
  462. return -1
  463. }
  464. return Signal(w>>shift) & 0xFF
  465. }
  466. func (w WaitStatus) TrapCause() int { return -1 }
  467. //sys waitpid(pid int, wstatus *_C_int, options int) (wpid int, err error)
  468. func Wait4(pid int, wstatus *WaitStatus, options int, rusage *Rusage) (wpid int, err error) {
  469. // TODO(mundaym): z/OS doesn't have wait4. I don't think getrusage does what we want.
  470. // At the moment rusage will not be touched.
  471. var status _C_int
  472. wpid, err = waitpid(pid, &status, options)
  473. if wstatus != nil {
  474. *wstatus = WaitStatus(status)
  475. }
  476. return
  477. }
  478. //sysnb gettimeofday(tv *timeval_zos) (err error)
  479. func Gettimeofday(tv *Timeval) (err error) {
  480. var tvz timeval_zos
  481. err = gettimeofday(&tvz)
  482. tv.Sec = tvz.Sec
  483. tv.Usec = int64(tvz.Usec)
  484. return
  485. }
  486. func Time(t *Time_t) (tt Time_t, err error) {
  487. var tv Timeval
  488. err = Gettimeofday(&tv)
  489. if err != nil {
  490. return 0, err
  491. }
  492. if t != nil {
  493. *t = Time_t(tv.Sec)
  494. }
  495. return Time_t(tv.Sec), nil
  496. }
  497. func setTimespec(sec, nsec int64) Timespec {
  498. return Timespec{Sec: sec, Nsec: nsec}
  499. }
  500. func setTimeval(sec, usec int64) Timeval { //fix
  501. return Timeval{Sec: sec, Usec: usec}
  502. }
  503. //sysnb pipe(p *[2]_C_int) (err error)
  504. func Pipe(p []int) (err error) {
  505. if len(p) != 2 {
  506. return EINVAL
  507. }
  508. var pp [2]_C_int
  509. err = pipe(&pp)
  510. p[0] = int(pp[0])
  511. p[1] = int(pp[1])
  512. return
  513. }
  514. //sys utimes(path string, timeval *[2]Timeval) (err error) = SYS___UTIMES_A
  515. func Utimes(path string, tv []Timeval) (err error) {
  516. if len(tv) != 2 {
  517. return EINVAL
  518. }
  519. return utimes(path, (*[2]Timeval)(unsafe.Pointer(&tv[0])))
  520. }
  521. func UtimesNano(path string, ts []Timespec) error {
  522. if len(ts) != 2 {
  523. return EINVAL
  524. }
  525. // Not as efficient as it could be because Timespec and
  526. // Timeval have different types in the different OSes
  527. tv := [2]Timeval{
  528. NsecToTimeval(TimespecToNsec(ts[0])),
  529. NsecToTimeval(TimespecToNsec(ts[1])),
  530. }
  531. return utimes(path, (*[2]Timeval)(unsafe.Pointer(&tv[0])))
  532. }
  533. func Getsockname(fd int) (sa Sockaddr, err error) {
  534. var rsa RawSockaddrAny
  535. var len _Socklen = SizeofSockaddrAny
  536. if err = getsockname(fd, &rsa, &len); err != nil {
  537. return
  538. }
  539. // TODO(neeilan) : Remove this 0 ( added to get sys/unix compiling on z/OS )
  540. return anyToSockaddr(0, &rsa)
  541. }
  542. const (
  543. // identifier constants
  544. nwmHeaderIdentifier = 0xd5e6d4c8
  545. nwmFilterIdentifier = 0xd5e6d4c6
  546. nwmTCPConnIdentifier = 0xd5e6d4c3
  547. nwmRecHeaderIdentifier = 0xd5e6d4d9
  548. nwmIPStatsIdentifier = 0xd5e6d4c9d7e2e340
  549. nwmIPGStatsIdentifier = 0xd5e6d4c9d7c7e2e3
  550. nwmTCPStatsIdentifier = 0xd5e6d4e3c3d7e2e3
  551. nwmUDPStatsIdentifier = 0xd5e6d4e4c4d7e2e3
  552. nwmICMPGStatsEntry = 0xd5e6d4c9c3d4d7c7
  553. nwmICMPTStatsEntry = 0xd5e6d4c9c3d4d7e3
  554. // nwmHeader constants
  555. nwmVersion1 = 1
  556. nwmVersion2 = 2
  557. nwmCurrentVer = 2
  558. nwmTCPConnType = 1
  559. nwmGlobalStatsType = 14
  560. // nwmFilter constants
  561. nwmFilterLclAddrMask = 0x20000000 // Local address
  562. nwmFilterSrcAddrMask = 0x20000000 // Source address
  563. nwmFilterLclPortMask = 0x10000000 // Local port
  564. nwmFilterSrcPortMask = 0x10000000 // Source port
  565. // nwmConnEntry constants
  566. nwmTCPStateClosed = 1
  567. nwmTCPStateListen = 2
  568. nwmTCPStateSynSent = 3
  569. nwmTCPStateSynRcvd = 4
  570. nwmTCPStateEstab = 5
  571. nwmTCPStateFinWait1 = 6
  572. nwmTCPStateFinWait2 = 7
  573. nwmTCPStateClosWait = 8
  574. nwmTCPStateLastAck = 9
  575. nwmTCPStateClosing = 10
  576. nwmTCPStateTimeWait = 11
  577. nwmTCPStateDeletTCB = 12
  578. // Existing constants on linux
  579. BPF_TCP_CLOSE = 1
  580. BPF_TCP_LISTEN = 2
  581. BPF_TCP_SYN_SENT = 3
  582. BPF_TCP_SYN_RECV = 4
  583. BPF_TCP_ESTABLISHED = 5
  584. BPF_TCP_FIN_WAIT1 = 6
  585. BPF_TCP_FIN_WAIT2 = 7
  586. BPF_TCP_CLOSE_WAIT = 8
  587. BPF_TCP_LAST_ACK = 9
  588. BPF_TCP_CLOSING = 10
  589. BPF_TCP_TIME_WAIT = 11
  590. BPF_TCP_NEW_SYN_RECV = -1
  591. BPF_TCP_MAX_STATES = -2
  592. )
  593. type nwmTriplet struct {
  594. offset uint32
  595. length uint32
  596. number uint32
  597. }
  598. type nwmQuadruplet struct {
  599. offset uint32
  600. length uint32
  601. number uint32
  602. match uint32
  603. }
  604. type nwmHeader struct {
  605. ident uint32
  606. length uint32
  607. version uint16
  608. nwmType uint16
  609. bytesNeeded uint32
  610. options uint32
  611. _ [16]byte
  612. inputDesc nwmTriplet
  613. outputDesc nwmQuadruplet
  614. }
  615. type nwmFilter struct {
  616. ident uint32
  617. flags uint32
  618. resourceName [8]byte
  619. resourceId uint32
  620. listenerId uint32
  621. local [28]byte // union of sockaddr4 and sockaddr6
  622. remote [28]byte // union of sockaddr4 and sockaddr6
  623. _ uint16
  624. _ uint16
  625. asid uint16
  626. _ [2]byte
  627. tnLuName [8]byte
  628. tnMonGrp uint32
  629. tnAppl [8]byte
  630. applData [40]byte
  631. nInterface [16]byte
  632. dVipa [16]byte
  633. dVipaPfx uint16
  634. dVipaPort uint16
  635. dVipaFamily byte
  636. _ [3]byte
  637. destXCF [16]byte
  638. destXCFPfx uint16
  639. destXCFFamily byte
  640. _ [1]byte
  641. targIP [16]byte
  642. targIPPfx uint16
  643. targIPFamily byte
  644. _ [1]byte
  645. _ [20]byte
  646. }
  647. type nwmRecHeader struct {
  648. ident uint32
  649. length uint32
  650. number byte
  651. _ [3]byte
  652. }
  653. type nwmTCPStatsEntry struct {
  654. ident uint64
  655. currEstab uint32
  656. activeOpened uint32
  657. passiveOpened uint32
  658. connClosed uint32
  659. estabResets uint32
  660. attemptFails uint32
  661. passiveDrops uint32
  662. timeWaitReused uint32
  663. inSegs uint64
  664. predictAck uint32
  665. predictData uint32
  666. inDupAck uint32
  667. inBadSum uint32
  668. inBadLen uint32
  669. inShort uint32
  670. inDiscOldTime uint32
  671. inAllBeforeWin uint32
  672. inSomeBeforeWin uint32
  673. inAllAfterWin uint32
  674. inSomeAfterWin uint32
  675. inOutOfOrder uint32
  676. inAfterClose uint32
  677. inWinProbes uint32
  678. inWinUpdates uint32
  679. outWinUpdates uint32
  680. outSegs uint64
  681. outDelayAcks uint32
  682. outRsts uint32
  683. retransSegs uint32
  684. retransTimeouts uint32
  685. retransDrops uint32
  686. pmtuRetrans uint32
  687. pmtuErrors uint32
  688. outWinProbes uint32
  689. probeDrops uint32
  690. keepAliveProbes uint32
  691. keepAliveDrops uint32
  692. finwait2Drops uint32
  693. acceptCount uint64
  694. inBulkQSegs uint64
  695. inDiscards uint64
  696. connFloods uint32
  697. connStalls uint32
  698. cfgEphemDef uint16
  699. ephemInUse uint16
  700. ephemHiWater uint16
  701. flags byte
  702. _ [1]byte
  703. ephemExhaust uint32
  704. smcRCurrEstabLnks uint32
  705. smcRLnkActTimeOut uint32
  706. smcRActLnkOpened uint32
  707. smcRPasLnkOpened uint32
  708. smcRLnksClosed uint32
  709. smcRCurrEstab uint32
  710. smcRActiveOpened uint32
  711. smcRPassiveOpened uint32
  712. smcRConnClosed uint32
  713. smcRInSegs uint64
  714. smcROutSegs uint64
  715. smcRInRsts uint32
  716. smcROutRsts uint32
  717. smcDCurrEstabLnks uint32
  718. smcDActLnkOpened uint32
  719. smcDPasLnkOpened uint32
  720. smcDLnksClosed uint32
  721. smcDCurrEstab uint32
  722. smcDActiveOpened uint32
  723. smcDPassiveOpened uint32
  724. smcDConnClosed uint32
  725. smcDInSegs uint64
  726. smcDOutSegs uint64
  727. smcDInRsts uint32
  728. smcDOutRsts uint32
  729. }
  730. type nwmConnEntry struct {
  731. ident uint32
  732. local [28]byte // union of sockaddr4 and sockaddr6
  733. remote [28]byte // union of sockaddr4 and sockaddr6
  734. startTime [8]byte // uint64, changed to prevent padding from being inserted
  735. lastActivity [8]byte // uint64
  736. bytesIn [8]byte // uint64
  737. bytesOut [8]byte // uint64
  738. inSegs [8]byte // uint64
  739. outSegs [8]byte // uint64
  740. state uint16
  741. activeOpen byte
  742. flag01 byte
  743. outBuffered uint32
  744. inBuffered uint32
  745. maxSndWnd uint32
  746. reXmtCount uint32
  747. congestionWnd uint32
  748. ssThresh uint32
  749. roundTripTime uint32
  750. roundTripVar uint32
  751. sendMSS uint32
  752. sndWnd uint32
  753. rcvBufSize uint32
  754. sndBufSize uint32
  755. outOfOrderCount uint32
  756. lcl0WindowCount uint32
  757. rmt0WindowCount uint32
  758. dupacks uint32
  759. flag02 byte
  760. sockOpt6Cont byte
  761. asid uint16
  762. resourceName [8]byte
  763. resourceId uint32
  764. subtask uint32
  765. sockOpt byte
  766. sockOpt6 byte
  767. clusterConnFlag byte
  768. proto byte
  769. targetAppl [8]byte
  770. luName [8]byte
  771. clientUserId [8]byte
  772. logMode [8]byte
  773. timeStamp uint32
  774. timeStampAge uint32
  775. serverResourceId uint32
  776. intfName [16]byte
  777. ttlsStatPol byte
  778. ttlsStatConn byte
  779. ttlsSSLProt uint16
  780. ttlsNegCiph [2]byte
  781. ttlsSecType byte
  782. ttlsFIPS140Mode byte
  783. ttlsUserID [8]byte
  784. applData [40]byte
  785. inOldestTime [8]byte // uint64
  786. outOldestTime [8]byte // uint64
  787. tcpTrustedPartner byte
  788. _ [3]byte
  789. bulkDataIntfName [16]byte
  790. ttlsNegCiph4 [4]byte
  791. smcReason uint32
  792. lclSMCLinkId uint32
  793. rmtSMCLinkId uint32
  794. smcStatus byte
  795. smcFlags byte
  796. _ [2]byte
  797. rcvWnd uint32
  798. lclSMCBufSz uint32
  799. rmtSMCBufSz uint32
  800. ttlsSessID [32]byte
  801. ttlsSessIDLen int16
  802. _ [1]byte
  803. smcDStatus byte
  804. smcDReason uint32
  805. }
  806. var svcNameTable [][]byte = [][]byte{
  807. []byte("\xc5\xe9\xc2\xd5\xd4\xc9\xc6\xf4"), // svc_EZBNMIF4
  808. }
  809. const (
  810. svc_EZBNMIF4 = 0
  811. )
  812. func GetsockoptTCPInfo(fd, level, opt int) (*TCPInfo, error) {
  813. jobname := []byte("\x5c\x40\x40\x40\x40\x40\x40\x40") // "*"
  814. responseBuffer := [4096]byte{0}
  815. var bufferAlet, reasonCode uint32 = 0, 0
  816. var bufferLen, returnValue, returnCode int32 = 4096, 0, 0
  817. dsa := [18]uint64{0}
  818. var argv [7]unsafe.Pointer
  819. argv[0] = unsafe.Pointer(&jobname[0])
  820. argv[1] = unsafe.Pointer(&responseBuffer[0])
  821. argv[2] = unsafe.Pointer(&bufferAlet)
  822. argv[3] = unsafe.Pointer(&bufferLen)
  823. argv[4] = unsafe.Pointer(&returnValue)
  824. argv[5] = unsafe.Pointer(&returnCode)
  825. argv[6] = unsafe.Pointer(&reasonCode)
  826. request := (*struct {
  827. header nwmHeader
  828. filter nwmFilter
  829. })(unsafe.Pointer(&responseBuffer[0]))
  830. EZBNMIF4 := svcLoad(&svcNameTable[svc_EZBNMIF4][0])
  831. if EZBNMIF4 == nil {
  832. return nil, errnoErr(EINVAL)
  833. }
  834. // GetGlobalStats EZBNMIF4 call
  835. request.header.ident = nwmHeaderIdentifier
  836. request.header.length = uint32(unsafe.Sizeof(request.header))
  837. request.header.version = nwmCurrentVer
  838. request.header.nwmType = nwmGlobalStatsType
  839. request.header.options = 0x80000000
  840. svcCall(EZBNMIF4, &argv[0], &dsa[0])
  841. // outputDesc field is filled by EZBNMIF4 on success
  842. if returnCode != 0 || request.header.outputDesc.offset == 0 {
  843. return nil, errnoErr(EINVAL)
  844. }
  845. // Check that EZBNMIF4 returned a nwmRecHeader
  846. recHeader := (*nwmRecHeader)(unsafe.Pointer(&responseBuffer[request.header.outputDesc.offset]))
  847. if recHeader.ident != nwmRecHeaderIdentifier {
  848. return nil, errnoErr(EINVAL)
  849. }
  850. // Parse nwmTriplets to get offsets of returned entries
  851. var sections []*uint64
  852. var sectionDesc *nwmTriplet = (*nwmTriplet)(unsafe.Pointer(&responseBuffer[0]))
  853. for i := uint32(0); i < uint32(recHeader.number); i++ {
  854. offset := request.header.outputDesc.offset + uint32(unsafe.Sizeof(*recHeader)) + i*uint32(unsafe.Sizeof(*sectionDesc))
  855. sectionDesc = (*nwmTriplet)(unsafe.Pointer(&responseBuffer[offset]))
  856. for j := uint32(0); j < sectionDesc.number; j++ {
  857. offset = request.header.outputDesc.offset + sectionDesc.offset + j*sectionDesc.length
  858. sections = append(sections, (*uint64)(unsafe.Pointer(&responseBuffer[offset])))
  859. }
  860. }
  861. // Find nwmTCPStatsEntry in returned entries
  862. var tcpStats *nwmTCPStatsEntry = nil
  863. for _, ptr := range sections {
  864. switch *ptr {
  865. case nwmTCPStatsIdentifier:
  866. if tcpStats != nil {
  867. return nil, errnoErr(EINVAL)
  868. }
  869. tcpStats = (*nwmTCPStatsEntry)(unsafe.Pointer(ptr))
  870. case nwmIPStatsIdentifier:
  871. case nwmIPGStatsIdentifier:
  872. case nwmUDPStatsIdentifier:
  873. case nwmICMPGStatsEntry:
  874. case nwmICMPTStatsEntry:
  875. default:
  876. return nil, errnoErr(EINVAL)
  877. }
  878. }
  879. if tcpStats == nil {
  880. return nil, errnoErr(EINVAL)
  881. }
  882. // GetConnectionDetail EZBNMIF4 call
  883. responseBuffer = [4096]byte{0}
  884. dsa = [18]uint64{0}
  885. bufferAlet, reasonCode = 0, 0
  886. bufferLen, returnValue, returnCode = 4096, 0, 0
  887. nameptr := (*uint32)(unsafe.Pointer(uintptr(0x21c))) // Get jobname of current process
  888. nameptr = (*uint32)(unsafe.Pointer(uintptr(*nameptr + 12)))
  889. argv[0] = unsafe.Pointer(uintptr(*nameptr))
  890. request.header.ident = nwmHeaderIdentifier
  891. request.header.length = uint32(unsafe.Sizeof(request.header))
  892. request.header.version = nwmCurrentVer
  893. request.header.nwmType = nwmTCPConnType
  894. request.header.options = 0x80000000
  895. request.filter.ident = nwmFilterIdentifier
  896. var localSockaddr RawSockaddrAny
  897. socklen := _Socklen(SizeofSockaddrAny)
  898. err := getsockname(fd, &localSockaddr, &socklen)
  899. if err != nil {
  900. return nil, errnoErr(EINVAL)
  901. }
  902. if localSockaddr.Addr.Family == AF_INET {
  903. localSockaddr := (*RawSockaddrInet4)(unsafe.Pointer(&localSockaddr.Addr))
  904. localSockFilter := (*RawSockaddrInet4)(unsafe.Pointer(&request.filter.local[0]))
  905. localSockFilter.Family = AF_INET
  906. var i int
  907. for i = 0; i < 4; i++ {
  908. if localSockaddr.Addr[i] != 0 {
  909. break
  910. }
  911. }
  912. if i != 4 {
  913. request.filter.flags |= nwmFilterLclAddrMask
  914. for i = 0; i < 4; i++ {
  915. localSockFilter.Addr[i] = localSockaddr.Addr[i]
  916. }
  917. }
  918. if localSockaddr.Port != 0 {
  919. request.filter.flags |= nwmFilterLclPortMask
  920. localSockFilter.Port = localSockaddr.Port
  921. }
  922. } else if localSockaddr.Addr.Family == AF_INET6 {
  923. localSockaddr := (*RawSockaddrInet6)(unsafe.Pointer(&localSockaddr.Addr))
  924. localSockFilter := (*RawSockaddrInet6)(unsafe.Pointer(&request.filter.local[0]))
  925. localSockFilter.Family = AF_INET6
  926. var i int
  927. for i = 0; i < 16; i++ {
  928. if localSockaddr.Addr[i] != 0 {
  929. break
  930. }
  931. }
  932. if i != 16 {
  933. request.filter.flags |= nwmFilterLclAddrMask
  934. for i = 0; i < 16; i++ {
  935. localSockFilter.Addr[i] = localSockaddr.Addr[i]
  936. }
  937. }
  938. if localSockaddr.Port != 0 {
  939. request.filter.flags |= nwmFilterLclPortMask
  940. localSockFilter.Port = localSockaddr.Port
  941. }
  942. }
  943. svcCall(EZBNMIF4, &argv[0], &dsa[0])
  944. // outputDesc field is filled by EZBNMIF4 on success
  945. if returnCode != 0 || request.header.outputDesc.offset == 0 {
  946. return nil, errnoErr(EINVAL)
  947. }
  948. // Check that EZBNMIF4 returned a nwmConnEntry
  949. conn := (*nwmConnEntry)(unsafe.Pointer(&responseBuffer[request.header.outputDesc.offset]))
  950. if conn.ident != nwmTCPConnIdentifier {
  951. return nil, errnoErr(EINVAL)
  952. }
  953. // Copy data from the returned data structures into tcpInfo
  954. // Stats from nwmConnEntry are specific to that connection.
  955. // Stats from nwmTCPStatsEntry are global (to the interface?)
  956. // Fields may not be an exact match. Some fields have no equivalent.
  957. var tcpinfo TCPInfo
  958. tcpinfo.State = uint8(conn.state)
  959. tcpinfo.Ca_state = 0 // dummy
  960. tcpinfo.Retransmits = uint8(tcpStats.retransSegs)
  961. tcpinfo.Probes = uint8(tcpStats.outWinProbes)
  962. tcpinfo.Backoff = 0 // dummy
  963. tcpinfo.Options = 0 // dummy
  964. tcpinfo.Rto = tcpStats.retransTimeouts
  965. tcpinfo.Ato = tcpStats.outDelayAcks
  966. tcpinfo.Snd_mss = conn.sendMSS
  967. tcpinfo.Rcv_mss = conn.sendMSS // dummy
  968. tcpinfo.Unacked = 0 // dummy
  969. tcpinfo.Sacked = 0 // dummy
  970. tcpinfo.Lost = 0 // dummy
  971. tcpinfo.Retrans = conn.reXmtCount
  972. tcpinfo.Fackets = 0 // dummy
  973. tcpinfo.Last_data_sent = uint32(*(*uint64)(unsafe.Pointer(&conn.lastActivity[0])))
  974. tcpinfo.Last_ack_sent = uint32(*(*uint64)(unsafe.Pointer(&conn.outOldestTime[0])))
  975. tcpinfo.Last_data_recv = uint32(*(*uint64)(unsafe.Pointer(&conn.inOldestTime[0])))
  976. tcpinfo.Last_ack_recv = uint32(*(*uint64)(unsafe.Pointer(&conn.inOldestTime[0])))
  977. tcpinfo.Pmtu = conn.sendMSS // dummy, NWMIfRouteMtu is a candidate
  978. tcpinfo.Rcv_ssthresh = conn.ssThresh
  979. tcpinfo.Rtt = conn.roundTripTime
  980. tcpinfo.Rttvar = conn.roundTripVar
  981. tcpinfo.Snd_ssthresh = conn.ssThresh // dummy
  982. tcpinfo.Snd_cwnd = conn.congestionWnd
  983. tcpinfo.Advmss = conn.sendMSS // dummy
  984. tcpinfo.Reordering = 0 // dummy
  985. tcpinfo.Rcv_rtt = conn.roundTripTime // dummy
  986. tcpinfo.Rcv_space = conn.sendMSS // dummy
  987. tcpinfo.Total_retrans = conn.reXmtCount
  988. svcUnload(&svcNameTable[svc_EZBNMIF4][0], EZBNMIF4)
  989. return &tcpinfo, nil
  990. }
  991. // GetsockoptString returns the string value of the socket option opt for the
  992. // socket associated with fd at the given socket level.
  993. func GetsockoptString(fd, level, opt int) (string, error) {
  994. buf := make([]byte, 256)
  995. vallen := _Socklen(len(buf))
  996. err := getsockopt(fd, level, opt, unsafe.Pointer(&buf[0]), &vallen)
  997. if err != nil {
  998. return "", err
  999. }
  1000. return string(buf[:vallen-1]), nil
  1001. }
  1002. func Recvmsg(fd int, p, oob []byte, flags int) (n, oobn int, recvflags int, from Sockaddr, err error) {
  1003. var msg Msghdr
  1004. var rsa RawSockaddrAny
  1005. msg.Name = (*byte)(unsafe.Pointer(&rsa))
  1006. msg.Namelen = SizeofSockaddrAny
  1007. var iov Iovec
  1008. if len(p) > 0 {
  1009. iov.Base = (*byte)(unsafe.Pointer(&p[0]))
  1010. iov.SetLen(len(p))
  1011. }
  1012. var dummy byte
  1013. if len(oob) > 0 {
  1014. // receive at least one normal byte
  1015. if len(p) == 0 {
  1016. iov.Base = &dummy
  1017. iov.SetLen(1)
  1018. }
  1019. msg.Control = (*byte)(unsafe.Pointer(&oob[0]))
  1020. msg.SetControllen(len(oob))
  1021. }
  1022. msg.Iov = &iov
  1023. msg.Iovlen = 1
  1024. if n, err = recvmsg(fd, &msg, flags); err != nil {
  1025. return
  1026. }
  1027. oobn = int(msg.Controllen)
  1028. recvflags = int(msg.Flags)
  1029. // source address is only specified if the socket is unconnected
  1030. if rsa.Addr.Family != AF_UNSPEC {
  1031. // TODO(neeilan): Remove 0 arg added to get this compiling on z/OS
  1032. from, err = anyToSockaddr(0, &rsa)
  1033. }
  1034. return
  1035. }
  1036. func Sendmsg(fd int, p, oob []byte, to Sockaddr, flags int) (err error) {
  1037. _, err = SendmsgN(fd, p, oob, to, flags)
  1038. return
  1039. }
  1040. func SendmsgN(fd int, p, oob []byte, to Sockaddr, flags int) (n int, err error) {
  1041. var ptr unsafe.Pointer
  1042. var salen _Socklen
  1043. if to != nil {
  1044. var err error
  1045. ptr, salen, err = to.sockaddr()
  1046. if err != nil {
  1047. return 0, err
  1048. }
  1049. }
  1050. var msg Msghdr
  1051. msg.Name = (*byte)(unsafe.Pointer(ptr))
  1052. msg.Namelen = int32(salen)
  1053. var iov Iovec
  1054. if len(p) > 0 {
  1055. iov.Base = (*byte)(unsafe.Pointer(&p[0]))
  1056. iov.SetLen(len(p))
  1057. }
  1058. var dummy byte
  1059. if len(oob) > 0 {
  1060. // send at least one normal byte
  1061. if len(p) == 0 {
  1062. iov.Base = &dummy
  1063. iov.SetLen(1)
  1064. }
  1065. msg.Control = (*byte)(unsafe.Pointer(&oob[0]))
  1066. msg.SetControllen(len(oob))
  1067. }
  1068. msg.Iov = &iov
  1069. msg.Iovlen = 1
  1070. if n, err = sendmsg(fd, &msg, flags); err != nil {
  1071. return 0, err
  1072. }
  1073. if len(oob) > 0 && len(p) == 0 {
  1074. n = 0
  1075. }
  1076. return n, nil
  1077. }
  1078. func Opendir(name string) (uintptr, error) {
  1079. p, err := BytePtrFromString(name)
  1080. if err != nil {
  1081. return 0, err
  1082. }
  1083. dir, _, e := syscall_syscall(SYS___OPENDIR_A, uintptr(unsafe.Pointer(p)), 0, 0)
  1084. runtime.KeepAlive(unsafe.Pointer(p))
  1085. if e != 0 {
  1086. err = errnoErr(e)
  1087. }
  1088. return dir, err
  1089. }
  1090. // clearsyscall.Errno resets the errno value to 0.
  1091. func clearErrno()
  1092. func Readdir(dir uintptr) (*Dirent, error) {
  1093. var ent Dirent
  1094. var res uintptr
  1095. // __readdir_r_a returns errno at the end of the directory stream, rather than 0.
  1096. // Therefore to avoid false positives we clear errno before calling it.
  1097. // TODO(neeilan): Commented this out to get sys/unix compiling on z/OS. Uncomment and fix. Error: "undefined: clearsyscall"
  1098. //clearsyscall.Errno() // TODO(mundaym): check pre-emption rules.
  1099. e, _, _ := syscall_syscall(SYS___READDIR_R_A, dir, uintptr(unsafe.Pointer(&ent)), uintptr(unsafe.Pointer(&res)))
  1100. var err error
  1101. if e != 0 {
  1102. err = errnoErr(Errno(e))
  1103. }
  1104. if res == 0 {
  1105. return nil, err
  1106. }
  1107. return &ent, err
  1108. }
  1109. func Closedir(dir uintptr) error {
  1110. _, _, e := syscall_syscall(SYS_CLOSEDIR, dir, 0, 0)
  1111. if e != 0 {
  1112. return errnoErr(e)
  1113. }
  1114. return nil
  1115. }
  1116. func Seekdir(dir uintptr, pos int) {
  1117. _, _, _ = syscall_syscall(SYS_SEEKDIR, dir, uintptr(pos), 0)
  1118. }
  1119. func Telldir(dir uintptr) (int, error) {
  1120. p, _, e := syscall_syscall(SYS_TELLDIR, dir, 0, 0)
  1121. pos := int(p)
  1122. if pos == -1 {
  1123. return pos, errnoErr(e)
  1124. }
  1125. return pos, nil
  1126. }
  1127. // FcntlFlock performs a fcntl syscall for the F_GETLK, F_SETLK or F_SETLKW command.
  1128. func FcntlFlock(fd uintptr, cmd int, lk *Flock_t) error {
  1129. // struct flock is packed on z/OS. We can't emulate that in Go so
  1130. // instead we pack it here.
  1131. var flock [24]byte
  1132. *(*int16)(unsafe.Pointer(&flock[0])) = lk.Type
  1133. *(*int16)(unsafe.Pointer(&flock[2])) = lk.Whence
  1134. *(*int64)(unsafe.Pointer(&flock[4])) = lk.Start
  1135. *(*int64)(unsafe.Pointer(&flock[12])) = lk.Len
  1136. *(*int32)(unsafe.Pointer(&flock[20])) = lk.Pid
  1137. _, _, errno := syscall_syscall(SYS_FCNTL, fd, uintptr(cmd), uintptr(unsafe.Pointer(&flock)))
  1138. lk.Type = *(*int16)(unsafe.Pointer(&flock[0]))
  1139. lk.Whence = *(*int16)(unsafe.Pointer(&flock[2]))
  1140. lk.Start = *(*int64)(unsafe.Pointer(&flock[4]))
  1141. lk.Len = *(*int64)(unsafe.Pointer(&flock[12]))
  1142. lk.Pid = *(*int32)(unsafe.Pointer(&flock[20]))
  1143. if errno == 0 {
  1144. return nil
  1145. }
  1146. return errno
  1147. }
  1148. func Flock(fd int, how int) error {
  1149. var flock_type int16
  1150. var fcntl_cmd int
  1151. switch how {
  1152. case LOCK_SH | LOCK_NB:
  1153. flock_type = F_RDLCK
  1154. fcntl_cmd = F_SETLK
  1155. case LOCK_EX | LOCK_NB:
  1156. flock_type = F_WRLCK
  1157. fcntl_cmd = F_SETLK
  1158. case LOCK_EX:
  1159. flock_type = F_WRLCK
  1160. fcntl_cmd = F_SETLKW
  1161. case LOCK_UN:
  1162. flock_type = F_UNLCK
  1163. fcntl_cmd = F_SETLKW
  1164. default:
  1165. }
  1166. flock := Flock_t{
  1167. Type: int16(flock_type),
  1168. Whence: int16(0),
  1169. Start: int64(0),
  1170. Len: int64(0),
  1171. Pid: int32(Getppid()),
  1172. }
  1173. err := FcntlFlock(uintptr(fd), fcntl_cmd, &flock)
  1174. return err
  1175. }
  1176. func Mlock(b []byte) (err error) {
  1177. _, _, e1 := syscall_syscall(SYS___MLOCKALL, _BPX_NONSWAP, 0, 0)
  1178. if e1 != 0 {
  1179. err = errnoErr(e1)
  1180. }
  1181. return
  1182. }
  1183. func Mlock2(b []byte, flags int) (err error) {
  1184. _, _, e1 := syscall_syscall(SYS___MLOCKALL, _BPX_NONSWAP, 0, 0)
  1185. if e1 != 0 {
  1186. err = errnoErr(e1)
  1187. }
  1188. return
  1189. }
  1190. func Mlockall(flags int) (err error) {
  1191. _, _, e1 := syscall_syscall(SYS___MLOCKALL, _BPX_NONSWAP, 0, 0)
  1192. if e1 != 0 {
  1193. err = errnoErr(e1)
  1194. }
  1195. return
  1196. }
  1197. func Munlock(b []byte) (err error) {
  1198. _, _, e1 := syscall_syscall(SYS___MLOCKALL, _BPX_SWAP, 0, 0)
  1199. if e1 != 0 {
  1200. err = errnoErr(e1)
  1201. }
  1202. return
  1203. }
  1204. func Munlockall() (err error) {
  1205. _, _, e1 := syscall_syscall(SYS___MLOCKALL, _BPX_SWAP, 0, 0)
  1206. if e1 != 0 {
  1207. err = errnoErr(e1)
  1208. }
  1209. return
  1210. }
  1211. func ClockGettime(clockid int32, ts *Timespec) error {
  1212. var ticks_per_sec uint32 = 100 //TODO(kenan): value is currently hardcoded; need sysconf() call otherwise
  1213. var nsec_per_sec int64 = 1000000000
  1214. if ts == nil {
  1215. return EFAULT
  1216. }
  1217. if clockid == CLOCK_REALTIME || clockid == CLOCK_MONOTONIC {
  1218. var nanotime int64 = runtime.Nanotime1()
  1219. ts.Sec = nanotime / nsec_per_sec
  1220. ts.Nsec = nanotime % nsec_per_sec
  1221. } else if clockid == CLOCK_PROCESS_CPUTIME_ID || clockid == CLOCK_THREAD_CPUTIME_ID {
  1222. var tm Tms
  1223. _, err := Times(&tm)
  1224. if err != nil {
  1225. return EFAULT
  1226. }
  1227. ts.Sec = int64(tm.Utime / ticks_per_sec)
  1228. ts.Nsec = int64(tm.Utime) * nsec_per_sec / int64(ticks_per_sec)
  1229. } else {
  1230. return EINVAL
  1231. }
  1232. return nil
  1233. }
  1234. func Statfs(path string, stat *Statfs_t) (err error) {
  1235. fd, err := open(path, O_RDONLY, 0)
  1236. defer Close(fd)
  1237. if err != nil {
  1238. return err
  1239. }
  1240. return Fstatfs(fd, stat)
  1241. }
  1242. var (
  1243. Stdin = 0
  1244. Stdout = 1
  1245. Stderr = 2
  1246. )
  1247. // Do the interface allocations only once for common
  1248. // Errno values.
  1249. var (
  1250. errEAGAIN error = syscall.EAGAIN
  1251. errEINVAL error = syscall.EINVAL
  1252. errENOENT error = syscall.ENOENT
  1253. )
  1254. var (
  1255. signalNameMapOnce sync.Once
  1256. signalNameMap map[string]syscall.Signal
  1257. )
  1258. // errnoErr returns common boxed Errno values, to prevent
  1259. // allocations at runtime.
  1260. func errnoErr(e Errno) error {
  1261. switch e {
  1262. case 0:
  1263. return nil
  1264. case EAGAIN:
  1265. return errEAGAIN
  1266. case EINVAL:
  1267. return errEINVAL
  1268. case ENOENT:
  1269. return errENOENT
  1270. }
  1271. return e
  1272. }
  1273. // ErrnoName returns the error name for error number e.
  1274. func ErrnoName(e Errno) string {
  1275. i := sort.Search(len(errorList), func(i int) bool {
  1276. return errorList[i].num >= e
  1277. })
  1278. if i < len(errorList) && errorList[i].num == e {
  1279. return errorList[i].name
  1280. }
  1281. return ""
  1282. }
  1283. // SignalName returns the signal name for signal number s.
  1284. func SignalName(s syscall.Signal) string {
  1285. i := sort.Search(len(signalList), func(i int) bool {
  1286. return signalList[i].num >= s
  1287. })
  1288. if i < len(signalList) && signalList[i].num == s {
  1289. return signalList[i].name
  1290. }
  1291. return ""
  1292. }
  1293. // SignalNum returns the syscall.Signal for signal named s,
  1294. // or 0 if a signal with such name is not found.
  1295. // The signal name should start with "SIG".
  1296. func SignalNum(s string) syscall.Signal {
  1297. signalNameMapOnce.Do(func() {
  1298. signalNameMap = make(map[string]syscall.Signal, len(signalList))
  1299. for _, signal := range signalList {
  1300. signalNameMap[signal.name] = signal.num
  1301. }
  1302. })
  1303. return signalNameMap[s]
  1304. }
  1305. // clen returns the index of the first NULL byte in n or len(n) if n contains no NULL byte.
  1306. func clen(n []byte) int {
  1307. i := bytes.IndexByte(n, 0)
  1308. if i == -1 {
  1309. i = len(n)
  1310. }
  1311. return i
  1312. }
  1313. // Mmap manager, for use by operating system-specific implementations.
  1314. type mmapper struct {
  1315. sync.Mutex
  1316. active map[*byte][]byte // active mappings; key is last byte in mapping
  1317. mmap func(addr, length uintptr, prot, flags, fd int, offset int64) (uintptr, error)
  1318. munmap func(addr uintptr, length uintptr) error
  1319. }
  1320. func (m *mmapper) Mmap(fd int, offset int64, length int, prot int, flags int) (data []byte, err error) {
  1321. if length <= 0 {
  1322. return nil, EINVAL
  1323. }
  1324. // Map the requested memory.
  1325. addr, errno := m.mmap(0, uintptr(length), prot, flags, fd, offset)
  1326. if errno != nil {
  1327. return nil, errno
  1328. }
  1329. // Slice memory layout
  1330. var sl = struct {
  1331. addr uintptr
  1332. len int
  1333. cap int
  1334. }{addr, length, length}
  1335. // Use unsafe to turn sl into a []byte.
  1336. b := *(*[]byte)(unsafe.Pointer(&sl))
  1337. // Register mapping in m and return it.
  1338. p := &b[cap(b)-1]
  1339. m.Lock()
  1340. defer m.Unlock()
  1341. m.active[p] = b
  1342. return b, nil
  1343. }
  1344. func (m *mmapper) Munmap(data []byte) (err error) {
  1345. if len(data) == 0 || len(data) != cap(data) {
  1346. return EINVAL
  1347. }
  1348. // Find the base of the mapping.
  1349. p := &data[cap(data)-1]
  1350. m.Lock()
  1351. defer m.Unlock()
  1352. b := m.active[p]
  1353. if b == nil || &b[0] != &data[0] {
  1354. return EINVAL
  1355. }
  1356. // Unmap the memory and update m.
  1357. if errno := m.munmap(uintptr(unsafe.Pointer(&b[0])), uintptr(len(b))); errno != nil {
  1358. return errno
  1359. }
  1360. delete(m.active, p)
  1361. return nil
  1362. }
  1363. func Read(fd int, p []byte) (n int, err error) {
  1364. n, err = read(fd, p)
  1365. if raceenabled {
  1366. if n > 0 {
  1367. raceWriteRange(unsafe.Pointer(&p[0]), n)
  1368. }
  1369. if err == nil {
  1370. raceAcquire(unsafe.Pointer(&ioSync))
  1371. }
  1372. }
  1373. return
  1374. }
  1375. func Write(fd int, p []byte) (n int, err error) {
  1376. if raceenabled {
  1377. raceReleaseMerge(unsafe.Pointer(&ioSync))
  1378. }
  1379. n, err = write(fd, p)
  1380. if raceenabled && n > 0 {
  1381. raceReadRange(unsafe.Pointer(&p[0]), n)
  1382. }
  1383. return
  1384. }
  1385. // For testing: clients can set this flag to force
  1386. // creation of IPv6 sockets to return EAFNOSUPPORT.
  1387. var SocketDisableIPv6 bool
  1388. // Sockaddr represents a socket address.
  1389. type Sockaddr interface {
  1390. sockaddr() (ptr unsafe.Pointer, len _Socklen, err error) // lowercase; only we can define Sockaddrs
  1391. }
  1392. // SockaddrInet4 implements the Sockaddr interface for AF_INET type sockets.
  1393. type SockaddrInet4 struct {
  1394. Port int
  1395. Addr [4]byte
  1396. raw RawSockaddrInet4
  1397. }
  1398. // SockaddrInet6 implements the Sockaddr interface for AF_INET6 type sockets.
  1399. type SockaddrInet6 struct {
  1400. Port int
  1401. ZoneId uint32
  1402. Addr [16]byte
  1403. raw RawSockaddrInet6
  1404. }
  1405. // SockaddrUnix implements the Sockaddr interface for AF_UNIX type sockets.
  1406. type SockaddrUnix struct {
  1407. Name string
  1408. raw RawSockaddrUnix
  1409. }
  1410. func Bind(fd int, sa Sockaddr) (err error) {
  1411. ptr, n, err := sa.sockaddr()
  1412. if err != nil {
  1413. return err
  1414. }
  1415. return bind(fd, ptr, n)
  1416. }
  1417. func Connect(fd int, sa Sockaddr) (err error) {
  1418. ptr, n, err := sa.sockaddr()
  1419. if err != nil {
  1420. return err
  1421. }
  1422. return connect(fd, ptr, n)
  1423. }
  1424. func Getpeername(fd int) (sa Sockaddr, err error) {
  1425. var rsa RawSockaddrAny
  1426. var len _Socklen = SizeofSockaddrAny
  1427. if err = getpeername(fd, &rsa, &len); err != nil {
  1428. return
  1429. }
  1430. return anyToSockaddr(fd, &rsa)
  1431. }
  1432. func GetsockoptByte(fd, level, opt int) (value byte, err error) {
  1433. var n byte
  1434. vallen := _Socklen(1)
  1435. err = getsockopt(fd, level, opt, unsafe.Pointer(&n), &vallen)
  1436. return n, err
  1437. }
  1438. func GetsockoptInt(fd, level, opt int) (value int, err error) {
  1439. var n int32
  1440. vallen := _Socklen(4)
  1441. err = getsockopt(fd, level, opt, unsafe.Pointer(&n), &vallen)
  1442. return int(n), err
  1443. }
  1444. func GetsockoptInet4Addr(fd, level, opt int) (value [4]byte, err error) {
  1445. vallen := _Socklen(4)
  1446. err = getsockopt(fd, level, opt, unsafe.Pointer(&value[0]), &vallen)
  1447. return value, err
  1448. }
  1449. func GetsockoptIPMreq(fd, level, opt int) (*IPMreq, error) {
  1450. var value IPMreq
  1451. vallen := _Socklen(SizeofIPMreq)
  1452. err := getsockopt(fd, level, opt, unsafe.Pointer(&value), &vallen)
  1453. return &value, err
  1454. }
  1455. func GetsockoptIPv6Mreq(fd, level, opt int) (*IPv6Mreq, error) {
  1456. var value IPv6Mreq
  1457. vallen := _Socklen(SizeofIPv6Mreq)
  1458. err := getsockopt(fd, level, opt, unsafe.Pointer(&value), &vallen)
  1459. return &value, err
  1460. }
  1461. func GetsockoptIPv6MTUInfo(fd, level, opt int) (*IPv6MTUInfo, error) {
  1462. var value IPv6MTUInfo
  1463. vallen := _Socklen(SizeofIPv6MTUInfo)
  1464. err := getsockopt(fd, level, opt, unsafe.Pointer(&value), &vallen)
  1465. return &value, err
  1466. }
  1467. func GetsockoptICMPv6Filter(fd, level, opt int) (*ICMPv6Filter, error) {
  1468. var value ICMPv6Filter
  1469. vallen := _Socklen(SizeofICMPv6Filter)
  1470. err := getsockopt(fd, level, opt, unsafe.Pointer(&value), &vallen)
  1471. return &value, err
  1472. }
  1473. func GetsockoptLinger(fd, level, opt int) (*Linger, error) {
  1474. var linger Linger
  1475. vallen := _Socklen(SizeofLinger)
  1476. err := getsockopt(fd, level, opt, unsafe.Pointer(&linger), &vallen)
  1477. return &linger, err
  1478. }
  1479. func GetsockoptTimeval(fd, level, opt int) (*Timeval, error) {
  1480. var tv Timeval
  1481. vallen := _Socklen(unsafe.Sizeof(tv))
  1482. err := getsockopt(fd, level, opt, unsafe.Pointer(&tv), &vallen)
  1483. return &tv, err
  1484. }
  1485. func GetsockoptUint64(fd, level, opt int) (value uint64, err error) {
  1486. var n uint64
  1487. vallen := _Socklen(8)
  1488. err = getsockopt(fd, level, opt, unsafe.Pointer(&n), &vallen)
  1489. return n, err
  1490. }
  1491. func Recvfrom(fd int, p []byte, flags int) (n int, from Sockaddr, err error) {
  1492. var rsa RawSockaddrAny
  1493. var len _Socklen = SizeofSockaddrAny
  1494. if n, err = recvfrom(fd, p, flags, &rsa, &len); err != nil {
  1495. return
  1496. }
  1497. if rsa.Addr.Family != AF_UNSPEC {
  1498. from, err = anyToSockaddr(fd, &rsa)
  1499. }
  1500. return
  1501. }
  1502. func Sendto(fd int, p []byte, flags int, to Sockaddr) (err error) {
  1503. ptr, n, err := to.sockaddr()
  1504. if err != nil {
  1505. return err
  1506. }
  1507. return sendto(fd, p, flags, ptr, n)
  1508. }
  1509. func SetsockoptByte(fd, level, opt int, value byte) (err error) {
  1510. return setsockopt(fd, level, opt, unsafe.Pointer(&value), 1)
  1511. }
  1512. func SetsockoptInt(fd, level, opt int, value int) (err error) {
  1513. var n = int32(value)
  1514. return setsockopt(fd, level, opt, unsafe.Pointer(&n), 4)
  1515. }
  1516. func SetsockoptInet4Addr(fd, level, opt int, value [4]byte) (err error) {
  1517. return setsockopt(fd, level, opt, unsafe.Pointer(&value[0]), 4)
  1518. }
  1519. func SetsockoptIPMreq(fd, level, opt int, mreq *IPMreq) (err error) {
  1520. return setsockopt(fd, level, opt, unsafe.Pointer(mreq), SizeofIPMreq)
  1521. }
  1522. func SetsockoptIPv6Mreq(fd, level, opt int, mreq *IPv6Mreq) (err error) {
  1523. return setsockopt(fd, level, opt, unsafe.Pointer(mreq), SizeofIPv6Mreq)
  1524. }
  1525. func SetsockoptICMPv6Filter(fd, level, opt int, filter *ICMPv6Filter) error {
  1526. return setsockopt(fd, level, opt, unsafe.Pointer(filter), SizeofICMPv6Filter)
  1527. }
  1528. func SetsockoptLinger(fd, level, opt int, l *Linger) (err error) {
  1529. return setsockopt(fd, level, opt, unsafe.Pointer(l), SizeofLinger)
  1530. }
  1531. func SetsockoptString(fd, level, opt int, s string) (err error) {
  1532. var p unsafe.Pointer
  1533. if len(s) > 0 {
  1534. p = unsafe.Pointer(&[]byte(s)[0])
  1535. }
  1536. return setsockopt(fd, level, opt, p, uintptr(len(s)))
  1537. }
  1538. func SetsockoptTimeval(fd, level, opt int, tv *Timeval) (err error) {
  1539. return setsockopt(fd, level, opt, unsafe.Pointer(tv), unsafe.Sizeof(*tv))
  1540. }
  1541. func SetsockoptUint64(fd, level, opt int, value uint64) (err error) {
  1542. return setsockopt(fd, level, opt, unsafe.Pointer(&value), 8)
  1543. }
  1544. func Socket(domain, typ, proto int) (fd int, err error) {
  1545. if domain == AF_INET6 && SocketDisableIPv6 {
  1546. return -1, EAFNOSUPPORT
  1547. }
  1548. fd, err = socket(domain, typ, proto)
  1549. return
  1550. }
  1551. func Socketpair(domain, typ, proto int) (fd [2]int, err error) {
  1552. var fdx [2]int32
  1553. err = socketpair(domain, typ, proto, &fdx)
  1554. if err == nil {
  1555. fd[0] = int(fdx[0])
  1556. fd[1] = int(fdx[1])
  1557. }
  1558. return
  1559. }
  1560. var ioSync int64
  1561. func CloseOnExec(fd int) { fcntl(fd, F_SETFD, FD_CLOEXEC) }
  1562. func SetNonblock(fd int, nonblocking bool) (err error) {
  1563. flag, err := fcntl(fd, F_GETFL, 0)
  1564. if err != nil {
  1565. return err
  1566. }
  1567. if nonblocking {
  1568. flag |= O_NONBLOCK
  1569. } else {
  1570. flag &= ^O_NONBLOCK
  1571. }
  1572. _, err = fcntl(fd, F_SETFL, flag)
  1573. return err
  1574. }
  1575. // Exec calls execve(2), which replaces the calling executable in the process
  1576. // tree. argv0 should be the full path to an executable ("/bin/ls") and the
  1577. // executable name should also be the first argument in argv (["ls", "-l"]).
  1578. // envv are the environment variables that should be passed to the new
  1579. // process (["USER=go", "PWD=/tmp"]).
  1580. func Exec(argv0 string, argv []string, envv []string) error {
  1581. return syscall.Exec(argv0, argv, envv)
  1582. }
  1583. func Mount(source string, target string, fstype string, flags uintptr, data string) (err error) {
  1584. if needspace := 8 - len(fstype); needspace <= 0 {
  1585. fstype = fstype[:8]
  1586. } else {
  1587. fstype += " "[:needspace]
  1588. }
  1589. return mount_LE(target, source, fstype, uint32(flags), int32(len(data)), data)
  1590. }
  1591. func Unmount(name string, mtm int) (err error) {
  1592. // mountpoint is always a full path and starts with a '/'
  1593. // check if input string is not a mountpoint but a filesystem name
  1594. if name[0] != '/' {
  1595. return unmount(name, mtm)
  1596. }
  1597. // treat name as mountpoint
  1598. b2s := func(arr []byte) string {
  1599. nulli := bytes.IndexByte(arr, 0)
  1600. if nulli == -1 {
  1601. return string(arr)
  1602. } else {
  1603. return string(arr[:nulli])
  1604. }
  1605. }
  1606. var buffer struct {
  1607. header W_Mnth
  1608. fsinfo [64]W_Mntent
  1609. }
  1610. fsCount, err := W_Getmntent_A((*byte)(unsafe.Pointer(&buffer)), int(unsafe.Sizeof(buffer)))
  1611. if err != nil {
  1612. return err
  1613. }
  1614. if fsCount == 0 {
  1615. return EINVAL
  1616. }
  1617. for i := 0; i < fsCount; i++ {
  1618. if b2s(buffer.fsinfo[i].Mountpoint[:]) == name {
  1619. err = unmount(b2s(buffer.fsinfo[i].Fsname[:]), mtm)
  1620. break
  1621. }
  1622. }
  1623. return err
  1624. }