syscall_aix.go 15 KB

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