syscall_js.go 6.6 KB

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