github.com/zach-klippenstein/go@v0.0.0-20150108044943-fcfbeb3adf58/src/syscall/syscall_nacl.go (about)

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