From 1d9e91e4a80d9052bba1bdaf708f3ff53abee9a6 Mon Sep 17 00:00:00 2001 From: Simo Piiroinen Date: Wed, 29 Aug 2018 13:01:22 +0300 Subject: [PATCH] [modesetting] Replace questionable async timer with sleep loop. JB#42757 In case of network setup failure usb-moded utilizes glib timer to retry the operation once once after 3 second delay. How well that has worked is a bit questionable as the dynamic mode setup continues with things that probably require functional network. Also, while it used to be guaranteed that the timer would not trigger before the synchronous mode switch operations have finished, this is no longer guaranteed due to the use of worker thread for the mode switch. Use retry three times with one second delay between retries instead of relying on a three second asynchronous timer with hazardous triggering time and context. Signed-off-by: Simo Piiroinen --- src/usb_moded-modesetting.c | 43 ++++++++++--------------------------- 1 file changed, 11 insertions(+), 32 deletions(-) diff --git a/src/usb_moded-modesetting.c b/src/usb_moded-modesetting.c index 1126d7b..2bd078d 100644 --- a/src/usb_moded-modesetting.c +++ b/src/usb_moded-modesetting.c @@ -62,8 +62,6 @@ static char *modesetting_strip (char *str); static char *modesetting_read_from_file (const char *path, size_t maxsize); int modesetting_write_to_file_real (const char *file, int line, const char *func, const char *path, const char *text); -static gboolean modesetting_network_retry_cb (gpointer data); - static bool modesetting_enter_mass_storage_mode (mode_list_elem_t *data); static int modesetting_leave_mass_storage_mode (mode_list_elem_t *data); static void modesetting_report_mass_storage_blocker(const char *mountpoint, int try); @@ -79,7 +77,6 @@ void modesetting_quit (void); * ========================================================================= */ static GHashTable *tracked_values = 0; -static guint modesetting_network_retry_id = 0; /* ========================================================================= * * Functions @@ -273,13 +270,6 @@ int modesetting_write_to_file_real(const char *file, int line, const char *func, return err; } -static gboolean modesetting_network_retry_cb(gpointer data) -{ - modesetting_network_retry_id = 0; - network_up(data); - return(FALSE); -} - #include typedef struct storage_info_t @@ -686,7 +676,6 @@ bool modesetting_enter_dynamic_mode(void) bool ack = false; mode_list_elem_t *data; - int network = 1; log_debug("DYNAMIC MODE: SETUP"); @@ -780,20 +769,20 @@ bool modesetting_enter_dynamic_mode(void) common_system(command); #else network_down(data); - network = network_up(data); + int error = network_up(data); + + /* In case of failure, retry upto 3 times */ + for( int i = 0; error && i < 3; ++i ) { + log_warning("Retry setting up the network"); + common_msleep(1000); + if( !(error = network_up(data)) ) + log_warning("Setting up the network succeeded"); + } + if( error ) + log_err("Setting up the network failed"); #endif /* DEBIAN */ } - /* try a second time to bring up the network if it failed the first time, - * this can happen with functionfs based gadgets (which is why we sleep for a bit */ - if(network != 0 && data->network) - { - log_debug("Retry setting up the network later\n"); - if(modesetting_network_retry_id) - g_source_remove(modesetting_network_retry_id); - modesetting_network_retry_id = g_timeout_add_seconds(3, modesetting_network_retry_cb, data); - } - /* Needs to be called before application post synching so * that the dhcp server has the right config */ if(data->nat || data->dhcp_server) @@ -839,16 +828,6 @@ void modesetting_leave_dynamic_mode(void) data = worker_get_usb_mode_data(); - /* - - - - - - - - - - - - - - - - - - - * - * Do not leave timers behind - * - - - - - - - - - - - - - - - - - - - */ - - if(modesetting_network_retry_id) - { - g_source_remove(modesetting_network_retry_id); - modesetting_network_retry_id = 0; - } - /* - - - - - - - - - - - - - - - - - - - * * Is a dynamic mode? * - - - - - - - - - - - - - - - - - - - */