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