libc_linux.go 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601
  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. "encoding/hex"
  7. "fmt"
  8. "os"
  9. "os/exec"
  10. "path/filepath"
  11. "runtime"
  12. "runtime/debug"
  13. "syscall"
  14. "time"
  15. "unsafe"
  16. guuid "github.com/google/uuid"
  17. "golang.org/x/sys/unix"
  18. "modernc.org/libc/errno"
  19. "modernc.org/libc/fcntl"
  20. "modernc.org/libc/fts"
  21. gonetdb "modernc.org/libc/honnef.co/go/netdb"
  22. "modernc.org/libc/langinfo"
  23. "modernc.org/libc/limits"
  24. "modernc.org/libc/netdb"
  25. "modernc.org/libc/netinet/in"
  26. "modernc.org/libc/signal"
  27. "modernc.org/libc/stdio"
  28. "modernc.org/libc/sys/socket"
  29. "modernc.org/libc/sys/stat"
  30. "modernc.org/libc/sys/types"
  31. "modernc.org/libc/termios"
  32. ctime "modernc.org/libc/time"
  33. "modernc.org/libc/unistd"
  34. "modernc.org/libc/uuid/uuid"
  35. )
  36. var (
  37. in6_addr_any in.In6_addr
  38. _ = X__ctype_b_loc
  39. )
  40. type (
  41. long = types.X__syscall_slong_t
  42. ulong = types.X__syscall_ulong_t
  43. )
  44. type file uintptr
  45. func (f file) fd() int32 { return (*stdio.FILE)(unsafe.Pointer(f)).F_fileno }
  46. func (f file) setFd(fd int32) { (*stdio.FILE)(unsafe.Pointer(f)).F_fileno = fd }
  47. func (f file) err() bool { return (*stdio.FILE)(unsafe.Pointer(f)).F_flags2&stdio.X_IO_ERR_SEEN != 0 }
  48. func (f file) setErr() { (*stdio.FILE)(unsafe.Pointer(f)).F_flags2 |= stdio.X_IO_ERR_SEEN }
  49. func (f file) close(t *TLS) int32 {
  50. r := Xclose(t, f.fd())
  51. Xfree(t, uintptr(f))
  52. if r < 0 {
  53. return stdio.EOF
  54. }
  55. return 0
  56. }
  57. func newFile(t *TLS, fd int32) uintptr {
  58. p := Xcalloc(t, 1, types.Size_t(unsafe.Sizeof(stdio.FILE{})))
  59. if p == 0 {
  60. return 0
  61. }
  62. file(p).setFd(fd)
  63. return p
  64. }
  65. func fwrite(fd int32, b []byte) (int, error) {
  66. if fd == unistd.STDOUT_FILENO {
  67. return write(b)
  68. }
  69. // if dmesgs {
  70. // dmesg("%v: fd %v: %s", origin(1), fd, b)
  71. // }
  72. return unix.Write(int(fd), b) //TODO use Xwrite
  73. }
  74. // int fprintf(FILE *stream, const char *format, ...);
  75. func Xfprintf(t *TLS, stream, format, args uintptr) int32 {
  76. n, _ := fwrite((*stdio.FILE)(unsafe.Pointer(stream)).F_fileno, printf(format, args))
  77. return int32(n)
  78. }
  79. // int usleep(useconds_t usec);
  80. func Xusleep(t *TLS, usec types.X__useconds_t) int32 {
  81. time.Sleep(time.Microsecond * time.Duration(usec))
  82. return 0
  83. }
  84. // int getrusage(int who, struct rusage *usage);
  85. func Xgetrusage(t *TLS, who int32, usage uintptr) int32 {
  86. if _, _, err := unix.Syscall(unix.SYS_GETRUSAGE, uintptr(who), usage, 0); err != 0 {
  87. t.setErrno(err)
  88. return -1
  89. }
  90. return 0
  91. }
  92. // int lstat(const char *pathname, struct stat *statbuf);
  93. func Xlstat(t *TLS, pathname, statbuf uintptr) int32 {
  94. return Xlstat64(t, pathname, statbuf)
  95. }
  96. // int stat(const char *pathname, struct stat *statbuf);
  97. func Xstat(t *TLS, pathname, statbuf uintptr) int32 {
  98. return Xstat64(t, pathname, statbuf)
  99. }
  100. // int chdir(const char *path);
  101. func Xchdir(t *TLS, path uintptr) int32 {
  102. if _, _, err := unix.Syscall(unix.SYS_CHDIR, path, 0, 0); err != 0 {
  103. t.setErrno(err)
  104. return -1
  105. }
  106. // if dmesgs {
  107. // dmesg("%v: %q: ok", origin(1), GoString(path))
  108. // }
  109. return 0
  110. }
  111. var localtime ctime.Tm
  112. // struct tm *localtime(const time_t *timep);
  113. func Xlocaltime(_ *TLS, timep uintptr) uintptr {
  114. loc := time.Local
  115. if r := getenv(Environ(), "TZ"); r != 0 {
  116. zone, off := parseZone(GoString(r))
  117. loc = time.FixedZone(zone, -off)
  118. }
  119. ut := *(*unix.Time_t)(unsafe.Pointer(timep))
  120. t := time.Unix(int64(ut), 0).In(loc)
  121. localtime.Ftm_sec = int32(t.Second())
  122. localtime.Ftm_min = int32(t.Minute())
  123. localtime.Ftm_hour = int32(t.Hour())
  124. localtime.Ftm_mday = int32(t.Day())
  125. localtime.Ftm_mon = int32(t.Month() - 1)
  126. localtime.Ftm_year = int32(t.Year() - 1900)
  127. localtime.Ftm_wday = int32(t.Weekday())
  128. localtime.Ftm_yday = int32(t.YearDay())
  129. localtime.Ftm_isdst = Bool32(isTimeDST(t))
  130. return uintptr(unsafe.Pointer(&localtime))
  131. }
  132. // struct tm *localtime_r(const time_t *timep, struct tm *result);
  133. func Xlocaltime_r(_ *TLS, timep, result uintptr) uintptr {
  134. loc := time.Local
  135. if r := getenv(Environ(), "TZ"); r != 0 {
  136. zone, off := parseZone(GoString(r))
  137. loc = time.FixedZone(zone, -off)
  138. }
  139. ut := *(*unix.Time_t)(unsafe.Pointer(timep))
  140. t := time.Unix(int64(ut), 0).In(loc)
  141. (*ctime.Tm)(unsafe.Pointer(result)).Ftm_sec = int32(t.Second())
  142. (*ctime.Tm)(unsafe.Pointer(result)).Ftm_min = int32(t.Minute())
  143. (*ctime.Tm)(unsafe.Pointer(result)).Ftm_hour = int32(t.Hour())
  144. (*ctime.Tm)(unsafe.Pointer(result)).Ftm_mday = int32(t.Day())
  145. (*ctime.Tm)(unsafe.Pointer(result)).Ftm_mon = int32(t.Month() - 1)
  146. (*ctime.Tm)(unsafe.Pointer(result)).Ftm_year = int32(t.Year() - 1900)
  147. (*ctime.Tm)(unsafe.Pointer(result)).Ftm_wday = int32(t.Weekday())
  148. (*ctime.Tm)(unsafe.Pointer(result)).Ftm_yday = int32(t.YearDay())
  149. (*ctime.Tm)(unsafe.Pointer(result)).Ftm_isdst = Bool32(isTimeDST(t))
  150. return result
  151. }
  152. // int open(const char *pathname, int flags, ...);
  153. func Xopen(t *TLS, pathname uintptr, flags int32, args uintptr) int32 {
  154. return Xopen64(t, pathname, flags, args)
  155. }
  156. // int open(const char *pathname, int flags, ...);
  157. func Xopen64(t *TLS, pathname uintptr, flags int32, args uintptr) int32 {
  158. //TODO- flags |= fcntl.O_LARGEFILE
  159. var mode types.Mode_t
  160. if args != 0 {
  161. mode = (types.Mode_t)(VaUint32(&args))
  162. }
  163. fdcwd := fcntl.AT_FDCWD
  164. n, _, err := unix.Syscall6(unix.SYS_OPENAT, uintptr(fdcwd), pathname, uintptr(flags|unix.O_LARGEFILE), uintptr(mode), 0, 0)
  165. if err != 0 {
  166. // if dmesgs {
  167. // dmesg("%v: %q %#x: %v", origin(1), GoString(pathname), flags, err)
  168. // }
  169. t.setErrno(err)
  170. return -1
  171. }
  172. // if dmesgs {
  173. // dmesg("%v: %q flags %#x mode %#o: fd %v", origin(1), GoString(pathname), flags, mode, n)
  174. // }
  175. return int32(n)
  176. }
  177. // int openat(int dirfd, const char *pathname, int flags, mode_t mode);
  178. func Xopenat(t *TLS, dirfd int32, pathname uintptr, flags int32, mode types.Mode_t) int32 {
  179. // From golang.org/x/sys/unix/zsyscall_linux.go
  180. fd, _, err := unix.Syscall6(unix.SYS_OPENAT, uintptr(dirfd), pathname, uintptr(flags), uintptr(mode), 0, 0)
  181. if err != 0 {
  182. t.setErrno(err)
  183. return -1
  184. }
  185. return int32(fd)
  186. }
  187. // off_t lseek(int fd, off_t offset, int whence);
  188. func Xlseek(t *TLS, fd int32, offset types.Off_t, whence int32) types.Off_t {
  189. return types.Off_t(Xlseek64(t, fd, offset, whence))
  190. }
  191. func whenceStr(whence int32) string {
  192. switch whence {
  193. case fcntl.SEEK_CUR:
  194. return "SEEK_CUR"
  195. case fcntl.SEEK_END:
  196. return "SEEK_END"
  197. case fcntl.SEEK_SET:
  198. return "SEEK_SET"
  199. default:
  200. return fmt.Sprintf("whence(%d)", whence)
  201. }
  202. }
  203. var fsyncStatbuf stat.Stat
  204. // int fsync(int fd);
  205. func Xfsync(t *TLS, fd int32) int32 {
  206. if noFsync {
  207. // Simulate -DSQLITE_NO_SYNC for sqlite3 testfixture, see function full_sync in sqlite3.c
  208. return Xfstat(t, fd, uintptr(unsafe.Pointer(&fsyncStatbuf)))
  209. }
  210. if _, _, err := unix.Syscall(unix.SYS_FSYNC, uintptr(fd), 0, 0); err != 0 {
  211. t.setErrno(err)
  212. return -1
  213. }
  214. // if dmesgs {
  215. // dmesg("%v: %d: ok", origin(1), fd)
  216. // }
  217. return 0
  218. }
  219. // long sysconf(int name);
  220. func Xsysconf(t *TLS, name int32) long {
  221. switch name {
  222. case unistd.X_SC_PAGESIZE:
  223. return long(unix.Getpagesize())
  224. case unistd.X_SC_GETPW_R_SIZE_MAX:
  225. return -1
  226. case unistd.X_SC_GETGR_R_SIZE_MAX:
  227. return -1
  228. case unistd.X_SC_NPROCESSORS_ONLN:
  229. return long(runtime.NumCPU())
  230. }
  231. panic(todo("", name))
  232. }
  233. // int close(int fd);
  234. func Xclose(t *TLS, fd int32) int32 {
  235. if _, _, err := unix.Syscall(unix.SYS_CLOSE, uintptr(fd), 0, 0); err != 0 {
  236. t.setErrno(err)
  237. return -1
  238. }
  239. // if dmesgs {
  240. // dmesg("%v: %d: ok", origin(1), fd)
  241. // }
  242. return 0
  243. }
  244. // char *getcwd(char *buf, size_t size);
  245. func Xgetcwd(t *TLS, buf uintptr, size types.Size_t) uintptr {
  246. n, _, err := unix.Syscall(unix.SYS_GETCWD, buf, uintptr(size), 0)
  247. if err != 0 {
  248. t.setErrno(err)
  249. return 0
  250. }
  251. // if dmesgs {
  252. // dmesg("%v: %q: ok", origin(1), GoString(buf))
  253. // }
  254. return n
  255. }
  256. // int fstat(int fd, struct stat *statbuf);
  257. func Xfstat(t *TLS, fd int32, statbuf uintptr) int32 {
  258. return Xfstat64(t, fd, statbuf)
  259. }
  260. // int ftruncate(int fd, off_t length);
  261. func Xftruncate(t *TLS, fd int32, length types.Off_t) int32 {
  262. return Xftruncate64(t, fd, length)
  263. }
  264. // int fcntl(int fd, int cmd, ... /* arg */ );
  265. func Xfcntl(t *TLS, fd, cmd int32, args uintptr) int32 {
  266. return Xfcntl64(t, fd, cmd, args)
  267. }
  268. // ssize_t read(int fd, void *buf, size_t count);
  269. func Xread(t *TLS, fd int32, buf uintptr, count types.Size_t) types.Ssize_t {
  270. n, _, err := unix.Syscall(unix.SYS_READ, uintptr(fd), buf, uintptr(count))
  271. if err != 0 {
  272. t.setErrno(err)
  273. return -1
  274. }
  275. // if dmesgs {
  276. // // dmesg("%v: %d %#x: %#x\n%s", origin(1), fd, count, n, hex.Dump(GoBytes(buf, int(n))))
  277. // dmesg("%v: %d %#x: %#x", origin(1), fd, count, n)
  278. // }
  279. return types.Ssize_t(n)
  280. }
  281. // ssize_t write(int fd, const void *buf, size_t count);
  282. func Xwrite(t *TLS, fd int32, buf uintptr, count types.Size_t) types.Ssize_t {
  283. const retry = 5
  284. var err syscall.Errno
  285. for i := 0; i < retry; i++ {
  286. var n uintptr
  287. switch n, _, err = unix.Syscall(unix.SYS_WRITE, uintptr(fd), buf, uintptr(count)); err {
  288. case 0:
  289. // if dmesgs {
  290. // // dmesg("%v: %d %#x: %#x\n%s", origin(1), fd, count, n, hex.Dump(GoBytes(buf, int(n))))
  291. // dmesg("%v: %d %#x: %#x", origin(1), fd, count, n)
  292. // }
  293. return types.Ssize_t(n)
  294. case errno.EAGAIN:
  295. // nop
  296. }
  297. }
  298. // if dmesgs {
  299. // dmesg("%v: fd %v, count %#x: %v", origin(1), fd, count, err)
  300. // }
  301. t.setErrno(err)
  302. return -1
  303. }
  304. // int fchmod(int fd, mode_t mode);
  305. func Xfchmod(t *TLS, fd int32, mode types.Mode_t) int32 {
  306. if _, _, err := unix.Syscall(unix.SYS_FCHMOD, uintptr(fd), uintptr(mode), 0); err != 0 {
  307. t.setErrno(err)
  308. return -1
  309. }
  310. // if dmesgs {
  311. // dmesg("%v: %d %#o: ok", origin(1), fd, mode)
  312. // }
  313. return 0
  314. }
  315. // int fchmodat(int dirfd, const char *pathname, mode_t mode, int flags);
  316. func Xfchmodat(t *TLS, dirfd int32, pathname uintptr, mode types.Mode_t, flags int32) int32 {
  317. // From golang.org/x/sys/unix/syscall_linux.go
  318. // Linux fchmodat doesn't support the flags parameter. Mimick glibc's behavior
  319. // and check the flags. Otherwise the mode would be applied to the symlink
  320. // destination which is not what the user expects.
  321. if flags&^unix.AT_SYMLINK_NOFOLLOW != 0 {
  322. t.setErrno(unix.EINVAL)
  323. return -1
  324. } else if flags&unix.AT_SYMLINK_NOFOLLOW != 0 {
  325. t.setErrno(unix.EOPNOTSUPP)
  326. return -1
  327. }
  328. // From golang.org/x/sys/unix/zsyscall_linux.go
  329. if _, _, err := unix.Syscall(unix.SYS_FCHMODAT, uintptr(dirfd), pathname, uintptr(mode)); err != 0 {
  330. t.setErrno(err)
  331. return -1
  332. }
  333. return 0
  334. }
  335. // int fchown(int fd, uid_t owner, gid_t group);
  336. func Xfchown(t *TLS, fd int32, owner types.Uid_t, group types.Gid_t) int32 {
  337. if _, _, err := unix.Syscall(unix.SYS_FCHOWN, uintptr(fd), uintptr(owner), uintptr(group)); err != 0 {
  338. t.setErrno(err)
  339. return -1
  340. }
  341. return 0
  342. }
  343. // uid_t geteuid(void);
  344. func Xgeteuid(t *TLS) types.Uid_t {
  345. n, _, _ := unix.Syscall(unix.SYS_GETEUID, 0, 0, 0)
  346. return types.Uid_t(n)
  347. }
  348. // int munmap(void *addr, size_t length);
  349. func Xmunmap(t *TLS, addr uintptr, length types.Size_t) int32 {
  350. if _, _, err := unix.Syscall(unix.SYS_MUNMAP, addr, uintptr(length), 0); err != 0 {
  351. t.setErrno(err)
  352. return -1
  353. }
  354. return 0
  355. }
  356. // int gettimeofday(struct timeval *tv, struct timezone *tz);
  357. func Xgettimeofday(t *TLS, tv, tz uintptr) int32 {
  358. if tz != 0 {
  359. panic(todo(""))
  360. }
  361. var tvs unix.Timeval
  362. err := unix.Gettimeofday(&tvs)
  363. if err != nil {
  364. t.setErrno(err)
  365. return -1
  366. }
  367. *(*unix.Timeval)(unsafe.Pointer(tv)) = tvs
  368. return 0
  369. }
  370. // int getsockopt(int sockfd, int level, int optname, void *optval, socklen_t *optlen);
  371. func Xgetsockopt(t *TLS, sockfd, level, optname int32, optval, optlen uintptr) int32 {
  372. if _, _, err := unix.Syscall6(unix.SYS_GETSOCKOPT, uintptr(sockfd), uintptr(level), uintptr(optname), optval, optlen, 0); err != 0 {
  373. t.setErrno(err)
  374. return -1
  375. }
  376. return 0
  377. }
  378. // int setsockopt(int sockfd, int level, int optname, const void *optval, socklen_t optlen);
  379. func Xsetsockopt(t *TLS, sockfd, level, optname int32, optval uintptr, optlen socket.Socklen_t) int32 {
  380. if _, _, err := unix.Syscall6(unix.SYS_SETSOCKOPT, uintptr(sockfd), uintptr(level), uintptr(optname), optval, uintptr(optlen), 0); err != 0 {
  381. t.setErrno(err)
  382. return -1
  383. }
  384. return 0
  385. }
  386. // int ioctl(int fd, unsigned long request, ...);
  387. func Xioctl(t *TLS, fd int32, request ulong, va uintptr) int32 {
  388. var argp uintptr
  389. if va != 0 {
  390. argp = VaUintptr(&va)
  391. }
  392. n, _, err := unix.Syscall(unix.SYS_IOCTL, uintptr(fd), uintptr(request), argp)
  393. if err != 0 {
  394. t.setErrno(err)
  395. return -1
  396. }
  397. return int32(n)
  398. }
  399. // int getsockname(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
  400. func Xgetsockname(t *TLS, sockfd int32, addr, addrlen uintptr) int32 {
  401. if _, _, err := unix.Syscall(unix.SYS_GETSOCKNAME, uintptr(sockfd), addr, addrlen); err != 0 {
  402. // if dmesgs {
  403. // dmesg("%v: fd %v: %v", origin(1), sockfd, err)
  404. // }
  405. t.setErrno(err)
  406. return -1
  407. }
  408. return 0
  409. }
  410. // int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout);
  411. func Xselect(t *TLS, nfds int32, readfds, writefds, exceptfds, timeout uintptr) int32 {
  412. n, err := unix.Select(
  413. int(nfds),
  414. (*unix.FdSet)(unsafe.Pointer(readfds)),
  415. (*unix.FdSet)(unsafe.Pointer(writefds)),
  416. (*unix.FdSet)(unsafe.Pointer(exceptfds)),
  417. (*unix.Timeval)(unsafe.Pointer(timeout)),
  418. )
  419. if err != nil {
  420. t.setErrno(err)
  421. return -1
  422. }
  423. return int32(n)
  424. }
  425. // int mkfifo(const char *pathname, mode_t mode);
  426. func Xmkfifo(t *TLS, pathname uintptr, mode types.Mode_t) int32 {
  427. if err := unix.Mkfifo(GoString(pathname), mode); err != nil {
  428. t.setErrno(err)
  429. return -1
  430. }
  431. return 0
  432. }
  433. // mode_t umask(mode_t mask);
  434. func Xumask(t *TLS, mask types.Mode_t) types.Mode_t {
  435. n, _, _ := unix.Syscall(unix.SYS_UMASK, uintptr(mask), 0, 0)
  436. return types.Mode_t(n)
  437. }
  438. // int execvp(const char *file, char *const argv[]);
  439. func Xexecvp(t *TLS, file, argv uintptr) int32 {
  440. if _, _, err := unix.Syscall(unix.SYS_EXECVE, file, argv, Environ()); err != 0 {
  441. t.setErrno(err)
  442. return -1
  443. }
  444. return 0
  445. }
  446. // pid_t waitpid(pid_t pid, int *wstatus, int options);
  447. func Xwaitpid(t *TLS, pid types.Pid_t, wstatus uintptr, optname int32) types.Pid_t {
  448. n, _, err := unix.Syscall6(unix.SYS_WAIT4, uintptr(pid), wstatus, uintptr(optname), 0, 0, 0)
  449. if err != 0 {
  450. t.setErrno(err)
  451. return -1
  452. }
  453. return types.Pid_t(n)
  454. }
  455. // int uname(struct utsname *buf);
  456. func Xuname(t *TLS, buf uintptr) int32 {
  457. if _, _, err := unix.Syscall(unix.SYS_UNAME, buf, 0, 0); err != 0 {
  458. t.setErrno(err)
  459. return -1
  460. }
  461. return 0
  462. }
  463. // ssize_t recv(int sockfd, void *buf, size_t len, int flags);
  464. func Xrecv(t *TLS, sockfd int32, buf uintptr, len types.Size_t, flags int32) types.Ssize_t {
  465. n, _, err := unix.Syscall6(unix.SYS_RECVFROM, uintptr(sockfd), buf, uintptr(len), uintptr(flags), 0, 0)
  466. if err != 0 {
  467. t.setErrno(err)
  468. return -1
  469. }
  470. return types.Ssize_t(n)
  471. }
  472. // ssize_t send(int sockfd, const void *buf, size_t len, int flags);
  473. func Xsend(t *TLS, sockfd int32, buf uintptr, len types.Size_t, flags int32) types.Ssize_t {
  474. n, _, err := unix.Syscall6(unix.SYS_SENDTO, uintptr(sockfd), buf, uintptr(len), uintptr(flags), 0, 0)
  475. if err != 0 {
  476. t.setErrno(err)
  477. return -1
  478. }
  479. return types.Ssize_t(n)
  480. }
  481. // int shutdown(int sockfd, int how);
  482. func Xshutdown(t *TLS, sockfd, how int32) int32 {
  483. if _, _, err := unix.Syscall(unix.SYS_SHUTDOWN, uintptr(sockfd), uintptr(how), 0); err != 0 {
  484. t.setErrno(err)
  485. return -1
  486. }
  487. return 0
  488. }
  489. // int getpeername(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
  490. func Xgetpeername(t *TLS, sockfd int32, addr uintptr, addrlen uintptr) int32 {
  491. if _, _, err := unix.Syscall(unix.SYS_GETPEERNAME, uintptr(sockfd), addr, uintptr(addrlen)); err != 0 {
  492. t.setErrno(err)
  493. return -1
  494. }
  495. return 0
  496. }
  497. // int socket(int domain, int type, int protocol);
  498. func Xsocket(t *TLS, domain, type1, protocol int32) int32 {
  499. n, _, err := unix.Syscall(unix.SYS_SOCKET, uintptr(domain), uintptr(type1), uintptr(protocol))
  500. if err != 0 {
  501. t.setErrno(err)
  502. return -1
  503. }
  504. return int32(n)
  505. }
  506. // int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
  507. func Xbind(t *TLS, sockfd int32, addr uintptr, addrlen uint32) int32 {
  508. n, _, err := unix.Syscall(unix.SYS_BIND, uintptr(sockfd), addr, uintptr(addrlen))
  509. if err != 0 {
  510. t.setErrno(err)
  511. return -1
  512. }
  513. return int32(n)
  514. }
  515. // int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
  516. func Xconnect(t *TLS, sockfd int32, addr uintptr, addrlen uint32) int32 {
  517. if _, _, err := unix.Syscall(unix.SYS_CONNECT, uintptr(sockfd), addr, uintptr(addrlen)); err != 0 {
  518. t.setErrno(err)
  519. return -1
  520. }
  521. return 0
  522. }
  523. // int listen(int sockfd, int backlog);
  524. func Xlisten(t *TLS, sockfd, backlog int32) int32 {
  525. if _, _, err := unix.Syscall(unix.SYS_LISTEN, uintptr(sockfd), uintptr(backlog), 0); err != 0 {
  526. t.setErrno(err)
  527. return -1
  528. }
  529. return 0
  530. }
  531. // int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
  532. func Xaccept(t *TLS, sockfd int32, addr uintptr, addrlen uintptr) int32 {
  533. n, _, err := unix.Syscall6(unix.SYS_ACCEPT4, uintptr(sockfd), addr, uintptr(addrlen), 0, 0, 0)
  534. if err != 0 {
  535. t.setErrno(err)
  536. return -1
  537. }
  538. return int32(n)
  539. }
  540. // int getrlimit(int resource, struct rlimit *rlim);
  541. func Xgetrlimit(t *TLS, resource int32, rlim uintptr) int32 {
  542. return Xgetrlimit64(t, resource, rlim)
  543. }
  544. // int setrlimit(int resource, const struct rlimit *rlim);
  545. func Xsetrlimit(t *TLS, resource int32, rlim uintptr) int32 {
  546. return Xsetrlimit64(t, resource, rlim)
  547. }
  548. // int setrlimit(int resource, const struct rlimit *rlim);
  549. func Xsetrlimit64(t *TLS, resource int32, rlim uintptr) int32 {
  550. if _, _, err := unix.Syscall(unix.SYS_SETRLIMIT, uintptr(resource), uintptr(rlim), 0); err != 0 {
  551. t.setErrno(err)
  552. return -1
  553. }
  554. return 0
  555. }
  556. // uid_t getuid(void);
  557. func Xgetuid(t *TLS) types.Uid_t {
  558. return types.Uid_t(os.Getuid())
  559. }
  560. // pid_t getpid(void);
  561. func Xgetpid(t *TLS) int32 {
  562. return int32(os.Getpid())
  563. }
  564. // int system(const char *command);
  565. func Xsystem(t *TLS, command uintptr) int32 {
  566. s := GoString(command)
  567. if command == 0 {
  568. panic(todo(""))
  569. }
  570. cmd := exec.Command("sh", "-c", s)
  571. cmd.Stdout = os.Stdout
  572. cmd.Stderr = os.Stderr
  573. err := cmd.Run()
  574. if err != nil {
  575. ps := err.(*exec.ExitError)
  576. return int32(ps.ExitCode())
  577. }
  578. return 0
  579. }
  580. // int setvbuf(FILE *stream, char *buf, int mode, size_t size);
  581. func Xsetvbuf(t *TLS, stream, buf uintptr, mode int32, size types.Size_t) int32 {
  582. return 0 //TODO
  583. }
  584. // int raise(int sig);
  585. func Xraise(t *TLS, sig int32) int32 {
  586. panic(todo(""))
  587. }
  588. // int backtrace(void **buffer, int size);
  589. func Xbacktrace(t *TLS, buf uintptr, size int32) int32 {
  590. panic(todo(""))
  591. }
  592. // void backtrace_symbols_fd(void *const *buffer, int size, int fd);
  593. func Xbacktrace_symbols_fd(t *TLS, buffer uintptr, size, fd int32) {
  594. panic(todo(""))
  595. }
  596. // int fileno(FILE *stream);
  597. func Xfileno(t *TLS, stream uintptr) int32 {
  598. if stream == 0 {
  599. t.setErrno(errno.EBADF)
  600. return -1
  601. }
  602. if fd := (*stdio.FILE)(unsafe.Pointer(stream)).F_fileno; fd >= 0 {
  603. return fd
  604. }
  605. t.setErrno(errno.EBADF)
  606. return -1
  607. }
  608. func newFtsent(t *TLS, info int, path string, stat *unix.Stat_t, err syscall.Errno) (r *fts.FTSENT) {
  609. var statp uintptr
  610. if stat != nil {
  611. statp = Xmalloc(t, types.Size_t(unsafe.Sizeof(unix.Stat_t{})))
  612. if statp == 0 {
  613. panic("OOM")
  614. }
  615. *(*unix.Stat_t)(unsafe.Pointer(statp)) = *stat
  616. }
  617. csp, errx := CString(path)
  618. if errx != nil {
  619. panic("OOM")
  620. }
  621. return &fts.FTSENT{
  622. Ffts_info: uint16(info),
  623. Ffts_path: csp,
  624. Ffts_pathlen: uint16(len(path)),
  625. Ffts_statp: statp,
  626. Ffts_errno: int32(err),
  627. }
  628. }
  629. func newCFtsent(t *TLS, info int, path string, stat *unix.Stat_t, err syscall.Errno) uintptr {
  630. p := Xcalloc(t, 1, types.Size_t(unsafe.Sizeof(fts.FTSENT{})))
  631. if p == 0 {
  632. panic("OOM")
  633. }
  634. *(*fts.FTSENT)(unsafe.Pointer(p)) = *newFtsent(t, info, path, stat, err)
  635. return p
  636. }
  637. func ftsentClose(t *TLS, p uintptr) {
  638. Xfree(t, (*fts.FTSENT)(unsafe.Pointer(p)).Ffts_path)
  639. Xfree(t, (*fts.FTSENT)(unsafe.Pointer(p)).Ffts_statp)
  640. }
  641. type ftstream struct {
  642. s []uintptr
  643. x int
  644. }
  645. func (f *ftstream) close(t *TLS) {
  646. for _, p := range f.s {
  647. ftsentClose(t, p)
  648. Xfree(t, p)
  649. }
  650. *f = ftstream{}
  651. }
  652. // FTS *fts_open(char * const *path_argv, int options, int (*compar)(const FTSENT **, const FTSENT **));
  653. func Xfts_open(t *TLS, path_argv uintptr, options int32, compar uintptr) uintptr {
  654. return Xfts64_open(t, path_argv, options, compar)
  655. }
  656. // FTS *fts_open(char * const *path_argv, int options, int (*compar)(const FTSENT **, const FTSENT **));
  657. func Xfts64_open(t *TLS, path_argv uintptr, options int32, compar uintptr) uintptr {
  658. f := &ftstream{}
  659. var walk func(string)
  660. walk = func(path string) {
  661. var fi os.FileInfo
  662. var err error
  663. switch {
  664. case options&fts.FTS_LOGICAL != 0:
  665. fi, err = os.Stat(path)
  666. case options&fts.FTS_PHYSICAL != 0:
  667. fi, err = os.Lstat(path)
  668. default:
  669. panic(todo(""))
  670. }
  671. if err != nil {
  672. return
  673. }
  674. var statp *unix.Stat_t
  675. if options&fts.FTS_NOSTAT == 0 {
  676. var stat unix.Stat_t
  677. switch {
  678. case options&fts.FTS_LOGICAL != 0:
  679. if err := unix.Stat(path, &stat); err != nil {
  680. panic(todo(""))
  681. }
  682. case options&fts.FTS_PHYSICAL != 0:
  683. if err := unix.Lstat(path, &stat); err != nil {
  684. panic(todo(""))
  685. }
  686. default:
  687. panic(todo(""))
  688. }
  689. statp = &stat
  690. }
  691. out:
  692. switch {
  693. case fi.IsDir():
  694. f.s = append(f.s, newCFtsent(t, fts.FTS_D, path, statp, 0))
  695. g, err := os.Open(path)
  696. switch x := err.(type) {
  697. case nil:
  698. // ok
  699. case *os.PathError:
  700. f.s = append(f.s, newCFtsent(t, fts.FTS_DNR, path, statp, errno.EACCES))
  701. break out
  702. default:
  703. panic(todo("%q: %v %T", path, x, x))
  704. }
  705. names, err := g.Readdirnames(-1)
  706. g.Close()
  707. if err != nil {
  708. panic(todo(""))
  709. }
  710. for _, name := range names {
  711. walk(path + "/" + name)
  712. if f == nil {
  713. break out
  714. }
  715. }
  716. f.s = append(f.s, newCFtsent(t, fts.FTS_DP, path, statp, 0))
  717. default:
  718. info := fts.FTS_F
  719. if fi.Mode()&os.ModeSymlink != 0 {
  720. info = fts.FTS_SL
  721. }
  722. switch {
  723. case statp != nil:
  724. f.s = append(f.s, newCFtsent(t, info, path, statp, 0))
  725. case options&fts.FTS_NOSTAT != 0:
  726. f.s = append(f.s, newCFtsent(t, fts.FTS_NSOK, path, nil, 0))
  727. default:
  728. panic(todo(""))
  729. }
  730. }
  731. }
  732. for {
  733. p := *(*uintptr)(unsafe.Pointer(path_argv))
  734. if p == 0 {
  735. if f == nil {
  736. return 0
  737. }
  738. if compar != 0 {
  739. panic(todo(""))
  740. }
  741. return addObject(f)
  742. }
  743. walk(GoString(p))
  744. path_argv += unsafe.Sizeof(uintptr(0))
  745. }
  746. }
  747. // FTSENT *fts_read(FTS *ftsp);
  748. func Xfts_read(t *TLS, ftsp uintptr) uintptr {
  749. return Xfts64_read(t, ftsp)
  750. }
  751. // FTSENT *fts_read(FTS *ftsp);
  752. func Xfts64_read(t *TLS, ftsp uintptr) uintptr {
  753. f := getObject(ftsp).(*ftstream)
  754. if f.x == len(f.s) {
  755. t.setErrno(0)
  756. return 0
  757. }
  758. r := f.s[f.x]
  759. if e := (*fts.FTSENT)(unsafe.Pointer(r)).Ffts_errno; e != 0 {
  760. t.setErrno(e)
  761. }
  762. f.x++
  763. return r
  764. }
  765. // int fts_close(FTS *ftsp);
  766. func Xfts_close(t *TLS, ftsp uintptr) int32 {
  767. return Xfts64_close(t, ftsp)
  768. }
  769. // int fts_close(FTS *ftsp);
  770. func Xfts64_close(t *TLS, ftsp uintptr) int32 {
  771. getObject(ftsp).(*ftstream).close(t)
  772. removeObject(ftsp)
  773. return 0
  774. }
  775. // void tzset (void);
  776. func Xtzset(t *TLS) {
  777. //TODO
  778. }
  779. var strerrorBuf [100]byte
  780. // char *strerror(int errnum);
  781. func Xstrerror(t *TLS, errnum int32) uintptr {
  782. if dmesgs {
  783. dmesg("%v: %v\n%s", origin(1), errnum, debug.Stack())
  784. }
  785. copy(strerrorBuf[:], fmt.Sprintf("strerror(%d)\x00", errnum))
  786. return uintptr(unsafe.Pointer(&strerrorBuf[0]))
  787. }
  788. // void *dlopen(const char *filename, int flags);
  789. func Xdlopen(t *TLS, filename uintptr, flags int32) uintptr {
  790. panic(todo("%q", GoString(filename)))
  791. }
  792. // char *dlerror(void);
  793. func Xdlerror(t *TLS) uintptr {
  794. panic(todo(""))
  795. }
  796. // int dlclose(void *handle);
  797. func Xdlclose(t *TLS, handle uintptr) int32 {
  798. panic(todo(""))
  799. }
  800. // void *dlsym(void *handle, const char *symbol);
  801. func Xdlsym(t *TLS, handle, symbol uintptr) uintptr {
  802. panic(todo(""))
  803. }
  804. // void perror(const char *s);
  805. func Xperror(t *TLS, s uintptr) {
  806. panic(todo(""))
  807. }
  808. // int pclose(FILE *stream);
  809. func Xpclose(t *TLS, stream uintptr) int32 {
  810. panic(todo(""))
  811. }
  812. var gai_strerrorBuf [100]byte
  813. // const char *gai_strerror(int errcode);
  814. func Xgai_strerror(t *TLS, errcode int32) uintptr {
  815. copy(gai_strerrorBuf[:], fmt.Sprintf("gai error %d\x00", errcode))
  816. return uintptr(unsafe.Pointer(&gai_strerrorBuf))
  817. }
  818. // int tcgetattr(int fd, struct termios *termios_p);
  819. func Xtcgetattr(t *TLS, fd int32, termios_p uintptr) int32 {
  820. panic(todo(""))
  821. }
  822. // int tcsetattr(int fd, int optional_actions, const struct termios *termios_p);
  823. func Xtcsetattr(t *TLS, fd, optional_actions int32, termios_p uintptr) int32 {
  824. panic(todo(""))
  825. }
  826. // speed_t cfgetospeed(const struct termios *termios_p);
  827. func Xcfgetospeed(t *TLS, termios_p uintptr) termios.Speed_t {
  828. panic(todo(""))
  829. }
  830. // int cfsetospeed(struct termios *termios_p, speed_t speed);
  831. func Xcfsetospeed(t *TLS, termios_p uintptr, speed uint32) int32 {
  832. panic(todo(""))
  833. }
  834. // int cfsetispeed(struct termios *termios_p, speed_t speed);
  835. func Xcfsetispeed(t *TLS, termios_p uintptr, speed uint32) int32 {
  836. panic(todo(""))
  837. }
  838. // pid_t fork(void);
  839. func Xfork(t *TLS) int32 {
  840. t.setErrno(errno.ENOSYS)
  841. return -1
  842. }
  843. var emptyStr = [1]byte{}
  844. // char *setlocale(int category, const char *locale);
  845. func Xsetlocale(t *TLS, category int32, locale uintptr) uintptr {
  846. return uintptr(unsafe.Pointer(&emptyStr)) //TODO
  847. }
  848. // char *nl_langinfo(nl_item item);
  849. func Xnl_langinfo(t *TLS, item langinfo.Nl_item) uintptr {
  850. return uintptr(unsafe.Pointer(&emptyStr)) //TODO
  851. }
  852. // FILE *popen(const char *command, const char *type);
  853. func Xpopen(t *TLS, command, type1 uintptr) uintptr {
  854. panic(todo(""))
  855. }
  856. // char *realpath(const char *path, char *resolved_path);
  857. func Xrealpath(t *TLS, path, resolved_path uintptr) uintptr {
  858. s, err := filepath.EvalSymlinks(GoString(path))
  859. if err != nil {
  860. if os.IsNotExist(err) {
  861. // if dmesgs {
  862. // dmesg("%v: %q: %v", origin(1), GoString(path), err)
  863. // }
  864. t.setErrno(errno.ENOENT)
  865. return 0
  866. }
  867. panic(todo("", err))
  868. }
  869. if resolved_path == 0 {
  870. panic(todo(""))
  871. }
  872. if len(s) >= limits.PATH_MAX {
  873. s = s[:limits.PATH_MAX-1]
  874. }
  875. copy((*RawMem)(unsafe.Pointer(resolved_path))[:len(s):len(s)], s)
  876. (*RawMem)(unsafe.Pointer(resolved_path))[len(s)] = 0
  877. return resolved_path
  878. }
  879. // struct tm *gmtime_r(const time_t *timep, struct tm *result);
  880. func Xgmtime_r(t *TLS, timep, result uintptr) uintptr {
  881. panic(todo(""))
  882. }
  883. // char *inet_ntoa(struct in_addr in);
  884. func Xinet_ntoa(t *TLS, in1 in.In_addr) uintptr {
  885. panic(todo(""))
  886. }
  887. func X__ccgo_in6addr_anyp(t *TLS) uintptr {
  888. return uintptr(unsafe.Pointer(&in6_addr_any))
  889. }
  890. func Xabort(t *TLS) {
  891. // if dmesgs {
  892. // dmesg("%v:\n%s", origin(1), debug.Stack())
  893. // }
  894. p := Xmalloc(t, types.Size_t(unsafe.Sizeof(signal.Sigaction{})))
  895. if p == 0 {
  896. panic("OOM")
  897. }
  898. *(*signal.Sigaction)(unsafe.Pointer(p)) = signal.Sigaction{
  899. F__sigaction_handler: struct{ Fsa_handler signal.X__sighandler_t }{Fsa_handler: signal.SIG_DFL},
  900. }
  901. Xsigaction(t, signal.SIGABRT, p, 0)
  902. Xfree(t, p)
  903. unix.Kill(unix.Getpid(), syscall.Signal(signal.SIGABRT))
  904. panic(todo("unrechable"))
  905. }
  906. // int fflush(FILE *stream);
  907. func Xfflush(t *TLS, stream uintptr) int32 {
  908. return 0 //TODO
  909. }
  910. // size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
  911. func Xfread(t *TLS, ptr uintptr, size, nmemb types.Size_t, stream uintptr) types.Size_t {
  912. m, _, err := unix.Syscall(unix.SYS_READ, uintptr(file(stream).fd()), ptr, uintptr(size*nmemb))
  913. if err != 0 {
  914. file(stream).setErr()
  915. return 0
  916. }
  917. // if dmesgs {
  918. // // dmesg("%v: %d %#x x %#x: %#x\n%s", origin(1), file(stream).fd(), size, nmemb, types.Size_t(m)/size, hex.Dump(GoBytes(ptr, int(m))))
  919. // dmesg("%v: %d %#x x %#x: %#x", origin(1), file(stream).fd(), size, nmemb, types.Size_t(m)/size)
  920. // }
  921. return types.Size_t(m) / size
  922. }
  923. // size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);
  924. func Xfwrite(t *TLS, ptr uintptr, size, nmemb types.Size_t, stream uintptr) types.Size_t {
  925. m, _, err := unix.Syscall(unix.SYS_WRITE, uintptr(file(stream).fd()), ptr, uintptr(size*nmemb))
  926. if err != 0 {
  927. file(stream).setErr()
  928. return 0
  929. }
  930. // if dmesgs {
  931. // // dmesg("%v: %d %#x x %#x: %#x\n%s", origin(1), file(stream).fd(), size, nmemb, types.Size_t(m)/size, hex.Dump(GoBytes(ptr, int(m))))
  932. // dmesg("%v: %d %#x x %#x: %#x", origin(1), file(stream).fd(), size, nmemb, types.Size_t(m)/size)
  933. // }
  934. return types.Size_t(m) / size
  935. }
  936. // int fclose(FILE *stream);
  937. func Xfclose(t *TLS, stream uintptr) int32 {
  938. return file(stream).close(t)
  939. }
  940. // int fputc(int c, FILE *stream);
  941. func Xfputc(t *TLS, c int32, stream uintptr) int32 {
  942. if _, err := fwrite(file(stream).fd(), []byte{byte(c)}); err != nil {
  943. return stdio.EOF
  944. }
  945. return int32(byte(c))
  946. }
  947. // int fseek(FILE *stream, long offset, int whence);
  948. func Xfseek(t *TLS, stream uintptr, offset long, whence int32) int32 {
  949. if n := Xlseek(t, int32(file(stream).fd()), types.Off_t(offset), whence); n < 0 {
  950. // if dmesgs {
  951. // dmesg("%v: fd %v, off %#x, whence %v: %v", origin(1), file(stream).fd(), offset, whenceStr(whence), n)
  952. // }
  953. file(stream).setErr()
  954. return -1
  955. }
  956. // if dmesgs {
  957. // dmesg("%v: fd %v, off %#x, whence %v: ok", origin(1), file(stream).fd(), offset, whenceStr(whence))
  958. // }
  959. return 0
  960. }
  961. // long ftell(FILE *stream);
  962. func Xftell(t *TLS, stream uintptr) long {
  963. n := Xlseek(t, file(stream).fd(), 0, stdio.SEEK_CUR)
  964. if n < 0 {
  965. file(stream).setErr()
  966. return -1
  967. }
  968. // if dmesgs {
  969. // dmesg("%v: fd %v, n %#x: ok %#x", origin(1), file(stream).fd(), n, long(n))
  970. // }
  971. return long(n)
  972. }
  973. // int ferror(FILE *stream);
  974. func Xferror(t *TLS, stream uintptr) int32 {
  975. return Bool32(file(stream).err())
  976. }
  977. // int ungetc(int c, FILE *stream);
  978. func Xungetc(t *TLS, c int32, stream uintptr) int32 {
  979. panic(todo(""))
  980. }
  981. // int fscanf(FILE *stream, const char *format, ...);
  982. func Xfscanf(t *TLS, stream, format, va uintptr) int32 {
  983. panic(todo(""))
  984. }
  985. // int fputs(const char *s, FILE *stream);
  986. func Xfputs(t *TLS, s, stream uintptr) int32 {
  987. if _, _, err := unix.Syscall(unix.SYS_WRITE, uintptr(file(stream).fd()), s, uintptr(Xstrlen(t, s))); err != 0 {
  988. return -1
  989. }
  990. return 0
  991. }
  992. var getservbynameStaticResult netdb.Servent
  993. // struct servent *getservbyname(const char *name, const char *proto);
  994. func Xgetservbyname(t *TLS, name, proto uintptr) uintptr {
  995. var protoent *gonetdb.Protoent
  996. if proto != 0 {
  997. protoent = gonetdb.GetProtoByName(GoString(proto))
  998. }
  999. servent := gonetdb.GetServByName(GoString(name), protoent)
  1000. if servent == nil {
  1001. // if dmesgs {
  1002. // dmesg("%q %q: nil (protoent %+v)", GoString(name), GoString(proto), protoent)
  1003. // }
  1004. return 0
  1005. }
  1006. Xfree(t, (*netdb.Servent)(unsafe.Pointer(&getservbynameStaticResult)).Fs_name)
  1007. if v := (*netdb.Servent)(unsafe.Pointer(&getservbynameStaticResult)).Fs_aliases; v != 0 {
  1008. for {
  1009. p := *(*uintptr)(unsafe.Pointer(v))
  1010. if p == 0 {
  1011. break
  1012. }
  1013. Xfree(t, p)
  1014. v += unsafe.Sizeof(uintptr(0))
  1015. }
  1016. Xfree(t, v)
  1017. }
  1018. Xfree(t, (*netdb.Servent)(unsafe.Pointer(&getservbynameStaticResult)).Fs_proto)
  1019. cname, err := CString(servent.Name)
  1020. if err != nil {
  1021. getservbynameStaticResult = netdb.Servent{}
  1022. return 0
  1023. }
  1024. var protoname uintptr
  1025. if protoent != nil {
  1026. if protoname, err = CString(protoent.Name); err != nil {
  1027. Xfree(t, cname)
  1028. getservbynameStaticResult = netdb.Servent{}
  1029. return 0
  1030. }
  1031. }
  1032. var a []uintptr
  1033. for _, v := range servent.Aliases {
  1034. cs, err := CString(v)
  1035. if err != nil {
  1036. for _, v := range a {
  1037. Xfree(t, v)
  1038. }
  1039. return 0
  1040. }
  1041. a = append(a, cs)
  1042. }
  1043. v := Xcalloc(t, types.Size_t(len(a)+1), types.Size_t(unsafe.Sizeof(uintptr(0))))
  1044. if v == 0 {
  1045. Xfree(t, cname)
  1046. Xfree(t, protoname)
  1047. for _, v := range a {
  1048. Xfree(t, v)
  1049. }
  1050. getservbynameStaticResult = netdb.Servent{}
  1051. return 0
  1052. }
  1053. for _, p := range a {
  1054. *(*uintptr)(unsafe.Pointer(v)) = p
  1055. v += unsafe.Sizeof(uintptr(0))
  1056. }
  1057. getservbynameStaticResult = netdb.Servent{
  1058. Fs_name: cname,
  1059. Fs_aliases: v,
  1060. Fs_port: int32(servent.Port),
  1061. Fs_proto: protoname,
  1062. }
  1063. return uintptr(unsafe.Pointer(&getservbynameStaticResult))
  1064. }
  1065. func Xreaddir64(t *TLS, dir uintptr) uintptr {
  1066. return Xreaddir(t, dir)
  1067. }
  1068. func __syscall(r, _ uintptr, errno syscall.Errno) long {
  1069. if errno != 0 {
  1070. return long(-errno)
  1071. }
  1072. return long(r)
  1073. }
  1074. func X__syscall1(t *TLS, trap, p1 long) long {
  1075. return __syscall(unix.Syscall(uintptr(trap), uintptr(p1), 0, 0))
  1076. }
  1077. func X__syscall3(t *TLS, trap, p1, p2, p3 long) long {
  1078. return __syscall(unix.Syscall(uintptr(trap), uintptr(p1), uintptr(p2), uintptr(p3)))
  1079. }
  1080. func X__syscall4(t *TLS, trap, p1, p2, p3, p4 long) long {
  1081. return __syscall(unix.Syscall6(uintptr(trap), uintptr(p1), uintptr(p2), uintptr(p3), uintptr(p4), 0, 0))
  1082. }
  1083. func fcntlCmdStr(cmd int32) string {
  1084. switch cmd {
  1085. case fcntl.F_GETOWN:
  1086. return "F_GETOWN"
  1087. case fcntl.F_SETLK:
  1088. return "F_SETLK"
  1089. case fcntl.F_GETLK:
  1090. return "F_GETLK"
  1091. case fcntl.F_SETFD:
  1092. return "F_SETFD"
  1093. case fcntl.F_GETFD:
  1094. return "F_GETFD"
  1095. default:
  1096. return fmt.Sprintf("cmd(%d)", cmd)
  1097. }
  1098. }
  1099. // int setenv(const char *name, const char *value, int overwrite);
  1100. func Xsetenv(t *TLS, name, value uintptr, overwrite int32) int32 {
  1101. panic(todo(""))
  1102. }
  1103. // int unsetenv(const char *name);
  1104. func Xunsetenv(t *TLS, name uintptr) int32 {
  1105. panic(todo(""))
  1106. }
  1107. // int pause(void);
  1108. func Xpause(t *TLS) int32 {
  1109. err := unix.Pause()
  1110. if err != nil {
  1111. t.setErrno(err)
  1112. }
  1113. return -1
  1114. }
  1115. // ssize_t writev(int fd, const struct iovec *iov, int iovcnt);
  1116. func Xwritev(t *TLS, fd int32, iov uintptr, iovcnt int32) types.Ssize_t {
  1117. // if dmesgs {
  1118. // dmesg("%v: fd %v iov %#x iovcnt %v", origin(1), fd, iov, iovcnt)
  1119. // }
  1120. if iovcnt == 0 {
  1121. panic(todo(""))
  1122. }
  1123. iovs := make([][]byte, iovcnt)
  1124. for ; iovcnt != 0; iovcnt-- {
  1125. base := (*unix.Iovec)(unsafe.Pointer(iov)).Base
  1126. len := (*unix.Iovec)(unsafe.Pointer(iov)).Len
  1127. // if dmesgs {
  1128. // dmesg("%v: base %#x len %v", origin(1), base, len)
  1129. // }
  1130. if base != nil && len != 0 {
  1131. iovs = append(iovs, (*RawMem)(unsafe.Pointer(base))[:len:len])
  1132. iov += unsafe.Sizeof(unix.Iovec{})
  1133. }
  1134. }
  1135. n, err := unix.Writev(int(fd), iovs)
  1136. if err != nil {
  1137. // if dmesgs {
  1138. // dmesg("%v: %v", origin(1), err)
  1139. // }
  1140. panic(todo(""))
  1141. }
  1142. return types.Ssize_t(n)
  1143. }
  1144. // int __isoc99_sscanf(const char *str, const char *format, ...);
  1145. func X__isoc99_sscanf(t *TLS, str, format, va uintptr) int32 {
  1146. r := Xsscanf(t, str, format, va)
  1147. // if dmesgs {
  1148. // dmesg("%v: %q %q: %d", origin(1), GoString(str), GoString(format), r)
  1149. // }
  1150. return r
  1151. }
  1152. // var ctimeStaticBuf [32]byte
  1153. //
  1154. // // char *ctime(const time_t *timep);
  1155. // func Xctime(t *TLS, timep uintptr) uintptr {
  1156. // return Xctime_r(t, timep, uintptr(unsafe.Pointer(&ctimeStaticBuf[0])))
  1157. // }
  1158. //
  1159. // // char *ctime_r(const time_t *timep, char *buf);
  1160. // func Xctime_r(t *TLS, timep, buf uintptr) uintptr {
  1161. // ut := *(*unix.Time_t)(unsafe.Pointer(timep))
  1162. // tm := time.Unix(int64(ut), 0).Local()
  1163. // s := tm.Format(time.ANSIC) + "\n\x00"
  1164. // copy((*RawMem)(unsafe.Pointer(buf))[:26:26], s)
  1165. // return buf
  1166. // }
  1167. // ssize_t pwrite(int fd, const void *buf, size_t count, off_t offset);
  1168. func Xpwrite(t *TLS, fd int32, buf uintptr, count types.Size_t, offset types.Off_t) types.Ssize_t {
  1169. var n int
  1170. var err error
  1171. switch {
  1172. case count == 0:
  1173. n, err = unix.Pwrite(int(fd), nil, int64(offset))
  1174. default:
  1175. n, err = unix.Pwrite(int(fd), (*RawMem)(unsafe.Pointer(buf))[:count:count], int64(offset))
  1176. if dmesgs {
  1177. dmesg("%v: fd %v, off %#x, count %#x\n%s", origin(1), fd, offset, count, hex.Dump((*RawMem)(unsafe.Pointer(buf))[:count:count]))
  1178. }
  1179. }
  1180. if err != nil {
  1181. if dmesgs {
  1182. dmesg("%v: %v FAIL", origin(1), err)
  1183. }
  1184. t.setErrno(err)
  1185. return -1
  1186. }
  1187. if dmesgs {
  1188. dmesg("%v: ok", origin(1))
  1189. }
  1190. return types.Ssize_t(n)
  1191. }
  1192. // int fstatfs(int fd, struct statfs *buf);
  1193. func Xfstatfs(t *TLS, fd int32, buf uintptr) int32 {
  1194. if err := unix.Fstatfs(int(fd), (*unix.Statfs_t)(unsafe.Pointer(buf))); err != nil {
  1195. t.setErrno(err)
  1196. return -1
  1197. }
  1198. return 0
  1199. }
  1200. // ssize_t getrandom(void *buf, size_t buflen, unsigned int flags);
  1201. func Xgetrandom(t *TLS, buf uintptr, buflen size_t, flags uint32) ssize_t {
  1202. n, err := unix.Getrandom((*RawMem)(unsafe.Pointer(buf))[:buflen], int(flags))
  1203. if err != nil {
  1204. t.setErrno(err)
  1205. return -1
  1206. }
  1207. return ssize_t(n)
  1208. }
  1209. // int posix_fadvise(int fd, off_t offset, off_t len, int advice);
  1210. func Xposix_fadvise(t *TLS, fd int32, offset, len types.Off_t, advice int32) int32 {
  1211. if err := unix.Fadvise(int(fd), int64(offset), int64(len), int(advice)); err != nil {
  1212. return int32(err.(unix.Errno))
  1213. }
  1214. return 0
  1215. }
  1216. // int fgetc(FILE *stream);
  1217. func Xfgetc(t *TLS, stream uintptr) int32 {
  1218. fd := int((*stdio.FILE)(unsafe.Pointer(stream)).F_fileno)
  1219. var buf [1]byte
  1220. if n, _ := unix.Read(fd, buf[:]); n != 0 {
  1221. return int32(buf[0])
  1222. }
  1223. return stdio.EOF
  1224. }
  1225. // void uuid_copy(uuid_t dst, uuid_t src);
  1226. func Xuuid_copy(t *TLS, dst, src uintptr) {
  1227. *(*uuid.Uuid_t)(unsafe.Pointer(dst)) = *(*uuid.Uuid_t)(unsafe.Pointer(src))
  1228. }
  1229. // int uuid_parse( char *in, uuid_t uu);
  1230. func Xuuid_parse(t *TLS, in uintptr, uu uintptr) int32 {
  1231. r, err := guuid.Parse(GoString(in))
  1232. if err != nil {
  1233. return -1
  1234. }
  1235. copy((*RawMem)(unsafe.Pointer(uu))[:unsafe.Sizeof(uuid.Uuid_t{})], r[:])
  1236. return 0
  1237. }
  1238. // int mkdirat(int dirfd, const char *pathname, mode_t mode);
  1239. func Xmkdirat(t *TLS, dirfd int32, pathname uintptr, mode types.Mode_t) int32 {
  1240. // From golang.org/x/sys/unix/zsyscall_linux.go
  1241. if _, _, err := unix.Syscall(unix.SYS_MKDIRAT, uintptr(dirfd), pathname, uintptr(mode)); err != 0 {
  1242. t.setErrno(err)
  1243. return -1
  1244. }
  1245. return 0
  1246. }
  1247. // int symlinkat(const char *target, int newdirfd, const char *linkpath);
  1248. func Xsymlinkat(t *TLS, target uintptr, newdirfd int32, linkpath uintptr) int32 {
  1249. // From golang.org/x/sys/unix/zsyscall_linux.go
  1250. if _, _, err := unix.Syscall(unix.SYS_SYMLINKAT, target, uintptr(newdirfd), linkpath); err != 0 {
  1251. t.setErrno(err)
  1252. return -1
  1253. }
  1254. return 0
  1255. }
  1256. // int utimensat(int dirfd, const char *pathname, const struct timespec times[2], int flags);
  1257. func Xutimensat(t *TLS, dirfd int32, pathname, times uintptr, flags int32) int32 {
  1258. // From golang.org/x/sys/unix/zsyscall_linux.go
  1259. if _, _, err := unix.Syscall6(unix.SYS_UTIMENSAT, uintptr(dirfd), pathname, times, uintptr(flags), 0, 0); err != 0 {
  1260. t.setErrno(err)
  1261. return -1
  1262. }
  1263. return 0
  1264. }
  1265. // int unlinkat(int dirfd, const char *pathname, int flags);
  1266. func Xunlinkat(t *TLS, dirfd int32, pathname uintptr, flags int32) int32 {
  1267. // From golang.org/x/sys/unix/zsyscall_linux.go
  1268. if _, _, err := unix.Syscall(unix.SYS_UNLINKAT, uintptr(dirfd), pathname, uintptr(flags)); err != 0 {
  1269. t.setErrno(err)
  1270. return -1
  1271. }
  1272. return 0
  1273. }
  1274. // int faccessat(int dirfd, const char *pathname, int mode, int flags);
  1275. func Xfaccessat(t *TLS, dirfd int32, pathname uintptr, mode, flags int32) int32 {
  1276. // From golang.org/x/sys/unix/zsyscall_linux.go
  1277. if _, _, err := unix.Syscall(unix.SYS_FACCESSAT, uintptr(dirfd), pathname, uintptr(mode)); err != 0 {
  1278. t.setErrno(err)
  1279. return -1
  1280. }
  1281. return 0
  1282. }
  1283. // int renameat2(int olddirfd, const char *oldpath, int newdirfd, const char *newpath, unsigned int flags);
  1284. func Xrenameat2(t *TLS, olddirfd int32, oldpath uintptr, newdirfd int32, newpath uintptr, flags int32) int32 {
  1285. // From golang.org/x/sys/unix/zsyscall_linux.go
  1286. if _, _, err := unix.Syscall6(unix.SYS_RENAMEAT2, uintptr(olddirfd), oldpath, uintptr(newdirfd), newpath, uintptr(flags), 0); err != 0 {
  1287. t.setErrno(err)
  1288. return -1
  1289. }
  1290. return 0
  1291. }
  1292. // int mknodat(int dirfd, const char *pathname, mode_t mode, dev_t dev);
  1293. func Xmknodat(t *TLS, dirfd int32, pathname uintptr, mode types.Mode_t, dev types.Dev_t) int32 {
  1294. // From golang.org/x/sys/unix/zsyscall_linux.go
  1295. if _, _, err := unix.Syscall6(unix.SYS_MKNODAT, uintptr(dirfd), pathname, uintptr(mode), uintptr(dev), 0, 0); err != 0 {
  1296. t.setErrno(err)
  1297. return -1
  1298. }
  1299. return 0
  1300. }
  1301. // int fchownat(int dirfd, const char *pathname, uid_t owner, gid_t group, int flags);
  1302. func Xfchownat(t *TLS, dirfd int32, pathname uintptr, uid types.Uid_t, gid types.Gid_t, flags int32) int32 {
  1303. // From golang.org/x/sys/unix/zsyscall_linux.go
  1304. if _, _, err := unix.Syscall6(unix.SYS_FCHOWNAT, uintptr(dirfd), pathname, uintptr(uid), uintptr(gid), uintptr(flags), 0); err != 0 {
  1305. t.setErrno(err)
  1306. return -1
  1307. }
  1308. return 0
  1309. }
  1310. // int linkat(int olddirfd, const char *oldpath, int newdirfd, const char *newpath, int flags);
  1311. func Xlinkat(t *TLS, olddirfd int32, oldpath uintptr, newdirfd int32, newpath uintptr, flags int32) int32 {
  1312. // From golang.org/x/sys/unix/zsyscall_linux.go
  1313. if _, _, err := unix.Syscall6(unix.SYS_LINKAT, uintptr(olddirfd), oldpath, uintptr(newdirfd), newpath, uintptr(flags), 0); err != 0 {
  1314. t.setErrno(err)
  1315. return -1
  1316. }
  1317. return 0
  1318. }
  1319. // int pipe2(int pipefd[2], int flags);
  1320. func Xpipe2(t *TLS, pipefd uintptr, flags int32) int32 {
  1321. // From golang.org/x/sys/unix/zsyscall_linux.go
  1322. if _, _, err := unix.Syscall(unix.SYS_PIPE2, pipefd, uintptr(flags), 0); err != 0 {
  1323. t.setErrno(t)
  1324. return -1
  1325. }
  1326. return 0
  1327. }
  1328. // int dup3(int oldfd, int newfd, int flags);
  1329. func Xdup3(t *TLS, oldfd int32, newfd int32, flags int32) int32 {
  1330. // From golang.org/x/sys/unix/zsyscall_linux.go
  1331. if _, _, err := unix.Syscall(unix.SYS_DUP3, uintptr(oldfd), uintptr(newfd), uintptr(flags)); err != 0 {
  1332. t.setErrno(err)
  1333. return -1
  1334. }
  1335. return 0
  1336. }
  1337. // ssize_t readlinkat(int dirfd, const char *pathname, char *buf, size_t bufsiz);
  1338. func Xreadlinkat(t *TLS, dirfd int32, pathname, buf uintptr, bufsiz types.Size_t) types.Ssize_t {
  1339. // From golang.org/x/sys/unix/zsyscall_linux.go
  1340. n, _, err := unix.Syscall6(unix.SYS_READLINKAT, uintptr(dirfd), pathname, buf, uintptr(bufsiz), 0, 0)
  1341. if err != 0 {
  1342. t.setErrno(err)
  1343. return -1
  1344. }
  1345. return types.Ssize_t(n)
  1346. }
  1347. // int nanosleep(const struct timespec *req, struct timespec *rem);
  1348. func Xnanosleep(t *TLS, req, rem uintptr) int32 {
  1349. v := *(*ctime.Timespec)(unsafe.Pointer(req))
  1350. time.Sleep(time.Second*time.Duration(v.Ftv_sec) + time.Duration(v.Ftv_nsec))
  1351. return 0
  1352. }