151 lines
5.1 KiB
C
151 lines
5.1 KiB
C
/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
|
|
*
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
* of this software and associated documentation files (the "Software"), to
|
|
* deal in the Software without restriction, including without limitation the
|
|
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
* sell copies of the Software, and to permit persons to whom the Software is
|
|
* furnished to do so, subject to the following conditions:
|
|
*
|
|
* The above copyright notice and this permission notice shall be included in
|
|
* all copies or substantial portions of the Software.
|
|
*
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
* IN THE SOFTWARE.
|
|
*/
|
|
|
|
#include <assert.h>
|
|
#include <errno.h>
|
|
#include <malloc.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
|
|
#include "uv.h"
|
|
#include "../uv-common.h"
|
|
#include "internal.h"
|
|
|
|
|
|
const uv_err_t uv_ok_ = { UV_OK, ERROR_SUCCESS };
|
|
|
|
|
|
/*
|
|
* Display an error message and abort the event loop.
|
|
*/
|
|
void uv_fatal_error(const int errorno, const char* syscall) {
|
|
char* buf = NULL;
|
|
const char* errmsg;
|
|
|
|
FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
|
|
FORMAT_MESSAGE_IGNORE_INSERTS, NULL, errorno,
|
|
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&buf, 0, NULL);
|
|
|
|
if (buf) {
|
|
errmsg = buf;
|
|
} else {
|
|
errmsg = "Unknown error";
|
|
}
|
|
|
|
/* FormatMessage messages include a newline character already, */
|
|
/* so don't add another. */
|
|
if (syscall) {
|
|
fprintf(stderr, "%s: (%d) %s", syscall, errorno, errmsg);
|
|
} else {
|
|
fprintf(stderr, "(%d) %s", errorno, errmsg);
|
|
}
|
|
|
|
if (buf) {
|
|
LocalFree(buf);
|
|
}
|
|
|
|
*((char*)NULL) = 0xff; /* Force debug break */
|
|
abort();
|
|
}
|
|
|
|
|
|
uv_err_t uv_last_error(uv_loop_t* loop) {
|
|
return loop->last_error;
|
|
}
|
|
|
|
|
|
/* TODO: thread safety */
|
|
static char* last_err_str_ = NULL;
|
|
|
|
char* uv_strerror(uv_err_t err) {
|
|
if (last_err_str_ != NULL) {
|
|
LocalFree(last_err_str_);
|
|
}
|
|
|
|
FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
|
|
FORMAT_MESSAGE_IGNORE_INSERTS, NULL, err.sys_errno_,
|
|
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR) &last_err_str_, 0,
|
|
NULL);
|
|
|
|
if (last_err_str_) {
|
|
return last_err_str_;
|
|
} else {
|
|
return "Unknown error";
|
|
}
|
|
}
|
|
|
|
|
|
uv_err_code uv_translate_sys_error(int sys_errno) {
|
|
switch (sys_errno) {
|
|
case ERROR_SUCCESS: return UV_OK;
|
|
case ERROR_NOACCESS: return UV_EACCESS;
|
|
case WSAEACCES: return UV_EACCESS;
|
|
case ERROR_ADDRESS_ALREADY_ASSOCIATED: return UV_EADDRINUSE;
|
|
case WSAEADDRINUSE: return UV_EADDRINUSE;
|
|
case WSAEADDRNOTAVAIL: return UV_EADDRNOTAVAIL;
|
|
case WSAEAFNOSUPPORT: return UV_EAFNOSUPPORT;
|
|
case WSAEWOULDBLOCK: return UV_EAGAIN;
|
|
case WSAEALREADY: return UV_EALREADY;
|
|
case ERROR_CONNECTION_REFUSED: return UV_ECONNREFUSED;
|
|
case WSAECONNREFUSED: return UV_ECONNREFUSED;
|
|
case WSAEFAULT: return UV_EFAULT;
|
|
case ERROR_HOST_UNREACHABLE: return UV_EHOSTUNREACH;
|
|
case WSAEHOSTUNREACH: return UV_EHOSTUNREACH;
|
|
case ERROR_INVALID_DATA: return UV_EINVAL;
|
|
case WSAEINVAL: return UV_EINVAL;
|
|
case ERROR_TOO_MANY_OPEN_FILES: return UV_EMFILE;
|
|
case WSAEMFILE: return UV_EMFILE;
|
|
case WSAEMSGSIZE: return UV_EMSGSIZE;
|
|
case ERROR_NETWORK_UNREACHABLE: return UV_ENETUNREACH;
|
|
case WSAENETUNREACH: return UV_ENETUNREACH;
|
|
case ERROR_OUTOFMEMORY: return UV_ENOMEM;
|
|
case ERROR_NOT_SUPPORTED: return UV_ENOTSUP;
|
|
case ERROR_INSUFFICIENT_BUFFER: return UV_EINVAL;
|
|
case ERROR_INVALID_FLAGS: return UV_EBADF;
|
|
case ERROR_INVALID_PARAMETER: return UV_EINVAL;
|
|
case ERROR_NO_UNICODE_TRANSLATION: return UV_ECHARSET;
|
|
case ERROR_BROKEN_PIPE: return UV_EOF;
|
|
case ERROR_PIPE_BUSY: return UV_EBUSY;
|
|
case ERROR_SEM_TIMEOUT: return UV_ETIMEDOUT;
|
|
default: return UV_UNKNOWN;
|
|
}
|
|
}
|
|
|
|
|
|
uv_err_t uv_new_sys_error(int sys_errno) {
|
|
uv_err_t e;
|
|
e.code = uv_translate_sys_error(sys_errno);
|
|
e.sys_errno_ = sys_errno;
|
|
return e;
|
|
}
|
|
|
|
|
|
void uv_set_sys_error(uv_loop_t* loop, int sys_errno) {
|
|
loop->last_error.code = uv_translate_sys_error(sys_errno);
|
|
loop->last_error.sys_errno_ = sys_errno;
|
|
}
|
|
|
|
|
|
void uv_set_error(uv_loop_t* loop, uv_err_code code, int sys_errno) {
|
|
loop->last_error.code = code;
|
|
loop->last_error.sys_errno_ = sys_errno;
|
|
}
|