From 5610dd7af4cca92df8f8fff4aeb6189146dec93e Mon Sep 17 00:00:00 2001 From: "Roger A. Light" Date: Tue, 18 Sep 2018 12:08:49 +0100 Subject: [PATCH] Better implementation of #948. --- src/mosquitto.c | 3 +-- src/mosquitto_broker_internal.h | 2 +- src/net.c | 13 +++++++++++-- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/mosquitto.c b/src/mosquitto.c index 4f2f32e0..2f1ae403 100644 --- a/src/mosquitto.c +++ b/src/mosquitto.c @@ -238,8 +238,7 @@ int main(int argc, char *argv[]) memset(&int_db, 0, sizeof(struct mosquitto_db)); - net__init(); - int_db.spare_sock = socket(AF_INET, SOCK_STREAM, 0); + net__broker_init(); config__init(&int_db, &config); rc = config__parse_args(&int_db, &config, argc, argv); diff --git a/src/mosquitto_broker_internal.h b/src/mosquitto_broker_internal.h index e41a80dd..1fd2784e 100644 --- a/src/mosquitto_broker_internal.h +++ b/src/mosquitto_broker_internal.h @@ -392,7 +392,6 @@ struct mosquitto_db{ #ifdef WITH_EPOLL int epollfd; #endif - mosq_sock_t spare_sock; }; enum mosquitto__bridge_direction{ @@ -515,6 +514,7 @@ int send__suback(struct mosquitto *context, uint16_t mid, uint32_t payloadlen, c /* ============================================================ * Network functions * ============================================================ */ +void net__broker_init(void); int net__socket_accept(struct mosquitto_db *db, mosq_sock_t listensock); int net__socket_listen(struct mosquitto__listener *listener); int net__socket_get_address(mosq_sock_t sock, char *buf, int len); diff --git a/src/net.c b/src/net.c index 60f9ecc8..9928eba5 100644 --- a/src/net.c +++ b/src/net.c @@ -59,6 +59,15 @@ static int tls_ex_index_listener = -1; #include "sys_tree.h" +/* For EMFILE handling */ +static mosq_sock_t spare_sock = INVALID_SOCKET; + +void net__broker_init(void) +{ + spare_sock = socket(AF_INET, SOCK_STREAM, 0); + net__init(); +} + static void net__print_error(int log, const char *format_str) { @@ -111,12 +120,12 @@ int net__socket_accept(struct mosquitto_db *db, mosq_sock_t listensock) * It would be nice to send a "server not available" connack here, * but there are lots of reasons why this would be tricky (TLS * being the big one). */ - COMPAT_CLOSE(db->spare_sock); + COMPAT_CLOSE(spare_sock); new_sock = accept(listensock, NULL, 0); if(new_sock != INVALID_SOCKET){ COMPAT_CLOSE(new_sock); } - db->spare_sock = socket(AF_INET, SOCK_STREAM, 0); + spare_sock = socket(AF_INET, SOCK_STREAM, 0); log__printf(NULL, MOSQ_LOG_NOTICE, "Unable to accept new connection, system socket count has been exceeded. Try increasing \"ulimit -n\" or equivalent."); }