libc_darwin_arm64.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. // Copyright 2020 The Libc 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. package libc // import "modernc.org/libc"
  5. import (
  6. "strings"
  7. "time"
  8. "unsafe"
  9. "golang.org/x/sys/unix"
  10. "modernc.org/libc/fcntl"
  11. "modernc.org/libc/signal"
  12. "modernc.org/libc/sys/types"
  13. "modernc.org/libc/utime"
  14. )
  15. // int sigaction(int signum, const struct sigaction *act, struct sigaction *oldact);
  16. func Xsigaction(t *TLS, signum int32, act, oldact uintptr) int32 {
  17. var kact, koldact uintptr
  18. if act != 0 {
  19. sz := int(unsafe.Sizeof(signal.X__sigaction{}))
  20. kact = t.Alloc(sz)
  21. defer t.Free(sz)
  22. (*signal.X__sigaction)(unsafe.Pointer(kact)).F__sigaction_u.F__sa_handler = (*signal.Sigaction)(unsafe.Pointer(act)).F__sigaction_u.F__sa_handler
  23. (*signal.X__sigaction)(unsafe.Pointer(kact)).Fsa_flags = (*signal.Sigaction)(unsafe.Pointer(act)).Fsa_flags
  24. Xmemcpy(t, kact+unsafe.Offsetof(signal.X__sigaction{}.Fsa_mask), act+unsafe.Offsetof(signal.Sigaction{}.Fsa_mask), types.Size_t(unsafe.Sizeof(signal.Sigset_t(0))))
  25. }
  26. if oldact != 0 {
  27. panic(todo(""))
  28. }
  29. if _, _, err := unix.Syscall6(unix.SYS_SIGACTION, uintptr(signum), kact, koldact, unsafe.Sizeof(signal.Sigset_t(0)), 0, 0); err != 0 {
  30. t.setErrno(err)
  31. return -1
  32. }
  33. if oldact != 0 {
  34. panic(todo(""))
  35. }
  36. return 0
  37. }
  38. // int fcntl(int fd, int cmd, ... /* arg */ );
  39. func Xfcntl64(t *TLS, fd, cmd int32, args uintptr) (r int32) {
  40. var err error
  41. var p uintptr
  42. var i int
  43. switch cmd {
  44. case fcntl.F_GETLK, fcntl.F_SETLK, fcntl.F_SETLKW:
  45. p = *(*uintptr)(unsafe.Pointer(args))
  46. err = unix.FcntlFlock(uintptr(fd), int(cmd), (*unix.Flock_t)(unsafe.Pointer(p)))
  47. case fcntl.F_GETFL, fcntl.F_FULLFSYNC:
  48. i, err = unix.FcntlInt(uintptr(fd), int(cmd), 0)
  49. r = int32(i)
  50. case fcntl.F_SETFD, fcntl.F_SETFL:
  51. arg := *(*int32)(unsafe.Pointer(args))
  52. _, err = unix.FcntlInt(uintptr(fd), int(cmd), int(arg))
  53. default:
  54. panic(todo("%v: %v %v", origin(1), fd, cmd))
  55. }
  56. if err != nil {
  57. if dmesgs {
  58. dmesg("%v: fd %v cmd %v p %#x: %v FAIL", origin(1), fcntlCmdStr(fd), cmd, p, err)
  59. }
  60. t.setErrno(err)
  61. return -1
  62. }
  63. if dmesgs {
  64. dmesg("%v: %d %s %#x: ok", origin(1), fd, fcntlCmdStr(cmd), p)
  65. }
  66. return r
  67. }
  68. // int lstat(const char *pathname, struct stat *statbuf);
  69. func Xlstat64(t *TLS, pathname, statbuf uintptr) int32 {
  70. if err := unix.Lstat(GoString(pathname), (*unix.Stat_t)(unsafe.Pointer(statbuf))); err != nil {
  71. if dmesgs {
  72. dmesg("%v: %q: %v FAIL", origin(1), GoString(pathname), err)
  73. }
  74. t.setErrno(err)
  75. return -1
  76. }
  77. if dmesgs {
  78. dmesg("%v: %q: ok", origin(1), GoString(pathname))
  79. }
  80. return 0
  81. }
  82. // int stat(const char *pathname, struct stat *statbuf);
  83. func Xstat64(t *TLS, pathname, statbuf uintptr) int32 {
  84. if err := unix.Stat(GoString(pathname), (*unix.Stat_t)(unsafe.Pointer(statbuf))); err != nil {
  85. if dmesgs {
  86. dmesg("%v: %q: %v FAIL", origin(1), GoString(pathname), err)
  87. }
  88. t.setErrno(err)
  89. return -1
  90. }
  91. if dmesgs {
  92. dmesg("%v: %q: ok", origin(1), GoString(pathname))
  93. }
  94. return 0
  95. }
  96. // int fstatfs(int fd, struct statfs *buf);
  97. func Xfstatfs(t *TLS, fd int32, buf uintptr) int32 {
  98. if err := unix.Fstatfs(int(fd), (*unix.Statfs_t)(unsafe.Pointer(buf))); err != nil {
  99. if dmesgs {
  100. dmesg("%v: %v: %v FAIL", origin(1), fd, err)
  101. }
  102. t.setErrno(err)
  103. return -1
  104. }
  105. if dmesgs {
  106. dmesg("%v: %v: ok", origin(1), fd)
  107. }
  108. return 0
  109. }
  110. // int statfs(const char *path, struct statfs *buf);
  111. func Xstatfs(t *TLS, path uintptr, buf uintptr) int32 {
  112. if err := unix.Statfs(GoString(path), (*unix.Statfs_t)(unsafe.Pointer(buf))); err != nil {
  113. if dmesgs {
  114. dmesg("%v: %q: %v FAIL", origin(1), GoString(path), err)
  115. }
  116. t.setErrno(err)
  117. return -1
  118. }
  119. if dmesgs {
  120. dmesg("%v: %q: ok", origin(1), GoString(path))
  121. }
  122. return 0
  123. }
  124. // int fstat(int fd, struct stat *statbuf);
  125. func Xfstat64(t *TLS, fd int32, statbuf uintptr) int32 {
  126. if err := unix.Fstat(int(fd), (*unix.Stat_t)(unsafe.Pointer(statbuf))); err != nil {
  127. if dmesgs {
  128. dmesg("%v: fd %d: %v FAIL", origin(1), fd, err)
  129. }
  130. t.setErrno(err)
  131. return -1
  132. }
  133. if dmesgs {
  134. dmesg("%v: fd %d: ok", origin(1), fd)
  135. }
  136. return 0
  137. }
  138. // off64_t lseek64(int fd, off64_t offset, int whence);
  139. func Xlseek64(t *TLS, fd int32, offset types.Off_t, whence int32) types.Off_t {
  140. n, err := unix.Seek(int(fd), int64(offset), int(whence))
  141. if err != nil {
  142. if dmesgs {
  143. dmesg("%v: %v FAIL", origin(1), err)
  144. }
  145. t.setErrno(err)
  146. return -1
  147. }
  148. if dmesgs {
  149. dmesg("%v: ok", origin(1))
  150. }
  151. return types.Off_t(n)
  152. }
  153. // int utime(const char *filename, const struct utimbuf *times);
  154. func Xutime(t *TLS, filename, times uintptr) int32 {
  155. var a []unix.Timeval
  156. if times != 0 {
  157. a = make([]unix.Timeval, 2)
  158. a[0].Sec = (*utime.Utimbuf)(unsafe.Pointer(times)).Factime
  159. a[1].Sec = (*utime.Utimbuf)(unsafe.Pointer(times)).Fmodtime
  160. }
  161. if err := unix.Utimes(GoString(filename), a); err != nil {
  162. if dmesgs {
  163. dmesg("%v: %v FAIL", origin(1), err)
  164. }
  165. t.setErrno(err)
  166. return -1
  167. }
  168. if dmesgs {
  169. dmesg("%v: ok", origin(1))
  170. }
  171. return 0
  172. }
  173. // unsigned int alarm(unsigned int seconds);
  174. func Xalarm(t *TLS, seconds uint32) uint32 {
  175. panic(todo(""))
  176. // n, _, err := unix.Syscall(unix.SYS_ALARM, uintptr(seconds), 0, 0)
  177. // if err != 0 {
  178. // panic(todo(""))
  179. // }
  180. // return uint32(n)
  181. }
  182. // time_t time(time_t *tloc);
  183. func Xtime(t *TLS, tloc uintptr) types.Time_t {
  184. n := time.Now().UTC().Unix()
  185. if tloc != 0 {
  186. *(*types.Time_t)(unsafe.Pointer(tloc)) = types.Time_t(n)
  187. }
  188. return types.Time_t(n)
  189. }
  190. // // int getrlimit(int resource, struct rlimit *rlim);
  191. // func Xgetrlimit64(t *TLS, resource int32, rlim uintptr) int32 {
  192. // if _, _, err := unix.Syscall(unix.SYS_GETRLIMIT, uintptr(resource), uintptr(rlim), 0); err != 0 {
  193. // t.setErrno(err)
  194. // return -1
  195. // }
  196. //
  197. // return 0
  198. // }
  199. // int mkdir(const char *path, mode_t mode);
  200. func Xmkdir(t *TLS, path uintptr, mode types.Mode_t) int32 {
  201. if err := unix.Mkdir(GoString(path), uint32(mode)); err != nil {
  202. if dmesgs {
  203. dmesg("%v: %q: %v FAIL", origin(1), GoString(path), err)
  204. }
  205. t.setErrno(err)
  206. return -1
  207. }
  208. if dmesgs {
  209. dmesg("%v: %q: ok", origin(1), GoString(path))
  210. }
  211. return 0
  212. }
  213. // int symlink(const char *target, const char *linkpath);
  214. func Xsymlink(t *TLS, target, linkpath uintptr) int32 {
  215. if err := unix.Symlink(GoString(target), GoString(linkpath)); err != nil {
  216. if dmesgs {
  217. dmesg("%v: %v FAIL", origin(1), err)
  218. }
  219. t.setErrno(err)
  220. return -1
  221. }
  222. if dmesgs {
  223. dmesg("%v: ok", origin(1))
  224. }
  225. return 0
  226. }
  227. // int chmod(const char *pathname, mode_t mode)
  228. func Xchmod(t *TLS, pathname uintptr, mode types.Mode_t) int32 {
  229. if err := unix.Chmod(GoString(pathname), uint32(mode)); err != nil {
  230. if dmesgs {
  231. dmesg("%v: %q %#o: %v FAIL", origin(1), GoString(pathname), mode, err)
  232. }
  233. t.setErrno(err)
  234. return -1
  235. }
  236. if dmesgs {
  237. dmesg("%v: %q %#o: ok", origin(1), GoString(pathname), mode)
  238. }
  239. return 0
  240. }
  241. // int utimes(const char *filename, const struct timeval times[2]);
  242. func Xutimes(t *TLS, filename, times uintptr) int32 {
  243. var a []unix.Timeval
  244. if times != 0 {
  245. a = make([]unix.Timeval, 2)
  246. a[0] = *(*unix.Timeval)(unsafe.Pointer(times))
  247. a[1] = *(*unix.Timeval)(unsafe.Pointer(times + unsafe.Sizeof(unix.Timeval{})))
  248. }
  249. if err := unix.Utimes(GoString(filename), a); err != nil {
  250. if dmesgs {
  251. dmesg("%v: %v FAIL", origin(1), err)
  252. }
  253. t.setErrno(err)
  254. return -1
  255. }
  256. if dmesgs {
  257. dmesg("%v: ok", origin(1))
  258. }
  259. return 0
  260. }
  261. // int unlink(const char *pathname);
  262. func Xunlink(t *TLS, pathname uintptr) int32 {
  263. if err := unix.Unlink(GoString(pathname)); err != nil {
  264. if dmesgs {
  265. dmesg("%v: %q: %v", origin(1), GoString(pathname), err)
  266. }
  267. t.setErrno(err)
  268. return -1
  269. }
  270. if dmesgs {
  271. dmesg("%v: ok", origin(1))
  272. }
  273. return 0
  274. }
  275. // int access(const char *pathname, int mode);
  276. func Xaccess(t *TLS, pathname uintptr, mode int32) int32 {
  277. if err := unix.Access(GoString(pathname), uint32(mode)); err != nil {
  278. if dmesgs {
  279. dmesg("%v: %q %#o: %v FAIL", origin(1), GoString(pathname), mode, err)
  280. }
  281. t.setErrno(err)
  282. return -1
  283. }
  284. if dmesgs {
  285. dmesg("%v: %q %#o: ok", origin(1), GoString(pathname), mode)
  286. }
  287. return 0
  288. }
  289. // int rename(const char *oldpath, const char *newpath);
  290. func Xrename(t *TLS, oldpath, newpath uintptr) int32 {
  291. if err := unix.Rename(GoString(oldpath), GoString(newpath)); err != nil {
  292. if dmesgs {
  293. dmesg("%v: %v FAIL", origin(1), err)
  294. }
  295. t.setErrno(err)
  296. return -1
  297. }
  298. if dmesgs {
  299. dmesg("%v: ok", origin(1))
  300. }
  301. return 0
  302. }
  303. // int mknod(const char *pathname, mode_t mode, dev_t dev);
  304. func Xmknod(t *TLS, pathname uintptr, mode types.Mode_t, dev types.Dev_t) int32 {
  305. panic(todo(""))
  306. // if _, _, err := unix.Syscall(unix.SYS_MKNOD, pathname, uintptr(mode), uintptr(dev)); err != 0 {
  307. // t.setErrno(err)
  308. // return -1
  309. // }
  310. // return 0
  311. }
  312. // int chown(const char *pathname, uid_t owner, gid_t group);
  313. func Xchown(t *TLS, pathname uintptr, owner types.Uid_t, group types.Gid_t) int32 {
  314. panic(todo(""))
  315. // if _, _, err := unix.Syscall(unix.SYS_CHOWN, pathname, uintptr(owner), uintptr(group)); err != 0 {
  316. // t.setErrno(err)
  317. // return -1
  318. // }
  319. // return 0
  320. }
  321. // int link(const char *oldpath, const char *newpath);
  322. func Xlink(t *TLS, oldpath, newpath uintptr) int32 {
  323. if _, _, err := unix.Syscall(unix.SYS_LINK, oldpath, newpath, 0); err != 0 {
  324. t.setErrno(err)
  325. return -1
  326. }
  327. return 0
  328. }
  329. // int dup2(int oldfd, int newfd);
  330. func Xdup2(t *TLS, oldfd, newfd int32) int32 {
  331. panic(todo(""))
  332. // n, _, err := unix.Syscall(unix.SYS_DUP2, uintptr(oldfd), uintptr(newfd), 0)
  333. // if err != 0 {
  334. // t.setErrno(err)
  335. // return -1
  336. // }
  337. // return int32(n)
  338. }
  339. // ssize_t readlink(const char *restrict path, char *restrict buf, size_t bufsize);
  340. func Xreadlink(t *TLS, path, buf uintptr, bufsize types.Size_t) types.Ssize_t {
  341. var n int
  342. var err error
  343. switch {
  344. case buf == 0 || bufsize == 0:
  345. n, err = unix.Readlink(GoString(path), nil)
  346. default:
  347. n, err = unix.Readlink(GoString(path), (*RawMem)(unsafe.Pointer(buf))[:bufsize:bufsize])
  348. }
  349. if err != nil {
  350. if dmesgs {
  351. dmesg("%v: %v FAIL", err)
  352. }
  353. t.setErrno(err)
  354. return -1
  355. }
  356. if dmesgs {
  357. dmesg("%v: ok")
  358. }
  359. return types.Ssize_t(n)
  360. }
  361. // FILE *fopen64(const char *pathname, const char *mode);
  362. func Xfopen64(t *TLS, pathname, mode uintptr) uintptr {
  363. m := strings.ReplaceAll(GoString(mode), "b", "")
  364. var flags int
  365. switch m {
  366. case "r":
  367. flags = fcntl.O_RDONLY
  368. case "r+":
  369. flags = fcntl.O_RDWR
  370. case "w":
  371. flags = fcntl.O_WRONLY | fcntl.O_CREAT | fcntl.O_TRUNC
  372. case "w+":
  373. flags = fcntl.O_RDWR | fcntl.O_CREAT | fcntl.O_TRUNC
  374. case "a":
  375. flags = fcntl.O_WRONLY | fcntl.O_CREAT | fcntl.O_APPEND
  376. case "a+":
  377. flags = fcntl.O_RDWR | fcntl.O_CREAT | fcntl.O_APPEND
  378. default:
  379. panic(m)
  380. }
  381. fd, err := unix.Open(GoString(pathname), int(flags), 0666)
  382. if err != nil {
  383. if dmesgs {
  384. dmesg("%v: %q %q: %v FAIL", origin(1), GoString(pathname), GoString(mode), err)
  385. }
  386. t.setErrno(err)
  387. return 0
  388. }
  389. if dmesgs {
  390. dmesg("%v: %q %q: fd %v", origin(1), GoString(pathname), GoString(mode), fd)
  391. }
  392. if p := newFile(t, int32(fd)); p != 0 {
  393. return p
  394. }
  395. panic("OOM")
  396. }