From: Sebastian Sjöberg Date: Wed, 14 Apr 2010 19:42:57 +0000 (-0400) Subject: Replace EVUTIL_CLOSESOCKET macro with a function X-Git-Tag: release-2.0.5-beta~48 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=899c1dcc9807650ec90e0d5c847d2997fce9b751;p=libevent 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) --- diff --git a/bufferevent_async.c b/bufferevent_async.c index 480f44a7..9079f7ea 100644 --- a/bufferevent_async.c +++ b/bufferevent_async.c @@ -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); } diff --git a/bufferevent_sock.c b/bufferevent_sock.c index 4f645551..f621e76d 100644 --- a/bufferevent_sock.c +++ b/bufferevent_sock.c @@ -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); diff --git a/evdns.c b/evdns.c index 88d6fa37..1635ab26 100644 --- a/evdns.c +++ b/evdns.c @@ -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) diff --git a/evutil.c b/evutil.c index b75a4697..2509f175 100644 --- a/evutil.c +++ b/evutil.c @@ -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; } diff --git a/http.c b/http.c index 72c4cc18..c62ca10c 100644 --- a/http.c +++ b/http.c @@ -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); } diff --git a/include/event2/util.h b/include/event2/util.h index cc18af1e..322d3dd0 100644 --- a/include/event2/util.h +++ b/include/event2/util.h @@ -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. diff --git a/listener.c b/listener.c index f25b775c..c0530821 100644 --- a/listener.c +++ b/listener.c @@ -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); } diff --git a/signal.c b/signal.c index d48e2d37..be02f9d5 100644 --- a/signal.c +++ b/signal.c @@ -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; diff --git a/test/regress.c b/test/regress.c index 5ad1342e..513c3c87 100644 --- a/test/regress.c +++ b/test/regress.c @@ -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 } diff --git a/test/regress_buffer.c b/test/regress_buffer.c index 89495915..291eee6e 100644 --- a/test/regress_buffer.c +++ b/test/regress_buffer.c @@ -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 diff --git a/test/regress_bufferevent.c b/test/regress_bufferevent.c index 49ec004b..ec9c0512 100644 --- a/test/regress_bufferevent.c +++ b/test/regress_bufferevent.c @@ -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); diff --git a/test/regress_dns.c b/test/regress_dns.c index 2ffd94b8..4098ee3e 100644 --- a/test/regress_dns.c +++ b/test/regress_dns.c @@ -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) diff --git a/test/regress_et.c b/test/regress_et.c index ba932564..98f3dcf2 100644 --- a/test/regress_et.c +++ b/test/regress_et.c @@ -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[] = { diff --git a/test/regress_http.c b/test/regress_http.c index e2c8078e..dd176c5c 100644 --- a/test/regress_http.c +++ b/test/regress_http.c @@ -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); } diff --git a/test/regress_listener.c b/test/regress_listener.c index b2e61468..5a097030 100644 --- a/test/regress_listener.c +++ b/test/regress_listener.c @@ -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); diff --git a/test/regress_main.c b/test/regress_main.c index 48da6273..f9544b6c 100644 --- a/test/regress_main.c +++ b/test/regress_main.c @@ -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) { diff --git a/test/regress_testutils.c b/test/regress_testutils.c index 1dd0fcdf..bb174833 100644 --- a/test/regress_testutils.c +++ b/test/regress_testutils.c @@ -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 diff --git a/test/regress_util.c b/test/regress_util.c index 0455145b..9b30d564 100644 --- a/test/regress_util.c +++ b/test/regress_util.c @@ -488,7 +488,7 @@ end: if (logmsg) free(logmsg); if (fd >= 0) - EVUTIL_CLOSESOCKET(fd); + evutil_closesocket(fd); } static void diff --git a/test/regress_zlib.c b/test/regress_zlib.c index a301eaf3..5ef78ea9 100644 --- a/test/regress_zlib.c +++ b/test/regress_zlib.c @@ -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]); } diff --git a/test/test-weof.c b/test/test-weof.c index f0da92b7..e5ff99d2 100644 --- a/test/test-weof.c +++ b/test/test-weof.c @@ -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;