syscall_nacl.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. // Copyright 2013 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. package unix
  5. import (
  6. "sync"
  7. "unsafe"
  8. )
  9. //sys naclClose(fd int) (err error) = sys_close
  10. //sys Exit(code int) (err error)
  11. //sys naclFstat(fd int, stat *Stat_t) (err error) = sys_fstat
  12. //sys naclRead(fd int, b []byte) (n int, err error) = sys_read
  13. //sys naclSeek(fd int, off *int64, whence int) (err error) = sys_lseek
  14. const direntSize = 8 + 8 + 2 + 256
  15. // native_client/src/trusted/service_runtime/include/sys/dirent.h
  16. type Dirent struct {
  17. Ino int64
  18. Off int64
  19. Reclen uint16
  20. Name [256]byte
  21. }
  22. func ParseDirent(buf []byte, max int, names []string) (consumed int, count int, newnames []string) {
  23. origlen := len(buf)
  24. count = 0
  25. for max != 0 && len(buf) > 0 {
  26. dirent := (*Dirent)(unsafe.Pointer(&buf[0]))
  27. buf = buf[dirent.Reclen:]
  28. if dirent.Ino == 0 { // File absent in directory.
  29. continue
  30. }
  31. bytes := (*[512 + PathMax]byte)(unsafe.Pointer(&dirent.Name[0]))
  32. var name = string(bytes[0:clen(bytes[:])])
  33. if name == "." || name == ".." { // Useless names
  34. continue
  35. }
  36. max--
  37. count++
  38. names = append(names, name)
  39. }
  40. return origlen - len(buf), count, names
  41. }
  42. func clen(n []byte) int {
  43. for i := 0; i < len(n); i++ {
  44. if n[i] == 0 {
  45. return i
  46. }
  47. }
  48. return len(n)
  49. }
  50. const PathMax = 256
  51. // An Errno is an unsigned number describing an error condition.
  52. // It implements the error interface. The zero Errno is by convention
  53. // a non-error, so code to convert from Errno to error should use:
  54. // err = nil
  55. // if errno != 0 {
  56. // err = errno
  57. // }
  58. type Errno uintptr
  59. func (e Errno) Error() string {
  60. if 0 <= int(e) && int(e) < len(errorstr) {
  61. s := errorstr[e]
  62. if s != "" {
  63. return s
  64. }
  65. }
  66. return "errno " + itoa(int(e))
  67. }
  68. func (e Errno) Temporary() bool {
  69. return e == EINTR || e == EMFILE || e.Timeout()
  70. }
  71. func (e Errno) Timeout() bool {
  72. return e == EAGAIN || e == EWOULDBLOCK || e == ETIMEDOUT
  73. }
  74. // A Signal is a number describing a process signal.
  75. // It implements the os.Signal interface.
  76. type Signal int
  77. const (
  78. _ Signal = iota
  79. SIGCHLD
  80. SIGINT
  81. SIGKILL
  82. SIGTRAP
  83. SIGQUIT
  84. )
  85. func (s Signal) Signal() {}
  86. func (s Signal) String() string {
  87. if 0 <= s && int(s) < len(signals) {
  88. str := signals[s]
  89. if str != "" {
  90. return str
  91. }
  92. }
  93. return "signal " + itoa(int(s))
  94. }
  95. var signals = [...]string{}
  96. // File system
  97. const (
  98. Stdin = 0
  99. Stdout = 1
  100. Stderr = 2
  101. )
  102. // native_client/src/trusted/service_runtime/include/sys/fcntl.h
  103. const (
  104. O_RDONLY = 0
  105. O_WRONLY = 1
  106. O_RDWR = 2
  107. O_ACCMODE = 3
  108. O_CREAT = 0100
  109. O_CREATE = O_CREAT // for ken
  110. O_TRUNC = 01000
  111. O_APPEND = 02000
  112. O_EXCL = 0200
  113. O_NONBLOCK = 04000
  114. O_NDELAY = O_NONBLOCK
  115. O_SYNC = 010000
  116. O_FSYNC = O_SYNC
  117. O_ASYNC = 020000
  118. O_CLOEXEC = 0
  119. FD_CLOEXEC = 1
  120. )
  121. // native_client/src/trusted/service_runtime/include/sys/fcntl.h
  122. const (
  123. F_DUPFD = 0
  124. F_GETFD = 1
  125. F_SETFD = 2
  126. F_GETFL = 3
  127. F_SETFL = 4
  128. F_GETOWN = 5
  129. F_SETOWN = 6
  130. F_GETLK = 7
  131. F_SETLK = 8
  132. F_SETLKW = 9
  133. F_RGETLK = 10
  134. F_RSETLK = 11
  135. F_CNVT = 12
  136. F_RSETLKW = 13
  137. F_RDLCK = 1
  138. F_WRLCK = 2
  139. F_UNLCK = 3
  140. F_UNLKSYS = 4
  141. )
  142. // native_client/src/trusted/service_runtime/include/bits/stat.h
  143. const (
  144. S_IFMT = 0000370000
  145. S_IFSHM_SYSV = 0000300000
  146. S_IFSEMA = 0000270000
  147. S_IFCOND = 0000260000
  148. S_IFMUTEX = 0000250000
  149. S_IFSHM = 0000240000
  150. S_IFBOUNDSOCK = 0000230000
  151. S_IFSOCKADDR = 0000220000
  152. S_IFDSOCK = 0000210000
  153. S_IFSOCK = 0000140000
  154. S_IFLNK = 0000120000
  155. S_IFREG = 0000100000
  156. S_IFBLK = 0000060000
  157. S_IFDIR = 0000040000
  158. S_IFCHR = 0000020000
  159. S_IFIFO = 0000010000
  160. S_UNSUP = 0000370000
  161. S_ISUID = 0004000
  162. S_ISGID = 0002000
  163. S_ISVTX = 0001000
  164. S_IREAD = 0400
  165. S_IWRITE = 0200
  166. S_IEXEC = 0100
  167. S_IRWXU = 0700
  168. S_IRUSR = 0400
  169. S_IWUSR = 0200
  170. S_IXUSR = 0100
  171. S_IRWXG = 070
  172. S_IRGRP = 040
  173. S_IWGRP = 020
  174. S_IXGRP = 010
  175. S_IRWXO = 07
  176. S_IROTH = 04
  177. S_IWOTH = 02
  178. S_IXOTH = 01
  179. )
  180. // native_client/src/trusted/service_runtime/include/sys/stat.h
  181. // native_client/src/trusted/service_runtime/include/machine/_types.h
  182. type Stat_t struct {
  183. Dev int64
  184. Ino uint64
  185. Mode uint32
  186. Nlink uint32
  187. Uid uint32
  188. Gid uint32
  189. Rdev int64
  190. Size int64
  191. Blksize int32
  192. Blocks int32
  193. Atime int64
  194. AtimeNsec int64
  195. Mtime int64
  196. MtimeNsec int64
  197. Ctime int64
  198. CtimeNsec int64
  199. }
  200. // Processes
  201. // Not supported on NaCl - just enough for package os.
  202. var ForkLock sync.RWMutex
  203. type WaitStatus uint32
  204. func (w WaitStatus) Exited() bool { return false }
  205. func (w WaitStatus) ExitStatus() int { return 0 }
  206. func (w WaitStatus) Signaled() bool { return false }
  207. func (w WaitStatus) Signal() Signal { return 0 }
  208. func (w WaitStatus) CoreDump() bool { return false }
  209. func (w WaitStatus) Stopped() bool { return false }
  210. func (w WaitStatus) Continued() bool { return false }
  211. func (w WaitStatus) StopSignal() Signal { return 0 }
  212. func (w WaitStatus) TrapCause() int { return 0 }
  213. // XXX made up
  214. type Rusage struct {
  215. Utime Timeval
  216. Stime Timeval
  217. }
  218. // XXX made up
  219. type ProcAttr struct {
  220. Dir string
  221. Env []string
  222. Files []uintptr
  223. Sys *SysProcAttr
  224. }
  225. type SysProcAttr struct {
  226. }
  227. // System
  228. func Syscall(trap, a1, a2, a3 uintptr) (r1, r2 uintptr, err Errno)
  229. func Syscall6(trap, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2 uintptr, err Errno) { return 0, 0, ENOSYS }
  230. func RawSyscall(trap, a1, a2, a3 uintptr) (r1, r2 uintptr, err Errno) { return 0, 0, ENOSYS }
  231. func RawSyscall6(trap, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2 uintptr, err Errno) {
  232. return 0, 0, ENOSYS
  233. }
  234. func Sysctl(key string) (string, error) {
  235. if key == "kern.hostname" {
  236. return "naclbox", nil
  237. }
  238. return "", ENOSYS
  239. }
  240. // Unimplemented Unix midden heap.
  241. const ImplementsGetwd = false
  242. func Getwd() (wd string, err error) { return "", ENOSYS }
  243. func Getegid() int { return 1 }
  244. func Geteuid() int { return 1 }
  245. func Getgid() int { return 1 }
  246. func Getgroups() ([]int, error) { return []int{1}, nil }
  247. func Getpagesize() int { return 65536 }
  248. func Getppid() int { return 2 }
  249. func Getpid() int { return 3 }
  250. func Getuid() int { return 1 }
  251. func Kill(pid int, signum Signal) error { return ENOSYS }
  252. func Sendfile(outfd int, infd int, offset *int64, count int) (written int, err error) {
  253. return 0, ENOSYS
  254. }
  255. func StartProcess(argv0 string, argv []string, attr *ProcAttr) (pid int, handle uintptr, err error) {
  256. return 0, 0, ENOSYS
  257. }
  258. func Wait4(pid int, wstatus *WaitStatus, options int, rusage *Rusage) (wpid int, err error) {
  259. return 0, ENOSYS
  260. }
  261. func RouteRIB(facility, param int) ([]byte, error) { return nil, ENOSYS }
  262. func ParseRoutingMessage(b []byte) ([]RoutingMessage, error) { return nil, ENOSYS }
  263. func ParseRoutingSockaddr(msg RoutingMessage) ([]Sockaddr, error) { return nil, ENOSYS }
  264. func SysctlUint32(name string) (value uint32, err error) { return 0, ENOSYS }