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