syscall_aix.go 16 KB

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