summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Haller <thaller@redhat.com>2019-08-05 10:10:37 +0200
committerThomas Haller <thaller@redhat.com>2019-08-05 10:11:01 +0200
commitd4fabd728a898b2dfee1cac5592c082bee6b4e5a (patch)
tree4bdb893ea5cdf0398624bdaf651df0f3b4ce8c84
parent956ffb7e9602f7d53e5eb693d1b0155913de45bc (diff)
parentb298f2e6058a5453c73a5a19c273b17b5b4b91b1 (diff)
downloadNetworkManager-d4fabd728a898b2dfee1cac5592c082bee6b4e5a.tar.gz
cli: merge branch 'th/nmcli-add-and-activate-cleanup'
https://gitlab.freedesktop.org/NetworkManager/NetworkManager/merge_requests/227
-rw-r--r--clients/cli/devices.c158
1 files changed, 83 insertions, 75 deletions
diff --git a/clients/cli/devices.c b/clients/cli/devices.c
index 1e199d8af7..8354fcac75 100644
--- a/clients/cli/devices.c
+++ b/clients/cli/devices.c
@@ -1836,29 +1836,52 @@ connected_state_cb (NMDevice *device, NMActiveConnection *active)
typedef struct {
NmCli *nmc;
NMDevice *device;
- gboolean hotspot;
- gboolean create;
char *specific_object;
+ bool hotspot:1;
+ bool create:1;
} AddAndActivateInfo;
+static AddAndActivateInfo *
+add_and_activate_info_new (NmCli *nmc,
+ NMDevice *device,
+ gboolean hotspot,
+ gboolean create,
+ const char *specific_object)
+{
+ AddAndActivateInfo *info;
+
+ info = g_slice_new (AddAndActivateInfo);
+ *info = (AddAndActivateInfo) {
+ .nmc = nmc,
+ .device = g_object_ref (device),
+ .hotspot = hotspot,
+ .create = create,
+ .specific_object = g_strdup (specific_object),
+ };
+ return info;
+}
+
static void
add_and_activate_info_free (AddAndActivateInfo *info)
{
g_object_unref (info->device);
g_free (info->specific_object);
- g_free (info);
+ nm_g_slice_free (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
add_and_activate_cb (GObject *client,
GAsyncResult *result,
gpointer user_data)
{
- AddAndActivateInfo *info = (AddAndActivateInfo *) user_data;
+ nm_auto_free_add_and_activate_info AddAndActivateInfo *info = user_data;
NmCli *nmc = info->nmc;
NMDevice *device = info->device;
- NMActiveConnection *active;
- GError *error = NULL;
+ gs_unref_object NMActiveConnection *active = NULL;
+ gs_free_error GError *error = NULL;
if (info->create)
active = nm_client_add_and_activate_connection_finish (NM_CLIENT (client), result, &error);
@@ -1866,37 +1889,36 @@ add_and_activate_cb (GObject *client,
active = nm_client_activate_connection_finish (NM_CLIENT (client), result, &error);
if (error) {
- if (info->hotspot)
+ if (info->hotspot) {
g_string_printf (nmc->return_text, _("Error: Failed to setup a Wi-Fi hotspot: %s"),
error->message);
- else if (info->create)
+ } else if (info->create) {
g_string_printf (nmc->return_text, _("Error: Failed to add/activate new connection: %s"),
error->message);
- else
+ } else {
g_string_printf (nmc->return_text, _("Error: Failed to activate connection: %s"),
error->message);
- g_error_free (error);
+ }
nmc->return_value = NMC_RESULT_ERROR_CON_ACTIVATION;
quit ();
- } else {
- if (nmc->nowait_flag) {
- g_object_unref (active);
- quit ();
- } else {
- g_object_ref (device);
- g_signal_connect (device, "notify::state", G_CALLBACK (device_state_cb), active);
- g_signal_connect (active, "notify::state", G_CALLBACK (active_state_cb), device);
+ return;
+ }
+
+ if (nmc->nowait_flag) {
+ quit ();
+ return;
+ }
- connected_state_cb (device, active);
+ g_signal_connect (device, "notify::state", G_CALLBACK (device_state_cb), active);
+ g_signal_connect (active, "notify::state", G_CALLBACK (active_state_cb), device);
- g_timeout_add_seconds (nmc->timeout, timeout_cb, nmc); /* Exit if timeout expires */
+ connected_state_cb (g_object_ref (device),
+ g_steal_pointer (&active));
- if (nmc->nmc_config.print_output == NMC_PRINT_PRETTY)
- progress_id = g_timeout_add (120, progress_cb, device);
- }
- }
+ g_timeout_add_seconds (nmc->timeout, timeout_cb, nmc); /* Exit if timeout expires */
- add_and_activate_info_free (info);
+ if (nmc->nmc_config.print_output == NMC_PRINT_PRETTY)
+ progress_id = g_timeout_add (120, progress_cb, device);
}
static void
@@ -1925,9 +1947,9 @@ create_connect_connection_for_device (AddAndActivateInfo *info)
static void
connect_device_cb (GObject *client, GAsyncResult *result, gpointer user_data)
{
- AddAndActivateInfo *info = (AddAndActivateInfo *) user_data;
+ nm_auto_free_add_and_activate_info AddAndActivateInfo *info = user_data;
NmCli *nmc = info->nmc;
- NMActiveConnection *active;
+ gs_unref_object NMActiveConnection *active = NULL;
GError *error = NULL;
const GPtrArray *devices;
NMDevice *device;
@@ -1938,7 +1960,7 @@ connect_device_cb (GObject *client, GAsyncResult *result, gpointer user_data)
/* If no connection existed for the device, create one and activate it */
if (g_error_matches (error, NM_MANAGER_ERROR, NM_MANAGER_ERROR_UNKNOWN_CONNECTION)) {
info->create = TRUE;
- create_connect_connection_for_device (info);
+ create_connect_connection_for_device (g_steal_pointer (&info));
return;
}
@@ -1947,42 +1969,41 @@ connect_device_cb (GObject *client, GAsyncResult *result, gpointer user_data)
g_error_free (error);
nmc->return_value = NMC_RESULT_ERROR_CON_ACTIVATION;
quit ();
- } else {
- g_assert (active);
- devices = nm_active_connection_get_devices (active);
- if (devices->len == 0) {
- g_string_printf (nmc->return_text, _("Error: Device activation failed: device was disconnected"));
- nmc->return_value = NMC_RESULT_ERROR_CON_ACTIVATION;
- g_object_unref (active);
- quit ();
- add_and_activate_info_free (info);
- return;
- }
+ return;
+ }
- device = g_ptr_array_index (devices, 0);
+ nm_assert (NM_IS_ACTIVE_CONNECTION (active));
- if (nmc->nowait_flag) {
- g_object_unref (active);
- quit ();
- } else {
- if (nmc->secret_agent) {
- NMRemoteConnection *connection = nm_active_connection_get_connection (active);
+ devices = nm_active_connection_get_devices (active);
+ if (devices->len == 0) {
+ g_string_printf (nmc->return_text, _("Error: Device activation failed: device was disconnected"));
+ nmc->return_value = NMC_RESULT_ERROR_CON_ACTIVATION;
+ quit ();
+ return;
+ }
- nm_secret_agent_simple_enable (nmc->secret_agent,
- nm_connection_get_path (NM_CONNECTION (connection)));
- }
+ device = g_ptr_array_index (devices, 0);
- g_object_ref (device);
- 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->nowait_flag) {
+ quit ();
+ return;
+ }
- connected_state_cb (device, active);
+ if (nmc->secret_agent) {
+ NMRemoteConnection *connection = nm_active_connection_get_connection (active);
- /* Start timer not to loop forever if "notify::state" signal is not issued */
- g_timeout_add_seconds (nmc->timeout, timeout_cb, nmc);
- }
+ nm_secret_agent_simple_enable (nmc->secret_agent,
+ nm_connection_get_path (NM_CONNECTION (connection)));
}
- add_and_activate_info_free (info);
+
+ 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));
+
+ /* Start timer not to loop forever if "notify::state" signal is not issued */
+ g_timeout_add_seconds (nmc->timeout, timeout_cb, nmc);
}
static NMCResultCode
@@ -2027,10 +2048,7 @@ do_device_connect (NmCli *nmc, int argc, char **argv)
nmc);
}
- info = g_malloc0 (sizeof (AddAndActivateInfo));
- info->nmc = nmc;
- info->device = g_object_ref (device);
- info->hotspot = FALSE;
+ info = add_and_activate_info_new (nmc, device, FALSE, FALSE, NULL);
nm_client_activate_connection_async (nmc->client,
NULL, /* let NM find a connection automatically */
@@ -3131,7 +3149,7 @@ static void
activate_update2_cb (GObject *source_object, GAsyncResult *res, gpointer user_data)
{
NMRemoteConnection *remote_con = NM_REMOTE_CONNECTION (source_object);
- AddAndActivateInfo *info = (AddAndActivateInfo *) user_data;
+ nm_auto_free_add_and_activate_info AddAndActivateInfo *info = user_data;
NmCli *nmc = info->nmc;
gs_unref_variant GVariant *ret = NULL;
GError *error = NULL;
@@ -3143,7 +3161,6 @@ activate_update2_cb (GObject *source_object, GAsyncResult *res, gpointer user_da
nmc->return_value = NMC_RESULT_ERROR_UNKNOWN;
g_error_free (error);
quit ();
- add_and_activate_info_free (info);
return;
}
@@ -3153,7 +3170,7 @@ activate_update2_cb (GObject *source_object, GAsyncResult *res, gpointer user_da
info->specific_object,
NULL,
add_and_activate_cb,
- info);
+ g_steal_pointer (&info));
}
static NMCResultCode
@@ -3560,12 +3577,7 @@ do_device_wifi_connect (NmCli *nmc, int argc, char **argv)
nmc->nowait_flag = (nmc->timeout == 0);
nmc->should_wait++;
- info = g_malloc0 (sizeof (AddAndActivateInfo));
- info->nmc = nmc;
- info->device = g_object_ref (device);
- info->hotspot = FALSE;
- info->create = !remote_con;
- info->specific_object = g_strdup (nm_object_get_path (NM_OBJECT (ap)));
+ info = add_and_activate_info_new (nmc, device, FALSE, !remote_con, nm_object_get_path (NM_OBJECT (ap)));
if (remote_con) {
nm_remote_connection_update2 (remote_con,
@@ -3932,11 +3944,7 @@ do_device_wifi_hotspot (NmCli *nmc, int argc, char **argv)
nmc->nowait_flag = (nmc->timeout == 0);
nmc->should_wait++;
- info = g_malloc0 (sizeof (AddAndActivateInfo));
- info->nmc = nmc;
- info->device = g_object_ref (device);
- info->hotspot = TRUE;
- info->create = TRUE;
+ info = add_and_activate_info_new (nmc, device, TRUE, TRUE, NULL);
nm_client_add_and_activate_connection_async (nmc->client,
connection,