Brad Fitzpatrick | 5194744 | 2016-03-01 22:57:46 +0000 | [diff] [blame] | 1 | // Copyright 2013 The Go Authors. All rights reserved. |
Dave Cheney | 7c8280c | 2014-02-25 09:47:42 -0500 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | package syscall |
| 6 | |
| 7 | import ( |
| 8 | "sync" |
| 9 | "unsafe" |
| 10 | ) |
| 11 | |
| 12 | //sys naclClose(fd int) (err error) = sys_close |
Dave Cheney | 7c8280c | 2014-02-25 09:47:42 -0500 | [diff] [blame] | 13 | //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 Ma | 003dccf | 2014-12-18 03:26:08 -0500 | [diff] [blame] | 16 | //sys naclGetRandomBytes(b []byte) (err error) = sys_get_random_bytes |
Dave Cheney | 7c8280c | 2014-02-25 09:47:42 -0500 | [diff] [blame] | 17 | |
| 18 | const direntSize = 8 + 8 + 2 + 256 |
| 19 | |
| 20 | // native_client/src/trusted/service_runtime/include/sys/dirent.h |
| 21 | type Dirent struct { |
| 22 | Ino int64 |
| 23 | Off int64 |
| 24 | Reclen uint16 |
| 25 | Name [256]byte |
| 26 | } |
| 27 | |
Damien Neil | f5f7d6e | 2016-06-03 15:04:53 -0700 | [diff] [blame] | 28 | func direntIno(buf []byte) (uint64, bool) { |
| 29 | return readInt(buf, unsafe.Offsetof(Dirent{}.Ino), unsafe.Sizeof(Dirent{}.Ino)) |
Dave Cheney | 7c8280c | 2014-02-25 09:47:42 -0500 | [diff] [blame] | 30 | } |
| 31 | |
Damien Neil | f5f7d6e | 2016-06-03 15:04:53 -0700 | [diff] [blame] | 32 | func direntReclen(buf []byte) (uint64, bool) { |
| 33 | return readInt(buf, unsafe.Offsetof(Dirent{}.Reclen), unsafe.Sizeof(Dirent{}.Reclen)) |
| 34 | } |
| 35 | |
| 36 | func direntNamlen(buf []byte) (uint64, bool) { |
| 37 | reclen, ok := direntReclen(buf) |
| 38 | if !ok { |
| 39 | return 0, false |
Dave Cheney | 7c8280c | 2014-02-25 09:47:42 -0500 | [diff] [blame] | 40 | } |
Damien Neil | f5f7d6e | 2016-06-03 15:04:53 -0700 | [diff] [blame] | 41 | return reclen - uint64(unsafe.Offsetof(Dirent{}.Name)), true |
Dave Cheney | 7c8280c | 2014-02-25 09:47:42 -0500 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | const PathMax = 256 |
| 45 | |
| 46 | // An Errno is an unsigned number describing an error condition. |
Brad Fitzpatrick | 5fea2cc | 2016-03-01 23:21:55 +0000 | [diff] [blame] | 47 | // It implements the error interface. The zero Errno is by convention |
Dave Cheney | 7c8280c | 2014-02-25 09:47:42 -0500 | [diff] [blame] | 48 | // a non-error, so code to convert from Errno to error should use: |
| 49 | // err = nil |
| 50 | // if errno != 0 { |
| 51 | // err = errno |
| 52 | // } |
| 53 | type Errno uintptr |
| 54 | |
| 55 | func (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 | |
| 65 | func (e Errno) Temporary() bool { |
| 66 | return e == EINTR || e == EMFILE || e.Timeout() |
| 67 | } |
| 68 | |
| 69 | func (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. |
| 75 | type Signal int |
| 76 | |
| 77 | const ( |
| 78 | _ Signal = iota |
| 79 | SIGCHLD |
| 80 | SIGINT |
| 81 | SIGKILL |
| 82 | SIGTRAP |
| 83 | SIGQUIT |
| 84 | ) |
| 85 | |
| 86 | func (s Signal) Signal() {} |
| 87 | |
| 88 | func (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 | |
| 98 | var signals = [...]string{} |
| 99 | |
| 100 | // File system |
| 101 | |
| 102 | const ( |
| 103 | Stdin = 0 |
| 104 | Stdout = 1 |
| 105 | Stderr = 2 |
| 106 | ) |
| 107 | |
| 108 | // native_client/src/trusted/service_runtime/include/sys/fcntl.h |
| 109 | const ( |
| 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 |
| 132 | const ( |
| 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 |
| 155 | const ( |
| 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 |
| 202 | type 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 | |
| 224 | var ForkLock sync.RWMutex |
| 225 | |
| 226 | type WaitStatus uint32 |
| 227 | |
| 228 | func (w WaitStatus) Exited() bool { return false } |
| 229 | func (w WaitStatus) ExitStatus() int { return 0 } |
| 230 | func (w WaitStatus) Signaled() bool { return false } |
| 231 | func (w WaitStatus) Signal() Signal { return 0 } |
| 232 | func (w WaitStatus) CoreDump() bool { return false } |
| 233 | func (w WaitStatus) Stopped() bool { return false } |
| 234 | func (w WaitStatus) Continued() bool { return false } |
| 235 | func (w WaitStatus) StopSignal() Signal { return 0 } |
| 236 | func (w WaitStatus) TrapCause() int { return 0 } |
| 237 | |
| 238 | // XXX made up |
| 239 | type Rusage struct { |
| 240 | Utime Timeval |
| 241 | Stime Timeval |
| 242 | } |
| 243 | |
| 244 | // XXX made up |
| 245 | type ProcAttr struct { |
| 246 | Dir string |
| 247 | Env []string |
| 248 | Files []uintptr |
| 249 | Sys *SysProcAttr |
| 250 | } |
| 251 | |
| 252 | type SysProcAttr struct { |
| 253 | } |
| 254 | |
| 255 | // System |
| 256 | |
| 257 | func Syscall(trap, a1, a2, a3 uintptr) (r1, r2 uintptr, err Errno) |
| 258 | func Syscall6(trap, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2 uintptr, err Errno) { return 0, 0, ENOSYS } |
| 259 | func RawSyscall(trap, a1, a2, a3 uintptr) (r1, r2 uintptr, err Errno) { return 0, 0, ENOSYS } |
| 260 | func RawSyscall6(trap, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2 uintptr, err Errno) { |
| 261 | return 0, 0, ENOSYS |
| 262 | } |
| 263 | |
| 264 | func 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 | |
| 273 | const ImplementsGetwd = false |
| 274 | |
| 275 | func Getwd() (wd string, err error) { return "", ENOSYS } |
| 276 | func Getegid() int { return 1 } |
| 277 | func Geteuid() int { return 1 } |
| 278 | func Getgid() int { return 1 } |
| 279 | func Getgroups() ([]int, error) { return []int{1}, nil } |
Dave Cheney | 7c8280c | 2014-02-25 09:47:42 -0500 | [diff] [blame] | 280 | func Getppid() int { return 2 } |
| 281 | func Getpid() int { return 3 } |
Brad Fitzpatrick | 66da885 | 2016-08-10 19:46:48 +0000 | [diff] [blame] | 282 | func Gettimeofday(tv *Timeval) error { return ENOSYS } |
Dave Cheney | 7c8280c | 2014-02-25 09:47:42 -0500 | [diff] [blame] | 283 | func Getuid() int { return 1 } |
| 284 | func Kill(pid int, signum Signal) error { return ENOSYS } |
| 285 | func Sendfile(outfd int, infd int, offset *int64, count int) (written int, err error) { |
| 286 | return 0, ENOSYS |
| 287 | } |
| 288 | func StartProcess(argv0 string, argv []string, attr *ProcAttr) (pid int, handle uintptr, err error) { |
| 289 | return 0, 0, ENOSYS |
| 290 | } |
| 291 | func Wait4(pid int, wstatus *WaitStatus, options int, rusage *Rusage) (wpid int, err error) { |
| 292 | return 0, ENOSYS |
| 293 | } |
| 294 | func RouteRIB(facility, param int) ([]byte, error) { return nil, ENOSYS } |
| 295 | func ParseRoutingMessage(b []byte) ([]RoutingMessage, error) { return nil, ENOSYS } |
| 296 | func ParseRoutingSockaddr(msg RoutingMessage) ([]Sockaddr, error) { return nil, ENOSYS } |
| 297 | func SysctlUint32(name string) (value uint32, err error) { return 0, ENOSYS } |
Brad Fitzpatrick | 8e69d43 | 2016-09-27 20:50:57 +0000 | [diff] [blame] | 298 | |
| 299 | type Iovec struct{} // dummy |