Skip to content
This repository has been archived by the owner on May 4, 2018. It is now read-only.

Commit

Permalink
Fixes #76. Unify OS error reporting
Browse files Browse the repository at this point in the history
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 28, 2011
1 parent 1d7e61f commit 23796d2
Show file tree
Hide file tree
Showing 35 changed files with 242 additions and 255 deletions.
2 changes: 1 addition & 1 deletion include/uv-private/uv-win.h
Expand Up @@ -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 }; \
Expand Down
2 changes: 1 addition & 1 deletion src/unix/cares.c
Expand Up @@ -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;
}

Expand Down
6 changes: 3 additions & 3 deletions src/unix/core.c
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion src/unix/cygwin.c
Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion src/unix/darwin.c
Expand Up @@ -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;
}

Expand Down
18 changes: 0 additions & 18 deletions src/unix/error.c
Expand Up @@ -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;
}
2 changes: 1 addition & 1 deletion src/unix/freebsd.c
Expand Up @@ -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;
}

Expand Down
42 changes: 21 additions & 21 deletions src/unix/fs.c
Expand Up @@ -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; \
} \
Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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;
}

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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;
}

Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand Down Expand Up @@ -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 {
Expand All @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down
2 changes: 0 additions & 2 deletions src/unix/internal.h
Expand Up @@ -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 */
Expand Down
4 changes: 2 additions & 2 deletions src/unix/linux.c
Expand Up @@ -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;
}

Expand All @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/unix/netbsd.c
Expand Up @@ -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;
}

Expand Down
18 changes: 9 additions & 9 deletions src/unix/pipe.c
Expand Up @@ -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;
}

Expand All @@ -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;
}
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 23796d2

Please sign in to comment.