]> granicus.if.org Git - libevent/commitdiff
Replace EVUTIL_CLOSESOCKET macro with a function
authorSebastian Sjöberg <Sebastian.Sjoberg@axis.com>
Wed, 14 Apr 2010 19:42:57 +0000 (15:42 -0400)
committerSebastian Sjöberg <Sebastian.Sjoberg@axis.com>
Wed, 14 Apr 2010 19:42:57 +0000 (15:42 -0400)
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)

20 files changed:
bufferevent_async.c
bufferevent_sock.c
evdns.c
evutil.c
http.c
include/event2/util.h
listener.c
signal.c
test/regress.c
test/regress_buffer.c
test/regress_bufferevent.c
test/regress_dns.c
test/regress_et.c
test/regress_http.c
test/regress_listener.c
test/regress_main.c
test/regress_testutils.c
test/regress_util.c
test/regress_zlib.c
test/test-weof.c

index 480f44a71cad4c6b29d0179bf235a31ac1812d4b..9079f7ead3f09d85b7ccc35d2540dc505d6deb07 100644 (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);
 }
 
index 4f64555199bf33e795323d5874edad7b3bb0eaeb..f621e76dacdde4034b12364d41d059783ecd9cf0 100644 (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);
diff --git a/evdns.c b/evdns.c
index 88d6fa378e9677dd736331bbd296cc6050700905..1635ab261c087ab780c4748027b80f59023f5662 100644 (file)
--- 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)
index b75a4697178b1c2d259bb2e8adfba5ee967a4648..2509f17525ac19bf40a695f9eccca8d633fb78a1 100644 (file)
--- 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 72c4cc18bddce76dd5203642ffc1109b82f41981..c62ca10c9e946d02fdd1b7b4c080625529f54de4 100644 (file)
--- 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);
 }
index cc18af1e25206759dda83e9bbfbfccddd7f0a00d..322d3dd08f71b7d831f31a45d4764f508db009ed 100644 (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.
index f25b775c06e61d1cd2415de74183eadff1e2e4ef..c053082127c25d3ee7bb06ea4b8a6f56d3ef974f 100644 (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);
 }
 
index d48e2d377e82905067f4b2abc10d1e4aba0d2028..be02f9d50b9edc83195b0579c1991321b12a1f93 100644 (file)
--- 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;
index 5ad1342ea8aa5379ccf738f5a483c7899fd762fe..513c3c879925cb2ca50712679cc18dff6c48681b 100644 (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
 }
index 89495915ad7819fd7d5b8f6c51e948611dbc1687..291eee6e5389b9b6f27f7f6674e510bf0944e33f 100644 (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
index 49ec004b611826b50f8c4ff76a9424a844a5cd01..ec9c05125f259a931624d340ddb17c9d89959b74 100644 (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);
index 2ffd94b80004f5b9bbc6ef9bde5919cf5c23ec37..4098ee3e4470700dba04a1eb8a77e95b52151ba9 100644 (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)
index ba9325649063b97a698591c2a2dbfb164077df13..98f3dcf23777d35c20096c8c3ac50b4e6c422cfe 100644 (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[] = {
index e2c8078eea27059e9acfd22418aba539b21cf576..dd176c5ce2cf0efecb3af972f3232f4bc02ec928 100644 (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);
 }
index b2e61468e7ec58033dbeb9013bc1ec2648c7381c..5a097030751a9733291588c9a7b10280f7592ea9 100644 (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);
index 48da62739e8f12d76a0b97d485c3bbe845406878..f9544b6c941fa0827f03afe796902c3d0be92dab 100644 (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) {
index 1dd0fcdf800ea2e87e95bd8fc4388db211e47f81..bb174833f6365c85dbf41e924e4d3f9877a7b3d4 100644 (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
index 0455145b72f36dc2d37a5dd018ed568498855f54..9b30d5646cff2ec4df6818319abc74b104b5fe47 100644 (file)
@@ -488,7 +488,7 @@ end:
        if (logmsg)
                free(logmsg);
        if (fd >= 0)
-               EVUTIL_CLOSESOCKET(fd);
+               evutil_closesocket(fd);
 }
 
 static void
index a301eaf3cbf072fc1f90a6b469bf2fa26a9f80e5..5ef78ea96e74a44238005003133ac3e54d639a13 100644 (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]);
 }
index f0da92b7d49528e6eeeb8f94d191732044e35c73..e5ff99d2010d052b288e371abe07471907f44467 100644 (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;