From: Nick Mathewson Date: Wed, 18 Oct 2017 16:14:30 +0000 (-0400) Subject: New function for Tor to treat itself as the "owner" of a socket X-Git-Tag: tor-0.3.3.1-alpha~272^2~2 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=4eb5753bd29b24fd5a523499add35a6214293cd9;p=thirdparty%2Ftor.git New function for Tor to treat itself as the "owner" of a socket Our socket accounting functions assumed that we'd never be asked to close a socket that we didn't open ourselves. But now we want to support taking control sockets that we inherit -- so we need a way of taking ownership of them, so we don't freak out later on when we close them. --- diff --git a/src/common/compat.c b/src/common/compat.c index 97eab94e04..38693b21fe 100644 --- a/src/common/compat.c +++ b/src/common/compat.c @@ -1273,11 +1273,22 @@ tor_open_socket_with_extensions(int domain, int type, int protocol, goto socket_ok; /* So that socket_ok will not be unused. */ socket_ok: + tor_take_socket_ownership(s); + return s; +} + +/** + * For socket accounting: remember that we are the owner of the socket + * s. This will prevent us from overallocating sockets, and prevent us + * from asserting later when we close the socket s. + */ +void +tor_take_socket_ownership(tor_socket_t s) +{ socket_accounting_lock(); ++n_sockets_open; mark_socket_open(s); socket_accounting_unlock(); - return s; } /** As accept(), but counts the number of open sockets. */ @@ -1358,10 +1369,7 @@ tor_accept_socket_with_extensions(tor_socket_t sockfd, struct sockaddr *addr, goto socket_ok; /* So that socket_ok will not be unused. */ socket_ok: - socket_accounting_lock(); - ++n_sockets_open; - mark_socket_open(s); - socket_accounting_unlock(); + tor_take_socket_ownership(s); return s; } diff --git a/src/common/compat.h b/src/common/compat.h index fee9e6587d..0aabee68c8 100644 --- a/src/common/compat.h +++ b/src/common/compat.h @@ -483,6 +483,7 @@ typedef int socklen_t; int tor_close_socket_simple(tor_socket_t s); MOCK_DECL(int, tor_close_socket, (tor_socket_t s)); +void tor_take_socket_ownership(tor_socket_t s); tor_socket_t tor_open_socket_with_extensions( int domain, int type, int protocol, int cloexec, int nonblock);