github.com/tetratelabs/wazero@v1.2.1/internal/wasip1/errno.go (about)

     1  package wasip1
     2  
     3  import (
     4  	"fmt"
     5  	"syscall"
     6  )
     7  
     8  // Errno is neither uint16 nor an alias for parity with wasm.ValueType.
     9  type Errno = uint32
    10  
    11  // ErrnoName returns the POSIX error code name, except ErrnoSuccess, which is
    12  // not an error. e.g. Errno2big -> "E2BIG"
    13  func ErrnoName(errno uint32) string {
    14  	if int(errno) < len(errnoToString) {
    15  		return errnoToString[errno]
    16  	}
    17  	return fmt.Sprintf("errno(%d)", errno)
    18  }
    19  
    20  // Note: Below prefers POSIX symbol names over WASI ones, even if the docs are from WASI.
    21  // See https://linux.die.net/man/3/errno
    22  // See https://github.com/WebAssembly/WASI/blob/snapshot-01/phases/snapshot/docs.md#variants-1
    23  const (
    24  	// ErrnoSuccess No error occurred. System call completed successfully.
    25  	ErrnoSuccess Errno = iota
    26  	// Errno2big Argument list too long.
    27  	Errno2big
    28  	// ErrnoAcces Permission denied.
    29  	ErrnoAcces
    30  	// ErrnoAddrinuse Address in use.
    31  	ErrnoAddrinuse
    32  	// ErrnoAddrnotavail Address not available.
    33  	ErrnoAddrnotavail
    34  	// ErrnoAfnosupport Address family not supported.
    35  	ErrnoAfnosupport
    36  	// ErrnoAgain Resource unavailable, or operation would block.
    37  	ErrnoAgain
    38  	// ErrnoAlready Connection already in progress.
    39  	ErrnoAlready
    40  	// ErrnoBadf Bad file descriptor.
    41  	ErrnoBadf
    42  	// ErrnoBadmsg Bad message.
    43  	ErrnoBadmsg
    44  	// ErrnoBusy Device or resource busy.
    45  	ErrnoBusy
    46  	// ErrnoCanceled Operation canceled.
    47  	ErrnoCanceled
    48  	// ErrnoChild No child processes.
    49  	ErrnoChild
    50  	// ErrnoConnaborted Connection aborted.
    51  	ErrnoConnaborted
    52  	// ErrnoConnrefused Connection refused.
    53  	ErrnoConnrefused
    54  	// ErrnoConnreset Connection reset.
    55  	ErrnoConnreset
    56  	// ErrnoDeadlk Resource deadlock would occur.
    57  	ErrnoDeadlk
    58  	// ErrnoDestaddrreq Destination address required.
    59  	ErrnoDestaddrreq
    60  	// ErrnoDom Mathematics argument out of domain of function.
    61  	ErrnoDom
    62  	// ErrnoDquot Reserved.
    63  	ErrnoDquot
    64  	// ErrnoExist File exists.
    65  	ErrnoExist
    66  	// ErrnoFault Bad address.
    67  	ErrnoFault
    68  	// ErrnoFbig File too large.
    69  	ErrnoFbig
    70  	// ErrnoHostunreach Host is unreachable.
    71  	ErrnoHostunreach
    72  	// ErrnoIdrm Identifier removed.
    73  	ErrnoIdrm
    74  	// ErrnoIlseq Illegal byte sequence.
    75  	ErrnoIlseq
    76  	// ErrnoInprogress Operation in progress.
    77  	ErrnoInprogress
    78  	// ErrnoIntr Interrupted function.
    79  	ErrnoIntr
    80  	// ErrnoInval Invalid argument.
    81  	ErrnoInval
    82  	// ErrnoIo I/O error.
    83  	ErrnoIo
    84  	// ErrnoIsconn Socket is connected.
    85  	ErrnoIsconn
    86  	// ErrnoIsdir Is a directory.
    87  	ErrnoIsdir
    88  	// ErrnoLoop Too many levels of symbolic links.
    89  	ErrnoLoop
    90  	// ErrnoMfile File descriptor value too large.
    91  	ErrnoMfile
    92  	// ErrnoMlink Too many links.
    93  	ErrnoMlink
    94  	// ErrnoMsgsize Message too large.
    95  	ErrnoMsgsize
    96  	// ErrnoMultihop Reserved.
    97  	ErrnoMultihop
    98  	// ErrnoNametoolong Filename too long.
    99  	ErrnoNametoolong
   100  	// ErrnoNetdown Network is down.
   101  	ErrnoNetdown
   102  	// ErrnoNetreset Connection aborted by network.
   103  	ErrnoNetreset
   104  	// ErrnoNetunreach Network unreachable.
   105  	ErrnoNetunreach
   106  	// ErrnoNfile Too many files open in system.
   107  	ErrnoNfile
   108  	// ErrnoNobufs No buffer space available.
   109  	ErrnoNobufs
   110  	// ErrnoNodev No such device.
   111  	ErrnoNodev
   112  	// ErrnoNoent No such file or directory.
   113  	ErrnoNoent
   114  	// ErrnoNoexec Executable file format error.
   115  	ErrnoNoexec
   116  	// ErrnoNolck No locks available.
   117  	ErrnoNolck
   118  	// ErrnoNolink Reserved.
   119  	ErrnoNolink
   120  	// ErrnoNomem Not enough space.
   121  	ErrnoNomem
   122  	// ErrnoNomsg No message of the desired type.
   123  	ErrnoNomsg
   124  	// ErrnoNoprotoopt No message of the desired type.
   125  	ErrnoNoprotoopt
   126  	// ErrnoNospc No space left on device.
   127  	ErrnoNospc
   128  	// ErrnoNosys function not supported.
   129  	ErrnoNosys
   130  	// ErrnoNotconn The socket is not connected.
   131  	ErrnoNotconn
   132  	// ErrnoNotdir Not a directory or a symbolic link to a directory.
   133  	ErrnoNotdir
   134  	// ErrnoNotempty Directory not empty.
   135  	ErrnoNotempty
   136  	// ErrnoNotrecoverable State not recoverable.
   137  	ErrnoNotrecoverable
   138  	// ErrnoNotsock Not a socket.
   139  	ErrnoNotsock
   140  	// ErrnoNotsup Not supported, or operation not supported on socket.
   141  	ErrnoNotsup
   142  	// ErrnoNotty Inappropriate I/O control operation.
   143  	ErrnoNotty
   144  	// ErrnoNxio No such device or address.
   145  	ErrnoNxio
   146  	// ErrnoOverflow Value too large to be stored in data type.
   147  	ErrnoOverflow
   148  	// ErrnoOwnerdead Previous owner died.
   149  	ErrnoOwnerdead
   150  	// ErrnoPerm Operation not permitted.
   151  	ErrnoPerm
   152  	// ErrnoPipe Broken pipe.
   153  	ErrnoPipe
   154  	// ErrnoProto Protocol error.
   155  	ErrnoProto
   156  	// ErrnoProtonosupport Protocol error.
   157  	ErrnoProtonosupport
   158  	// ErrnoPrototype Protocol wrong type for socket.
   159  	ErrnoPrototype
   160  	// ErrnoRange Result too large.
   161  	ErrnoRange
   162  	// ErrnoRofs Read-only file system.
   163  	ErrnoRofs
   164  	// ErrnoSpipe Invalid seek.
   165  	ErrnoSpipe
   166  	// ErrnoSrch No such process.
   167  	ErrnoSrch
   168  	// ErrnoStale Reserved.
   169  	ErrnoStale
   170  	// ErrnoTimedout Connection timed out.
   171  	ErrnoTimedout
   172  	// ErrnoTxtbsy Text file busy.
   173  	ErrnoTxtbsy
   174  	// ErrnoXdev Cross-device link.
   175  	ErrnoXdev
   176  
   177  	// Note: ErrnoNotcapable was removed by WASI maintainers.
   178  	// See https://github.com/WebAssembly/wasi-libc/pull/294
   179  )
   180  
   181  var errnoToString = [...]string{
   182  	"ESUCCESS",
   183  	"E2BIG",
   184  	"EACCES",
   185  	"EADDRINUSE",
   186  	"EADDRNOTAVAIL",
   187  	"EAFNOSUPPORT",
   188  	"EAGAIN",
   189  	"EALREADY",
   190  	"EBADF",
   191  	"EBADMSG",
   192  	"EBUSY",
   193  	"ECANCELED",
   194  	"ECHILD",
   195  	"ECONNABORTED",
   196  	"ECONNREFUSED",
   197  	"ECONNRESET",
   198  	"EDEADLK",
   199  	"EDESTADDRREQ",
   200  	"EDOM",
   201  	"EDQUOT",
   202  	"EEXIST",
   203  	"EFAULT",
   204  	"EFBIG",
   205  	"EHOSTUNREACH",
   206  	"EIDRM",
   207  	"EILSEQ",
   208  	"EINPROGRESS",
   209  	"EINTR",
   210  	"EINVAL",
   211  	"EIO",
   212  	"EISCONN",
   213  	"EISDIR",
   214  	"ELOOP",
   215  	"EMFILE",
   216  	"EMLINK",
   217  	"EMSGSIZE",
   218  	"EMULTIHOP",
   219  	"ENAMETOOLONG",
   220  	"ENETDOWN",
   221  	"ENETRESET",
   222  	"ENETUNREACH",
   223  	"ENFILE",
   224  	"ENOBUFS",
   225  	"ENODEV",
   226  	"ENOENT",
   227  	"ENOEXEC",
   228  	"ENOLCK",
   229  	"ENOLINK",
   230  	"ENOMEM",
   231  	"ENOMSG",
   232  	"ENOPROTOOPT",
   233  	"ENOSPC",
   234  	"ENOSYS",
   235  	"ENOTCONN",
   236  	"ENOTDIR",
   237  	"ENOTEMPTY",
   238  	"ENOTRECOVERABLE",
   239  	"ENOTSOCK",
   240  	"ENOTSUP",
   241  	"ENOTTY",
   242  	"ENXIO",
   243  	"EOVERFLOW",
   244  	"EOWNERDEAD",
   245  	"EPERM",
   246  	"EPIPE",
   247  	"EPROTO",
   248  	"EPROTONOSUPPORT",
   249  	"EPROTOTYPE",
   250  	"ERANGE",
   251  	"EROFS",
   252  	"ESPIPE",
   253  	"ESRCH",
   254  	"ESTALE",
   255  	"ETIMEDOUT",
   256  	"ETXTBSY",
   257  	"EXDEV",
   258  	"ENOTCAPABLE",
   259  }
   260  
   261  // ToErrno coerces the error to a WASI Errno.
   262  //
   263  // Note: Coercion isn't centralized in sys.FSContext because ABI use different
   264  // error codes. For example, wasi-filesystem and GOOS=js don't map to these
   265  // Errno.
   266  func ToErrno(errno syscall.Errno) Errno {
   267  	switch errno {
   268  	case 0:
   269  		return ErrnoSuccess
   270  	case syscall.EACCES:
   271  		return ErrnoAcces
   272  	case syscall.EAGAIN:
   273  		return ErrnoAgain
   274  	case syscall.EBADF:
   275  		return ErrnoBadf
   276  	case syscall.EEXIST:
   277  		return ErrnoExist
   278  	case syscall.EFAULT:
   279  		return ErrnoFault
   280  	case syscall.EINTR:
   281  		return ErrnoIntr
   282  	case syscall.EINVAL:
   283  		return ErrnoInval
   284  	case syscall.EIO:
   285  		return ErrnoIo
   286  	case syscall.EISDIR:
   287  		return ErrnoIsdir
   288  	case syscall.ELOOP:
   289  		return ErrnoLoop
   290  	case syscall.ENAMETOOLONG:
   291  		return ErrnoNametoolong
   292  	case syscall.ENOENT:
   293  		return ErrnoNoent
   294  	case syscall.ENOSYS:
   295  		return ErrnoNosys
   296  	case syscall.ENOTDIR:
   297  		return ErrnoNotdir
   298  	case syscall.ENOTEMPTY:
   299  		return ErrnoNotempty
   300  	case syscall.ENOTSOCK:
   301  		return ErrnoNotsock
   302  	case syscall.ENOTSUP:
   303  		return ErrnoNotsup
   304  	case syscall.EPERM:
   305  		return ErrnoPerm
   306  	case syscall.EROFS:
   307  		return ErrnoRofs
   308  	default:
   309  		return ErrnoIo
   310  	}
   311  }