From b5bedfe9e4b1e06bbf496e182195d4d11794781b Mon Sep 17 00:00:00 2001 From: Jan Vidar Krey Date: Tue, 29 Jul 2014 17:35:58 +0200 Subject: [PATCH] uhub-admin: don't busy loop before a connection has been established. This happened due to the network polling mechanism had nothing to poll for, so it returned immediately only to be called again (during DNS lookup). This fix introduces a control pipe that is polled for reading, althoug nothing is ever sent to that pipe. But, it can be used instead of the signal handler approach which is currently used for terminating the program. --- src/tools/adcclient.c | 29 ++++++++++++++--------------- src/tools/admin.c | 30 ++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 15 deletions(-) diff --git a/src/tools/adcclient.c b/src/tools/adcclient.c index 1eae4fe..a445c1d 100644 --- a/src/tools/adcclient.c +++ b/src/tools/adcclient.c @@ -600,6 +600,17 @@ int ADC_client_connect(struct ADC_client* client, const char* address) return 1; } +static void ADC_client_send_handshake(struct ADC_client* client) +{ + ADC_TRACE; + struct adc_message* handshake = adc_msg_create(ADC_HANDSHAKE); + + client->callback(client, ADC_CLIENT_CONNECTED, 0); + net_con_update(client->con, NET_EVENT_READ); + ADC_client_send(client, handshake); + ADC_client_set_state(client, ps_protocol); + adc_msg_free(handshake); +} static void ADC_client_on_connected(struct ADC_client* client) { @@ -610,32 +621,20 @@ static void ADC_client_on_connected(struct ADC_client* client) net_con_update(client->con, NET_EVENT_READ | NET_EVENT_WRITE); client->callback(client, ADC_CLIENT_SSL_HANDSHAKE, 0); ADC_client_set_state(client, ps_conn_ssl); - net_con_ssl_handshake(client->con, net_con_ssl_mode_client, NULL); } else #endif - { - struct adc_message* handshake = adc_msg_create(ADC_HANDSHAKE); - net_con_update(client->con, NET_EVENT_READ); - client->callback(client, ADC_CLIENT_CONNECTED, 0); - ADC_client_send(client, handshake); - ADC_client_set_state(client, ps_protocol); - adc_msg_free(handshake); - } + ADC_client_send_handshake(client); + } #ifdef SSL_SUPPORT static void ADC_client_on_connected_ssl(struct ADC_client* client) { ADC_TRACE; - struct adc_message* handshake = adc_msg_create(ADC_HANDSHAKE); client->callback(client, ADC_CLIENT_SSL_OK, 0); - client->callback(client, ADC_CLIENT_CONNECTED, 0); - net_con_update(client->con, NET_EVENT_READ); - ADC_client_send(client, handshake); - ADC_client_set_state(client, ps_protocol); - adc_msg_free(handshake); + ADC_client_send_handshake(client); } #endif diff --git a/src/tools/admin.c b/src/tools/admin.c index f230576..bf114cd 100644 --- a/src/tools/admin.c +++ b/src/tools/admin.c @@ -159,6 +159,9 @@ static int handle(struct ADC_client* client, enum ADC_client_callback_type type, static int running = 1; #if !defined(WIN32) +static int adm_pipes[2] = { -1, -1 }; +static struct net_connection* adm_con = 0; + void adm_handle_signal(int sig) { switch (sig) @@ -195,6 +198,10 @@ static int signals[] = 0 }; +void adm_callback(struct net_connection* con, int event, void* ptr) +{ +} + void adm_setup_signal_handlers() { sigset_t sig_set; @@ -215,6 +222,26 @@ void adm_setup_signal_handlers() } } +void adm_setup_control_pipe() +{ + int ret = pipe(adm_pipes); + if (ret == -1) + { + LOG_ERROR("Unable to setup control pipes."); + } + adm_con = net_con_create(); + net_con_initialize(adm_con, adm_pipes[0], adm_callback, 0, NET_EVENT_READ); +} + +void adm_shutdown_control_pipe() +{ + net_con_destroy(adm_con); + close(adm_pipes[0]); + close(adm_pipes[1]); + adm_pipes[0] = -1; + adm_pipes[0] = -1; +} + void adm_shutdown_signal_handlers() { } @@ -233,6 +260,7 @@ int main(int argc, char** argv) struct ADC_client* client; net_initialize(); + adm_setup_control_pipe(); memset(g_usermap, 0, sizeof(g_usermap)); @@ -242,6 +270,8 @@ int main(int argc, char** argv) while (running && net_backend_process()) { } + adm_shutdown_control_pipe(); + ADC_client_destroy(client); net_destroy(); adm_shutdown_signal_handlers();