syscall_aix.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  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. func direntIno(buf []byte) (uint64, bool) {
  246. return readInt(buf, unsafe.Offsetof(Dirent{}.Ino), unsafe.Sizeof(Dirent{}.Ino))
  247. }
  248. func direntReclen(buf []byte) (uint64, bool) {
  249. return readInt(buf, unsafe.Offsetof(Dirent{}.Reclen), unsafe.Sizeof(Dirent{}.Reclen))
  250. }
  251. func direntNamlen(buf []byte) (uint64, bool) {
  252. reclen, ok := direntReclen(buf)
  253. if !ok {
  254. return 0, false
  255. }
  256. return reclen - uint64(unsafe.Offsetof(Dirent{}.Name)), true
  257. }
  258. //sys getdirent(fd int, buf []byte) (n int, err error)
  259. func Getdents(fd int, buf []byte) (n int, err error) {
  260. return getdirent(fd, buf)
  261. }
  262. //sys wait4(pid Pid_t, status *_C_int, options int, rusage *Rusage) (wpid Pid_t, err error)
  263. func Wait4(pid int, wstatus *WaitStatus, options int, rusage *Rusage) (wpid int, err error) {
  264. var status _C_int
  265. var r Pid_t
  266. err = ERESTART
  267. // AIX wait4 may return with ERESTART errno, while the processus is still
  268. // active.
  269. for err == ERESTART {
  270. r, err = wait4(Pid_t(pid), &status, options, rusage)
  271. }
  272. wpid = int(r)
  273. if wstatus != nil {
  274. *wstatus = WaitStatus(status)
  275. }
  276. return
  277. }
  278. /*
  279. * Wait
  280. */
  281. type WaitStatus uint32
  282. func (w WaitStatus) Stopped() bool { return w&0x40 != 0 }
  283. func (w WaitStatus) StopSignal() Signal {
  284. if !w.Stopped() {
  285. return -1
  286. }
  287. return Signal(w>>8) & 0xFF
  288. }
  289. func (w WaitStatus) Exited() bool { return w&0xFF == 0 }
  290. func (w WaitStatus) ExitStatus() int {
  291. if !w.Exited() {
  292. return -1
  293. }
  294. return int((w >> 8) & 0xFF)
  295. }
  296. func (w WaitStatus) Signaled() bool { return w&0x40 == 0 && w&0xFF != 0 }
  297. func (w WaitStatus) Signal() Signal {
  298. if !w.Signaled() {
  299. return -1
  300. }
  301. return Signal(w>>16) & 0xFF
  302. }
  303. func (w WaitStatus) Continued() bool { return w&0x01000000 != 0 }
  304. func (w WaitStatus) CoreDump() bool { return w&0x80 == 0x80 }
  305. func (w WaitStatus) TrapCause() int { return -1 }
  306. //sys ioctl(fd int, req uint, arg uintptr) (err error)
  307. // fcntl must never be called with cmd=F_DUP2FD because it doesn't work on AIX
  308. // There is no way to create a custom fcntl and to keep //sys fcntl easily,
  309. // Therefore, the programmer must call dup2 instead of fcntl in this case.
  310. // FcntlInt performs a fcntl syscall on fd with the provided command and argument.
  311. //sys FcntlInt(fd uintptr, cmd int, arg int) (r int,err error) = fcntl
  312. // FcntlFlock performs a fcntl syscall for the F_GETLK, F_SETLK or F_SETLKW command.
  313. //sys FcntlFlock(fd uintptr, cmd int, lk *Flock_t) (err error) = fcntl
  314. //sys fcntl(fd int, cmd int, arg int) (val int, err error)
  315. /*
  316. * Direct access
  317. */
  318. //sys Acct(path string) (err error)
  319. //sys Chdir(path string) (err error)
  320. //sys Chroot(path string) (err error)
  321. //sys Close(fd int) (err error)
  322. //sys Dup(oldfd int) (fd int, err error)
  323. //sys Exit(code int)
  324. //sys Faccessat(dirfd int, path string, mode uint32, flags int) (err error)
  325. //sys Fchdir(fd int) (err error)
  326. //sys Fchmod(fd int, mode uint32) (err error)
  327. //sys Fchmodat(dirfd int, path string, mode uint32, flags int) (err error)
  328. //sys Fchownat(dirfd int, path string, uid int, gid int, flags int) (err error)
  329. //sys Fdatasync(fd int) (err error)
  330. //sys Fsync(fd int) (err error)
  331. // readdir_r
  332. //sysnb Getpgid(pid int) (pgid int, err error)
  333. //sys Getpgrp() (pid int)
  334. //sysnb Getpid() (pid int)
  335. //sysnb Getppid() (ppid int)
  336. //sys Getpriority(which int, who int) (prio int, err error)
  337. //sysnb Getrusage(who int, rusage *Rusage) (err error)
  338. //sysnb Getsid(pid int) (sid int, err error)
  339. //sysnb Kill(pid int, sig Signal) (err error)
  340. //sys Klogctl(typ int, buf []byte) (n int, err error) = syslog
  341. //sys Mkdir(dirfd int, path string, mode uint32) (err error)
  342. //sys Mkdirat(dirfd int, path string, mode uint32) (err error)
  343. //sys Mkfifo(path string, mode uint32) (err error)
  344. //sys Mknod(path string, mode uint32, dev int) (err error)
  345. //sys Mknodat(dirfd int, path string, mode uint32, dev int) (err error)
  346. //sys Nanosleep(time *Timespec, leftover *Timespec) (err error)
  347. //sys Open(path string, mode int, perm uint32) (fd int, err error) = open64
  348. //sys Openat(dirfd int, path string, flags int, mode uint32) (fd int, err error)
  349. //sys read(fd int, p []byte) (n int, err error)
  350. //sys Readlink(path string, buf []byte) (n int, err error)
  351. //sys Renameat(olddirfd int, oldpath string, newdirfd int, newpath string) (err error)
  352. //sys Setdomainname(p []byte) (err error)
  353. //sys Sethostname(p []byte) (err error)
  354. //sysnb Setpgid(pid int, pgid int) (err error)
  355. //sysnb Setsid() (pid int, err error)
  356. //sysnb Settimeofday(tv *Timeval) (err error)
  357. //sys Setuid(uid int) (err error)
  358. //sys Setgid(uid int) (err error)
  359. //sys Setpriority(which int, who int, prio int) (err error)
  360. //sys Statx(dirfd int, path string, flags int, mask int, stat *Statx_t) (err error)
  361. //sys Sync()
  362. //sysnb Times(tms *Tms) (ticks uintptr, err error)
  363. //sysnb Umask(mask int) (oldmask int)
  364. //sysnb Uname(buf *Utsname) (err error)
  365. //sys Unlink(path string) (err error)
  366. //sys Unlinkat(dirfd int, path string, flags int) (err error)
  367. //sys Ustat(dev int, ubuf *Ustat_t) (err error)
  368. //sys write(fd int, p []byte) (n int, err error)
  369. //sys readlen(fd int, p *byte, np int) (n int, err error) = read
  370. //sys writelen(fd int, p *byte, np int) (n int, err error) = write
  371. //sys Dup2(oldfd int, newfd int) (err error)
  372. //sys Fadvise(fd int, offset int64, length int64, advice int) (err error) = posix_fadvise64
  373. //sys Fchown(fd int, uid int, gid int) (err error)
  374. //sys fstat(fd int, stat *Stat_t) (err error)
  375. //sys fstatat(dirfd int, path string, stat *Stat_t, flags int) (err error) = fstatat
  376. //sys Fstatfs(fd int, buf *Statfs_t) (err error)
  377. //sys Ftruncate(fd int, length int64) (err error)
  378. //sysnb Getegid() (egid int)
  379. //sysnb Geteuid() (euid int)
  380. //sysnb Getgid() (gid int)
  381. //sysnb Getuid() (uid int)
  382. //sys Lchown(path string, uid int, gid int) (err error)
  383. //sys Listen(s int, n int) (err error)
  384. //sys lstat(path string, stat *Stat_t) (err error)
  385. //sys Pause() (err error)
  386. //sys Pread(fd int, p []byte, offset int64) (n int, err error) = pread64
  387. //sys Pwrite(fd int, p []byte, offset int64) (n int, err error) = pwrite64
  388. //sys Select(nfd int, r *FdSet, w *FdSet, e *FdSet, timeout *Timeval) (n int, err error)
  389. //sys Pselect(nfd int, r *FdSet, w *FdSet, e *FdSet, timeout *Timespec, sigmask *Sigset_t) (n int, err error)
  390. //sysnb Setregid(rgid int, egid int) (err error)
  391. //sysnb Setreuid(ruid int, euid int) (err error)
  392. //sys Shutdown(fd int, how int) (err error)
  393. //sys Splice(rfd int, roff *int64, wfd int, woff *int64, len int, flags int) (n int64, err error)
  394. //sys stat(path string, statptr *Stat_t) (err error)
  395. //sys Statfs(path string, buf *Statfs_t) (err error)
  396. //sys Truncate(path string, length int64) (err error)
  397. //sys bind(s int, addr unsafe.Pointer, addrlen _Socklen) (err error)
  398. //sys connect(s int, addr unsafe.Pointer, addrlen _Socklen) (err error)
  399. //sysnb getgroups(n int, list *_Gid_t) (nn int, err error)
  400. //sysnb setgroups(n int, list *_Gid_t) (err error)
  401. //sys getsockopt(s int, level int, name int, val unsafe.Pointer, vallen *_Socklen) (err error)
  402. //sys setsockopt(s int, level int, name int, val unsafe.Pointer, vallen uintptr) (err error)
  403. //sysnb socket(domain int, typ int, proto int) (fd int, err error)
  404. //sysnb socketpair(domain int, typ int, proto int, fd *[2]int32) (err error)
  405. //sysnb getpeername(fd int, rsa *RawSockaddrAny, addrlen *_Socklen) (err error)
  406. //sysnb getsockname(fd int, rsa *RawSockaddrAny, addrlen *_Socklen) (err error)
  407. //sys recvfrom(fd int, p []byte, flags int, from *RawSockaddrAny, fromlen *_Socklen) (n int, err error)
  408. //sys sendto(s int, buf []byte, flags int, to unsafe.Pointer, addrlen _Socklen) (err error)
  409. // In order to use msghdr structure with Control, Controllen, nrecvmsg and nsendmsg must be used.
  410. //sys recvmsg(s int, msg *Msghdr, flags int) (n int, err error) = nrecvmsg
  411. //sys sendmsg(s int, msg *Msghdr, flags int) (n int, err error) = nsendmsg
  412. //sys munmap(addr uintptr, length uintptr) (err error)
  413. var mapper = &mmapper{
  414. active: make(map[*byte][]byte),
  415. mmap: mmap,
  416. munmap: munmap,
  417. }
  418. func Mmap(fd int, offset int64, length int, prot int, flags int) (data []byte, err error) {
  419. return mapper.Mmap(fd, offset, length, prot, flags)
  420. }
  421. func Munmap(b []byte) (err error) {
  422. return mapper.Munmap(b)
  423. }
  424. //sys Madvise(b []byte, advice int) (err error)
  425. //sys Mprotect(b []byte, prot int) (err error)
  426. //sys Mlock(b []byte) (err error)
  427. //sys Mlockall(flags int) (err error)
  428. //sys Msync(b []byte, flags int) (err error)
  429. //sys Munlock(b []byte) (err error)
  430. //sys Munlockall() (err error)
  431. //sysnb pipe(p *[2]_C_int) (err error)
  432. func Pipe(p []int) (err error) {
  433. if len(p) != 2 {
  434. return EINVAL
  435. }
  436. var pp [2]_C_int
  437. err = pipe(&pp)
  438. p[0] = int(pp[0])
  439. p[1] = int(pp[1])
  440. return
  441. }
  442. //sys poll(fds *PollFd, nfds int, timeout int) (n int, err error)
  443. func Poll(fds []PollFd, timeout int) (n int, err error) {
  444. if len(fds) == 0 {
  445. return poll(nil, 0, timeout)
  446. }
  447. return poll(&fds[0], len(fds), timeout)
  448. }
  449. //sys gettimeofday(tv *Timeval, tzp *Timezone) (err error)
  450. //sysnb Time(t *Time_t) (tt Time_t, err error)
  451. //sys Utime(path string, buf *Utimbuf) (err error)
  452. //sys Getsystemcfg(label int) (n uint64)
  453. //sys umount(target string) (err error)
  454. func Unmount(target string, flags int) (err error) {
  455. if flags != 0 {
  456. // AIX doesn't have any flags for umount.
  457. return ENOSYS
  458. }
  459. return umount(target)
  460. }