syscall_aix.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  1. // Copyright 2018 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. // +build aix
  5. // Aix system calls.
  6. // This file is compiled as ordinary Go code,
  7. // but it is also input to mksyscall,
  8. // which parses the //sys lines and generates system call stubs.
  9. // Note that sometimes we use a lowercase //sys name and
  10. // wrap it in our own nicer implementation.
  11. package unix
  12. import "unsafe"
  13. /*
  14. * Wrapped
  15. */
  16. //sys utimes(path string, times *[2]Timeval) (err error)
  17. func Utimes(path string, tv []Timeval) error {
  18. if len(tv) != 2 {
  19. return EINVAL
  20. }
  21. return utimes(path, (*[2]Timeval)(unsafe.Pointer(&tv[0])))
  22. }
  23. //sys utimensat(dirfd int, path string, times *[2]Timespec, flag int) (err error)
  24. func UtimesNano(path string, ts []Timespec) error {
  25. if len(ts) != 2 {
  26. return EINVAL
  27. }
  28. return utimensat(AT_FDCWD, path, (*[2]Timespec)(unsafe.Pointer(&ts[0])), 0)
  29. }
  30. func UtimesNanoAt(dirfd int, path string, ts []Timespec, flags int) error {
  31. if ts == nil {
  32. return utimensat(dirfd, path, nil, flags)
  33. }
  34. if len(ts) != 2 {
  35. return EINVAL
  36. }
  37. return utimensat(dirfd, path, (*[2]Timespec)(unsafe.Pointer(&ts[0])), flags)
  38. }
  39. func (sa *SockaddrInet4) sockaddr() (unsafe.Pointer, _Socklen, error) {
  40. if sa.Port < 0 || sa.Port > 0xFFFF {
  41. return nil, 0, EINVAL
  42. }
  43. sa.raw.Family = AF_INET
  44. p := (*[2]byte)(unsafe.Pointer(&sa.raw.Port))
  45. p[0] = byte(sa.Port >> 8)
  46. p[1] = byte(sa.Port)
  47. for i := 0; i < len(sa.Addr); i++ {
  48. sa.raw.Addr[i] = sa.Addr[i]
  49. }
  50. return unsafe.Pointer(&sa.raw), SizeofSockaddrInet4, nil
  51. }
  52. func (sa *SockaddrInet6) sockaddr() (unsafe.Pointer, _Socklen, error) {
  53. if sa.Port < 0 || sa.Port > 0xFFFF {
  54. return nil, 0, EINVAL
  55. }
  56. sa.raw.Family = AF_INET6
  57. p := (*[2]byte)(unsafe.Pointer(&sa.raw.Port))
  58. p[0] = byte(sa.Port >> 8)
  59. p[1] = byte(sa.Port)
  60. sa.raw.Scope_id = sa.ZoneId
  61. for i := 0; i < len(sa.Addr); i++ {
  62. sa.raw.Addr[i] = sa.Addr[i]
  63. }
  64. return unsafe.Pointer(&sa.raw), SizeofSockaddrInet6, nil
  65. }
  66. func (sa *SockaddrUnix) sockaddr() (unsafe.Pointer, _Socklen, error) {
  67. name := sa.Name
  68. n := len(name)
  69. if n > len(sa.raw.Path) {
  70. return nil, 0, EINVAL
  71. }
  72. if n == len(sa.raw.Path) && name[0] != '@' {
  73. return nil, 0, EINVAL
  74. }
  75. sa.raw.Family = AF_UNIX
  76. for i := 0; i < n; i++ {
  77. sa.raw.Path[i] = uint8(name[i])
  78. }
  79. // length is family (uint16), name, NUL.
  80. sl := _Socklen(2)
  81. if n > 0 {
  82. sl += _Socklen(n) + 1
  83. }
  84. if sa.raw.Path[0] == '@' {
  85. sa.raw.Path[0] = 0
  86. // Don't count trailing NUL for abstract address.
  87. sl--
  88. }
  89. return unsafe.Pointer(&sa.raw), sl, nil
  90. }
  91. func Getsockname(fd int) (sa Sockaddr, err error) {
  92. var rsa RawSockaddrAny
  93. var len _Socklen = SizeofSockaddrAny
  94. if err = getsockname(fd, &rsa, &len); err != nil {
  95. return
  96. }
  97. return anyToSockaddr(fd, &rsa)
  98. }
  99. //sys getcwd(buf []byte) (err error)
  100. const ImplementsGetwd = true
  101. func Getwd() (ret string, err error) {
  102. for len := uint64(4096); ; len *= 2 {
  103. b := make([]byte, len)
  104. err := getcwd(b)
  105. if err == nil {
  106. i := 0
  107. for b[i] != 0 {
  108. i++
  109. }
  110. return string(b[0:i]), nil
  111. }
  112. if err != ERANGE {
  113. return "", err
  114. }
  115. }
  116. }
  117. func Getcwd(buf []byte) (n int, err error) {
  118. err = getcwd(buf)
  119. if err == nil {
  120. i := 0
  121. for buf[i] != 0 {
  122. i++
  123. }
  124. n = i + 1
  125. }
  126. return
  127. }
  128. func Getgroups() (gids []int, err error) {
  129. n, err := getgroups(0, nil)
  130. if err != nil {
  131. return nil, err
  132. }
  133. if n == 0 {
  134. return nil, nil
  135. }
  136. // Sanity check group count. Max is 16 on BSD.
  137. if n < 0 || n > 1000 {
  138. return nil, EINVAL
  139. }
  140. a := make([]_Gid_t, n)
  141. n, err = getgroups(n, &a[0])
  142. if err != nil {
  143. return nil, err
  144. }
  145. gids = make([]int, n)
  146. for i, v := range a[0:n] {
  147. gids[i] = int(v)
  148. }
  149. return
  150. }
  151. func Setgroups(gids []int) (err error) {
  152. if len(gids) == 0 {
  153. return setgroups(0, nil)
  154. }
  155. a := make([]_Gid_t, len(gids))
  156. for i, v := range gids {
  157. a[i] = _Gid_t(v)
  158. }
  159. return setgroups(len(a), &a[0])
  160. }
  161. /*
  162. * Socket
  163. */
  164. //sys accept(s int, rsa *RawSockaddrAny, addrlen *_Socklen) (fd int, err error)
  165. func Accept(fd int) (nfd int, sa Sockaddr, err error) {
  166. var rsa RawSockaddrAny
  167. var len _Socklen = SizeofSockaddrAny
  168. nfd, err = accept(fd, &rsa, &len)
  169. if nfd == -1 {
  170. return
  171. }
  172. sa, err = anyToSockaddr(fd, &rsa)
  173. if err != nil {
  174. Close(nfd)
  175. nfd = 0
  176. }
  177. return
  178. }
  179. func Recvmsg(fd int, p, oob []byte, flags int) (n, oobn int, recvflags int, from Sockaddr, err error) {
  180. // Recvmsg not implemented on AIX
  181. sa := new(SockaddrUnix)
  182. return -1, -1, -1, sa, ENOSYS
  183. }
  184. func Sendmsg(fd int, p, oob []byte, to Sockaddr, flags int) (err error) {
  185. _, err = SendmsgN(fd, p, oob, to, flags)
  186. return
  187. }
  188. func SendmsgN(fd int, p, oob []byte, to Sockaddr, flags int) (n int, err error) {
  189. // SendmsgN not implemented on AIX
  190. return -1, ENOSYS
  191. }
  192. func anyToSockaddr(fd int, rsa *RawSockaddrAny) (Sockaddr, error) {
  193. switch rsa.Addr.Family {
  194. case AF_UNIX:
  195. pp := (*RawSockaddrUnix)(unsafe.Pointer(rsa))
  196. sa := new(SockaddrUnix)
  197. // Some versions of AIX have a bug in getsockname (see IV78655).
  198. // We can't rely on sa.Len being set correctly.
  199. n := SizeofSockaddrUnix - 3 // subtract leading Family, Len, terminating NUL.
  200. for i := 0; i < n; i++ {
  201. if pp.Path[i] == 0 {
  202. n = i
  203. break
  204. }
  205. }
  206. bytes := (*[10000]byte)(unsafe.Pointer(&pp.Path[0]))[0:n]
  207. sa.Name = string(bytes)
  208. return sa, nil
  209. case AF_INET:
  210. pp := (*RawSockaddrInet4)(unsafe.Pointer(rsa))
  211. sa := new(SockaddrInet4)
  212. p := (*[2]byte)(unsafe.Pointer(&pp.Port))
  213. sa.Port = int(p[0])<<8 + int(p[1])
  214. for i := 0; i < len(sa.Addr); i++ {
  215. sa.Addr[i] = pp.Addr[i]
  216. }
  217. return sa, nil
  218. case AF_INET6:
  219. pp := (*RawSockaddrInet6)(unsafe.Pointer(rsa))
  220. sa := new(SockaddrInet6)
  221. p := (*[2]byte)(unsafe.Pointer(&pp.Port))
  222. sa.Port = int(p[0])<<8 + int(p[1])
  223. sa.ZoneId = pp.Scope_id
  224. for i := 0; i < len(sa.Addr); i++ {
  225. sa.Addr[i] = pp.Addr[i]
  226. }
  227. return sa, nil
  228. }
  229. return nil, EAFNOSUPPORT
  230. }
  231. func Gettimeofday(tv *Timeval) (err error) {
  232. err = gettimeofday(tv, nil)
  233. return
  234. }
  235. func Sendfile(outfd int, infd int, offset *int64, count int) (written int, err error) {
  236. if raceenabled {
  237. raceReleaseMerge(unsafe.Pointer(&ioSync))
  238. }
  239. return sendfile(outfd, infd, offset, count)
  240. }
  241. // TODO
  242. func sendfile(outfd int, infd int, offset *int64, count int) (written int, err error) {
  243. return -1, ENOSYS
  244. }
  245. //sys getdirent(fd int, buf []byte) (n int, err error)
  246. func ReadDirent(fd int, buf []byte) (n int, err error) {
  247. return getdirent(fd, buf)
  248. }
  249. //sys wait4(pid Pid_t, status *_C_int, options int, rusage *Rusage) (wpid Pid_t, err error)
  250. func Wait4(pid int, wstatus *WaitStatus, options int, rusage *Rusage) (wpid int, err error) {
  251. var status _C_int
  252. var r Pid_t
  253. err = ERESTART
  254. // AIX wait4 may return with ERESTART errno, while the processus is still
  255. // active.
  256. for err == ERESTART {
  257. r, err = wait4(Pid_t(pid), &status, options, rusage)
  258. }
  259. wpid = int(r)
  260. if wstatus != nil {
  261. *wstatus = WaitStatus(status)
  262. }
  263. return
  264. }
  265. /*
  266. * Wait
  267. */
  268. type WaitStatus uint32
  269. func (w WaitStatus) Stopped() bool { return w&0x40 != 0 }
  270. func (w WaitStatus) StopSignal() Signal {
  271. if !w.Stopped() {
  272. return -1
  273. }
  274. return Signal(w>>8) & 0xFF
  275. }
  276. func (w WaitStatus) Exited() bool { return w&0xFF == 0 }
  277. func (w WaitStatus) ExitStatus() int {
  278. if !w.Exited() {
  279. return -1
  280. }
  281. return int((w >> 8) & 0xFF)
  282. }
  283. func (w WaitStatus) Signaled() bool { return w&0x40 == 0 && w&0xFF != 0 }
  284. func (w WaitStatus) Signal() Signal {
  285. if !w.Signaled() {
  286. return -1
  287. }
  288. return Signal(w>>16) & 0xFF
  289. }
  290. func (w WaitStatus) Continued() bool { return w&0x01000000 != 0 }
  291. func (w WaitStatus) CoreDump() bool { return w&0x200 != 0 }
  292. func (w WaitStatus) TrapCause() int { return -1 }
  293. //sys ioctl(fd int, req uint, arg uintptr) (err error)
  294. // ioctl itself should not be exposed directly, but additional get/set
  295. // functions for specific types are permissible.
  296. // IoctlSetInt performs an ioctl operation which sets an integer value
  297. // on fd, using the specified request number.
  298. func IoctlSetInt(fd int, req uint, value int) error {
  299. return ioctl(fd, req, uintptr(value))
  300. }
  301. func ioctlSetWinsize(fd int, req uint, value *Winsize) error {
  302. return ioctl(fd, req, uintptr(unsafe.Pointer(value)))
  303. }
  304. func ioctlSetTermios(fd int, req uint, value *Termios) error {
  305. return ioctl(fd, req, uintptr(unsafe.Pointer(value)))
  306. }
  307. // IoctlGetInt performs an ioctl operation which gets an integer value
  308. // from fd, using the specified request number.
  309. func IoctlGetInt(fd int, req uint) (int, error) {
  310. var value int
  311. err := ioctl(fd, req, uintptr(unsafe.Pointer(&value)))
  312. return value, err
  313. }
  314. func IoctlGetWinsize(fd int, req uint) (*Winsize, error) {
  315. var value Winsize
  316. err := ioctl(fd, req, uintptr(unsafe.Pointer(&value)))
  317. return &value, err
  318. }
  319. func IoctlGetTermios(fd int, req uint) (*Termios, error) {
  320. var value Termios
  321. err := ioctl(fd, req, uintptr(unsafe.Pointer(&value)))
  322. return &value, err
  323. }
  324. // fcntl must never be called with cmd=F_DUP2FD because it doesn't work on AIX
  325. // There is no way to create a custom fcntl and to keep //sys fcntl easily,
  326. // Therefore, the programmer must call dup2 instead of fcntl in this case.
  327. // FcntlInt performs a fcntl syscall on fd with the provided command and argument.
  328. //sys FcntlInt(fd uintptr, cmd int, arg int) (r int,err error) = fcntl
  329. // FcntlFlock performs a fcntl syscall for the F_GETLK, F_SETLK or F_SETLKW command.
  330. //sys FcntlFlock(fd uintptr, cmd int, lk *Flock_t) (err error) = fcntl
  331. //sys fcntl(fd int, cmd int, arg int) (val int, err error)
  332. /*
  333. * Direct access
  334. */
  335. //sys Acct(path string) (err error)
  336. //sys Chdir(path string) (err error)
  337. //sys Chroot(path string) (err error)
  338. //sys Close(fd int) (err error)
  339. //sys Dup(oldfd int) (fd int, err error)
  340. //sys Exit(code int)
  341. //sys Faccessat(dirfd int, path string, mode uint32, flags int) (err error)
  342. //sys Fchdir(fd int) (err error)
  343. //sys Fchmod(fd int, mode uint32) (err error)
  344. //sys Fchmodat(dirfd int, path string, mode uint32, flags int) (err error)
  345. //sys Fchownat(dirfd int, path string, uid int, gid int, flags int) (err error)
  346. //sys Fdatasync(fd int) (err error)
  347. //sys Fsync(fd int) (err error)
  348. // readdir_r
  349. //sysnb Getpgid(pid int) (pgid int, err error)
  350. //sys Getpgrp() (pid int)
  351. //sysnb Getpid() (pid int)
  352. //sysnb Getppid() (ppid int)
  353. //sys Getpriority(which int, who int) (prio int, err error)
  354. //sysnb Getrusage(who int, rusage *Rusage) (err error)
  355. //sysnb Getsid(pid int) (sid int, err error)
  356. //sysnb Kill(pid int, sig Signal) (err error)
  357. //sys Klogctl(typ int, buf []byte) (n int, err error) = syslog
  358. //sys Mkdir(dirfd int, path string, mode uint32) (err error)
  359. //sys Mkdirat(dirfd int, path string, mode uint32) (err error)
  360. //sys Mkfifo(path string, mode uint32) (err error)
  361. //sys Mknod(path string, mode uint32, dev int) (err error)
  362. //sys Mknodat(dirfd int, path string, mode uint32, dev int) (err error)
  363. //sys Nanosleep(time *Timespec, leftover *Timespec) (err error)
  364. //sys Open(path string, mode int, perm uint32) (fd int, err error) = open64
  365. //sys Openat(dirfd int, path string, flags int, mode uint32) (fd int, err error)
  366. //sys read(fd int, p []byte) (n int, err error)
  367. //sys Readlink(path string, buf []byte) (n int, err error)
  368. //sys Renameat(olddirfd int, oldpath string, newdirfd int, newpath string) (err error)
  369. //sys Setdomainname(p []byte) (err error)
  370. //sys Sethostname(p []byte) (err error)
  371. //sysnb Setpgid(pid int, pgid int) (err error)
  372. //sysnb Setsid() (pid int, err error)
  373. //sysnb Settimeofday(tv *Timeval) (err error)
  374. //sys Setuid(uid int) (err error)
  375. //sys Setgid(uid int) (err error)
  376. //sys Setpriority(which int, who int, prio int) (err error)
  377. //sys Statx(dirfd int, path string, flags int, mask int, stat *Statx_t) (err error)
  378. //sys Sync()
  379. //sysnb Times(tms *Tms) (ticks uintptr, err error)
  380. //sysnb Umask(mask int) (oldmask int)
  381. //sysnb Uname(buf *Utsname) (err error)
  382. //TODO umount
  383. // //sys Unmount(target string, flags int) (err error) = umount
  384. //sys Unlink(path string) (err error)
  385. //sys Unlinkat(dirfd int, path string, flags int) (err error)
  386. //sys Ustat(dev int, ubuf *Ustat_t) (err error)
  387. //sys write(fd int, p []byte) (n int, err error)
  388. //sys readlen(fd int, p *byte, np int) (n int, err error) = read
  389. //sys writelen(fd int, p *byte, np int) (n int, err error) = write
  390. //sys Dup2(oldfd int, newfd int) (err error)
  391. //sys Fadvise(fd int, offset int64, length int64, advice int) (err error) = posix_fadvise64
  392. //sys Fchown(fd int, uid int, gid int) (err error)
  393. //sys Fstat(fd int, stat *Stat_t) (err error)
  394. //sys Fstatat(dirfd int, path string, stat *Stat_t, flags int) (err error) = fstatat
  395. //sys Fstatfs(fd int, buf *Statfs_t) (err error)
  396. //sys Ftruncate(fd int, length int64) (err error)
  397. //sysnb Getegid() (egid int)
  398. //sysnb Geteuid() (euid int)
  399. //sysnb Getgid() (gid int)
  400. //sysnb Getuid() (uid int)
  401. //sys Lchown(path string, uid int, gid int) (err error)
  402. //sys Listen(s int, n int) (err error)
  403. //sys Lstat(path string, stat *Stat_t) (err error)
  404. //sys Pause() (err error)
  405. //sys Pread(fd int, p []byte, offset int64) (n int, err error) = pread64
  406. //sys Pwrite(fd int, p []byte, offset int64) (n int, err error) = pwrite64
  407. //TODO Select
  408. // //sys Select(nfd int, r *FdSet, w *FdSet, e *FdSet, timeout *Timeval) (n int, err error)
  409. //sys Pselect(nfd int, r *FdSet, w *FdSet, e *FdSet, timeout *Timespec, sigmask *Sigset_t) (n int, err error)
  410. //sysnb Setregid(rgid int, egid int) (err error)
  411. //sysnb Setreuid(ruid int, euid int) (err error)
  412. //sys Shutdown(fd int, how int) (err error)
  413. //sys Splice(rfd int, roff *int64, wfd int, woff *int64, len int, flags int) (n int64, err error)
  414. //sys Stat(path string, stat *Stat_t) (err error)
  415. //sys Statfs(path string, buf *Statfs_t) (err error)
  416. //sys Truncate(path string, length int64) (err error)
  417. //sys bind(s int, addr unsafe.Pointer, addrlen _Socklen) (err error)
  418. //sys connect(s int, addr unsafe.Pointer, addrlen _Socklen) (err error)
  419. //sysnb getgroups(n int, list *_Gid_t) (nn int, err error)
  420. //sysnb setgroups(n int, list *_Gid_t) (err error)
  421. //sys getsockopt(s int, level int, name int, val unsafe.Pointer, vallen *_Socklen) (err error)
  422. //sys setsockopt(s int, level int, name int, val unsafe.Pointer, vallen uintptr) (err error)
  423. //sysnb socket(domain int, typ int, proto int) (fd int, err error)
  424. //sysnb socketpair(domain int, typ int, proto int, fd *[2]int32) (err error)
  425. //sysnb getpeername(fd int, rsa *RawSockaddrAny, addrlen *_Socklen) (err error)
  426. //sysnb getsockname(fd int, rsa *RawSockaddrAny, addrlen *_Socklen) (err error)
  427. //sys recvfrom(fd int, p []byte, flags int, from *RawSockaddrAny, fromlen *_Socklen) (n int, err error)
  428. //sys sendto(s int, buf []byte, flags int, to unsafe.Pointer, addrlen _Socklen) (err error)
  429. //sys recvmsg(s int, msg *Msghdr, flags int) (n int, err error)
  430. //sys sendmsg(s int, msg *Msghdr, flags int) (n int, err error)
  431. //sys munmap(addr uintptr, length uintptr) (err error)
  432. var mapper = &mmapper{
  433. active: make(map[*byte][]byte),
  434. mmap: mmap,
  435. munmap: munmap,
  436. }
  437. func Mmap(fd int, offset int64, length int, prot int, flags int) (data []byte, err error) {
  438. return mapper.Mmap(fd, offset, length, prot, flags)
  439. }
  440. func Munmap(b []byte) (err error) {
  441. return mapper.Munmap(b)
  442. }
  443. //sys Madvise(b []byte, advice int) (err error)
  444. //sys Mprotect(b []byte, prot int) (err error)
  445. //sys Mlock(b []byte) (err error)
  446. //sys Mlockall(flags int) (err error)
  447. //sys Msync(b []byte, flags int) (err error)
  448. //sys Munlock(b []byte) (err error)
  449. //sys Munlockall() (err error)
  450. //sysnb pipe(p *[2]_C_int) (err error)
  451. func Pipe(p []int) (err error) {
  452. if len(p) != 2 {
  453. return EINVAL
  454. }
  455. var pp [2]_C_int
  456. err = pipe(&pp)
  457. p[0] = int(pp[0])
  458. p[1] = int(pp[1])
  459. return
  460. }
  461. //sys poll(fds *PollFd, nfds int, timeout int) (n int, err error)
  462. func Poll(fds []PollFd, timeout int) (n int, err error) {
  463. if len(fds) == 0 {
  464. return poll(nil, 0, timeout)
  465. }
  466. return poll(&fds[0], len(fds), timeout)
  467. }
  468. //sys gettimeofday(tv *Timeval, tzp *Timezone) (err error)
  469. //sysnb Time(t *Time_t) (tt Time_t, err error)
  470. //sys Utime(path string, buf *Utimbuf) (err error)