libc_linux_386.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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. "os"
  7. "strings"
  8. "unsafe"
  9. "golang.org/x/sys/unix"
  10. "modernc.org/libc/errno"
  11. "modernc.org/libc/fcntl"
  12. "modernc.org/libc/signal"
  13. "modernc.org/libc/sys/stat"
  14. "modernc.org/libc/sys/types"
  15. )
  16. // int sigaction(int signum, const struct sigaction *act, struct sigaction *oldact);
  17. func Xsigaction(t *TLS, signum int32, act, oldact uintptr) int32 {
  18. // musl/arch/x32/ksigaction.h
  19. //
  20. // struct k_sigaction {
  21. // void (*handler)(int);
  22. // unsigned long flags;
  23. // void (*restorer)(void);
  24. // unsigned mask[2];
  25. // };
  26. type k_sigaction struct {
  27. handler uintptr
  28. flags ulong
  29. restorer uintptr
  30. mask [2]uint32
  31. }
  32. var kact, koldact uintptr
  33. if act != 0 {
  34. sz := int(unsafe.Sizeof(k_sigaction{}))
  35. kact = t.Alloc(sz)
  36. defer t.Free(sz)
  37. *(*k_sigaction)(unsafe.Pointer(kact)) = k_sigaction{
  38. handler: (*signal.Sigaction)(unsafe.Pointer(act)).F__sigaction_handler.Fsa_handler,
  39. flags: ulong((*signal.Sigaction)(unsafe.Pointer(act)).Fsa_flags),
  40. restorer: (*signal.Sigaction)(unsafe.Pointer(act)).Fsa_restorer,
  41. }
  42. Xmemcpy(t, kact+unsafe.Offsetof(k_sigaction{}.mask), act+unsafe.Offsetof(signal.Sigaction{}.Fsa_mask), types.Size_t(unsafe.Sizeof(k_sigaction{}.mask)))
  43. }
  44. if oldact != 0 {
  45. panic(todo(""))
  46. }
  47. if _, _, err := unix.Syscall6(unix.SYS_RT_SIGACTION, uintptr(signum), kact, koldact, unsafe.Sizeof(k_sigaction{}.mask), 0, 0); err != 0 {
  48. t.setErrno(err)
  49. return -1
  50. }
  51. if oldact != 0 {
  52. panic(todo(""))
  53. }
  54. return 0
  55. }
  56. // int fcntl(int fd, int cmd, ... /* arg */ );
  57. func Xfcntl64(t *TLS, fd, cmd int32, args uintptr) int32 {
  58. var arg uintptr
  59. if args != 0 {
  60. arg = *(*uintptr)(unsafe.Pointer(args))
  61. }
  62. if cmd == fcntl.F_SETFL {
  63. arg |= unix.O_LARGEFILE
  64. }
  65. n, _, err := unix.Syscall(unix.SYS_FCNTL64, uintptr(fd), uintptr(cmd), arg)
  66. if err != 0 {
  67. if dmesgs {
  68. dmesg("%v: fd %v cmd %v", origin(1), fcntlCmdStr(fd), cmd)
  69. }
  70. t.setErrno(err)
  71. return -1
  72. }
  73. if dmesgs {
  74. dmesg("%v: %d %s %#x: %d", origin(1), fd, fcntlCmdStr(cmd), arg, n)
  75. }
  76. return int32(n)
  77. }
  78. // int lstat(const char *pathname, struct stat *statbuf);
  79. func Xlstat64(t *TLS, pathname, statbuf uintptr) int32 {
  80. if _, _, err := unix.Syscall(unix.SYS_LSTAT64, pathname, statbuf, 0); err != 0 {
  81. if dmesgs {
  82. dmesg("%v: %q: %v", origin(1), GoString(pathname), err)
  83. }
  84. t.setErrno(err)
  85. return -1
  86. }
  87. if dmesgs {
  88. dmesg("%v: %q: ok", origin(1), GoString(pathname))
  89. }
  90. return 0
  91. }
  92. // int stat(const char *pathname, struct stat *statbuf);
  93. func Xstat64(t *TLS, pathname, statbuf uintptr) int32 {
  94. if _, _, err := unix.Syscall(unix.SYS_STAT64, pathname, statbuf, 0); err != 0 {
  95. if dmesgs {
  96. dmesg("%v: %q: %v", origin(1), GoString(pathname), err)
  97. }
  98. t.setErrno(err)
  99. return -1
  100. }
  101. if dmesgs {
  102. dmesg("%v: %q: ok", origin(1), GoString(pathname))
  103. }
  104. return 0
  105. }
  106. // int fstat(int fd, struct stat *statbuf);
  107. func Xfstat64(t *TLS, fd int32, statbuf uintptr) int32 {
  108. if _, _, err := unix.Syscall(unix.SYS_FSTAT64, uintptr(fd), statbuf, 0); err != 0 {
  109. if dmesgs {
  110. dmesg("%v: fd %d: %v", origin(1), fd, err)
  111. }
  112. t.setErrno(err)
  113. return -1
  114. }
  115. if dmesgs {
  116. dmesg("%v: %d, size %#x: ok\n%+v", origin(1), fd, (*stat.Stat)(unsafe.Pointer(statbuf)).Fst_size, (*stat.Stat)(unsafe.Pointer(statbuf)))
  117. }
  118. return 0
  119. }
  120. // void *mremap(void *old_address, size_t old_size, size_t new_size, int flags, ... /* void *new_address */);
  121. func Xmremap(t *TLS, old_address uintptr, old_size, new_size types.Size_t, flags int32, args uintptr) uintptr {
  122. var arg uintptr
  123. if args != 0 {
  124. arg = *(*uintptr)(unsafe.Pointer(args))
  125. }
  126. data, _, err := unix.Syscall6(unix.SYS_MREMAP, old_address, uintptr(old_size), uintptr(new_size), uintptr(flags), arg, 0)
  127. if err != 0 {
  128. if dmesgs {
  129. dmesg("%v: %v", origin(1), err)
  130. }
  131. t.setErrno(err)
  132. return ^uintptr(0) // (void*)-1
  133. }
  134. if dmesgs {
  135. dmesg("%v: %#x", origin(1), data)
  136. }
  137. return data
  138. }
  139. func Xmmap(t *TLS, addr uintptr, length types.Size_t, prot, flags, fd int32, offset types.Off_t) uintptr {
  140. return Xmmap64(t, addr, length, prot, flags, fd, offset)
  141. }
  142. // void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset);
  143. func Xmmap64(t *TLS, addr uintptr, length types.Size_t, prot, flags, fd int32, offset types.Off_t) uintptr {
  144. data, _, err := unix.Syscall6(unix.SYS_MMAP2, addr, uintptr(length), uintptr(prot), uintptr(flags), uintptr(fd), uintptr(offset>>12))
  145. if err != 0 {
  146. if dmesgs {
  147. dmesg("%v: %v", origin(1), err)
  148. }
  149. t.setErrno(err)
  150. return ^uintptr(0) // (void*)-1
  151. }
  152. if dmesgs {
  153. dmesg("%v: %#x", origin(1), data)
  154. }
  155. return data
  156. }
  157. // int ftruncate(int fd, off_t length);
  158. func Xftruncate64(t *TLS, fd int32, length types.Off_t) int32 {
  159. if _, _, err := unix.Syscall(unix.SYS_FTRUNCATE64, uintptr(fd), uintptr(length), uintptr(length>>32)); err != 0 {
  160. if dmesgs {
  161. dmesg("%v: fd %d: %v", origin(1), fd, err)
  162. }
  163. t.setErrno(err)
  164. return -1
  165. }
  166. if dmesgs {
  167. dmesg("%v: %d %#x: ok", origin(1), fd, length)
  168. }
  169. return 0
  170. }
  171. // off64_t lseek64(int fd, off64_t offset, int whence);
  172. func Xlseek64(t *TLS, fd int32, offset types.Off_t, whence int32) types.Off_t {
  173. bp := t.Alloc(int(unsafe.Sizeof(types.X__loff_t(0))))
  174. defer t.Free(int(unsafe.Sizeof(types.X__loff_t(0))))
  175. if _, _, err := unix.Syscall6(unix.SYS__LLSEEK, uintptr(fd), uintptr(offset>>32), uintptr(offset), bp, uintptr(whence), 0); err != 0 {
  176. if dmesgs {
  177. dmesg("%v: fd %v, off %#x, whence %v: %v", origin(1), fd, offset, whenceStr(whence), err)
  178. }
  179. t.setErrno(err)
  180. return -1
  181. }
  182. if dmesgs {
  183. dmesg("%v: fd %v, off %#x, whence %v: %#x", origin(1), fd, offset, whenceStr(whence), *(*types.Off_t)(unsafe.Pointer(bp)))
  184. }
  185. return *(*types.Off_t)(unsafe.Pointer(bp))
  186. }
  187. // int utime(const char *filename, const struct utimbuf *times);
  188. func Xutime(t *TLS, filename, times uintptr) int32 {
  189. if _, _, err := unix.Syscall(unix.SYS_UTIME, filename, times, 0); err != 0 {
  190. t.setErrno(err)
  191. return -1
  192. }
  193. return 0
  194. }
  195. // unsigned int alarm(unsigned int seconds);
  196. func Xalarm(t *TLS, seconds uint32) uint32 {
  197. n, _, err := unix.Syscall(unix.SYS_ALARM, uintptr(seconds), 0, 0)
  198. if err != 0 {
  199. panic(todo(""))
  200. }
  201. return uint32(n)
  202. }
  203. // int getrlimit(int resource, struct rlimit *rlim);
  204. func Xgetrlimit64(t *TLS, resource int32, rlim uintptr) int32 {
  205. if _, _, err := unix.Syscall(unix.SYS_GETRLIMIT, uintptr(resource), uintptr(rlim), 0); err != 0 {
  206. t.setErrno(err)
  207. return -1
  208. }
  209. return 0
  210. }
  211. // time_t time(time_t *tloc);
  212. func Xtime(t *TLS, tloc uintptr) types.Time_t {
  213. n, _, err := unix.Syscall(unix.SYS_TIME, tloc, 0, 0)
  214. if err != 0 {
  215. t.setErrno(err)
  216. return types.Time_t(-1)
  217. }
  218. if tloc != 0 {
  219. *(*types.Time_t)(unsafe.Pointer(tloc)) = types.Time_t(n)
  220. }
  221. return types.Time_t(n)
  222. }
  223. // int mkdir(const char *path, mode_t mode);
  224. func Xmkdir(t *TLS, path uintptr, mode types.Mode_t) int32 {
  225. if _, _, err := unix.Syscall(unix.SYS_MKDIR, path, uintptr(mode), 0); err != 0 {
  226. t.setErrno(err)
  227. return -1
  228. }
  229. if dmesgs {
  230. dmesg("%v: %q: ok", origin(1), GoString(path))
  231. }
  232. return 0
  233. }
  234. // int symlink(const char *target, const char *linkpath);
  235. func Xsymlink(t *TLS, target, linkpath uintptr) int32 {
  236. if _, _, err := unix.Syscall(unix.SYS_SYMLINK, target, linkpath, 0); err != 0 {
  237. t.setErrno(err)
  238. return -1
  239. }
  240. if dmesgs {
  241. dmesg("%v: %q %q: ok", origin(1), GoString(target), GoString(linkpath))
  242. }
  243. return 0
  244. }
  245. // int chmod(const char *pathname, mode_t mode)
  246. func Xchmod(t *TLS, pathname uintptr, mode types.Mode_t) int32 {
  247. if _, _, err := unix.Syscall(unix.SYS_CHMOD, pathname, uintptr(mode), 0); err != 0 {
  248. t.setErrno(err)
  249. return -1
  250. }
  251. if dmesgs {
  252. dmesg("%v: %q %#o: ok", origin(1), GoString(pathname), mode)
  253. }
  254. return 0
  255. }
  256. // int utimes(const char *filename, const struct timeval times[2]);
  257. func Xutimes(t *TLS, filename, times uintptr) int32 {
  258. if _, _, err := unix.Syscall(unix.SYS_UTIMES, filename, times, 0); err != 0 {
  259. t.setErrno(err)
  260. return -1
  261. }
  262. if dmesgs {
  263. dmesg("%v: %q: ok", origin(1), GoString(filename))
  264. }
  265. return 0
  266. }
  267. // int unlink(const char *pathname);
  268. func Xunlink(t *TLS, pathname uintptr) int32 {
  269. if _, _, err := unix.Syscall(unix.SYS_UNLINK, pathname, 0, 0); err != 0 {
  270. t.setErrno(err)
  271. return -1
  272. }
  273. if dmesgs {
  274. dmesg("%v: %q: ok", origin(1), GoString(pathname))
  275. }
  276. return 0
  277. }
  278. // int access(const char *pathname, int mode);
  279. func Xaccess(t *TLS, pathname uintptr, mode int32) int32 {
  280. if _, _, err := unix.Syscall(unix.SYS_ACCESS, pathname, uintptr(mode), 0); err != 0 {
  281. if dmesgs {
  282. dmesg("%v: %q: %v", origin(1), GoString(pathname), err)
  283. }
  284. t.setErrno(err)
  285. return -1
  286. }
  287. if dmesgs {
  288. dmesg("%v: %q %#o: ok", origin(1), GoString(pathname), mode)
  289. }
  290. return 0
  291. }
  292. // int rmdir(const char *pathname);
  293. func Xrmdir(t *TLS, pathname uintptr) int32 {
  294. if _, _, err := unix.Syscall(unix.SYS_RMDIR, pathname, 0, 0); err != 0 {
  295. t.setErrno(err)
  296. return -1
  297. }
  298. if dmesgs {
  299. dmesg("%v: %q: ok", origin(1), GoString(pathname))
  300. }
  301. return 0
  302. }
  303. // int rename(const char *oldpath, const char *newpath);
  304. func Xrename(t *TLS, oldpath, newpath uintptr) int32 {
  305. if _, _, err := unix.Syscall(unix.SYS_RENAME, oldpath, newpath, 0); err != 0 {
  306. t.setErrno(err)
  307. return -1
  308. }
  309. return 0
  310. }
  311. // int mknod(const char *pathname, mode_t mode, dev_t dev);
  312. func Xmknod(t *TLS, pathname uintptr, mode types.Mode_t, dev types.Dev_t) int32 {
  313. if _, _, err := unix.Syscall(unix.SYS_MKNOD, pathname, uintptr(mode), uintptr(dev)); err != 0 {
  314. t.setErrno(err)
  315. return -1
  316. }
  317. return 0
  318. }
  319. // int chown(const char *pathname, uid_t owner, gid_t group);
  320. func Xchown(t *TLS, pathname uintptr, owner types.Uid_t, group types.Gid_t) int32 {
  321. if _, _, err := unix.Syscall(unix.SYS_CHOWN, pathname, uintptr(owner), uintptr(group)); err != 0 {
  322. t.setErrno(err)
  323. return -1
  324. }
  325. return 0
  326. }
  327. // int link(const char *oldpath, const char *newpath);
  328. func Xlink(t *TLS, oldpath, newpath uintptr) int32 {
  329. if _, _, err := unix.Syscall(unix.SYS_LINK, oldpath, newpath, 0); err != 0 {
  330. t.setErrno(err)
  331. return -1
  332. }
  333. return 0
  334. }
  335. // int pipe(int pipefd[2]);
  336. func Xpipe(t *TLS, pipefd uintptr) int32 {
  337. if _, _, err := unix.Syscall(unix.SYS_PIPE, pipefd, 0, 0); err != 0 {
  338. t.setErrno(err)
  339. return -1
  340. }
  341. return 0
  342. }
  343. // int dup2(int oldfd, int newfd);
  344. func Xdup2(t *TLS, oldfd, newfd int32) int32 {
  345. n, _, err := unix.Syscall(unix.SYS_DUP2, uintptr(oldfd), uintptr(newfd), 0)
  346. if err != 0 {
  347. t.setErrno(err)
  348. return -1
  349. }
  350. return int32(n)
  351. }
  352. // ssize_t readlink(const char *restrict path, char *restrict buf, size_t bufsize);
  353. func Xreadlink(t *TLS, path, buf uintptr, bufsize types.Size_t) types.Ssize_t {
  354. n, _, err := unix.Syscall(unix.SYS_READLINK, path, buf, uintptr(bufsize))
  355. if err != 0 {
  356. t.setErrno(err)
  357. return -1
  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 = os.O_RDONLY
  368. case "r+":
  369. flags = os.O_RDWR
  370. case "w":
  371. flags = os.O_WRONLY | os.O_CREATE | os.O_TRUNC
  372. case "w+":
  373. flags = os.O_RDWR | os.O_CREATE | os.O_TRUNC
  374. case "a":
  375. flags = os.O_WRONLY | os.O_CREATE | os.O_APPEND
  376. case "a+":
  377. flags = os.O_RDWR | os.O_CREATE | os.O_APPEND
  378. default:
  379. panic(m)
  380. }
  381. //TODO- flags |= fcntl.O_LARGEFILE
  382. fd, _, err := unix.Syscall(unix.SYS_OPEN, pathname, uintptr(flags|unix.O_LARGEFILE), 0666)
  383. if err != 0 {
  384. t.setErrno(err)
  385. return 0
  386. }
  387. if p := newFile(t, int32(fd)); p != 0 {
  388. return p
  389. }
  390. Xclose(t, int32(fd))
  391. t.setErrno(errno.ENOMEM)
  392. return 0
  393. }