Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

Fixes #76. Unify OS error reporting

As a nice fringe benefit, this also shaves a word
off of a windows TCP handle by replacing "uv_err_t
bind_error" with "int bind_error".
  • Loading branch information...
erickt authored and ry committed Sep 27, 2011
1 parent 1d7e61f commit 23796d208c1309270ee09ff566d00859cdf2e35b
@@ -151,7 +151,7 @@ RB_HEAD(uv_timer_tree_s, uv_timer_s);

#define UV_TCP_PRIVATE_FIELDS \
SOCKET socket; \
uv_err_t bind_error; \
int bind_error; \
union { \
struct { uv_tcp_server_fields }; \
struct { uv_tcp_connection_fields }; \
@@ -147,7 +147,7 @@ int uv_ares_init_options(uv_loop_t* loop, ares_channel *channelptr,

/* only allow single init at a time */
if (loop->channel != NULL) {
uv_err_new_artificial(loop, UV_EALREADY);
uv__set_artificial_error(loop, UV_EALREADY);
return -1;
}

@@ -563,7 +563,7 @@ int uv_timer_stop(uv_timer_t* timer) {

int uv_timer_again(uv_timer_t* timer) {
if (!ev_is_active(&timer->timer_watcher)) {
uv_err_new(timer->loop, EINVAL);
uv__set_sys_error(timer->loop, EINVAL);
return -1;
}

@@ -595,7 +595,7 @@ static int uv_getaddrinfo_done(eio_req* req) {

if (handle->retcode != 0) {
/* TODO how to display gai error strings? */
uv_err_new(handle->loop, handle->retcode);
uv__set_sys_error(handle->loop, handle->retcode);
}

handle->cb(handle, handle->retcode, res);
@@ -626,7 +626,7 @@ int uv_getaddrinfo(uv_loop_t* loop,

if (handle == NULL || cb == NULL ||
(hostname == NULL && service == NULL)) {
uv_err_new_artificial(loop, UV_EINVAL);
uv__set_artificial_error(loop, UV_EINVAL);
return -1;
}

@@ -58,7 +58,7 @@ int uv_fs_event_init(uv_loop_t* loop,
uv_fs_event_t* handle,
const char* filename,
uv_fs_event_cb cb) {
uv_err_new(loop, ENOSYS);
uv__set_sys_error(loop, ENOSYS);
return -1;
}

@@ -73,7 +73,7 @@ int uv_fs_event_init(uv_loop_t* loop,
uv_fs_event_t* handle,
const char* filename,
uv_fs_event_cb cb) {
uv_err_new(loop, ENOSYS);
uv__set_sys_error(loop, ENOSYS);
return -1;
}

@@ -90,21 +90,3 @@ uv_err_code uv_translate_sys_error(int sys_errno) {
assert(0 && "unreachable");
return -1;
}


uv_err_t uv_err_new_artificial(uv_loop_t* loop, int code) {
uv_err_t err;
err.sys_errno_ = 0;
err.code = code;
loop->last_err = err;
return err;
}


uv_err_t uv_err_new(uv_loop_t* loop, int sys_error) {
uv_err_t err;
err.sys_errno_ = sys_error;
err.code = uv_translate_sys_error(sys_error);
loop->last_err = err;
return err;
}
@@ -72,7 +72,7 @@ int uv_fs_event_init(uv_loop_t* loop,
uv_fs_event_t* handle,
const char* filename,
uv_fs_event_cb cb) {
uv_err_new(loop, ENOSYS);
uv__set_sys_error(loop, ENOSYS);
return -1;
}

@@ -46,15 +46,15 @@
/* async */ \
req->eio = eiofunc(args, EIO_PRI_DEFAULT, uv__fs_after, req); \
if (!req->eio) { \
uv_err_new(loop, ENOMEM); \
uv__set_sys_error(loop, ENOMEM); \
return -1; \
} \
uv_ref(loop); \
} else { \
/* sync */ \
req->result = func(args); \
if (req->result) { \
uv_err_new(loop, errno); \
uv__set_sys_error(loop, errno); \
} \
return req->result; \
} \
@@ -192,15 +192,15 @@ int uv_fs_open(uv_loop_t* loop, uv_fs_t* req, const char* path, int flags,
uv_ref(loop);
req->eio = eio_open(path, flags, mode, EIO_PRI_DEFAULT, uv__fs_after, req);
if (!req->eio) {
uv_err_new(loop, ENOMEM);
uv__set_sys_error(loop, ENOMEM);
return -1;
}

} else {
/* sync */
req->result = open(path, flags, mode);
if (req->result < 0) {
uv_err_new(loop, errno);
uv__set_sys_error(loop, errno);
return -1;
}

@@ -224,7 +224,7 @@ int uv_fs_read(uv_loop_t* loop, uv_fs_t* req, uv_file fd, void* buf,
uv__fs_after, req);

if (!req->eio) {
uv_err_new(loop, ENOMEM);
uv__set_sys_error(loop, ENOMEM);
return -1;
}

@@ -235,7 +235,7 @@ int uv_fs_read(uv_loop_t* loop, uv_fs_t* req, uv_file fd, void* buf,
pread(fd, buf, length, offset);

if (req->result < 0) {
uv_err_new(loop, errno);
uv__set_sys_error(loop, errno);
return -1;
}

@@ -261,7 +261,7 @@ int uv_fs_write(uv_loop_t* loop, uv_fs_t* req, uv_file file, void* buf,
req->eio = eio_write(file, buf, length, offset, EIO_PRI_DEFAULT,
uv__fs_after, req);
if (!req->eio) {
uv_err_new(loop, ENOMEM);
uv__set_sys_error(loop, ENOMEM);
return -1;
}

@@ -272,7 +272,7 @@ int uv_fs_write(uv_loop_t* loop, uv_fs_t* req, uv_file file, void* buf,
pwrite(file, buf, length, offset);

if (req->result < 0) {
uv_err_new(loop, errno);
uv__set_sys_error(loop, errno);
return -1;
}

@@ -308,15 +308,15 @@ int uv_fs_readdir(uv_loop_t* loop, uv_fs_t* req, const char* path, int flags,
uv_ref(loop);
req->eio = eio_readdir(path, flags, EIO_PRI_DEFAULT, uv__fs_after, req);
if (!req->eio) {
uv_err_new(loop, ENOMEM);
uv__set_sys_error(loop, ENOMEM);
return -1;
}

} else {
/* sync */
DIR* dir = opendir(path);
if (!dir) {
uv_err_new(loop, errno);
uv__set_sys_error(loop, errno);
req->result = -1;
return -1;
}
@@ -345,7 +345,7 @@ int uv_fs_readdir(uv_loop_t* loop, uv_fs_t* req, const char* path, int flags,

r = closedir(dir);
if (r) {
uv_err_new(loop, errno);
uv__set_sys_error(loop, errno);
req->result = -1;
return -1;
}
@@ -381,7 +381,7 @@ int uv_fs_stat(uv_loop_t* loop, uv_fs_t* req, const char* path, uv_fs_cb cb) {
free(pathdup);

if (!req->eio) {
uv_err_new(loop, ENOMEM);
uv__set_sys_error(loop, ENOMEM);
return -1;
}

@@ -392,7 +392,7 @@ int uv_fs_stat(uv_loop_t* loop, uv_fs_t* req, const char* path, uv_fs_cb cb) {
free(pathdup);

if (req->result < 0) {
uv_err_new(loop, errno);
uv__set_sys_error(loop, errno);
return -1;
}

@@ -413,7 +413,7 @@ int uv_fs_fstat(uv_loop_t* loop, uv_fs_t* req, uv_file file, uv_fs_cb cb) {
req->eio = eio_fstat(file, EIO_PRI_DEFAULT, uv__fs_after, req);

if (!req->eio) {
uv_err_new(loop, ENOMEM);
uv__set_sys_error(loop, ENOMEM);
return -1;
}

@@ -422,7 +422,7 @@ int uv_fs_fstat(uv_loop_t* loop, uv_fs_t* req, uv_file file, uv_fs_cb cb) {
req->result = fstat(file, &req->statbuf);

if (req->result < 0) {
uv_err_new(loop, errno);
uv__set_sys_error(loop, errno);
return -1;
}

@@ -520,7 +520,7 @@ int uv_fs_futime(uv_loop_t* loop, uv_fs_t* req, uv_file file, double atime,

WRAP_EIO(UV_FS_FUTIME, eio_futime, _futime, ARGS3(file, atime, mtime))
#else
uv_err_new(loop, ENOSYS);
uv__set_sys_error(loop, ENOSYS);
return -1;
#endif
}
@@ -550,7 +550,7 @@ int uv_fs_lstat(uv_loop_t* loop, uv_fs_t* req, const char* path, uv_fs_cb cb) {
free(pathdup);

if (!req->eio) {
uv_err_new(loop, ENOMEM);
uv__set_sys_error(loop, ENOMEM);
return -1;
}

@@ -561,7 +561,7 @@ int uv_fs_lstat(uv_loop_t* loop, uv_fs_t* req, const char* path, uv_fs_cb cb) {
free(pathdup);

if (req->result < 0) {
uv_err_new(loop, errno);
uv__set_sys_error(loop, errno);
return -1;
}

@@ -600,7 +600,7 @@ int uv_fs_readlink(uv_loop_t* loop, uv_fs_t* req, const char* path,
uv_ref(loop);
return 0;
} else {
uv_err_new(loop, ENOMEM);
uv__set_sys_error(loop, ENOMEM);
return -1;
}
} else {
@@ -616,7 +616,7 @@ int uv_fs_readlink(uv_loop_t* loop, uv_fs_t* req, const char* path,
}

if ((buf = malloc(size + 1)) == NULL) {
uv_err_new(loop, ENOMEM);
uv__set_sys_error(loop, ENOMEM);
return -1;
}

@@ -693,7 +693,7 @@ int uv_queue_work(uv_loop_t* loop, uv_work_t* req, uv_work_cb work_cb,
req->eio = eio_custom(uv__work, EIO_PRI_DEFAULT, uv__after_work, req);

if (!req->eio) {
uv_err_new(loop, ENOMEM);
uv__set_sys_error(loop, ENOMEM);
return -1;
}

@@ -96,8 +96,6 @@ int uv__socket(int domain, int type, int protocol);

/* error */
uv_err_code uv_translate_sys_error(int sys_errno);
uv_err_t uv_err_new(uv_loop_t* loop, int sys_error);
uv_err_t uv_err_new_artificial(uv_loop_t* loop, int code);
void uv_fatal_error(const int errorno, const char* syscall);

/* stream */
@@ -145,7 +145,7 @@ int uv_fs_event_init(uv_loop_t* loop,
* keep creating new inotify fds.
*/
if ((fd = new_inotify_fd()) == -1) {
uv_err_new(loop, errno);
uv__set_sys_error(loop, errno);
return -1;
}

@@ -158,7 +158,7 @@ int uv_fs_event_init(uv_loop_t* loop,
| IN_MOVED_TO;

if (inotify_add_watch(fd, filename, flags) == -1) {
uv_err_new(loop, errno);
uv__set_sys_error(loop, errno);
uv__close(fd);
return -1;
}
@@ -75,7 +75,7 @@ int uv_fs_event_init(uv_loop_t* loop,
uv_fs_event_t* handle,
const char* filename,
uv_fs_event_cb cb) {
uv_err_new(loop, ENOSYS);
uv__set_sys_error(loop, ENOSYS);
return -1;
}

@@ -53,21 +53,21 @@ int uv_pipe_bind(uv_pipe_t* handle, const char* name) {

/* Already bound? */
if (handle->fd >= 0) {
uv_err_new_artificial(handle->loop, UV_EINVAL);
uv__set_artificial_error(handle->loop, UV_EINVAL);
goto out;
}

/* Make a copy of the file name, it outlives this function's scope. */
if ((pipe_fname = strdup(name)) == NULL) {
uv_err_new(handle->loop, ENOMEM);
uv__set_sys_error(handle->loop, ENOMEM);
goto out;
}

/* We've got a copy, don't touch the original any more. */
name = NULL;

if ((sockfd = uv__socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
uv_err_new(handle->loop, errno);
uv__set_sys_error(handle->loop, errno);
goto out;
}

@@ -88,7 +88,7 @@ int uv_pipe_bind(uv_pipe_t* handle, const char* name) {
|| unlink(pipe_fname) == -1
|| bind(sockfd, (struct sockaddr*)&saddr, sizeof saddr) == -1) {
/* Convert ENOENT to EACCES for compatibility with Windows. */
uv_err_new(handle->loop, (errno == ENOENT) ? EACCES : errno);
uv__set_sys_error(handle->loop, (errno == ENOENT) ? EACCES : errno);
goto out;
}
}
@@ -125,13 +125,13 @@ int uv_pipe_listen(uv_pipe_t* handle, int backlog, uv_connection_cb cb) {
status = -1;

if (handle->fd == -1) {
uv_err_new_artificial(handle->loop, UV_EINVAL);
uv__set_artificial_error(handle->loop, UV_EINVAL);
goto out;
}
assert(handle->fd >= 0);

if ((status = listen(handle->fd, backlog)) == -1) {
uv_err_new(handle->loop, errno);
uv__set_sys_error(handle->loop, errno);
} else {
handle->connection_cb = cb;
ev_io_init(&handle->read_watcher, uv__pipe_accept, handle->fd, EV_READ);
@@ -190,7 +190,7 @@ int uv_pipe_connect(uv_connect_t* req,
status = -1;

if ((sockfd = uv__socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
uv_err_new(handle->loop, errno);
uv__set_sys_error(handle->loop, errno);
goto out;
}

@@ -207,7 +207,7 @@ int uv_pipe_connect(uv_connect_t* req,
while (r == -1 && errno == EINTR);

if (r == -1) {
uv_err_new(handle->loop, errno);
uv__set_sys_error(handle->loop, errno);
uv__close(sockfd);
goto out;
}
@@ -257,7 +257,7 @@ void uv__pipe_accept(EV_P_ ev_io* watcher, int revents) {
if (errno == EAGAIN || errno == EWOULDBLOCK) {
assert(0 && "EAGAIN on uv__accept(pipefd)");
} else {
uv_err_new(pipe->loop, errno);
uv__set_sys_error(pipe->loop, errno);
}
} else {
pipe->accepted_fd = sockfd;

0 comments on commit 23796d2

Please sign in to comment.
You can’t perform that action at this time.