libc_darwin_amd64.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. "unsafe"
  8. "golang.org/x/sys/unix"
  9. "modernc.org/libc/fcntl"
  10. "modernc.org/libc/signal"
  11. "modernc.org/libc/sys/types"
  12. "modernc.org/libc/utime"
  13. )
  14. // int sigaction(int signum, const struct sigaction *act, struct sigaction *oldact);
  15. func Xsigaction(t *TLS, signum int32, act, oldact uintptr) int32 {
  16. var kact, koldact uintptr
  17. if act != 0 {
  18. sz := int(unsafe.Sizeof(signal.X__sigaction{}))
  19. kact = t.Alloc(sz)
  20. defer t.Free(sz)
  21. (*signal.X__sigaction)(unsafe.Pointer(kact)).F__sigaction_u.F__sa_handler = (*signal.Sigaction)(unsafe.Pointer(act)).F__sigaction_u.F__sa_handler
  22. (*signal.X__sigaction)(unsafe.Pointer(kact)).Fsa_flags = (*signal.Sigaction)(unsafe.Pointer(act)).Fsa_flags
  23. 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))))
  24. }
  25. if oldact != 0 {
  26. panic(todo(""))
  27. }
  28. if _, _, err := unix.Syscall6(unix.SYS_SIGACTION, uintptr(signum), kact, koldact, unsafe.Sizeof(signal.Sigset_t(0)), 0, 0); err != 0 {
  29. t.setErrno(err)
  30. return -1
  31. }
  32. if oldact != 0 {
  33. panic(todo(""))
  34. }
  35. return 0
  36. }
  37. // int fcntl(int fd, int cmd, ... /* arg */ );
  38. func Xfcntl64(t *TLS, fd, cmd int32, args uintptr) (r int32) {
  39. var err error
  40. var p uintptr
  41. var i int
  42. switch cmd {
  43. case fcntl.F_GETLK, fcntl.F_SETLK:
  44. p = *(*uintptr)(unsafe.Pointer(args))
  45. err = unix.FcntlFlock(uintptr(fd), int(cmd), (*unix.Flock_t)(unsafe.Pointer(p)))
  46. case fcntl.F_GETFL, fcntl.F_FULLFSYNC:
  47. i, err = unix.FcntlInt(uintptr(fd), int(cmd), 0)
  48. r = int32(i)
  49. case fcntl.F_SETFD, fcntl.F_SETFL:
  50. arg := *(*int32)(unsafe.Pointer(args))
  51. _, err = unix.FcntlInt(uintptr(fd), int(cmd), int(arg))
  52. default:
  53. panic(todo("%v: %v %v", origin(1), fd, cmd))
  54. }
  55. if err != nil {
  56. if dmesgs {
  57. dmesg("%v: fd %v cmd %v p %#x: %v FAIL", origin(1), fcntlCmdStr(fd), cmd, p, err)
  58. }
  59. t.setErrno(err)
  60. return -1
  61. }
  62. if dmesgs {
  63. dmesg("%v: %d %s %#x: ok", origin(1), fd, fcntlCmdStr(cmd), p)
  64. }
  65. return r
  66. }
  67. // int lstat(const char *pathname, struct stat *statbuf);
  68. func Xlstat64(t *TLS, pathname, statbuf uintptr) int32 {
  69. if err := unix.Lstat(GoString(pathname), (*unix.Stat_t)(unsafe.Pointer(statbuf))); err != nil {
  70. if dmesgs {
  71. dmesg("%v: %q: %v FAIL", origin(1), GoString(pathname), err)
  72. }
  73. t.setErrno(err)
  74. return -1
  75. }
  76. if dmesgs {
  77. dmesg("%v: %q: ok", origin(1), GoString(pathname))
  78. }
  79. return 0
  80. }
  81. // int stat(const char *pathname, struct stat *statbuf);
  82. func Xstat64(t *TLS, pathname, statbuf uintptr) int32 {
  83. if err := unix.Stat(GoString(pathname), (*unix.Stat_t)(unsafe.Pointer(statbuf))); err != nil {
  84. if dmesgs {
  85. dmesg("%v: %q: %v FAIL", origin(1), GoString(pathname), err)
  86. }
  87. t.setErrno(err)
  88. return -1
  89. }
  90. if dmesgs {
  91. dmesg("%v: %q: ok", origin(1), GoString(pathname))
  92. }
  93. return 0
  94. }
  95. // int fstatfs(int fd, struct statfs *buf);
  96. func Xfstatfs(t *TLS, fd int32, buf uintptr) int32 {
  97. if err := unix.Fstatfs(int(fd), (*unix.Statfs_t)(unsafe.Pointer(buf))); err != nil {
  98. if dmesgs {
  99. dmesg("%v: %v: %v FAIL", origin(1), fd, err)
  100. }
  101. t.setErrno(err)
  102. return -1
  103. }
  104. if dmesgs {
  105. dmesg("%v: %v: ok", origin(1), fd)
  106. }
  107. return 0
  108. }
  109. // int statfs(const char *path, struct statfs *buf);
  110. func Xstatfs(t *TLS, path uintptr, buf uintptr) int32 {
  111. if err := unix.Statfs(GoString(path), (*unix.Statfs_t)(unsafe.Pointer(buf))); err != nil {
  112. if dmesgs {
  113. dmesg("%v: %q: %v FAIL", origin(1), GoString(path), err)
  114. }
  115. t.setErrno(err)
  116. return -1
  117. }
  118. if dmesgs {
  119. dmesg("%v: %q: ok", origin(1), GoString(path))
  120. }
  121. return 0
  122. }
  123. // int fstat(int fd, struct stat *statbuf);
  124. func Xfstat64(t *TLS, fd int32, statbuf uintptr) int32 {
  125. if err := unix.Fstat(int(fd), (*unix.Stat_t)(unsafe.Pointer(statbuf))); err != nil {
  126. if dmesgs {
  127. dmesg("%v: fd %d: %v FAIL", origin(1), fd, err)
  128. }
  129. t.setErrno(err)
  130. return -1
  131. }
  132. if dmesgs {
  133. dmesg("%v: fd %d: ok", origin(1), fd)
  134. }
  135. return 0
  136. }
  137. // off64_t lseek64(int fd, off64_t offset, int whence);
  138. func Xlseek64(t *TLS, fd int32, offset types.Off_t, whence int32) types.Off_t {
  139. n, err := unix.Seek(int(fd), int64(offset), int(whence))
  140. if err != nil {
  141. if dmesgs {
  142. dmesg("%v: %v FAIL", origin(1), err)
  143. }
  144. t.setErrno(err)
  145. return -1
  146. }
  147. if dmesgs {
  148. dmesg("%v: ok", origin(1))
  149. }
  150. return types.Off_t(n)
  151. }
  152. // int utime(const char *filename, const struct utimbuf *times);
  153. func Xutime(t *TLS, filename, times uintptr) int32 {
  154. var a []unix.Timeval
  155. if times != 0 {
  156. a = make([]unix.Timeval, 2)
  157. a[0].Sec = (*utime.Utimbuf)(unsafe.Pointer(times)).Factime
  158. a[1].Sec = (*utime.Utimbuf)(unsafe.Pointer(times)).Fmodtime
  159. }
  160. if err := unix.Utimes(GoString(filename), a); err != nil {
  161. if dmesgs {
  162. dmesg("%v: %v FAIL", origin(1), err)
  163. }
  164. t.setErrno(err)
  165. return -1
  166. }
  167. if dmesgs {
  168. dmesg("%v: ok", origin(1))
  169. }
  170. return 0
  171. }
  172. // unsigned int alarm(unsigned int seconds);
  173. func Xalarm(t *TLS, seconds uint32) uint32 {
  174. panic(todo(""))
  175. // n, _, err := unix.Syscall(unix.SYS_ALARM, uintptr(seconds), 0, 0)
  176. // if err != 0 {
  177. // panic(todo(""))
  178. // }
  179. // return uint32(n)
  180. }
  181. // time_t time(time_t *tloc);
  182. func Xtime(t *TLS, tloc uintptr) types.Time_t {
  183. panic(todo(""))
  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. panic(todo(""))
  324. // if _, _, err := unix.Syscall(unix.SYS_LINK, oldpath, newpath, 0); err != 0 {
  325. // t.setErrno(err)
  326. // return -1
  327. // }
  328. // return 0
  329. }
  330. // int dup2(int oldfd, int newfd);
  331. func Xdup2(t *TLS, oldfd, newfd int32) int32 {
  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. }