Make errors retrievable from tor_socketpair; resolve bug 163.

svn:r4509
This commit is contained in:
Nick Mathewson 2005-06-30 07:17:38 +00:00
parent 584a0ae397
commit d85bfe83bf
4 changed files with 42 additions and 42 deletions

View File

@ -294,15 +294,17 @@ set_socket_nonblocking(int socket)
* be able to read while localhost is down later (the socket pair may * be able to read while localhost is down later (the socket pair may
* even close, depending on OS-specific timeouts). * even close, depending on OS-specific timeouts).
* *
* XXX Bug: this function assumes errno is how you report errors, but * Returns 0 on success and -errno on failure; do not rely on the value
* that isn't the case for Windows, which is where it's most likely * of errno or WSAGetLastSocketError().
* to be called.
**/ **/
/* It would be nicer just to set errno, but that won't work for windows. */
int int
tor_socketpair(int family, int type, int protocol, int fd[2]) tor_socketpair(int family, int type, int protocol, int fd[2])
{ {
#ifdef HAVE_SOCKETPAIR #ifdef HAVE_SOCKETPAIR
return socketpair(family, type, protocol, fd); int r;
r = socketpair(family, type, protocol, fd);
return r < 0 ? -errno : r;
#else #else
/* This socketpair does not work when localhost is down. So /* This socketpair does not work when localhost is down. So
* it's really not the same thing at all. But it's close enough * it's really not the same thing at all. But it's close enough
@ -315,6 +317,7 @@ tor_socketpair(int family, int type, int protocol, int fd[2])
struct sockaddr_in listen_addr; struct sockaddr_in listen_addr;
struct sockaddr_in connect_addr; struct sockaddr_in connect_addr;
int size; int size;
int saved_errno = -1;
if (protocol if (protocol
#ifdef AF_UNIX #ifdef AF_UNIX
@ -322,24 +325,22 @@ tor_socketpair(int family, int type, int protocol, int fd[2])
#endif #endif
) { ) {
#ifdef MS_WINDOWS #ifdef MS_WINDOWS
errno = WSAEAFNOSUPPORT; return -WSAEAFNOSUPPORT;
#else #else
errno = EAFNOSUPPORT; return -EAFNOSUPPORT;
#endif #endif
return -1;
} }
if (!fd) { if (!fd) {
errno = EINVAL; return -EINVAL;
return -1;
} }
listener = socket(AF_INET, type, 0); listener = socket(AF_INET, type, 0);
if (listener == -1) if (listener == -1)
return -1; return -tor_socket_errno(-1);
if (!SOCKET_IS_POLLABLE(listener)) { if (!SOCKET_IS_POLLABLE(listener)) {
log_fn(LOG_WARN, "Too many connections; can't open socketpair"); log_fn(LOG_WARN, "Too many connections; can't open socketpair");
tor_close_socket(listener); tor_close_socket(listener);
return -1; return -ENCONN;
} }
memset(&listen_addr, 0, sizeof(listen_addr)); memset(&listen_addr, 0, sizeof(listen_addr));
listen_addr.sin_family = AF_INET; listen_addr.sin_family = AF_INET;
@ -396,22 +397,20 @@ tor_socketpair(int family, int type, int protocol, int fd[2])
abort_tidy_up_and_fail: abort_tidy_up_and_fail:
#ifdef MS_WINDOWS #ifdef MS_WINDOWS
errno = WSAECONNABORTED; saved_errno = WSAECONNABORTED;
#else #else
errno = ECONNABORTED; /* I hope this is portable and appropriate. */ saved_errno = ECONNABORTED; /* I hope this is portable and appropriate. */
#endif #endif
tidy_up_and_fail: tidy_up_and_fail:
{ if (saved_errno < 0)
int save_errno = errno; saved_errno = errno;
if (listener != -1) if (listener != -1)
tor_close_socket(listener); tor_close_socket(listener);
if (connector != -1) if (connector != -1)
tor_close_socket(connector); tor_close_socket(connector);
if (acceptor != -1) if (acceptor != -1)
tor_close_socket(acceptor); tor_close_socket(acceptor);
errno = save_errno; return -saved_errno;
return -1;
}
#endif #endif
} }

View File

@ -1207,12 +1207,13 @@ connection_ap_make_bridge(char *address, uint16_t port)
{ {
int fd[2]; int fd[2];
connection_t *conn; connection_t *conn;
int err;
log_fn(LOG_INFO,"Making AP bridge to %s:%d ...",safe_str(address),port); log_fn(LOG_INFO,"Making AP bridge to %s:%d ...",safe_str(address),port);
if (tor_socketpair(AF_UNIX, SOCK_STREAM, 0, fd) < 0) { if ((err = tor_socketpair(AF_UNIX, SOCK_STREAM, 0, fd)) < 0) {
log(LOG_WARN,"Couldn't construct socketpair (%s). Network down? Delaying.", log(LOG_WARN,"Couldn't construct socketpair (%s). Network down? Delaying.",
tor_socket_strerror(tor_socket_errno(-1))); tor_socket_strerror(-err));
return -1; return -1;
} }

View File

@ -301,11 +301,11 @@ spawn_cpuworker(void)
int *fdarray; int *fdarray;
int fd; int fd;
connection_t *conn; connection_t *conn;
int err;
fdarray = tor_malloc(sizeof(int)*2); fdarray = tor_malloc(sizeof(int)*2);
if (tor_socketpair(AF_UNIX, SOCK_STREAM, 0, fdarray) < 0) { if ((err = tor_socketpair(AF_UNIX, SOCK_STREAM, 0, fdarray)) < 0) {
log(LOG_ERR, "Couldn't construct socketpair: %s", log(LOG_ERR, "Couldn't construct socketpair: %s", tor_socket_strerror(-err));
tor_socket_strerror(tor_socket_errno(-1)));
tor_cleanup(); tor_cleanup();
tor_free(fdarray); tor_free(fdarray);
exit(1); exit(1);

View File

@ -824,11 +824,11 @@ spawn_dnsworker(void)
int *fdarray; int *fdarray;
int fd; int fd;
connection_t *conn; connection_t *conn;
int err;
fdarray = tor_malloc(sizeof(int)*2); fdarray = tor_malloc(sizeof(int)*2);
if (tor_socketpair(AF_UNIX, SOCK_STREAM, 0, fdarray) < 0) { if ((err = tor_socketpair(AF_UNIX, SOCK_STREAM, 0, fdarray)) < 0) {
log(LOG_ERR, "Couldn't construct socketpair: %s", log(LOG_ERR, "Couldn't construct socketpair: %s", tor_socket_strerror(-err));
tor_socket_strerror(tor_socket_errno(-1)));
tor_cleanup(); tor_cleanup();
tor_free(fdarray); tor_free(fdarray);
exit(1); exit(1);