1e66f31c5Sopenharmony_ci/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
2e66f31c5Sopenharmony_ci *
3e66f31c5Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a copy
4e66f31c5Sopenharmony_ci * of this software and associated documentation files (the "Software"), to
5e66f31c5Sopenharmony_ci * deal in the Software without restriction, including without limitation the
6e66f31c5Sopenharmony_ci * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7e66f31c5Sopenharmony_ci * sell copies of the Software, and to permit persons to whom the Software is
8e66f31c5Sopenharmony_ci * furnished to do so, subject to the following conditions:
9e66f31c5Sopenharmony_ci *
10e66f31c5Sopenharmony_ci * The above copyright notice and this permission notice shall be included in
11e66f31c5Sopenharmony_ci * all copies or substantial portions of the Software.
12e66f31c5Sopenharmony_ci *
13e66f31c5Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14e66f31c5Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15e66f31c5Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16e66f31c5Sopenharmony_ci * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17e66f31c5Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18e66f31c5Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19e66f31c5Sopenharmony_ci * IN THE SOFTWARE.
20e66f31c5Sopenharmony_ci */
21e66f31c5Sopenharmony_ci
22e66f31c5Sopenharmony_ci#include <assert.h>
23e66f31c5Sopenharmony_ci#include <errno.h>
24e66f31c5Sopenharmony_ci#include <stdio.h>
25e66f31c5Sopenharmony_ci#include <string.h>
26e66f31c5Sopenharmony_ci#include <stdlib.h>
27e66f31c5Sopenharmony_ci
28e66f31c5Sopenharmony_ci#include "uv.h"
29e66f31c5Sopenharmony_ci#include "internal.h"
30e66f31c5Sopenharmony_ci
31e66f31c5Sopenharmony_ci
32e66f31c5Sopenharmony_ci/*
33e66f31c5Sopenharmony_ci * Display an error message and abort the event loop.
34e66f31c5Sopenharmony_ci */
35e66f31c5Sopenharmony_civoid uv_fatal_error(const int errorno, const char* syscall) {
36e66f31c5Sopenharmony_ci  char* buf = NULL;
37e66f31c5Sopenharmony_ci  const char* errmsg;
38e66f31c5Sopenharmony_ci
39e66f31c5Sopenharmony_ci  FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
40e66f31c5Sopenharmony_ci      FORMAT_MESSAGE_IGNORE_INSERTS, NULL, errorno,
41e66f31c5Sopenharmony_ci      MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&buf, 0, NULL);
42e66f31c5Sopenharmony_ci
43e66f31c5Sopenharmony_ci  if (buf) {
44e66f31c5Sopenharmony_ci    errmsg = buf;
45e66f31c5Sopenharmony_ci  } else {
46e66f31c5Sopenharmony_ci    errmsg = "Unknown error";
47e66f31c5Sopenharmony_ci  }
48e66f31c5Sopenharmony_ci
49e66f31c5Sopenharmony_ci  /* FormatMessage messages include a newline character already, so don't add
50e66f31c5Sopenharmony_ci   * another. */
51e66f31c5Sopenharmony_ci  if (syscall) {
52e66f31c5Sopenharmony_ci    fprintf(stderr, "%s: (%d) %s", syscall, errorno, errmsg);
53e66f31c5Sopenharmony_ci  } else {
54e66f31c5Sopenharmony_ci    fprintf(stderr, "(%d) %s", errorno, errmsg);
55e66f31c5Sopenharmony_ci  }
56e66f31c5Sopenharmony_ci
57e66f31c5Sopenharmony_ci  if (buf) {
58e66f31c5Sopenharmony_ci    LocalFree(buf);
59e66f31c5Sopenharmony_ci  }
60e66f31c5Sopenharmony_ci
61e66f31c5Sopenharmony_ci  DebugBreak();
62e66f31c5Sopenharmony_ci  abort();
63e66f31c5Sopenharmony_ci}
64e66f31c5Sopenharmony_ci
65e66f31c5Sopenharmony_ci
66e66f31c5Sopenharmony_ciint uv_translate_sys_error(int sys_errno) {
67e66f31c5Sopenharmony_ci  if (sys_errno <= 0) {
68e66f31c5Sopenharmony_ci    return sys_errno;  /* If < 0 then it's already a libuv error. */
69e66f31c5Sopenharmony_ci  }
70e66f31c5Sopenharmony_ci
71e66f31c5Sopenharmony_ci  switch (sys_errno) {
72e66f31c5Sopenharmony_ci    case ERROR_NOACCESS:                    return UV_EACCES;
73e66f31c5Sopenharmony_ci    case WSAEACCES:                         return UV_EACCES;
74e66f31c5Sopenharmony_ci    case ERROR_ELEVATION_REQUIRED:          return UV_EACCES;
75e66f31c5Sopenharmony_ci    case ERROR_CANT_ACCESS_FILE:            return UV_EACCES;
76e66f31c5Sopenharmony_ci    case ERROR_ADDRESS_ALREADY_ASSOCIATED:  return UV_EADDRINUSE;
77e66f31c5Sopenharmony_ci    case WSAEADDRINUSE:                     return UV_EADDRINUSE;
78e66f31c5Sopenharmony_ci    case WSAEADDRNOTAVAIL:                  return UV_EADDRNOTAVAIL;
79e66f31c5Sopenharmony_ci    case WSAEAFNOSUPPORT:                   return UV_EAFNOSUPPORT;
80e66f31c5Sopenharmony_ci    case WSAEWOULDBLOCK:                    return UV_EAGAIN;
81e66f31c5Sopenharmony_ci    case WSAEALREADY:                       return UV_EALREADY;
82e66f31c5Sopenharmony_ci    case ERROR_INVALID_FLAGS:               return UV_EBADF;
83e66f31c5Sopenharmony_ci    case ERROR_INVALID_HANDLE:              return UV_EBADF;
84e66f31c5Sopenharmony_ci    case ERROR_LOCK_VIOLATION:              return UV_EBUSY;
85e66f31c5Sopenharmony_ci    case ERROR_PIPE_BUSY:                   return UV_EBUSY;
86e66f31c5Sopenharmony_ci    case ERROR_SHARING_VIOLATION:           return UV_EBUSY;
87e66f31c5Sopenharmony_ci    case ERROR_OPERATION_ABORTED:           return UV_ECANCELED;
88e66f31c5Sopenharmony_ci    case WSAEINTR:                          return UV_ECANCELED;
89e66f31c5Sopenharmony_ci    case ERROR_NO_UNICODE_TRANSLATION:      return UV_ECHARSET;
90e66f31c5Sopenharmony_ci    case ERROR_CONNECTION_ABORTED:          return UV_ECONNABORTED;
91e66f31c5Sopenharmony_ci    case WSAECONNABORTED:                   return UV_ECONNABORTED;
92e66f31c5Sopenharmony_ci    case ERROR_CONNECTION_REFUSED:          return UV_ECONNREFUSED;
93e66f31c5Sopenharmony_ci    case WSAECONNREFUSED:                   return UV_ECONNREFUSED;
94e66f31c5Sopenharmony_ci    case ERROR_NETNAME_DELETED:             return UV_ECONNRESET;
95e66f31c5Sopenharmony_ci    case WSAECONNRESET:                     return UV_ECONNRESET;
96e66f31c5Sopenharmony_ci    case ERROR_ALREADY_EXISTS:              return UV_EEXIST;
97e66f31c5Sopenharmony_ci    case ERROR_FILE_EXISTS:                 return UV_EEXIST;
98e66f31c5Sopenharmony_ci    case ERROR_BUFFER_OVERFLOW:             return UV_EFAULT;
99e66f31c5Sopenharmony_ci    case WSAEFAULT:                         return UV_EFAULT;
100e66f31c5Sopenharmony_ci    case ERROR_HOST_UNREACHABLE:            return UV_EHOSTUNREACH;
101e66f31c5Sopenharmony_ci    case WSAEHOSTUNREACH:                   return UV_EHOSTUNREACH;
102e66f31c5Sopenharmony_ci    case ERROR_INSUFFICIENT_BUFFER:         return UV_EINVAL;
103e66f31c5Sopenharmony_ci    case ERROR_INVALID_DATA:                return UV_EINVAL;
104e66f31c5Sopenharmony_ci    case ERROR_INVALID_PARAMETER:           return UV_EINVAL;
105e66f31c5Sopenharmony_ci    case ERROR_SYMLINK_NOT_SUPPORTED:       return UV_EINVAL;
106e66f31c5Sopenharmony_ci    case WSAEINVAL:                         return UV_EINVAL;
107e66f31c5Sopenharmony_ci    case WSAEPFNOSUPPORT:                   return UV_EINVAL;
108e66f31c5Sopenharmony_ci    case ERROR_BEGINNING_OF_MEDIA:          return UV_EIO;
109e66f31c5Sopenharmony_ci    case ERROR_BUS_RESET:                   return UV_EIO;
110e66f31c5Sopenharmony_ci    case ERROR_CRC:                         return UV_EIO;
111e66f31c5Sopenharmony_ci    case ERROR_DEVICE_DOOR_OPEN:            return UV_EIO;
112e66f31c5Sopenharmony_ci    case ERROR_DEVICE_REQUIRES_CLEANING:    return UV_EIO;
113e66f31c5Sopenharmony_ci    case ERROR_DISK_CORRUPT:                return UV_EIO;
114e66f31c5Sopenharmony_ci    case ERROR_EOM_OVERFLOW:                return UV_EIO;
115e66f31c5Sopenharmony_ci    case ERROR_FILEMARK_DETECTED:           return UV_EIO;
116e66f31c5Sopenharmony_ci    case ERROR_GEN_FAILURE:                 return UV_EIO;
117e66f31c5Sopenharmony_ci    case ERROR_INVALID_BLOCK_LENGTH:        return UV_EIO;
118e66f31c5Sopenharmony_ci    case ERROR_IO_DEVICE:                   return UV_EIO;
119e66f31c5Sopenharmony_ci    case ERROR_NO_DATA_DETECTED:            return UV_EIO;
120e66f31c5Sopenharmony_ci    case ERROR_NO_SIGNAL_SENT:              return UV_EIO;
121e66f31c5Sopenharmony_ci    case ERROR_OPEN_FAILED:                 return UV_EIO;
122e66f31c5Sopenharmony_ci    case ERROR_SETMARK_DETECTED:            return UV_EIO;
123e66f31c5Sopenharmony_ci    case ERROR_SIGNAL_REFUSED:              return UV_EIO;
124e66f31c5Sopenharmony_ci    case WSAEISCONN:                        return UV_EISCONN;
125e66f31c5Sopenharmony_ci    case ERROR_CANT_RESOLVE_FILENAME:       return UV_ELOOP;
126e66f31c5Sopenharmony_ci    case ERROR_TOO_MANY_OPEN_FILES:         return UV_EMFILE;
127e66f31c5Sopenharmony_ci    case WSAEMFILE:                         return UV_EMFILE;
128e66f31c5Sopenharmony_ci    case WSAEMSGSIZE:                       return UV_EMSGSIZE;
129e66f31c5Sopenharmony_ci    case ERROR_FILENAME_EXCED_RANGE:        return UV_ENAMETOOLONG;
130e66f31c5Sopenharmony_ci    case ERROR_NETWORK_UNREACHABLE:         return UV_ENETUNREACH;
131e66f31c5Sopenharmony_ci    case WSAENETUNREACH:                    return UV_ENETUNREACH;
132e66f31c5Sopenharmony_ci    case WSAENOBUFS:                        return UV_ENOBUFS;
133e66f31c5Sopenharmony_ci    case ERROR_BAD_PATHNAME:                return UV_ENOENT;
134e66f31c5Sopenharmony_ci    case ERROR_DIRECTORY:                   return UV_ENOENT;
135e66f31c5Sopenharmony_ci    case ERROR_ENVVAR_NOT_FOUND:            return UV_ENOENT;
136e66f31c5Sopenharmony_ci    case ERROR_FILE_NOT_FOUND:              return UV_ENOENT;
137e66f31c5Sopenharmony_ci    case ERROR_INVALID_NAME:                return UV_ENOENT;
138e66f31c5Sopenharmony_ci    case ERROR_INVALID_DRIVE:               return UV_ENOENT;
139e66f31c5Sopenharmony_ci    case ERROR_INVALID_REPARSE_DATA:        return UV_ENOENT;
140e66f31c5Sopenharmony_ci    case ERROR_MOD_NOT_FOUND:               return UV_ENOENT;
141e66f31c5Sopenharmony_ci    case ERROR_PATH_NOT_FOUND:              return UV_ENOENT;
142e66f31c5Sopenharmony_ci    case WSAHOST_NOT_FOUND:                 return UV_ENOENT;
143e66f31c5Sopenharmony_ci    case WSANO_DATA:                        return UV_ENOENT;
144e66f31c5Sopenharmony_ci    case ERROR_NOT_ENOUGH_MEMORY:           return UV_ENOMEM;
145e66f31c5Sopenharmony_ci    case ERROR_OUTOFMEMORY:                 return UV_ENOMEM;
146e66f31c5Sopenharmony_ci    case ERROR_CANNOT_MAKE:                 return UV_ENOSPC;
147e66f31c5Sopenharmony_ci    case ERROR_DISK_FULL:                   return UV_ENOSPC;
148e66f31c5Sopenharmony_ci    case ERROR_EA_TABLE_FULL:               return UV_ENOSPC;
149e66f31c5Sopenharmony_ci    case ERROR_END_OF_MEDIA:                return UV_ENOSPC;
150e66f31c5Sopenharmony_ci    case ERROR_HANDLE_DISK_FULL:            return UV_ENOSPC;
151e66f31c5Sopenharmony_ci    case ERROR_NOT_CONNECTED:               return UV_ENOTCONN;
152e66f31c5Sopenharmony_ci    case WSAENOTCONN:                       return UV_ENOTCONN;
153e66f31c5Sopenharmony_ci    case ERROR_DIR_NOT_EMPTY:               return UV_ENOTEMPTY;
154e66f31c5Sopenharmony_ci    case WSAENOTSOCK:                       return UV_ENOTSOCK;
155e66f31c5Sopenharmony_ci    case ERROR_NOT_SUPPORTED:               return UV_ENOTSUP;
156e66f31c5Sopenharmony_ci    case ERROR_BROKEN_PIPE:                 return UV_EOF;
157e66f31c5Sopenharmony_ci    case ERROR_ACCESS_DENIED:               return UV_EPERM;
158e66f31c5Sopenharmony_ci    case ERROR_PRIVILEGE_NOT_HELD:          return UV_EPERM;
159e66f31c5Sopenharmony_ci    case ERROR_BAD_PIPE:                    return UV_EPIPE;
160e66f31c5Sopenharmony_ci    case ERROR_NO_DATA:                     return UV_EPIPE;
161e66f31c5Sopenharmony_ci    case ERROR_PIPE_NOT_CONNECTED:          return UV_EPIPE;
162e66f31c5Sopenharmony_ci    case WSAESHUTDOWN:                      return UV_EPIPE;
163e66f31c5Sopenharmony_ci    case WSAEPROTONOSUPPORT:                return UV_EPROTONOSUPPORT;
164e66f31c5Sopenharmony_ci    case ERROR_WRITE_PROTECT:               return UV_EROFS;
165e66f31c5Sopenharmony_ci    case ERROR_SEM_TIMEOUT:                 return UV_ETIMEDOUT;
166e66f31c5Sopenharmony_ci    case WSAETIMEDOUT:                      return UV_ETIMEDOUT;
167e66f31c5Sopenharmony_ci    case ERROR_NOT_SAME_DEVICE:             return UV_EXDEV;
168e66f31c5Sopenharmony_ci    case ERROR_INVALID_FUNCTION:            return UV_EISDIR;
169e66f31c5Sopenharmony_ci    case ERROR_META_EXPANSION_TOO_LONG:     return UV_E2BIG;
170e66f31c5Sopenharmony_ci    case WSAESOCKTNOSUPPORT:                return UV_ESOCKTNOSUPPORT;
171e66f31c5Sopenharmony_ci    default:                                return UV_UNKNOWN;
172e66f31c5Sopenharmony_ci  }
173e66f31c5Sopenharmony_ci}
174