Replace EVUTIL_CLOSESOCKET macro with a function

The EVUTIL_CLOSESOCKET() macro required you to include unistd.h in your
source for POSIX.  We might as well turn it into a function: an extra
function call is going to be cheap in comparison with the system call.

We retain the EVUTIL_CLOSESOCKET() macro as an alias for the new
evutil_closesocket() function.

(commit message from email by Nick and Sebastian)
This commit is contained in:
Sebastian Sjöberg 2010-04-14 15:42:57 -04:00
parent 0861d1708b
commit 899c1dcc98
20 changed files with 69 additions and 62 deletions

View File

@ -285,7 +285,7 @@ be_async_destruct(struct bufferevent *bev)
/* delete this in case non-blocking connect was used */
event_del(&bev->ev_write);
if (bev_p->options & BEV_OPT_CLOSE_ON_FREE)
EVUTIL_CLOSESOCKET(fd);
evutil_closesocket(fd);
_bufferevent_del_generic_timeout_cbs(bev);
}

View File

@ -419,7 +419,7 @@ bufferevent_socket_connect(struct bufferevent *bev,
freesock:
_bufferevent_run_eventcb(bev, BEV_EVENT_ERROR);
if (ownfd)
EVUTIL_CLOSESOCKET(fd);
evutil_closesocket(fd);
/* do something about the error? */
done:
_bufferevent_decref_and_unlock(bev);

10
evdns.c
View File

@ -402,8 +402,6 @@ static int strtoint(const char *const str);
EVLOCK_ASSERT_LOCKED((base)->lock)
#endif
#define CLOSE_SOCKET(s) EVUTIL_CLOSESOCKET(s)
static const char *
debug_ntoa(u32 address)
{
@ -2003,7 +2001,7 @@ server_port_free(struct evdns_server_port *port)
EVUTIL_ASSERT(!port->refcnt);
EVUTIL_ASSERT(!port->pending_replies);
if (port->socket > 0) {
CLOSE_SOCKET(port->socket);
evutil_closesocket(port->socket);
port->socket = -1;
}
(void) event_del(&port->event);
@ -2257,7 +2255,7 @@ evdns_base_clear_nameservers_and_suspend(struct evdns_base *base)
if (evtimer_initialized(&server->timeout_event))
(void) evtimer_del(&server->timeout_event);
if (server->socket >= 0)
CLOSE_SOCKET(server->socket);
evutil_closesocket(server->socket);
mm_free(server);
if (next == started_at)
break;
@ -2394,7 +2392,7 @@ _evdns_nameserver_add_impl(struct evdns_base *base, const struct sockaddr *addre
return 0;
out2:
CLOSE_SOCKET(ns->socket);
evutil_closesocket(ns->socket);
out1:
event_debug_unassign(&ns->event);
mm_free(ns);
@ -3715,7 +3713,7 @@ static void
evdns_nameserver_free(struct nameserver *server)
{
if (server->socket >= 0)
CLOSE_SOCKET(server->socket);
evutil_closesocket(server->socket);
(void) event_del(&server->event);
event_debug_unassign(&server->event);
if (server->state == 0)

View File

@ -207,7 +207,7 @@ evutil_socketpair(int family, int type, int protocol, evutil_socket_t fd[2])
goto tidy_up_and_fail;
if (size != sizeof(listen_addr))
goto abort_tidy_up_and_fail;
EVUTIL_CLOSESOCKET(listener);
evutil_closesocket(listener);
/* Now check we are talking to ourself by matching port and host on the
two sockets. */
if (getsockname(connector, (struct sockaddr *) &connect_addr, &size) == -1)
@ -228,11 +228,11 @@ evutil_socketpair(int family, int type, int protocol, evutil_socket_t fd[2])
if (saved_errno < 0)
saved_errno = WSAGetLastError();
if (listener != -1)
EVUTIL_CLOSESOCKET(listener);
evutil_closesocket(listener);
if (connector != -1)
EVUTIL_CLOSESOCKET(connector);
evutil_closesocket(connector);
if (acceptor != -1)
EVUTIL_CLOSESOCKET(acceptor);
evutil_closesocket(acceptor);
EVUTIL_SET_SOCKET_ERROR(saved_errno);
return -1;
@ -295,6 +295,16 @@ evutil_make_socket_closeonexec(evutil_socket_t fd)
return 0;
}
int
evutil_closesocket(evutil_socket_t sock)
{
#ifndef WIN32
return close(sock);
#else
return closesocket(sock);
#endif
}
ev_int64_t
evutil_strtoll(const char *s, char **endptr, int base)
{
@ -394,7 +404,7 @@ evutil_socket_connect(evutil_socket_t *fd_ptr, struct sockaddr *sa, int socklen)
err:
if (made_fd) {
EVUTIL_CLOSESOCKET(*fd_ptr);
evutil_closesocket(*fd_ptr);
*fd_ptr = -1;
}
return -1;
@ -497,7 +507,7 @@ evutil_check_interfaces(int force_recheck)
}
}
if (fd >= 0)
EVUTIL_CLOSESOCKET(fd);
evutil_closesocket(fd);
if ((fd = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP)) >= 0 &&
connect(fd, (struct sockaddr*)&sin6, sizeof(sin6)) == 0 &&
@ -521,7 +531,7 @@ evutil_check_interfaces(int force_recheck)
}
if (fd >= 0)
EVUTIL_CLOSESOCKET(fd);
evutil_closesocket(fd);
return 0;
}

10
http.c
View File

@ -977,7 +977,7 @@ evhttp_connection_free(struct evhttp_connection *evcon)
bufferevent_free(evcon->bufev);
if (evcon->fd != -1)
EVUTIL_CLOSESOCKET(evcon->fd);
evutil_closesocket(evcon->fd);
if (evcon->bind_address != NULL)
mm_free(evcon->bind_address);
@ -1043,7 +1043,7 @@ evhttp_connection_reset(struct evhttp_connection *evcon)
if (evhttp_connected(evcon) && evcon->closecb != NULL)
(*evcon->closecb)(evcon, evcon->closecb_arg);
EVUTIL_CLOSESOCKET(evcon->fd);
evutil_closesocket(evcon->fd);
evcon->fd = -1;
}
@ -2518,7 +2518,7 @@ evhttp_bind_socket_with_handle(struct evhttp *http, const char *address, ev_uint
if (listen(fd, 128) == -1) {
event_sock_warn(fd, "%s: listen", __func__);
EVUTIL_CLOSESOCKET(fd);
evutil_closesocket(fd);
return (NULL);
}
@ -3008,7 +3008,7 @@ evhttp_get_request(struct evhttp *http, evutil_socket_t fd,
evcon = evhttp_get_request_connection(http, fd, sa, salen);
if (evcon == NULL) {
event_sock_warn(fd, "%s: cannot get connection on %d", __func__, fd);
EVUTIL_CLOSESOCKET(fd);
evutil_closesocket(fd);
return;
}
@ -3104,7 +3104,7 @@ bind_socket_ai(struct evutil_addrinfo *ai, int reuse)
out:
serrno = EVUTIL_SOCKET_ERROR();
EVUTIL_CLOSESOCKET(fd);
evutil_closesocket(fd);
EVUTIL_SET_SOCKET_ERROR(serrno);
return (-1);
}

View File

@ -238,15 +238,14 @@ int evutil_make_listen_socket_reuseable(evutil_socket_t sock);
*/
int evutil_make_socket_closeonexec(evutil_socket_t sock);
#ifdef WIN32
/** Do the platform-specific call needed to close a socket returned from
socket() or accept(). */
#define EVUTIL_CLOSESOCKET(s) closesocket(s)
#else
/** Do the platform-specific call needed to close a socket returned from
socket() or accept(). */
#define EVUTIL_CLOSESOCKET(s) close(s)
#endif
socket() or accept().
@param sock The socket to be closed
@return 0 on success, -1 on failure
*/
int evutil_closesocket(evutil_socket_t sock);
#define EVUTIL_CLOSESOCKET(s) evutil_closesocket(s)
/* Winsock handles socket errors differently from the rest of the world.
* Elsewhere, a socket error is like any other error and is stored in errno.

View File

@ -168,13 +168,13 @@ evconnlistener_new_bind(struct event_base *base, evconnlistener_cb cb,
return NULL;
if (evutil_make_socket_nonblocking(fd) < 0) {
EVUTIL_CLOSESOCKET(fd);
evutil_closesocket(fd);
return NULL;
}
if (flags & LEV_OPT_CLOSE_ON_EXEC) {
if (evutil_make_socket_closeonexec(fd) < 0) {
EVUTIL_CLOSESOCKET(fd);
evutil_closesocket(fd);
return NULL;
}
}
@ -186,14 +186,14 @@ evconnlistener_new_bind(struct event_base *base, evconnlistener_cb cb,
if (sa) {
if (bind(fd, sa, socklen)<0) {
EVUTIL_CLOSESOCKET(fd);
evutil_closesocket(fd);
return NULL;
}
}
listener = evconnlistener_new(base, cb, ptr, flags, backlog, fd);
if (!listener) {
EVUTIL_CLOSESOCKET(fd);
evutil_closesocket(fd);
return NULL;
}
@ -215,7 +215,7 @@ event_listener_destroy(struct evconnlistener *lev)
event_del(&lev_e->listener);
if (lev->flags & LEV_OPT_CLOSE_ON_FREE)
EVUTIL_CLOSESOCKET(event_get_fd(&lev_e->listener));
evutil_closesocket(event_get_fd(&lev_e->listener));
event_debug_unassign(&lev_e->listener);
}

View File

@ -327,11 +327,11 @@ evsig_dealloc(struct event_base *base)
}
if (base->sig.ev_signal_pair[0] != -1) {
EVUTIL_CLOSESOCKET(base->sig.ev_signal_pair[0]);
evutil_closesocket(base->sig.ev_signal_pair[0]);
base->sig.ev_signal_pair[0] = -1;
}
if (base->sig.ev_signal_pair[1] != -1) {
EVUTIL_CLOSESOCKET(base->sig.ev_signal_pair[1]);
evutil_closesocket(base->sig.ev_signal_pair[1]);
base->sig.ev_signal_pair[1] = -1;
}
base->sig.sh_old_max = 0;

View File

@ -2088,7 +2088,7 @@ end:
if (ev[i])
event_free(ev[i]);
if (sock[i] >= 0)
EVUTIL_CLOSESOCKET(sock[i]);
evutil_closesocket(sock[i]);
}
#undef MANY
}

View File

@ -639,8 +639,8 @@ test_evbuffer_add_file(void *ptr)
evbuffer_validate(src);
end:
EVUTIL_CLOSESOCKET(pair[0]);
EVUTIL_CLOSESOCKET(pair[1]);
evutil_closesocket(pair[0]);
evutil_closesocket(pair[1]);
evbuffer_free(src);
}
#endif

View File

@ -541,7 +541,7 @@ close_socket_cb(evutil_socket_t fd, short what, void *arg)
{
evutil_socket_t *fdp = arg;
if (*fdp >= 0) {
EVUTIL_CLOSESOCKET(*fdp);
evutil_closesocket(*fdp);
*fdp = -1;
}
}
@ -603,7 +603,7 @@ test_bufferevent_connect_fail(void *arg)
end:
if (fake_listener >= 0)
EVUTIL_CLOSESOCKET(fake_listener);
evutil_closesocket(fake_listener);
if (bev)
bufferevent_free(bev);

View File

@ -425,7 +425,7 @@ end:
if (port)
evdns_close_server_port(port);
if (sock >= 0)
EVUTIL_CLOSESOCKET(sock);
evutil_closesocket(sock);
if (base)
evdns_base_free(base, 0);
}
@ -1020,7 +1020,7 @@ end:
if (listener)
evconnlistener_free(listener);
if (server_fd>=0)
EVUTIL_CLOSESOCKET(server_fd);
evutil_closesocket(server_fd);
if (port)
evdns_close_server_port(port);
if (dns)

View File

@ -145,8 +145,8 @@ test_edgetriggered(void *et)
}
if (base)
event_base_free(base);
EVUTIL_CLOSESOCKET(pair[0]);
EVUTIL_CLOSESOCKET(pair[1]);
evutil_closesocket(pair[0]);
evutil_closesocket(pair[1]);
}
struct testcase_t edgetriggered_testcases[] = {

View File

@ -364,7 +364,7 @@ http_basic_test(void)
/* connect to the second port */
bufferevent_free(bev);
EVUTIL_CLOSESOCKET(fd);
evutil_closesocket(fd);
fd = http_connect("127.0.0.1", port + 1);
@ -383,7 +383,7 @@ http_basic_test(void)
event_dispatch();
bufferevent_free(bev);
EVUTIL_CLOSESOCKET(fd);
evutil_closesocket(fd);
evhttp_free(http);
@ -529,7 +529,7 @@ http_bad_request_test(void)
event_dispatch();
bufferevent_free(bev);
EVUTIL_CLOSESOCKET(fd);
evutil_closesocket(fd);
if (test_ok != 0) {
fprintf(stdout, "FAILED\n");
@ -636,7 +636,7 @@ http_delete_test(void)
event_dispatch();
bufferevent_free(bev);
EVUTIL_CLOSESOCKET(fd);
evutil_closesocket(fd);
evhttp_free(http);
@ -1463,7 +1463,7 @@ http_failure_test(void)
event_dispatch();
bufferevent_free(bev);
EVUTIL_CLOSESOCKET(fd);
evutil_closesocket(fd);
evhttp_free(http);
@ -1693,7 +1693,7 @@ http_base_test(void)
event_base_dispatch(base);
bufferevent_free(bev);
EVUTIL_CLOSESOCKET(fd);
evutil_closesocket(fd);
evhttp_free(http);
@ -1776,7 +1776,7 @@ _http_incomplete_test(int use_timeout)
bufferevent_free(bev);
if (use_timeout) {
EVUTIL_CLOSESOCKET(fd);
evutil_closesocket(fd);
}
evhttp_free(http);
@ -2387,7 +2387,7 @@ http_multi_line_header_test(void)
if (bev)
bufferevent_free(bev);
if (fd >= 0)
EVUTIL_CLOSESOCKET(fd);
evutil_closesocket(fd);
if (http)
evhttp_free(http);
}
@ -2584,7 +2584,7 @@ terminate_chunked_client(evutil_socket_t fd, short event, void *arg)
{
struct terminate_state *state = arg;
bufferevent_free(state->bev);
EVUTIL_CLOSESOCKET(state->fd);
evutil_closesocket(state->fd);
}
static void
@ -2639,7 +2639,7 @@ http_terminate_chunked_test(void)
end:
if (fd >= 0)
EVUTIL_CLOSESOCKET(fd);
evutil_closesocket(fd);
if (http)
evhttp_free(http);
}

View File

@ -55,7 +55,7 @@ acceptcb(struct evconnlistener *listener, evutil_socket_t fd,
int *ptr = arg;
--*ptr;
TT_BLATHER(("Got one for %p", ptr));
EVUTIL_CLOSESOCKET(fd);
evutil_closesocket(fd);
if (! *ptr)
evconnlistener_disable(listener);

View File

@ -221,9 +221,9 @@ basic_test_cleanup(const struct testcase_t *testcase, void *ptr)
if (testcase->flags & TT_NEED_SOCKETPAIR) {
if (data->pair[0] != -1)
EVUTIL_CLOSESOCKET(data->pair[0]);
evutil_closesocket(data->pair[0]);
if (data->pair[1] != -1)
EVUTIL_CLOSESOCKET(data->pair[1]);
evutil_closesocket(data->pair[1]);
}
if (testcase->flags & TT_NEED_DNS) {

View File

@ -129,7 +129,7 @@ regress_clean_dnsserver(void)
if (dns_port)
evdns_close_server_port(dns_port);
if (dns_sock >= 0)
EVUTIL_CLOSESOCKET(dns_sock);
evutil_closesocket(dns_sock);
}
void

View File

@ -488,7 +488,7 @@ end:
if (logmsg)
free(logmsg);
if (fd >= 0)
EVUTIL_CLOSESOCKET(fd);
evutil_closesocket(fd);
}
static void

View File

@ -313,7 +313,7 @@ end:
bufferevent_free(bev2);
if (pair[0] >= 0)
EVUTIL_CLOSESOCKET(pair[0]);
evutil_closesocket(pair[0]);
if (pair[1] >= 0)
EVUTIL_CLOSESOCKET(pair[1]);
evutil_closesocket(pair[1]);
}

View File

@ -51,7 +51,7 @@ write_cb(evutil_socket_t fd, short event, void *arg)
if (len > 0) {
if (!called)
event_add(arg, NULL);
EVUTIL_CLOSESOCKET(pair[0]);
evutil_closesocket(pair[0]);
} else if (called == 1)
test_okay = 0;