modernc.org/libc@v1.24.1/libc_linux.go (about)

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