1// Copyright 2018 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.
4
5// +build js,wasm
6
7package syscall
8
9import (
10	"sync"
11	"unsafe"
12)
13
14const direntSize = 8 + 8 + 2 + 256
15
16type Dirent struct {
17	Reclen uint16
18	Name   [256]byte
19}
20
21func direntIno(buf []byte) (uint64, bool) {
22	return 1, true
23}
24
25func direntReclen(buf []byte) (uint64, bool) {
26	return readInt(buf, unsafe.Offsetof(Dirent{}.Reclen), unsafe.Sizeof(Dirent{}.Reclen))
27}
28
29func direntNamlen(buf []byte) (uint64, bool) {
30	reclen, ok := direntReclen(buf)
31	if !ok {
32		return 0, false
33	}
34	return reclen - uint64(unsafe.Offsetof(Dirent{}.Name)), true
35}
36
37const PathMax = 256
38
39// An Errno is an unsigned number describing an error condition.
40// It implements the error interface. The zero Errno is by convention
41// a non-error, so code to convert from Errno to error should use:
42//	err = nil
43//	if errno != 0 {
44//		err = errno
45//	}
46type Errno uintptr
47
48func (e Errno) Error() string {
49	if 0 <= int(e) && int(e) < len(errorstr) {
50		s := errorstr[e]
51		if s != "" {
52			return s
53		}
54	}
55	return "errno " + itoa(int(e))
56}
57
58func (e Errno) Temporary() bool {
59	return e == EINTR || e == EMFILE || e.Timeout()
60}
61
62func (e Errno) Timeout() bool {
63	return e == EAGAIN || e == EWOULDBLOCK || e == ETIMEDOUT
64}
65
66// A Signal is a number describing a process signal.
67// It implements the os.Signal interface.
68type Signal int
69
70const (
71	_ Signal = iota
72	SIGCHLD
73	SIGINT
74	SIGKILL
75	SIGTRAP
76	SIGQUIT
77	SIGTERM
78)
79
80func (s Signal) Signal() {}
81
82func (s Signal) String() string {
83	if 0 <= s && int(s) < len(signals) {
84		str := signals[s]
85		if str != "" {
86			return str
87		}
88	}
89	return "signal " + itoa(int(s))
90}
91
92var signals = [...]string{}
93
94// File system
95
96const (
97	Stdin  = 0
98	Stdout = 1
99	Stderr = 2
100)
101
102const (
103	O_RDONLY = 0
104	O_WRONLY = 1
105	O_RDWR   = 2
106
107	O_CREAT  = 0100
108	O_CREATE = O_CREAT
109	O_TRUNC  = 01000
110	O_APPEND = 02000
111	O_EXCL   = 0200
112	O_SYNC   = 010000
113
114	O_CLOEXEC = 0
115)
116
117const (
118	F_DUPFD   = 0
119	F_GETFD   = 1
120	F_SETFD   = 2
121	F_GETFL   = 3
122	F_SETFL   = 4
123	F_GETOWN  = 5
124	F_SETOWN  = 6
125	F_GETLK   = 7
126	F_SETLK   = 8
127	F_SETLKW  = 9
128	F_RGETLK  = 10
129	F_RSETLK  = 11
130	F_CNVT    = 12
131	F_RSETLKW = 13
132
133	F_RDLCK   = 1
134	F_WRLCK   = 2
135	F_UNLCK   = 3
136	F_UNLKSYS = 4
137)
138
139const (
140	S_IFMT        = 0000370000
141	S_IFSHM_SYSV  = 0000300000
142	S_IFSEMA      = 0000270000
143	S_IFCOND      = 0000260000
144	S_IFMUTEX     = 0000250000
145	S_IFSHM       = 0000240000
146	S_IFBOUNDSOCK = 0000230000
147	S_IFSOCKADDR  = 0000220000
148	S_IFDSOCK     = 0000210000
149
150	S_IFSOCK = 0000140000
151	S_IFLNK  = 0000120000
152	S_IFREG  = 0000100000
153	S_IFBLK  = 0000060000
154	S_IFDIR  = 0000040000
155	S_IFCHR  = 0000020000
156	S_IFIFO  = 0000010000
157
158	S_UNSUP = 0000370000
159
160	S_ISUID = 0004000
161	S_ISGID = 0002000
162	S_ISVTX = 0001000
163
164	S_IREAD  = 0400
165	S_IWRITE = 0200
166	S_IEXEC  = 0100
167
168	S_IRWXU = 0700
169	S_IRUSR = 0400
170	S_IWUSR = 0200
171	S_IXUSR = 0100
172
173	S_IRWXG = 070
174	S_IRGRP = 040
175	S_IWGRP = 020
176	S_IXGRP = 010
177
178	S_IRWXO = 07
179	S_IROTH = 04
180	S_IWOTH = 02
181	S_IXOTH = 01
182)
183
184type Stat_t struct {
185	Dev       int64
186	Ino       uint64
187	Mode      uint32
188	Nlink     uint32
189	Uid       uint32
190	Gid       uint32
191	Rdev      int64
192	Size      int64
193	Blksize   int32
194	Blocks    int32
195	Atime     int64
196	AtimeNsec int64
197	Mtime     int64
198	MtimeNsec int64
199	Ctime     int64
200	CtimeNsec int64
201}
202
203// Processes
204// Not supported - just enough for package os.
205
206var ForkLock sync.RWMutex
207
208type WaitStatus uint32
209
210func (w WaitStatus) Exited() bool       { return false }
211func (w WaitStatus) ExitStatus() int    { return 0 }
212func (w WaitStatus) Signaled() bool     { return false }
213func (w WaitStatus) Signal() Signal     { return 0 }
214func (w WaitStatus) CoreDump() bool     { return false }
215func (w WaitStatus) Stopped() bool      { return false }
216func (w WaitStatus) Continued() bool    { return false }
217func (w WaitStatus) StopSignal() Signal { return 0 }
218func (w WaitStatus) TrapCause() int     { return 0 }
219
220// XXX made up
221type Rusage struct {
222	Utime Timeval
223	Stime Timeval
224}
225
226// XXX made up
227type ProcAttr struct {
228	Dir   string
229	Env   []string
230	Files []uintptr
231	Sys   *SysProcAttr
232}
233
234type SysProcAttr struct {
235}
236
237func Syscall(trap, a1, a2, a3 uintptr) (r1, r2 uintptr, err Errno) {
238	return 0, 0, ENOSYS
239}
240
241func Syscall6(trap, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2 uintptr, err Errno) {
242	return 0, 0, ENOSYS
243}
244
245func RawSyscall(trap, a1, a2, a3 uintptr) (r1, r2 uintptr, err Errno) {
246	return 0, 0, ENOSYS
247}
248
249func RawSyscall6(trap, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2 uintptr, err Errno) {
250	return 0, 0, ENOSYS
251}
252
253func Sysctl(key string) (string, error) {
254	if key == "kern.hostname" {
255		return "js", nil
256	}
257	return "", ENOSYS
258}
259
260const ImplementsGetwd = true
261
262func Getwd() (wd string, err error) {
263	var buf [PathMax]byte
264	n, err := Getcwd(buf[0:])
265	if err != nil {
266		return "", err
267	}
268	return string(buf[:n]), nil
269}
270
271func Getegid() int                      { return 1 }
272func Geteuid() int                      { return 1 }
273func Getgid() int                       { return 1 }
274func Getgroups() ([]int, error)         { return []int{1}, nil }
275func Getppid() int                      { return 2 }
276func Getpid() int                       { return 3 }
277func Gettimeofday(tv *Timeval) error    { return ENOSYS }
278func Getuid() int                       { return 1 }
279func Kill(pid int, signum Signal) error { return ENOSYS }
280func Sendfile(outfd int, infd int, offset *int64, count int) (written int, err error) {
281	return 0, ENOSYS
282}
283func StartProcess(argv0 string, argv []string, attr *ProcAttr) (pid int, handle uintptr, err error) {
284	return 0, 0, ENOSYS
285}
286func Wait4(pid int, wstatus *WaitStatus, options int, rusage *Rusage) (wpid int, err error) {
287	return 0, ENOSYS
288}
289
290type Iovec struct{} // dummy
291
292type Timespec struct {
293	Sec  int64
294	Nsec int64
295}
296
297type Timeval struct {
298	Sec  int64
299	Usec int64
300}
301
302func setTimespec(sec, nsec int64) Timespec {
303	return Timespec{Sec: sec, Nsec: nsec}
304}
305
306func setTimeval(sec, usec int64) Timeval {
307	return Timeval{Sec: sec, Usec: usec}
308}
309