libc_openbsd.go (41039B)
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 5 package libc // import "modernc.org/libc" 6 7 import ( 8 "fmt" 9 "io" 10 "os" 11 "os/exec" 12 "path/filepath" 13 "runtime" 14 "runtime/debug" 15 "strings" 16 "syscall" 17 gotime "time" 18 "unsafe" 19 20 guuid "github.com/google/uuid" 21 "golang.org/x/sys/unix" 22 "modernc.org/libc/errno" 23 "modernc.org/libc/fcntl" 24 "modernc.org/libc/fts" 25 gonetdb "modernc.org/libc/honnef.co/go/netdb" 26 "modernc.org/libc/langinfo" 27 "modernc.org/libc/limits" 28 "modernc.org/libc/netdb" 29 "modernc.org/libc/netinet/in" 30 "modernc.org/libc/stdio" 31 "modernc.org/libc/sys/socket" 32 "modernc.org/libc/sys/stat" 33 "modernc.org/libc/sys/types" 34 "modernc.org/libc/termios" 35 "modernc.org/libc/time" 36 "modernc.org/libc/unistd" 37 "modernc.org/libc/uuid" 38 ) 39 40 var ( 41 in6_addr_any in.In6_addr 42 ) 43 44 // // Keep these outside of the var block otherwise go generate will miss them. 45 var X__stderrp = Xstdout 46 var X__stdinp = Xstdin 47 var X__stdoutp = Xstdout 48 var X__sF [3]stdio.FILE 49 var X_tolower_tab_ = Xmalloc(nil, 2*65537) 50 var X_toupper_tab_ = Xmalloc(nil, 2*65537) 51 52 func init() { 53 for c := rune(0); c < 0xffff; c++ { 54 y := c 55 s := strings.ToLower(string(c)) 56 a := []rune(s) 57 if len(a) != 0 { 58 y = a[0] 59 } 60 (*[65536]uint16)(unsafe.Pointer(X_tolower_tab_))[c+1] = uint16(y) 61 y = c 62 s = strings.ToUpper(string(c)) 63 a = []rune(s) 64 if len(a) != 0 { 65 y = a[0] 66 } 67 (*[65536]uint16)(unsafe.Pointer(X_toupper_tab_))[c+1] = uint16(y) 68 } 69 } 70 71 // include/stdio.h:486:extern int __isthreaded; 72 var X__isthreaded int32 73 74 // lib/libc/locale/mblocal.h:62: int __mb_sb_limit; 75 var X__mb_sb_limit int32 = 128 // UTF-8 76 77 // include/runetype.h:94:extern _Thread_local const _RuneLocale *_ThreadRuneLocale; 78 var X_ThreadRuneLocale uintptr //TODO initialize and implement _Thread_local semantics. 79 80 // include/xlocale/_ctype.h:54:_RuneLocale *__runes_for_locale(locale_t, int*); 81 func X__runes_for_locale(t *TLS, l locale_t, p uintptr) uintptr { 82 panic(todo("")) 83 } 84 85 type file uintptr 86 87 func (f file) fd() int32 { return int32((*stdio.FILE)(unsafe.Pointer(f)).F_file) } 88 func (f file) setFd(fd int32) { (*stdio.FILE)(unsafe.Pointer(f)).F_file = int16(fd) } 89 90 func (f file) err() bool { 91 return (*stdio.FILE)(unsafe.Pointer(f)).F_flags&1 != 0 92 } 93 94 func (f file) setErr() { 95 (*stdio.FILE)(unsafe.Pointer(f)).F_flags |= 1 96 } 97 98 func (f file) close(t *TLS) int32 { 99 fd := f.fd() 100 r := Xclose(t, fd) 101 switch fd { 102 case unistd.STDIN_FILENO, unistd.STDOUT_FILENO, unistd.STDERR_FILENO: 103 X__sF[fd] = stdio.FILE{} 104 default: 105 Xfree(t, uintptr(f)) 106 } 107 if r < 0 { 108 return stdio.EOF 109 } 110 111 return 0 112 } 113 114 func newFile(t *TLS, fd int32) uintptr { 115 var p uintptr 116 switch fd { 117 case unistd.STDIN_FILENO: 118 p = uintptr(unsafe.Pointer(&X__sF[0])) 119 case unistd.STDOUT_FILENO: 120 p = uintptr(unsafe.Pointer(&X__sF[1])) 121 case unistd.STDERR_FILENO: 122 p = uintptr(unsafe.Pointer(&X__sF[2])) 123 default: 124 if p = Xcalloc(t, 1, types.Size_t(unsafe.Sizeof(stdio.FILE{}))); p == 0 { 125 return 0 126 } 127 } 128 file(p).setFd(fd) 129 return p 130 } 131 132 func fwrite(fd int32, b []byte) (int, error) { 133 if fd == unistd.STDOUT_FILENO { 134 return write(b) 135 } 136 137 // if dmesgs { 138 // dmesg("%v: fd %v: %s", origin(1), fd, b) 139 // } 140 return unix.Write(int(fd), b) //TODO use Xwrite 141 } 142 143 // unsigned long ___runetype(__ct_rune_t) __pure; 144 func X___runetype(t *TLS, x int32) ulong { 145 panic(todo("")) 146 } 147 148 // int fprintf(FILE *stream, const char *format, ...); 149 func Xfprintf(t *TLS, stream, format, args uintptr) int32 { 150 n, _ := fwrite(int32((*stdio.FILE)(unsafe.Pointer(stream)).F_file), printf(format, args)) 151 return int32(n) 152 } 153 154 // int usleep(useconds_t usec); 155 func Xusleep(t *TLS, usec uint32) int32 { 156 gotime.Sleep(gotime.Microsecond * gotime.Duration(usec)) 157 return 0 158 } 159 160 // int getrusage(int who, struct rusage *usage); 161 func Xgetrusage(t *TLS, who int32, usage uintptr) int32 { 162 if _, _, err := unix.Syscall(unix.SYS_GETRUSAGE, uintptr(who), usage, 0); err != 0 { 163 t.setErrno(err) 164 return -1 165 } 166 167 return 0 168 } 169 170 // int fgetc(FILE *stream); 171 func Xfgetc(t *TLS, stream uintptr) int32 { 172 fd := int((*stdio.FILE)(unsafe.Pointer(stream)).F_file) 173 var buf [1]byte 174 if n, _ := unix.Read(fd, buf[:]); n != 0 { 175 return int32(buf[0]) 176 } 177 178 return stdio.EOF 179 } 180 181 // int lstat(const char *pathname, struct stat *statbuf); 182 func Xlstat(t *TLS, pathname, statbuf uintptr) int32 { 183 return Xlstat64(t, pathname, statbuf) 184 } 185 186 // int stat(const char *pathname, struct stat *statbuf); 187 func Xstat(t *TLS, pathname, statbuf uintptr) int32 { 188 return Xstat64(t, pathname, statbuf) 189 } 190 191 // int chdir(const char *path); 192 func Xchdir(t *TLS, path uintptr) int32 { 193 if _, _, err := unix.Syscall(unix.SYS_CHDIR, path, 0, 0); err != 0 { 194 t.setErrno(err) 195 return -1 196 } 197 198 // if dmesgs { 199 // dmesg("%v: %q: ok", origin(1), GoString(path)) 200 // } 201 return 0 202 } 203 204 var localtime time.Tm 205 206 // struct tm *localtime(const time_t *timep); 207 func Xlocaltime(_ *TLS, timep uintptr) uintptr { 208 loc := getLocalLocation() 209 ut := *(*time.Time_t)(unsafe.Pointer(timep)) 210 t := gotime.Unix(int64(ut), 0).In(loc) 211 localtime.Ftm_sec = int32(t.Second()) 212 localtime.Ftm_min = int32(t.Minute()) 213 localtime.Ftm_hour = int32(t.Hour()) 214 localtime.Ftm_mday = int32(t.Day()) 215 localtime.Ftm_mon = int32(t.Month() - 1) 216 localtime.Ftm_year = int32(t.Year() - 1900) 217 localtime.Ftm_wday = int32(t.Weekday()) 218 localtime.Ftm_yday = int32(t.YearDay()) 219 localtime.Ftm_isdst = Bool32(isTimeDST(t)) 220 return uintptr(unsafe.Pointer(&localtime)) 221 } 222 223 // struct tm *localtime_r(const time_t *timep, struct tm *result); 224 func Xlocaltime_r(_ *TLS, timep, result uintptr) uintptr { 225 loc := getLocalLocation() 226 ut := *(*time.Time_t)(unsafe.Pointer(timep)) 227 t := gotime.Unix(int64(ut), 0).In(loc) 228 (*time.Tm)(unsafe.Pointer(result)).Ftm_sec = int32(t.Second()) 229 (*time.Tm)(unsafe.Pointer(result)).Ftm_min = int32(t.Minute()) 230 (*time.Tm)(unsafe.Pointer(result)).Ftm_hour = int32(t.Hour()) 231 (*time.Tm)(unsafe.Pointer(result)).Ftm_mday = int32(t.Day()) 232 (*time.Tm)(unsafe.Pointer(result)).Ftm_mon = int32(t.Month() - 1) 233 (*time.Tm)(unsafe.Pointer(result)).Ftm_year = int32(t.Year() - 1900) 234 (*time.Tm)(unsafe.Pointer(result)).Ftm_wday = int32(t.Weekday()) 235 (*time.Tm)(unsafe.Pointer(result)).Ftm_yday = int32(t.YearDay()) 236 (*time.Tm)(unsafe.Pointer(result)).Ftm_isdst = Bool32(isTimeDST(t)) 237 return result 238 } 239 240 // int open(const char *pathname, int flags, ...); 241 func Xopen(t *TLS, pathname uintptr, flags int32, args uintptr) int32 { 242 return Xopen64(t, pathname, flags, args) 243 } 244 245 // int open(const char *pathname, int flags, ...); 246 func Xopen64(t *TLS, pathname uintptr, flags int32, args uintptr) int32 { 247 var mode types.Mode_t 248 if args != 0 { 249 mode = (types.Mode_t)(VaUint32(&args)) 250 } 251 fdcwd := fcntl.AT_FDCWD 252 n, _, err := unix.Syscall6(unix.SYS_OPENAT, uintptr(fdcwd), pathname, uintptr(flags), uintptr(mode), 0, 0) 253 if err != 0 { 254 // if dmesgs { 255 // dmesg("%v: %q %#x: %v", origin(1), GoString(pathname), flags, err) 256 // } 257 t.setErrno(err) 258 return -1 259 } 260 261 // if dmesgs { 262 // dmesg("%v: %q flags %#x mode %#o: fd %v", origin(1), GoString(pathname), flags, mode, n) 263 // } 264 return int32(n) 265 } 266 267 // off_t lseek(int fd, off_t offset, int whence); 268 func Xlseek(t *TLS, fd int32, offset types.Off_t, whence int32) types.Off_t { 269 return types.Off_t(Xlseek64(t, fd, offset, whence)) 270 } 271 272 func whenceStr(whence int32) string { 273 panic(todo("")) 274 } 275 276 var fsyncStatbuf stat.Stat 277 278 // int fsync(int fd); 279 func Xfsync(t *TLS, fd int32) int32 { 280 if noFsync { 281 // Simulate -DSQLITE_NO_SYNC for sqlite3 testfixture, see function full_sync in sqlite3.c 282 return Xfstat(t, fd, uintptr(unsafe.Pointer(&fsyncStatbuf))) 283 } 284 285 if _, _, err := unix.Syscall(unix.SYS_FSYNC, uintptr(fd), 0, 0); err != 0 { 286 t.setErrno(err) 287 return -1 288 } 289 290 // if dmesgs { 291 // dmesg("%v: %d: ok", origin(1), fd) 292 // } 293 return 0 294 } 295 296 // long sysconf(int name); 297 func Xsysconf(t *TLS, name int32) long { 298 switch name { 299 case unistd.X_SC_PAGESIZE: 300 return long(unix.Getpagesize()) 301 case unistd.X_SC_GETPW_R_SIZE_MAX: 302 return -1 303 case unistd.X_SC_GETGR_R_SIZE_MAX: 304 return -1 305 case unistd.X_SC_NPROCESSORS_ONLN: 306 return long(runtime.NumCPU()) 307 } 308 309 panic(todo("", name)) 310 } 311 312 // int close(int fd); 313 func Xclose(t *TLS, fd int32) int32 { 314 if _, _, err := unix.Syscall(unix.SYS_CLOSE, uintptr(fd), 0, 0); err != 0 { 315 t.setErrno(err) 316 return -1 317 } 318 319 // if dmesgs { 320 // dmesg("%v: %d: ok", origin(1), fd) 321 // } 322 return 0 323 } 324 325 // char *getcwd(char *buf, size_t size); 326 func Xgetcwd(t *TLS, buf uintptr, size types.Size_t) uintptr { 327 if _, err := unix.Getcwd((*RawMem)(unsafe.Pointer(buf))[:size:size]); err != nil { 328 if dmesgs { 329 dmesg("%v: %v FAIL", origin(1), err) 330 } 331 t.setErrno(err) 332 return 0 333 } 334 335 if dmesgs { 336 dmesg("%v: ok", origin(1)) 337 } 338 return buf 339 } 340 341 // int fstat(int fd, struct stat *statbuf); 342 func Xfstat(t *TLS, fd int32, statbuf uintptr) int32 { 343 return Xfstat64(t, fd, statbuf) 344 } 345 346 // int ftruncate(int fd, off_t length); 347 func Xftruncate(t *TLS, fd int32, length types.Off_t) int32 { 348 if err := unix.Ftruncate(int(fd), int64(length)); err != nil { 349 if dmesgs { 350 dmesg("%v: fd %d: %v FAIL", origin(1), fd, err) 351 } 352 t.setErrno(err) 353 return -1 354 } 355 356 if dmesgs { 357 dmesg("%v: %d %#x: ok", origin(1), fd, length) 358 } 359 return 0 360 } 361 362 // int fcntl(int fd, int cmd, ... /* arg */ ); 363 func Xfcntl(t *TLS, fd, cmd int32, args uintptr) int32 { 364 return Xfcntl64(t, fd, cmd, args) 365 } 366 367 // ssize_t read(int fd, void *buf, size_t count); 368 func Xread(t *TLS, fd int32, buf uintptr, count types.Size_t) types.Ssize_t { 369 n, _, err := unix.Syscall(unix.SYS_READ, uintptr(fd), buf, uintptr(count)) 370 if err != 0 { 371 t.setErrno(err) 372 return -1 373 } 374 375 // if dmesgs { 376 // // dmesg("%v: %d %#x: %#x\n%s", origin(1), fd, count, n, hex.Dump(GoBytes(buf, int(n)))) 377 // dmesg("%v: %d %#x: %#x", origin(1), fd, count, n) 378 // } 379 return types.Ssize_t(n) 380 } 381 382 // ssize_t write(int fd, const void *buf, size_t count); 383 func Xwrite(t *TLS, fd int32, buf uintptr, count types.Size_t) types.Ssize_t { 384 const retry = 5 385 var err syscall.Errno 386 for i := 0; i < retry; i++ { 387 var n uintptr 388 switch n, _, err = unix.Syscall(unix.SYS_WRITE, uintptr(fd), buf, uintptr(count)); err { 389 case 0: 390 // if dmesgs { 391 // // dmesg("%v: %d %#x: %#x\n%s", origin(1), fd, count, n, hex.Dump(GoBytes(buf, int(n)))) 392 // dmesg("%v: %d %#x: %#x", origin(1), fd, count, n) 393 // } 394 return types.Ssize_t(n) 395 case errno.EAGAIN: 396 // nop 397 } 398 } 399 400 // if dmesgs { 401 // dmesg("%v: fd %v, count %#x: %v", origin(1), fd, count, err) 402 // } 403 t.setErrno(err) 404 return -1 405 } 406 407 // int fchmod(int fd, mode_t mode); 408 func Xfchmod(t *TLS, fd int32, mode types.Mode_t) int32 { 409 if _, _, err := unix.Syscall(unix.SYS_FCHMOD, uintptr(fd), uintptr(mode), 0); err != 0 { 410 t.setErrno(err) 411 return -1 412 } 413 414 // if dmesgs { 415 // dmesg("%v: %d %#o: ok", origin(1), fd, mode) 416 // } 417 return 0 418 } 419 420 // int fchown(int fd, uid_t owner, gid_t group); 421 func Xfchown(t *TLS, fd int32, owner types.Uid_t, group types.Gid_t) int32 { 422 if _, _, err := unix.Syscall(unix.SYS_FCHOWN, uintptr(fd), uintptr(owner), uintptr(group)); err != 0 { 423 t.setErrno(err) 424 return -1 425 } 426 427 return 0 428 } 429 430 // uid_t geteuid(void); 431 func Xgeteuid(t *TLS) types.Uid_t { 432 n, _, _ := unix.Syscall(unix.SYS_GETEUID, 0, 0, 0) 433 return types.Uid_t(n) 434 } 435 436 // int munmap(void *addr, size_t length); 437 func Xmunmap(t *TLS, addr uintptr, length types.Size_t) int32 { 438 if _, _, err := unix.Syscall(unix.SYS_MUNMAP, addr, uintptr(length), 0); err != 0 { 439 t.setErrno(err) 440 return -1 441 } 442 443 return 0 444 } 445 446 // int gettimeofday(struct timeval *tv, struct timezone *tz); 447 func Xgettimeofday(t *TLS, tv, tz uintptr) int32 { 448 if tz != 0 { 449 panic(todo("")) 450 } 451 452 var tvs unix.Timeval 453 err := unix.Gettimeofday(&tvs) 454 if err != nil { 455 t.setErrno(err) 456 return -1 457 } 458 459 *(*unix.Timeval)(unsafe.Pointer(tv)) = tvs 460 return 0 461 } 462 463 // int getsockopt(int sockfd, int level, int optname, void *optval, socklen_t *optlen); 464 func Xgetsockopt(t *TLS, sockfd, level, optname int32, optval, optlen uintptr) int32 { 465 if _, _, err := unix.Syscall6(unix.SYS_GETSOCKOPT, uintptr(sockfd), uintptr(level), uintptr(optname), optval, optlen, 0); err != 0 { 466 t.setErrno(err) 467 return -1 468 } 469 470 return 0 471 } 472 473 // int setsockopt(int sockfd, int level, int optname, const void *optval, socklen_t optlen); 474 func Xsetsockopt(t *TLS, sockfd, level, optname int32, optval uintptr, optlen socket.Socklen_t) int32 { 475 if _, _, err := unix.Syscall6(unix.SYS_SETSOCKOPT, uintptr(sockfd), uintptr(level), uintptr(optname), optval, uintptr(optlen), 0); err != 0 { 476 t.setErrno(err) 477 return -1 478 } 479 480 return 0 481 } 482 483 // int ioctl(int fd, unsigned long request, ...); 484 func Xioctl(t *TLS, fd int32, request ulong, va uintptr) int32 { 485 var argp uintptr 486 if va != 0 { 487 argp = VaUintptr(&va) 488 } 489 n, _, err := unix.Syscall(unix.SYS_IOCTL, uintptr(fd), uintptr(request), argp) 490 if err != 0 { 491 t.setErrno(err) 492 return -1 493 } 494 495 return int32(n) 496 } 497 498 // int getsockname(int sockfd, struct sockaddr *addr, socklen_t *addrlen); 499 func Xgetsockname(t *TLS, sockfd int32, addr, addrlen uintptr) int32 { 500 if _, _, err := unix.Syscall(unix.SYS_GETSOCKNAME, uintptr(sockfd), addr, addrlen); err != 0 { 501 // if dmesgs { 502 // dmesg("%v: fd %v: %v", origin(1), sockfd, err) 503 // } 504 t.setErrno(err) 505 return -1 506 } 507 508 return 0 509 } 510 511 // int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout); 512 func Xselect(t *TLS, nfds int32, readfds, writefds, exceptfds, timeout uintptr) int32 { 513 n, err := unix.Select( 514 int(nfds), 515 (*unix.FdSet)(unsafe.Pointer(readfds)), 516 (*unix.FdSet)(unsafe.Pointer(writefds)), 517 (*unix.FdSet)(unsafe.Pointer(exceptfds)), 518 (*unix.Timeval)(unsafe.Pointer(timeout)), 519 ) 520 if err != nil { 521 t.setErrno(err) 522 return -1 523 } 524 525 return int32(n) 526 } 527 528 // int mkfifo(const char *pathname, mode_t mode); 529 func Xmkfifo(t *TLS, pathname uintptr, mode types.Mode_t) int32 { 530 panic(todo("")) 531 } 532 533 // mode_t umask(mode_t mask); 534 func Xumask(t *TLS, mask types.Mode_t) types.Mode_t { 535 n, _, _ := unix.Syscall(unix.SYS_UMASK, uintptr(mask), 0, 0) 536 return types.Mode_t(n) 537 } 538 539 // int execvp(const char *file, char *const argv[]); 540 func Xexecvp(t *TLS, file, argv uintptr) int32 { 541 if _, _, err := unix.Syscall(unix.SYS_EXECVE, file, argv, Environ()); err != 0 { 542 t.setErrno(err) 543 return -1 544 } 545 546 return 0 547 } 548 549 // pid_t waitpid(pid_t pid, int *wstatus, int options); 550 func Xwaitpid(t *TLS, pid types.Pid_t, wstatus uintptr, optname int32) types.Pid_t { 551 n, _, err := unix.Syscall6(unix.SYS_WAIT4, uintptr(pid), wstatus, uintptr(optname), 0, 0, 0) 552 if err != 0 { 553 t.setErrno(err) 554 return -1 555 } 556 557 return types.Pid_t(n) 558 } 559 560 // int uname(struct utsname *buf); 561 func Xuname(t *TLS, buf uintptr) int32 { 562 if err := unix.Uname((*unix.Utsname)(unsafe.Pointer(buf))); err != nil { 563 if dmesgs { 564 dmesg("%v: %v FAIL", origin(1), err) 565 } 566 t.setErrno(err) 567 return -1 568 } 569 570 if dmesgs { 571 dmesg("%v: ok", origin(1)) 572 } 573 return 0 574 } 575 576 // ssize_t recv(int sockfd, void *buf, size_t len, int flags); 577 func Xrecv(t *TLS, sockfd int32, buf uintptr, len types.Size_t, flags int32) types.Ssize_t { 578 n, _, err := unix.Syscall6(unix.SYS_RECVFROM, uintptr(sockfd), buf, uintptr(len), uintptr(flags), 0, 0) 579 if err != 0 { 580 t.setErrno(err) 581 return -1 582 } 583 584 return types.Ssize_t(n) 585 } 586 587 // ssize_t send(int sockfd, const void *buf, size_t len, int flags); 588 func Xsend(t *TLS, sockfd int32, buf uintptr, len types.Size_t, flags int32) types.Ssize_t { 589 n, _, err := unix.Syscall6(unix.SYS_SENDTO, uintptr(sockfd), buf, uintptr(len), uintptr(flags), 0, 0) 590 if err != 0 { 591 t.setErrno(err) 592 return -1 593 } 594 595 return types.Ssize_t(n) 596 } 597 598 // int shutdown(int sockfd, int how); 599 func Xshutdown(t *TLS, sockfd, how int32) int32 { 600 if _, _, err := unix.Syscall(unix.SYS_SHUTDOWN, uintptr(sockfd), uintptr(how), 0); err != 0 { 601 t.setErrno(err) 602 return -1 603 } 604 605 return 0 606 } 607 608 // int getpeername(int sockfd, struct sockaddr *addr, socklen_t *addrlen); 609 func Xgetpeername(t *TLS, sockfd int32, addr uintptr, addrlen uintptr) int32 { 610 if _, _, err := unix.Syscall(unix.SYS_GETPEERNAME, uintptr(sockfd), addr, uintptr(addrlen)); err != 0 { 611 t.setErrno(err) 612 return -1 613 } 614 615 return 0 616 } 617 618 // int socket(int domain, int type, int protocol); 619 func Xsocket(t *TLS, domain, type1, protocol int32) int32 { 620 n, _, err := unix.Syscall(unix.SYS_SOCKET, uintptr(domain), uintptr(type1), uintptr(protocol)) 621 if err != 0 { 622 t.setErrno(err) 623 return -1 624 } 625 626 return int32(n) 627 } 628 629 // int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen); 630 func Xbind(t *TLS, sockfd int32, addr uintptr, addrlen uint32) int32 { 631 n, _, err := unix.Syscall(unix.SYS_BIND, uintptr(sockfd), addr, uintptr(addrlen)) 632 if err != 0 { 633 t.setErrno(err) 634 return -1 635 } 636 637 return int32(n) 638 } 639 640 // int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen); 641 func Xconnect(t *TLS, sockfd int32, addr uintptr, addrlen uint32) int32 { 642 if _, _, err := unix.Syscall(unix.SYS_CONNECT, uintptr(sockfd), addr, uintptr(addrlen)); err != 0 { 643 t.setErrno(err) 644 return -1 645 } 646 647 return 0 648 } 649 650 // int listen(int sockfd, int backlog); 651 func Xlisten(t *TLS, sockfd, backlog int32) int32 { 652 if _, _, err := unix.Syscall(unix.SYS_LISTEN, uintptr(sockfd), uintptr(backlog), 0); err != 0 { 653 t.setErrno(err) 654 return -1 655 } 656 657 return 0 658 } 659 660 // int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen); 661 func Xaccept(t *TLS, sockfd int32, addr uintptr, addrlen uintptr) int32 { 662 panic(todo("")) 663 // n, _, err := unix.Syscall6(unix.SYS_ACCEPT4, uintptr(sockfd), addr, uintptr(addrlen), 0, 0, 0) 664 // if err != 0 { 665 // t.setErrno(err) 666 // return -1 667 // } 668 669 // return int32(n) 670 } 671 672 // int getrlimit(int resource, struct rlimit *rlim); 673 func Xgetrlimit(t *TLS, resource int32, rlim uintptr) int32 { 674 return Xgetrlimit64(t, resource, rlim) 675 } 676 677 // int setrlimit(int resource, const struct rlimit *rlim); 678 func Xsetrlimit(t *TLS, resource int32, rlim uintptr) int32 { 679 return Xsetrlimit64(t, resource, rlim) 680 } 681 682 // int setrlimit(int resource, const struct rlimit *rlim); 683 func Xsetrlimit64(t *TLS, resource int32, rlim uintptr) int32 { 684 if _, _, err := unix.Syscall(unix.SYS_SETRLIMIT, uintptr(resource), uintptr(rlim), 0); err != 0 { 685 t.setErrno(err) 686 return -1 687 } 688 689 return 0 690 } 691 692 // uid_t getuid(void); 693 func Xgetuid(t *TLS) types.Uid_t { 694 return types.Uid_t(os.Getuid()) 695 } 696 697 // pid_t getpid(void); 698 func Xgetpid(t *TLS) int32 { 699 return int32(os.Getpid()) 700 } 701 702 // int system(const char *command); 703 func Xsystem(t *TLS, command uintptr) int32 { 704 s := GoString(command) 705 if command == 0 { 706 panic(todo("")) 707 } 708 709 cmd := exec.Command("sh", "-c", s) 710 cmd.Stdout = os.Stdout 711 cmd.Stderr = os.Stderr 712 err := cmd.Run() 713 if err != nil { 714 ps := err.(*exec.ExitError) 715 return int32(ps.ExitCode()) 716 } 717 718 return 0 719 } 720 721 // int setvbuf(FILE *stream, char *buf, int mode, size_t size); 722 func Xsetvbuf(t *TLS, stream, buf uintptr, mode int32, size types.Size_t) int32 { 723 return 0 //TODO 724 } 725 726 // int raise(int sig); 727 func Xraise(t *TLS, sig int32) int32 { 728 panic(todo("")) 729 } 730 731 // int backtrace(void **buffer, int size); 732 func Xbacktrace(t *TLS, buf uintptr, size int32) int32 { 733 panic(todo("")) 734 } 735 736 // void backtrace_symbols_fd(void *const *buffer, int size, int fd); 737 func Xbacktrace_symbols_fd(t *TLS, buffer uintptr, size, fd int32) { 738 panic(todo("")) 739 } 740 741 // int fileno(FILE *stream); 742 func Xfileno(t *TLS, stream uintptr) int32 { 743 panic(todo("")) 744 } 745 746 func newCFtsent(t *TLS, info int, path string, stat *unix.Stat_t, err syscall.Errno) uintptr { 747 p := Xcalloc(t, 1, types.Size_t(unsafe.Sizeof(fts.FTSENT{}))) 748 if p == 0 { 749 panic("OOM") 750 } 751 752 *(*fts.FTSENT)(unsafe.Pointer(p)) = *newFtsent(t, info, path, stat, err) 753 return p 754 } 755 756 func ftsentClose(t *TLS, p uintptr) { 757 Xfree(t, (*fts.FTSENT)(unsafe.Pointer(p)).Ffts_path) 758 Xfree(t, (*fts.FTSENT)(unsafe.Pointer(p)).Ffts_statp) 759 } 760 761 type ftstream struct { 762 s []uintptr 763 x int 764 } 765 766 func (f *ftstream) close(t *TLS) { 767 for _, p := range f.s { 768 ftsentClose(t, p) 769 Xfree(t, p) 770 } 771 *f = ftstream{} 772 } 773 774 // FTS *fts_open(char * const *path_argv, int options, int (*compar)(const FTSENT **, const FTSENT **)); 775 func Xfts_open(t *TLS, path_argv uintptr, options int32, compar uintptr) uintptr { 776 return Xfts64_open(t, path_argv, options, compar) 777 } 778 779 // FTS *fts_open(char * const *path_argv, int options, int (*compar)(const FTSENT **, const FTSENT **)); 780 func Xfts64_open(t *TLS, path_argv uintptr, options int32, compar uintptr) uintptr { 781 f := &ftstream{} 782 783 var walk func(string) 784 walk = func(path string) { 785 var fi os.FileInfo 786 var err error 787 switch { 788 case options&fts.FTS_LOGICAL != 0: 789 fi, err = os.Stat(path) 790 case options&fts.FTS_PHYSICAL != 0: 791 fi, err = os.Lstat(path) 792 default: 793 panic(todo("")) 794 } 795 796 if err != nil { 797 return 798 } 799 800 var statp *unix.Stat_t 801 if options&fts.FTS_NOSTAT == 0 { 802 var stat unix.Stat_t 803 switch { 804 case options&fts.FTS_LOGICAL != 0: 805 if err := unix.Stat(path, &stat); err != nil { 806 panic(todo("")) 807 } 808 case options&fts.FTS_PHYSICAL != 0: 809 if err := unix.Lstat(path, &stat); err != nil { 810 panic(todo("")) 811 } 812 default: 813 panic(todo("")) 814 } 815 816 statp = &stat 817 } 818 819 out: 820 switch { 821 case fi.IsDir(): 822 f.s = append(f.s, newCFtsent(t, fts.FTS_D, path, statp, 0)) 823 g, err := os.Open(path) 824 switch x := err.(type) { 825 case nil: 826 // ok 827 case *os.PathError: 828 f.s = append(f.s, newCFtsent(t, fts.FTS_DNR, path, statp, errno.EACCES)) 829 break out 830 default: 831 panic(todo("%q: %v %T", path, x, x)) 832 } 833 834 names, err := g.Readdirnames(-1) 835 g.Close() 836 if err != nil { 837 panic(todo("")) 838 } 839 840 for _, name := range names { 841 walk(path + "/" + name) 842 if f == nil { 843 break out 844 } 845 } 846 847 f.s = append(f.s, newCFtsent(t, fts.FTS_DP, path, statp, 0)) 848 default: 849 info := fts.FTS_F 850 if fi.Mode()&os.ModeSymlink != 0 { 851 info = fts.FTS_SL 852 } 853 switch { 854 case statp != nil: 855 f.s = append(f.s, newCFtsent(t, info, path, statp, 0)) 856 case options&fts.FTS_NOSTAT != 0: 857 f.s = append(f.s, newCFtsent(t, fts.FTS_NSOK, path, nil, 0)) 858 default: 859 panic(todo("")) 860 } 861 } 862 } 863 864 for { 865 p := *(*uintptr)(unsafe.Pointer(path_argv)) 866 if p == 0 { 867 if f == nil { 868 return 0 869 } 870 871 if compar != 0 { 872 panic(todo("")) 873 } 874 875 return addObject(f) 876 } 877 878 walk(GoString(p)) 879 path_argv += unsafe.Sizeof(uintptr(0)) 880 } 881 } 882 883 // FTSENT *fts_read(FTS *ftsp); 884 func Xfts_read(t *TLS, ftsp uintptr) uintptr { 885 return Xfts64_read(t, ftsp) 886 } 887 888 // FTSENT *fts_read(FTS *ftsp); 889 func Xfts64_read(t *TLS, ftsp uintptr) uintptr { 890 f := getObject(ftsp).(*ftstream) 891 if f.x == len(f.s) { 892 t.setErrno(0) 893 return 0 894 } 895 896 r := f.s[f.x] 897 if e := (*fts.FTSENT)(unsafe.Pointer(r)).Ffts_errno; e != 0 { 898 t.setErrno(e) 899 } 900 f.x++ 901 return r 902 } 903 904 // int fts_close(FTS *ftsp); 905 func Xfts_close(t *TLS, ftsp uintptr) int32 { 906 return Xfts64_close(t, ftsp) 907 } 908 909 // int fts_close(FTS *ftsp); 910 func Xfts64_close(t *TLS, ftsp uintptr) int32 { 911 getObject(ftsp).(*ftstream).close(t) 912 removeObject(ftsp) 913 return 0 914 } 915 916 // void tzset (void); 917 func Xtzset(t *TLS) { 918 //TODO 919 } 920 921 var strerrorBuf [100]byte 922 923 // char *strerror(int errnum); 924 func Xstrerror(t *TLS, errnum int32) uintptr { 925 if dmesgs { 926 dmesg("%v: %v\n%s", origin(1), errnum, debug.Stack()) 927 } 928 copy(strerrorBuf[:], fmt.Sprintf("strerror(%d)\x00", errnum)) 929 return uintptr(unsafe.Pointer(&strerrorBuf[0])) 930 } 931 932 // void *dlopen(const char *filename, int flags); 933 func Xdlopen(t *TLS, filename uintptr, flags int32) uintptr { 934 panic(todo("")) 935 } 936 937 // char *dlerror(void); 938 func Xdlerror(t *TLS) uintptr { 939 panic(todo("")) 940 } 941 942 // int dlclose(void *handle); 943 func Xdlclose(t *TLS, handle uintptr) int32 { 944 panic(todo("")) 945 } 946 947 // void *dlsym(void *handle, const char *symbol); 948 func Xdlsym(t *TLS, handle, symbol uintptr) uintptr { 949 panic(todo("")) 950 } 951 952 // void perror(const char *s); 953 func Xperror(t *TLS, s uintptr) { 954 panic(todo("")) 955 } 956 957 // int pclose(FILE *stream); 958 func Xpclose(t *TLS, stream uintptr) int32 { 959 panic(todo("")) 960 } 961 962 var gai_strerrorBuf [100]byte 963 964 // const char *gai_strerror(int errcode); 965 func Xgai_strerror(t *TLS, errcode int32) uintptr { 966 copy(gai_strerrorBuf[:], fmt.Sprintf("gai error %d\x00", errcode)) 967 return uintptr(unsafe.Pointer(&gai_strerrorBuf)) 968 } 969 970 // int tcgetattr(int fd, struct termios *termios_p); 971 func Xtcgetattr(t *TLS, fd int32, termios_p uintptr) int32 { 972 panic(todo("")) 973 } 974 975 // int tcsetattr(int fd, int optional_actions, const struct termios *termios_p); 976 func Xtcsetattr(t *TLS, fd, optional_actions int32, termios_p uintptr) int32 { 977 panic(todo("")) 978 } 979 980 // speed_t cfgetospeed(const struct termios *termios_p); 981 func Xcfgetospeed(t *TLS, termios_p uintptr) termios.Speed_t { 982 panic(todo("")) 983 } 984 985 // int cfsetospeed(struct termios *termios_p, speed_t speed); 986 func Xcfsetospeed(t *TLS, termios_p uintptr, speed uint32) int32 { 987 panic(todo("")) 988 } 989 990 // int cfsetispeed(struct termios *termios_p, speed_t speed); 991 func Xcfsetispeed(t *TLS, termios_p uintptr, speed uint32) int32 { 992 panic(todo("")) 993 } 994 995 // pid_t fork(void); 996 func Xfork(t *TLS) int32 { 997 t.setErrno(errno.ENOSYS) 998 return -1 999 } 1000 1001 var emptyStr = [1]byte{} 1002 1003 // char *setlocale(int category, const char *locale); 1004 func Xsetlocale(t *TLS, category int32, locale uintptr) uintptr { 1005 return uintptr(unsafe.Pointer(&emptyStr)) //TODO 1006 } 1007 1008 // char *nl_langinfo(nl_item item); 1009 func Xnl_langinfo(t *TLS, item langinfo.Nl_item) uintptr { 1010 return uintptr(unsafe.Pointer(&emptyStr)) //TODO 1011 } 1012 1013 // FILE *popen(const char *command, const char *type); 1014 func Xpopen(t *TLS, command, type1 uintptr) uintptr { 1015 panic(todo("")) 1016 } 1017 1018 // char *realpath(const char *path, char *resolved_path); 1019 func Xrealpath(t *TLS, path, resolved_path uintptr) uintptr { 1020 s, err := filepath.EvalSymlinks(GoString(path)) 1021 if err != nil { 1022 if os.IsNotExist(err) { 1023 // if dmesgs { 1024 // dmesg("%v: %q: %v", origin(1), GoString(path), err) 1025 // } 1026 t.setErrno(errno.ENOENT) 1027 return 0 1028 } 1029 1030 panic(todo("", err)) 1031 } 1032 1033 if resolved_path == 0 { 1034 panic(todo("")) 1035 } 1036 1037 if len(s) >= limits.PATH_MAX { 1038 s = s[:limits.PATH_MAX-1] 1039 } 1040 1041 copy((*RawMem)(unsafe.Pointer(resolved_path))[:len(s):len(s)], s) 1042 (*RawMem)(unsafe.Pointer(resolved_path))[len(s)] = 0 1043 return resolved_path 1044 } 1045 1046 // struct tm *gmtime_r(const time_t *timep, struct tm *result); 1047 func Xgmtime_r(t *TLS, timep, result uintptr) uintptr { 1048 panic(todo("")) 1049 } 1050 1051 // char *inet_ntoa(struct in_addr in); 1052 func Xinet_ntoa(t *TLS, in1 in.In_addr) uintptr { 1053 panic(todo("")) 1054 } 1055 1056 func X__ccgo_in6addr_anyp(t *TLS) uintptr { 1057 return uintptr(unsafe.Pointer(&in6_addr_any)) 1058 } 1059 1060 func Xabort(t *TLS) { 1061 panic(todo("")) //TODO 1062 // if dmesgs { 1063 // dmesg("%v:", origin(1)) 1064 // } 1065 // p := Xcalloc(t, 1, types.Size_t(unsafe.Sizeof(signal.Sigaction{}))) 1066 // if p == 0 { 1067 // panic("OOM") 1068 // } 1069 1070 // (*signal.Sigaction)(unsafe.Pointer(p)).F__sigaction_u.F__sa_handler = signal.SIG_DFL 1071 // Xsigaction(t, signal.SIGABRT, p, 0) 1072 // Xfree(t, p) 1073 // unix.Kill(unix.Getpid(), syscall.Signal(signal.SIGABRT)) 1074 // panic(todo("unrechable")) 1075 } 1076 1077 // int fflush(FILE *stream); 1078 func Xfflush(t *TLS, stream uintptr) int32 { 1079 return 0 //TODO 1080 } 1081 1082 // size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream); 1083 func Xfread(t *TLS, ptr uintptr, size, nmemb types.Size_t, stream uintptr) types.Size_t { 1084 m, _, err := unix.Syscall(unix.SYS_READ, uintptr(file(stream).fd()), ptr, uintptr(size*nmemb)) 1085 if err != 0 { 1086 file(stream).setErr() 1087 return 0 1088 } 1089 1090 // if dmesgs { 1091 // // 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)))) 1092 // dmesg("%v: %d %#x x %#x: %#x", origin(1), file(stream).fd(), size, nmemb, types.Size_t(m)/size) 1093 // } 1094 return types.Size_t(m) / size 1095 } 1096 1097 // size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream); 1098 func Xfwrite(t *TLS, ptr uintptr, size, nmemb types.Size_t, stream uintptr) types.Size_t { 1099 m, _, err := unix.Syscall(unix.SYS_WRITE, uintptr(file(stream).fd()), ptr, uintptr(size*nmemb)) 1100 if err != 0 { 1101 file(stream).setErr() 1102 return 0 1103 } 1104 1105 // if dmesgs { 1106 // // 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)))) 1107 // dmesg("%v: %d %#x x %#x: %#x", origin(1), file(stream).fd(), size, nmemb, types.Size_t(m)/size) 1108 // } 1109 return types.Size_t(m) / size 1110 } 1111 1112 // int fclose(FILE *stream); 1113 func Xfclose(t *TLS, stream uintptr) int32 { 1114 return file(stream).close(t) 1115 } 1116 1117 // int fputc(int c, FILE *stream); 1118 func Xfputc(t *TLS, c int32, stream uintptr) int32 { 1119 if _, err := fwrite(file(stream).fd(), []byte{byte(c)}); err != nil { 1120 return stdio.EOF 1121 } 1122 1123 return int32(byte(c)) 1124 } 1125 1126 // int fseek(FILE *stream, long offset, int whence); 1127 func Xfseek(t *TLS, stream uintptr, offset long, whence int32) int32 { 1128 if n := Xlseek(t, int32(file(stream).fd()), types.Off_t(offset), whence); n < 0 { 1129 // if dmesgs { 1130 // dmesg("%v: fd %v, off %#x, whence %v: %v", origin(1), file(stream).fd(), offset, whenceStr(whence), n) 1131 // } 1132 file(stream).setErr() 1133 return -1 1134 } 1135 1136 // if dmesgs { 1137 // dmesg("%v: fd %v, off %#x, whence %v: ok", origin(1), file(stream).fd(), offset, whenceStr(whence)) 1138 // } 1139 return 0 1140 } 1141 1142 // long ftell(FILE *stream); 1143 func Xftell(t *TLS, stream uintptr) long { 1144 n := Xlseek(t, file(stream).fd(), 0, stdio.SEEK_CUR) 1145 if n < 0 { 1146 file(stream).setErr() 1147 return -1 1148 } 1149 1150 // if dmesgs { 1151 // dmesg("%v: fd %v, n %#x: ok %#x", origin(1), file(stream).fd(), n, long(n)) 1152 // } 1153 return long(n) 1154 } 1155 1156 // int ferror(FILE *stream); 1157 func Xferror(t *TLS, stream uintptr) int32 { 1158 return Bool32(file(stream).err()) 1159 } 1160 1161 // int ungetc(int c, FILE *stream); 1162 func Xungetc(t *TLS, c int32, stream uintptr) int32 { 1163 panic(todo("")) 1164 } 1165 1166 // int fscanf(FILE *stream, const char *format, ...); 1167 func Xfscanf(t *TLS, stream, format, va uintptr) int32 { 1168 panic(todo("")) 1169 } 1170 1171 // int fputs(const char *s, FILE *stream); 1172 func Xfputs(t *TLS, s, stream uintptr) int32 { 1173 if _, _, err := unix.Syscall(unix.SYS_WRITE, uintptr(file(stream).fd()), s, uintptr(Xstrlen(t, s))); err != 0 { 1174 return -1 1175 } 1176 1177 return 0 1178 } 1179 1180 var getservbynameStaticResult netdb.Servent 1181 1182 // struct servent *getservbyname(const char *name, const char *proto); 1183 func Xgetservbyname(t *TLS, name, proto uintptr) uintptr { 1184 var protoent *gonetdb.Protoent 1185 if proto != 0 { 1186 protoent = gonetdb.GetProtoByName(GoString(proto)) 1187 } 1188 servent := gonetdb.GetServByName(GoString(name), protoent) 1189 if servent == nil { 1190 // if dmesgs { 1191 // dmesg("%q %q: nil (protoent %+v)", GoString(name), GoString(proto), protoent) 1192 // } 1193 return 0 1194 } 1195 1196 Xfree(t, (*netdb.Servent)(unsafe.Pointer(&getservbynameStaticResult)).Fs_name) 1197 if v := (*netdb.Servent)(unsafe.Pointer(&getservbynameStaticResult)).Fs_aliases; v != 0 { 1198 for { 1199 p := *(*uintptr)(unsafe.Pointer(v)) 1200 if p == 0 { 1201 break 1202 } 1203 1204 Xfree(t, p) 1205 v += unsafe.Sizeof(uintptr(0)) 1206 } 1207 Xfree(t, v) 1208 } 1209 Xfree(t, (*netdb.Servent)(unsafe.Pointer(&getservbynameStaticResult)).Fs_proto) 1210 cname, err := CString(servent.Name) 1211 if err != nil { 1212 getservbynameStaticResult = netdb.Servent{} 1213 return 0 1214 } 1215 1216 var protoname uintptr 1217 if protoent != nil { 1218 if protoname, err = CString(protoent.Name); err != nil { 1219 Xfree(t, cname) 1220 getservbynameStaticResult = netdb.Servent{} 1221 return 0 1222 } 1223 } 1224 var a []uintptr 1225 for _, v := range servent.Aliases { 1226 cs, err := CString(v) 1227 if err != nil { 1228 for _, v := range a { 1229 Xfree(t, v) 1230 } 1231 return 0 1232 } 1233 1234 a = append(a, cs) 1235 } 1236 v := Xcalloc(t, types.Size_t(len(a)+1), types.Size_t(unsafe.Sizeof(uintptr(0)))) 1237 if v == 0 { 1238 Xfree(t, cname) 1239 Xfree(t, protoname) 1240 for _, v := range a { 1241 Xfree(t, v) 1242 } 1243 getservbynameStaticResult = netdb.Servent{} 1244 return 0 1245 } 1246 for _, p := range a { 1247 *(*uintptr)(unsafe.Pointer(v)) = p 1248 v += unsafe.Sizeof(uintptr(0)) 1249 } 1250 1251 getservbynameStaticResult = netdb.Servent{ 1252 Fs_name: cname, 1253 Fs_aliases: v, 1254 Fs_port: int32(servent.Port), 1255 Fs_proto: protoname, 1256 } 1257 return uintptr(unsafe.Pointer(&getservbynameStaticResult)) 1258 } 1259 1260 func Xreaddir64(t *TLS, dir uintptr) uintptr { 1261 return Xreaddir(t, dir) 1262 } 1263 1264 func __syscall(r, _ uintptr, errno syscall.Errno) long { 1265 if errno != 0 { 1266 return long(-errno) 1267 } 1268 1269 return long(r) 1270 } 1271 1272 func X__syscall1(t *TLS, trap, p1 long) long { 1273 return __syscall(unix.Syscall(uintptr(trap), uintptr(p1), 0, 0)) 1274 } 1275 1276 func X__syscall3(t *TLS, trap, p1, p2, p3 long) long { 1277 return __syscall(unix.Syscall(uintptr(trap), uintptr(p1), uintptr(p2), uintptr(p3))) 1278 } 1279 1280 func X__syscall4(t *TLS, trap, p1, p2, p3, p4 long) long { 1281 return __syscall(unix.Syscall6(uintptr(trap), uintptr(p1), uintptr(p2), uintptr(p3), uintptr(p4), 0, 0)) 1282 } 1283 1284 func fcntlCmdStr(cmd int32) string { 1285 switch cmd { 1286 case fcntl.F_GETOWN: 1287 return "F_GETOWN" 1288 case fcntl.F_SETLK: 1289 return "F_SETLK" 1290 case fcntl.F_GETLK: 1291 return "F_GETLK" 1292 case fcntl.F_SETFD: 1293 return "F_SETFD" 1294 case fcntl.F_GETFD: 1295 return "F_GETFD" 1296 default: 1297 return fmt.Sprintf("cmd(%d)", cmd) 1298 } 1299 } 1300 1301 // int setenv(const char *name, const char *value, int overwrite); 1302 func Xsetenv(t *TLS, name, value uintptr, overwrite int32) int32 { 1303 panic(todo("")) 1304 } 1305 1306 // int unsetenv(const char *name); 1307 func Xunsetenv(t *TLS, name uintptr) int32 { 1308 panic(todo("")) 1309 } 1310 1311 // int pause(void); 1312 func Xpause(t *TLS) int32 { 1313 panic(todo("")) 1314 } 1315 1316 // ssize_t writev(int fd, const struct iovec *iov, int iovcnt); 1317 func Xwritev(t *TLS, fd int32, iov uintptr, iovcnt int32) types.Ssize_t { 1318 panic(todo("")) 1319 } 1320 1321 // int __isoc99_sscanf(const char *str, const char *format, ...); 1322 func X__isoc99_sscanf(t *TLS, str, format, va uintptr) int32 { 1323 r := Xsscanf(t, str, format, va) 1324 // if dmesgs { 1325 // dmesg("%v: %q %q: %d", origin(1), GoString(str), GoString(format), r) 1326 // } 1327 return r 1328 } 1329 1330 // void __assert(const char * func, const char * file, int line, const char *expr) __dead2; 1331 func X__assert(t *TLS, fn, file uintptr, line int32, expr uintptr) { 1332 X__assert_fail(t, expr, file, uint32(line), fn) 1333 } 1334 1335 func X__assert13(t *TLS, file uintptr, line int32, fn, msg uintptr) { 1336 X__assert_fail(t, msg, file, uint32(line), fn) 1337 } 1338 1339 // include/stdio.h:456:int __swbuf(int, FILE *); 1340 func X__swbuf(t *TLS, n int32, file uintptr) int32 { 1341 return Xfputc(t, n, file) //TODO improve performance, use a real buffer. 1342 } 1343 1344 // int rmdir(const char *pathname); 1345 func Xrmdir(t *TLS, pathname uintptr) int32 { 1346 if err := unix.Rmdir(GoString(pathname)); err != nil { 1347 if dmesgs { 1348 dmesg("%v: %v FAIL", origin(1), err) 1349 } 1350 t.setErrno(err) 1351 return -1 1352 } 1353 1354 if dmesgs { 1355 dmesg("%v: ok", origin(1)) 1356 } 1357 return 0 1358 } 1359 1360 // struct dirent *readdir(DIR *dirp); 1361 func Xreaddir(t *TLS, dir uintptr) uintptr { 1362 if (*darwinDir)(unsafe.Pointer(dir)).eof { 1363 return 0 1364 } 1365 1366 if (*darwinDir)(unsafe.Pointer(dir)).l == (*darwinDir)(unsafe.Pointer(dir)).h { 1367 n, err := unix.Getdirentries((*darwinDir)(unsafe.Pointer(dir)).fd, (*darwinDir)(unsafe.Pointer(dir)).buf[:], nil) 1368 // trc("must read: %v %v", n, err) 1369 if n == 0 { 1370 if err != nil && err != io.EOF { 1371 if dmesgs { 1372 dmesg("%v: %v FAIL", origin(1), err) 1373 } 1374 t.setErrno(err) 1375 } 1376 (*darwinDir)(unsafe.Pointer(dir)).eof = true 1377 return 0 1378 } 1379 1380 (*darwinDir)(unsafe.Pointer(dir)).l = 0 1381 (*darwinDir)(unsafe.Pointer(dir)).h = n 1382 // trc("new l %v, h %v", (*darwinDir)(unsafe.Pointer(dir)).l, (*darwinDir)(unsafe.Pointer(dir)).h) 1383 } 1384 de := dir + unsafe.Offsetof(darwinDir{}.buf) + uintptr((*darwinDir)(unsafe.Pointer(dir)).l) 1385 (*darwinDir)(unsafe.Pointer(dir)).l += int((*unix.Dirent)(unsafe.Pointer(de)).Reclen) 1386 return de 1387 } 1388 1389 type darwinDir struct { 1390 buf [4096]byte 1391 fd int 1392 h int 1393 l int 1394 1395 eof bool 1396 } 1397 1398 // int sscanf(const char *str, const char *format, ...); 1399 func Xsscanf(t *TLS, str, format, va uintptr) int32 { 1400 r := scanf(strings.NewReader(GoString(str)), format, va) 1401 // if dmesgs { 1402 // dmesg("%v: %q %q: %d", origin(1), GoString(str), GoString(format), r) 1403 // } 1404 return r 1405 } 1406 1407 // int * __error(void); 1408 func X__error(t *TLS) uintptr { 1409 return t.errnop 1410 } 1411 1412 func Xclosedir(t *TLS, dir uintptr) int32 { 1413 r := Xclose(t, int32((*darwinDir)(unsafe.Pointer(dir)).fd)) 1414 Xfree(t, dir) 1415 return r 1416 } 1417 1418 // int __xuname(int namesize, void *namebuf) 1419 func X__xuname(t *TLS, namesize int32, namebuf uintptr) int32 { 1420 return Xuname(t, namebuf) 1421 } 1422 1423 // int chflags(const char *path, u_int flags); 1424 func Xchflags(t *TLS, path uintptr, flags uint32) int32 { 1425 if err := unix.Chflags(GoString(path), int(flags)); err != nil { 1426 if dmesgs { 1427 dmesg("%v: %v FAIL", origin(1), err) 1428 } 1429 t.setErrno(err) 1430 return -1 1431 } 1432 1433 if dmesgs { 1434 dmesg("%v: ok", origin(1)) 1435 } 1436 return 0 1437 } 1438 1439 // int pipe(int pipefd[2]); 1440 func Xpipe(t *TLS, pipefd uintptr) int32 { 1441 var a [2]int 1442 if err := syscall.Pipe(a[:]); err != nil { 1443 if dmesgs { 1444 dmesg("%v: %v FAIL", origin(1), err) 1445 } 1446 t.setErrno(err) 1447 return -1 1448 } 1449 1450 *(*[2]int32)(unsafe.Pointer(pipefd)) = [2]int32{int32(a[0]), int32(a[1])} 1451 if dmesgs { 1452 dmesg("%v: %v ok", origin(1), a) 1453 } 1454 return 0 1455 } 1456 1457 // char *inet_ntoa(struct in_addr in); 1458 func X__inet_ntoa(t *TLS, in1 in.In_addr) uintptr { 1459 panic(todo("")) 1460 } 1461 1462 func Xmmap(t *TLS, addr uintptr, length types.Size_t, prot, flags, fd int32, offset types.Off_t) uintptr { 1463 // On 2021-12-23, a new syscall for mmap was introduced: 1464 // 1465 // 49 STD NOLOCK { void *sys_mmap(void *addr, size_t len, int prot, \ 1466 // int flags, int fd, off_t pos); } 1467 // src: https://github.com/golang/go/issues/59661 1468 1469 const unix_SYS_MMAP = 49 1470 1471 // Cannot avoid the syscall here, addr sometimes matter. 1472 data, _, err := unix.Syscall6(unix_SYS_MMAP, addr, uintptr(length), uintptr(prot), uintptr(flags), uintptr(fd), uintptr(offset)) 1473 if err != 0 { 1474 if dmesgs { 1475 dmesg("%v: %v FAIL", origin(1), err) 1476 } 1477 t.setErrno(err) 1478 return ^uintptr(0) // (void*)-1 1479 } 1480 1481 if dmesgs { 1482 dmesg("%v: %#x", origin(1), data) 1483 } 1484 return data 1485 } 1486 1487 func X__errno(t *TLS) uintptr { 1488 return X__errno_location(t) 1489 } 1490 1491 func X__ccgo_pthreadMutexattrGettype(tls *TLS, a uintptr) int32 { /* pthread_attr_get.c:93:5: */ 1492 return (int32((*pthread_mutexattr_t)(unsafe.Pointer(a)).F__attr & uint32(3))) 1493 } 1494 1495 func X__ccgo_getMutexType(tls *TLS, m uintptr) int32 { /* pthread_mutex_lock.c:3:5: */ 1496 return (*(*int32)(unsafe.Pointer((m /* &.__u */ /* &.__i */))) & 15) 1497 } 1498 1499 func X__ccgo_pthreadAttrGetDetachState(tls *TLS, a uintptr) int32 { /* pthread_attr_get.c:3:5: */ 1500 return *(*int32)(unsafe.Pointer((a /* &.__u */ /* &.__i */) + 6*4)) 1501 } 1502 1503 func Xpthread_attr_getdetachstate(tls *TLS, a uintptr, state uintptr) int32 { /* pthread_attr_get.c:7:5: */ 1504 *(*int32)(unsafe.Pointer(state)) = *(*int32)(unsafe.Pointer((a /* &.__u */ /* &.__i */) + 6*4)) 1505 return 0 1506 } 1507 1508 func Xpthread_attr_setdetachstate(tls *TLS, a uintptr, state int32) int32 { /* pthread_attr_setdetachstate.c:3:5: */ 1509 if uint32(state) > 1 { 1510 return 22 1511 } 1512 *(*int32)(unsafe.Pointer((a /* &.__u */ /* &.__i */) + 6*4)) = state 1513 return 0 1514 } 1515 1516 func Xpthread_mutexattr_destroy(tls *TLS, a uintptr) int32 { /* pthread_mutexattr_destroy.c:3:5: */ 1517 return 0 1518 } 1519 1520 func Xpthread_mutexattr_init(tls *TLS, a uintptr) int32 { /* pthread_mutexattr_init.c:3:5: */ 1521 *(*pthread_mutexattr_t)(unsafe.Pointer(a)) = pthread_mutexattr_t{} 1522 return 0 1523 } 1524 1525 func Xpthread_mutexattr_settype(tls *TLS, a uintptr, type1 int32) int32 { /* pthread_mutexattr_settype.c:3:5: */ 1526 if uint32(type1) > uint32(2) { 1527 return 22 1528 } 1529 (*pthread_mutexattr_t)(unsafe.Pointer(a)).F__attr = (((*pthread_mutexattr_t)(unsafe.Pointer(a)).F__attr & Uint32FromInt32(CplInt32(3))) | uint32(type1)) 1530 return 0 1531 } 1532 1533 // int uuid_parse( char *in, uuid_t uu); 1534 func Xuuid_parse(t *TLS, in uintptr, uu uintptr) int32 { 1535 r, err := guuid.Parse(GoString(in)) 1536 if err != nil { 1537 return -1 1538 } 1539 1540 copy((*RawMem)(unsafe.Pointer(uu))[:unsafe.Sizeof(uuid.Uuid_t{})], r[:]) 1541 return 0 1542 } 1543 1544 func X__srget(t *TLS, stream uintptr) int32 { return Xgetc(t, stream) } 1545 1546 // void __assert2(const char *, int, const char *, const char *); 1547 // __assert2(__FILE__, __LINE__, __func__, #e)) 1548 func X__assert2(t *TLS, file uintptr, line int32, fn, expr uintptr) { 1549 X__assert_fail(t, expr, file, uint32(line), fn) 1550 } 1551 1552 // int getpagesize(void); 1553 func Xgetpagesize(t *TLS) int32 { 1554 return int32(unix.Getpagesize()) 1555 } 1556 1557 const PTHREAD_MUTEX_DEFAULT = 0 1558 1559 // The pthread_mutex_init() function shall initialize the mutex referenced by 1560 // mutex with attributes specified by attr. If attr is NULL, the default mutex 1561 // attributes are used; the effect shall be the same as passing the address of 1562 // a default mutex attributes object. Upon successful initialization, the state 1563 // of the mutex becomes initialized and unlocked. 1564 // 1565 // If successful, the pthread_mutex_destroy() and pthread_mutex_init() 1566 // functions shall return zero; otherwise, an error number shall be returned to 1567 // indicate the error. 1568 // 1569 // int pthread_mutex_init(pthread_mutex_t *restrict mutex, const pthread_mutexattr_t *restrict attr); 1570 func Xpthread_mutex_init(t *TLS, pMutex, pAttr uintptr) int32 { 1571 typ := PTHREAD_MUTEX_DEFAULT 1572 if pAttr != 0 { 1573 typ = int(X__ccgo_pthreadMutexattrGettype(t, pAttr)) 1574 } 1575 mutexesMu.Lock() 1576 1577 defer mutexesMu.Unlock() 1578 1579 mutexes[pMutex] = newMutex(typ) 1580 return 0 1581 } 1582 1583 // uint16_t __builtin_bswap16 (uint32_t x) 1584 func Xbswap16(t *TLS, x uint16) uint16 { 1585 return X__builtin_bswap16(t, x) 1586 } 1587 1588 // uint32_t __builtin_bswap32 (uint32_t x) 1589 func Xbswap32(t *TLS, x uint32) uint32 { 1590 return X__builtin_bswap32(t, x) 1591 } 1592 1593 // uint64_t __builtin_bswap64 (uint64_t x) 1594 func Xbswap64(t *TLS, x uint64) uint64 { 1595 return X__builtin_bswap64(t, x) 1596 } 1597 1598 func X__builtin_isblank(t *TLS, _c int32) int32 { return Xisblank(t, _c) }