1 // Copyright 2023 The Go 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.
16 type Dircookie = uint64
21 FILETYPE_UNKNOWN Filetype = iota
23 FILETYPE_CHARACTER_DEVICE
27 FILETYPE_SOCKET_STREAM
28 FILETYPE_SYMBOLIC_LINK
32 // The offset of the next directory entry stored in this directory.
34 // The serial number of the file referred to by this directory entry.
36 // The length of the name of the directory entry.
38 // The type of the file referred to by this directory entry.
40 // Name of the directory entry.
44 func direntIno(buf []byte) (uint64, bool) {
45 return readInt(buf, unsafe.Offsetof(Dirent{}.Ino), unsafe.Sizeof(Dirent{}.Ino))
48 func direntReclen(buf []byte) (uint64, bool) {
49 namelen, ok := direntNamlen(buf)
50 return 24 + namelen, ok
53 func direntNamlen(buf []byte) (uint64, bool) {
54 return readInt(buf, unsafe.Offsetof(Dirent{}.Namlen), unsafe.Sizeof(Dirent{}.Namlen))
57 // An Errno is an unsigned number describing an error condition.
58 // It implements the error interface. The zero Errno is by convention
59 // a non-error, so code to convert from Errno to error should use:
67 func (e Errno) Error() string {
68 if 0 <= int(e) && int(e) < len(errorstr) {
74 return "errno " + itoa.Itoa(int(e))
77 func (e Errno) Is(target error) bool {
79 case oserror.ErrPermission:
80 return e == EACCES || e == EPERM
81 case oserror.ErrExist:
82 return e == EEXIST || e == ENOTEMPTY
83 case oserror.ErrNotExist:
85 case errors.ErrUnsupported:
91 func (e Errno) Temporary() bool {
92 return e == EINTR || e == EMFILE || e.Timeout()
95 func (e Errno) Timeout() bool {
96 return e == EAGAIN || e == ETIMEDOUT
99 // A Signal is a number describing a process signal.
100 // It implements the os.Signal interface.
104 SIGNONE Signal = iota
137 func (s Signal) Signal() {}
139 func (s Signal) String() string {
150 return "illegal instruction"
152 return "trace/breakpoint trap"
158 return "floating point exception"
162 return "user defined signal 1"
164 return "segmentation fault"
166 return "user defined signal 2"
174 return "child exited"
178 return "stopped (signal)"
182 return "stopped (tty input)"
184 return "stopped (tty output)"
186 return "urgent I/O condition"
188 return "CPU time limit exceeded"
190 return "file size limit exceeded"
192 return "virtual timer expired"
194 return "profiling timer expired"
196 return "window changed"
198 return "I/O possible"
200 return "power failure"
202 return "bad system call"
204 return "signal " + itoa.Itoa(int(s))
253 S_IFSHM_SYSV = 0000300000
254 S_IFSEMA = 0000270000
255 S_IFCOND = 0000260000
256 S_IFMUTEX = 0000250000
258 S_IFBOUNDSOCK = 0000230000
259 S_IFSOCKADDR = 0000220000
260 S_IFDSOCK = 0000210000
262 S_IFSOCK = 0000140000
296 type WaitStatus uint32
298 func (w WaitStatus) Exited() bool { return false }
299 func (w WaitStatus) ExitStatus() int { return 0 }
300 func (w WaitStatus) Signaled() bool { return false }
301 func (w WaitStatus) Signal() Signal { return 0 }
302 func (w WaitStatus) CoreDump() bool { return false }
303 func (w WaitStatus) Stopped() bool { return false }
304 func (w WaitStatus) Continued() bool { return false }
305 func (w WaitStatus) StopSignal() Signal { return 0 }
306 func (w WaitStatus) TrapCause() int { return 0 }
308 // Rusage is a placeholder to allow compilation of the os/exec package
309 // because we need Go programs to be portable across platforms. WASI does
310 // not have a mechanism to to spawn processes so there is no reason for an
311 // application to take a dependency on this type.
317 // ProcAttr is a placeholder to allow compilation of the os/exec package
318 // because we need Go programs to be portable across platforms. WASI does
319 // not have a mechanism to to spawn processes so there is no reason for an
320 // application to take a dependency on this type.
321 type ProcAttr struct {
328 type SysProcAttr struct {
331 func Syscall(trap, a1, a2, a3 uintptr) (r1, r2 uintptr, err Errno) {
335 func Syscall6(trap, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2 uintptr, err Errno) {
339 func RawSyscall(trap, a1, a2, a3 uintptr) (r1, r2 uintptr, err Errno) {
343 func RawSyscall6(trap, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2 uintptr, err Errno) {
347 func Sysctl(key string) (string, error) {
348 if key == "kern.hostname" {
370 func Getgroups() ([]int, error) {
382 func Gettimeofday(tv *Timeval) error {
384 if errno := clock_time_get(clockRealtime, 1e3, unsafe.Pointer(&time)); errno != 0 {
387 tv.setTimestamp(time)
391 func Kill(pid int, signum Signal) error {
392 // WASI does not have the notion of processes nor signal handlers.
394 // Any signal that the application raises to the process itself will
395 // be interpreted as being cause for termination.
396 if pid > 0 && pid != Getpid() {
399 ProcExit(128 + int32(signum))
403 func Sendfile(outfd int, infd int, offset *int64, count int) (written int, err error) {
407 func StartProcess(argv0 string, argv []string, attr *ProcAttr) (pid int, handle uintptr, err error) {
411 func Wait4(pid int, wstatus *WaitStatus, options int, rusage *Rusage) (wpid int, err error) {
415 func Umask(mask int) int {
419 type Timespec struct {
424 func (ts *Timespec) timestamp() timestamp {
425 return timestamp(ts.Sec*1e9) + timestamp(ts.Nsec)
428 func (ts *Timespec) setTimestamp(t timestamp) {
429 ts.Sec = int64(t / 1e9)
430 ts.Nsec = int64(t % 1e9)
433 type Timeval struct {
438 func (tv *Timeval) timestamp() timestamp {
439 return timestamp(tv.Sec*1e9) + timestamp(tv.Usec*1e3)
442 func (tv *Timeval) setTimestamp(t timestamp) {
443 tv.Sec = int64(t / 1e9)
444 tv.Usec = int64((t % 1e9) / 1e3)
447 func setTimespec(sec, nsec int64) Timespec {
448 return Timespec{Sec: sec, Nsec: nsec}
451 func setTimeval(sec, usec int64) Timeval {
452 return Timeval{Sec: sec, Usec: usec}
455 type clockid = uint32
458 clockRealtime clockid = iota
460 clockProcessCPUTimeID
464 //go:wasmimport wasi_snapshot_preview1 clock_time_get
466 func clock_time_get(id clockid, precision timestamp, time unsafe.Pointer) Errno
468 func SetNonblock(fd int, nonblocking bool) error {
469 flags, err := fd_fdstat_get_flags(fd)
474 flags |= FDFLAG_NONBLOCK
476 flags &^= FDFLAG_NONBLOCK
478 errno := fd_fdstat_set_flags(int32(fd), flags)
479 return errnoErr(errno)
491 func Getrlimit(which int, lim *Rlimit) error {