diff --git a/clients/cli/devices.c b/clients/cli/devices.c index e89c0c3747..4321713a0e 100644 --- a/clients/cli/devices.c +++ b/clients/cli/devices.c @@ -1770,62 +1770,10 @@ progress_cb (gpointer user_data) return TRUE; } -static void connected_state_cb (NMDevice *device, NMActiveConnection *active); - -static void -device_state_cb (NMDevice *device, GParamSpec *pspec, gpointer user_data) -{ - NMActiveConnection *active = (NMActiveConnection *) user_data; - - connected_state_cb (device, active); -} - -static void -active_state_cb (NMActiveConnection *active, GParamSpec *pspec, gpointer user_data) -{ - NMDevice *device = (NMDevice *) user_data; - - connected_state_cb (device, active); -} - -static void -connected_state_cb (NMDevice *device, NMActiveConnection *active) -{ - NMDeviceState state; - NMDeviceStateReason reason; - NMActiveConnectionState ac_state; - - state = nm_device_get_state (device); - ac_state = nm_active_connection_get_state (active); - - if (ac_state == NM_ACTIVE_CONNECTION_STATE_ACTIVATING) - return; - - if (state == NM_DEVICE_STATE_ACTIVATED) { - nmc_terminal_erase_line (); - g_print (_("Device '%s' successfully activated with '%s'.\n"), - nm_device_get_iface (device), - nm_active_connection_get_uuid (active)); - } else if ( state <= NM_DEVICE_STATE_DISCONNECTED - || state >= NM_DEVICE_STATE_DEACTIVATING) { - reason = nm_device_get_state_reason (device); - g_print (_("Error: Connection activation failed: (%d) %s.\n"), - reason, gettext (nmc_device_reason_to_string (reason))); - } else - return; - - g_signal_handlers_disconnect_by_func (active, G_CALLBACK (active_state_cb), device); - g_signal_handlers_disconnect_by_func (device, G_CALLBACK (device_state_cb), active); - - g_object_unref (active); - g_object_unref (device); - - quit (); -} - typedef struct { NmCli *nmc; NMDevice *device; + NMActiveConnection *active; char *specific_object; bool hotspot:1; bool create:1; @@ -1855,6 +1803,7 @@ static void add_and_activate_info_free (AddAndActivateInfo *info) { g_object_unref (info->device); + g_clear_object (&info->active); g_free (info->specific_object); nm_g_slice_free (info); } @@ -1862,6 +1811,40 @@ add_and_activate_info_free (AddAndActivateInfo *info) NM_AUTO_DEFINE_FCN0 (AddAndActivateInfo *, _nm_auto_free_add_and_activate_info, add_and_activate_info_free) #define nm_auto_free_add_and_activate_info nm_auto (_nm_auto_free_add_and_activate_info) +static void +connected_state_cb (AddAndActivateInfo *info) +{ + NMDeviceState state; + NMDeviceStateReason reason; + NMActiveConnectionState ac_state; + + state = nm_device_get_state (info->device); + ac_state = nm_active_connection_get_state (info->active); + + if (ac_state == NM_ACTIVE_CONNECTION_STATE_ACTIVATING) + return; + + if (state == NM_DEVICE_STATE_ACTIVATED) { + nmc_terminal_erase_line (); + g_print (_("Device '%s' successfully activated with '%s'.\n"), + nm_device_get_iface (info->device), + nm_active_connection_get_uuid (info->active)); + } else if ( state <= NM_DEVICE_STATE_DISCONNECTED + || state >= NM_DEVICE_STATE_DEACTIVATING) { + reason = nm_device_get_state_reason (info->device); + g_print (_("Error: Connection activation failed: (%d) %s.\n"), + reason, gettext (nmc_device_reason_to_string (reason))); + } else { + return; + } + + g_signal_handlers_disconnect_by_func (info->active, G_CALLBACK (connected_state_cb), info); + g_signal_handlers_disconnect_by_func (info->device, G_CALLBACK (connected_state_cb), info); + add_and_activate_info_free (info); + + quit (); +} + static void add_and_activate_cb (GObject *client, GAsyncResult *result, @@ -1869,7 +1852,6 @@ add_and_activate_cb (GObject *client, { nm_auto_free_add_and_activate_info AddAndActivateInfo *info = user_data; NmCli *nmc = info->nmc; - NMDevice *device = info->device; gs_unref_object NMActiveConnection *active = NULL; gs_free_error GError *error = NULL; @@ -1899,16 +1881,15 @@ add_and_activate_cb (GObject *client, return; } - g_signal_connect (device, "notify::state", G_CALLBACK (device_state_cb), active); - g_signal_connect (active, "notify::state", G_CALLBACK (active_state_cb), device); + if (nmc->nmc_config.print_output == NMC_PRINT_PRETTY) + progress_id = g_timeout_add (120, progress_cb, info->device); - connected_state_cb (g_object_ref (device), - g_steal_pointer (&active)); + info->active = g_steal_pointer (&active); + g_signal_connect_swapped (info->device, "notify::state", G_CALLBACK (connected_state_cb), info); + g_signal_connect_swapped (info->active, "notify::state", G_CALLBACK (connected_state_cb), info); + connected_state_cb (g_steal_pointer (&info)); g_timeout_add_seconds (nmc->timeout, timeout_cb, nmc); /* Exit if timeout expires */ - - if (nmc->nmc_config.print_output == NMC_PRINT_PRETTY) - progress_id = g_timeout_add (120, progress_cb, device); } static void @@ -1940,7 +1921,6 @@ connect_device_cb (GObject *client, GAsyncResult *result, gpointer user_data) nm_auto_free_add_and_activate_info AddAndActivateInfo *info = user_data; NmCli *nmc = info->nmc; gs_unref_object NMActiveConnection *active = NULL; - NMDevice *device = info->device; GError *error = NULL; active = nm_client_activate_connection_finish (NM_CLIENT (client), result, &error); @@ -1975,11 +1955,10 @@ connect_device_cb (GObject *client, GAsyncResult *result, gpointer user_data) nm_connection_get_path (NM_CONNECTION (connection))); } - g_signal_connect (device, "notify::state", G_CALLBACK (device_state_cb), active); - g_signal_connect (active, "notify::state", G_CALLBACK (active_state_cb), device); - - connected_state_cb (g_object_ref (device), - g_steal_pointer (&active)); + info->active = g_steal_pointer (&active); + g_signal_connect_swapped (info->device, "notify::state", G_CALLBACK (connected_state_cb), info); + g_signal_connect_swapped (info->active, "notify::state", G_CALLBACK (connected_state_cb), info); + connected_state_cb (g_steal_pointer (&info)); /* Start timer not to loop forever if "notify::state" signal is not issued */ g_timeout_add_seconds (nmc->timeout, timeout_cb, nmc);