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