source: code/trunk/vendor/modernc.org/libc/libc_linux.go@ 823

Last change on this file since 823 was 822, checked in by yakumo.izuru, 22 months ago

Prefer immortal.run over runit and rc.d, use vendored modules
for convenience.

Signed-off-by: Izuru Yakumo <yakumo.izuru@…>

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